Skip to content

Commit 1745701

Browse files
committed
implementing parameters to set graph uris and base uris
1 parent 847cee9 commit 1745701

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/bald/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def load(afilepath):
536536
finally:
537537
f.close()
538538

539-
def load_netcdf(afilepath, uri=None):
539+
def load_netcdf(afilepath, uri=None, baseuri=None):
540540
"""
541541
Validate a file with respect to binary-array-linked-data.
542542
Returns a :class:`bald.validation.Validation`
@@ -600,11 +600,14 @@ def load_netcdf(afilepath, uri=None):
600600
sattrs = fhandle.variables[name].__dict__.copy()
601601
# inconsistent use of '/'; fix it
602602
identity = name
603+
if baseuri is not None:
604+
identity = baseuri + "/" + name
603605

604606
# netCDF coordinate variable special case
605607
if (len(fhandle.variables[name].dimensions) == 1 and
606608
fhandle.variables[name].dimensions[0] == name):
607-
sattrs['bald__array'] = name
609+
#sattrs['bald__array'] = name
610+
sattrs['bald__array'] = identity
608611
sattrs['rdf__type'] = 'bald__Reference'
609612

610613
if fhandle.variables[name].shape:
@@ -648,6 +651,8 @@ def load_netcdf(afilepath, uri=None):
648651
# Else, define a bald:childBroadcast
649652
else:
650653
identity = '{}_{}_ref'.format(name, dim)
654+
if baseuri is not None:
655+
identity = baseuri + '/' + '{}_{}_ref'.format(name, dim)
651656
rattrs = {}
652657
rattrs['rdf__type'] = 'bald__Reference'
653658
reshape = [1 for adim in var_shape]

nc2rdf/nc2rdf.py

Lines changed: 8 additions & 6 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):
9+
def nc2rdf(ncfilename, outformat, container_uri=None, default_baseuri=None):
1010
#print("nc2rdf test")
1111
#print(ncfile)
12-
root_container = bald.load_netcdf(ncfilename)
12+
root_container = bald.load_netcdf(ncfilename, uri=container_uri, baseuri=default_baseuri)
1313
ttl = root_container.rdfgraph().serialize(format=outformat).decode("utf-8")
1414
print(ttl)
1515

16-
def cdl2rdf(cdl_file, outformat):
16+
def cdl2rdf(cdl_file, outformat, container_uri=None, default_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)
23+
nc2rdf(tfilename, outformat, container_uri=container_uri, default_baseuri=default_baseuri)
2424

2525
os.close(tfile)
2626
os.remove(tfilename)
@@ -29,15 +29,17 @@ def cdl2rdf(cdl_file, outformat):
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")
33+
parser.add_argument('--baseuri', action="store", dest="baseuri", help="Base URI for the graph")
3234
parser.add_argument('--cdl', action="store_true", dest="isCDL", default=False, help="Flag to indicate file is CDL")
3335
parser.add_argument('--nc', action="store_true", dest="isNC", default=False, help="Flag to indicate file is netCDF")
3436
parser.add_argument("ncfile", help="Path for the netCDF file")
3537

3638
args = parser.parse_args()
3739

3840
if(args.isCDL or args.ncfile.endswith(".cdl") or args.ncfile.endswith('.CDL')):
39-
cdl2rdf(args.ncfile, args.format)
41+
cdl2rdf(args.ncfile, args.format, container_uri=args.uri, default_baseuri=args.baseuri)
4042
elif(args.isNC or args.ncfile.endswith(".nc") or args.ncfile.endswith('.NC')):
41-
nc2rdf(args.ncfile, args.format)
43+
nc2rdf(args.ncfile, args.format, container_uri=args.uri, default_baseuri=args.baseuri)
4244
else:
4345
print("Unrecognised file suffix. Please indicate if CDL or NC via --cdl or --nc");

0 commit comments

Comments
 (0)