Reputation: 25
In my attempt to stack multiple raster images on top of one another, I ID my target files:
ncfiles <- list.files("~/Desktop/Summer 2020/Tropomi/AH.Error1/Error", full.names = T, pattern = "*.nc")
Create an empty raster:
bigstack <- stack()
Construct a for
loop to read through my files in the folder and stack the rasters:
for (i in 1: length(ncfiles)){
fname <-(ncfiles[i])
f <- nc_open(fname)
ah <- ncvar_get(f, varid = "DETAILED_RESULTS/aerosol_optical_thickness")
lon <- ncvar_get(nc, varid = "PRODUCT/longitude")
lat <- ncvar_get(nc, varid = "PRODUCT/latitude")
nc_close(f)
s1 <- data.frame(as.vector(lon), as.vector(lat), as.vector(ah))
crsLatLon <- "+proj=longlat +datum=WGS84"
ex <- extent(c(-180,180,-90,90))
pmraster <- raster(ncol=360*10, nrow=180*10, crs=crsLatLon,ext=ex)
pmraster <- rasterize(s1[,1:2], pmraster, s1[,3], fun=mean, na.rm=T)
exHI <- extent(c(-180,-140,10,30))
levelplot(crop(pmraster,exHI))
bigstack <- stack(bigstack, pmraster)
print("test")
}
I hit a wall with this error message after only 1 iteration:
[1] "test"
Error in data.frame(as.vector(lon), as.vector(lat), as.vector(ah)) :
arguments imply differing number of rows: 1428672, 1301440
Is the path of least resistance forward to define each file have identical number of rows? If so, how can I correct this error? Thank you in advance.
Upvotes: 0
Views: 62
Reputation: 47536
You need to figure out what is going on here:
s1 <- cbind(as.vector(lon), as.vector(lat), as.vector(ah))
What is the length of lon
, lat
, and ah
; and why are they different?Something is not matching so you are doing something wrong, likely terribly wrong.
edit
You were combing data from different files (f
and nc
). Now fixed below
pmraster <- raster(res=0.1, ext=extent(c(-180,-140,10,30)))
biglist <- list()
for (i in 1: length(ncfiles)){
print(i); flush.console()
f <- nc_open(ncfiles[i])
ah <- ncvar_get(f, varid = "DETAILED_RESULTS/aerosol_optical_thickness")
lon <- ncvar_get(f, varid = "PRODUCT/longitude")
lat <- ncvar_get(f, varid = "PRODUCT/latitude")
nc_close(f)
s1 <- cbind(as.vector(lon), as.vector(lat), as.vector(ah))
biglist[[i]] <- rasterize(s1[,1:2], pmraster, s1[,3], fun=mean, na.rm=T)
}
s <- stack(biglist)
Upvotes: 0