如何为遥感数据数组添加日期,构建可筛选的NDVI与云量时间序列?
Hey there! Let's work through this problem together. You’ve got a 3D remote sensing array (30 lat × 40 lon × 212 monthly slices) spanning March 2000 to October 2017, but the ts function left you without proper date formatting to filter rainy seasons. Here’s how to fix this while keeping your spatial (lat/lon) and temporal context intact:
Since you know the exact start and end of your time series, create a proper Date vector that matches each of your 212 slices:
# Create monthly date sequence from March 2000 to October 2017 date_seq <- seq.Date( from = as.Date("2000-03-01"), to = as.Date("2017-10-01"), by = "month" ) # Double-check the length matches your data slices length(date_seq) # Should return 212
You’ve got a few solid options depending on whether you want to keep the array format, use tidy data tools, or leverage spatial-temporal packages designed for remote sensing:
Option A: Add dates as an attribute to your existing array
If you want to stick with the 3D array structure, just attach the date sequence as an attribute. This keeps your spatial dimensions intact while giving you date-based filtering power:
# Assume your original array is named `name.ts.ndvi` attr(name.ts.ndvi, "dates") <- date_seq # Access the dates anytime with attr(name.ts.ndvi, "dates") # Filter for rainy season (example: May–October; adjust months to match your region) rainy_month_indices <- which(format(date_seq, "%m") %in% c("05", "06", "07", "08", "09", "10")) rainy_season_data <- name.ts.ndvi[, , rainy_month_indices]
Option B: Convert to a tidy data frame (great for filtering/visualization)
If you prefer working with tidy data (using dplyr/tidyr), reshape your array into a long-format data frame. This makes season-based filtering straightforward:
library(tidyr) library(dplyr) # Reshape array to a long data frame tidy_ndvi <- as.data.frame.table(name.ts.ndvi) %>% rename(lat = Var1, lon = Var2, time_slice = Var3, ndvi_value = Freq) %>% # Add date, month, and year columns mutate( date = date_seq[as.integer(time_slice)], month = format(date, "%m"), year = format(date, "%Y") ) # Filter for rainy season in one line rainy_season_df <- tidy_ndvi %>% filter(month %in% c("05", "06", "07", "08", "09", "10"))
Option C: Use spatial-temporal packages (ideal for remote sensing)
For remote sensing data, specialized packages like raster or stars are built to handle spatial + temporal data seamlessly. Here’s how to use raster (adjust coordinate bounds to match your actual data):
library(raster) # Convert your array to a RasterBrick (preserves spatial info) # Replace xmn/xmx/ymn/ymx with your actual longitude/latitude bounds ndvi_brick <- brick( name.ts.ndvi, xmn = -180, xmx = 180, # Example bounds; update to your data's range ymn = -90, ymx = 90, crs = "+proj=longlat +datum=WGS84" # Set your correct coordinate system ) # Attach dates to the brick ndvi_brick <- setZ(ndvi_brick, date_seq, name = "date") # Filter rainy season layers rainy_layers <- which(format(getZ(ndvi_brick), "%m") %in% c("05", "06", "07", "08", "09", "10")) rainy_season_brick <- ndvi_brick[[rainy_layers]]
ts didn’t work for your needs The ts object is great for regular time series analysis, but it stores time as a numeric index (not actual Date objects). This makes it tricky to filter based on calendar months or seasons directly. Using real Date vectors gives you the flexibility to target specific time periods easily.
内容的提问来源于stack exchange,提问作者Francis Criens

