|
| 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