def list_composite_functions(self): """Retrieve a list of available Composite Functions on Brazil Data Cube database.""" schemas = CompositeFunction.query().all() return [Serializer.serialize(schema) for schema in schemas], 200
def _create_cube_definition(cls, cube_id: str, params: dict) -> dict: """Create a data cube definition. Basically, the definition consists in `Collection` and `Band` attributes. Note: It does not try to create when data cube already exists. Args: cube_id - Data cube params - Dict of required values to create data cube. See @validators.py Returns: A serialized data cube information. """ cube_parts = get_cube_parts(cube_id) function = cube_parts.composite_function cube_id = cube_parts.datacube cube = Collection.query().filter( Collection.name == cube_id, Collection.version == params['version']).first() grs = GridRefSys.query().filter( GridRefSys.name == params['grs']).first() if grs is None: abort(404, f'Grid {params["grs"]} not found.') cube_function = CompositeFunction.query().filter( CompositeFunction.alias == function).first() if cube_function is None: abort(404, f'Function {function} not found.') data = dict(name='Meter', symbol='m') resolution_meter, _ = get_or_create_model(ResolutionUnit, defaults=data, symbol='m') mime_type, _ = get_or_create_model(MimeType, defaults=dict(name=COG_MIME_TYPE), name=COG_MIME_TYPE) if cube is None: cube = Collection( name=cube_id, title=params['title'], temporal_composition_schema=params['temporal_composition'] if function != 'IDT' else None, composite_function_id=cube_function.id, grs=grs, _metadata=params['metadata'], description=params['description'], collection_type='cube', is_public=params.get('public', True), version=params['version']) cube.save(commit=False) bands = [] default_bands = (CLEAR_OBSERVATION_NAME.lower(), TOTAL_OBSERVATION_NAME.lower(), PROVENANCE_NAME.lower()) band_map = dict() for band in params['bands']: name = band['name'].strip() if name in default_bands: continue is_not_cloud = params['quality_band'] != band['name'] if band['name'] == params['quality_band']: data_type = 'uint8' else: data_type = band['data_type'] band_model = Band(name=name, common_name=band['common_name'], collection=cube, min_value=0, max_value=10000 if is_not_cloud else 4, nodata=-9999 if is_not_cloud else 255, scale=0.0001 if is_not_cloud else 1, data_type=data_type, resolution_x=params['resolution'], resolution_y=params['resolution'], resolution_unit_id=resolution_meter.id, description='', mime_type_id=mime_type.id) if band.get('metadata'): band_model._metadata = cls._validate_band_metadata( deepcopy(band['metadata']), band_map) band_model.save(commit=False) bands.append(band_model) band_map[name] = band_model if band_model._metadata: for _band_origin_id in band_model._metadata['expression'][ 'bands']: band_provenance = BandSRC(band_src_id=_band_origin_id, band_id=band_model.id) band_provenance.save(commit=False) quicklook = Quicklook( red=band_map[params['bands_quicklook'][0]].id, green=band_map[params['bands_quicklook'][1]].id, blue=band_map[params['bands_quicklook'][2]].id, collection=cube) quicklook.save(commit=False) # Create default Cube Bands if function != 'IDT': _ = cls.get_or_create_band(cube.id, **CLEAR_OBSERVATION_ATTRIBUTES, resolution_unit_id=resolution_meter.id, resolution_x=params['resolution'], resolution_y=params['resolution']) _ = cls.get_or_create_band(cube.id, **TOTAL_OBSERVATION_ATTRIBUTES, resolution_unit_id=resolution_meter.id, resolution_x=params['resolution'], resolution_y=params['resolution']) if function == 'STK': _ = cls.get_or_create_band( cube.id, **PROVENANCE_ATTRIBUTES, resolution_unit_id=resolution_meter.id, resolution_x=params['resolution'], resolution_y=params['resolution']) if params.get('is_combined') and function != 'MED': _ = cls.get_or_create_band(cube.id, **DATASOURCE_ATTRIBUTES, resolution_unit_id=resolution_meter.id, resolution_x=params['resolution'], resolution_y=params['resolution']) return CollectionForm().dump(cube)