Skip to content

Commit 1f1f431

Browse files
authored
Merge pull request #95 from marqh/prefixLocator
DCAT Distribution
2 parents ff82890 + 119b1a7 commit 1f1f431

12 files changed

+185
-42
lines changed

.travis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ install:
1717
- conda install --quiet --file requirements.txt
1818
- conda list
1919
- conda info -a
20-
- wget https://github.com/marqh/terra/archive/master.zip
21-
- unzip master.zip
22-
- cd terra-master
23-
- python setup.py --quiet install
24-
- cd ..
2520
- python setup.py --quiet install
2621

2722
script:

lib/bald/__init__.py

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,11 @@
1414
import pyparsing
1515
import rdflib
1616
import rdflib.collection
17+
import rdflib.namespace
1718
import requests
1819
import six
1920

20-
# try:
21-
# #import terra.datetime
22-
# from terra import datetime
23-
# terra_imp = True
24-
# except ImportError:
25-
# terra_imp = False
26-
2721
from bald import datetime
28-
terra_imp = True
2922
import bald.validation as bv
3023

3124
__version__ = '0.3'
@@ -288,14 +281,19 @@ def check_uri(self, uri):
288281

289282
class Resource(object):
290283
_rdftype = 'bald__Resource'
284+
# def __init__(self, baseuri, relative_id, attrs=None, prefixes=None,
285+
# aliases=None, alias_graph=None):
291286
def __init__(self, baseuri, relative_id, attrs=None, prefixes=None,
292-
aliases=None, alias_graph=None):
287+
aliases=None, alias_graph=None, file_resource=False, file_locator=None):
288+
293289
"""
294290
A resource of metadata statements.
295291
296292
attrs: an dictionary of key value pair attributes
297293
"""
298294
self.baseuri = baseuri
295+
self.file_locator = file_locator
296+
self.is_file = file_resource
299297
self.relative_id = relative_id
300298

301299
if attrs is None:
@@ -339,7 +337,8 @@ def __repr__(self):
339337
def __setattr__(self, attr, value):
340338
reserved_attrs = ['baseuri', 'relative_id', 'prefixes', '_prefixes',
341339
'_prefix_suffix', '_http_uri_prefix', '_http_uri',
342-
'aliases', 'alias_graph', 'attrs', '_rdftype']
340+
'aliases', 'alias_graph', 'attrs', '_rdftype', 'file_locator',
341+
'is_file']
343342
if attr in reserved_attrs:
344343
object.__setattr__(self, attr, value)
345344
else:
@@ -535,6 +534,38 @@ def viewgraph(self):
535534

536535
return html
537536

537+
def _dcat_location(self, graph, selfnode):
538+
graph.bind('dcat', 'http://www.w3.org/ns/dcat#')
539+
graph.bind('dct', 'http://purl.org/dc/terms/')
540+
# template = ('dcat:distribution [
541+
# a dcat:Distribution;
542+
# dcat:downloadURL <{}>;
543+
# dcat:mediaType [
544+
# a dct:MediaType;
545+
# dct:identifier "application/x-netcdf"
546+
# ];
547+
# dct:format [
548+
# a dct:MediaType;
549+
# dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/>
550+
# ]
551+
# ].')
552+
dcatnode = rdflib.BNode()
553+
dcfnode = rdflib.BNode()
554+
graph.add((selfnode, rdflib.URIRef('http://www.w3.org/ns/dcat#distribution'), dcatnode))
555+
graph.add((dcatnode, rdflib.namespace.RDF.type, rdflib.URIRef('http://www.w3.org/ns/dcat#Distribution')))
556+
if self.file_locator is not None:
557+
graph.add((dcatnode, rdflib.URIRef('http://www.w3.org/ns/dcat#downloadURL'), rdflib.URIRef(self.file_locator)))
558+
dcatmednode = rdflib.BNode()
559+
graph.add((dcatmednode, rdflib.namespace.RDF.type, rdflib.URIRef('http://www.w3.org/ns/dcat#MediaType')))
560+
graph.add((dcatmednode, rdflib.URIRef('http://purl.org/dc/terms/identifier'), rdflib.Literal('application/x-netcdf')))
561+
graph.add((dcatnode, rdflib.URIRef('http://www.w3.org/ns/dcat#mediaType'), dcatmednode))
562+
563+
graph.add((dcfnode, rdflib.namespace.RDF.type, rdflib.URIRef('http://purl.org/dc/terms/MediaType')))
564+
graph.add((dcfnode, rdflib.URIRef('http://purl.org/dc/terms/identifier'),
565+
rdflib.URIRef('http://vocab.nerc.ac.uk/collection/M01/current/NC/')))
566+
graph.add((selfnode, rdflib.URIRef('http://purl.org/dc/terms/format'), dcfnode))
567+
568+
538569
def rdfnode(self, graph):
539570
"""
540571
Create an RDF Node,
@@ -568,10 +599,10 @@ def rdfnode(self, graph):
568599
if is_http_uri(rdfobj):
569600

570601
rdfobj = rdflib.URIRef(rdfobj)
571-
elif terra_imp and isinstance(rdfobj, datetime.EpochDateTimes):
602+
elif isinstance(rdfobj, datetime.EpochDateTimes):
572603
rdfobj = rdflib.Literal(str(rdfobj), datatype=rdflib.XSD.dateTime)
573604
elif isinstance(rdfobj, float):
574-
rdfobj = rdflib.Literal(rdfobj, datatype=rdflib.XSD.decimal)
605+
rdfobj = rdflib.Literal(float(rdfobj), datatype=rdflib.XSD.decimal)
575606
else:
576607
rdfobj = rdflib.Literal(rdfobj)
577608
rdfpred = rdflib.URIRef(rdfpred)
@@ -594,6 +625,9 @@ def rdfnode(self, graph):
594625
col = rdflib.collection.Collection(graph, list_name, list_items)
595626
graph.add((selfnode, rdfpred, list_name))
596627

628+
if self.is_file:
629+
self._dcat_location(graph, selfnode)
630+
597631
return selfnode
598632

599633
def rdfgraph(self):
@@ -757,7 +791,7 @@ def load(afilepath):
757791
except NameError:
758792
pass
759793

760-
def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
794+
def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None, file_locator=None):
761795
"""
762796
Load a file with respect to binary-array-linked-data.
763797
Returns a :class:`bald.Collection`
@@ -773,6 +807,7 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
773807
baseuri = 'file://{}/'.format(afilepath)
774808
elif type(baseuri) == str and not baseuri.endswith('/'):
775809
baseuri = '{}/'.format(baseuri)
810+
776811
identity = baseuri
777812
prefix_var_name = None
778813
if hasattr(fhandle, 'bald__isPrefixedBy'):
@@ -852,7 +887,8 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
852887
# raise ValueError('duplicate aliases')
853888
# aliases = careful_update(aliases, dict(new_aliases))
854889
root_container = Container(baseuri, '', attrs, prefixes=prefixes,
855-
aliases=aliases, alias_graph=aliasgraph)
890+
aliases=aliases, alias_graph=aliasgraph,
891+
file_resource=True, file_locator=file_locator)
856892

857893
root_container.attrs['bald__contains'] = set()
858894
file_variables = {}
@@ -886,7 +922,7 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
886922
sattrs['bald__last_value'] = float(sattrs['bald__last_value'])
887923

888924
# datetime special case
889-
if 'units' in fhandle.variables[name].ncattrs() and terra_imp:
925+
if 'units' in fhandle.variables[name].ncattrs():
890926
ustr = fhandle.variables[name].getncattr('units')
891927
pattern = '^([a-z]+) since ([0-9T:\\. -]+)'
892928

lib/bald/tests/integration/TTL/GEMS_CO2_Apr2006.ttl

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
@prefix NetCDF: <http://def.scitools.org.uk/NetCDF/> .
33
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
44
@prefix cf_sname: <http://vocab.nerc.ac.uk/standard_name/> .
5+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
6+
@prefix dct: <http://purl.org/dc/terms/> .
57
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
68
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
79
@prefix this: <file://CDL/GEMS_CO2_Apr2006.cdl/> .
@@ -10,6 +12,11 @@
1012

1113
this: a bald:Container ;
1214
NetCDF:Conventions "CF-1.0" ;
15+
dct:format [ a dct:MediaType ;
16+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
17+
dcat:distribution [ a dcat:Distribution ;
18+
dcat:mediaType [ a dcat:MediaType ;
19+
dct:identifier "application/x-netcdf" ] ] ;
1320
bald:contains this:co2,
1421
this:latitude,
1522
this:levelist,
@@ -29,14 +36,14 @@ this:co2 a bald:Array ;
2936
bald:target this:levelist ;
3037
bald:targetReshape ( 1 60 1 1 ) ;
3138
bald:targetShape ( 60 ) ],
32-
[ a bald:Reference ;
33-
bald:target this:latitude ;
34-
bald:targetReshape ( 1 1 181 1 ) ;
35-
bald:targetShape ( 181 ) ],
3639
[ a bald:Reference ;
3740
bald:target this:time ;
3841
bald:targetReshape ( 1 1 1 1 ) ;
3942
bald:targetShape ( 1 ) ],
43+
[ a bald:Reference ;
44+
bald:target this:latitude ;
45+
bald:targetReshape ( 1 1 181 1 ) ;
46+
bald:targetShape ( 181 ) ],
4047
[ a bald:Reference ;
4148
bald:target this:longitude ;
4249
bald:targetReshape ( 1 1 1 360 ) ;
@@ -49,21 +56,21 @@ this:lnsp a bald:Array ;
4956
NetCDF:add_offset 11.2087164280841 ;
5057
NetCDF:long_name "Logarithm of surface pressure" ;
5158
bald:references [ a bald:Reference ;
52-
bald:target this:longitude ;
53-
bald:targetReshape ( 1 1 1 360 ) ;
54-
bald:targetShape ( 360 ) ],
55-
[ a bald:Reference ;
5659
bald:target this:time ;
5760
bald:targetReshape ( 1 1 1 1 ) ;
5861
bald:targetShape ( 1 ) ],
59-
[ a bald:Reference ;
60-
bald:target this:latitude ;
61-
bald:targetReshape ( 1 1 181 1 ) ;
62-
bald:targetShape ( 181 ) ],
6362
[ a bald:Reference ;
6463
bald:target this:levelist ;
6564
bald:targetReshape ( 1 60 1 1 ) ;
66-
bald:targetShape ( 60 ) ] ;
65+
bald:targetShape ( 60 ) ],
66+
[ a bald:Reference ;
67+
bald:target this:longitude ;
68+
bald:targetReshape ( 1 1 1 360 ) ;
69+
bald:targetShape ( 360 ) ],
70+
[ a bald:Reference ;
71+
bald:target this:latitude ;
72+
bald:targetReshape ( 1 1 181 1 ) ;
73+
bald:targetShape ( 181 ) ] ;
6774
bald:shape ( 1 60 181 360 ) .
6875

6976
this:latitude a bald:Array ;

lib/bald/tests/integration/TTL/ProcessChain0300.ttl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
@prefix NWP: <https://codes.nws.noaa.gov/NumericalWeatherPrediction> .
44
@prefix StatPP: <https://codes.nws.noaa.gov/StatisticalPostProcessing> .
55
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
6-
@prefix bald1: <https://www.opengis.net/def/binary-array-ld/x> .
76
@prefix cf_sname: <http://vocab.nerc.ac.uk/standard_name/> .
7+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
8+
@prefix dct: <http://purl.org/dc/terms/> .
89
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
910
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
1011
@prefix this: <file://CDL/ProcessChain0300.cdl/> .
@@ -13,6 +14,11 @@
1314

1415
this: a bald:Container ;
1516
this:process_chain "gfsmos_process_chain" ;
17+
dct:format [ a dct:MediaType ;
18+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
19+
dcat:distribution [ a dcat:Distribution ;
20+
dcat:mediaType [ a dcat:MediaType ;
21+
dct:identifier "application/x-netcdf" ] ] ;
1622
bald:contains this:gfsmos_process_chain,
1723
this:step1,
1824
this:step2 ;

lib/bald/tests/integration/TTL/array_reference.ttl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
2+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
3+
@prefix dct: <http://purl.org/dc/terms/> .
24
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
35
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
46
@prefix this: <file://CDL/array_reference.cdl/> .
57
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
68
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
79

810
this: a bald:Container ;
11+
dct:format [ a dct:MediaType ;
12+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
13+
dcat:distribution [ a dcat:Distribution ;
14+
dcat:mediaType [ a dcat:MediaType ;
15+
dct:identifier "application/x-netcdf" ] ] ;
916
bald:contains this:child_variable,
1017
this:parent_variable ;
1118
bald:isPrefixedBy "prefix_list" .

lib/bald/tests/integration/TTL/array_reference_withbase.ttl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
2+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
3+
@prefix dct: <http://purl.org/dc/terms/> .
24
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
35
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
46
@prefix this: <http://example.org/base/> .
57
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
68
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
79

810
this: a bald:Container ;
11+
dct:format [ a dct:MediaType ;
12+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
13+
dcat:distribution [ a dcat:Distribution ;
14+
dcat:mediaType [ a dcat:MediaType ;
15+
dct:identifier "application/x-netcdf" ] ] ;
916
bald:contains this:child_variable,
1017
this:parent_variable ;
1118
bald:isPrefixedBy "prefix_list" .

lib/bald/tests/integration/TTL/ereefs_gbr4_ncld.ttl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
@prefix NetCDF: <http://def.scitools.org.uk/NetCDF/> .
33
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
44
@prefix cf_sname: <http://vocab.nerc.ac.uk/standard_name/> .
5+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
6+
@prefix dct: <http://purl.org/dc/terms/> .
57
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
68
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
79
@prefix this: <file://CDL/ereefs_gbr4_ncld.cdl/> .
@@ -17,6 +19,11 @@ this: a bald:Container ;
1719
this:shoc_version "v1.1 rev(5249)" ;
1820
NetCDF:Conventions "CF-1.0" ;
1921
NetCDF:title "GBR4 Hydro" ;
22+
dct:format [ a dct:MediaType ;
23+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
24+
dcat:distribution [ a dcat:Distribution ;
25+
dcat:mediaType [ a dcat:MediaType ;
26+
dct:identifier "application/x-netcdf" ] ] ;
2027
bald:contains this:botz,
2128
this:eta,
2229
this:latitude,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@prefix CFTerms: <http://def.scitools.org.uk/CFTerms/> .
2+
@prefix NetCDF: <http://def.scitools.org.uk/NetCDF/> .
3+
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
4+
@prefix cf_sname: <http://vocab.nerc.ac.uk/standard_name/> .
5+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
6+
@prefix dct: <http://purl.org/dc/terms/> .
7+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
8+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
9+
@prefix this: <file://CDL/hgroups.cdl/> .
10+
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
11+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
12+
13+
this: a bald:Container ;
14+
dct:format [ a dct:MediaType ;
15+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
16+
dcat:distribution [ a dcat:Distribution ;
17+
dcat:downloadURL <https://www.unidata.ucar.edu/software/netcdf/examples/test_hgroups.cdl> ;
18+
dcat:mediaType [ a dcat:MediaType ;
19+
dct:identifier "application/x-netcdf" ] ] ;
20+
bald:contains this:UTC_time .
21+
22+
this:UTC_time a bald:Array ;
23+
this:name "time" ;
24+
this:unit "none" ;
25+
bald:shape ( 74 ) .
26+

lib/bald/tests/integration/TTL/multi_array_reference.ttl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
2+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
3+
@prefix dct: <http://purl.org/dc/terms/> .
24
@prefix metce: <http://codes.wmo.int/common/observation-type/METCE/2013/> .
35
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
46
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@@ -7,6 +9,11 @@
79
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
810

911
this: a bald:Container ;
12+
dct:format [ a dct:MediaType ;
13+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
14+
dcat:distribution [ a dcat:Distribution ;
15+
dcat:mediaType [ a dcat:MediaType ;
16+
dct:identifier "application/x-netcdf" ] ] ;
1017
bald:contains this:data_variable1,
1118
this:data_variable2,
1219
this:list_collection,

lib/bald/tests/integration/TTL/point_template.ttl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
@prefix NetCDF: <http://def.scitools.org.uk/NetCDF/> .
33
@prefix bald: <https://www.opengis.net/def/binary-array-ld/> .
44
@prefix cf_sname: <http://vocab.nerc.ac.uk/standard_name/> .
5+
@prefix dcat: <http://www.w3.org/ns/dcat#> .
6+
@prefix dct: <http://purl.org/dc/terms/> .
57
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
68
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
79
@prefix this: <file://CDL/point_template.cdl/> .
@@ -69,6 +71,11 @@ this: a bald:Container ;
6971
NetCDF:Conventions "CF-1.6, ACDD-1.3" ;
7072
NetCDF:history "This file was created on 2016-09-22T18:16:06.590413Z" ;
7173
NetCDF:title "Oceanographic and surface meteorological data collected from the cordell bank monitoring station by the National Centers for Environmental Information (NCEI) in the Cordell Bank National Marine Sanctuary from 2015-03-25 to 2015-03-25" ;
74+
dct:format [ a dct:MediaType ;
75+
dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/> ] ;
76+
dcat:distribution [ a dcat:Distribution ;
77+
dcat:mediaType [ a dcat:MediaType ;
78+
dct:identifier "application/x-netcdf" ] ] ;
7279
bald:contains this:crs,
7380
this:instrument1,
7481
this:lat,

0 commit comments

Comments
 (0)