Hi - I'm trying to process a large number of raster files and it is causing
an error after 243 files. Below is a simplified code snippet and the error
message. I tried doing closeAllConnections() but it didn't help so I'm not
sure what else needs to be done. There should really only ever be 1 raster
read connection open, 1 write raster connection and 4 different readOGR
connections open.

*ERROR:*
Error in .local(.Object, ...) :
  TIFFOpen:/Users/dosc3612/Documents/wwa/boulder2002133.tif: Too many open
files

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
(from subset.modscag.R#21) :
  Cannot create a RasterLayer object from this file.

*CODE:*
domain=readOGR(pn,bn) #reading in a polygon shape file with which to crop
the big raster

proc=function(fn,bd,dname){
    r=raster(fn)
    projection(r)=CRS('+proj=longlat +datum=NAD83')
    r <- mask(r,water.mask,maskvalue=1)
    r=crop(r,bd)
    f=unlist(strsplit(fn,'/'))
    f=f[length(f)]
    print(paste0(outfile,dname,f))

writeRaster(r,filename=paste0(outfile,dname,f),format='GTiff',overwrite=T)\
}

ffns=list.files(path='.',pattern=glob2rx('*.tif'),full.names=T)
lapply(ffns,domain,'basin1')


*SESSIONINFO:*> sessionInfo()
R version 3.0.3 (2014-03-06)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rgdal_0.8-11  raster_2.1-49 sp_1.0-13     plyr_1.8

loaded via a namespace (and not attached):
[1] compiler_3.0.3  grid_3.0.3      lattice_0.20-27 tools_3.0.3


*TRACEBACK:*> traceback()
7: stop("Cannot create a RasterLayer object from this file.")
6: .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
       ...)
5: .local(x, ...)
4: raster(fn)
3: raster(fn) at subset.modscag.R#14
2:
FUN(c("/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002014.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002021.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002032.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002035.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002037.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002044.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002046.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002062.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002090.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002092.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002094.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002103.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002107.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002112.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002114.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002119.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002121.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002126.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002129.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002133.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002137.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002150.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002151.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002156.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002160.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002163.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002165.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002167.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002174.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002179.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002181.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002183.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002190.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002213.tif",


"/Volumes/hydroData/WestUS_Data/UCO_FSCA//2002_finished_20140423_1538/2002226.tif"
   )[[1L]], ...)
1: lapply(ffns, proc, domain_bld, "boulder")

	[[alternative HTML version deleted]]

