@@ -414,6 +414,9 @@ def rdfnode(self, graph):
414
414
selfnode = rdflib .URIRef (self .identity )
415
415
for attr in self .attrs :
416
416
objs = self .attrs [attr ]
417
+ if (isinstance (objs , np .ndarray )):
418
+ #try to convert np.ndarray to a list
419
+ objs = objs .tolist ()
417
420
if not (isinstance (objs , set ) or isinstance (objs , list )):
418
421
objs = set ([objs ])
419
422
for obj in objs :
@@ -438,6 +441,11 @@ def rdfgraph(self):
438
441
"""
439
442
graph = rdflib .Graph ()
440
443
graph .bind ('bald' , 'http://binary-array-ld.net/latest/' )
444
+ for prefix_name in self ._prefixes :
445
+ #strip the double underscore suffix
446
+ new_name = prefix_name [:- 2 ]
447
+
448
+ graph .bind (new_name , self ._prefixes [prefix_name ])
441
449
graph = self .rdfnode (graph )
442
450
443
451
return graph
@@ -530,75 +538,96 @@ def load(afilepath):
530
538
finally :
531
539
f .close ()
532
540
533
- def load_netcdf (afilepath , uri = None ):
541
+ def load_netcdf (afilepath , baseuri = None ):
534
542
"""
535
543
Validate a file with respect to binary-array-linked-data.
536
544
Returns a :class:`bald.validation.Validation`
537
545
"""
538
546
539
547
with load (afilepath ) as fhandle :
540
- prefix_group = (fhandle [fhandle .bald__isPrefixedBy ] if
548
+ prefix_var_name = None
549
+ if hasattr (fhandle , 'bald__isPrefixedBy' ):
550
+ prefix_var_name = fhandle .bald__isPrefixedBy
551
+
552
+ prefix_var = (fhandle [fhandle .bald__isPrefixedBy ] if
541
553
hasattr (fhandle , 'bald__isPrefixedBy' ) else {})
542
554
prefixes = {}
543
555
544
556
skipped_variables = []
545
- if prefix_group != {}:
546
- prefixes = (dict ([(prefix , getattr (prefix_group , prefix )) for
547
- prefix in prefix_group .ncattrs ()]))
548
- if isinstance (prefix_group , netCDF4 ._netCDF4 .Variable ):
549
- skipped_variables .append (prefix_group .name )
557
+ if prefix_var != {}:
558
+ prefixes = (dict ([(prefix , getattr (prefix_var , prefix )) for
559
+ prefix in prefix_var .ncattrs ()]))
560
+ if isinstance (prefix_var , netCDF4 ._netCDF4 .Variable ):
561
+ skipped_variables .append (prefix_var .name )
550
562
else :
551
563
for k in fhandle .ncattrs ():
552
564
if k .endswith ('__' ):
553
565
prefixes [k ] = getattr (fhandle , k )
554
- alias_group = (fhandle [fhandle .bald__isAliasedBy ]
566
+
567
+ # check that default set is handled, i.e. bald__ and rdf__
568
+ if 'bald__' not in prefixes :
569
+ prefixes ['bald__' ] = "http://binary-array-ld.net/latest/"
570
+
571
+ if 'rdf__' not in prefixes :
572
+ prefixes ['rdf__' ] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
573
+
574
+ alias_var_name = None
575
+ if hasattr (fhandle , 'bald__isAliasedBy' ):
576
+ alias_var_name = fhandle .bald__isAliasedBy
577
+
578
+ alias_var = (fhandle [fhandle .bald__isAliasedBy ]
555
579
if hasattr (fhandle , 'bald__isAliasedBy' ) else {})
556
580
aliases = {}
557
- if alias_group != {}:
558
- aliases = (dict ([(alias , getattr (alias_group , alias ))
559
- for alias in alias_group .ncattrs ()]))
560
- if isinstance (alias_group , netCDF4 ._netCDF4 .Variable ):
561
- skipped_variables .append (alias_group .name )
581
+ if alias_var != {}:
582
+ aliases = (dict ([(alias , getattr (alias_var , alias ))
583
+ for alias in alias_var .ncattrs ()]))
584
+ if isinstance (alias_var , netCDF4 ._netCDF4 .Variable ):
585
+ skipped_variables .append (alias_var .name )
562
586
563
587
attrs = {}
564
588
for k in fhandle .ncattrs ():
565
589
attrs [k ] = getattr (fhandle , k )
566
590
# It would be nice to use the URI of the file if it is known.
567
- if uri is not None :
568
- identity = uri
591
+ if baseuri is not None :
592
+ identity = baseuri
569
593
else :
570
594
identity = 'root'
571
595
root_container = Container (identity , attrs , prefixes = prefixes ,
572
596
aliases = aliases )
573
597
root_container .attrs ['bald__contains' ] = []
574
598
file_variables = {}
575
599
for name in fhandle .variables :
600
+ if name == prefix_var_name or name == alias_var_name :
601
+ continue
576
602
577
603
sattrs = fhandle .variables [name ].__dict__ .copy ()
578
604
# inconsistent use of '/'; fix it
579
605
identity = name
606
+ if baseuri is not None :
607
+ identity = baseuri + "/" + name
580
608
581
609
# netCDF coordinate variable special case
582
610
if (len (fhandle .variables [name ].dimensions ) == 1 and
583
611
fhandle .variables [name ].dimensions [0 ] == name ):
584
- sattrs ['bald__array' ] = name
612
+ #sattrs['bald__array'] = name
613
+ sattrs ['bald__array' ] = identity
585
614
sattrs ['rdf__type' ] = 'bald__Reference'
615
+
586
616
if fhandle .variables [name ].shape :
587
617
sattrs ['bald__shape' ] = fhandle .variables [name ].shape
588
618
var = Array (identity , sattrs , prefixes = prefixes , aliases = aliases )
589
619
else :
590
620
var = Subject (identity , sattrs , prefixes = prefixes , aliases = aliases )
591
- if name not in skipped_variables :
592
- # Don't include skipped variables, such as prefix or alias
593
- # variables, within the containment relation.
594
- root_container .attrs ['bald__contains' ].append (var )
595
-
621
+ root_container .attrs ['bald__contains' ].append (var )
596
622
file_variables [name ] = var
597
623
598
624
599
625
600
626
# cycle again and find references
601
627
for name in fhandle .variables :
628
+ if name == prefix_var_name or name == alias_var_name :
629
+ continue
630
+
602
631
var = file_variables [name ]
603
632
# reverse lookup based on type to be added
604
633
lookups = ['bald__references' , 'bald__array' ]
@@ -624,6 +653,8 @@ def load_netcdf(afilepath, uri=None):
624
653
# Else, define a bald:childBroadcast
625
654
else :
626
655
identity = '{}_{}_ref' .format (name , dim )
656
+ if baseuri is not None :
657
+ identity = baseuri + '/' + '{}_{}_ref' .format (name , dim )
627
658
rattrs = {}
628
659
rattrs ['rdf__type' ] = 'bald__Reference'
629
660
reshape = [1 for adim in var_shape ]
0 commit comments