diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index 120300d..c421292 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -240,6 +240,7 @@ def __getitem__(self, item): def check_uri(self, uri): result = False + #print("Checking uri: " + uri) if self[uri].status_code == 200: result = True return result @@ -414,6 +415,12 @@ def rdfnode(self, graph): selfnode = rdflib.URIRef(self.identity) for attr in self.attrs: objs = self.attrs[attr] + if(isinstance(objs, np.ndarray)): + #print("Found np.ndarray") + #print(objs) + #print(attr) + #try to convert np.ndarray to a list + objs = objs.tolist() if not (isinstance(objs, set) or isinstance(objs, list)): objs = set([objs]) for obj in objs: @@ -438,6 +445,14 @@ def rdfgraph(self): """ graph = rdflib.Graph() graph.bind('bald', 'http://binary-array-ld.net/latest/') + for prefix_name in self._prefixes: + #strip the double underscore suffix + new_name = prefix_name[:-2] + #print(prefix_name) + #print(new_name) + #print(self._prefixes[prefix_name]) + + graph.bind(new_name, self._prefixes[prefix_name]) graph = self.rdfnode(graph) return graph @@ -530,35 +545,48 @@ def load(afilepath): finally: f.close() -def load_netcdf(afilepath, uri=None): +def load_netcdf(afilepath, uri=None, baseuri=None): """ Validate a file with respect to binary-array-linked-data. Returns a :class:`bald.validation.Validation` """ with load(afilepath) as fhandle: - prefix_group = (fhandle[fhandle.bald__isPrefixedBy] if + prefix_var_name = None + if hasattr(fhandle, 'bald__isPrefixedBy'): + prefix_var_name = fhandle.bald__isPrefixedBy + + prefix_var = (fhandle[fhandle.bald__isPrefixedBy] if hasattr(fhandle, 'bald__isPrefixedBy') else {}) prefixes = {} - - skipped_variables = [] - if prefix_group != {}: - prefixes = (dict([(prefix, getattr(prefix_group, prefix)) for - prefix in prefix_group.ncattrs()])) - if isinstance(prefix_group, netCDF4._netCDF4.Variable): - skipped_variables.append(prefix_group.name) + if prefix_var != {} : + prefixes = (dict([(prefix, getattr(prefix_var, prefix)) for + prefix in prefix_var.ncattrs()])) else: for k in fhandle.ncattrs(): if k.endswith('__'): prefixes[k] = getattr(fhandle, k) - alias_group = (fhandle[fhandle.bald__isAliasedBy] + + # check that default set is handled, i.e. bald__ and rdf__ + if 'bald__' not in prefixes: + prefixes['bald__'] = "http://binary-array-ld.net/latest/" + + if 'rdf__' not in prefixes: + prefixes['rdf__'] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" + + #print(prefixes) + + alias_var_name = None + if hasattr(fhandle, 'bald__isAliasedBy'): + alias_var_name = fhandle.bald__isAliasedBy + + alias_var = (fhandle[fhandle.bald__isAliasedBy] if hasattr(fhandle, 'bald__isAliasedBy') else {}) aliases = {} - if alias_group != {}: - aliases = (dict([(alias, getattr(alias_group, alias)) - for alias in alias_group.ncattrs()])) - if isinstance(alias_group, netCDF4._netCDF4.Variable): - skipped_variables.append(alias_group.name) + if alias_var != {}: + aliases = (dict([(alias, getattr(alias_var, alias)) + for alias in alias_var.ncattrs()])) + #print(aliases) attrs = {} for k in fhandle.ncattrs(): @@ -573,32 +601,40 @@ def load_netcdf(afilepath, uri=None): root_container.attrs['bald__contains'] = [] file_variables = {} for name in fhandle.variables: + #print(name) + if name == prefix_var_name or name == alias_var_name: + #print("Skipping " + name) + continue sattrs = fhandle.variables[name].__dict__.copy() # inconsistent use of '/'; fix it identity = name + if baseuri is not None: + identity = baseuri + "/" + name # netCDF coordinate variable special case if (len(fhandle.variables[name].dimensions) == 1 and fhandle.variables[name].dimensions[0] == name): - sattrs['bald__array'] = name + #sattrs['bald__array'] = name + sattrs['bald__array'] = identity sattrs['rdf__type'] = 'bald__Reference' + if fhandle.variables[name].shape: sattrs['bald__shape'] = fhandle.variables[name].shape var = Array(identity, sattrs, prefixes=prefixes, aliases=aliases) else: var = Subject(identity, sattrs, prefixes=prefixes, aliases=aliases) - if name not in skipped_variables: - # Don't include skipped variables, such as prefix or alias - # variables, within the containment relation. - root_container.attrs['bald__contains'].append(var) - + root_container.attrs['bald__contains'].append(var) file_variables[name] = var # cycle again and find references for name in fhandle.variables: + if name == prefix_var_name or name == alias_var_name: + #print("Skipping " + name) + continue + var = file_variables[name] # reverse lookup based on type to be added lookups = ['bald__references', 'bald__array'] @@ -624,6 +660,8 @@ def load_netcdf(afilepath, uri=None): # Else, define a bald:childBroadcast else: identity = '{}_{}_ref'.format(name, dim) + if baseuri is not None: + identity = baseuri + '/' + '{}_{}_ref'.format(name, dim) rattrs = {} rattrs['rdf__type'] = 'bald__Reference' reshape = [1 for adim in var_shape] diff --git a/lib/bald/tests/integration/CDL/array_alias_v2.cdl b/lib/bald/tests/integration/CDL/array_alias_v2.cdl new file mode 100644 index 0000000..10fc484 --- /dev/null +++ b/lib/bald/tests/integration/CDL/array_alias_v2.cdl @@ -0,0 +1,58 @@ +netcdf tmpMwXy8U { +dimensions: + pdim0 = 11 ; + pdim1 = 17 ; +variables: + int prefix_list ; + prefix_list:bald__ = "http://binary-array-ld.net/latest/" ; + prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + + int alias_list ; + alias_list:SDN_ParameterDiscoveryCode = "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#SDN_ParameterDiscoveryCode" ; + alias_list:BactTaxaAbundSed = "http://vocab.nerc.ac.uk/collection/P02/current/BAUC/" ; + alias_list:standard_name = "http://def.scitools.org.uk/CFTerms/standard_name" ; + alias_list:air_temperature = "http://vocab.nerc.ac.uk/collection/P07/current/CFSN0023/" ; + + int parent_variable(pdim0, pdim1) ; + parent_variable:rdf__type = "bald__Array" ; + parent_variable:SDN_ParameterDiscoveryCode = "BactTaxaAbundSed" ; + parent_variable:submursible_name = "Nautilus" ; + + int temp(pdim0, pdim1) ; + temp:standard_name = "air_temperature" ; + +// global attributes: + :_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ; + :rdf__type = "bald__Container" ; + :bald__isPrefixedBy = "prefix_list" ; + :bald__isAliasedBy = "alias_list" ; +data: + + parent_variable = + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ; + + temp = + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ; + + +} diff --git a/lib/bald/tests/integration/CDL/array_geo.cdl b/lib/bald/tests/integration/CDL/array_geo.cdl new file mode 100644 index 0000000..70585c0 --- /dev/null +++ b/lib/bald/tests/integration/CDL/array_geo.cdl @@ -0,0 +1,45 @@ +netcdf tmpMwXy8U { +dimensions: + pdim0 = 11 ; + pdim1 = 17 ; +variables: + int prefix_list ; + prefix_list:bald__ = "http://binary-array-ld.net/latest/" ; + prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + prefix_list:rdfs__ = "http://www.w3.org/2000/01/rdf-schema#" ; + prefix_list:cf__ = "http://def.scitools.org.uk/CFTerms/" ; + prefix_list:geo__ = "http://www.opengis.net/ont/geosparql#" ; + + int temp(pdim0, pdim1) ; + temp:cf__standard_name = "air_temperature" ; + temp:cf__long_name = "Air temperature obs example at point" ; + temp:rdfs__label = "Air temperature obs example at point" ; + temp:geo__asWKT = "POINT(-77.03524 38.889468)" ; + + int pressure(pdim0, pdim1) ; + pressure:cf__standard_name = "air_pressure" ; + pressure:cf__long_name = "Air pressure at UCAR Centre Green" ; + pressure:rdfs__label = "Air pressure at UCAR Centre Green" ; + pressure:geo__asWKT = "POINT(-105.24584700000003 40.0315278)" ; + +// global attributes: + :_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ; + :rdf__type = "bald__Container" ; + :bald__isPrefixedBy = "prefix_list" ; +data: + + temp = + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ; + + +} diff --git a/lib/bald/tests/integration/CDL/array_prefix_v2.cdl b/lib/bald/tests/integration/CDL/array_prefix_v2.cdl new file mode 100644 index 0000000..609c304 --- /dev/null +++ b/lib/bald/tests/integration/CDL/array_prefix_v2.cdl @@ -0,0 +1,55 @@ +netcdf tmpMwXy8U { +dimensions: + pdim0 = 11 ; + pdim1 = 17 ; +variables: + int prefix_list ; + prefix_list:bald__ = "http://binary-array-ld.net/latest/" ; + prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + prefix_list:sdn__ = "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#" ; + prefix_list:sdn-vocab__= "http://vocab.nerc.ac.uk/collection/P02/current/" ; + prefix_list:cf__ = "http://def.scitools.org.uk/CFTerms/" ; + prefix_list:cfsn__ = "http://vocab.nerc.ac.uk/collection/P07/current/CFSN0023/" ; + + int parent_variable(pdim0, pdim1) ; + parent_variable:rdf__type = "bald__Array" ; + parent_variable:sdn__SDN_ParameterDiscoveryCode = "sdn-vocab__BAUC" ; + parent_variable:submursible_name = "Nautilus" ; + + int temp(pdim0, pdim1) ; + temp:standard_name = "air_temperature" ; + +// global attributes: + :_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ; + :rdf__type = "bald__Container" ; + :bald__isPrefixedBy = "prefix_list" ; +data: + + parent_variable = + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ; + + temp = + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ; + + +} diff --git a/lib/bald/tests/integration/CDL/array_prefix_v2_full.cdl b/lib/bald/tests/integration/CDL/array_prefix_v2_full.cdl new file mode 100644 index 0000000..9272f74 --- /dev/null +++ b/lib/bald/tests/integration/CDL/array_prefix_v2_full.cdl @@ -0,0 +1,57 @@ +netcdf tmpMwXy8U { +dimensions: + pdim0 = 11 ; + pdim1 = 17 ; +variables: + int prefix_list ; + prefix_list:bald__ = "http://binary-array-ld.net/latest/" ; + prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + prefix_list:sdn__ = "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#" ; + prefix_list:sdn-vocab__= "http://vocab.nerc.ac.uk/collection/P02/current/" ; + prefix_list:cf__ = "http://def.scitools.org.uk/CFTerms/" ; + prefix_list:cfsn-mmi__ = "http://mmisw.org/ont/cf/parameter/" ; + prefix_list:cfsn-nerc__ = "http://vocab.nerc.ac.uk/collection/P07/current/"; + + int parent_variable(pdim0, pdim1) ; + parent_variable:rdf__type = "bald__Array" ; + parent_variable:sdn__SDN_ParameterDiscoveryCode = "sdn-vocab__BAUC" ; + parent_variable:submursible_name = "Nautilus" ; + + int temp(pdim0, pdim1) ; + temp:cf__standard_name = "cfsn-mmi__air_temperature" ; + //temp:cf__standard_name = "cfsn-nerc__CFSN0023"; + +// global attributes: + :_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ; + :rdf__type = "bald__Container" ; + :bald__isPrefixedBy = "prefix_list" ; +data: + + parent_variable = + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ; + + temp = + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ; + + +} diff --git a/lib/bald/tests/integration/CDL/multi_array_reference.cdl b/lib/bald/tests/integration/CDL/multi_array_reference.cdl index ccf2e3f..fe4241e 100644 --- a/lib/bald/tests/integration/CDL/multi_array_reference.cdl +++ b/lib/bald/tests/integration/CDL/multi_array_reference.cdl @@ -3,6 +3,11 @@ dimensions: pdim0 = 11 ; pdim1 = 17 ; variables: + int prefix_list(pdim0, pdim1) ; + prefix_list:bald__ = "http://binary-array-ld.net/latest/" ; + prefix_list:metce__ = "http://codes.wmo.int/common/observation-type/METCE/2013/" ; + prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; + int variable1(pdim0, pdim1) ; variable1:bald__references = "location_variable" ; variable1:long_name = "Gerald"; @@ -31,11 +36,4 @@ variables: :_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ; :bald__isPrefixedBy = "prefix_list" ; -group: prefix_list { - - // group attributes: - :bald__ = "http://binary-array-ld.net/latest/" ; - :metce__ = "http://codes.wmo.int/common/observation-type/METCE/2013/" ; - :rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ; - } // group bald__prefix_list } diff --git a/lib/bald/tests/integration/TTL/multi_array_reference.ttl b/lib/bald/tests/integration/TTL/multi_array_reference.ttl index e495087..f42a3c5 100644 --- a/lib/bald/tests/integration/TTL/multi_array_reference.ttl +++ b/lib/bald/tests/integration/TTL/multi_array_reference.ttl @@ -1,4 +1,5 @@ @prefix bald: . +@prefix metce: . @prefix rdf: . @prefix rdfs: . @prefix xml: . @@ -21,7 +22,7 @@ ; bald:shape "(11, 17)" ; "Gerald" ; - . + metce:SamplingObservation . a bald:Array ; bald:references , @@ -29,7 +30,7 @@ ; bald:shape "(11, 17)" ; "Imelda" ; - . + metce:SamplingObservation . a bald:Reference, bald:Subject ; diff --git a/lib/bald/tests/integration/test_cdl.py b/lib/bald/tests/integration/test_cdl.py index 92f0540..03b2d50 100644 --- a/lib/bald/tests/integration/test_cdl.py +++ b/lib/bald/tests/integration/test_cdl.py @@ -64,7 +64,7 @@ def test_grid_OISST_GHRSST(self): validation = bald.validate_netcdf(tfile) exns = validation.exceptions() exns.sort() - expected = ['http://www.ncdc.noaa.gov/sst is not resolving as a resource (404).', + expected = [ 'http://www.ncdc.noaa.gov/sst is not resolving as a resource (404).', 'http://www.ncdc.noaa.gov/sst/ is not resolving as a resource (404).'] expected.sort() self.assertTrue(not validation.is_valid() and exns == expected, diff --git a/lib/bald/tests/integration/test_cdl_rdfgraph.py b/lib/bald/tests/integration/test_cdl_rdfgraph.py index ced6c8a..ca54b07 100644 --- a/lib/bald/tests/integration/test_cdl_rdfgraph.py +++ b/lib/bald/tests/integration/test_cdl_rdfgraph.py @@ -36,3 +36,15 @@ def test_multi_array_reference(self): with open(os.path.join(self.ttl_path, 'multi_array_reference.ttl'), 'r') as sf: expected_ttl = sf.read() self.assertEqual(expected_ttl, ttl) + + def test_ereefs(self): + with self.temp_filename('.nc') as tfile: + cdl_file = os.path.join(self.cdl_path, 'ereefs_gbr4_ncld.cdl') + subprocess.check_call(['ncgen', '-o', tfile, cdl_file]) + root_container = bald.load_netcdf(tfile) + try: + g = root_container.rdfgraph() + ttl = g.serialize(format='n3').decode("utf-8") + except TypeError: + self.fail("Test case could not convert ereefs CDL to RDF") + diff --git a/lib/bald/tests/integration/test_cdl_v2.py b/lib/bald/tests/integration/test_cdl_v2.py new file mode 100644 index 0000000..fc5e030 --- /dev/null +++ b/lib/bald/tests/integration/test_cdl_v2.py @@ -0,0 +1,52 @@ +import glob +import os +import subprocess +import unittest + +import netCDF4 +import numpy as np + +import bald +from bald.tests import BaldTestCase + + +class Test(BaldTestCase): + def setUp(self): + self.cdl_path = os.path.join(os.path.dirname(__file__), 'CDL') + + +def test_alias_v2(self): + """Test alias version 2 style """ + with self.temp_filename('.nc') as tfile: + cdl_file = os.path.join(self.cdl_path, 'array_alias_v2.cdl') + print(cdl_file) + subprocess.check_call(['ncgen', '-o', tfile, cdl_file]) + validation = bald.validate_netcdf(tfile) + exns = validation.exceptions() + self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns)) + +setattr(Test, 'test_alias_v2', test_alias_v2) + +def test_prefix_v2(self): + """Test prefix version 2 style """ + with self.temp_filename('.nc') as tfile: + cdl_file = os.path.join(self.cdl_path, 'array_prefix_v2.cdl') + print(cdl_file) + subprocess.check_call(['ncgen', '-o', tfile, cdl_file]) + validation = bald.validate_netcdf(tfile) + exns = validation.exceptions() + self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns)) + +setattr(Test, 'test_prefix_v2', test_prefix_v2) + +def test_prefix_v2_full(self): + """Test prefix version 2 style - full example""" + with self.temp_filename('.nc') as tfile: + cdl_file = os.path.join(self.cdl_path, 'array_prefix_v2_full.cdl') + print(cdl_file) + subprocess.check_call(['ncgen', '-o', tfile, cdl_file]) + validation = bald.validate_netcdf(tfile) + exns = validation.exceptions() + self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns)) + +setattr(Test, 'test_prefix_v2_full', test_prefix_v2_full) diff --git a/lib/bald/validation.py b/lib/bald/validation.py index 60a7b59..44ea115 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -90,9 +90,11 @@ def _check_uri(uri, exceptions): exceptions.append(msg) return exceptions + ''' Skip checking prefixes as whole graphs could be big! for pref, uri in self.subject.prefixes().items(): exceptions = _check_uri(self.subject.unpack_uri(uri), exceptions) + ''' for alias, uri in self.subject.aliases.items(): exceptions = _check_uri(self.subject.unpack_uri(uri), exceptions) diff --git a/nc2rdf/nc2rdf.py b/nc2rdf/nc2rdf.py index 120edac..2f3ab82 100644 --- a/nc2rdf/nc2rdf.py +++ b/nc2rdf/nc2rdf.py @@ -6,21 +6,21 @@ import numpy as np import bald -def nc2rdf(ncfilename, outformat): +def nc2rdf(ncfilename, outformat, container_uri=None, default_baseuri=None): #print("nc2rdf test") #print(ncfile) - root_container = bald.load_netcdf(ncfilename) + root_container = bald.load_netcdf(ncfilename, uri=container_uri, baseuri=default_baseuri) ttl = root_container.rdfgraph().serialize(format=outformat).decode("utf-8") print(ttl) -def cdl2rdf(cdl_file, outformat): +def cdl2rdf(cdl_file, outformat, container_uri=None, default_baseuri=None): #print("cdl2rdf test") #print(cdl_file) tfile, tfilename = tempfile.mkstemp('.nc') #print(tfilename) subprocess.check_call(['ncgen', '-o', tfilename, cdl_file]) - nc2rdf(tfilename, outformat) + nc2rdf(tfilename, outformat, container_uri=container_uri, default_baseuri=default_baseuri) os.close(tfile) os.remove(tfilename) @@ -29,6 +29,8 @@ def cdl2rdf(cdl_file, outformat): if __name__ == "__main__": parser = argparse.ArgumentParser(description='Convert netCDF metadata to RDF.') parser.add_argument('-o', action="store", dest="format", default='n3', help="RDF output format (n3 *default, ttl, xml)") + parser.add_argument('--uri', action="store", dest="uri", help="Container URI for the root of the graph") + parser.add_argument('--baseuri', action="store", dest="baseuri", help="Base URI for the graph") parser.add_argument('--cdl', action="store_true", dest="isCDL", default=False, help="Flag to indicate file is CDL") parser.add_argument('--nc', action="store_true", dest="isNC", default=False, help="Flag to indicate file is netCDF") parser.add_argument("ncfile", help="Path for the netCDF file") @@ -36,8 +38,8 @@ def cdl2rdf(cdl_file, outformat): args = parser.parse_args() if(args.isCDL or args.ncfile.endswith(".cdl") or args.ncfile.endswith('.CDL')): - cdl2rdf(args.ncfile, args.format) + cdl2rdf(args.ncfile, args.format, container_uri=args.uri, default_baseuri=args.baseuri) elif(args.isNC or args.ncfile.endswith(".nc") or args.ncfile.endswith('.NC')): - nc2rdf(args.ncfile, args.format) + nc2rdf(args.ncfile, args.format, container_uri=args.uri, default_baseuri=args.baseuri) else: print("Unrecognised file suffix. Please indicate if CDL or NC via --cdl or --nc");