Skip to content

Commit 38e8b9d

Browse files
committed
refactor, tidy, test fixes
1 parent 8722778 commit 38e8b9d

File tree

4 files changed

+77
-93
lines changed

4 files changed

+77
-93
lines changed

lib/bald/__init__.py

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -950,28 +950,21 @@ 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-
# import pdb; pdb.set_trace()
956-
# var.attrs[sattr] = set((file_variables.get(sattrs[sattr]),))
957-
# el
958953
if isinstance(sattrs[sattr], six.string_types):
959954

960955
if sattrs[sattr].startswith('(') and sattrs[sattr].endswith(')'):
961956
potrefs_list = sattrs[sattr].lstrip('( ').rstrip(' )').split(' ')
962-
#if len(potrefs_list) > 1:
963957
refs = np.array([file_variables.get(pref) is not None
964958
for pref in potrefs_list])
965959
if np.all(refs):
966960
var.attrs[sattr] = [file_variables.get(pref)
967961
for pref in potrefs_list]
968962
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]
963+
_make_ref_entities(var, fhandle,
964+
pref, name, baseuri,
965+
root_container,
966+
file_variables, prefixes,
967+
aliases, aliasgraph)
975968

976969
else:
977970
potrefs_set = sattrs[sattr].split(' ')
@@ -981,80 +974,62 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
981974
var.attrs[sattr] = set([file_variables.get(pref)
982975
for pref in potrefs_set])
983976
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-
1020-
1021-
# coordinate variables are bald__references except for
1022-
# variables that already declare themselves as bald__Reference
977+
_make_ref_entities(var, fhandle,
978+
pref, name, baseuri,
979+
root_container,
980+
file_variables, prefixes,
981+
aliases, aliasgraph)
982+
983+
984+
# coordinate variables are bald__references too
1023985
if 'bald__Reference' not in var.rdf__type:
1024986
for dim in fhandle.variables[name].dimensions:
1025987
if file_variables.get(dim) and name != dim:
1026-
cv_shape = fhandle.variables[dim].shape
1027-
var_shape = fhandle.variables[name].shape
1028-
refset = var.attrs.get('bald__references', set())
1029-
# Only the dimension defining the last dimension will
1030-
# broadcase correctly. But create all references
1031-
# equally, for consistency.
1032-
identity = '{}_{}_ref'.format(name, dim)
1033-
1034-
rattrs = {}
1035-
rattrs['rdf__type'] = 'bald__Reference'
1036-
reshape = [1 for adim in var_shape]
1037-
1038-
cvi = fhandle.variables[name].dimensions.index(dim)
1039-
reshape[cvi] = int(fhandle.variables[dim].size)
1040-
1041-
rattrs['bald__childBroadcast'] = reshape
1042-
rattrs['bald__array'] = set((file_variables.get(dim),))
1043-
ref_node = Subject(baseuri, identity, rattrs,
1044-
prefixes=prefixes,
1045-
aliases=aliases,
1046-
alias_graph=aliasgraph)
1047-
root_container.attrs['bald__contains'].add(ref_node)
1048-
file_variables[identity] = ref_node
1049-
refset.add(ref_node)
1050-
# br = careful_update(var.attrs.get('bald__references',
1051-
# {}), refset)
1052-
# var.attrs['bald__references'] = br
1053-
var.attrs['bald__references'] = refset
1054-
988+
_make_ref_entities(var, fhandle, dim, name,
989+
baseuri, root_container,
990+
file_variables, prefixes,
991+
aliases, aliasgraph)
1055992

1056993
return root_container
1057994

995+
def _make_ref_entities(var, fhandle, pref, name, baseuri,
996+
root_container, file_variables,
997+
prefixes, aliases, aliasgraph):
998+
shapematch = (fhandle.variables[name].shape ==
999+
fhandle.variables[pref].shape)
1000+
1001+
if (fhandle.variables[name].shape and not shapematch and
1002+
fhandle.variables[pref].shape):
1003+
try:
1004+
refset = var.attrs.get('bald__references', set())
1005+
cv_shape = fhandle.variables[pref].shape
1006+
var_shape = fhandle.variables[name].shape
1007+
identity = '{}_{}_ref'.format(name, pref)
1008+
rattrs = {}
1009+
rattrs['rdf__type'] = 'bald__Reference'
1010+
reshape = [1 for adim in var_shape]
1011+
1012+
dims = fhandle.variables[pref].dimensions
1013+
for dim in dims:
1014+
cvi = fhandle.variables[name].dimensions.index(dim)
1015+
reshape[cvi] = int(fhandle.dimensions[dim].size)
1016+
rattrs['bald__childBroadcast'] = reshape
1017+
rattrs['bald__array'] = set((file_variables.get(pref),))
1018+
ref_node = Subject(baseuri, identity, rattrs,
1019+
prefixes=prefixes,
1020+
aliases=aliases,
1021+
alias_graph=aliasgraph)
1022+
root_container.attrs['bald__contains'].add(ref_node)
1023+
file_variables[identity] = ref_node
1024+
refset.add(ref_node)
1025+
var.attrs['bald__references'] = refset
1026+
except ValueError:
1027+
# Indexing and dimension identification can fail, especially
1028+
# with unexpectedy formated files. Fail silently on load, to
1029+
# that a partial graph may be returned. Issues like this are
1030+
# deferred to validation.
1031+
pass
1032+
10581033

10591034
def validate_netcdf(afilepath, baseuri=None, cache=None, uris_resolve=False):
10601035
"""

0 commit comments

Comments
 (0)