Raster to rasterbrick does not preserve Date/Time

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Raster to rasterbrick does not preserve Date/Time

Miluji Sb
Dear all,

I have a raster with multiple variables and date/time information;

      time  Size:17164   *** is unlimited ***
            standard_name: time
            bounds: time_bnds
            units: day as %Y%m%d.%f
            calendar: proleptic_gregorian
        bnds  Size:2

My goal is to convert the raster file to rasterbrick for extraction
purposes;

rbrick <- brick(r,  values=TRUE, varname="var1")

However, after conversion - the date/time information is not preserved. How
can I preserve this information?

class       : RasterBrick
dimensions  : 600, 1440, 864000, 17164  (nrow, ncol, ncell, nlayers)
resolution  : 0.25, 0.25  (x, y)
extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : /work/ncfile.nc4
names       : X19700101.4375, X19700102.4375, X19700103.4375,
X19700104.4375, X19700105.4375, X19700106.4375, X19700107.4375,
X19700108.4375, X19700109.4375, X19700110.4375, X19700111.4375,
X19700112.4375, X19700113.4375, X19700114.4375, X19700115.4375, ...
z-value     : 19700101.4375, 20161231.4375 (min, max)
varname     : var1

Any help will be greatly appreciated!

Sincerely,

Milu

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Raster to rasterbrick does not preserve Date/Time

Ben Tupper
Hi,

I'm not sure about reading the datetime stamp directly form the file, but you have enough info to convert the z-value which is well described in your ncdf.  I'm not familiar with the '%.f' format specifier, but it looks like it is a fractional day.  Something like this perhaps will work?

z      <- raster::getZ(rbick)
z0     <- floor(z)
zf     <- z - z0
dt     <- as.POSIXct(as.character(z0), format = "%Y%m%d") + 24*60*60*zf
rbrick <- raster::setZ(rbrick, z, name = 'datetime')

You'll need to think through the timezone issues that might arise.  as.POSIXct() has arguments that can help you with that.

Cheers,
Ben


> On Nov 4, 2018, at 12:39 PM, Miluji Sb <[hidden email]> wrote:
>
> Dear all,
>
> I have a raster with multiple variables and date/time information;
>
>      time  Size:17164   *** is unlimited ***
>            standard_name: time
>            bounds: time_bnds
>            units: day as %Y%m%d.%f
>            calendar: proleptic_gregorian
>        bnds  Size:2
>
> My goal is to convert the raster file to rasterbrick for extraction
> purposes;
>
> rbrick <- brick(r,  values=TRUE, varname="var1")
>
> However, after conversion - the date/time information is not preserved. How
> can I preserve this information?
>
> class       : RasterBrick
> dimensions  : 600, 1440, 864000, 17164  (nrow, ncol, ncell, nlayers)
> resolution  : 0.25, 0.25  (x, y)
> extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> data source : /work/ncfile.nc4
> names       : X19700101.4375, X19700102.4375, X19700103.4375,
> X19700104.4375, X19700105.4375, X19700106.4375, X19700107.4375,
> X19700108.4375, X19700109.4375, X19700110.4375, X19700111.4375,
> X19700112.4375, X19700113.4375, X19700114.4375, X19700115.4375, ...
> z-value     : 19700101.4375, 20161231.4375 (min, max)
> varname     : var1
>
> Any help will be greatly appreciated!
>
> Sincerely,
>
> Milu
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Ecological Forecasting: https://eco.bigelow.org/

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Raster to rasterbrick does not preserve Date/Time

Miluji Sb
Thank you, Ben! This works well.

Sincerely,

Milu

On Sun, Nov 4, 2018 at 11:08 PM Ben Tupper <[hidden email]> wrote:

> Hi,
>
> I'm not sure about reading the datetime stamp directly form the file, but
> you have enough info to convert the z-value which is well described in your
> ncdf.  I'm not familiar with the '%.f' format specifier, but it looks like
> it is a fractional day.  Something like this perhaps will work?
>
> z      <- raster::getZ(rbick)
> z0     <- floor(z)
> zf     <- z - z0
> dt     <- as.POSIXct(as.character(z0), format = "%Y%m%d") + 24*60*60*zf
> rbrick <- raster::setZ(rbrick, z, name = 'datetime')
>
> You'll need to think through the timezone issues that might arise.
> as.POSIXct() has arguments that can help you with that.
>
> Cheers,
> Ben
>
>
> > On Nov 4, 2018, at 12:39 PM, Miluji Sb <[hidden email]> wrote:
> >
> > Dear all,
> >
> > I have a raster with multiple variables and date/time information;
> >
> >      time  Size:17164   *** is unlimited ***
> >            standard_name: time
> >            bounds: time_bnds
> >            units: day as %Y%m%d.%f
> >            calendar: proleptic_gregorian
> >        bnds  Size:2
> >
> > My goal is to convert the raster file to rasterbrick for extraction
> > purposes;
> >
> > rbrick <- brick(r,  values=TRUE, varname="var1")
> >
> > However, after conversion - the date/time information is not preserved.
> How
> > can I preserve this information?
> >
> > class       : RasterBrick
> > dimensions  : 600, 1440, 864000, 17164  (nrow, ncol, ncell, nlayers)
> > resolution  : 0.25, 0.25  (x, y)
> > extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
> > coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> > data source : /work/ncfile.nc4
> > names       : X19700101.4375, X19700102.4375, X19700103.4375,
> > X19700104.4375, X19700105.4375, X19700106.4375, X19700107.4375,
> > X19700108.4375, X19700109.4375, X19700110.4375, X19700111.4375,
> > X19700112.4375, X19700113.4375, X19700114.4375, X19700115.4375, ...
> > z-value     : 19700101.4375, 20161231.4375 (min, max)
> > varname     : var1
> >
> > Any help will be greatly appreciated!
> >
> > Sincerely,
> >
> > Milu
> >
> >       [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org
>
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo