def metamap_dataset(self, ncdataset): with app.app_context(): # gets a metamap document of this service using wicken beliefs = IOOSNCCheck.beliefs() ncnamespaces = {'nc': pb_namespaces['ncml']} doc = NetCDFDogma('nc', beliefs, ncdataset, namespaces=ncnamespaces) # now make a map out of this # @TODO wicken should make this easier m_names, m_units = ['Variable Names*', 'Variable Units*'] metamap = {} for k in beliefs: try: metamap[k] = getattr(doc, doc._fixup_belief(k)[0]) except Exception as e: app.logger.exception("Problem setting belief (%s)", k) metamap[m_names] = [ ] # Override the Wicken return to preserve the order metamap[m_units] = [ ] # Override the Wicken return to preserve the order # Wicken doesn't preserve the order between the names and the units, # so what you wind up with is two lists that can't be related, but we # want to keep the relationship between the name and the units for k in ncdataset.variables.iterkeys(): var_name = k standard_name = getattr(ncdataset.variables[k], 'standard_name', '') units = getattr(ncdataset.variables[k], 'units', '') # Only map metadata where we have all three if var_name and standard_name and units: metamap[m_names].append('%s (%s)' % (var_name, standard_name)) metamap[m_units].append(units) return metamap
def get_pair(self, nc_dataset): ''' Return a pairwise object for the dataset ''' if isinstance(nc_dataset, basestring): nc_dataset = Dataset(nc_dataset, 'r') self.addCleanup(nc_dataset.close) dogma = NetCDFDogma('nc', self.cf.beliefs(), nc_dataset) pair = DSPair(nc_dataset, dogma) return pair
def metamap_dataset(self, ncdataset): with app.app_context(): # gets a metamap document of this service using wicken beliefs = IOOSNCCheck.beliefs() ncnamespaces = {'nc': pb_namespaces['ncml']} doc = NetCDFDogma('nc', beliefs, ncdataset, namespaces=ncnamespaces) # now make a map out of this # @TODO wicken should make this easier m_names, m_units = ['Variable Names*', 'Variable Units*'] metamap = {} for k in beliefs: try: metamap[k] = getattr(doc, doc._fixup_belief(k)[0]) except Exception: app.logger.exception("Problem setting belief (%s)", k) # Override the Wicken return to preserve the order metamap[m_names] = [] # Override the Wicken return to preserve the order metamap[m_units] = [] # Wicken doesn't preserve the order between the names and the units, # so what you wind up with is two lists that can't be related, but we # want to keep the relationship between the name and the units for k in ncdataset.variables.iterkeys(): var_name = k standard_name = getattr( ncdataset.variables[k], 'standard_name', '') units = getattr(ncdataset.variables[k], 'units', '') # Only map metadata where we have all three if var_name and standard_name and units: metamap[m_names].append('%s (%s)' % (var_name, standard_name)) metamap[m_units].append(units) return metamap
def metamap_dataset(self, ncdataset): with app.app_context(): # gets a metamap document of this service using wicken beliefs = IOOSNCCheck.beliefs() ncnamespaces = {'nc':pb_namespaces['ncml']} doc = NetCDFDogma('nc', beliefs, ncdataset, namespaces=ncnamespaces) # now make a map out of this # @TODO wicken should make this easier metamap = {} for k in beliefs: try: metamap[k] = getattr(doc, doc._fixup_belief(k)[0]) except Exception as e: print k, e return metamap
def load_datapair(self, ds): # allow ncml as well as nc prefixes namespaces = pb_namespaces.copy() namespaces['nc'] = namespaces['ncml'] namespaces['ncml'] = namespaces['ncml'] data_object = NetCDFDogma('ds', self.beliefs(), ds, namespaces=namespaces) return DSPair(ds, data_object)
# typically loaded from a file - a modified version of the IOOS Asset_SOS_MAP spread sheet for example 'service_provider_institution': """/ncml:netcdf/ncml:attribute[@name='institution']/@value""", 'data_conventions': """/ncml:netcdf/ncml:attribute[@name='institution']/@value""", 'latitude_units': """/ncml:netcdf/ncml:variable[@name='latitude']/ncml:attribute[@name='units']/@value""", 'does_not_exist': """/ncml:netcdf/ncml:attribute[@name='does_not_exist']/@value""" } #read a file ds = Dataset('test_ncs/result_surface.nc', 'a') data_object = NetCDFDogma('NetcdfCF', cf_metadata_beliefs, ds) data_object.__class__.__name__ data_object.service_provider_institution data_object.data_conventions data_object.latitude_units # will return empty data_object.does_not_exist data_object.service_provider_institution = 'David Stuebe' data_object.data_conventions = 'CF-3.14159' data_object.latitude_units = 'degrees a little bit west of north' # will fail - can't make a new attribute element (yet?) data_object.does_not_exist = 'foobar'
cf_metadata_beliefs = { # A map of what you call the metadata property to an xpath expression for it in some convention/encoding # typically loaded from a file - a modified version of the IOOS Asset_SOS_MAP spread sheet for example 'service_provider_institution':"""/ncml:netcdf/ncml:attribute[@name='institution']/@value""", 'data_conventions':"""/ncml:netcdf/ncml:attribute[@name='institution']/@value""", 'latitude_units':"""/ncml:netcdf/ncml:variable[@name='latitude']/ncml:attribute[@name='units']/@value""", 'does_not_exist':"""/ncml:netcdf/ncml:attribute[@name='does_not_exist']/@value""" } #read a file ds = Dataset('test_ncs/result_surface.nc','a') data_object = NetCDFDogma('NetcdfCF',cf_metadata_beliefs,ds) data_object.__class__.__name__ data_object.service_provider_institution data_object.data_conventions data_object.latitude_units # will return empty data_object.does_not_exist data_object.service_provider_institution = 'David Stuebe' data_object.data_conventions = 'CF-3.14159' data_object.latitude_units = 'degrees a little bit west of north'