Error while extracting netcdf files into raster - r

I have many NCDF files in a folder. I try to extract them into raster brick using raster and ncdf4 packages. If I separately extract each NCDF file it works. However, I try to extract all files using for loop then it gives me error.
R<-list.files("D:/Results/TimeSeries/NETCDF/")
r<-brick(paste0("D:/Results/TimeSeries/NETCDF/",R[[1]]),varname="T_min")
for(i in 2:length(R)){
r1<-brick(paste0("D:/Results/TimeSeries/NETCDF/",R[[i]]),varname="T_min")
r<-brick(r,r1)
}
Error in as.integer(nl) : cannot coerce type 'S4' to vector of type
'integer'
If I look at r and r1 separately they seem to have same extent and both are raster brick type:
> r
class : RasterBrick
dimensions : 81, 81, 6561, 122 (nrow, ncol, ncell, nlayers)
resolution : 1, 1 (x, y)
extent : 0.5, 81.5, 0.5, 81.5 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : D:\Results\TimeSeries\NETCDF\timeseries_1km_2026.nc
names : X0026.05.02, X0026.05.03, X0026.05.04, X0026.05.05, X0026.05.06, X0026.05.07, X0026.05.08, X0026.05.09, X0026.05.10, X0026.05.11, X0026.05.12, X0026.05.13, X0026.05.14, X0026.05.15, X0026.05.16, ...
Date : 0026-05-02, 0026-08-31 (min, max)
varname : T_min
> r1
class : RasterBrick
dimensions : 81, 81, 6561, 122 (nrow, ncol, ncell, nlayers)
resolution : 1, 1 (x, y)
extent : 0.5, 81.5, 0.5, 81.5 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : D:\Results\TimeSeries\NETCDF\timeseries_1km_2027.nc
names : X0027.05.02, X0027.05.03, X0027.05.04, X0027.05.05, X0027.05.06, X0027.05.07, X0027.05.08, X0027.05.09, X0027.05.10, X0027.05.11, X0027.05.12, X0027.05.13, X0027.05.14, X0027.05.15, X0027.05.16, ...
Date : 0027-05-02, 0027-08-31 (min, max)
varname : T_min
Please help.

There is no need to loop, raster is vectorized, try
p <- "D:/Results/TimeSeries/NETCDF"
R <- list.files(p, pattern = "nc$")
r <- raster::stack(file.path(p, R), varname = "T_min")
If you did need to loop, I'd do it like this:
r <- raster::stack(lapply(file.path(p, R), raster::raster, varname = "T_min"))
Edit: replace raster::raster with raster::stack.
Also note the use of file.path, and the facilities available within list.files. (Pasting text for file paths can be problematic, and is more complicated than using available functions).

Related

terra and raster package gives flip warning for reading the same raster

I have a raster in working directory:
When I read this using terra package, it reads fine
r1 <- terra::rast(my_rast)
r1
class : SpatRaster
dimensions : 6000, 6000, 8 (nrow, ncol, nlyr)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 59.99958, 64.99958, 24.99958, 29.99958 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : n25e060.tif
names : n25e060_1, n25e060_2, n25e060_3, n25e060_4, n25e060_5, n25e060_6, ...
But If I read the same raster using the raster package, I get the following warning:
r2 <- raster::raster(my_rast)
Warning message:
In .rasterFromGDAL(x, band = band, objecttype, ...) :
data seems flipped. Consider using: flip(x, direction='y')
class : RasterLayer
band : 1 (of 8 bands)
dimensions : 6000, 6000, 3.6e+07 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 59.99958, 64.99958, 25.00042, 30.00042 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : n25e060.tif
names : n25e060
values : 0, 255 (min, max)
Why is this happening?
That suggests that either {terra} can directly use "flipped" raster data, whereas that {raster} cannot or that {terra} does not notice that the data are flipped. I believe the former to be true, but, while this would be easy for you to verify, I do not have the file so I cannot be 100% sure.

terra function to extract all bands of one variable? (equivalent to brick('x', varname='y'))

I have a netcdf file that contains 79 variables, and for each variable there are 365 bands (one for each day of the year). I want to read all bands of one variable (i.e., a raster with 365 ayers). With the 'raster' package this would work as follows:
dailyvalues <- brick('GLOBAL_2010_day.nc', varname ='WDEP_PREC')
Result is a RasterBrick with 365 layers:
> dailyvalues
class : RasterBrick
dimensions : 180, 360, 64800, 365 (nrow, ncol, ncell, nlayers)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : GLOBAL_2010_day.nc
names : X2010.01.01, X2010.01.02, X2010.01.03, X2010.01.04, X2010.01.05, X2010.01.06, X2010.01.07, X2010.01.08, X2010.01.09, X2010.01.10, X2010.01.11, X2010.01.12, X2010.01.13, X2010.01.14, X2010.01.15, ...
Date : 2010-01-01, 2010-12-31 (min, max)
varname : WDEP_PREC
But I haven't suceeded to do this with the 'terra' package. I tried
dailyvalues <- rast('GLOBAL_2010_day.nc')
> dailyvalues
class : SpatRaster
dimensions : 180, 360, 28835 (nrow, ncol, nlyr)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs
sources : GLOBAL_2010_day.nc:WDEP_PREC (365 layers)
GLOBAL_2010_day.nc:WDEP_SOX (365 layers)
GLOBAL_2010_day.nc:WDEP_OXN (365 layers)
... and 76 more source(s)
varnames : WDEP_PREC (WDEP_PREC)
WDEP_SOX (WDEP_SOX)
WDEP_OXN (WDEP_OXN)
...
names : WDEP_PREC_1, WDEP_PREC_2, WDEP_PREC_3, WDEP_PREC_4, WDEP_PREC_5, WDEP_PREC_6, ...
unit : mm, mm, mm, mm, mm, mm, ...
time : 2010-01-01 18:00:00 to 2010-12-31 12:00:00
The resulting SpatRaster has 79 'sources', but what is the syntax to use if I want to extract one 'source'? Adding varname = 'WDEP_PREC' in the rast function doesn't work. I tried dailyvalues$... but that calls single layers only (as listed under 'names').
The argument to use is called subds in terra.
dailyvalues = rast("GLOBAL_2010_day.nc", subds="WDEP_PREC")
You should be able to do (as #dww says)
dailyvalues = rast("GLOBAL_2010_day.nc", subds="WDEP_PREC")
Or you can make a SpatRasterDataset
x = sds("GLOBAL_2010_day.nc")
And then extract the sub-dataset you want with
r <- x["WDEP_PREC"]
or
r <- x[1]
Furthermore, you can do
r <- rast('NETCDF:"GLOBAL_2010_day.nc:WDEP_PREC":WDEP_PREC')
(for the above to work you may need to add the path to the filename, or set your working directory to where it is)

How do I recode non-standard date dimension of a netcdf file in R?

I'm looking for a tip on how to recode time/date dimension of a netcdf file/raster brick. I extracted a monthly temperature data and the date dimension is not in the standard date format. For example, 1850.04166666667, 1850.125, means January and February 1850 respectively in the data and R couldn't understand them. Here's the data structure
> eco.sst
class : RasterBrick
dimensions : 180, 360, 64800, 2030 (nrow, ncol, ncell, nlayers)
resolution : 1, 1 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Admin\AppData\Local\Temp\Rtmp6vFWNG\raster\r_tmp_2019-04-18_183528_11516_79080.grd
names : X1850.04166666667, X1850.125, X1850.20833333333, X1850.29166666667, X1850.375, X1850.45833333333, X1850.54166666667, X1850.625, X1850.70833333333, X1850.79166666667, X1850.875, X1850.95833333333, X1851.04166666667, X1851.125, X1851.20833333333, ...
min values : -0.668686926, -0.273689777, -1.902773499, -3.406341553, -3.626811743, -2.131400585, -1.569969416, -1.630665064, -2.418994188, -1.971702099, -0.822018623, -3.423746109, -1.303600550, -0.786648691, -1.452626705, ...
max values : 3.926926e+00, 1.550823e+00, 1.151636e+00, 6.622851e-01, 9.673859e-01, 7.178870e-01, 9.010030e-01, 4.363060e-01, 5.231520e-01, 3.346115e-01, 2.156055e-01, 6.418970e-01, 2.259051e+00, 3.802529e+00, 2.077996e+00, ...
time : 1850.04166666667, 2019.125 (min, max)
I wrote a function to replace the decimal points;
fun.repl = function (x, na.rm = TRUE) str_replace(x, ".04166666667", "-1")
such that 1850.04166666667, which means January 1850 becomes 1850-1;
which I then applied to the raster in the form:
stackApply(eco.sst, "time", fun.repl)
eco.sst is the raster brick while "time" is referencing the time dimension. But the function did not work. I suspect that I don't understand the data structure well enough to successfully apply the function. I'm new to this and also finding out that this is a rare problem with netcdf.
I expect 1850.04166666667, 1850.125, 1850.20833333333, 1850.29166666667...which means January to April 1850 to become standard dates in the form.... 1850-1, 1850-2, 1850-3, 1850-4 etc
Here is another way to get the dates
todate <- function(x) {
year <- trunc(x)
month <- round(12 * ((x - year) + 1/24))
as.Date(paste0(year, "-", month, "-15"))
}
dts <- c(1850.04166666667, 1850.125)
d <- todate(dts)
d
#[1] "1850-01-15" "1850-02-15"
Note that I use the 15th day for each month in order to get a valid date --- but that seems to be what is intended.
Now you probably want to use setZ, see the example below. (stackApply makes no sense here and the arguments you use are not valid either)
library(raster)
r <- raster(ncol=10, nrow=10)
s <- stack(lapply(1:2, function(x) setValues(r, runif(ncell(r)))))
s <- setZ(s, d)
s
#class : RasterStack
#dimensions : 10, 10, 100, 2 (nrow, ncol, ncell, nlayers)
#resolution : 36, 18 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
#names : layer.1, layer.2
#min values : 0.01477963, 0.01178438
#max values : 0.9980334, 0.9939610
#time : 1850-01-15, 1850-02-15

Apply boot::inv.logit() to a raster in R

I have a Formal Class Raster and I am trying to apply the boot::inv.logit() function to its raster cells, for example:
r1 <- raster(nrows=25, ncols=25, vals=rtnorm(n = 625, .1, .9))
r2 <- boot::inv.logit(r1)
However, when I try that, it retruns an error:
> Error in plogis(x) : Non-numeric argument to mathematical function
If I turn the raster into a matrix, and then back to raster, it gets the job done, but I loose all the other info associated with the "Formal Class Raster" I had at the beginning, which is not ideal:
r2 <- boot::inv.logit(as.matrix(r1))
r2 <- as.raster(r2)
Is there an easy way to either recover the Formal Class Raster info I had before or apply the inv.logit() to the raster without the as.matrix() transformation? Thank you in advance.
If you want to apply the function to the raster, use the calc method from raster:
r2 <- calc(r1,boot::inv.logit)
> r2
# class : RasterLayer
# dimensions : 25, 25, 625 (nrow, ncol, ncell)
# resolution : 14.4, 7.2 (x, y)
# extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
# data source : in memory
# names : layer
# values : 0.07434905, 0.9498965 (min, max)
Alternatively, you can make an empty copy of r1, and just fill in the values coming out of inv.logit:
r2 <- raster(r1)
r2[] <- boot::inv.logit(as.matrix(r1))

Modifying and Masking Environmental Layers within specific asia area in R

I am trying to prepare the environmental layers (constrained in specific Asia area) for use in Maxent model. However, I ran into some error messages in the last line:
library(sp)
library(maptools)
library(rworldmap)
library(dismo)
# A specified range of Asia area that suitable for special species
tsta <- read.csv('CM10_Kop_Shp_V1.2/Asiaclip/Asiaclipt.csv',as.is=TRUE)[https://drive.google.com/file/d/0B4vIx9MCfJgfbHpINTlyUGZVbXc/view?usp=sharing][1]
tsta <- tsta[,seq(1,4)]
coordinates(tsta) = c("Lon", "Lat")
gridded(tsta) <- TRUE
ra <- raster(tsta)
# a Rasterstack contains global range of 40 bioclim variables
files3 <- list.files(path=paste
("CM10_1975H_Bio_ASCII_V1.2/CM10_1975H_Bio_V1.2"),
, pattern='txt',full.names=TRUE )[https://www.climond.org/Core/Authenticated/Data/CM10_V1.2/CM10_Bio_V1.2/CM10_Bio_ASCII_V1.2/CM10_1975H_Bio_ASCII_V1.2.zip][1]
predictors3 <- stack(files3)
asia.predictors3 <- mask(predictors3,ra)
Error in compareRaster(x, mask) : different extent
The details for predictors3 were
predictors3
class : RasterStack
dimensions : 857, 2160, 1851120, 40 (nrow, ncol, ncell, nlayers)
resolution : 0.1666667, 0.1666667 (x, y)
extent : -180, 180, -59.16667, 83.66667 (xmin, xmax, ymin, ymax)
coord. ref. : NA
names : CM10_1975H_Bio01_V1.2, CM10_1975H_Bio02_V1.2, CM10_1975H_Bio03_V1.2, CM10_1975H_Bio04_V1.2, CM10_1975H_Bio05_V1.2, CM10_1975H_Bio06_V1.2, CM10_1975H_Bio07_V1.2, CM10_1975H_Bio08_V1.2, CM10_1975H_Bio09_V1.2, CM10_1975H_Bio10_V1.2, CM10_1975H_Bio11_V1.2, CM10_1975H_Bio12_V1.2, CM10_1975H_Bio13_V1.2, CM10_1975H_Bio14_V1.2, CM10_1975H_Bio15_V1.2, ...
The details for ra were:
ra
class : RasterLayer
dimensions : 213, 290, 61770 (nrow, ncol, ncell)
resolution : 0.1666667, 0.1666667 (x, y)
extent : 97.5, 145.8333, 18.16667, 53.66667 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : in memory
names : Location
values : 168505, 377653 (min, max)
My goal is to prepare a RasterLayer or Rasterstack contains all variables of "predictors3" but limited in the range of "ra". As you can see the extent of ra was included in the extent of predictors3 and their resolutions were identical. How should I fix the error?
In this case, as the origin and resolution of ra and predictors3 are the same, you can use crop
predictors3 <- raster(xmn=-180, xmx=180, ymn=-59.16667, ymx=83.66667, res=1/6)
ra <- raster(xmn=97.5, xmx=145.8333, ymn=18.16667, ymx=53.66667, res=1/6)
x <- crop(predictors3, ra)
In other cases, you may need to use (dis)aggregate or resample
According to the above suggestions, I crop the the global climate layer "predictors3" to identify the extent of two rasters. Then, mask the latest raster to acquire the targeting variables limited in specific area.
asia.predictors <- crop(predictors3,ra)
asia.predictors3 <- mask(asia.predictors,ra)

Resources