Exemple #1
0
class Backend(roles.BackendRole):
    def __init__(self, rescale_configs=None, **kwargs):
        self.rescale_configs = rescale_configs or [DEFAULT_RCONFIG]
        self.rescaler = Rescaler(*self.rescale_configs)
        super(Backend, self).__init__(**kwargs)

    @property
    def known_grids(self):
        # should work regardless of grid
        return None

    def create_output_from_product(self, gridded_product, output_pattern=None,
                                   data_type=None, inc_by_one=None, fill_value=0,
                                   tiled=False, blockxsize=None, blockysize=None, **kwargs):
        data_type = data_type or np.uint8
        etype = np2etype[data_type]
        inc_by_one = inc_by_one or False
        grid_def = gridded_product["grid_definition"]
        if not output_pattern:
            output_pattern = DEFAULT_OUTPUT_PATTERN
        if "{" in output_pattern:
            # format the filename
            of_kwargs = gridded_product.copy(as_dict=True)
            of_kwargs["data_type"] = data_type
            output_filename = self.create_output_filename(output_pattern,
                                                          grid_name=grid_def["grid_name"],
                                                          rows=grid_def["height"],
                                                          columns=grid_def["width"],
                                                          **of_kwargs)
        else:
            output_filename = output_pattern

        if os.path.isfile(output_filename):
            if not self.overwrite_existing:
                LOG.error("Geotiff file already exists: %s", output_filename)
                raise RuntimeError("Geotiff file already exists: %s" % (output_filename,))
            else:
                LOG.warning("Geotiff file already exists, will overwrite: %s", output_filename)

        try:
            if np.issubdtype(data_type, np.floating):
                # assume they don't want to scale floating point
                data = gridded_product.get_data_array()
                rescale_options = {}
            else:
                LOG.debug("Scaling %s data to fit in geotiff...", gridded_product["product_name"])
                rescale_options = self.rescaler.get_rescale_options(gridded_product,
                                                                    data_type,
                                                                    inc_by_one=inc_by_one,
                                                                    fill_value=fill_value)
                data = self.rescaler.rescale_product(gridded_product, data_type, rescale_options=rescale_options.copy())

            # Create the geotiff
            # X and Y rotation are 0 in most cases so we just hard-code it
            geotransform = gridded_product["grid_definition"].gdal_geotransform
            gtiff = create_geotiff(data, output_filename, grid_def["proj4_definition"], geotransform,
                                   etype=etype, tiled=tiled, blockxsize=blockxsize, blockysize=blockysize,
                                   **kwargs)

            if rescale_options.get("method") == "linear" and "min_in" in rescale_options and "max_in" in rescale_options:
                LOG.debug("Setting geotiff metadata for linear min/max values")
                gtiff.SetMetadataItem("min_in", str(rescale_options["min_in"]))
                gtiff.SetMetadataItem("max_in", str(rescale_options["max_in"]))
        except StandardError:
            if not self.keep_intermediate and os.path.isfile(output_filename):
                os.remove(output_filename)
            raise

        return output_filename