MATLAB: Loss of precision with netcdf time when using datenum

datenumdoublenetcdfprecisiontime

Hi there,
I am trying to read the time variable in from an netcdf file and convert it to datenum values but the end time always seems to fall short (26-11-2005 instead of 01-01-2006).
The time variable from the netcdf file is in 'days since 1860-01-01 00:00:00' and in '365_day' format but I am having trouble getting the proper datenum for the data. The time format is also 'double' precision so I am not sure why I am going wrong when I use the following for example:
time1980_05 = timestamp1980_05' + datenum('1860-01-01 00:00:00');
Any ideas?

Best Answer

I wrote a function, daynoleap2datenum, that does the conversion that Peter suggested (in a slightly simpler manner... I just figure out which years need a leap day and bump the numbers up one at those locations). It converts the output to either datenumbers or datetime objects, so it can work regardless of Matlab version.
As you can see in this example, the missing leap days are what lead to your simple addition method coming up a bit short:
t = [0 58:60 364]';
t1 = datetime(2000,1,1) + t
t2 = daynoleap2datenum(t, 2000, 'dt')
t1 =
01-Jan-2000 00:00:00
28-Feb-2000 00:00:00
29-Feb-2000 00:00:00
01-Mar-2000 00:00:00
30-Dec-2000 00:00:00
t2 =
01-Jan-2000 00:00:00
28-Feb-2000 00:00:00
01-Mar-2000 00:00:00
02-Mar-2000 00:00:00
31-Dec-2000 00:00:00