从NetCDF提取矩阵转栅格行偏移问题求助(R语言)
It looks like that southward offset is almost certainly tied to mismatched latitude ordering between your adjusted matrix and how the raster package expects spatial data to be structured. Let's break down the problem and fix it step by step:
Key Issue
NetCDF files often store latitude values in north-to-south order (starting at the highest northern latitude and decreasing southward), but the raster package expects raster data to be ordered south-to-north (rows start at the southernmost latitude). When you replicated rows to fix uneven spacing, you kept the original north-to-south row order, which misaligns with the WGS84 projection's coordinate mapping—hence the southward shift.
Step-by-Step Solution
First, let's clean up the overcomplicated rotation logic and align the matrix rows with the correct latitude order:
Verify Latitude Storage Order
First confirm how your original NetCDF stores latitude:library(raster) library(ncdf4) library(rgdal) # Load NetCDF data dname <- "GI" clim_ncdf <- nc_open("NetCDF_GI.nc") lon <- ncvar_get(clim_ncdf,"Longitude") lat <- ncvar_get(clim_ncdf,"Latitude") # Check if latitude is north-to-south (decreasing) or south-to-north (increasing) print(lat)If
latstarts at a high northern latitude and decreases toward the south, we'll need to flip the matrix rows later.Simplify Matrix Orientation
Your triplerotatecall is overcomplicating things. Let's correctly orient the matrix to match longitude/latitude dimensions:EI1 <- ncvar_get(clim_ncdf, dname) # Most NetCDFs store variables as [lat_rows, lon_cols]—transpose to match raster's expected [lon_cols, lat_rows] EI <- t(EI1)Fix Uneven Latitude Spacing (With Alignment)
Keep your original row replication logic, but add a critical step to align rows with south-to-north latitude:# Create equal-spaced latitude vector (south to north) nlat <- seq(min(lat), max(lat), 0.5) # Your original row replication rules rows_m_reps <- rep(1, nrow(EI)) rows_m_reps[58] <- 2 rows_m_reps[59] <- 10 # Replicate rows to match equal spacing EI_adj <- EI[rep(1:nrow(EI), rows_m_reps), ] # Critical fix: Reverse rows if original latitude was north-to-south if (is.unsorted(lat, strictly = TRUE)) { # Checks if lat is decreasing EI_adj <- EI_adj[nrow(EI_adj):1, ] }Generate Corrected Raster
Now create the raster with the properly aligned matrix—this should eliminate the southward offset:EIr_adj <- raster(EI_adj, xmn = min(lon), xmx = max(lon), ymn = min(nlat), ymx = max(nlat), crs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0") # Plot to verify alignment plot(EIr_adj) world_admin <- readOGR("Countries_WGS84.shp") plot(world_admin, add = TRUE)
Why This Works
- By reversing rows when the original latitude is north-to-south, we align each matrix row with the corresponding south-to-north latitude in
nlat. - The
rasterpackage maps the first row of your matrix to the southernmost latitude (ymn) and the last row to the northernmost (ymx), so matching this order fixes the spatial misalignment.
内容的提问来源于stack exchange,提问作者MIH

