Skip to content

Commit 91fa7ed

Browse files
committed
use rdf lists for shape and broadcast
1 parent 47faa71 commit 91fa7ed

File tree

7 files changed

+115
-93
lines changed

7 files changed

+115
-93
lines changed

lib/bald/__init__.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -805,14 +805,14 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
805805
sattrs['bald__first_value'] = fhandle.variables[name][0]
806806
if np.issubdtype(sattrs['bald__first_value'], np.integer):
807807
sattrs['bald__first_value'] = int(sattrs['bald__first_value'])
808-
elif np.issubdtype(sattrs['bald__first_value'], np.float):
808+
elif np.issubdtype(sattrs['bald__first_value'], np.floating):
809809
sattrs['bald__first_value'] = float(sattrs['bald__first_value'])
810810
if (len(fhandle.variables[name]) > 1 and
811811
not isinstance(fhandle.variables[name][-1], np.ma.core.MaskedConstant)):
812812
sattrs['bald__last_value'] = fhandle.variables[name][-1]
813813
if np.issubdtype(sattrs['bald__last_value'], np.integer):
814814
sattrs['bald__last_value'] = int(sattrs['bald__last_value'])
815-
elif np.issubdtype(sattrs['bald__last_value'], np.float):
815+
elif np.issubdtype(sattrs['bald__last_value'], np.floating):
816816
sattrs['bald__last_value'] = float(sattrs['bald__last_value'])
817817

818818
# datetime special case
@@ -868,7 +868,7 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
868868

869869

870870
if fhandle.variables[name].shape:
871-
sattrs['bald__shape'] = fhandle.variables[name].shape
871+
sattrs['bald__shape'] = list(fhandle.variables[name].shape)
872872
var = Array(baseuri, name, sattrs, prefixes=prefixes,
873873
aliases=aliases, alias_graph=aliasgraph)
874874
else:
@@ -981,30 +981,26 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
981981
var_shape = fhandle.variables[name].shape
982982
refset = var.attrs.get('bald__references', set())
983983
# Only the dimension defining the last dimension will
984-
# broadcase correctly
985-
if var_shape[-1] == cv_shape[0]:
986-
refset.add(file_variables.get(dim))
987-
# Else, define a bald:childBroadcast
988-
else:
989-
# import pdb; pdb.set_trace()
990-
identity = '{}_{}_ref'.format(name, dim)
991-
# if baseuri is not None:
992-
# identity = baseuri + '/' + '{}_{}_ref'.format(name, dim)
993-
rattrs = {}
994-
rattrs['rdf__type'] = 'bald__Reference'
995-
reshape = [1 for adim in var_shape]
996-
997-
cvi = fhandle.variables[name].dimensions.index(dim)
998-
reshape[cvi] = fhandle.variables[dim].size
999-
rattrs['bald__childBroadcast'] = tuple(reshape)
1000-
rattrs['bald__array'] = set((file_variables.get(dim),))
1001-
ref_node = Subject(baseuri, identity, rattrs,
1002-
prefixes=prefixes,
1003-
aliases=aliases,
1004-
alias_graph=aliasgraph)
1005-
root_container.attrs['bald__contains'].add(ref_node)
1006-
file_variables[name] = ref_node
1007-
refset.add(ref_node)
984+
# broadcase correctly. But create all references
985+
# equally, for consistency.
986+
identity = '{}_{}_ref'.format(name, dim)
987+
988+
rattrs = {}
989+
rattrs['rdf__type'] = 'bald__Reference'
990+
reshape = [1 for adim in var_shape]
991+
992+
cvi = fhandle.variables[name].dimensions.index(dim)
993+
reshape[cvi] = int(fhandle.variables[dim].size)
994+
995+
rattrs['bald__childBroadcast'] = reshape
996+
rattrs['bald__array'] = set((file_variables.get(dim),))
997+
ref_node = Subject(baseuri, identity, rattrs,
998+
prefixes=prefixes,
999+
aliases=aliases,
1000+
alias_graph=aliasgraph)
1001+
root_container.attrs['bald__contains'].add(ref_node)
1002+
file_variables[name] = ref_node
1003+
refset.add(ref_node)
10081004
var.attrs['bald__references'] = refset
10091005

10101006

@@ -1121,7 +1117,7 @@ def _hdf_group(fhandle, identity='root', baseuri=None, prefixes=None,
11211117
#if hasattr(dataset, 'shape'):
11221118
elif isinstance(dataset, h5py._hl.dataset.Dataset):
11231119
sattrs = dict(dataset.attrs)
1124-
sattrs['bald__shape'] = dataset.shape
1120+
sattrs['bald__shape'] = list(dataset.shape)
11251121
dset = Array(baseuri, name, sattrs, prefixes, aliases, aliasgraph)
11261122
root_container.attrs['bald__contains'].add(dset)
11271123
file_variables[dataset.name] = dset

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
bald:contains this:co2,
1313
this:co2_latitude_ref,
1414
this:co2_levelist_ref,
15+
this:co2_longitude_ref,
1516
this:co2_time_ref,
1617
this:latitude,
1718
this:levelist,
1819
this:lnsp,
1920
this:lnsp_latitude_ref,
2021
this:lnsp_levelist_ref,
22+
this:lnsp_longitude_ref,
2123
this:lnsp_time_ref,
2224
this:longitude,
2325
this:time ;
@@ -26,9 +28,9 @@
2628
this:co2 a bald:Array ;
2729
bald:references this:co2_latitude_ref,
2830
this:co2_levelist_ref,
29-
this:co2_time_ref,
30-
this:longitude ;
31-
bald:shape "(1, 60, 181, 360)" ;
31+
this:co2_longitude_ref,
32+
this:co2_time_ref ;
33+
bald:shape ( 1 60 181 360 ) ;
3234
CFTerms:missing_value "-32767" ;
3335
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/mass_fraction_of_carbon_dioxide_in_air/> ;
3436
NetCDF:FillValue "-32767" ;
@@ -40,9 +42,9 @@ this:co2 a bald:Array ;
4042
this:lnsp a bald:Array ;
4143
bald:references this:lnsp_latitude_ref,
4244
this:lnsp_levelist_ref,
43-
this:lnsp_time_ref,
44-
this:longitude ;
45-
bald:shape "(1, 60, 181, 360)" ;
45+
this:lnsp_longitude_ref,
46+
this:lnsp_time_ref ;
47+
bald:shape ( 1 60 181 360 ) ;
4648
CFTerms:missing_value "-32767" ;
4749
NetCDF:FillValue "-32767" ;
4850
NetCDF:add_offset 11.2087164280841 ;
@@ -52,39 +54,49 @@ this:lnsp a bald:Array ;
5254
this:co2_latitude_ref a bald:Reference,
5355
bald:Subject ;
5456
bald:array this:latitude ;
55-
bald:childBroadcast "(1, 1, 181, 1)" .
57+
bald:childBroadcast ( 1 1 181 1 ) .
5658

5759
this:co2_levelist_ref a bald:Reference,
5860
bald:Subject ;
5961
bald:array this:levelist ;
60-
bald:childBroadcast "(1, 60, 1, 1)" .
62+
bald:childBroadcast ( 1 60 1 1 ) .
63+
64+
this:co2_longitude_ref a bald:Reference,
65+
bald:Subject ;
66+
bald:array this:longitude ;
67+
bald:childBroadcast ( 1 1 1 360 ) .
6168

6269
this:co2_time_ref a bald:Reference,
6370
bald:Subject ;
6471
bald:array this:time ;
65-
bald:childBroadcast "(1, 1, 1, 1)" .
72+
bald:childBroadcast ( 1 1 1 1 ) .
6673

6774
this:lnsp_latitude_ref a bald:Reference,
6875
bald:Subject ;
6976
bald:array this:latitude ;
70-
bald:childBroadcast "(1, 1, 181, 1)" .
77+
bald:childBroadcast ( 1 1 181 1 ) .
7178

7279
this:lnsp_levelist_ref a bald:Reference,
7380
bald:Subject ;
7481
bald:array this:levelist ;
75-
bald:childBroadcast "(1, 60, 1, 1)" .
82+
bald:childBroadcast ( 1 60 1 1 ) .
83+
84+
this:lnsp_longitude_ref a bald:Reference,
85+
bald:Subject ;
86+
bald:array this:longitude ;
87+
bald:childBroadcast ( 1 1 1 360 ) .
7688

7789
this:lnsp_time_ref a bald:Reference,
7890
bald:Subject ;
7991
bald:array this:time ;
80-
bald:childBroadcast "(1, 1, 1, 1)" .
92+
bald:childBroadcast ( 1 1 1 1 ) .
8193

8294
this:latitude a bald:Array,
8395
bald:Reference ;
8496
bald:array this:latitude ;
8597
bald:first_value 90.0 ;
8698
bald:last_value -90.0 ;
87-
bald:shape "(181,)" ;
99+
bald:shape ( 181 ) ;
88100
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/latitude/> ;
89101
NetCDF:units "degrees_north" .
90102

@@ -93,23 +105,23 @@ this:levelist a bald:Array,
93105
bald:array this:levelist ;
94106
bald:first_value 1 ;
95107
bald:last_value 60 ;
96-
bald:shape "(60,)" ;
108+
bald:shape ( 60 ) ;
97109
NetCDF:long_name "model_level_number" .
98110

99111
this:longitude a bald:Array,
100112
bald:Reference ;
101113
bald:array this:longitude ;
102114
bald:first_value 0.0 ;
103115
bald:last_value 359.0 ;
104-
bald:shape "(360,)" ;
116+
bald:shape ( 360 ) ;
105117
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/longitude/> ;
106118
NetCDF:units "degrees_east" .
107119

108120
this:time a bald:Array,
109121
bald:Reference ;
110122
bald:array this:time ;
111123
bald:first_value "2006-04-01T00:00:00"^^xsd:dateTime ;
112-
bald:shape "(1,)" ;
124+
bald:shape ( 1 ) ;
113125
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/time/> ;
114126
NetCDF:units "hours since 1900-01-01 00:00:0.0" .
115127

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
this:parent_variable a bald:Array ;
1414
bald:references this:child_variable ;
15-
bald:shape "(11, 17)" .
15+
bald:shape ( 11 17 ) .
1616

1717
this:child_variable a bald:Array,
1818
bald:Reference ;
1919
bald:array this:child_variable ;
20-
bald:shape "(11, 17)" .
20+
bald:shape ( 11 17 ) .
2121

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
this:parent_variable a bald:Array ;
1414
bald:references this:child_variable ;
15-
bald:shape "(11, 17)" .
15+
bald:shape ( 11 17 ) .
1616

1717
this:child_variable a bald:Array,
1818
bald:Reference ;
1919
bald:array this:child_variable ;
20-
bald:shape "(11, 17)" .
20+
bald:shape ( 11 17 ) .
2121

0 commit comments

Comments
 (0)