Mercurial > forge
changeset 12587:75ddf8166364 octave-forge
add more time units
author | abarth93 |
---|---|
date | Thu, 09 Apr 2015 10:02:47 +0000 |
parents | 43fdbcb25f46 |
children | 3f24658504ab |
files | extra/ncArray/inst/nctimeunits.m |
diffstat | 1 files changed, 39 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/extra/ncArray/inst/nctimeunits.m Mon Apr 06 18:44:52 2015 +0000 +++ b/extra/ncArray/inst/nctimeunits.m Thu Apr 09 10:02:47 2015 +0000 @@ -5,31 +5,61 @@ % Parse the netCDF time unit u and returns the time offset (days since 31 % December 1 BC, as datenum) and scaling factor f (in days). % See the netCDF CF convention for the structure of the time units. +% http://cfconventions.org/Data/cf-conventions/cf-conventions-1.6/build/cf-conventions.html#time-coordinate +% Also: http://www.unidata.ucar.edu/software/thredds/current/netcdf-java/CDM/CalendarDateTime.html function [t0,f] = nctimeunits(u) -if strfind(u,'seconds') +% years in days for udunits +% http://cfconventions.org/Data/cf-conventions/cf-conventions-1.6/build/cf-conventions.html#time-coordinate +year_in_days = 365.242198781; + + +l = strfind(u,'since'); + +if length(l) ~= 1 + error(['time units sould expect one "since": "' u '"']); +end + +period = strtrim(lower(u(1:l-1))); +reference_date = strtrim(u(l+6:end)); + +if strcmp(period,'millisec') || strcmp(period,'msec') + f = 1/(24*60*60*1000); +elseif strcmp(period,'second') || strcmp(period,'seconds') ... + || strcmp(period,'s') || strcmp(period,'sec') f = 1/(24*60*60); -elseif strfind(u,'hours') +elseif strcmp(period,'minute') || strcmp(period,'minutes') ... + || strcmp(period,'min') + f = 1/(24*60); +elseif strcmp(period,'hour') || strcmp(period,'hours') ... + || strcmp(period,'hr') f = 1/24; -elseif strfind(u,'days') +elseif strcmp(period,'day') || strcmp(period,'days') f = 1; +elseif strcmp(period,'week') || strcmp(period,'weeks') + f = 1/(24*60*60*7); +elseif strcmp(period,'year') || strcmp(period,'years') ... + strcmp(period,'yr') + f = year_in_days; +elseif strcmp(period,'month') || strcmp(period,'months') ... + strcmp(period,'mon') + f = year_in_days/12; else - error(['unknown units "' u '"']); + error(['unknown units "' period '"']); end -l = strfind(u,'since')+6; try - t0 = datenum(u(l:end),'yyyy-mm-dd HH:MM:SS'); + t0 = datenum(reference_date,'yyyy-mm-dd HH:MM:SS'); catch try - t0 = datenum(u(l:end),'yyyy-mm-ddTHH:MM:SS'); + t0 = datenum(reference_date,'yyyy-mm-ddTHH:MM:SS'); catch try - t0 = datenum(u(l:end),'yyyy-mm-dd'); + t0 = datenum(reference_date,'yyyy-mm-dd'); catch - error(['date format is not recogized ' u(l:end)]) + error(['date format is not recogized ' reference_date]) end end end