def test_resample_grid(tgrid): """ Test resampling grid """ new_mask_grid = os.path.join(tgrid.input, 'v_mask.tif') era_grid = os.path.join(tgrid.input, 'era_raw.tif') resampled_grid = os.path.join(tgrid.write, 'resampled.tif') resample_grid(original_grid=era_grid, match_grid=new_mask_grid, to_file=resampled_grid) compare_resampled_grid = os.path.join(tgrid.write, 'resampled.tif') compare_files(resampled_grid, compare_resampled_grid, raster=True)
def resample(self, variable, match_grid): """Resample data to grid. Parameters ---------- variable: :obj:`str` Name of variable in dataset. match_grid: :func:`gdal.Dataset` or :func:`sloot.grid.GDALGrid` Grid you want the data resampled to match resolution. You can also pass the path to the grid. """ new_data = [] for band in range(self._obj.dims[self.time_dim]): data = self._obj[variable][band].values arr_grid = ArrayGrid(in_array=data, wkt_projection=self.projection.ExportToWkt(), geotransform=self.geotransform) resampled_data_grid = resample_grid(original_grid=arr_grid, match_grid=match_grid, as_gdal_grid=True) new_data.append(resampled_data_grid.np_array()) self.to_datetime() return self._export_dataset(variable, np.array(new_data), resampled_data_grid)
def addRoughnessMapFromLandUse(self, name, session, land_use_grid, land_use_to_roughness_table=None, land_use_grid_id=None, ): ''' Adds a roughness map from land use file Example:: from gsshapy.orm import ProjectFile from gsshapy.lib import db_tools as dbt from os import path, chdir gssha_directory = '/gsshapy/tests/grid_standard/gssha_project' land_use_grid = 'LC_5min_global_2012.tif' land_use_to_roughness_table = ''/gsshapy/gridtogssha/land_cover/land_cover_glcf_modis.txt' chdir(gssha_directory) # Create Test DB sqlalchemy_url, sql_engine = dbt.init_sqlite_memory() # Create DB Sessions db_session = dbt.create_session(sqlalchemy_url, sql_engine) # Instantiate GSSHAPY object for reading to database project_manager = ProjectFile() # Call read method project_manager.readInput(directory=gssha_directory, projectFileName='grid_standard.prj', session=db_session) project_manager.mapTableFile.addRoughnessMapFromLandUse("roughness", db_session, land_use_to_roughness_table, land_use_grid, ) # WRITE OUT UPDATED GSSHA PROJECT FILE project_manager.writeInput(session=db_session, directory=gssha_directory, name='grid_standard') ''' LAND_USE_GRID_TABLES = { 'nga' : 'land_cover_nga.txt', 'glcf' : 'land_cover_glcf_modis.txt', 'nlcd' : 'land_cover_nlcd.txt', } # read in table if isinstance(land_use_to_roughness_table, pd.DataFrame): df = land_use_to_roughness_table else: if land_use_to_roughness_table is None: if land_use_grid_id is None: raise ValueError("Must have land_use_to_roughness_table or land_use_grid_id set ...") land_use_to_roughness_table = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'grid', 'land_cover', LAND_USE_GRID_TABLES[land_use_grid_id]) df = pd.read_table(land_use_to_roughness_table, delim_whitespace=True, header=None, skiprows=1, names=('id', 'description', 'roughness'), dtype={'id':'int', 'description':'str', 'roughness':'float'}, ) # resample land use grid to gssha grid land_use_resampled = resample_grid(land_use_grid, self.projectFile.getGrid(), resample_method=gdalconst.GRA_NearestNeighbour, as_gdal_grid=True) unique_land_use_ids = np.unique(land_use_resampled.np_array()) #only add ids in index map subset df = df[df.id.isin(unique_land_use_ids)] # make sure all needed land use IDs exist for land_use_id in unique_land_use_ids: if land_use_id not in df.id.values: raise IndexError("Land use ID {0} not found in table.".format(land_use_id)) # delete duplicate/old tables with same name if they exist self.deleteMapTable("ROUGHNESS", session) # get num ids mapTable = MapTable(name="ROUGHNESS", numIDs=len(df.index), maxNumCells=0, numSed=0, numContam=0) # Create GSSHAPY IndexMap object from result object indexMap = IndexMap(name=name) indexMap.mapTableFile = self mapTable.indexMap = indexMap # Associate MapTable with this MapTableFile and IndexMaps mapTable.mapTableFile = self # add values to table for row in df.itertuples(): idx = MTIndex(str(row.id), row.description, '') idx.indexMap = indexMap val = MTValue('ROUGH', row.roughness) val.index = idx val.mapTable = mapTable project_path = "" proj_path_card = self.projectFile.getCard('PROJECT_PATH') if proj_path_card: project_path = proj_path_card.value.strip('"').strip("'") # remove MANNING_N card becasue it is mutually exclusive manningn_card = self.projectFile.getCard('MANNING_N') if manningn_card: session.delete(manningn_card) session.commit() # add path to file mapTable.indexMap.filename = os.path.join(project_path, '{0}.idx'.format(name)) # write file land_use_resampled.to_grass_ascii(mapTable.indexMap.filename, print_nodata=False) # update project card if not self.projectFile.getCard('MAPPING_TABLE'): self.projectFile.setCard('MAPPING_TABLE', '{0}.cmt'.format(self.projectFile.name), add_quotes=True)
def generateFromRaster(self, elevation_raster, shapefile_path=None, out_elevation_grid=None, resample_method=gdalconst.GRA_Average, load_raster_to_db=True): """ Generates an elevation grid for the GSSHA simulation from an elevation raster Example:: from gsshapy.orm import ProjectFile, ElevationGridFile from gsshapy.lib import db_tools as dbt gssha_directory = '/gsshapy/tests/grid_standard/gssha_project' elevation_raster = 'elevation.tif' project_manager, db_sessionmaker = \ dbt.get_project_session('grid_standard', gssha_directory) db_session = db_sessionmaker() # read project file project_manager.readInput(directory=gssha_directory, projectFileName='grid_standard.prj', session=db_session) # generate elevation grid elevation_grid = ElevationGridFile(session=db_session, project_file=project_manager) elevation_grid.generateFromRaster(elevation_raster) # write out updated parameters project_manager.writeInput(session=db_session, directory=gssha_directory, name='grid_standard') """ if not self.projectFile: raise ValueError("Must be connected to project file ...") # make sure paths are absolute as the working directory changes elevation_raster = os.path.abspath(elevation_raster) shapefile_path = os.path.abspath(shapefile_path) # must match elevation mask grid mask_grid = self.projectFile.getGrid() if out_elevation_grid is None: out_elevation_grid = '{0}.{1}'.format(self.projectFile.name, self.fileExtension) elevation_grid = resample_grid(elevation_raster, mask_grid, resample_method=resample_method, as_gdal_grid=True) with tmp_chdir(self.projectFile.project_directory): elevation_grid.to_grass_ascii(out_elevation_grid, print_nodata=False) # read raster into object if load_raster_to_db: self._load_raster_text(out_elevation_grid) self.filename = out_elevation_grid self.projectFile.setCard("ELEVATION", out_elevation_grid, add_quotes=True) # find outlet and add slope self.projectFile.findOutlet(shapefile_path)
def generateFromRaster(self, elevation_raster, shapefile_path=None, out_elevation_grid=None, resample_method=gdalconst.GRA_Average): ''' Generates an elevation grid for the GSSHA simulation from an elevation raster Example:: from gsshapy.orm import ProjectFile, ElevationGridFile from gsshapy.lib import db_tools as dbt from os import path, chdir gssha_directory = '/gsshapy/tests/grid_standard/gssha_project' elevation_raster = 'elevation.tif' # Create Test DB sqlalchemy_url, sql_engine = dbt.init_sqlite_memory() # Create DB Sessions db_session = dbt.create_session(sqlalchemy_url, sql_engine) # Instantiate GSSHAPY object for reading to database project_manager = ProjectFile() # read project file project_manager.readInput(directory=gssha_directory, projectFileName='grid_standard.prj', session=db_session) # generate elevation grid elevation_grid = ElevationGridFile(session=db_session, project_file=project_manager) elevation_grid.generateFromRaster(elevation_raster) # write out updated parameters project_manager.writeInput(session=db_session, directory=gssha_directory, name='grid_standard') ''' if not self.projectFile: raise ValueError("Must be connected to project file ...") # must match elevation mask grid mask_grid = self.projectFile.getGrid() if out_elevation_grid is None: project_path = '' project_path_card = self.projectFile.getCard('PROJECT_PATH') if project_path_card: project_path = project_path_card.value.strip('"').strip("'") out_elevation_grid = os.path.join( project_path, '{0}.{1}'.format(self.projectFile.name, self.fileExtension), ) elevation_grid = resample_grid(elevation_raster, mask_grid, resample_method=resample_method, as_gdal_grid=True) elevation_grid.to_grass_ascii(out_elevation_grid, print_nodata=False) self.filename = out_elevation_grid self.projectFile.setCard("ELEVATION", out_elevation_grid, add_quotes=True) # read raster into object self._load_raster_text(out_elevation_grid) # find outlet and add slope self.projectFile.findOutlet(shapefile_path)
def addRoughnessMapFromLandUse(self, name, session, land_use_grid, land_use_to_roughness_table=None, land_use_grid_id=None, ): """ Adds a roughness map from land use file Example:: from gsshapy.orm import ProjectFile from gsshapy.lib import db_tools as dbt from os import path, chdir gssha_directory = '/gsshapy/tests/grid_standard/gssha_project' land_use_grid = 'LC_5min_global_2012.tif' land_use_to_roughness_table = ''/gsshapy/gridtogssha/land_cover/land_cover_glcf_modis.txt' # Create Test DB sqlalchemy_url, sql_engine = dbt.init_sqlite_memory() # Create DB Sessions db_session = dbt.create_session(sqlalchemy_url, sql_engine) # Instantiate GSSHAPY object for reading to database project_manager = ProjectFile() # Call read method project_manager.readInput(directory=gssha_directory, projectFileName='grid_standard.prj', session=db_session) project_manager.mapTableFile.addRoughnessMapFromLandUse("roughness", db_session, land_use_to_roughness_table, land_use_grid, ) # WRITE OUT UPDATED GSSHA PROJECT FILE project_manager.writeInput(session=db_session, directory=gssha_directory, name='grid_standard') """ LAND_USE_GRID_TABLES = { 'nga' : 'land_cover_nga.txt', 'glcf' : 'land_cover_glcf_modis.txt', 'nlcd' : 'land_cover_nlcd.txt', } # read in table if isinstance(land_use_to_roughness_table, pd.DataFrame): df = land_use_to_roughness_table else: if land_use_to_roughness_table is None: if land_use_grid_id is None: raise ValueError("Must have land_use_to_roughness_table or land_use_grid_id set ...") land_use_to_roughness_table = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'grid', 'land_cover', LAND_USE_GRID_TABLES[land_use_grid_id]) # make sure paths are absolute as the working directory changes land_use_to_roughness_table = os.path.abspath(land_use_to_roughness_table) df = pd.read_table(land_use_to_roughness_table, delim_whitespace=True, header=None, skiprows=1, names=('id', 'description', 'roughness'), dtype={'id':'int', 'description':'str', 'roughness':'float'}, ) # make sure paths are absolute as the working directory changes land_use_grid = os.path.abspath(land_use_grid) # resample land use grid to gssha grid land_use_resampled = resample_grid(land_use_grid, self.projectFile.getGrid(), resample_method=gdalconst.GRA_NearestNeighbour, as_gdal_grid=True) unique_land_use_ids = np.unique(land_use_resampled.np_array()) # only add ids in index map subset df = df[df.id.isin(unique_land_use_ids)] # make sure all needed land use IDs exist for land_use_id in unique_land_use_ids: if land_use_id not in df.id.values: raise IndexError("Land use ID {0} not found in table.".format(land_use_id)) # delete duplicate/old tables with same name if they exist self.deleteMapTable("ROUGHNESS", session) # get num ids mapTable = MapTable(name="ROUGHNESS", numIDs=len(df.index), maxNumCells=0, numSed=0, numContam=0) # Create GSSHAPY IndexMap object from result object indexMap = IndexMap(name=name) indexMap.mapTableFile = self mapTable.indexMap = indexMap # Associate MapTable with this MapTableFile and IndexMaps mapTable.mapTableFile = self # add values to table for row in df.itertuples(): idx = MTIndex(str(row.id), row.description, '') idx.indexMap = indexMap val = MTValue('ROUGH', row.roughness) val.index = idx val.mapTable = mapTable # remove MANNING_N card becasue it is mutually exclusive manningn_card = self.projectFile.getCard('MANNING_N') if manningn_card: session.delete(manningn_card) session.commit() mapTable.indexMap.filename = '{0}.idx'.format(name) # write file with tmp_chdir(self.projectFile.project_directory): land_use_resampled.to_grass_ascii(mapTable.indexMap.filename, print_nodata=False) # update project card if not self.projectFile.getCard('MAPPING_TABLE'): self.projectFile.setCard('MAPPING_TABLE', '{0}.cmt'.format(self.projectFile.name), add_quotes=True)
def generateFromRaster(self, elevation_raster, shapefile_path=None, out_elevation_grid=None, resample_method=gdalconst.GRA_Average, load_raster_to_db=True): """ Generates an elevation grid for the GSSHA simulation from an elevation raster Example:: from gsshapy.orm import ProjectFile, ElevationGridFile from gsshapy.lib import db_tools as dbt gssha_directory = '/gsshapy/tests/grid_standard/gssha_project' elevation_raster = 'elevation.tif' project_manager, db_sessionmaker = \ dbt.get_project_session('grid_standard', gssha_directory) db_session = db_sessionmaker() # read project file project_manager.readInput(directory=gssha_directory, projectFileName='grid_standard.prj', session=db_session) # generate elevation grid elevation_grid = ElevationGridFile(session=db_session, project_file=project_manager) elevation_grid.generateFromRaster(elevation_raster) # write out updated parameters project_manager.writeInput(session=db_session, directory=gssha_directory, name='grid_standard') """ if not self.projectFile: raise ValueError("Must be connected to project file ...") # make sure paths are absolute as the working directory changes elevation_raster = os.path.abspath(elevation_raster) shapefile_path = os.path.abspath(shapefile_path) # must match elevation mask grid mask_grid = self.projectFile.getGrid() if out_elevation_grid is None: out_elevation_grid = '{0}.{1}'.format(self.projectFile.name, self.fileExtension) elevation_grid = resample_grid(elevation_raster, mask_grid, resample_method=resample_method, as_gdal_grid=True) with tmp_chdir(self.projectFile.project_directory): elevation_grid.to_grass_ascii(out_elevation_grid, print_nodata=False) # read raster into object if load_raster_to_db: self._load_raster_text(out_elevation_grid) self.filename = out_elevation_grid self.projectFile.setCard("ELEVATION", out_elevation_grid, add_quotes=True) # find outlet and add slope self.projectFile.findOutlet(shapefile_path)