date netcdf4 '1900-01-01 00:00:00.0'

Hi Everyone, Please, could you help me.
I have a .nc file. I am using:
install.packages('fields')
install.packages('ncdf4')
require(ncdf4)
require(fields)
f <- nc_open("/path/f.nc")

lat <- ncvar_get(f, "latitude")
lon <- ncvar_get(f, "longitude")
tm <- ncvar_get(f, "time")
tm <- as.Date(tm, origin = '1900-01-01 00:00:00.0')
tm

The tm is not working as I wanted. I would like to know how I transform my six hourly data from int32 to (00:00, 06:00,12:00,18:00).
Dataset is from January 2007 to December 2017. Details as follows:
Year:

2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017

Month:

January, February, March, April, May, June, July, August, September, October, November, December

Day:

01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31

Time:

00:00, 06:00, 12:00, 18:00

But when I open "f", it says the origin is 1900-01-00.

Thank you!!
Cheers!

If the date (timestamp in fact) is in Integer format, then as.POSIXct() should do the trick

 as.POSIXct(3852342528, origin = '1900-01-01 00:00:00.0')
[1] "2022-01-28 08:08:48 CET"

Hi ! Thank you very much!
But how do I make it jump from 6 in 6 hours. It is not hourly, but 6-hourly, or 4 times each day.

Cheers!

To be honest I don't understand the request. Would you provide a subset of your data?

Are the original dates random? If you wants to aggregate to every 6 hours, then lubridate may help:

Sys.time()
[1] "2022-02-01 19:53:02 CET"

lubridate::floor_date(Sys.time(), "6 hours")
[1] "2022-02-01 18:00:00 CET"

Grzegorz

Hi, thanks for answering.
My data should start in 2007.01.01 and finish 2017.12.31. Daily data, with 4 time periods each day, i.e., 00:00, 06:00, 12:00 and 18:00. Doing as suggested above, I get the following and it seems the 6 interval is jumping in the seconds, like 6 in 6 seconds, instead of 6 in 6 hours as I need.

tm <- ncvar_get(f, "time")
tm <- as.POSIXct(tm, origin = '1900-01-01 00:00:00.0')
tm
[1] "1900-01-11 20:32:24 GMT" "1900-01-11 20:32:30 GMT"
[3] "1900-01-11 20:32:36 GMT" "1900-01-11 20:32:42 GMT"
[5] "1900-01-11 20:32:48 GMT" "1900-01-11 20:32:54 GMT"
[7] "1900-01-11 20:33:00 GMT" "1900-01-11 20:33:06 GMT"
[9] "1900-01-11 20:33:12 GMT" "1900-01-11 20:33:18 GMT"
[11] "1900-01-11 20:33:24 GMT" "1900-01-11 20:33:30 GMT"
[13] "1900-01-11 20:33:36 GMT" "1900-01-11 20:33:42 GMT"
[15] "1900-01-11 20:33:48 GMT" "1900-01-11 20:33:54 GMT"
[17] "1900-01-11 20:34:00 GMT" "1900-01-11 20:34:06 GMT"
[19] "1900-01-11 20:34:12 GMT" "1900-01-11 20:34:18 GMT"
[21] "1900-01-11 20:34:24 GMT" "1900-01-11 20:34:30 GMT"
[23] "1900-01-11 20:34:36 GMT" "1900-01-11 20:34:42 GMT"
[25] "1900-01-11 20:34:48 GMT" "1900-01-11 20:34:54 GMT"
[27] "1900-01-11 20:35:00 GMT" "1900-01-11 20:35:06 GMT"
[29] "1900-01-11 20:35:12 GMT" "1900-01-11 20:35:18 GMT"
[31] "1900-01-11 20:35:24 GMT" "1900-01-11 20:35:30 GMT"
[33] "1900-01-11 20:35:36 GMT" "1900-01-11 20:35:42 GMT"

Then I guess, you have to change the origin, like:

tm <- as.POSIXct(tm, origin = '2007-01-01 00:00:00.0')

Can you show tm before conversion to datetime? After:

tm <- ncvar_get(f, "time")

Regards,
Grzegorz

Thanks Grzegorz,
I've tried changing the origin and it keeps jumping 6 in 6 in the seconds...
tm <- as.POSIXct(tm, origin = '2007-01-01 00:00:00.0')
[993] "2007-01-11 22:11:36 GMT" "2007-01-11 22:11:42 GMT"
[995] "2007-01-11 22:11:48 GMT" "2007-01-11 22:11:54 GMT"
[997] "2007-01-11 22:12:00 GMT" "2007-01-11 22:12:06 GMT"
[999] "2007-01-11 22:12:12 GMT" "2007-01-11 22:12:18 GMT"
[ reached 'max' / getOption("max.print") -- omitted 15072 entries ]

lat <- ncvar_get(f, "latitude")
lon <- ncvar_get(f, "longitude")
tm <- ncvar_get(f, "time")
tm
[1] 937944 937950 937956 937962 937968 937974 937980 937986 937992 937998
[11] 938004 938010 938016 938022 938028 938034 938040 938046 938052 938058
[21] 938064 938070 938076 938082 938088 938094 938100 938106 938112 938118
[31] 938124 938130 938136 938142 938148 938154 938160 938166 938172 938178
[41] 938184 938190 938196 938202 938208 938214 938220 938226 938232 938238

Cheers!

Multiply by 60^2 :slight_smile:

a <- 937944 * 60 * 60
as.POSIXct(a, origin = '1900-01-01 00:00:00.0', tz = "UTC")
  [1] "2007-01-01 UTC"

Cheers,
G.

1 Like

Brilliant!!!! :slight_smile: Thank you so much!!!!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.