data_workflow/gfsworkflow.py
def resample(wrksppath, region, model):
    """
    Script to resample rasters from .25 o .0025 degree in order for rasterstats to work
    Dependencies: datetime, os, numpy, rasterio
    """
    logging.info('\nResampling the rasters for ' + region)
    # Define app workspace and sub-paths
    tiffs = os.path.join(wrksppath, region, model + '_GeoTIFFs')
    resampleds = os.path.join(wrksppath, region, model + '_GeoTIFFs_resampled')

    # Create directory for the resampled GeoTIFFs
    if not os.path.exists(tiffs):
        logging.info('There is no tiffs folder. You must have already resampled them. Skipping resampling')
        return

    # List all Resampled GeoTIFFs
    files = os.listdir(tiffs)
    files = [tif for tif in files if tif.endswith('.tif')]
    files.sort()

    # Read raster dimensions
    path = os.path.join(tiffs, files[0])
    raster_dim = rasterio.open(path)
    width = raster_dim.width
    height = raster_dim.height
    lon_min = raster_dim.bounds.left
    lon_max = raster_dim.bounds.right
    lat_min = raster_dim.bounds.bottom
    lat_max = raster_dim.bounds.top

    # Geotransform for each resampled raster (east, south, west, north, width, height)
    geotransform_res = rasterio.transform.from_bounds(lon_min, lat_min, lon_max, lat_max, width * 100, height * 100)

    # Resample each GeoTIFF
    for file in files:
        path = os.path.join(tiffs, file)
        logging.info(path)
        with rasterio.open(path) as dataset:
            data = dataset.read(
                out_shape=(int(dataset.height * 100), int(dataset.width * 100)),
                # Reduce 100 to 10 if using the whole globe
                resampling=Resampling.nearest
            )

        # Convert new resampled array from 3D to 2D
        data = numpy.squeeze(data, axis=0)

        # Specify the filepath of the resampled raster
        resample_filename = file.replace('.tif', '_resampled.tif')
        resample_filepath = os.path.join(resampleds, resample_filename)

        # Save the GeoTIFF
        with rasterio.open(
                resample_filepath,
                'w',
                driver='GTiff',
                height=data.shape[0],
                width=data.shape[1],
                count=1,
                dtype=data.dtype,
                nodata=numpy.nan,
                crs='+proj=latlong',
                transform=geotransform_res,
        ) as dst:
            dst.write(data, 1)

    # delete the non-resampled tiffs now that we dont need them
    shutil.rmtree(tiffs)

    return

    Script to resample rasters from .25 o .0025 degree in order for rasterstats to work
    Dependencies: datetime, os, numpy, rasterio
    
Script to resample rasters from .25 o .0025 degree in order for rasterstats to work
Dependencies: datetime, os, numpy, rasterio
Script to resample rasters from . 25 o . 0025 degree in order for rasterstats to work Dependencies : datetime os numpy rasterio
def resample(wrksppath, region, model):
    logging.info('\nResampling the rasters for ' + region)
    tiffs = os.path.join(wrksppath, region, model + '_GeoTIFFs')
    resampleds = os.path.join(wrksppath, region, model + '_GeoTIFFs_resampled')
    if not os.path.exists(tiffs):
        logging.info('There is no tiffs folder. You must have already resampled them. Skipping resampling')
        return
    files = os.listdir(tiffs)
    files = [tif for tif in files if tif.endswith('.tif')]
    files.sort()
    path = os.path.join(tiffs, files[0])
    raster_dim = rasterio.open(path)
    width = raster_dim.width
    height = raster_dim.height
    lon_min = raster_dim.bounds.left
    lon_max = raster_dim.bounds.right
    lat_min = raster_dim.bounds.bottom
    lat_max = raster_dim.bounds.top
    geotransform_res = rasterio.transform.from_bounds(lon_min, lat_min, lon_max, lat_max, width * 100, height * 100)
    for file in files:
        path = os.path.join(tiffs, file)
        logging.info(path)
        with rasterio.open(path) as dataset:
            data = dataset.read(
                out_shape=(int(dataset.height * 100), int(dataset.width * 100)),
                resampling=Resampling.nearest
            )
        data = numpy.squeeze(data, axis=0)
        resample_filename = file.replace('.tif', '_resampled.tif')
        resample_filepath = os.path.join(resampleds, resample_filename)
        with rasterio.open(
                resample_filepath,
                'w',
                driver='GTiff',
                height=data.shape[0],
                width=data.shape[1],
                count=1,
                dtype=data.dtype,
                nodata=numpy.nan,
                crs='+proj=latlong',
                transform=geotransform_res,
        ) as dst:
            dst.write(data, 1)
    shutil.rmtree(tiffs)
    return
def resample ( wrksppath , region , model ) : logging . info ( '\nResampling the rasters for ' + region ) tiffs = os . path . join ( wrksppath , region , model + '_GeoTIFFs' ) resampleds = os . path . join ( wrksppath , region , model + '_GeoTIFFs_resampled' ) if not os . path . exists ( tiffs ) : logging . info ( 'There is no tiffs folder. You must have already resampled them. Skipping resampling' ) return files = os . listdir ( tiffs ) files = [ tif for tif in files if tif . endswith ( '.tif' ) ] files . sort ( ) path = os . path . join ( tiffs , files [ 0 ] ) raster_dim = rasterio . open ( path ) width = raster_dim . width height = raster_dim . height lon_min = raster_dim . bounds . left lon_max = raster_dim . bounds . right lat_min = raster_dim . bounds . bottom lat_max = raster_dim . bounds . top geotransform_res = rasterio . transform . from_bounds ( lon_min , lat_min , lon_max , lat_max , width * 100 , height * 100 ) for file in files : path = os . path . join ( tiffs , file ) logging . info ( path ) with rasterio . open ( path ) as dataset : data = dataset . read ( out_shape = ( int ( dataset . height * 100 ) , int ( dataset . width * 100 ) ) , resampling = Resampling . nearest ) data = numpy . squeeze ( data , axis = 0 ) resample_filename = file . replace ( '.tif' , '_resampled.tif' ) resample_filepath = os . path . join ( resampleds , resample_filename ) with rasterio . open ( resample_filepath , 'w' , driver = 'GTiff' , height = data . shape [ 0 ] , width = data . shape [ 1 ] , count = 1 , dtype = data . dtype , nodata = numpy . nan , crs = '+proj=latlong' , transform = geotransform_res , ) as dst : dst . write ( data , 1 ) shutil . rmtree ( tiffs ) return
Script to resample rasters from .25 o .0025 degree in order for rasterstats to work
Dependencies: datetime, os, numpy, rasterio
Script to resample rasters from . 25 o . 0025 degree in order for rasterstats to work Dependencies : datetime os numpy rasterio
"""
    Script to resample rasters from .25 o .0025 degree in order for rasterstats to work
    Dependencies: datetime, os, numpy, rasterio
    """ # Define app workspace and sub-paths # Create directory for the resampled GeoTIFFs # List all Resampled GeoTIFFs # Read raster dimensions # Geotransform for each resampled raster (east, south, west, north, width, height) # Resample each GeoTIFF # Reduce 100 to 10 if using the whole globe # Convert new resampled array from 3D to 2D # Specify the filepath of the resampled raster # Save the GeoTIFF # delete the non-resampled tiffs now that we dont need them
wrksppath
region
model