Example #1
0
    def _resolve(self, expression, gdal_dataset):
        """
        Resolves the expression in the context of the gdal dataset
        :param str expression: the expression to resolve
        :param util.gdal_util.GDALGmlUtil gdal_dataset: the dataset for which to resolve
        :return:
        """
        if expression.startswith("metadata:"):
            value = gdal_dataset.get_metadata_tag(
                expression.replace("metadata:", ""))
        else:
            user_bands = []
            for band in GDALGmlUtil.get_fields_range_type(gdal_dataset):
                user_band = UserBand(band.field_name, "", "", "", "",
                                     band.nill_values, band.uom_code)
                user_bands.append(user_band)

            gdal_dictionary = {
                "resolutionX": gdal_dataset.get_offset_vectors()[0],
                "resolutionY": gdal_dataset.get_offset_vectors()[1],
                "originX": gdal_dataset.get_origin_x(),
                "originY": gdal_dataset.get_origin_y(),
                "minX": gdal_dataset.get_extents_x()[0],
                "maxX": gdal_dataset.get_extents_x()[1],
                "minY": gdal_dataset.get_extents_y()[0],
                "maxY": gdal_dataset.get_extents_y()[1],
                "bands": user_bands
            }
            if expression in gdal_dictionary:
                value = gdal_dictionary[expression]
            else:
                raise RuntimeException(
                    "Cannot evaluate the given grib expression {} on the given container."
                    .format(str(expression)))
        return value
Example #2
0
    def _read_bands(self):
        """
        Returns a user band extracted from the ingredients if specified (required for netCDF/GRIB)
        :rtype: list[UserBand]
        """
        if "bands" in self.options['coverage']['slicer']:
            bands = self.options['coverage']['slicer']['bands']

            number_of_bands = len(bands)

            # NOTE: rasdaman supports 1 band grib only to import
            recipe_type = self.options['coverage']['slicer']['type']
            if recipe_type == GRIBToCoverageConverter.RECIPE_TYPE and number_of_bands > 1:
                raise RuntimeError("Only single band grib files are currently supported. "
                                   "Given " + str(number_of_bands) + " bands in ingredient file.")

            ret_bands = []
            i = 0
            for band in bands:
                identifier = self._read_or_empty_string(band, "identifier")

                if recipe_type == GdalToCoverageConverter.RECIPE_TYPE:
                    # NOTE: for old ingredients with wrong defined "identifier" with band name instead of index 0-based
                    if not identifier.isdigit():
                        identifier = str(i)

                ret_bands.append(UserBand(
                    identifier,
                    self._read_or_empty_string(band, "name"),
                    self._read_or_empty_string(band, "description"),
                    self._read_or_empty_string(band, "definition"),
                    self._read_or_empty_string(band, "nilReason"),
                    self._read_or_empty_string(band, "nilValue").split(","),
                    self._read_or_empty_string(band, "uomCode")
                ))

                i += 1

            return ret_bands
        else:
            if self.options['coverage']['slicer']['type'] == GdalToCoverageConverter.RECIPE_TYPE:
                # If gdal does not specify bands in ingredient file, just fetch all bands from first file
                for file in self.session.get_files():
                    try:
                        gdal_util = GDALGmlUtil(file)
                        gdal_fields = gdal_util.get_fields_range_type()

                        ret_bands = []
                        for field in gdal_fields:
                            ret_bands.append(UserBand(
                                field.field_name,
                                field.field_name,
                                None,
                                None,
                                None,
                                field.nill_values
                            ))
                        break
                    except Exception as e:
                        if ConfigManager.skip == True:
                            pass
                        else:
                            raise e

                return ret_bands
            else:
                raise RuntimeError("'bands' must be specified in ingredient file for netCDF/GRIB recipes.")