Error using writeRaster on a large RasterStack - r

I have a RasterStack in R called "preds2" that is 4.1 GB and was outputted from 4 RasterStacks and 2 RasterLayers (wveg, wfps_lag, wfps, ndvi, swt, lu):
cl <- makeCluster(4)
registerDoSNOW(cl)
preds<-foreach(j = 1:nlayers(ndvi))%dopar%{
library(raster)
library(SpaDES)
time <- stack(wveg,wfps_lag[[j]],wfps[[j]],ndvi[[j]],swt[[j]],lu)
names(time) <- c('wveg','wfps_lag','wfps','ndvi','swt','lu')
exp(raster::predict(time,m,const=fact_table,exclude=c("s(wlch)","s(wetid)")))
}
stopCluster(cl)
preds2<-stack(preds)
> preds2
class : RasterStack
dimensions : 6617, 11771, 77888707, 27 (nrow, ncol, ncell, nlayers)
resolution : 0.0002694946, 0.0002694946 (x, y)
extent : -95.69591, -92.52369, 41.71803, 43.50128 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
names : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10, layer.11, layer.12, layer.13, layer.14, layer.15, ...
min values : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
max values : 0.001754114, 0.001754114, 0.001754114, 0.001754114, 0.001730909, 0.001601078, 0.081421784, 3.510447853, 5.134697329, 7.547881571, 10.945457688, 13.332227330, 14.864517447, 16.708383138, 16.631466329, ...
I am trying to write this RasterStack to a .tif file but get an error:
> writeRaster(stack(preds3), filename = "C:\\Users\\RL\\Documents\\preds.tif", options="INTERLEAVE=BAND", overwrite=TRUE)
Error in file(fn, "rb") : cannot open the connection
In addition: Warning message:
In file(fn, "rb") :
cannot open file 'C:\Users\RL\AppData\Local\Temp\RtmpeegnBN\raster\r_tmp_2020-04-25_004109_2364_37231.gri': No such file or directory
Same error for using calc on the object "preds2" as well. I've created much smaller RasterStacks before with this code without any problem. Online blogs and documentation suggest this error may be due to it being such a large RasterStack (e.g. suggestion on storing "preds2" as a rasterTmpFile but I still get the same error when reading the temp file). Suggestions with code (as I'm new to R) would be appreciated. Thanks!

Figured it out -- the problem was that the RasterStack was too large to be stored in R's working memory and was insteaad saved to a temporary file on my computer that, for whatever reason, I couldn't find or access in R. So I just specified the temporary working directory.
options(rasterTmpDir = "C:\\Users\\RL\\Temp_R_folder")

Related

Error in shapefile(dataFolder, "file.shp")) : could not find function "shapefile"

I am getting an error, saying could not find the function "shapefile".
I also tried all the possible solutions.
I did the shp, .shx and .dbf files in the same folder and many more but it doesn't work, any help, please?
library(raster)
setwd("filename of the folder ")
S <-shapefile(datafolder,"file.shp"))
then error
Error in shapefile(dataFolder, "file.shp")) : could not find function "shapefile"
Here I show that/how raster::shapefile works.
library(raster)
filename <- system.file("external/lux.shp", package="raster")
filename
#[1] "C:/soft/R/R-4.1.1/library/raster/external/lux.shp"
p <- shapefile(filename)
p
#class : SpatialPolygonsDataFrame
#features : 12
#extent : 5.74414, 6.528252, 49.44781, 50.18162 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#variables : 5
#names : ID_1, NAME_1, ID_2, NAME_2, AREA
#min values : 1, Diekirch, 1, Capellen, 76
#max values : 3, Luxembourg, 12, Wiltz, 312
Your
S <-shapefile(datafolder,"file.shp"))
is not proper R syntax (parenthesis do not match). Perhaps you want
S <- shapefile("file.shp")
or
S <- shapefile(file.path(datafolder,"file.shp"))
in which case you do not need to set the working directory

Extracting a variable from multiple hdf files

I have these 3 files here. Trying to stack them and extract a variable called "OzoneTropColumn". I am able to extract data for single file but unable to extract for multiple files.
library(raster)
library(ncdf4)
list_col1 <- list.files("E:/TES", pattern = "*.hdf", full.names = TRUE)
ncin1 <- raster::stack(list_col1, varname = "Data Fields/OzoneTropColumn", ncdf=TRUE)
Any help would be much appreciated.
Thank you
I don't know if this really works since I am no expert in raster or importing hdf files. But this code did load the three files in R for me but still gave some warnings:-
library(raster)
library(ncdf4)
list_col1 <- as.list(list.files("E:/TES", pattern = "*.hdf",
full.names = TRUE))
ncin1 <- raster::stack(list_col1,
varname = "Data Fields/OzoneTropColumn",
ncdf=TRUE)
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_1 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
[1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Data Fields/phony_dim_0 BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
But does shows me that it has read the 3 files
ncin1[[1]]
class : RasterLayer
dimensions : 83, 90, 7470 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0.5, 90.5, 0.5, 83.5 (xmin, xmax, ymin, ymax)
crs : NA
source : /home/shawn/Downloads/TES/TES-Aura_L3-O3-M2004m09_F01_12.hdf
names : Data.Fields.OzoneTropColumn.1
zvar : Data Fields/OzoneTropColumn
ncin1[[2]]
class : RasterLayer
dimensions : 83, 90, 7470 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0.5, 90.5, 0.5, 83.5 (xmin, xmax, ymin, ymax)
crs : NA
source : /home/shawn/Downloads/TES/TES-Aura_L3-O3-M2004m10_F01_12.hdf
names : Data.Fields.OzoneTropColumn.2
zvar : Data Fields/OzoneTropColumn
ncin1[[3]]
class : RasterLayer
dimensions : 83, 90, 7470 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0.5, 90.5, 0.5, 83.5 (xmin, xmax, ymin, ymax)
crs : NA
source : /home/shawn/Downloads/TES/TES-Aura_L3-O3-M2004m11_F01_12.hdf
names : Data.Fields.OzoneTropColumn.3
zvar : Data Fields/OzoneTropColumn
The script posted by Robert works well the only difference there was using as.list and without using as.list]1]1. I tried before using without as.list. The extracted data loses its georeference for reason unknown. So had to give extent and projection after raster stack. Thanks Robert.

Extraction of data from multiple netcdf files at five coordinates files and writing them to five separate csv files

I have 365 .nc files located in a folder containing daily soil moisture information. I want to extract data at five-coordinate locations for the whole year and write them into five separate csv files. My code is attached below. However, I am getting this error after the line:
s <- stack(ff)
>Error in if (is.na(get("has_proj_def.dat", envir = .RGDAL_CACHE))) { : argument is of length zero In addition: Warning message: In .varName(nc, varname, warn = warn) : varname used is: sm If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor
No idea how to proceed further.
library(raster)
library(ncdf4)
ptf <- "D://SMOS_ECV_SM//SMOS_ECV_SM//ECV_SM_Data_1978_2010//1978"
ff <- list.files(path=ptf, pattern="[.]nc$", full.names=TRUE)
s <- stack(ff)
points <- rbind(c(0,1), c(100,120), c(80,5), c(85,4), c(82,4))
v <- extract(s, points)
for (i in 1:ncol(v)) {
write.csv(v[,i,drop=FALSE], paste0("file", i, ".csv"))
}
library(raster)
#Loading required package: sp
f <- list.files("try", full=T)
First try for a single file
r <- raster(f[1])
#Loading required namespace: ncdf4
#Warning message:
#In .varName(nc, varname, warn = warn) : varname used is: sm
#If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor
To get rid of the warning:
r <- raster(f[1], varname="sm")
Now for all files
s <- stack(f, varname="sm")
s
#class : RasterStack
#dimensions : 720, 1440, 1036800, 2 (nrow, ncol, ncell, nlayers)
#resolution : 0.25, 0.25 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +no_defs
#names : Soil.Moisture.1, Soil.Moisture.2
Extract values
points <- rbind(c(-96.7, 47), c(34.55, 54.85))
v <- extract(s, points)
v
# Soil.Moisture.1 Soil.Moisture.2
#[1,] 0.3254 0.3018
#[2,] 0.3386 0.3386

stack and brick function error despite all of the rasters have been

Good day everyone..
I have 13 bioclimatic variables (in .tiff format) that I will used to perform sdm by using dismo package.
I followed the tutorial written by Robert J. Hijmans and Jane Elith.
However, when I tried to stack all of the variables, I got the following error
Error in .local(.Object, ...) :
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file.
All of my file's coordinate system, extent, and cell size have been adjusted so they are all the same..
when I tried to used the alternative brick function, I got the following error :
Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) :
Cannot create a RasterLayer object from this file.
In addition: There were 12 warnings (use warnings() to see them)
I used the warning() message but it was empty..
do any of you have any hints regarding what may be the cause of such errors?
i've tried to google it, but unfortunately, no answer can solve it.
Thank you in advance..
Here presented is the fraction of the transcript
#setting the workspace
setwd("D:/Riset/MaxentSelaginella/newpaperproject_part2/MakalahVI/Workspace_R")
#Loading Libraries
library("sp")
library("raster")
library("maptools")
library("rgdal")
library("dismo")
library("rJava")
#open the csv file
obs.data <- read.csv(file = "data3/Selaginella_plana.csv", sep = ",")
#open Environmental Data
files <- list.files(path = "data3/tif/", pattern = ".tif", full.names=TRUE)
#stacking all the files
predictors <- brick(files)
I guess you need to use stack instead of brick. As per brick help, in fact:
A RasterBrick is a multi-layer raster object. They are typically created from
a multi-layer (band) file; but they can also exist entirely in memory.
They are similar to a RasterStack (that can be created with stack), but processing
time should be shorter when using a RasterBrick. Yet they are less flexible as they can only point to a single file.
So, if we try to “stack” multiple files:
library(raster)
r <- raster(ncols = 100, nrows = 100, vals = 1:10000)
rfile1 <- tempfile(fileext = ".tif")
writeRaster(r, filename = rfile1)
rfile2 <- tempfile(fileext = ".tif")
writeRaster(r, filename = rfile2)
files_to_stack <- c(rfile1, rfile2)
This fails:
brick(files_to_stack)
#> Warning in if (x == "" | x == ".") {: the condition has length > 1 and only
#> the first element will be used
#> Warning in if (!start %in% c("htt", "ftp")) {: the condition has length > 1
#> and only the first element will be used
#> Warning in if (fileext %in% c(".GRD", ".GRI")) {: the condition has length
#> > 1 and only the first element will be used
#> Warning in if (!file.exists(x)) {: the condition has length > 1 and only
#> the first element will be used
.....
#> Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...): Cannot create a RasterLayer object from this file.
While this works:
stack(files_to_stack)
#> class : RasterStack
#> dimensions : 100, 100, 10000, 2 (nrow, ncol, ncell, nlayers)
#> resolution : 3.6, 1.8 (x, y)
#> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
#> names : file46e41bcd78e3, file46e43ea75bad
#> min values : 1, 1
#> max values : 10000, 10000
If you want to have a brick to get some gain in “efficiency” in further
processing, you can save the different "layers" as a multiband tiff, and then open using brick:
rfile_multi <- tempfile(fileext = ".tif")
writeRaster(stack(files_to_stack), filename = rfile_multi)
brick(rfile_multi)
#> class : RasterBrick
#> dimensions : 100, 100, 10000, 2 (nrow, ncol, ncell, nlayers)
#> resolution : 3.6, 1.8 (x, y)
#> extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#> coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
#> data source : D:\RTemp\RtmpacXztJ\file4808784f268c.tif
#> names : file4808784f268c.1, file4808784f268c.2
#> min values : 1, 1
#> max values : 10000, 10000
Created on 2018-11-10 by the reprex package (v0.2.1)

Save multi layer RasterBrick to harddisk

I have a multilayer RasterBrick representing a topographic map that I want to save to the harddisk as grd or tif format, so that others can work with later.
This is the RasterBrick:
class : RasterBrick
dimensions : 2400, 4200, 10080000, 3 (nrow, ncol, ncell, nlayers)
resolution : 100, 100 (x, y)
extent : 480000, 9e+05, 62000, 302000 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : in memory
names : layer.1, layer.2, layer.3
min values : 2.8725, 2.8725, 2.8725
max values : 254.5175, 254.5175, 254.5175
I tried to save it with this command:
outfile <- writeRaster(brick, filename='grid.tif', format="GTiff", overwrite=TRUE)
and this:
outfile <- writeRaster(m, filename='grid.grd', format="raster", overwrite=TRUE)
But the tif file is corrupt and the grd object only contains one layer and is not recognized as multi layer RasterBrick when I read it back in using raster().
The aim is to use the topographic map as background for thematic maps.
Try this:
outfile <- writeRaster(brick, filename='grid.tif', format="GTiff", overwrite=TRUE,options=c("INTERLEAVE=BAND","COMPRESS=LZW"))

Resources