def metadata_index_responder(sesh, network, climo=False): '''The function creates a pydap csv response which lists variable metadata out of the database. It returns an generator for the contents of the file :param sesh: database session :type sesh: sqlalchemy.orm.session.Session :param network: Name of the network for which variables should be listed :type network: str :rtype: generator ''' maxlen = 256 if climo: climo_filt = or_(Variable.cell_method.like('%within%'), Variable.cell_method.like('%over%')) else: climo_filt = not_(or_(Variable.cell_method.like('%within%'), Variable.cell_method.like('%over%'))) rv = sesh.query(Variable).join(Network).filter(Network.name == network).filter(climo_filt) a = np.array([(var.name, var.standard_name, var.cell_method, var.unit) for var in rv], dtype=np.dtype({'names': ['variable', 'standard_name', 'cell_method', 'unit'], 'formats':[(str, maxlen), (str, maxlen), (str, maxlen), (str, maxlen)]}) ) dst = DatasetType('Variable metadata') seq = SequenceType('variables') seq['variable'] = BaseType('variable') seq['standard_name'] = BaseType('standard_name', reference='http://llnl.gov/') seq['cell_method'] = BaseType('cell_method', reference='http://llnl.gov/') seq['unit'] = BaseType('unit') seq.data = a dst['variables'] = seq responder = BaseHandler(dst) environ = {'PATH_INFO': '/variables.foo.ascii', 'REQUEST_METHOD': 'GET'} return responder(environ, null_start_response)
def density(dataset, salinity, temperature, pressure): """Calculate in-situ density. This function calculated in-situ density from absolute salinity and conservative temperature, using the `gsw.rho` function. Returns a new sequence with the data. """ # find sequence for sequence in walk(dataset, SequenceType): break else: raise ConstraintExpressionError( 'Function "bounds" should be used on a Sequence.') selection = sequence[salinity.name, temperature.name, pressure.name] rows = [tuple(row) for row in selection] data = np.rec.fromrecords(rows, names=['salinity', 'temperature', 'pressure']) rho = gsw.rho(data['salinity'], data['temperature'], data['pressure']) out = SequenceType("result") out['rho'] = BaseType("rho", units="kg/m**3") out.data = np.rec.fromrecords(rho.reshape(-1, 1), names=['rho']) return out
def density(dataset, salinity, temperature, pressure): """Calculate in-situ density. This function calculated in-situ density from absolute salinity and conservative temperature, using the `gsw.rho` function. Returns a new sequence with the data. """ # find sequence for sequence in walk(dataset, SequenceType): break else: raise ConstraintExpressionError( 'Function "bounds" should be used on a Sequence.') selection = sequence[salinity.name, temperature.name, pressure.name] rows = [tuple(row) for row in selection] data = np.rec.fromrecords( rows, names=['salinity', 'temperature', 'pressure']) rho = gsw.rho(data['salinity'], data['temperature'], data['pressure']) out = SequenceType("result") out['rho'] = BaseType("rho", units="kg/m**3") out.data = np.rec.fromrecords(rho.reshape(-1, 1), names=['rho']) return out
def test_regexp(self): sequence = SequenceType("sequence") sequence["name"] = BaseType("name") sequence.data = IterData([ ("John", "Paul", "George", "Ringo"), ], sequence) filtered = sequence[ConstraintExpression('sequence.name=~"J.*"')] self.assertEqual(list(filtered.iterdata()), [("John", )])
def test_regexp(self): sequence = SequenceType("sequence") sequence["name"] = BaseType("name") sequence.data = IterData([ ("John", "Paul", "George", "Ringo"), ], sequence) filtered = sequence[ConstraintExpression('sequence.name=~"J.*"')] self.assertEqual(list(filtered.iterdata()), [("John",)])
def sequence_example(): """Create a standard sequence from the DAP spec.""" example = SequenceType("example") example["index"] = BaseType("index") example["temperature"] = BaseType("temperature") example["site"] = BaseType("site") example.data = np.rec.fromrecords([ (10, 15.2, "Diamond_St"), (11, 13.1, 'Blacktail_Loop'), (12, 13.3, 'Platinum_St'), (13, 12.1, 'Kodiak_Trail')], names=list(example.keys())) return example
def sequence_example(): """Create a standard sequence from the DAP spec.""" example = SequenceType("example") example["index"] = BaseType("index") example["temperature"] = BaseType("temperature") example["site"] = BaseType("site") example.data = np.rec.fromrecords([ (10, 15.2, "Diamond_St"), (11, 13.1, 'Blacktail_Loop'), (12, 13.3, 'Platinum_St'), (13, 12.1, 'Kodiak_Trail')], names=list(example.keys())) return example
def sequence_type_data(): """ Simple sequence test data """ data = [(10, 15.2, 'Diamond_St'), (11, 13.1, 'Blacktail_Loop'), (12, 13.3, 'Platinum_St'), (13, 12.1, 'Kodiak_Trail')] dtype = [('index', '<i4'), ('temperature', '<f8'), ('station', 'S40')] seq = SequenceType('sequence') seq['index'] = BaseType('index') seq['temperature'] = BaseType('temperature') seq['station'] = BaseType('station') seq.data = np.array(data, dtype=dtype) return seq
def sequence_type_data(): """ Simple sequence test data """ data = [(10, 15.2, 'Diamond_St'), (11, 13.1, 'Blacktail_Loop'), (12, 13.3, 'Platinum_St'), (13, 12.1, 'Kodiak_Trail')] dtype = [('index', '<i4'), ('temperature', '<f8'), ('station', 'S40')] seq = SequenceType('sequence') seq['index'] = BaseType('index') seq['temperature'] = BaseType('temperature') seq['station'] = BaseType('station') seq.data = np.array(data, dtype=dtype) return seq
def double(dataset, var): """A dummy function that doubles a value. The value must be in a sequence. Return a new sequence with the value doubled. """ # sequence is the first variable sequence = next(dataset.children()) # get a single variable and double its value selection = sequence[var.name] rows = [(value * 2, ) for value in selection] # create output sequence out = SequenceType("result") out["double"] = BaseType("double") out.data = np.rec.fromrecords(rows, names=["double"]) return out
def double(dataset, var): """A dummy function that doubles a value. The value must be in a sequence. Return a new sequence with the value doubled. """ # sequence is the first variable sequence = next(dataset.children()) # get a single variable and double its value selection = sequence[var.name] rows = [(value*2,) for value in selection] # create output sequence out = SequenceType("result") out["double"] = BaseType("double") out.data = np.rec.fromrecords(rows, names=["double"]) return out
# A very simple sequence: flat and with no strings. This sequence can be mapped # directly to a Numpy structured array, and can be easily encoded and decoded # in the DAP spec. VerySimpleSequence = DatasetType("VerySimpleSequence") VerySimpleSequence["sequence"] = SequenceType("sequence") VerySimpleSequence["sequence"]["byte"] = BaseType("byte") VerySimpleSequence["sequence"]["int"] = BaseType("int") VerySimpleSequence["sequence"]["float"] = BaseType("float") VerySimpleSequence["sequence"].data = np.array([ (0, 1, 10.), (1, 2, 20.), (2, 3, 30.), (3, 4, 40.), (4, 5, 50.), (5, 6, 60.), (6, 7, 70.), (7, 8, 80.), ], dtype=[('byte', 'b'), ('int', 'i4'), ('float', 'f4')]) # A nested sequence. NestedSequence = DatasetType("NestedSequence") NestedSequence["location"] = SequenceType("location") NestedSequence["location"]["lat"] = BaseType("lat") NestedSequence["location"]["lon"] = BaseType("lon") NestedSequence["location"]["elev"] = BaseType("elev") NestedSequence["location"]["time_series"] = SequenceType("time_series") NestedSequence["location"]["time_series"]["time"] = BaseType("time")
from collections import OrderedDict # A very simple sequence: flat and with no strings. This sequence can be mapped # directly to a Numpy structured array, and can be easily encoded and decoded # in the DAP spec. VerySimpleSequence = DatasetType("VerySimpleSequence") VerySimpleSequence["sequence"] = SequenceType("sequence") VerySimpleSequence["sequence"]["byte"] = BaseType("byte") VerySimpleSequence["sequence"]["int"] = BaseType("int") VerySimpleSequence["sequence"]["float"] = BaseType("float") VerySimpleSequence["sequence"].data = np.array([ (0, 1, 10.), (1, 2, 20.), (2, 3, 30.), (3, 4, 40.), (4, 5, 50.), (5, 6, 60.), (6, 7, 70.), (7, 8, 80.), ], dtype=[('byte', 'b'), ('int', 'i4'), ('float', 'f4')]) # A nested sequence. NestedSequence = DatasetType("NestedSequence") NestedSequence["location"] = SequenceType("location") NestedSequence["location"]["lat"] = BaseType("lat") NestedSequence["location"]["lon"] = BaseType("lon") NestedSequence["location"]["elev"] = BaseType("elev") NestedSequence["location"]["time_series"] = SequenceType("time_series") NestedSequence["location"]["time_series"]["time"] = BaseType("time") NestedSequence["location"]["time_series"]["slp"] = BaseType("slp")