user11384727
user11384727

Reputation: 67

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

Upvotes: 0

Views: 92

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47146

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 

Upvotes: 1

Related Questions