Skip to content

Commit 8722778

Browse files
committed
reference management
1 parent 91fa7ed commit 8722778

File tree

4 files changed

+241
-59
lines changed

4 files changed

+241
-59
lines changed

lib/bald/__init__.py

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -950,33 +950,79 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
950950
for sattr in (sattr for sattr in sattrs if
951951
root_container.unpack_predicate(sattr) in ref_prefs):
952952

953-
if (isinstance(sattrs[sattr], six.string_types) and
954-
file_variables.get(sattrs[sattr])):
955-
# next: remove all use of set, everything is dict or orderedDict
956-
var.attrs[sattr] = set((file_variables.get(sattrs[sattr]),))
957-
elif isinstance(sattrs[sattr], six.string_types):
953+
# if (isinstance(sattrs[sattr], six.string_types) and
954+
# file_variables.get(sattrs[sattr])):
955+
# import pdb; pdb.set_trace()
956+
# var.attrs[sattr] = set((file_variables.get(sattrs[sattr]),))
957+
# el
958+
if isinstance(sattrs[sattr], six.string_types):
959+
958960
if sattrs[sattr].startswith('(') and sattrs[sattr].endswith(')'):
959961
potrefs_list = sattrs[sattr].lstrip('( ').rstrip(' )').split(' ')
960-
if len(potrefs_list) > 1:
961-
refs = np.array([file_variables.get(pref) is not None
962-
for pref in potrefs_list])
963-
if np.all(refs):
964-
var.attrs[sattr] = [file_variables.get(pref)
965-
for pref in potrefs_list]
962+
#if len(potrefs_list) > 1:
963+
refs = np.array([file_variables.get(pref) is not None
964+
for pref in potrefs_list])
965+
if np.all(refs):
966+
var.attrs[sattr] = [file_variables.get(pref)
967+
for pref in potrefs_list]
968+
for pref in potrefs_list:
969+
cv_shape = fhandle.variables[pref].shape
970+
var_shape = fhandle.variables[name].shape
971+
identity = '{}_{}_ref'.format(name, pref)
972+
rattrs = {}
973+
rattrs['rdf__type'] = 'bald__Reference'
974+
reshape = [1 for adim in var_shape]
975+
966976
else:
967977
potrefs_set = sattrs[sattr].split(' ')
968-
if len(potrefs_set) > 1:
969-
refs = np.array([file_variables.get(pref) is not None
970-
for pref in potrefs_set])
971-
if np.all(refs):
972-
var.attrs[sattr] = set([file_variables.get(pref)
973-
for pref in potrefs_set])
978+
refs = np.array([file_variables.get(pref) is not None
979+
for pref in potrefs_set])
980+
if np.all(refs):
981+
var.attrs[sattr] = set([file_variables.get(pref)
982+
for pref in potrefs_set])
983+
for pref in potrefs_set:
984+
shapematch = (fhandle.variables[name].shape ==
985+
fhandle.variables[pref].shape)
986+
if (fhandle.variables[name].shape and
987+
not shapematch and
988+
fhandle.variables[pref].shape):
989+
# if .shape is not null and .shapes don't match
990+
refset = var.attrs.get('bald__references',
991+
set())
992+
cv_shape = fhandle.variables[pref].shape
993+
var_shape = fhandle.variables[name].shape
994+
identity = '{}_{}_ref'.format(name, pref)
995+
rattrs = {}
996+
rattrs['rdf__type'] = 'bald__Reference'
997+
reshape = [1 for adim in var_shape]
998+
# if name == 'salt':
999+
# import pdb; pdb.set_trace()
1000+
dims = fhandle.variables[pref].dimensions
1001+
for dim in dims:
1002+
try:
1003+
cvi = fhandle.variables[name].dimensions.index(dim)
1004+
except Exception:
1005+
import pdb; pdb.set_trace()
1006+
cvi = fhandle.variables[name].dimensions.index(dim)
1007+
reshape[cvi] = int(fhandle.dimensions[dim].size)
1008+
rattrs['bald__childBroadcast'] = reshape
1009+
rattrs['bald__array'] = set((file_variables.get(pref),))
1010+
ref_node = Subject(baseuri, identity, rattrs,
1011+
prefixes=prefixes,
1012+
aliases=aliases,
1013+
alias_graph=aliasgraph)
1014+
root_container.attrs['bald__contains'].add(ref_node)
1015+
file_variables[identity] = ref_node
1016+
refset.add(ref_node)
1017+
var.attrs['bald__references'] = refset
1018+
1019+
9741020

9751021
# coordinate variables are bald__references except for
9761022
# variables that already declare themselves as bald__Reference
9771023
if 'bald__Reference' not in var.rdf__type:
9781024
for dim in fhandle.variables[name].dimensions:
979-
if file_variables.get(dim):
1025+
if file_variables.get(dim) and name != dim:
9801026
cv_shape = fhandle.variables[dim].shape
9811027
var_shape = fhandle.variables[name].shape
9821028
refset = var.attrs.get('bald__references', set())
@@ -999,8 +1045,11 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
9991045
aliases=aliases,
10001046
alias_graph=aliasgraph)
10011047
root_container.attrs['bald__contains'].add(ref_node)
1002-
file_variables[name] = ref_node
1048+
file_variables[identity] = ref_node
10031049
refset.add(ref_node)
1050+
# br = careful_update(var.attrs.get('bald__references',
1051+
# {}), refset)
1052+
# var.attrs['bald__references'] = br
10041053
var.attrs['bald__references'] = refset
10051054

10061055

lib/bald/tests/integration/CDL/multi_array_reference.cdl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ variables:
2323
int pdim1(pdim1) ;
2424

2525
int location_variable(pdim0, pdim1) ;
26-
location_variable:rdf__type = "bald__Reference";
27-
location_variable:bald__array = "location_variable" ;
2826
location_variable:bald__references = "location_reference_system" ;
2927

3028
int location_reference_system;
31-
location_variable:rdf__type = "bald__Reference";
32-
location_reference_system:bald__array = "location_reference_system";
3329
location_reference_system:pcode = "4897";
3430

3531
int set_collection ;

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

Lines changed: 133 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,39 @@
1717
this:shoc_version "v1.1 rev(5249)" ;
1818
bald:contains this:botz,
1919
this:eta,
20+
this:eta_latitude_ref,
21+
this:eta_longitude_ref,
2022
this:eta_time_ref,
2123
this:latitude,
2224
this:longitude,
2325
this:salt,
26+
this:salt_latitude_ref,
27+
this:salt_longitude_ref,
2428
this:salt_time_ref,
29+
this:salt_zc_ref,
2530
this:temp,
31+
this:temp_latitude_ref,
32+
this:temp_longitude_ref,
2633
this:temp_time_ref,
34+
this:temp_zc_ref,
2735
this:time,
2836
this:u,
37+
this:u_latitude_ref,
38+
this:u_longitude_ref,
2939
this:u_time_ref,
40+
this:u_zc_ref,
3041
this:v,
42+
this:v_latitude_ref,
43+
this:v_longitude_ref,
3144
this:v_time_ref,
45+
this:v_zc_ref,
3246
this:wspeed_u,
47+
this:wspeed_u_latitude_ref,
48+
this:wspeed_u_longitude_ref,
3349
this:wspeed_u_time_ref,
3450
this:wspeed_v,
51+
this:wspeed_v_latitude_ref,
52+
this:wspeed_v_longitude_ref,
3553
this:wspeed_v_time_ref,
3654
this:zc ;
3755
bald:isPrefixedBy "prefix_list" ;
@@ -54,7 +72,9 @@ this:eta a bald:Array ;
5472
this:scaledQuantityKind_id <http://environment.data.gov.au/def/property/sea_surface_elevation> ;
5573
this:substanceOrTaxon_id <http://environment.data.gov.au/def/feature/ocean_near_surface> ;
5674
this:unit_id <http://qudt.org/vocab/unit#Meter> ;
57-
bald:references this:eta_time_ref ;
75+
bald:references this:eta_latitude_ref,
76+
this:eta_longitude_ref,
77+
this:eta_time_ref ;
5878
bald:shape ( 5 180 600 ) ;
5979
CFTerms:coordinates this:latitude,
6080
this:longitude,
@@ -71,7 +91,10 @@ this:salt a bald:Array ;
7191
this:scaledQuantityKind_id <http://environment.data.gov.au/def/property/practical_salinity> ;
7292
this:substanceOrTaxon_id <http://sweet.jpl.nasa.gov/2.2/matrWater.owl#SaltWater> ;
7393
this:unit_id <http://environment.data.gov.au/water/quality/def/unit/PSU> ;
74-
bald:references this:salt_time_ref ;
94+
bald:references this:salt_latitude_ref,
95+
this:salt_longitude_ref,
96+
this:salt_time_ref,
97+
this:salt_zc_ref ;
7598
bald:shape ( 5 47 180 600 ) ;
7699
CFTerms:coordinates this:latitude,
77100
this:longitude,
@@ -88,7 +111,10 @@ this:temp a bald:Array ;
88111
this:scaledQuantityKind_id <http://environment.data.gov.au/def/property/sea_water_temperature> ;
89112
this:substanceOrTaxon_id <http://sweet.jpl.nasa.gov/2.2/matrWater.owl#SaltWater> ;
90113
this:unit_id <http://qudt.org/vocab/unit#DegreeCelsius> ;
91-
bald:references this:temp_time_ref ;
114+
bald:references this:temp_latitude_ref,
115+
this:temp_longitude_ref,
116+
this:temp_time_ref,
117+
this:temp_zc_ref ;
92118
bald:shape ( 5 47 180 600 ) ;
93119
CFTerms:coordinates this:latitude,
94120
this:longitude,
@@ -107,7 +133,10 @@ this:u a bald:Array ;
107133
this:unit_id <http://qudt.org/vocab/unit#MeterPerSecond> ;
108134
this:vector_components "u v" ;
109135
this:vector_name "Currents" ;
110-
bald:references this:u_time_ref ;
136+
bald:references this:u_latitude_ref,
137+
this:u_longitude_ref,
138+
this:u_time_ref,
139+
this:u_zc_ref ;
111140
bald:shape ( 5 47 180 600 ) ;
112141
CFTerms:coordinates this:latitude,
113142
this:longitude,
@@ -126,7 +155,10 @@ this:v a bald:Array ;
126155
this:unit_id <http://qudt.org/vocab/unit#MeterPerSecond> ;
127156
this:vector_components "u v" ;
128157
this:vector_name "Currents" ;
129-
bald:references this:v_time_ref ;
158+
bald:references this:v_latitude_ref,
159+
this:v_longitude_ref,
160+
this:v_time_ref,
161+
this:v_zc_ref ;
130162
bald:shape ( 5 47 180 600 ) ;
131163
CFTerms:coordinates this:latitude,
132164
this:longitude,
@@ -139,7 +171,9 @@ this:v a bald:Array ;
139171
NetCDF:valid_range ( -100.0 100.0 ) .
140172

141173
this:wspeed_u a bald:Array ;
142-
bald:references this:wspeed_u_time_ref ;
174+
bald:references this:wspeed_u_latitude_ref,
175+
this:wspeed_u_longitude_ref,
176+
this:wspeed_u_time_ref ;
143177
bald:shape ( 5 180 600 ) ;
144178
CFTerms:coordinates this:latitude,
145179
this:longitude,
@@ -150,7 +184,9 @@ this:wspeed_u a bald:Array ;
150184
NetCDF:valid_range ( -1000.0 1000.0 ) .
151185

152186
this:wspeed_v a bald:Array ;
153-
bald:references this:wspeed_v_time_ref ;
187+
bald:references this:wspeed_v_latitude_ref,
188+
this:wspeed_v_longitude_ref,
189+
this:wspeed_v_time_ref ;
154190
bald:shape ( 5 180 600 ) ;
155191
CFTerms:coordinates this:latitude,
156192
this:longitude,
@@ -160,36 +196,126 @@ this:wspeed_v a bald:Array ;
160196
NetCDF:units "ms-1" ;
161197
NetCDF:valid_range ( -1000.0 1000.0 ) .
162198

199+
this:eta_latitude_ref a bald:Reference,
200+
bald:Subject ;
201+
bald:array this:latitude ;
202+
bald:childBroadcast ( 1 180 600 ) .
203+
204+
this:eta_longitude_ref a bald:Reference,
205+
bald:Subject ;
206+
bald:array this:longitude ;
207+
bald:childBroadcast ( 1 180 600 ) .
208+
163209
this:eta_time_ref a bald:Reference,
164210
bald:Subject ;
165211
bald:array this:time ;
166212
bald:childBroadcast ( 5 1 1 ) .
167213

214+
this:salt_latitude_ref a bald:Reference,
215+
bald:Subject ;
216+
bald:array this:latitude ;
217+
bald:childBroadcast ( 1 1 180 600 ) .
218+
219+
this:salt_longitude_ref a bald:Reference,
220+
bald:Subject ;
221+
bald:array this:longitude ;
222+
bald:childBroadcast ( 1 1 180 600 ) .
223+
168224
this:salt_time_ref a bald:Reference,
169225
bald:Subject ;
170226
bald:array this:time ;
171227
bald:childBroadcast ( 5 1 1 1 ) .
172228

229+
this:salt_zc_ref a bald:Reference,
230+
bald:Subject ;
231+
bald:array this:zc ;
232+
bald:childBroadcast ( 1 47 1 1 ) .
233+
234+
this:temp_latitude_ref a bald:Reference,
235+
bald:Subject ;
236+
bald:array this:latitude ;
237+
bald:childBroadcast ( 1 1 180 600 ) .
238+
239+
this:temp_longitude_ref a bald:Reference,
240+
bald:Subject ;
241+
bald:array this:longitude ;
242+
bald:childBroadcast ( 1 1 180 600 ) .
243+
173244
this:temp_time_ref a bald:Reference,
174245
bald:Subject ;
175246
bald:array this:time ;
176247
bald:childBroadcast ( 5 1 1 1 ) .
177248

249+
this:temp_zc_ref a bald:Reference,
250+
bald:Subject ;
251+
bald:array this:zc ;
252+
bald:childBroadcast ( 1 47 1 1 ) .
253+
254+
this:u_latitude_ref a bald:Reference,
255+
bald:Subject ;
256+
bald:array this:latitude ;
257+
bald:childBroadcast ( 1 1 180 600 ) .
258+
259+
this:u_longitude_ref a bald:Reference,
260+
bald:Subject ;
261+
bald:array this:longitude ;
262+
bald:childBroadcast ( 1 1 180 600 ) .
263+
178264
this:u_time_ref a bald:Reference,
179265
bald:Subject ;
180266
bald:array this:time ;
181267
bald:childBroadcast ( 5 1 1 1 ) .
182268

269+
this:u_zc_ref a bald:Reference,
270+
bald:Subject ;
271+
bald:array this:zc ;
272+
bald:childBroadcast ( 1 47 1 1 ) .
273+
274+
this:v_latitude_ref a bald:Reference,
275+
bald:Subject ;
276+
bald:array this:latitude ;
277+
bald:childBroadcast ( 1 1 180 600 ) .
278+
279+
this:v_longitude_ref a bald:Reference,
280+
bald:Subject ;
281+
bald:array this:longitude ;
282+
bald:childBroadcast ( 1 1 180 600 ) .
283+
183284
this:v_time_ref a bald:Reference,
184285
bald:Subject ;
185286
bald:array this:time ;
186287
bald:childBroadcast ( 5 1 1 1 ) .
187288

289+
this:v_zc_ref a bald:Reference,
290+
bald:Subject ;
291+
bald:array this:zc ;
292+
bald:childBroadcast ( 1 47 1 1 ) .
293+
294+
this:wspeed_u_latitude_ref a bald:Reference,
295+
bald:Subject ;
296+
bald:array this:latitude ;
297+
bald:childBroadcast ( 1 180 600 ) .
298+
299+
this:wspeed_u_longitude_ref a bald:Reference,
300+
bald:Subject ;
301+
bald:array this:longitude ;
302+
bald:childBroadcast ( 1 180 600 ) .
303+
188304
this:wspeed_u_time_ref a bald:Reference,
189305
bald:Subject ;
190306
bald:array this:time ;
191307
bald:childBroadcast ( 5 1 1 ) .
192308

309+
this:wspeed_v_latitude_ref a bald:Reference,
310+
bald:Subject ;
311+
bald:array this:latitude ;
312+
bald:childBroadcast ( 1 180 600 ) .
313+
314+
this:wspeed_v_longitude_ref a bald:Reference,
315+
bald:Subject ;
316+
bald:array this:longitude ;
317+
bald:childBroadcast ( 1 180 600 ) .
318+
193319
this:wspeed_v_time_ref a bald:Reference,
194320
bald:Subject ;
195321
bald:array this:time ;

0 commit comments

Comments
 (0)