Skip to content

Commit 4cf1112

Browse files
committed
Refactoring alias and prefix handling to variable
1 parent 61221a4 commit 4cf1112

File tree

6 files changed

+246
-8
lines changed

6 files changed

+246
-8
lines changed

lib/bald/__init__.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ def __getitem__(self, item):
240240

241241
def check_uri(self, uri):
242242
result = False
243+
#print("Checking uri: " + uri)
243244
if self[uri].status_code == 200:
244245
result = True
245246
return result
@@ -438,6 +439,11 @@ def rdfgraph(self):
438439
"""
439440
graph = rdflib.Graph()
440441
graph.bind('bald', 'http://binary-array-ld.net/latest/')
442+
for prefix_name in self._prefixes:
443+
#strip the double underscore suffix
444+
new_name = prefix_name[:-2]
445+
446+
graph.bind(new_name, self._prefixes[prefix_name])
441447
graph = self.rdfnode(graph)
442448

443449
return graph
@@ -537,22 +543,34 @@ def load_netcdf(afilepath, uri=None):
537543
"""
538544

539545
with load(afilepath) as fhandle:
540-
prefix_group = (fhandle[fhandle.bald__isPrefixedBy] if
546+
prefix_var_name = None
547+
if hasattr(fhandle, 'bald__isPrefixedBy'):
548+
prefix_var_name = fhandle.bald__isPrefixedBy
549+
550+
prefix_var = (fhandle[fhandle.bald__isPrefixedBy] if
541551
hasattr(fhandle, 'bald__isPrefixedBy') else {})
542552
prefixes = {}
543-
if prefix_group:
544-
prefixes = (dict([(prefix, getattr(prefix_group, prefix)) for
545-
prefix in prefix_group.ncattrs()]))
553+
if prefix_var is not {} :
554+
prefixes = (dict([(prefix, getattr(prefix_var, prefix)) for
555+
prefix in prefix_var.ncattrs()]))
546556
else:
547557
for k in fhandle.ncattrs():
548558
if k.endswith('__'):
549559
prefixes[k] = getattr(fhandle, k)
550-
alias_group = (fhandle[fhandle.bald__isAliasedBy]
560+
561+
#print(prefixes)
562+
563+
alias_var_name = None
564+
if hasattr(fhandle, 'bald__isAliasedBy'):
565+
alias_var_name = fhandle.bald__isAliasedBy
566+
567+
alias_var = (fhandle[fhandle.bald__isAliasedBy]
551568
if hasattr(fhandle, 'bald__isAliasedBy') else {})
552569
aliases = {}
553-
if alias_group:
554-
aliases = (dict([(alias, getattr(alias_group, alias))
555-
for alias in alias_group.ncattrs()]))
570+
if alias_var != {}:
571+
aliases = (dict([(alias, getattr(alias_var, alias))
572+
for alias in alias_var.ncattrs()]))
573+
#print(aliases)
556574

557575
attrs = {}
558576
for k in fhandle.ncattrs():
@@ -567,6 +585,10 @@ def load_netcdf(afilepath, uri=None):
567585
root_container.attrs['bald__contains'] = []
568586
file_variables = {}
569587
for name in fhandle.variables:
588+
#print(name)
589+
if name == prefix_var_name or name == alias_var_name:
590+
#print("Skipping " + name)
591+
continue
570592

571593
sattrs = fhandle.variables[name].__dict__.copy()
572594
# inconsistent use of '/'; fix it
@@ -590,6 +612,10 @@ def load_netcdf(afilepath, uri=None):
590612

591613
# cycle again and find references
592614
for name in fhandle.variables:
615+
if name == prefix_var_name or name == alias_var_name:
616+
#print("Skipping " + name)
617+
continue
618+
593619
var = file_variables[name]
594620
# reverse lookup based on type to be added
595621
lookups = ['bald__references', 'bald__array']
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
netcdf tmpMwXy8U {
2+
dimensions:
3+
pdim0 = 11 ;
4+
pdim1 = 17 ;
5+
variables:
6+
int prefix_list ;
7+
prefix_list:bald__ = "http://binary-array-ld.net/latest/" ;
8+
prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
9+
10+
int alias_list ;
11+
alias_list:SDN_ParameterDiscoveryCode = "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#SDN_ParameterDiscoveryCode" ;
12+
alias_list:BactTaxaAbundSed = "http://vocab.nerc.ac.uk/collection/P02/current/BAUC/" ;
13+
alias_list:standard_name = "https://def.scitools.org.uk/CFTerms/standard_name" ;
14+
alias_list:air_temperature = "http://vocab.nerc.ac.uk/collection/P07/current/CFSN0023/" ;
15+
16+
int parent_variable(pdim0, pdim1) ;
17+
parent_variable:rdf__type = "bald__Array" ;
18+
parent_variable:SDN_ParameterDiscoveryCode = "BactTaxaAbundSed" ;
19+
parent_variable:submursible_name = "Nautilus" ;
20+
21+
int temp(pdim0, pdim1) ;
22+
temp:standard_name = "air_temperature" ;
23+
24+
// global attributes:
25+
:_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ;
26+
:rdf__type = "bald__Container" ;
27+
:bald__isPrefixedBy = "prefix_list" ;
28+
:bald__isAliasedBy = "alias_list" ;
29+
data:
30+
31+
parent_variable =
32+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
33+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
34+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
35+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
36+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
37+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
38+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
39+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
40+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
41+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
42+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
43+
44+
temp =
45+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
46+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
47+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
48+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
49+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
50+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
51+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
52+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
53+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
54+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
55+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
56+
57+
58+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
netcdf tmpMwXy8U {
2+
dimensions:
3+
pdim0 = 11 ;
4+
pdim1 = 17 ;
5+
variables:
6+
int prefix_list ;
7+
prefix_list:bald__ = "http://binary-array-ld.net/latest/" ;
8+
prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
9+
prefix_list:sdn__ = "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#" ;
10+
prefix_list:sdn-vocab__= "http://vocab.nerc.ac.uk/collection/P02/current/" ;
11+
prefix_list:cf__ = "http://def.scitools.org.uk/CFTerms/" ;
12+
prefix_list:cfsn__ = "http://vocab.nerc.ac.uk/collection/P07/current/CFSN0023/" ;
13+
14+
int parent_variable(pdim0, pdim1) ;
15+
parent_variable:rdf__type = "bald__Array" ;
16+
parent_variable:sdn__SDN_ParameterDiscoveryCode = "sdn-vocab__BAUC" ;
17+
parent_variable:submursible_name = "Nautilus" ;
18+
19+
int temp(pdim0, pdim1) ;
20+
temp:standard_name = "air_temperature" ;
21+
22+
// global attributes:
23+
:_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ;
24+
:rdf__type = "bald__Container" ;
25+
:bald__isPrefixedBy = "prefix_list" ;
26+
data:
27+
28+
parent_variable =
29+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
30+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
31+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
32+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
33+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
34+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
35+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
36+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
37+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
38+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
39+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
40+
41+
temp =
42+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
43+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
44+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
45+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
46+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
47+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
48+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
49+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
50+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
51+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
52+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
53+
54+
55+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
netcdf tmpMwXy8U {
2+
dimensions:
3+
pdim0 = 11 ;
4+
pdim1 = 17 ;
5+
variables:
6+
int prefix_list ;
7+
prefix_list:bald__ = "http://binary-array-ld.net/latest/" ;
8+
prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
9+
prefix_list:sdn__ = "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#" ;
10+
prefix_list:sdn-vocab__= "http://vocab.nerc.ac.uk/collection/P02/current/" ;
11+
prefix_list:cf__ = "http://def.scitools.org.uk/CFTerms/" ;
12+
prefix_list:cfsn-mmi__ = "http://mmisw.org/ont/cf/parameter/" ;
13+
prefix_list:cfsn-nerc__ = "http://vocab.nerc.ac.uk/collection/P07/current/";
14+
15+
int parent_variable(pdim0, pdim1) ;
16+
parent_variable:rdf__type = "bald__Array" ;
17+
parent_variable:sdn__SDN_ParameterDiscoveryCode = "sdn-vocab__BAUC" ;
18+
parent_variable:submursible_name = "Nautilus" ;
19+
20+
int temp(pdim0, pdim1) ;
21+
temp:cf__standard_name = "cfsn-mmi__air_temperature" ;
22+
//temp:cf__standard_name = "cfsn-nerc__CFSN0023";
23+
24+
// global attributes:
25+
:_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ;
26+
:rdf__type = "bald__Container" ;
27+
:bald__isPrefixedBy = "prefix_list" ;
28+
data:
29+
30+
parent_variable =
31+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
32+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
33+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
34+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
35+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
36+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
37+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
38+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
39+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
40+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
41+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
42+
43+
temp =
44+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
45+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
46+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
47+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
48+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
49+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
50+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
51+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
52+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
53+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
54+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
55+
56+
57+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import glob
2+
import os
3+
import subprocess
4+
import unittest
5+
6+
import netCDF4
7+
import numpy as np
8+
9+
import bald
10+
from bald.tests import BaldTestCase
11+
12+
13+
class Test(BaldTestCase):
14+
def setUp(self):
15+
self.cdl_path = os.path.join(os.path.dirname(__file__), 'CDL')
16+
17+
18+
def test_prefix_v2(self):
19+
"""Test prefix version 2 style """
20+
with self.temp_filename('.nc') as tfile:
21+
cdl_file = os.path.join(self.cdl_path, 'array_prefix_v2.cdl')
22+
print(cdl_file)
23+
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
24+
validation = bald.validate_netcdf(tfile)
25+
exns = validation.exceptions()
26+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
27+
28+
setattr(Test, 'test_prefix_v2', test_prefix_v2)
29+
30+
def test_prefix_v2_full(self):
31+
"""Test prefix version 2 style - full example"""
32+
with self.temp_filename('.nc') as tfile:
33+
cdl_file = os.path.join(self.cdl_path, 'array_prefix_v2_full.cdl')
34+
print(cdl_file)
35+
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
36+
validation = bald.validate_netcdf(tfile)
37+
exns = validation.exceptions()
38+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
39+
40+
setattr(Test, 'test_prefix_v2_full', test_prefix_v2_full)

lib/bald/validation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ def _check_uri(uri, exceptions):
9090
exceptions.append(msg)
9191
return exceptions
9292

93+
''' Skip checking prefixes as whole graphs could be big!
9394
for pref, uri in self.subject.prefixes().items():
9495
exceptions = _check_uri(self.subject.unpack_uri(uri),
9596
exceptions)
97+
'''
9698
for alias, uri in self.subject.aliases.items():
9799
exceptions = _check_uri(self.subject.unpack_uri(uri),
98100
exceptions)

0 commit comments

Comments
 (0)