def create_geotiff(dt_aggregate, code='5min'): pathhelper = utils.PathHelper(basedir=config.AGGREGATE_DIR, code=code, template='{code}_{timestamp}.h5') rasterlayerkwargs = utils.rain_kwargs( name='jet', max_rain=2, threshold=0.008, ) aggregatepath = pathhelper.path(dt_aggregate) tifpath_rd = os.path.join(config.IMG_DIR, 'geotiff', 'rd', dt_aggregate.strftime('%Y-%m-%d-%H-%M.tiff')) with h5py.File(aggregatepath, 'r') as h5: array = h5['precipitation'] mask = np.equal(array, h5.attrs['fill_value']) masked_array = np.ma.array(array, mask=mask) # Create the rd tiff. utils.makedir(os.path.dirname(tifpath_rd)) gridtools.RasterLayer(array=masked_array, extent=h5.attrs['grid_extent'], projection=h5.attrs['grid_projection'], **rasterlayerkwargs).save(tifpath_rd, rgba=True) logging.info('saved {}.'.format(os.path.basename(tifpath_rd))) logging.debug('saved {}.'.format(tifpath_rd))
def create_geotiff(dt_aggregate, code='5min'): pathhelper = utils.PathHelper( basedir=config.AGGREGATE_DIR, code=code, template='{code}_{timestamp}.h5' ) rasterlayerkwargs = utils.rain_kwargs( name='jet', max_rain=2, threshold=0.008, ) aggregatepath = pathhelper.path(dt_aggregate) tifpath_rd = os.path.join( config.IMG_DIR, 'geotiff', 'rd', dt_aggregate.strftime('%Y-%m-%d-%H-%M.tiff') ) with h5py.File(aggregatepath, 'r') as h5: array = h5['precipitation'] mask = np.equal(array, h5.attrs['fill_value']) masked_array = np.ma.array(array, mask=mask) # Create the rd tiff. utils.makedir(os.path.dirname(tifpath_rd)) gridtools.RasterLayer(array=masked_array, extent=h5.attrs['grid_extent'], projection=h5.attrs['grid_projection'], **rasterlayerkwargs).save(tifpath_rd, rgba=True) logging.info('saved {}.'.format(os.path.basename(tifpath_rd))) logging.debug('saved {}.'.format(tifpath_rd))
def data_image(masked_array, max_rain=20, threshold=0.0): """ """ basegrid = scans.BASEGRID rasterlayerkwargs = utils.rain_kwargs( name='jet', max_rain=max_rain, threshold=threshold, ) return gridtools.RasterLayer( array=masked_array, extent=basegrid.extent, projection=basegrid.projection, **rasterlayerkwargs).image()
def data_image(masked_array, max_rain=20, threshold=0.0): """ """ basegrid = scans.BASEGRID rasterlayerkwargs = utils.rain_kwargs( name='jet', max_rain=max_rain, threshold=threshold, ) return gridtools.RasterLayer(array=masked_array, extent=basegrid.extent, projection=basegrid.projection, **rasterlayerkwargs).image()
def _aggregate_radar(self, aggregatedatetime, method): template = "aggregate.m{method}_%Y%m%d%H%M%S.{extension}" path = os.path.join( config.AGGREGATE_DIR, aggregatedatetime.strftime(template).format(method=method, extension="tif") ) if os.path.exists(path): logging.debug("We have this one in cache: {}".format(os.path.basename(path))) return gdal.Open(path) logging.info("doing {}".format(aggregatedatetime)) phkwargs = dict(basedir=config.COMPOSITE_DIR, template="{code}_{timestamp}.tif") ph = utils.PathHelper(code=self.CODES[method], **phkwargs) datetime_start = aggregatedatetime - datetime.timedelta(days=1) datetime_stop = aggregatedatetime - datetime.timedelta(minutes=5) text = "{}-{}".format(datetime_start.strftime("%Y%m%d%H%M"), datetime_stop.strftime("%Y%m%d%H%M")) scandatetimes = utils.DateRange(text).iterdatetimes() dataset = gdal.GetDriverByName(b"mem").CreateCopy(b"", gdal.Open(ph.path(datetime.datetime(2011, 1, 1)))) rain = np.ma.zeros(gridtools.BaseGrid(dataset).get_shape()) count = np.zeros(gridtools.BaseGrid(dataset).get_shape()) for scandatetime in scandatetimes: logging.debug("adding {}".format(scandatetime)) composite = gdal.Open(ph.path(scandatetime)) if composite is None: logging.warn("No composite found for method {} at {}".format(method, scandatetime)) continue ma = gridtools.ds2ma(composite) count += ~ma.mask # Where no mask, we count rain rain += ma.filled(0) rain /= 12 # Composite unit is mm/hr, but we add every 5 minutes. rain.mask = np.less(count, 1) dataset.GetRasterBand(1).WriteArray(rain.filled(config.NODATAVALUE)) gdal.GetDriverByName(b"GTiff").CreateCopy(path, dataset, 0, ["COMPRESS=DEFLATE"]) gridtools.RasterLayer(dataset, **utils.rain_kwargs(name="jet")).save(path.replace(".tif", ".png")) # Adding the counts as tif count_dataset = gdal.GetDriverByName(b"gtiff").Create( path.replace(".tif", "_count.tif"), dataset.RasterXSize, dataset.RasterYSize, 1, gdalconst.GDT_UInt16 ) count_dataset.GetRasterBand(1).WriteArray(count) return dataset
def _aggregate_radar(self, aggregatedatetime, method): template = 'aggregate.m{method}_%Y%m%d%H%M%S.{extension}' path = os.path.join( config.AGGREGATE_DIR, aggregatedatetime.strftime(template).format( method=method, extension='tif', ), ) if os.path.exists(path): logging.debug('We have this one in cache: {}'.format( os.path.basename(path), )) return gdal.Open(path) logging.info('doing {}'.format(aggregatedatetime)) phkwargs = dict( basedir=config.COMPOSITE_DIR, template='{code}_{timestamp}.tif', ) ph = utils.PathHelper(code=self.CODES[method], **phkwargs) datetime_start = aggregatedatetime - datetime.timedelta(days=1) datetime_stop = aggregatedatetime - datetime.timedelta(minutes=5) text = '{}-{}'.format( datetime_start.strftime('%Y%m%d%H%M'), datetime_stop.strftime('%Y%m%d%H%M'), ) scandatetimes = utils.DateRange(text).iterdatetimes() dataset = gdal.GetDriverByName(b'mem').CreateCopy( b'', gdal.Open(ph.path(datetime.datetime(2011, 1, 1), )), ) rain = np.ma.zeros(gridtools.BaseGrid(dataset).get_shape()) count = np.zeros(gridtools.BaseGrid(dataset).get_shape()) for scandatetime in scandatetimes: logging.debug('adding {}'.format(scandatetime)) composite = gdal.Open(ph.path(scandatetime)) if composite is None: logging.warn('No composite found for method {} at {}'.format( method, scandatetime, )) continue ma = gridtools.ds2ma(composite) count += ~ma.mask # Where no mask, we count rain rain += ma.filled(0) rain /= 12 # Composite unit is mm/hr, but we add every 5 minutes. rain.mask = np.less(count, 1) dataset.GetRasterBand(1).WriteArray(rain.filled(config.NODATAVALUE)) gdal.GetDriverByName(b'GTiff').CreateCopy(path, dataset, 0, ['COMPRESS=DEFLATE']) gridtools.RasterLayer(dataset, **utils.rain_kwargs(name='jet')).save( path.replace('.tif', '.png'), ) # Adding the counts as tif count_dataset = gdal.GetDriverByName(b'gtiff').Create( path.replace('.tif', '_count.tif'), dataset.RasterXSize, dataset.RasterYSize, 1, gdalconst.GDT_UInt16, ) count_dataset.GetRasterBand(1).WriteArray(count) return dataset