Skip to content

Commit 6eb1391

Browse files
committed
dcat distribution
1 parent ff82890 commit 6eb1391

11 files changed

+184
-28
lines changed

lib/bald/__init__.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pyparsing
1515
import rdflib
1616
import rdflib.collection
17+
import rdflib.namespace
1718
import requests
1819
import six
1920

@@ -288,14 +289,19 @@ def check_uri(self, uri):
288289

289290
class Resource(object):
290291
_rdftype = 'bald__Resource'
292+
# def __init__(self, baseuri, relative_id, attrs=None, prefixes=None,
293+
# aliases=None, alias_graph=None):
291294
def __init__(self, baseuri, relative_id, attrs=None, prefixes=None,
292-
aliases=None, alias_graph=None):
295+
aliases=None, alias_graph=None, file_resource=False, file_locator=None):
296+
293297
"""
294298
A resource of metadata statements.
295299
296300
attrs: an dictionary of key value pair attributes
297301
"""
298302
self.baseuri = baseuri
303+
self.file_locator = file_locator
304+
self.is_file = file_resource
299305
self.relative_id = relative_id
300306

301307
if attrs is None:
@@ -339,7 +345,8 @@ def __repr__(self):
339345
def __setattr__(self, attr, value):
340346
reserved_attrs = ['baseuri', 'relative_id', 'prefixes', '_prefixes',
341347
'_prefix_suffix', '_http_uri_prefix', '_http_uri',
342-
'aliases', 'alias_graph', 'attrs', '_rdftype']
348+
'aliases', 'alias_graph', 'attrs', '_rdftype', 'file_locator',
349+
'is_file']
343350
if attr in reserved_attrs:
344351
object.__setattr__(self, attr, value)
345352
else:
@@ -535,6 +542,38 @@ def viewgraph(self):
535542

536543
return html
537544

545+
def _dcat_location(self, graph, selfnode):
546+
graph.bind('dcat', 'http://www.w3.org/ns/dcat#')
547+
graph.bind('dct', 'http://purl.org/dc/terms/')
548+
# template = ('dcat:distribution [
549+
# a dcat:Distribution;
550+
# dcat:downloadURL <{}>;
551+
# dcat:mediaType [
552+
# a dct:MediaType;
553+
# dct:identifier "application/x-netcdf"
554+
# ];
555+
# dct:format [
556+
# a dct:MediaType;
557+
# dct:identifier <http://vocab.nerc.ac.uk/collection/M01/current/NC/>
558+
# ]
559+
# ].')
560+
dcatnode = rdflib.BNode()
561+
dcfnode = rdflib.BNode()
562+
graph.add((selfnode, rdflib.URIRef('http://www.w3.org/ns/dcat#distribution'), dcatnode))
563+
graph.add((dcatnode, rdflib.namespace.RDF.type, rdflib.URIRef('http://www.w3.org/ns/dcat#Distribution')))
564+
if self.file_locator is not None:
565+
graph.add((dcatnode, rdflib.URIRef('http://www.w3.org/ns/dcat#downloadURL'), rdflib.URIRef(self.file_locator)))
566+
dcatmednode = rdflib.BNode()
567+
graph.add((dcatmednode, rdflib.namespace.RDF.type, rdflib.URIRef('http://www.w3.org/ns/dcat#MediaType')))
568+
graph.add((dcatmednode, rdflib.URIRef('http://purl.org/dc/terms/identifier'), rdflib.Literal('application/x-netcdf')))
569+
graph.add((dcatnode, rdflib.URIRef('http://www.w3.org/ns/dcat#mediaType'), dcatmednode))
570+
571+
graph.add((dcfnode, rdflib.namespace.RDF.type, rdflib.URIRef('http://purl.org/dc/terms/MediaType')))
572+
graph.add((dcfnode, rdflib.URIRef('http://purl.org/dc/terms/identifier'),
573+
rdflib.URIRef('http://vocab.nerc.ac.uk/collection/M01/current/NC/')))
574+
graph.add((selfnode, rdflib.URIRef('http://purl.org/dc/terms/format'), dcfnode))
575+
576+
538577
def rdfnode(self, graph):
539578
"""
540579
Create an RDF Node,
@@ -594,6 +633,9 @@ def rdfnode(self, graph):
594633
col = rdflib.collection.Collection(graph, list_name, list_items)
595634
graph.add((selfnode, rdfpred, list_name))
596635

636+
if self.is_file:
637+
self._dcat_location(graph, selfnode)
638+
597639
return selfnode
598640

599641
def rdfgraph(self):
@@ -757,7 +799,7 @@ def load(afilepath):
757799
except NameError:
758800
pass
759801

760-
def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
802+
def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None, file_locator=None):
761803
"""
762804
Load a file with respect to binary-array-linked-data.
763805
Returns a :class:`bald.Collection`
@@ -773,6 +815,7 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
773815
baseuri = 'file://{}/'.format(afilepath)
774816
elif type(baseuri) == str and not baseuri.endswith('/'):
775817
baseuri = '{}/'.format(baseuri)
818+
776819
identity = baseuri
777820
prefix_var_name = None
778821
if hasattr(fhandle, 'bald__isPrefixedBy'):
@@ -852,7 +895,8 @@ def load_netcdf(afilepath, baseuri=None, alias_dict=None, cache=None):
852895
# raise ValueError('duplicate aliases')
853896
# aliases = careful_update(aliases, dict(new_aliases))
854897
root_container = Container(baseuri, '', attrs, prefixes=prefixes,
855-
aliases=aliases, alias_graph=aliasgraph)
898+
aliases=aliases, alias_graph=aliasgraph,
899+
file_resource=True, file_locator=file_locator)
856900

857901
root_container.attrs['bald__contains'] = set()
858902
file_variables = {}

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

Lines changed: 22 additions & 15 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,
@@ -21,34 +28,38 @@ this:co2 a bald:Array ;
2128
CFTerms:missing_value "-32767" ;
2229
CFTerms:standard_name <http://vocab.nerc.ac.uk/standard_name/mass_fraction_of_carbon_dioxide_in_air/> ;
2330
NetCDF:FillValue "-32767" ;
24-
NetCDF:add_offset 403.192219379918 ;
31+
NetCDF:add_offset 403.19221938 ;
2532
NetCDF:long_name "Carbon Dioxide" ;
26-
NetCDF:scale_factor 0.000981685145029486 ;
33+
NetCDF:scale_factor 0.000981685145029 ;
2734
NetCDF:units "kg kg**-1" ;
2835
bald:references [ a bald:Reference ;
29-
bald:target this:levelist ;
30-
bald:targetReshape ( 1 60 1 1 ) ;
31-
bald:targetShape ( 60 ) ],
32-
[ a bald:Reference ;
3336
bald:target this:latitude ;
3437
bald:targetReshape ( 1 1 181 1 ) ;
3538
bald:targetShape ( 181 ) ],
39+
[ a bald:Reference ;
40+
bald:target this:longitude ;
41+
bald:targetReshape ( 1 1 1 360 ) ;
42+
bald:targetShape ( 360 ) ],
3643
[ a bald:Reference ;
3744
bald:target this:time ;
3845
bald:targetReshape ( 1 1 1 1 ) ;
3946
bald:targetShape ( 1 ) ],
4047
[ a bald:Reference ;
41-
bald:target this:longitude ;
42-
bald:targetReshape ( 1 1 1 360 ) ;
43-
bald:targetShape ( 360 ) ] ;
48+
bald:target this:levelist ;
49+
bald:targetReshape ( 1 60 1 1 ) ;
50+
bald:targetShape ( 60 ) ] ;
4451
bald:shape ( 1 60 181 360 ) .
4552

4653
this:lnsp a bald:Array ;
4754
CFTerms:missing_value "-32767" ;
4855
NetCDF:FillValue "-32767" ;
49-
NetCDF:add_offset 11.2087164280841 ;
56+
NetCDF:add_offset 11.2087164281 ;
5057
NetCDF:long_name "Logarithm of surface pressure" ;
5158
bald:references [ a bald:Reference ;
59+
bald:target this:levelist ;
60+
bald:targetReshape ( 1 60 1 1 ) ;
61+
bald:targetShape ( 60 ) ],
62+
[ a bald:Reference ;
5263
bald:target this:longitude ;
5364
bald:targetReshape ( 1 1 1 360 ) ;
5465
bald:targetShape ( 360 ) ],
@@ -59,11 +70,7 @@ this:lnsp a bald:Array ;
5970
[ a bald:Reference ;
6071
bald:target this:latitude ;
6172
bald:targetReshape ( 1 1 181 1 ) ;
62-
bald:targetShape ( 181 ) ],
63-
[ a bald:Reference ;
64-
bald:target this:levelist ;
65-
bald:targetReshape ( 1 60 1 1 ) ;
66-
bald:targetShape ( 60 ) ] ;
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)