I am attempting to open and plot about 60 netcdf4 files from a directory. I followed a similar example originally posted here.
"~/Desktop/Summer 2020/Tropomi/Aerosol Height"
ncfiles <- list.files("~/Desktop/Summer 2020/Tropomi/Aerosol Height")
for (i in 1: length(ncfiles)){
fname <-(paste0("~/Desktop/Summer 2020/Tropomi/Aerosol Height/", ncfiles[i]))
f <- nc_open(fname)
print(fname)
}
When I run this section, the correct files are being read and printed to the screen but suddenly stops with this error message:
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180715T201241_20180715T215609_03908_01_010301_20190530T173144.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180715T215411_20180715T233739_03909_01_010301_20190530T174631.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180715T233541_20180716T011909_03910_01_010301_20190530T175745.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180716T011711_20180716T030038_03911_01_010301_20190530T181100.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180716T213507_20180716T231835_03923_01_010301_20190530T202107.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180716T231637_20180717T010005_03924_01_010301_20190530T203126.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180717T005807_20180717T024134_03925_01_010301_20190530T203847.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180717T211603_20180717T225931_03937_01_010301_20190530T231939.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180717T225733_20180718T004100_03938_01_010301_20190530T233047.nc"
[1] "~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180718T003902_20180718T022230_03939_01_010301_20190530T234019.nc"
Error in R_nc4_open: NetCDF: HDF error
Error in nc_open(fname) :
Error in nc_open trying to open file ~/Desktop/Summer 2020/Tropomi/Aerosol Height/S5P_RPRO_L2__AER_LH_20180718T205659_20180718T224026_03951_01_010301_20190531T015417.nc
I have opened the file highlighted by the error message outside of the loop so I don't believe the file is corrupt. Does anyone know why this is happening? Thanks in advance!
UPDATE:
Using try
, I am able to run the loop to completion and print all desired files to the console. The next step for me is read in the files, extract variables, and stack the raster images on top of one another resulting in a single image. Try
and TryCatch
are not working and my loop breaks as soon as the first error is encountered.
for (i in 1: length(ncfiles)){
try(
{
fname <-(ncfiles[i])
f <- nc_open(fname)
print(fname)},
silent = T)
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))
}
How can I modify my loop structure to skip the errors?