@@ -402,6 +402,8 @@ def unpack_predicate(self, astring):
402
402
return result
403
403
404
404
def unpack_rdfobject (self , astring , predicate ):
405
+ # if astring == 'x_wind':
406
+ # import pdb; pdb.set_trace()
405
407
result = astring
406
408
if isinstance (astring , six .string_types ) and self ._prefix_suffix .match (astring ):
407
409
prefix , suffix = self ._prefix_suffix .match (astring ).groups ()
@@ -431,6 +433,8 @@ def unpack_rdfobject(self, astring, predicate):
431
433
result = str (results [0 ][0 ])
432
434
except pyparsing .ParseException :
433
435
pass
436
+ except ValueError :
437
+ pass
434
438
return result
435
439
436
440
# def unpack_uri(self, astring):
@@ -900,7 +904,9 @@ def _prefixes_and_aliases(fhandle, identity, alias_dict, cache):
900
904
return prefixes , aliases , aliasgraph , prefix_var_name
901
905
902
906
903
- def _load_netcdf_group (agroup , baseuri , gk , root_container , prefixes , prefix_group_name , aliases , aliasgraph , cache ):
907
+ def _load_netcdf_group (fhandle , agroup , baseuri , gk , root_container , file_variables , prefixes , prefix_group_name , aliases , aliasgraph , cache ):
908
+ file_variables = file_variables .copy ()
909
+
904
910
gattrs = {}
905
911
for k in agroup .ncattrs ():
906
912
gattrs [k ] = getattr (agroup , k )
@@ -912,47 +918,47 @@ def _load_netcdf_group(agroup, baseuri, gk, root_container, prefixes, prefix_gro
912
918
913
919
gcontainer .attrs ['bald__contains' ] = set ()
914
920
915
- _load_netcdf_group_vars (agroup , gcontainer , gidentity , gattrs , prefixes , prefix_group_name , aliases , aliasgraph , cache )
921
+ _load_netcdf_group_vars (fhandle , agroup , gcontainer , gidentity , gattrs , file_variables , prefixes , prefix_group_name , aliases , aliasgraph , cache )
916
922
if 'bald__contains' not in root_container .attrs :
917
923
root_container .attrs ['bald__contains' ] = set ()
918
924
root_container .attrs ['bald__contains' ].add (gcontainer )
919
925
for gk in agroup .groups :
920
926
921
- _load_netcdf_group (agroup .groups [gk ], baseuri , gk , gcontainer ,
927
+ _load_netcdf_group (fhandle , agroup .groups [gk ], baseuri , gk , gcontainer , file_variables ,
922
928
prefixes , prefix_group_name , aliases , aliasgraph , cache )
923
929
924
930
925
931
926
- def _load_netcdf_group_vars (fhandle , root_container , baseuri , attrs , prefixes ,
932
+ def _load_netcdf_group_vars (fhandle , agroup , root_container , baseuri , attrs , file_variables , prefixes ,
927
933
prefix_var_name , aliases , aliasgraph , cache ):
928
- file_variables = {}
929
- for name in fhandle .variables :
934
+
935
+ for name in agroup .variables :
930
936
if name == prefix_var_name :
931
937
continue
932
938
933
- sattrs = fhandle .variables [name ].__dict__ .copy ()
939
+ sattrs = agroup .variables [name ].__dict__ .copy ()
934
940
935
941
identity = name
936
942
if baseuri is not None :
937
943
identity = baseuri + name
938
944
939
945
# netCDF coordinate variable special case
940
- if (len (fhandle .variables [name ].dimensions ) == 1 and
941
- fhandle .variables [name ].dimensions [0 ] == name and
942
- len (fhandle .variables [name ]) > 0 ):
946
+ if (len (agroup .variables [name ].dimensions ) == 1 and
947
+ agroup .variables [name ].dimensions [0 ] == name and
948
+ len (agroup .variables [name ]) > 0 ):
943
949
944
- if not isinstance (fhandle .variables [name ][0 ], np .ma .core .MaskedConstant ):
945
- sattrs ['bald__first_value' ] = fhandle .variables [name ][0 ]
950
+ if not isinstance (agroup .variables [name ][0 ], np .ma .core .MaskedConstant ):
951
+ sattrs ['bald__first_value' ] = agroup .variables [name ][0 ]
946
952
if isinstance (sattrs ['bald__first_value' ], str ):
947
953
pass
948
954
949
955
elif np .issubdtype (sattrs ['bald__first_value' ], np .integer ):
950
956
sattrs ['bald__first_value' ] = int (sattrs ['bald__first_value' ])
951
957
elif np .issubdtype (sattrs ['bald__first_value' ], np .floating ):
952
958
sattrs ['bald__first_value' ] = float (sattrs ['bald__first_value' ])
953
- if (len (fhandle .variables [name ]) > 1 and
954
- not isinstance (fhandle .variables [name ][- 1 ], np .ma .core .MaskedConstant )):
955
- sattrs ['bald__last_value' ] = fhandle .variables [name ][- 1 ]
959
+ if (len (agroup .variables [name ]) > 1 and
960
+ not isinstance (agroup .variables [name ][- 1 ], np .ma .core .MaskedConstant )):
961
+ sattrs ['bald__last_value' ] = agroup .variables [name ][- 1 ]
956
962
if isinstance (sattrs ['bald__last_value' ], str ):
957
963
pass
958
964
elif np .issubdtype (sattrs ['bald__last_value' ], np .integer ):
@@ -961,8 +967,8 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
961
967
sattrs ['bald__last_value' ] = float (sattrs ['bald__last_value' ])
962
968
963
969
# datetime special case
964
- if 'units' in fhandle .variables [name ].ncattrs ():
965
- ustr = fhandle .variables [name ].getncattr ('units' )
970
+ if 'units' in agroup .variables [name ].ncattrs ():
971
+ ustr = agroup .variables [name ].getncattr ('units' )
966
972
pattern = '^([a-z]+) since ([0-9T:\\ . -]+)'
967
973
968
974
amatch = re .match (pattern , ustr )
@@ -973,15 +979,15 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
973
979
tog = datetime .parse_datetime (origin ,
974
980
calendar = ig )
975
981
if tog is not None :
976
- dtype = '{}{}' .format (fhandle .variables [name ].dtype .kind ,
977
- fhandle .variables [name ].dtype .itemsize )
982
+ dtype = '{}{}' .format (agroup .variables [name ].dtype .kind ,
983
+ agroup .variables [name ].dtype .itemsize )
978
984
fv = netCDF4 .default_fillvals .get (dtype )
979
985
first = None
980
- if fhandle .variables [name ][0 ] == fv :
981
- first = np .ma .MaskedArray (fhandle .variables [name ][0 ],
986
+ if agroup .variables [name ][0 ] == fv :
987
+ first = np .ma .MaskedArray (agroup .variables [name ][0 ],
982
988
mask = True )
983
989
else :
984
- first = fhandle .variables [name ][0 ]
990
+ first = agroup .variables [name ][0 ]
985
991
if first is not None :
986
992
try :
987
993
first = int (first )
@@ -992,12 +998,12 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
992
998
epoch = tog )
993
999
if first is not np .ma .masked :
994
1000
sattrs ['bald__first_value' ] = edate_first
995
- if len (fhandle .variables [name ]) > 1 :
996
- if fhandle .variables [name ][0 ] == fv :
997
- last = np .ma .MaskedArray (fhandle .variables [name ][- 1 ],
1001
+ if len (agroup .variables [name ]) > 1 :
1002
+ if agroup .variables [name ][0 ] == fv :
1003
+ last = np .ma .MaskedArray (agroup .variables [name ][- 1 ],
998
1004
mask = True )
999
1005
else :
1000
- last = fhandle .variables [name ][- 1 ]
1006
+ last = agroup .variables [name ][- 1 ]
1001
1007
if last :
1002
1008
try :
1003
1009
last = round (last )
@@ -1013,8 +1019,8 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
1013
1019
1014
1020
1015
1021
1016
- if fhandle .variables [name ].shape :
1017
- sattrs ['bald__shape' ] = list (fhandle .variables [name ].shape )
1022
+ if agroup .variables [name ].shape :
1023
+ sattrs ['bald__shape' ] = list (agroup .variables [name ].shape )
1018
1024
var = Array (baseuri , name , sattrs , prefixes = prefixes ,
1019
1025
aliases = aliases , alias_graph = aliasgraph )
1020
1026
else :
@@ -1024,8 +1030,22 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
1024
1030
1025
1031
file_variables [name ] = var
1026
1032
1033
+ for prefix in prefixes :
1034
+ if prefixes [prefix ].startswith ('http' ):
1035
+ # print('parsing: {}'.format(prefixes[prefix][:-1]))
1036
+ try :
1037
+ aliasgraph .parse (data = cache [prefixes [prefix ][:- 1 ]].text , format = 'xml' )
1038
+ # print('parsed: {}'.format(prefixes[prefix][:-1]))
1039
+ except Exception :
1040
+ try :
1041
+ aliasgraph .parse (data = cache [prefixes [prefix ][:- 1 ]].text , format = 'n3' )
1042
+ # print('parsed: {} (n3)'.format(prefixes[prefix][:-1]))
1043
+ except Exception :
1044
+ pass
1045
+
1027
1046
reference_prefixes = dict ()
1028
- reference_graph = copy .copy (aliasgraph )
1047
+ # reference_graph = copy.copy(aliasgraph)
1048
+ reference_graph = aliasgraph
1029
1049
1030
1050
response = cache ['https://www.opengis.net/def/binary-array-ld' ]
1031
1051
reference_graph .parse (data = response .text , format = 'n3' )
@@ -1081,18 +1101,19 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
1081
1101
ref_prefs = [str (ref [0 ]) for ref in list (refs )]
1082
1102
1083
1103
# cycle again and find references
1084
- for name in fhandle .variables :
1104
+ for name in agroup .variables :
1105
+
1085
1106
if name == prefix_var_name :
1086
1107
continue
1087
1108
1088
1109
var = file_variables [name ]
1089
- sattrs = fhandle .variables [name ].__dict__ .copy ()
1110
+ sattrs = agroup .variables [name ].__dict__ .copy ()
1090
1111
1091
1112
# coordinate variables are bald__references too
1092
1113
if 'bald__Reference' not in var .rdf__type :
1093
- for dim in fhandle .variables [name ].dimensions :
1114
+ for dim in agroup .variables [name ].dimensions :
1094
1115
if file_variables .get (dim ) and name != dim :
1095
- _make_ref_entities (var , fhandle , dim , name ,
1116
+ _make_ref_entities (var , fhandle , agroup , dim , name ,
1096
1117
baseuri , root_container ,
1097
1118
file_variables , prefixes ,
1098
1119
aliases , aliasgraph )
@@ -1111,7 +1132,7 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
1111
1132
var .attrs [sattr ] = [file_variables .get (pref )
1112
1133
for pref in potrefs_list ]
1113
1134
for pref in potrefs_list :
1114
- _make_ref_entities (var , fhandle ,
1135
+ _make_ref_entities (var , fhandle , agroup ,
1115
1136
pref , name , baseuri ,
1116
1137
root_container ,
1117
1138
file_variables , prefixes ,
@@ -1126,8 +1147,8 @@ def _load_netcdf_group_vars(fhandle, root_container, baseuri, attrs, prefixes,
1126
1147
for pref in potrefs_set ])
1127
1148
for pref in potrefs_set :
1128
1149
# coordinate variables already handled
1129
- if pref not in fhandle .variables [name ].dimensions :
1130
- _make_ref_entities (var , fhandle ,
1150
+ if pref not in agroup .variables [name ].dimensions :
1151
+ _make_ref_entities (var , fhandle , agroup ,
1131
1152
pref , name , baseuri ,
1132
1153
root_container ,
1133
1154
file_variables , prefixes ,
@@ -1166,45 +1187,73 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None, file_locat
1166
1187
file_resource = True , file_locator = file_locator )
1167
1188
1168
1189
root_container .attrs ['bald__contains' ] = set ()
1169
-
1170
- _load_netcdf_group_vars (fhandle , root_container , baseuri , attrs , prefixes ,
1190
+
1191
+ file_variables = {}
1192
+ _load_netcdf_group_vars (fhandle , fhandle , root_container , baseuri , attrs , file_variables , prefixes ,
1171
1193
prefix_group_name , aliases , aliasgraph , cache )
1172
1194
1173
1195
for gk in fhandle .groups :
1174
1196
if gk == prefix_group_name :
1175
1197
continue
1176
1198
1177
- _load_netcdf_group (fhandle .groups [gk ], baseuri , gk , root_container ,
1199
+ _load_netcdf_group (fhandle , fhandle .groups [gk ], baseuri , gk , root_container , file_variables ,
1178
1200
prefixes , prefix_group_name , aliases , aliasgraph , cache )
1179
1201
# _create_references(root_container,
1180
1202
# prefixes, prefix_group_name, aliases, aliasgraph, cache)
1181
1203
1182
1204
return root_container
1183
1205
1184
- def _make_ref_entities (var , fhandle , pref , name , baseuri ,
1206
+ def _make_ref_entities (var , fhandle , variables , pref , name , baseuri ,
1185
1207
root_container , file_variables ,
1186
1208
prefixes , aliases , aliasgraph ):
1187
- shapematch = (fhandle .variables [name ].shape ==
1188
- fhandle .variables [pref ].shape )
1209
+ namevar = None
1210
+ prefvar = None
1211
+ try :
1212
+ prefvar = variables [pref ]
1213
+ except IndexError :
1214
+ try :
1215
+ if not pref .startswith ('/' ):
1216
+ ppref = '/' + pref
1217
+ prefvar = fhandle [ppref ]
1218
+ except IndexError :
1219
+ pass
1220
+ try :
1221
+ namevar = variables [name ]
1222
+ except IndexError :
1223
+ try :
1224
+ if not name .startswith ('/' ):
1225
+ nname = '/' + name
1226
+ namevar = fhandle [nname ]
1227
+ except IndexError :
1228
+ pass
1189
1229
1190
- if (fhandle .variables [name ].shape and not shapematch and
1191
- fhandle .variables [pref ].shape ):
1230
+ # if pref in variables:
1231
+ # prefvar = variables[pref]
1232
+ # elif pref in fhandle:
1233
+ # prefvar = fhandle[pref]
1234
+ # if name in variables:
1235
+ # namevar = variables[name]
1236
+ # elif name in fhandle:
1237
+ # namevar = fhandle[name]
1238
+ shapematch = (namevar .shape == prefvar .shape )
1239
+
1240
+ if (namevar is not None and prefvar is not None and namevar .shape and not shapematch and
1241
+ prefvar .shape ):
1192
1242
try :
1193
1243
refset = var .attrs .get ('bald__references' , set ())
1194
1244
if not isinstance (refset , set ):
1195
1245
refset = set ((refset ,))
1196
1246
identity = None
1197
1247
rattrs = {}
1198
1248
1199
- reshapes = netcdf_shared_dimensions (fhandle .variables [name ],
1200
- fhandle .variables [pref ])
1249
+ reshapes = netcdf_shared_dimensions (namevar , prefvar )
1201
1250
1202
- rattrs ['bald__targetShape' ] = list (fhandle . variables [ pref ] .shape )
1251
+ rattrs ['bald__targetShape' ] = list (prefvar .shape )
1203
1252
sourceReshape = [i [1 ] for i in reshapes ['sourceReshape' ].items ()]
1204
- if sourceReshape != list (fhandle . variables [ name ] .shape ):
1253
+ if sourceReshape != list (namevar .shape ):
1205
1254
rattrs ['bald__sourceReshape' ] = sourceReshape
1206
1255
targetReshape = [i [1 ] for i in reshapes ['targetReshape' ].items ()]
1207
- if targetReshape != list (fhandle . variables [ pref ] .shape ):
1256
+ if targetReshape != list (prefvar .shape ):
1208
1257
rattrs ['bald__targetReshape' ] = targetReshape
1209
1258
rattrs ['bald__target' ] = set ((file_variables .get (pref ),))
1210
1259
ref_node = Reference (baseuri , identity , rattrs ,
@@ -1214,13 +1263,14 @@ def _make_ref_entities(var, fhandle, pref, name, baseuri,
1214
1263
1215
1264
refset .add (ref_node )
1216
1265
var .attrs ['bald__references' ] = refset
1266
+ # Indexing and dimension identification can fail, especially
1267
+ # with unexpectedy formated files. Fail silently on load, to
1268
+ # that a partial graph may be returned. Issues like this are
1269
+ # deferred to validation.
1217
1270
except ValueError :
1218
- # Indexing and dimension identification can fail, especially
1219
- # with unexpectedy formated files. Fail silently on load, to
1220
- # that a partial graph may be returned. Issues like this are
1221
- # deferred to validation.
1222
1271
pass
1223
-
1272
+ # except IndexError:
1273
+ # pass
1224
1274
1225
1275
def validate_netcdf (afilepath , baseuri = None , cache = None , uris_resolve = False ):
1226
1276
"""
0 commit comments