From 5220db86112b79fd2c6c76598a62621d363222ae Mon Sep 17 00:00:00 2001 From: gantian127 Date: Fri, 5 Mar 2021 17:11:33 -0700 Subject: [PATCH 1/2] update bmi for steady state data: units, end time, update --- soilgrids/bmi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/soilgrids/bmi.py b/soilgrids/bmi.py index d8a4485..e153101 100755 --- a/soilgrids/bmi.py +++ b/soilgrids/bmi.py @@ -58,7 +58,7 @@ def get_end_time(self) -> float: float The maximum model time. """ - return 9999.0 + return 0.0 def get_grid_face_edges(self, grid: int, face_edges: numpy.ndarray) -> numpy.ndarray: """Get the face-edge connectivity. @@ -387,7 +387,7 @@ def get_time_units(self) -> str: ----- CSDMS uses the UDUNITS standard from Unidata. """ - return 'none' + return '1' def get_value(self, name: str, dest: numpy.ndarray) -> numpy.ndarray: """Get a copy of values of the given variable. @@ -644,7 +644,7 @@ def update(self) -> None: then they can be computed by the :func:`initialize` method and this method can return with no action. """ - self._time_index += 1.0 + raise NotImplementedError("update") def update_until(self, time: float) -> None: """Advance model state until the given time. From 1555996c7ea951e52fc957cae0c83d061ebc38fe Mon Sep 17 00:00:00 2001 From: gantian127 Date: Fri, 5 Mar 2021 18:14:34 -0700 Subject: [PATCH 2/2] update bmi functions and config.yaml --- notebooks/config_file.yaml | 17 ++++++++------- notebooks/config_file2.yaml | 21 ++++++++++--------- soilgrids/bmi.py | 42 ++++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/notebooks/config_file.yaml b/notebooks/config_file.yaml index ac27f54..d90593e 100755 --- a/notebooks/config_file.yaml +++ b/notebooks/config_file.yaml @@ -1,8 +1,9 @@ -service_id: phh2o -coverage_id: phh2o_0-5cm_mean -crs: urn:ogc:def:crs:EPSG::152160 -west: -1784000 -south: 1356000 -east: -1140000 -north: 1863000 -output: test.tif +bmi-soilgrids: + service_id: phh2o + coverage_id: phh2o_0-5cm_mean + crs: urn:ogc:def:crs:EPSG::152160 + west: -1784000 + south: 1356000 + east: -1140000 + north: 1863000 + output: test.tif diff --git a/notebooks/config_file2.yaml b/notebooks/config_file2.yaml index 57f1fd6..18cc9e4 100755 --- a/notebooks/config_file2.yaml +++ b/notebooks/config_file2.yaml @@ -1,10 +1,11 @@ -service_id: phh2o -coverage_id: phh2o_0-5cm_mean -crs: urn:ogc:def:crs:EPSG::4326 -west: -105.38 -south: 39.45 -east: -104.5 -north: 40.07 -width: 316 -height: 275 -output: test.tif +bmi-soilgrids: + service_id: phh2o + coverage_id: phh2o_0-5cm_mean + crs: urn:ogc:def:crs:EPSG::4326 + west: -105.38 + south: 39.45 + east: -104.5 + north: 40.07 + width: 316 + height: 275 + output: test2.tif diff --git a/soilgrids/bmi.py b/soilgrids/bmi.py index e153101..a320efe 100755 --- a/soilgrids/bmi.py +++ b/soilgrids/bmi.py @@ -31,7 +31,12 @@ def finalize(self) -> None: loop. This typically includes deallocating memory, closing files and printing reports. """ - self._time_index = 0.0 + self._var = {} + self._grid = {} + self._input_var_names = () + self._output_var_names = () + self._dataset = None + def get_component_name(self) -> str: """Name of the component. @@ -49,7 +54,7 @@ def get_current_time(self) -> float: float The current model time. """ - return self._time_index + return 0.0 def get_end_time(self) -> float: """End time of the model. @@ -200,7 +205,7 @@ def get_grid_rank(self, grid: int) -> int: int Rank of the grid. """ - return 2 + return len(self._grid[grid].shape) def get_grid_shape(self, grid: int, shape: numpy.ndarray) -> numpy.ndarray: """Get dimensions of the computational grid. @@ -276,7 +281,8 @@ def get_grid_x(self, grid: int, x: numpy.ndarray) -> numpy.ndarray: ndarray of float The input numpy array that holds the grid's column x-coordinates. """ - raise NotImplementedError("get_grid_x") + x[:] = self._dataset.x.values + return x def get_grid_y(self, grid: int, y: numpy.ndarray) -> numpy.ndarray: """Get coordinates of grid nodes in the y direction. @@ -291,7 +297,8 @@ def get_grid_y(self, grid: int, y: numpy.ndarray) -> numpy.ndarray: ndarray of float The input numpy array that holds the grid's row y-coordinates. """ - raise NotImplementedError("get_grid_y") + y[:] = self._dataset.y.values + return y def get_grid_z(self, grid: int, z: numpy.ndarray) -> numpy.ndarray: """Get coordinates of grid nodes in the z direction. @@ -334,7 +341,7 @@ def get_input_item_count(self) -> int: int The number of input variables. """ - return 0 + return len(self._input_var_names) def get_output_var_names(self) -> Tuple[str]: """List of a model's output variables. @@ -355,7 +362,7 @@ def get_output_item_count(self) -> int: int The number of output variables. """ - return 1 + return len(self._output_var_names) def get_start_time(self) -> float: """Start time of the model. @@ -375,7 +382,7 @@ def get_time_step(self) -> float: float The time step used in model. """ - return 1.0 + return 0.0 def get_time_units(self) -> str: """Time units of the model. @@ -387,7 +394,7 @@ def get_time_units(self) -> str: ----- CSDMS uses the UDUNITS standard from Unidata. """ - return '1' + return "1" def get_value(self, name: str, dest: numpy.ndarray) -> numpy.ndarray: """Get a copy of values of the given variable. @@ -406,7 +413,7 @@ def get_value(self, name: str, dest: numpy.ndarray) -> numpy.ndarray: The same numpy array that was passed as an input buffer. """ # return all the value at current time step, for scalar it is just one value - dest[:] = self._dataset[0].values.reshape(-1).copy() + dest[:] = self.get_value_ptr(name).reshape(-1).copy() return dest def get_value_at_indices( @@ -427,7 +434,7 @@ def get_value_at_indices( Value of the model variable at the given location. """ # return the value at current time step with given index in 1D or 2D grid. when it is scalar no need for ind - dest[:] = self._dataset[0].values.reshape(-1)[inds] + dest[:] = self.get_value_ptr(name).reshape(-1)[inds] return dest def get_value_ptr(self, name: str) -> numpy.ndarray: @@ -575,17 +582,22 @@ def initialize(self, config_file: str) -> None: """ if config_file: with open(config_file, "r") as fp: - conf = yaml.safe_load(fp) + conf = yaml.safe_load(fp).get('bmi-soilgrids', {}) else: - conf = {} + conf = {'service_id': 'phh2o', + 'coverage_id': 'phh2o_0-5cm_mean', + 'crs': 'urn:ogc:def:crs:EPSG::152160', + 'west': -1784000, + 'south': 1356000, + 'east': -1140000, + 'north': 1863000, + 'output': 'test.tif'} soilgrids = SoilGrids() self._dataset = soilgrids.get_coverage_data(**conf) self._output_var_names = tuple([soilgrids.metadata['variable_name']]) - self._time_index = 0.0 - array = self._dataset[0].values self._grid = { 0: BmiGridUniformRectilinear(