Пример #1
0
 def _surface_metadata(sconfig):
     metadata = {}
     if "x_resolution" in sconfig:
         metadata["x_resolution"] = float(sconfig["x_resolution"])
     else:
         metadata["x_coordinates"] = tuple(map(float, config.string_to_list(sconfig["x_coordinates"])))
     if "y_resolution" in sconfig:
         metadata["y_resolution"] = float(sconfig["y_resolution"])
     else:
         metadata["y_coordinates"] = tuple(map(float, config.string_to_list(sconfig["y_coordinates"])))
     return metadata
Пример #2
0
    def __init__(self, name, model_metadata, config):
        """Constructor.

        Args:
            name (str)
                Name of block.
            model_metadata (ModelMetadata)
                Metadata for model domain containing the block.
            config (dict)
                Block parameters as dictionary.
                Keys:
                    - x_resolution: Resolution in x-direction (m) if uniform resolution in x-direction.
                    - x_coordinates: Array of x coordinates (m) if variable resolution in x-direction.
                    - y_resolution: Resolution in y-direction (m) if uniform resolution in y-direction.
                    - y_coordinates: Array of y coordinates (m) if variable resolution in y-direction.
                    - z_resolution: Resolution in z-direction (m) if uniform resolution in z-direction.
                    - z_top: Elevation of top of block (m) if uniform resolution in z-direction.
                    - z_bot: Elevation of bottom of block (m) if uniform resolution in z-direction.
                    - z_coordinates: Array of z coordinates (m) if variable resolution in z-direction.
                    - z_top_offset: Vertical offset of top set of points below top of block (m) (used to avoid roundoff errors).
                    - chunk_size: Dimensions of dataset chunk (should be about 10Kb - 1Mb)
        """
        self.name = name
        self.model_metadata = model_metadata

        if "x_resolution" in config:
            self.x_resolution = float(config["x_resolution"])
            self.x_coordinates = None
        else:
            self.x_resolution = None
            self.x_coordinates = tuple(
                map(float, string_to_list(config["x_coordinates"])))
        if "y_resolution" in config:
            self.y_resolution = float(config["y_resolution"])
            self.y_coordinates = None
        else:
            self.y_resolution = None
            self.y_coordinates = tuple(
                map(float, string_to_list(config["y_coordinates"])))
        if "z_resolution" in config:
            self.z_resolution = float(config["z_resolution"])
            self.z_coordinates = None
            self.z_top = float(config["z_top"])
            self.z_bot = float(config["z_bot"])
        else:
            self.z_resolution = None
            self.z_coordinates = tuple(
                map(float, string_to_list(config["z_coordinates"])))
            self.z_top = numpy.max(self.z_coordinates)
            self.z_bot = numpy.min(self.z_coordinates)

        self.z_top_offset = float(config["z_top_offset"])
        self.chunk_size = tuple(map(int, string_to_list(config["chunk_size"])))
Пример #3
0
    def _initialize(self, config):
        """Setup model.

        Args:
            config (dict)
                Model configuration.
        """
        self.metadata = ModelMetadata(config)

        for name in string_to_list(config["domain"]["blocks"]):
            block = Block(name, self.metadata, config[name])
            self.blocks.append(block)

        self.storage = HDF5Storage(config["geomodelgrids"]["filename"])
        if "top_surface" in config:
            self.top_surface = Surface("top_surface", self.metadata,
                                       config["top_surface"], self.storage)
        else:
            self.top_surface = None
        if "topography_bathymetry" in config:
            self.topo_bathy = Surface("topography_bathymetry", self.metadata,
                                      config["topography_bathymetry"],
                                      self.storage)
        else:
            self.topo_bathy = None
Пример #4
0
 def _block_metadata(bconfig):
     metadata = {}
     if "x_resolution" in bconfig:
         metadata["x_resolution"] = float(bconfig["x_resolution"])
     else:
         metadata["x_coordinates"] = tuple(map(float, config.string_to_list(bconfig["x_coordinates"])))
     if "y_resolution" in bconfig:
         metadata["y_resolution"] = float(bconfig["y_resolution"])
     else:
         metadata["y_coordinates"] = tuple(map(float, config.string_to_list(bconfig["y_coordinates"])))
     if "z_resolution" in bconfig:
         metadata["z_resolution"] = float(bconfig["z_resolution"])
         metadata["z_top"] = float(bconfig["z_top"])
         metadata["z_bot"] = float(bconfig["z_bot"])
     else:
         metadata["z_coordinates"] = tuple(map(float, config.string_to_list(bconfig["z_coordinates"])))
         metadata["z_top"] = numpy.max(metadata["z_coordinates"])
     return metadata
Пример #5
0
    def __init__(self, name, model_metadata, config, storage):
        """Constructor.

        Args:
            name (str)
                Name of surface.
            model_metadata (ModelMetadata)
                Metadata for model domain associated with block.
            config (dict)
                Keys:
                    - x_resolution: Resolution in x-direction (m) if uniform resolution in x-direction.
                    - x_coordinates: Array of x coordinates (m) if variable resolution in x-direction.
                    - y_resolution: Resolution in y-direction (m) if uniform resolution in y-direction.
                    - y_coordinates: Array of y coordinates (m) if variable resolution in y-direction.
                    - chunk_size: Dimensions of dataset chunk (should be about 10Kb - 1Mb)
            storage (HDF5Storage)
                Storage interface.
        """
        self.name = name
        self.model_metadata = model_metadata
        if "x_resolution" in config:
            self.x_resolution = float(config["x_resolution"])
            self.x_coordinates = None
        else:
            self.x_resolution = None
            self.x_coordinates = tuple(
                map(float, string_to_list(config["x_coordinates"])))
        if "y_resolution" in config:
            self.y_resolution = float(config["y_resolution"])
            self.y_coordinates = None
        else:
            self.y_resolution = None
            self.y_coordinates = tuple(
                map(float, string_to_list(config["y_coordinates"])))

        self.chunk_size = tuple(map(int, string_to_list(config["chunk_size"])))
        self.storage = storage
Пример #6
0
    def get_topography_bathymetry(self, points):
        """Query EarthVision model for elevation of topoggraphy or bathymetry at points.

        Args:
            points (numpy.array [Nx,Ny])
                Numpy array with coordinates of points in model coordinates.
        Returns:
            Numpy array [Nx,Ny] of elevation of topography or bathymetry at points.
        """
        POINTS_FILENAME = "topography_bathymetry_points.dat"  # Must have .dat suffix.
        ELEV_FILENAME = "topography_bathymetry_elev.dat"  # Must have .dat suffix.

        points_abspath = os.path.join(self.model_dir, POINTS_FILENAME)
        elev_abspath = os.path.join(self.model_dir, ELEV_FILENAME)
        scale = units.length_scale(self.config["earthvision"]["xy_units"])

        numpy.savetxt(points_abspath,
                      points.reshape((-1, points.shape[2])) / scale,
                      fmt="%16.8e")

        elev = -1.0e+20 * numpy.ones(points.shape[0:2])
        for grd_filename in string_to_list(
                self.config["earthvision"]["topography_bathymetry_2grd"]):
            formula = "{filename_out}<elev> = bakint({ev2grd}, {filename_in}<x>, {filename_in}<y>);".format(
                filename_in=POINTS_FILENAME,
                filename_out=ELEV_FILENAME,
                ev2grd=grd_filename)
            elev_grd = self.api.ev_fp(formula,
                                      elev_abspath).reshape(points.shape[0:2])
            elev_grd *= units.length_scale(
                self.config["earthvision"]["elev_units"])
            elev = numpy.maximum(elev_grd, elev)

        os.remove(points_abspath)
        os.remove(elev_abspath)
        return elev.reshape((elev.shape[0], elev.shape[1], 1))
Пример #7
0
    def __init__(self, config):
        """Constructor.

        Args:
            config (dict)
                Dictionary with model configuration.
                Keys:
                    - title: Title of model.
                    - id: Model id.
                    - description: Description of model.
                    - keywords: List of keywords describing model.
                    - history: Description of model creation.
                    - comment: Additional comments about model.
                    - version: Model version number.
                    - creator_name: Name of person creating GeoModelGrids model.
                    - creator_email: Email of creator.
                    - creator_institution: Institution of creator.
                    - acknowledgement: Acknowledgement for model.
                    - authors: List of model authors.
                    - references: List of references for model.
                    - repository_name: Name of repository holding model.
                    - repository_url: URL of repository.
                    - repository_doi: Digital Object Identifier for repository.
                    - license: Name of the license for model.
                    - coordsys
                        - crs
                        - origin_x
                        - origin_y
                        - y_azimuth
                    - data
                        - values
                        - units
                        - layout
                    - domain
                        - dim_x
                        - dim_y
                        - dim_z
        """
        self.title = config["geomodelgrids"]["title"]
        self.id = config["geomodelgrids"]["id"]
        self.description = config["geomodelgrids"]["description"]
        self.keywords = string_to_list(config["geomodelgrids"]["keywords"])
        self.history = config["geomodelgrids"]["history"]
        self.comment = config["geomodelgrids"]["comment"]
        self.version = config["geomodelgrids"]["version"]

        self.creator_name = config["geomodelgrids"]["creator_name"]
        self.creator_email = config["geomodelgrids"]["creator_email"]
        self.creator_institution = config["geomodelgrids"][
            "creator_institution"]
        self.acknowledgement = config["geomodelgrids"]["acknowledgement"]
        self.authors = string_to_list(config["geomodelgrids"]["authors"],
                                      delimiter="|")
        self.references = string_to_list(config["geomodelgrids"]["references"],
                                         delimiter="|")
        self.repository_name = config["geomodelgrids"]["repository_name"]
        self.repository_url = config["geomodelgrids"]["repository_url"]
        self.repository_doi = config["geomodelgrids"]["repository_doi"]
        self.license = config["geomodelgrids"]["license"]

        # Data
        self.data_values = string_to_list(config["data"]["values"])
        self.data_units = string_to_list(config["data"]["units"])
        if config["data"]["layout"] in ["vertex"]:
            self.data_layout = config["data"]["layout"]
        else:
            raise NotImplementedError(
                "Currently only vertex-based data are implemented.")

        # Coordinate system
        self.crs = config["coordsys"]["crs"]
        self.origin_x = float(config["coordsys"]["origin_x"])
        self.origin_y = float(config["coordsys"]["origin_y"])
        self.y_azimuth = float(config["coordsys"]["y_azimuth"])

        # Domain dimensions
        self.dim_x = float(config["domain"]["dim_x"])
        self.dim_y = float(config["domain"]["dim_y"])
        self.dim_z = float(config["domain"]["dim_z"])

        # Auxiliary data
        if "auxiliary" in config["data"]:
            import json
            self.auxiliary = json.loads(config["data"]["auxiliary"])
        else:
            self.auxiliary = {}
Пример #8
0
    def setUp(self):
        def _surface_metadata(sconfig):
            metadata = {}
            if "x_resolution" in sconfig:
                metadata["x_resolution"] = float(sconfig["x_resolution"])
            else:
                metadata["x_coordinates"] = tuple(map(float, config.string_to_list(sconfig["x_coordinates"])))
            if "y_resolution" in sconfig:
                metadata["y_resolution"] = float(sconfig["y_resolution"])
            else:
                metadata["y_coordinates"] = tuple(map(float, config.string_to_list(sconfig["y_coordinates"])))
            return metadata

        def _block_metadata(bconfig):
            metadata = {}
            if "x_resolution" in bconfig:
                metadata["x_resolution"] = float(bconfig["x_resolution"])
            else:
                metadata["x_coordinates"] = tuple(map(float, config.string_to_list(bconfig["x_coordinates"])))
            if "y_resolution" in bconfig:
                metadata["y_resolution"] = float(bconfig["y_resolution"])
            else:
                metadata["y_coordinates"] = tuple(map(float, config.string_to_list(bconfig["y_coordinates"])))
            if "z_resolution" in bconfig:
                metadata["z_resolution"] = float(bconfig["z_resolution"])
                metadata["z_top"] = float(bconfig["z_top"])
                metadata["z_bot"] = float(bconfig["z_bot"])
            else:
                metadata["z_coordinates"] = tuple(map(float, config.string_to_list(bconfig["z_coordinates"])))
                metadata["z_top"] = numpy.max(metadata["z_coordinates"])
            return metadata

        model_config = config.get_config([self.CONFIG_FILENAME])
        auxiliary = {"int_value": 1, "float_value": 2.0, "str_value": "abc"}
        self.metadata = {
            "filename": model_config["geomodelgrids"]["filename"],
            "title": model_config["geomodelgrids"]["title"],
            "id": model_config["geomodelgrids"]["id"],
            "description": model_config["geomodelgrids"]["description"],
            "keywords": config.string_to_list(model_config["geomodelgrids"]["keywords"]),
            "history": model_config["geomodelgrids"]["history"],
            "comment": model_config["geomodelgrids"]["comment"],
            "version": model_config["geomodelgrids"]["version"],
            "creator_name": model_config["geomodelgrids"]["creator_name"],
            "creator_institution": model_config["geomodelgrids"]["creator_institution"],
            "creator_email": model_config["geomodelgrids"]["creator_email"],
            "acknowledgement": model_config["geomodelgrids"]["acknowledgement"],
            "authors": config.string_to_list(model_config["geomodelgrids"]["authors"], delimiter="|"),
            "references": config.string_to_list(model_config["geomodelgrids"]["references"], delimiter="|"),
            "repository_name": model_config["geomodelgrids"]["repository_name"],
            "repository_url": model_config["geomodelgrids"]["repository_url"],
            "repository_doi": model_config["geomodelgrids"]["repository_doi"],
            "license": model_config["geomodelgrids"]["license"],
            "data_values": config.string_to_list(model_config["data"]["values"]),
            "data_units": config.string_to_list(model_config["data"]["units"]),
            "data_layout": model_config["data"]["layout"],
            "crs": model_config["coordsys"]["crs"],
            "origin_x": float(model_config["coordsys"]["origin_x"]),
            "origin_y": float(model_config["coordsys"]["origin_y"]),
            "y_azimuth": float(model_config["coordsys"]["y_azimuth"]),
            "dim_x": float(model_config["domain"]["dim_x"]),
            "dim_y": float(model_config["domain"]["dim_y"]),
            "dim_z": float(model_config["domain"]["dim_z"]),
            "auxiliary": numpy.string_(json.dumps(auxiliary, sort_keys=True)),
            "top_surface": _surface_metadata(model_config["top_surface"]),
            "topography_bathymetry": _surface_metadata(model_config["topography_bathymetry"]),
            "blocks": {
                "top": _block_metadata(model_config["top"]),
                "bottom": _block_metadata(model_config["bottom"]),
            }
        }
Пример #9
0
    def get_values(self, block, top_surface, topo_bathy, batch=None):
        """Get values at points.

        Args:
            block (Block)
                Block information.
            top_surface (Surface)
                Elevation of top surface of model.
            topo_bathy (Surface)
                Elevation of topography or bathymetry used to define depth.
            batch (BatchGenerator3D)
                Current batch of points in block.
        """
        def _varxy_index_fn(coordinates):
            x_grid = numpy.array(coordinates)
            x_grid.sort()
            x_round = -int(numpy.log10(numpy.min(numpy.diff(x_grid))))
            return x_grid.searchsorted(round(x, x_round))

        if batch:
            raise NotImplementedError(
                "Batches with CSV files are not implemented.")

        comment_flag = self.config["csv"].get("comment_flag", "#")
        csv_data = numpy.loadtxt(self.config["csv"]["filename"],
                                 comments=comment_flag)
        model_x, model_y, model_z = self._to_model_xyz(csv_data)

        value_names = string_to_list(self.config["data"]["values"])
        value_columns = [
            int(self.config["csv.columns"][value]) for value in value_names
        ]

        if block.x_resolution:

            def get_xindex(x, block):
                return round(x / block.x_resolution)
        else:

            def get_xindex(x, block):
                return _varxy_index_fn(block.x_coordinates)

        if block.y_resolution:

            def get_yindex(y, block):
                return round(y / block.y_resolution)
        else:

            def get_yindex(y, block):
                return _varxy_index_fn(block.y_coordinates)

        if block.z_resolution:

            def get_zindex(z, block):
                return round(z / block.z_resolution)
        else:
            z_grid = numpy.array(block.z_coordinates)
            z_grid.sort()
            z_round = -int(numpy.log10(numpy.min(numpy.diff(z_grid))))

            def get_zindex(z, block):
                return len(z_grid) - 1 - z_grid.searchsorted(round(z, z_round))

        nx, ny, nz = block.get_dims()
        values = NODATA_VALUE * numpy.ones(
            (nx, ny, nz, len(value_names)), dtype=numpy.float32)
        for row, x, y, z in zip(csv_data, model_x, model_y, model_z):
            ix = get_xindex(x, block)
            iy = get_yindex(y, block)
            iz = get_zindex(z, block)
            row_values = row[value_columns]
            values[ix, iy, iz, :] = row_values
        return values