Search code examples
rlapplyskip

Skip error in lapply and continue processing ncdf4 files in R


I submitted an R script on LINUX HPC using the "sub" script. I have written a functionin R to apply to a list. However, it stops running once it encounters a bad file. How do I write the R function in such a way that it skips the error and continues on the good netcdf files? the script:

##list files in the SEVIRI data folder
LST1<-list.files(pattern="GT_SSD.*\\.nc",recursive=T, path="/data atsr/SEVIRI/2007")

##Function to create rasters
fun2<-function(x){
##Open the 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 I apply the function

LST2<-lapply(LST1,fun2)

The error message is:

 Error in nc_open(x) : 
 Error in nc_open trying to 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 this happen. How do I ensure it keeps running on the good ones, please? The codes above are just the first set of codes.


Solution

  • Here is an example with try. Note that I much simplified your function. I cannot be sure about this, as I do not have your data, but this much more direct approach works in most cases. You certainly do not need to create a SpatialPolygons object for 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)