Time format in netcdf files

91 views (last 30 days)
mahmoud zemzami
mahmoud zemzami on 27 Jun 2020
Answered: mahmoud zemzami on 29 Jun 2020
Hi,
II want to extract time series from a netcdf file but the time format is like this:
datetime_start = 2006-01-16T12:00:00Z
datetime_stop = 2100-12-16T12:00:00Z
ncid = netcdf.open('pr_Amon_inmcm4_rcp85_r1i1p1_200601-210012.nc','NC_NOWRITE');
filename = ncinfo('pr_Amon_inmcm4_rcp85_r1i1p1_200601-210012.nc');
disp(filename);
ncdisp('pr_Amon_inmcm4_rcp85_r1i1p1_200601-210012.nc');
% 2. EXTRACT VARIABLES
longitude = ncread('pr_Amon_BNU-ESM_rcp85_r1i1p1_200601-210012.nc','lon');%
latitude = ncread('pr_Amon_BNU-ESM_rcp85_r1i1p1_200601-210012.nc','lat');%
time = ncread('pr_Amon_BNU-ESM_rcp85_r1i1p1_200601-210012.nc','time');% the time is in months
So how can I extract time series from this file from 2020-01-01 to 2050-01-12
  2 Comments
dpb
dpb on 28 Jun 2020
Would help to have a (small) sample file if want folks to try something...I've never used netcdf so "know nuthink!" about it. Looks like a standard time format, though.
What's the problem?
Walter Roberson
Walter Roberson on 28 Jun 2020
I have never seen a netcdf file that represented time in anything like 2006-01-16T12:00:00Z .
The netcdf files I have seen represent time in terms of elapsed fixed-length units since a particular date. For example "days" where "day" has been defined as 24*60*60 seconds (so ignoring leap seconds and ignoring timezones.) The only time I have seen "months" as the units was when an integral number of months was used, in which case you could never narrow in as finely as a particular time of a particular day of the month.
It is not uncommon in netcdf to use time units of convenience that do not correspond to real practices -- for example using months as is they were exactly 30 days each, or using fixed 365 day years as if leap years do not exist.

Sign in to comment.

Answers (2)

per isakson
per isakson on 28 Jun 2020
Edited: per isakson on 28 Jun 2020
"I want to extract time series from a netcdf file but the time format is like this: " Why "but", that's the way date-time is stored in this particular nc-file. You need to convert the text of the nc-file to a suitable Matlab data type, e.g. datetime
>> dt = datetime( '2100-12-16T12:00:00Z' ...
, 'InputFormat','yyyy-MM-dd''T''HH:mm:ssXXX' ...
, 'TimeZone','UTC' )
dt =
datetime
16-Dec-2100 12:00:00
"So how can I extract time series from this file from 2020-01-01 to 2050-01-12" You cannot use these dates as input arguments to the function, ncread(). There are two alternatives
  • read the entire time series as you show in your question, convert the values of your variable, time, to a datetime variable, and finally delete the parts of the series that you don't need.
  • You know beforehand which "rows" you want to read and use the input arguments, start and count of ncread()
  2 Comments
mahmoud zemzami
mahmoud zemzami on 29 Jun 2020
Edited: Walter Roberson on 29 Jun 2020
Hi,
Thank you for your answer.
Here you find a link to the netcdf file with which I'm trying to work.
As I mentionned previously, I want to extract data from a date (let say since 2020-01-01 to 2030-01-01)
The stroed data are monthly.
Walter Roberson
Walter Roberson on 29 Jun 2020
Are you expecting that the data in the file goes to December 2100 ? (Note: I do not mean December 2010 !!)
If I read the attributes correctly, the file is using a fixed 365 day year, and the entries in the variable time are days since 1850-1-1 . If you use days(TheTime) + datetime(1850,1,1) then all of the entries come out as nice noon or midnights. But if you start to correct for leap years, then the entries get messy.

Sign in to comment.


mahmoud zemzami
mahmoud zemzami on 29 Jun 2020
Hi, the data starts since 1850-01-01 and the calendar is with no leap.
My question is very basic. How can I extract time series from for example 1800-01-01 to 1805-01-01.
I used this function but I'dont know how to use time in it.
ncid = netcdf.open('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012.nc','NC_NOWRITE');
filename = ncinfo('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012.nc');
disp(filename);
ncdisp('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012.nc');
% 2. EXTRACT VARIABLES----------------------------------------------
longitude = ncread('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012.nc','lon');
latitude = ncread('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012.nc','lat');
time = ncread('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012.nc','time');
precipitation1 = ncread('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012.nc','pr');
An then I'ma not able to use time in this function:
precipitation3 = ncread('pr_Amon_BNU-ESM_rcp26_r1i1p1_200601-210012','pr',[30 20 45],[10 10 5900],[1 1 1]);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!