def test_bounding_box(): with pytest.raises(ValueError): # west > east SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='urn:ogc:def:crs:EPSG::152160', west=1784000, south=1356000, east=-1140000, north=1863000, output='test.tif') with pytest.raises(ValueError): # south>north SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='urn:ogc:def:crs:EPSG::152160', west=-1784000, south=1356000, east=-1140000, north=-1863000, output='test.tif')
def main(service_id, coverage_id, crs, bbox, resx, resy, width, height, response_crs, output): west, south, east, north = list(map(float, bbox.split(','))) SoilGrids().get_coverage_data(service_id=service_id, coverage_id=coverage_id, crs=crs, west=west, south=south, east=east, north=north, output=output, resx=resx, resy=resy, width=width, height=height, response_crs=response_crs) if os.path.isfile(output): print('Done')
def test_data_download(tmpdir): data = SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='urn:ogc:def:crs:EPSG::152160', west=-1784000, south=1356000, east=-1140000, north=1863000, output=os.path.join(tmpdir, 'test.tif')) assert isinstance(data, xarray.core.dataarray.DataArray) assert len(os.listdir(tmpdir)) == 1 data2 = SoilGrids().get_coverage_data('phh2o', '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=os.path.join(tmpdir, 'test2.tif')) assert isinstance(data2, xarray.core.dataarray.DataArray) assert len(os.listdir(tmpdir)) == 2 data3 = SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='urn:ogc:def:crs:EPSG::4326', west=-105.38, south=39.45, east=-104.5, north=40.07, response_crs='urn:ogc:def:crs:EPSG::152160', width=316, height=275, output=os.path.join(tmpdir, 'test3.tif')) assert isinstance(data3, xarray.core.dataarray.DataArray) assert len(os.listdir(tmpdir)) == 3
def test_get_coverage_list(): with pytest.raises(ValueError): SoilGrids().get_coverage_list('wrong_service_id')
def test_response_crs(): with pytest.raises(ValueError): SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='urn:ogc:def:crs:EPSG::4326', west=-1784000, south=1356000, east=-1140000, north=1863000, response_crs='error', output='test.tif')
def test_width_height(): with pytest.raises(ValueError): SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='urn:ogc:def:crs:EPSG::4326', west=-105.38, south=39.45, east=-104.5, north=40.07, output='test.tif')
def test_output(): with pytest.raises(ValueError): SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='urn:ogc:def:crs:EPSG::152160', west=-1784000, south=1356000, east=-1140000, north=1863000, output='wrong file')
def test_crs(): with pytest.raises(ValueError): SoilGrids().get_coverage_data('phh2o', 'phh2o_0-5cm_mean', crs='wrong code', west=-1784000, south=1356000, east=-1140000, north=1863000, output='test.tif')
def test_coverage_id(): with pytest.raises(ValueError): SoilGrids().get_coverage_data('phh2o', 'wrong id', crs='urn:ogc:def:crs:EPSG::152160', west=-1784000, south=1356000, east=-1140000, north=1863000, output='test.tif')
def test_get_coverage_info(): with pytest.raises(ValueError): SoilGrids().get_coverage_info('wrong_service_id', 'wrong_coverage_id') with pytest.raises(ValueError): SoilGrids().get_coverage_info('bdod', 'wrong_coverage_id')
def initialize(self, config_file: str) -> None: """Perform startup tasks for the model. Perform all tasks that take place before entering the model's time loop, including opening files and initializing the model state. Model inputs are read from a text-based configuration file, specified by `filename`. Parameters ---------- config_file : str, optional The path to the model configuration file. Notes ----- Models should be refactored, if necessary, to use a configuration file. CSDMS does not impose any constraint on how configuration files are formatted, although YAML is recommended. A template of a model's configuration file with placeholder values is used by the BMI. """ if config_file: with open(config_file, "r") as fp: conf = yaml.safe_load(fp).get('bmi-soilgrids', {}) else: 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']]) array = self._dataset[0].values self._grid = { 0: BmiGridUniformRectilinear( shape=[int(dim) for dim in array.shape], yx_spacing=( self._dataset.attrs['res'][1], self._dataset.attrs['res'][0]), # original res is (x,y) yx_of_lower_left=(self._dataset.coords['y'].values[-1], self._dataset.coords['x'].values[0]), ), } self._var[self._output_var_names[0]] = BmiVar( dtype=str(array.dtype), itemsize=array.itemsize, nbytes=array.nbytes, # nbytes for current time step value units=soilgrids.metadata[ "variable_units"], # TODO: translate var name into CSDMS standard name location= "node", # scalar value has no location on a grid (node, face, edge) grid=0, # grid id number )