def is_cf_height_constrained(self, comp): item_sn = metarelate.Item(('<http://def.scitools.org.uk/cfdatamodel/' 'standard_name>'), 'standard_name') item_h = metarelate.Item(('<http://vocab.nerc.ac.uk/standard_name/' 'height>'), 'height') snprop = metarelate.StatementProperty(item_sn, item_h) item_u = metarelate.Item(('<http://def.scitools.org.uk/cfdatamodel/' 'units>'), 'units') uprop = metarelate.StatementProperty(item_u, metarelate.Item('"m"', 'm')) pts_pred = metarelate.Item(('<http://def.scitools.org.uk/cfdatamodel/' 'points>'), 'points') result = False if self.is_cf_constrained(comp): props = comp.dim_coord.component.properties if len(props) == 3: if snprop in props and uprop in props: preds = [prop.predicate for prop in props] if pts_pred in preds: result = True return result
def cfname(name, units): """Create a new Component for the CF name and units.""" # Fail if unit not udunits parseable # checkunit = Unit(units) standard_name = '{p}{c}'.format(p=pre['cfnames'], c=name) req = requests.get(standard_name) if req.status_code == 200: name = standard_name pred = '{}standard_name'.format(pre['cfmodel']) else: pred = '{}long_name'.format(pre['cfmodel']) cfun = '{}units'.format(pre['cfmodel']) if units == '1': units = u'1' acfuprop = metarelate.StatementProperty(metarelate.Item(cfun, 'units'), metarelate.Item(units, units)) acfnprop = metarelate.StatementProperty( metarelate.Item(pred, pred.split('/')[-1]), metarelate.Item(name, name.split('/')[-1])) cff = '{}Field'.format(pre['cfmodel']) acfcomp = metarelate.Component(None, cff, [acfnprop, acfuprop]) return acfcomp
def property_cf_units(): data = '<http://def.scitools.org.uk/cfdatamodel/units>' notation = 'units' name = metarelate.Item(data, notation) value = metarelate.Item('m s-1') return metarelate.StatementProperty(name, value)
def property_um_stash(): data = '<http://reference.metoffice.gov.uk/um/f3/stash>' notation = 'stash' name = metarelate.Item(data, notation) data = '<http://reference.metoffice.gov.uk/um/stash/m02s32i202>' notation = 'm02s32i202' value = metarelate.Item(data, notation) return metarelate.StatementProperty(name, value)
def property2_cf_standard_name(): data = '<http://def.scitools.org.uk/cfdatamodel/standard_name>' notation = 'standard_name' predicate = metarelate.Item(data, notation) data = '<http://vocab.nerc.ac.uk/standard_name/x_wind>' notation = 'x_wind' rdfobject = metarelate.Item(data, notation) return metarelate.StatementProperty(predicate, rdfobject)
def property_cf_standard_name(): data = '<http://def.scitools.org.uk/cfdatamodel/standard_name>' notation = 'standard_name' predicate = metarelate.Item(data, notation) data = '<http://vocab.nerc.ac.uk/standard_name/' \ 'tendency_of_sea_ice_thickness_due_to_dynamics>' notation = 'tendency_of_sea_ice_thickness_due_to_dynamics' rdfobject = metarelate.Item(data, notation) return metarelate.StatementProperty(predicate, rdfobject)
def grib2_comp(arecord, errs): """Create a new Component from the provided GRIB2 parameter. """ griburi = 'http://codes.wmo.int/grib2/codeflag/4.2/{d}-{c}-{i}' griburi = griburi.format(d=arecord.disc, c=arecord.pcat, i=arecord.pnum) req = requests.get(griburi) if req.status_code != 200: errs.append('unrecognised grib2 parameter code: {}'.format(griburi)) gpd = 'http://codes.wmo.int/def/grib2/parameterId' agribprop = metarelate.StatementProperty( metarelate.Item(gpd, 'grib2_parameter'), metarelate.Item(griburi)) gribmsg = 'http://codes.wmo.int/def/codeform/GRIB-message' agribcomp = metarelate.Component(None, gribmsg, [agribprop]) return (agribcomp, errs)
def test_setattr(self): prop = stock.property_cf_units() ctype = '<http://def.scitools.org.uk/cfdatamodel/Field>' acomp = metarelate.Component(None, com_type=ctype, properties=[prop]) data = '<http://def.scitools.org.uk/cfdatamodel/standard_name>' notation = 'standard_name' ptype = metarelate.Item(data, notation) data = '<http://vocab.nerc.ac.uk/standard_name/x_wind' notation = 'x_wind' value = metarelate.Item(data, notation) acomp.standard_name = metarelate.StatementProperty(ptype, value) self.assertTrue( isinstance(acomp.standard_name, metarelate.StatementProperty))
def stash_comp(stashmsi, errs): """Create a new Component from the provided stash code. """ stashuri = '{p}{c}'.format(p=pre['moStCon'], c=stashmsi) headers = { 'content-type': 'application/ld+json', 'Accept': 'application/ld+json' } req = requests.get(stashuri, headers=headers) if req.status_code != 200: errs.append('unrecognised stash code: {}'.format(stashuri)) pred = metarelate.Item('{}stash'.format(pre['moumdpF3']), 'stash') robj = metarelate.Item(stashuri, stashmsi) astashprop = metarelate.StatementProperty(pred, robj) ppff = '{}UMField'.format(pre['moumdpF3']) astashcomp = metarelate.Component(None, ppff, [astashprop]) return (astashcomp, errs)
def retrieve_mapping_templates(self, sourcetype, targettype, service=None): """ return the format specific mappings for a particular source and target component type """ if not isinstance(sourcetype, metarelate.Item): sourcetype = metarelate.Item(sourcetype) if not isinstance(targettype, metarelate.Item): targettype = metarelate.Item(targettype) if not (sourcetype.is_uri() and targettype.is_uri()): raise ValueError('sourcetype and targettype must both be URIs') qstr = ('SELECT ?mapping ?source ?target ?invertible ?inverted ' '''(GROUP_CONCAT(?valueMap; SEPARATOR = '&') AS ?valueMaps) ''' 'WHERE { ' 'GRAPH <http://metarelate.net/mappings.ttl> { { ' '?mapping mr:source ?source ; ' 'mr:target ?target ; ' 'mr:invertible ?invertible .' 'BIND("False" AS ?inverted) ' 'OPTIONAL {?mapping mr:hasValueMap ?valueMap . } ' 'MINUS {?mapping ^dc:replaces+ ?anothermap} ' '} UNION { ' '?mapping mr:source ?target ; ' ' mr:target ?source ; ' ' mr:invertible "True" . ' 'BIND("True" AS ?inverted) ' 'BIND("True" AS ?invertible) ' 'OPTIONAL {?mapping mr:hasValueMap ?valueMap . } ' 'MINUS {?mapping ^dc:replaces+ ?anothermap} ' '} } ' 'GRAPH <http://metarelate.net/concepts.ttl> { ' '?source rdf:type %s . ' '?target rdf:type %s . ' '}} ' 'GROUP BY ?mapping ?source ?target ?inverted ?invertible ' 'ORDER BY ?mapping') % (sourcetype.data, targettype.data) if service is not None: qstr = ('SELECT ?mapping ?source ?target ?invertible ?inverted ' '?valueMaps ' 'WHERE { ' "SERVICE <%s> {" "%s" "}}" % (service, qstr)) map_templates = self.run_query(qstr) return json.dumps(map_templates)
def retrieve_mappings(self, sourcetype, targettype, service=None): sourcetype = metarelate.Item(sourcetype) targettype = metarelate.Item(targettype) templates = self.retrieve_mapping_templates(sourcetype, targettype, service=service) map_templates = json.loads(templates) mapping_list = deque() mapping_queue = Queue() mq = 0 for mt in map_templates: mapping_queue.put( metarelate.Mapping(mt.get('mapping'), invertible=mt.get('invertible'), inverted=mt.get('inverted'))) mq += 1 for i in range(MAXTHREADS): MappingPopulateWorker(mapping_queue, mapping_list, self, service).start() mapping_queue.join() if len(mapping_list) != mq: msg = '{} entries in mapping_list, expected {}' raise ValueError(msg.format(len(mapping_list), mq)) return mapping_list