|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import unittest |
| 5 | + |
| 6 | +import netCDF4 |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +import bald |
| 10 | +from bald.tests import BaldTestCase |
| 11 | +from rdflib import Graph |
| 12 | + |
| 13 | + |
| 14 | +class Test(BaldTestCase): |
| 15 | + def setUp(self): |
| 16 | + self.cdl_path = os.path.join(os.path.dirname(__file__), 'CDL') |
| 17 | + self.ttl_path = os.path.join(os.path.dirname(__file__), 'TTL') |
| 18 | + self.graph = Graph() |
| 19 | + |
| 20 | + #load bald graphs from cdl |
| 21 | + for cdl_file in glob.glob(os.path.join(os.path.dirname(__file__), 'CDL', '*.cdl')): |
| 22 | + with self.temp_filename('.nc') as tfile: |
| 23 | + subprocess.check_call(['ncgen', '-o', tfile, cdl_file]) |
| 24 | + root_container = bald.load_netcdf(tfile) |
| 25 | + curr_g = root_container.rdfgraph() |
| 26 | + |
| 27 | + #merge into graph in test obj |
| 28 | + self.graph = self.graph + curr_g |
| 29 | + |
| 30 | + def test_sparql_count_standard_names(self): |
| 31 | + #query standard_name values used and frequency |
| 32 | + qres = self.graph.query( |
| 33 | + """ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 34 | + PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 35 | + PREFIX bald: <http://binary-array-ld.net/latest/> |
| 36 | + SELECT ?name (COUNT(?name) as ?NELEMENTS) |
| 37 | + WHERE { |
| 38 | + ?contained a bald:Array . |
| 39 | + ?contained ?pred ?name |
| 40 | + FILTER(regex(str(?pred), "standard_name")) |
| 41 | + } |
| 42 | + GROUP BY ?name |
| 43 | + ORDER BY DESC(?NELEMENTS) |
| 44 | + """) |
| 45 | + for row in qres: |
| 46 | + print("%s :: %s" % row) |
| 47 | + #print( len(qres)) |
| 48 | + expected_result_rows = 17 |
| 49 | + self.assertTrue(len(qres) == expected_result_rows) |
| 50 | + |
| 51 | + def test_sparql_demo_graph_viz_labels(self): |
| 52 | + #query standard_name values used and frequency |
| 53 | + qres = self.graph.query( |
| 54 | + """ |
| 55 | + PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 56 | + PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 57 | + PREFIX bald: <http://binary-array-ld.net/latest/> |
| 58 | + PREFIX cf: <http://def.scitools.org.uk/CFTerms/> |
| 59 | + SELECT ?container ?contained ?containerName ?containedlabel |
| 60 | + WHERE { |
| 61 | + ?container a bald:Container . |
| 62 | + ?container bald:contains ?contained . |
| 63 | + ?contained a bald:Array . |
| 64 | + { ?contained cf:long_name ?containedlabel } |
| 65 | + UNION |
| 66 | + { ?contained ?lnprop ?containedlabel |
| 67 | + FILTER(regex(str(?lnprop), "long_name", "i")) |
| 68 | + } |
| 69 | + BIND( str(?container) as ?containerName) } |
| 70 | + """) |
| 71 | + for row in qres: |
| 72 | + print("%s, %s, %s, %s" % row) |
| 73 | + # print( len(qres)) |
| 74 | + expected_result_rows = 150 |
| 75 | + self.assertTrue(len(qres) == expected_result_rows) |
| 76 | + |
0 commit comments