Why is ncread reading in times which are wrong by a year?

조회 수: 6 (최근 30일)
K E
K E 2015년 8월 4일
편집: Kelly Kearney 2015년 8월 14일
I am getting the wrong year when I read in data from the file below using ncread.
url = 'http://nomads.ncep.noaa.gov:9090/dods/wave/wna/wna20150804/wna20150804_00z';
timeFromNetcdf = ncread(url, 'time');
datestr([timeFromNetcdf(1) timeFromNetcdf(end)])
ans =
04-Aug-2014 00:00:00
11-Aug-2014 12:00:00
You can see from the file name that the year is actually 2015 not 2014. Am I doing something wrong in ncread, perhaps due to the zero-based index in netcdf vs. one-based index in matlab, or is the time variable in the netcdf file simply wrong?

채택된 답변

Kelly Kearney
Kelly Kearney 2015년 8월 6일
편집: Kelly Kearney 2015년 8월 6일
No, it's not a bug. Matlab datenumbers are defined as days since Jan 0, 0000. But if you look at the attributes for your time variable:
>> ncdisp(url, 'time')
Source:
http://nomads.ncep.noaa.gov:9090/dods/wave/wna/wna20150804/wna20150804_00z
Format:
classic
Dimensions:
time = 61
Variables:
time
Size: 61x1
Dimensions: time
Datatype: double
Attributes:
grads_dim = 't'
grads_mapping = 'linear'
grads_size = '61'
grads_min = '00z04aug2015'
grads_step = '3hr'
units = 'days since 1-1-1 00:00:0.0'
long_name = 'time'
minimum = '00z04aug2015'
maximum = '12z11aug2015'
resolution = 0.125
you'll see that the reference time for that variable is Jan 1, 0001. So the proper conversion is
t = ncread(url, 'time') + datenum(1,1,1);
datestr(t([1 end]))
ans =
06-Aug-2015 00:00:00
13-Aug-2015 12:00:00
(Though there seems to be a 2-day shift in there... if this is model data based on a 365-day year or something like that, then datenumbers may not be the best thing to use).
  댓글 수: 2
K E
K E 2015년 8월 11일
Great, thanks. I know for other netcdf variables, ncread will apply any offset and scaling; wish it did that for time as well.
Kelly Kearney
Kelly Kearney 2015년 8월 14일
편집: Kelly Kearney 2015년 8월 14일
The add_offset and scale_factor properties are universal characteristics of netCDF data, defined in the file conventions, so ncread was designed to look for these properties.
However, the units property of a time dimension (or any dimension or variable, for that matter) is completely arbitrary. Your file happens to use days as a unit, but it could be seconds, hours, blue moons, whatever... there's really no way for ncread to know what conversion you would want.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rohit Kudva
Rohit Kudva 2015년 8월 6일
Hi,
I executed the code you provided and I was able to reproduce this issue. This seems to be a bug in MATLAB. I work for MathWorks and I have forwarded this feedback to the appropriate product team.
- Rohit

카테고리

Help CenterFile Exchange에서 Manage Products에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by