in analysis/webservice/algorithms/DataInBoundsSearch.py [0:0]
def calc(self, computeOptions, **args):
ds, parameter, start_time, end_time, bounding_polygon,\
metadata_filter, min_elevation, max_elevation, compact = self.parse_arguments(computeOptions)
includemeta = computeOptions.get_include_meta()
log = logging.getLogger(__name__)
min_lat = max_lat = min_lon = max_lon = None
tile_service = self._get_tile_service()
if bounding_polygon:
min_lat = bounding_polygon.bounds[1]
max_lat = bounding_polygon.bounds[3]
min_lon = bounding_polygon.bounds[0]
max_lon = bounding_polygon.bounds[2]
tiles = tile_service.find_tiles_in_box(min_lat, max_lat, min_lon, max_lon, ds, start_time, end_time,
min_elevation=min_elevation, max_elevation=max_elevation, fetch_data=False)
need_to_fetch = True
else:
tiles = self._get_tile_service().get_tiles_by_metadata(metadata_filter, ds, start_time, end_time)
need_to_fetch = False
data = []
log.info(f'Matched {len(tiles):,} tiles.')
for i in range(len(tiles)-1, -1, -1): # tile in tiles:
tile = tiles.pop(i)
tile_id = tile.tile_id
log.info(f'Processing tile {tile_id} | {i=}')
if need_to_fetch:
tile = tile_service.fetch_data_for_tiles(tile)[0]
tile = tile_service.mask_tiles_to_bbox(min_lat, max_lat, min_lon, max_lon, [tile])
tile = tile_service.mask_tiles_to_time_range(start_time, end_time, tile)
if min_elevation is not None and max_elevation is not None:
tile = tile_service.mask_tiles_to_elevation(min_elevation, max_elevation, tile)
if len(tile) == 0:
log.info(f'Skipping empty tile {tile_id}')
continue
tile = tile[0]
for nexus_point in tile.nexus_point_generator():
point = dict()
point['id'] = tile.tile_id
if parameter == 'sst':
point['sst'] = nexus_point.data_vals
elif parameter == 'sss':
point['sss'] = nexus_point.data_vals
elif parameter == 'wind':
point['wind_u'] = nexus_point.data_vals
try:
point['wind_v'] = tile.meta_data['wind_v'][tuple(nexus_point.index)]
except (KeyError, IndexError):
pass
try:
point['wind_direction'] = tile.meta_data['wind_dir'][tuple(nexus_point.index)]
except (KeyError, IndexError):
pass
try:
point['wind_speed'] = tile.meta_data['wind_speed'][tuple(nexus_point.index)]
except (KeyError, IndexError):
pass
else:
variables = []
data_vals = nexus_point.data_vals if tile.is_multi else [nexus_point.data_vals]
for value, variable in zip(data_vals, tile.variables):
if variable.standard_name:
var_name = variable.standard_name
else:
var_name = variable.variable_name
variables.append({var_name: value})
point['variables'] = variables
data.append({
'latitude': nexus_point.latitude,
'longitude': nexus_point.longitude,
'time': nexus_point.time,
'elevation': nexus_point.depth,
'data': point
})
if includemeta and len(tiles) > 0:
meta = [tile.get_summary() for tile in tiles]
else:
meta = None
result = DataInBoundsResult(
results=data,
stats={},
meta=meta,
compact=compact
)
result.extendMeta(min_lat, max_lat, min_lon, max_lon, "", start_time, end_time)
log.info(f'Finished subsetting. Generated {len(data):,} points')
return result