Skip to content

Commit 751beb4

Browse files
committed
removing uri in load_netcdf and prefer baseuri, fixing affected code and adding test
1 parent ff4f540 commit 751beb4

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

lib/bald/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def load(afilepath):
538538
finally:
539539
f.close()
540540

541-
def load_netcdf(afilepath, uri=None, baseuri=None):
541+
def load_netcdf(afilepath, baseuri=None):
542542
"""
543543
Validate a file with respect to binary-array-linked-data.
544544
Returns a :class:`bald.validation.Validation`
@@ -588,8 +588,8 @@ def load_netcdf(afilepath, uri=None, baseuri=None):
588588
for k in fhandle.ncattrs():
589589
attrs[k] = getattr(fhandle, k)
590590
# It would be nice to use the URI of the file if it is known.
591-
if uri is not None:
592-
identity = uri
591+
if baseuri is not None:
592+
identity = baseuri
593593
else:
594594
identity = 'root'
595595
root_container = Container(identity, attrs, prefixes=prefixes,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@prefix bald: <http://binary-array-ld.net/latest/> .
2+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
3+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
4+
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
5+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
6+
7+
<http://example.org/base> a bald:Container ;
8+
bald:contains <http://example.org/base/child_variable>,
9+
<http://example.org/base/parent_variable> ;
10+
bald:isPrefixedBy "prefix_list" .
11+
12+
<http://example.org/base/parent_variable> a bald:Array ;
13+
bald:references <http://example.org/base/child_variable> ;
14+
bald:shape "(11, 17)" .
15+
16+
<http://example.org/base/child_variable> a bald:Array,
17+
bald:Reference ;
18+
bald:array <http://example.org/base/child_variable> ;
19+
bald:shape "(11, 17)" .
20+

lib/bald/tests/integration/test_cdl_rdfgraph.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ def test_array_reference(self):
2525
expected_ttl = sf.read()
2626
self.assertEqual(expected_ttl, ttl)
2727

28+
def test_array_reference_with_baseuri(self):
29+
with self.temp_filename('.nc') as tfile:
30+
cdl_file = os.path.join(self.cdl_path, 'array_reference.cdl')
31+
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
32+
root_container = bald.load_netcdf(tfile, baseuri='http://example.org/base')
33+
ttl = root_container.rdfgraph().serialize(format='n3').decode("utf-8")
34+
#with open(os.path.join(self.ttl_path, 'array_reference_withbase.ttl'), 'w') as sf:
35+
# sf.write(ttl)
36+
with open(os.path.join(self.ttl_path, 'array_reference_withbase.ttl'), 'r') as sf:
37+
expected_ttl = sf.read()
38+
self.assertEqual(expected_ttl, ttl)
39+
2840
def test_multi_array_reference(self):
2941
with self.temp_filename('.nc') as tfile:
3042
cdl_file = os.path.join(self.cdl_path, 'multi_array_reference.cdl')

nc2rdf/nc2rdf.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
import numpy as np
77
import bald
88

9-
def nc2rdf(ncfilename, outformat, container_uri=None, default_baseuri=None):
9+
def nc2rdf(ncfilename, outformat, baseuri=None):
1010
#print("nc2rdf test")
1111
#print(ncfile)
12-
root_container = bald.load_netcdf(ncfilename, uri=container_uri, baseuri=default_baseuri)
12+
root_container = bald.load_netcdf(ncfilename, baseuri=baseuri)
1313
ttl = root_container.rdfgraph().serialize(format=outformat).decode("utf-8")
1414
print(ttl)
1515

16-
def cdl2rdf(cdl_file, outformat, container_uri=None, default_baseuri=None):
16+
def cdl2rdf(cdl_file, outformat, baseuri=None):
1717
#print("cdl2rdf test")
1818
#print(cdl_file)
1919
tfile, tfilename = tempfile.mkstemp('.nc')
2020
#print(tfilename)
2121
subprocess.check_call(['ncgen', '-o', tfilename, cdl_file])
2222

23-
nc2rdf(tfilename, outformat, container_uri=container_uri, default_baseuri=default_baseuri)
23+
nc2rdf(tfilename, outformat, baseuri=baseuri)
2424

2525
os.close(tfile)
2626
os.remove(tfilename)
@@ -29,7 +29,6 @@ def cdl2rdf(cdl_file, outformat, container_uri=None, default_baseuri=None):
2929
if __name__ == "__main__":
3030
parser = argparse.ArgumentParser(description='Convert netCDF metadata to RDF.')
3131
parser.add_argument('-o', action="store", dest="format", default='n3', help="RDF output format (n3 *default, ttl, xml)")
32-
parser.add_argument('--uri', action="store", dest="uri", help="Container URI for the root of the graph")
3332
parser.add_argument('--baseuri', action="store", dest="baseuri", help="Base URI for the graph")
3433
parser.add_argument('--cdl', action="store_true", dest="isCDL", default=False, help="Flag to indicate file is CDL")
3534
parser.add_argument('--nc', action="store_true", dest="isNC", default=False, help="Flag to indicate file is netCDF")
@@ -38,8 +37,8 @@ def cdl2rdf(cdl_file, outformat, container_uri=None, default_baseuri=None):
3837
args = parser.parse_args()
3938

4039
if(args.isCDL or args.ncfile.endswith(".cdl") or args.ncfile.endswith('.CDL')):
41-
cdl2rdf(args.ncfile, args.format, container_uri=args.uri, default_baseuri=args.baseuri)
40+
cdl2rdf(args.ncfile, args.format, baseuri=args.baseuri)
4241
elif(args.isNC or args.ncfile.endswith(".nc") or args.ncfile.endswith('.NC')):
43-
nc2rdf(args.ncfile, args.format, container_uri=args.uri, default_baseuri=args.baseuri)
42+
nc2rdf(args.ncfile, args.format, baseuri=args.baseuri)
4443
else:
4544
print("Unrecognised file suffix. Please indicate if CDL or NC via --cdl or --nc");

0 commit comments

Comments
 (0)