Skip to content

Instantly share code, notes, and snippets.

@v0lat1le
Created November 23, 2015 11:19
Show Gist options
  • Select an option

  • Save v0lat1le/7eb8bd89034d6ea5607a to your computer and use it in GitHub Desktop.

Select an option

Save v0lat1le/7eb8bd89034d6ea5607a to your computer and use it in GitHub Desktop.
I Can Haz GDF
def get_descriptor(query=None):
# descriptor = {
# 'LS5TM': { # storage_type identifier
# 'dimensions': ['x', 'y', 't'],
# 'variables': { # These will be the variables which can be accessed as arrays
# 'B10': {
# 'datatype': 'int16',
# 'nodata_value': -999
# },
# 'B20': {
# 'datatype': 'int16',
# 'nodata_value': -999
# },
# 'B30': {
# 'datatype': 'int16',
# 'nodata_value': -999
# },
# 'B40': {
# 'datatype': 'int16',
# 'nodata_value': -999
# },
# 'B50': {
# 'datatype': 'int16',
# 'nodata_value': -999
# },
# 'B70': {
# 'datatype': 'int16',
# 'nodata_value': -999
# },
# 'PQ': { # There is no reason why we can't put PQ in with NBAR if we want to
# 'datatype': 'int16'
# }
# },
# 'result_min': (140, -36, 1293840000),
# 'result_max': (141, -35, 1325376000),
# 'overlap': (0, 0, 0), # We won't be doing this in the pilot
# 'buffer_size': (128, 128, 128), # Chunk size to use
# 'result_shape': (8000, 8000, 40), # Overall size of result set
# 'irregular_indices': { # Regularly indexed dimensions (e.g. x & y) won't need to be specified,
# # but we could also do that here if we wanted to
# 't': date_array # Array of days since 1/1/1970
# },
# 'storage_units': { # Should wind up with 8 for the 2x2x2 query above
# (140, -36, 2010): { # Storage unit indices
# 'storage_min': (140, -36, 1293840000),
# 'storage_max': (141, -35, 1293800400),
# 'storage_shape': (4000, 4000, 24)
# },
# (140, -36, 2011): { # Storage unit indices
# 'storage_min': (140, -36, 1293800400),
# 'storage_max': (141, -35, 1325376000),
# 'storage_shape': (4000, 4000, 23)
# },
# (140, -35, 2011): { # Storage unit indices
# 'storage_min': (140, -36, 1293840000),
# 'storage_max': (141, -35, 1293800400),
# 'storage_shape': (4000, 4000, 20)
# }
# # ...
# # <more storage_unit sub-descriptors>
# # ...
# }
# # ...
# # <more storage unit type sub-descriptors>
# # ...
# }
# }
warnings.warn("get_descriptor is deprecated. Don't use unless your name is Peter", DeprecationWarning)
data_index = index.data_management_connect()
sus = data_index.get_storage_units()
descriptor = {}
for su in sus:
thing_key = su.storage_mapping.match.metadata['platform']['code'] + '_' + \
su.storage_mapping.match.metadata['instrument']['name']
thing = descriptor.setdefault(thing_key, {
'storage_units': {},
'variables': {},
'result_min': None,
'result_max': None,
'dimensions': None,
'result_shape': None
})
for name, attrs in su.descriptor['measurements'].items():
if len(attrs['dimensions']) == 3:
thing['variables'][name] = {
'datatype': attrs['dtype'],
'nodata_value': attrs['nodata']
}
thing['dimensions'] = attrs['dimensions']
storage_min = tuple(min(su.descriptor['coordinates'][dim]['begin'],
su.descriptor['coordinates'][dim]['end']) for dim in thing['dimensions'])
storage_max = tuple(max(su.descriptor['coordinates'][dim]['begin'],
su.descriptor['coordinates'][dim]['end']) for dim in thing['dimensions'])
storage_shape = tuple(su.descriptor['coordinates'][dim]['length'] for dim in thing['dimensions'])
thing['storage_units'][storage_min] = {
'storage_min': storage_min,
'storage_max': storage_max,
'storage_shape': storage_shape,
'filepath': su.filepath
}
if not thing['result_min']:
thing['result_min'] = storage_min
thing['result_max'] = storage_max
thing['result_shape'] = storage_shape
for idx, dim in enumerate(thing['dimensions']):
if storage_min[idx] < thing['result_min'][idx]:
thing['result_min'] = thing['result_min'][:idx] + \
(storage_min[idx],) + \
thing['result_min'][idx + 1:]
thing['result_shape'] = thing['result_shape'][:idx] + \
(thing['result_shape'][idx] + storage_shape[idx],) + \
thing['result_shape'][idx + 1:]
if storage_max[idx] > thing['result_max'][idx]:
thing['result_max'] = thing['result_max'][:idx] + \
(storage_max[idx],) + \
thing['result_max'][idx + 1:]
thing['result_shape'] = thing['result_shape'][:idx] + \
(thing['result_shape'][idx] + storage_shape[idx],) + \
thing['result_shape'][idx + 1:]
print(descriptor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment