Skip to content

Commit 9b5a7c9

Browse files
committed
xsd typing
1 parent 15afaf4 commit 9b5a7c9

File tree

5 files changed

+85
-67
lines changed

5 files changed

+85
-67
lines changed

lib/bald/__init__.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,12 @@ def rdfnode(self, graph):
537537
else:
538538
rdfobj = self.unpack_rdfobject(obj, rdfpred)
539539
if is_http_uri(rdfobj):
540-
# if rdfobj == 'http://www.ecmwf.int/research/EU_projects/ENSEMBLES/index.html, http://www.ecmwf.int/research/EU_projects/ENSEMBLES/experiments/index.html':
541-
# import pdb; pdb.set_trace()
542540

543541
rdfobj = rdflib.URIRef(rdfobj)
542+
elif terra_imp and isinstance(rdfobj, terra.datetime.EpochDateTimes):
543+
rdfobj = rdflib.Literal(str(rdfobj), datatype=rdflib.XSD.dateTime)
544+
elif isinstance(rdfobj, float):
545+
rdfobj = rdflib.Literal(rdfobj, datatype=rdflib.XSD.decimal)
544546
else:
545547
rdfobj = rdflib.Literal(rdfobj)
546548
rdfpred = rdflib.URIRef(rdfpred)
@@ -780,9 +782,19 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
780782
sattrs['bald__array'] = name
781783
sattrs['rdf__type'] = 'bald__Reference'
782784

783-
sattrs['bald__first_value'] = fhandle.variables[name][0]
784-
if len(fhandle.variables[name]) > 1:
785-
sattrs['bald__last_value'] = fhandle.variables[name][-1]
785+
if not isinstance(fhandle.variables[name][0], np.ma.core.MaskedConstant):
786+
sattrs['bald__first_value'] = fhandle.variables[name][0]
787+
if np.issubdtype(sattrs['bald__first_value'], np.integer):
788+
sattrs['bald__first_value'] = int(sattrs['bald__first_value'])
789+
elif np.issubdtype(sattrs['bald__first_value'], np.float):
790+
sattrs['bald__first_value'] = float(sattrs['bald__first_value'])
791+
if (len(fhandle.variables[name]) > 1 and
792+
not isinstance(fhandle.variables[name][-1], np.ma.core.MaskedConstant)):
793+
sattrs['bald__last_value'] = fhandle.variables[name][-1]
794+
if np.issubdtype(sattrs['bald__last_value'], np.integer):
795+
sattrs['bald__last_value'] = int(sattrs['bald__last_value'])
796+
elif np.issubdtype(sattrs['bald__last_value'], np.float):
797+
sattrs['bald__last_value'] = float(sattrs['bald__last_value'])
786798

787799
# datetime special case
788800
if 'units' in fhandle.variables[name].ncattrs() and terra_imp:
@@ -805,22 +817,32 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
805817
mask=True)
806818
else:
807819
first = fhandle.variables[name][0]
808-
809-
edate_first = terra.datetime.EpochDateTimes(first,
810-
quantity,
811-
epoch=tog)
812-
813-
sattrs['bald__first_value'] = str(edate_first)
820+
if first:
821+
try:
822+
first = int(first)
823+
except Exception:
824+
pass
825+
edate_first = terra.datetime.EpochDateTimes(first,
826+
quantity,
827+
epoch=tog)
828+
829+
sattrs['bald__first_value'] = edate_first
814830
if len(fhandle.variables[name]) > 1:
815831
if fhandle.variables[name][0] == fv:
816832
last = np.ma.MaskedArray(fhandle.variables[name][-1],
817833
mask=True)
818834
else:
819835
last = fhandle.variables[name][-1]
820-
edate_last = terra.datetime.EpochDateTimes(last,
821-
quantity,
822-
epoch=tog)
823-
sattrs['bald__last_value'] = str(edate_last)
836+
if last:
837+
try:
838+
last = round(last)
839+
except Exception:
840+
pass
841+
edate_last = terra.datetime.EpochDateTimes(last,
842+
quantity,
843+
epoch=tog)
844+
845+
sattrs['bald__last_value'] = edate_last
824846

825847

826848

lib/bald/tests/integration/HTML/multi_array_reference.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@
182182
var file://CDL/multi_array_reference.cdl/list_collection = instance('file://CDL/multi_array_reference.cdl/list_collection:<a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |'], '#878800');
183183
var file://CDL/multi_array_reference.cdl/location_reference_system = instance('file://CDL/multi_array_reference.cdl/location_reference_system:<a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', 'pcode: 4897'], '#878800');
184184
var file://CDL/multi_array_reference.cdl/location_variable = instance('file://CDL/multi_array_reference.cdl/location_variable:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
185-
var file://CDL/multi_array_reference.cdl/pdim0 = instance('file://CDL/multi_array_reference.cdl/pdim0:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/first_value" xlink:show=new text-decoration="underline">bald__first_value</a>: --', '<a xlink:href="http://binary-array-ld.net/latest/last_value" xlink:show=new text-decoration="underline">bald__last_value</a>: --', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11'], '#878800');
186-
var file://CDL/multi_array_reference.cdl/pdim1 = instance('file://CDL/multi_array_reference.cdl/pdim1:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/first_value" xlink:show=new text-decoration="underline">bald__first_value</a>: --', '<a xlink:href="http://binary-array-ld.net/latest/last_value" xlink:show=new text-decoration="underline">bald__last_value</a>: --', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 17'], '#878800');
185+
var file://CDL/multi_array_reference.cdl/pdim0 = instance('file://CDL/multi_array_reference.cdl/pdim0:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11'], '#878800');
186+
var file://CDL/multi_array_reference.cdl/pdim1 = instance('file://CDL/multi_array_reference.cdl/pdim1:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 17'], '#878800');
187187
var file://CDL/multi_array_reference.cdl/set_collection = instance('file://CDL/multi_array_reference.cdl/set_collection:<a xlink:href="http://binary-array-ld.net/latest/Subject" xlink:show=new text-decoration="underline">bald__Subject</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |'], '#878800');
188188
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable1, 'bald__contains', 'top', true);
189189
link(file://CDL/multi_array_reference.cdl, file://CDL/multi_array_reference.cdl/data_variable1_pdim0_ref, 'bald__contains', 'top', true);

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ ns1:co2 a bald:Array ;
2929
ns1:co2_time_ref,
3030
ns1:longitude ;
3131
bald:shape "(1, 60, 181, 360)" ;
32-
CFTerms:add_offset 4.031922e+02 ;
32+
CFTerms:add_offset 403.19221938 ;
3333
CFTerms:long_name "Carbon Dioxide" ;
3434
CFTerms:missing_value "-32767" ;
35-
CFTerms:scale_factor 9.816851e-04 ;
35+
CFTerms:scale_factor 0.000981685145029 ;
3636
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/mass_fraction_of_carbon_dioxide_in_air/> ;
3737
CFTerms:units "kg kg**-1" .
3838

@@ -43,10 +43,10 @@ ns1:lnsp a bald:Array ;
4343
ns1:lnsp_time_ref,
4444
ns1:longitude ;
4545
bald:shape "(1, 60, 181, 360)" ;
46-
CFTerms:add_offset 1.120872e+01 ;
46+
CFTerms:add_offset 11.2087164281 ;
4747
CFTerms:long_name "Logarithm of surface pressure" ;
4848
CFTerms:missing_value "-32767" ;
49-
CFTerms:scale_factor 1.039525e-05 .
49+
CFTerms:scale_factor 1.0395245784e-05 .
5050

5151
ns1:co2_latitude_ref a bald:Reference,
5252
bald:Subject ;
@@ -81,33 +81,33 @@ ns1:lnsp_time_ref a bald:Reference,
8181
ns1:latitude a bald:Array,
8282
bald:Reference ;
8383
bald:array ns1:latitude ;
84-
bald:first_value "90.0" ;
85-
bald:last_value "-90.0" ;
84+
bald:first_value 90.0 ;
85+
bald:last_value -90.0 ;
8686
bald:shape "(181,)" ;
8787
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/latitude/> ;
8888
CFTerms:units "degrees_north" .
8989

9090
ns1:levelist a bald:Array,
9191
bald:Reference ;
9292
bald:array ns1:levelist ;
93-
bald:first_value "1" ;
94-
bald:last_value "60" ;
93+
bald:first_value 1 ;
94+
bald:last_value 60 ;
9595
bald:shape "(60,)" ;
9696
CFTerms:long_name "model_level_number" .
9797

9898
ns1:longitude a bald:Array,
9999
bald:Reference ;
100100
bald:array ns1:longitude ;
101-
bald:first_value "0.0" ;
102-
bald:last_value "359.0" ;
101+
bald:first_value 0.0 ;
102+
bald:last_value 359.0 ;
103103
bald:shape "(360,)" ;
104104
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/longitude/> ;
105105
CFTerms:units "degrees_east" .
106106

107107
ns1:time a bald:Array,
108108
bald:Reference ;
109109
bald:array ns1:time ;
110-
bald:first_value "2006-04-01T00:00:00" ;
110+
bald:first_value "2006-04-01T00:00:00"^^xsd:dateTime ;
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" .

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,10 @@ ns1:location_variable a bald:Array,
6666
ns1:pdim0 a bald:Array,
6767
bald:Reference ;
6868
bald:array ns1:pdim0 ;
69-
bald:first_value "None" ;
70-
bald:last_value "None" ;
7169
bald:shape "(11,)" .
7270

7371
ns1:pdim1 a bald:Array,
7472
bald:Reference ;
7573
bald:array ns1:pdim1 ;
76-
bald:first_value "None" ;
77-
bald:last_value "None" ;
7874
bald:shape "(17,)" .
7975

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
ns1:geospatial_bounds "POINT (-123.458000 38.048000)" ;
2727
ns1:geospatial_bounds_crs "EPSG:4326" ;
2828
ns1:geospatial_bounds_vertical_crs "EPSG:5829" ;
29-
ns1:geospatial_lat_max 3.8048e+01 ;
30-
ns1:geospatial_lat_min 3.8048e+01 ;
29+
ns1:geospatial_lat_max 38.048 ;
30+
ns1:geospatial_lat_min 38.048 ;
3131
ns1:geospatial_lat_units "degrees_north" ;
32-
ns1:geospatial_lon_max -1.23458e+02 ;
33-
ns1:geospatial_lon_min -1.23458e+02 ;
32+
ns1:geospatial_lon_max -123.458 ;
33+
ns1:geospatial_lon_min -123.458 ;
3434
ns1:geospatial_lon_units "degrees_east" ;
35-
ns1:geospatial_vertical_max 1.5e+00 ;
36-
ns1:geospatial_vertical_min 1.5e+00 ;
35+
ns1:geospatial_vertical_max 1.5 ;
36+
ns1:geospatial_vertical_min 1.5 ;
3737
ns1:geospatial_vertical_positive "down" ;
3838
ns1:geospatial_vertical_units "m" ;
3939
ns1:history "This file was created on 2016-09-22T18:16:06.590413Z" ;
@@ -81,9 +81,9 @@
8181
ns1:crs a bald:Subject ;
8282
ns1:epsg_code "EPSG:4326" ;
8383
ns1:grid_mapping_name "latitude_longitude" ;
84-
ns1:inverse_flattening 2.982572e+02 ;
85-
ns1:longitude_of_prime_meridian 0e+00 ;
86-
ns1:semi_major_axis 6.378137e+06 .
84+
ns1:inverse_flattening 298.257223563 ;
85+
ns1:longitude_of_prime_meridian 0.0 ;
86+
ns1:semi_major_axis 6378137.0 .
8787

8888
ns1:instrument1 a bald:Subject ;
8989
ns1:accuracy "" ;
@@ -106,83 +106,83 @@ ns1:platform1 a bald:Subject ;
106106
CFTerms:long_name "cordell bank monitoring station" .
107107

108108
ns1:sal a bald:Array ;
109-
ns1:_FillValue -9.999e+03 ;
109+
ns1:_FillValue -9999.0 ;
110110
ns1:comment "These data are bogus!!!!!" ;
111111
ns1:coverage_content_type "physicalMeasurement" ;
112-
ns1:data_max 3.3e+01 ;
113-
ns1:data_min 3.3e+01 ;
112+
ns1:data_max 33.0 ;
113+
ns1:data_min 33.0 ;
114114
ns1:instrument "instrument1" ;
115115
ns1:ncei_name "SALINITY" ;
116116
ns1:platform "platform1" ;
117117
ns1:references <http://www.numpy.org/> ;
118118
ns1:source "numpy.random.rand function." ;
119-
ns1:valid_max 1e+02 ;
120-
ns1:valid_min 0e+00 ;
119+
ns1:valid_max 100.0 ;
120+
ns1:valid_min 0.0 ;
121121
bald:shape "(1,)" ;
122-
CFTerms:add_offset 0e+00 ;
122+
CFTerms:add_offset 0.0 ;
123123
CFTerms:cell_methods "time: point longitude: point latitude: point" ;
124124
CFTerms:coordinates ns1:lat,
125125
ns1:lon,
126126
ns1:time,
127127
ns1:z ;
128128
CFTerms:grid_mapping "crs" ;
129129
CFTerms:long_name "Salinity" ;
130-
CFTerms:missing_value -8.888e+03 ;
131-
CFTerms:scale_factor 1e+00 ;
130+
CFTerms:missing_value -8888.0 ;
131+
CFTerms:scale_factor 1.0 ;
132132
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/sea_water_salinity/> ;
133133
CFTerms:units "0.001" .
134134

135135
ns1:temp a bald:Array ;
136-
ns1:_FillValue -9.999e+03 ;
136+
ns1:_FillValue -9999.0 ;
137137
ns1:comment "These data are bogus!!!!!" ;
138138
ns1:coverage_content_type "physicalMeasurement" ;
139-
ns1:data_max 1.3e+01 ;
140-
ns1:data_min 1.3e+01 ;
139+
ns1:data_max 13.0 ;
140+
ns1:data_min 13.0 ;
141141
ns1:instrument "instrument1" ;
142142
ns1:ncei_name "WATER TEMPERATURE" ;
143143
ns1:platform "platform1" ;
144144
ns1:references <http://www.numpy.org/> ;
145145
ns1:source "numpy.random.rand function." ;
146-
ns1:valid_max 1e+02 ;
147-
ns1:valid_min 0e+00 ;
146+
ns1:valid_max 100.0 ;
147+
ns1:valid_min 0.0 ;
148148
bald:shape "(1,)" ;
149-
CFTerms:add_offset 0e+00 ;
149+
CFTerms:add_offset 0.0 ;
150150
CFTerms:cell_methods "time: point longitude: point latitude: point" ;
151151
CFTerms:coordinates ns1:lat,
152152
ns1:lon,
153153
ns1:time,
154154
ns1:z ;
155155
CFTerms:grid_mapping "crs" ;
156156
CFTerms:long_name "Temperature" ;
157-
CFTerms:missing_value -8.888e+03 ;
158-
CFTerms:scale_factor 1e+00 ;
157+
CFTerms:missing_value -8888.0 ;
158+
CFTerms:scale_factor 1.0 ;
159159
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/sea_water_temperature/> ;
160160
CFTerms:units "degree_Celsius" .
161161

162162
ns1:lat a bald:Array ;
163-
ns1:_FillValue -9.999e+03 ;
163+
ns1:_FillValue -9999.0 ;
164164
ns1:comment "These data are bogus!!!!!" ;
165-
ns1:valid_max 9e+01 ;
166-
ns1:valid_min -9e+01 ;
165+
ns1:valid_max 90.0 ;
166+
ns1:valid_min -90.0 ;
167167
bald:shape "(1,)" ;
168168
CFTerms:axis "Y" ;
169169
CFTerms:long_name "Latitude" ;
170170
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/latitude/> ;
171171
CFTerms:units "degrees_north" .
172172

173173
ns1:lon a bald:Array ;
174-
ns1:_FillValue -9.999e+03 ;
174+
ns1:_FillValue -9999.0 ;
175175
ns1:comment "These data are bogus!!!!!" ;
176-
ns1:valid_max 1.8e+02 ;
177-
ns1:valid_min -1.8e+02 ;
176+
ns1:valid_max 180.0 ;
177+
ns1:valid_min -180.0 ;
178178
bald:shape "(1,)" ;
179179
CFTerms:axis "X" ;
180180
CFTerms:long_name "Longitude" ;
181181
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/longitude/> ;
182182
CFTerms:units "degrees_east" .
183183

184184
ns1:time a bald:Array ;
185-
ns1:_FillValue -9.999e+03 ;
185+
ns1:_FillValue -9999.0 ;
186186
ns1:comment "These data are bogus!!!!!" ;
187187
bald:shape "(1,)" ;
188188
CFTerms:axis "T" ;
@@ -192,10 +192,10 @@ ns1:time a bald:Array ;
192192
CFTerms:units "seconds since 1970-01-01 00:00:00 UTC" .
193193

194194
ns1:z a bald:Array ;
195-
ns1:_FillValue -9.999e+03 ;
195+
ns1:_FillValue -9999.0 ;
196196
ns1:comment "These data are bogus!!!!!" ;
197-
ns1:valid_max 1.0971e+04 ;
198-
ns1:valid_min 0e+00 ;
197+
ns1:valid_max 10971.0 ;
198+
ns1:valid_min 0.0 ;
199199
bald:shape "(1,)" ;
200200
CFTerms:axis "Z" ;
201201
CFTerms:long_name "depth of sensor" ;

0 commit comments

Comments
 (0)