@@ -476,6 +476,8 @@ def graph_elems(self):
476
476
477
477
if hasattr (self , 'bald__array' ):
478
478
for aref in self .bald__array :
479
+ if isinstance (aref , str ):
480
+ raise TypeError ('unexpected string: {}' .format (aref ))
479
481
alink = "link({var}, {target}, 'bald__array', 'bottom');"
480
482
alink = alink .format (var = self .identity , target = aref .identity )
481
483
links .append (alink )
@@ -511,19 +513,6 @@ def graph_elems(self):
511
513
return instances , links
512
514
513
515
514
- @contextlib .contextmanager
515
- def load (afilepath ):
516
- if afilepath .endswith ('.hdf' ):
517
- loader = h5py .File
518
- else :
519
- raise ValueError ('filepath suffix not supported' )
520
- try :
521
- f = loader (afilepath , "r" )
522
- yield f
523
- finally :
524
- f .close ()
525
-
526
-
527
516
@contextlib .contextmanager
528
517
def load (afilepath ):
529
518
if afilepath .endswith ('.hdf' ):
@@ -540,11 +529,13 @@ def load(afilepath):
540
529
541
530
def load_netcdf (afilepath , baseuri = None ):
542
531
"""
543
- Validate a file with respect to binary-array-linked-data.
544
- Returns a :class:`bald.validation.Validation `
532
+ Load a file with respect to binary-array-linked-data.
533
+ Returns a :class:`bald.Collection `
545
534
"""
546
535
547
536
with load (afilepath ) as fhandle :
537
+ if baseuri is None :
538
+ baseuri = 'file://{}' .format (afilepath )
548
539
prefix_var_name = None
549
540
if hasattr (fhandle , 'bald__isPrefixedBy' ):
550
541
prefix_var_name = fhandle .bald__isPrefixedBy
@@ -609,8 +600,7 @@ def load_netcdf(afilepath, baseuri=None):
609
600
# netCDF coordinate variable special case
610
601
if (len (fhandle .variables [name ].dimensions ) == 1 and
611
602
fhandle .variables [name ].dimensions [0 ] == name ):
612
- #sattrs['bald__array'] = name
613
- sattrs ['bald__array' ] = identity
603
+ sattrs ['bald__array' ] = name
614
604
sattrs ['rdf__type' ] = 'bald__Reference'
615
605
616
606
if fhandle .variables [name ].shape :
@@ -629,18 +619,42 @@ def load_netcdf(afilepath, baseuri=None):
629
619
continue
630
620
631
621
var = file_variables [name ]
632
- # reverse lookup based on type to be added
633
- lookups = ['bald__references' , 'bald__array' ]
634
- for lookup in lookups :
635
- if lookup in var .attrs :
636
- child_dset_set = var .attrs [lookup ].split (' ' )
637
- var .attrs [lookup ] = set ()
638
- for child_dset_name in child_dset_set :
639
- carray = file_variables .get (child_dset_name )
640
- var .attrs [lookup ].add (carray )
622
+ sattrs = fhandle .variables [name ].__dict__ .copy ()
623
+ # netCDF coordinate variable special case
624
+ if (len (fhandle .variables [name ].dimensions ) == 1 and
625
+ fhandle .variables [name ].dimensions [0 ] == name ):
626
+ sattrs ['bald__array' ] = name
627
+
628
+ for sattr in sattrs :
629
+ if (isinstance (sattrs [sattr ], str ) and
630
+ file_variables .get (sattrs [sattr ])):
631
+ # next: remove all use of set, everything is dict or orderedDict
632
+ var .attrs [sattr ] = set ((file_variables .get (sattrs [sattr ]),))
633
+ elif isinstance (sattrs [sattr ], str ):
634
+ potrefs_list = sattrs [sattr ].split (',' )
635
+ potrefs_set = sattrs [sattr ].split (' ' )
636
+ if len (potrefs_list ) > 1 :
637
+ refs = np .array ([file_variables .get (pref ) is not None
638
+ for pref in potrefs_list ])
639
+ if np .all (refs ):
640
+ var .attrs [sattr ] = [file_variables .get (pref )
641
+ for pref in potrefs_list ]
642
+
643
+ elif len (potrefs_set ) > 1 :
644
+ refs = np .array ([file_variables .get (pref ) is not None
645
+ for pref in potrefs_set ])
646
+ if np .all (refs ):
647
+ var .attrs [sattr ] = set ([file_variables .get (pref )
648
+ for pref in potrefs_set ])
649
+
650
+ # if name == 'pdim0':
651
+ # import pdb; pdb.set_trace()
652
+
641
653
# coordinate variables are bald__references except for
642
654
# variables that already declare themselves as bald__Reference
643
655
if 'bald__Reference' not in var .rdf__type :
656
+ # if name == 'pdim0':
657
+ # import pdb; pdb.set_trace()
644
658
for dim in fhandle .variables [name ].dimensions :
645
659
if file_variables .get (dim ):
646
660
cv_shape = fhandle .variables [dim ].shape
0 commit comments