def get_grid(self, mapdir=None): if not hasattr(self, 'grid') or (self.grid is None): if mapdir is None: mapdir = join(dirname(self._config_fn), 'staticmaps') # ldd is used for coupling to routing / flood model _ldd_fn = join(mapdir, 'wflow_ldd.map') if not isfile(_ldd_fn): raise IOError('ldd file not found') # landmask used for masking out coordinates outside mask _lm_fn = join(mapdir, 'wflow_subcatch.map') if not isfile(_lm_fn): raise IOError('subcatch file not found') self.logger.info('Getting rgrid info based on {}'.format( basename(_lm_fn))) with rasterio.open(_lm_fn, 'r') as ds: # TODO: change transform instead of flipping the grid every time .. self.grid = RGrid(ds.transform, ds.height, ds.width, crs=ds.crs, mask=ds.read(1) >= 1) # river is used for the 1D cells _riv_fn = join(mapdir, 'wflow_river.map') if isfile(_riv_fn): with rasterio.open(_riv_fn, 'r') as ds: row, col = np.where(ds.read(1) == 1) x, y = self.grid.xy(row=row, col=col) inds = self.grid.ravel_multi_index(row, col) self.grid.set_1d(nodes=np.array(zip(x, y)), links=None, inds=inds) # read file with pcr readmap self.logger.info('Getting drainage direction from {}'.format( basename(_ldd_fn))) self.grid.set_dd(_ldd_fn, ddtype='ldd') return self.grid
def get_grid(self): """Retreving spatial information about model domain from PCR-GLOBWB landmask file using rasterio operations; also retrieving PCR-GLOBWB drainage direction (LDD) information Raises: IOError -- function requires LDD file; if not found, IOerror is raised IOError -- function requires landsmask file; if not found, IOerror is raised Returns: grid -- rasterio grid object """ if not hasattr(self, 'grid') or (self.grid is None): # ldd is used for coupling to routing / flood model _indir = abspath( self.get_attribute_value('globalOptions:inputDir')) _ldd_fn = glib.getabspath( self.get_attribute_value('routingOptions:lddMap'), _indir) if not isfile(_ldd_fn): raise IOError('ldd file not found {}'.format(_ldd_fn)) # landmask used for masking out coordinates outside mask if provided _lm_fn = glib.getabspath( self.get_attribute_value('globalOptions:landmask'), _indir) if isfile(_lm_fn): with rasterio.open(_lm_fn, 'r') as ds: mask = ds.read(1) == 1 mask_name = basename(_lm_fn) # use ldd for masking else: mask = None mask_name = basename(_ldd_fn) self.logger.info( 'Getting rgrid info based on {}; mask based on {}'.format( basename(_ldd_fn), mask_name)) with rasterio.open(_ldd_fn, 'r') as ds: if mask is None: mask = ds.read(1) != ds.nodata self.grid = RGrid(ds.transform, ds.height, ds.width, crs=ds.crs, mask=mask) # read file with pcr readmap self.logger.info('Getting drainage direction from {}'.format( basename(_ldd_fn))) self.grid.set_dd(_ldd_fn, ddtype='ldd') self.grid.get_pits() return self.grid
def get_grid(self): if not hasattr(self, 'grid') or (self.grid is None): # dem file used for rgrid and mask of 2D domain _dem_fn = glib.getabspath(str(self.get_attribute_value('DEMfile')), self._mapdir) if not isfile(_dem_fn): raise IOError('DEMfile file not found') self.logger.info('Getting rgrid info based on {}'.format( basename(_dem_fn))) with rasterio.open(_dem_fn, 'r') as ds: self.grid = RGrid(ds.transform, ds.height, ds.width, crs=ds.crs, mask=ds.read(1) != ds.nodata) # riv width file used for "1D coords" _width_fn = glib.getabspath( str(self.get_attribute_value('SGCwidth')), self._mapdir) if not isfile(_width_fn): raise IOError('SGCwidth file not found') with rasterio.open(_width_fn, 'r') as ds: row, col = np.where(ds.read(1) > 0) x, y = self.grid.xy(row=row, col=col) inds = self.grid.ravel_multi_index(row, col) self.grid.set_1d(nodes=np.array(zip(x, y)), links=None, inds=inds) return self.grid