Skip to content

Commit 61221a4

Browse files
authored
Merge pull request #52 from jyucsiro/nc2rdf
Proposing a command line tool nc2rdf
2 parents 6cef8b7 + d9cac6c commit 61221a4

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

nc2rdf/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# nc2rdf
2+
3+
This tool allows users to input CDL or netCDF files and get an RDF graph returned.
4+
5+
Example:
6+
```
7+
$ python nc2rdf.py ../lib/bald/tests/integration/CDL/array_alias.cdl
8+
```
9+
10+
By default, nc2rdf tries to detect CDL and nc files using the file suffix. Output by default is
11+
RDF N3 triples.
12+
13+
Users can specify CDL files using the `--cdl` option.
14+
```
15+
$ python nc2rdf.py --cdl myfile.cdl
16+
```
17+
18+
Users can specify netCDF files usings the `--nc` ioption.
19+
```
20+
$ python nc2rdf.py --nc myfile.nc
21+
```
22+
23+
More info, refer to the help option
24+
```
25+
$ python nc2rdf.py -h
26+
```
27+
28+
Other outputs include TTL and XML
29+
```
30+
$ python nc2rdf.py -o ttl myfile.nc
31+
$ python nc2rdf.py -o xml myfile.nc
32+
```
33+
34+
Note: This command-line tool is experimental and is subject to changes, however serves as a prototype for accessing bald functions for netCDF related files to RDF.

nc2rdf/nc2rdf.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import argparse
2+
import os
3+
import subprocess
4+
import tempfile
5+
import netCDF4
6+
import numpy as np
7+
import bald
8+
9+
def nc2rdf(ncfilename, outformat):
10+
#print("nc2rdf test")
11+
#print(ncfile)
12+
root_container = bald.load_netcdf(ncfilename)
13+
ttl = root_container.rdfgraph().serialize(format=outformat).decode("utf-8")
14+
print(ttl)
15+
16+
def cdl2rdf(cdl_file, outformat):
17+
#print("cdl2rdf test")
18+
#print(cdl_file)
19+
tfile, tfilename = tempfile.mkstemp('.nc')
20+
#print(tfilename)
21+
subprocess.check_call(['ncgen', '-o', tfilename, cdl_file])
22+
23+
nc2rdf(tfilename, outformat)
24+
25+
os.close(tfile)
26+
os.remove(tfilename)
27+
28+
29+
if __name__ == "__main__":
30+
parser = argparse.ArgumentParser(description='Convert netCDF metadata to RDF.')
31+
parser.add_argument('-o', action="store", dest="format", default='n3', help="RDF output format (n3 *default, ttl, xml)")
32+
parser.add_argument('--cdl', action="store_true", dest="isCDL", default=False, help="Flag to indicate file is CDL")
33+
parser.add_argument('--nc', action="store_true", dest="isNC", default=False, help="Flag to indicate file is netCDF")
34+
parser.add_argument("ncfile", help="Path for the netCDF file")
35+
36+
args = parser.parse_args()
37+
38+
if(args.isCDL or args.ncfile.endswith(".cdl") or args.ncfile.endswith('.CDL')):
39+
cdl2rdf(args.ncfile, args.format)
40+
elif(args.isNC or args.ncfile.endswith(".nc") or args.ncfile.endswith('.NC')):
41+
nc2rdf(args.ncfile, args.format)
42+
else:
43+
print("Unrecognised file suffix. Please indicate if CDL or NC via --cdl or --nc");

0 commit comments

Comments
 (0)