Skip to content

Commit 002e0f8

Browse files
committed
datetime for nc coord var
1 parent 96123dc commit 002e0f8

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ install:
1717
- conda install --quiet --file requirements.txt
1818
- conda list
1919
- conda info -a
20+
- wget https://github.com/marqh/terra/archive/master.zip
21+
- unzip terra-master.zip
22+
- cd terra-master
23+
- python setup.py --quiet install
24+
- cd ..
2025
- python setup.py --quiet install
2126

2227
script:

lib/bald/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import rdflib
1111
import requests
1212
import six
13+
import terra.datetime
1314

1415
import bald.validation as bv
1516

@@ -750,9 +751,41 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
750751
fhandle.variables[name].dimensions[0] == name):
751752
sattrs['bald__array'] = name
752753
sattrs['rdf__type'] = 'bald__Reference'
754+
755+
# if fhandle.variables
753756
sattrs['bald__first_value'] = fhandle.variables[name][0]
754757
if len(fhandle.variables[name]) > 1:
755758
sattrs['bald__last_value'] = fhandle.variables[name][-1]
759+
760+
# datetime special case
761+
if 'units' in fhandle.variables[name].ncattrs():# and False:
762+
ustr = fhandle.variables[name].getncattr('units')
763+
pattern = '^([a-z]+) since ([0-9T:\\. -]+)'
764+
765+
amatch = re.match(pattern, ustr)
766+
if amatch:
767+
quantity = amatch.group(1)
768+
origin = amatch.group(2)
769+
ig = terra.datetime.ISOGregorian()
770+
tog = terra.datetime.parse_datetime(origin,
771+
calendar=ig)
772+
first = np.array((fhandle.variables[name][0],))
773+
774+
edate_first = terra.datetime.EpochDateTimes(first,
775+
quantity,
776+
epoch=tog)
777+
778+
sattrs['bald__first_value'] = str(edate_first)
779+
if len(fhandle.variables[name]) > 1:
780+
last = np.array((fhandle.variables[name][-1],))
781+
edate_last = terra.datetime.EpochDateTimes(last,
782+
quantity,
783+
epoch=tog)
784+
sattrs['bald__last_value'] = str(edate_last)
785+
786+
787+
788+
756789

757790
if fhandle.variables[name].shape:
758791
sattrs['bald__shape'] = fhandle.variables[name].shape

lib/bald/tests/integration/TTL/GEMS_CO2_Apr2006.ttl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ns1:longitude a bald:Array,
107107
ns1:time a bald:Array,
108108
bald:Reference ;
109109
bald:array ns1:time ;
110-
bald:first_value "931344" ;
110+
bald:first_value "2006-04-01T00:00:00" ;
111111
bald:shape "(1,)" ;
112112
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/time/> ;
113113
CFTerms:units "hours since 1900-01-01 00:00:0.0" .

0 commit comments

Comments
 (0)