Skip error in lapply and continue processing ncdf4 files in R -
i submitted r script on linux hpc using "sub" script. have written functionin r apply list. however, stops running once encounters bad file. how write r function in such way skips error , continues on netcdf files? script:
##list files in seviri data folder lst1<-list.files(pattern="gt_ssd.*\\.nc",recursive=t, path="/data atsr/seviri/2007") ##function create rasters fun2<-function(x){ ##open files y1<-nc_open(x) ##get soil moisture variable y2<-ncvar_get( y1,"lst") y3<-t(y2) r1<-raster(y3, xmn=-80,xmx=80,ymn=-42,ymx=80) proj4string(r1)<-crs("+proj=longlat +ellps=wgs84") frm <- extent(c(-19, 19,2,29)) pfrm <- as(frm, 'spatialpolygons') r3<-crop(r1,pfrm)} when apply function
lst2<-lapply(lst1,fun2) the error message is:
error in nc_open(x) : error in nc_open trying open file gt_sev_2p/gt_ssd- l2-sevir_lst_2-20110122_010000-lipm-0.05x0.05-v1.0.nc the script stops running once happen. how ensure keeps running on ones, please? codes above first set of codes.
here example try. note simplified function. cannot sure this, not have data, more direct approach works in cases. not need create spatialpolygons object use in crop.
fun2 <- function(x, ext) { r1 <- try(raster(x, var="lst"), silent=true) if (class(r1) == 'try-error') { return(na) } frm <- extent(c(-19, 19, 2, 29)) crop(r1, frm) } x <- lapply(lst1, fun2)
Comments
Post a Comment