data_workflow/gfsworkflow.py
def zonal_statistics(wrksppath, timestamp, region, model):
    """
    Script to calculate average precip over FFGS polygon shapefile
    Dependencies: datetime, os, pandas, rasterstats
    """
    logging.info('\nDoing Zonal Statistics on ' + region)
    # Define app workspace and sub-paths
    resampleds = os.path.join(wrksppath, region, model + '_GeoTIFFs_resampled')
    shp_path = os.path.join(wrksppath, region, 'shapefiles', 'ffgs_' + region + '.shp')

    stat_file = os.path.join(wrksppath, region, model + 'results.csv')

    # check that there are resampled tiffs to do zonal statistics on
    if not os.path.exists(resampleds):
        logging.info('There are no resampled tiffs to do zonal statistics on. Skipping Zonal Statistics')
        return

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

    # do zonal statistics for each resampled tiff file and put it in the stats dataframe
    stats_df = pd.DataFrame()
    for i in range(len(files)):
        logging.info('starting zonal statistics for ' + files[i])
        ras_path = os.path.join(resampleds, files[i])
        stats = rasterstats.zonal_stats(
            shp_path,
            ras_path,
            stats=['count', 'max', 'mean'],
            geojson_out=True
            )

        timestep = files[i][:10]

        # for each stat that you get out, write it to the dataframe
        logging.info('writing the statistics for this file to the dataframe')
        for j in range(len(stats)):

            temp_data = stats[j]['properties']
            temp_data.update({'Forecast Timestamp': timestamp})
            temp_data.update({'Timestep': timestep})

            temp_df = pd.DataFrame([temp_data])
            stats_df = stats_df.append(temp_df, ignore_index=True)

    # write the resulting dataframe to a csv
    logging.info('\ndone with zonal statistics, rounding values, writing to a csv file')
    stats_df = stats_df.round({'max': 1, 'mean': 1})
    stats_df.to_csv(stat_file, index=False)

    # delete the resampled tiffs now that we dont need them
    logging.info('deleting the resampled tiffs directory')
    shutil.rmtree(resampleds)

    return

    Script to calculate average precip over FFGS polygon shapefile
    Dependencies: datetime, os, pandas, rasterstats
    
Script to calculate average precip over FFGS polygon shapefile
Dependencies: datetime, os, pandas, rasterstats
Script to calculate average precip over FFGS polygon shapefile Dependencies : datetime os pandas rasterstats
def zonal_statistics(wrksppath, timestamp, region, model):
    logging.info('\nDoing Zonal Statistics on ' + region)
    resampleds = os.path.join(wrksppath, region, model + '_GeoTIFFs_resampled')
    shp_path = os.path.join(wrksppath, region, 'shapefiles', 'ffgs_' + region + '.shp')
    stat_file = os.path.join(wrksppath, region, model + 'results.csv')
    if not os.path.exists(resampleds):
        logging.info('There are no resampled tiffs to do zonal statistics on. Skipping Zonal Statistics')
        return
    files = os.listdir(resampleds)
    files = [tif for tif in files if tif.endswith('.tif')]
    files.sort()
    stats_df = pd.DataFrame()
    for i in range(len(files)):
        logging.info('starting zonal statistics for ' + files[i])
        ras_path = os.path.join(resampleds, files[i])
        stats = rasterstats.zonal_stats(
            shp_path,
            ras_path,
            stats=['count', 'max', 'mean'],
            geojson_out=True
            )
        timestep = files[i][:10]
        logging.info('writing the statistics for this file to the dataframe')
        for j in range(len(stats)):
            temp_data = stats[j]['properties']
            temp_data.update({'Forecast Timestamp': timestamp})
            temp_data.update({'Timestep': timestep})
            temp_df = pd.DataFrame([temp_data])
            stats_df = stats_df.append(temp_df, ignore_index=True)
    logging.info('\ndone with zonal statistics, rounding values, writing to a csv file')
    stats_df = stats_df.round({'max': 1, 'mean': 1})
    stats_df.to_csv(stat_file, index=False)
    logging.info('deleting the resampled tiffs directory')
    shutil.rmtree(resampleds)
    return
def zonal_statistics ( wrksppath , timestamp , region , model ) : logging . info ( '\nDoing Zonal Statistics on ' + region ) resampleds = os . path . join ( wrksppath , region , model + '_GeoTIFFs_resampled' ) shp_path = os . path . join ( wrksppath , region , 'shapefiles' , 'ffgs_' + region + '.shp' ) stat_file = os . path . join ( wrksppath , region , model + 'results.csv' ) if not os . path . exists ( resampleds ) : logging . info ( 'There are no resampled tiffs to do zonal statistics on. Skipping Zonal Statistics' ) return files = os . listdir ( resampleds ) files = [ tif for tif in files if tif . endswith ( '.tif' ) ] files . sort ( ) stats_df = pd . DataFrame ( ) for i in range ( len ( files ) ) : logging . info ( 'starting zonal statistics for ' + files [ i ] ) ras_path = os . path . join ( resampleds , files [ i ] ) stats = rasterstats . zonal_stats ( shp_path , ras_path , stats = [ 'count' , 'max' , 'mean' ] , geojson_out = True ) timestep = files [ i ] [ : 10 ] logging . info ( 'writing the statistics for this file to the dataframe' ) for j in range ( len ( stats ) ) : temp_data = stats [ j ] [ 'properties' ] temp_data . update ( { 'Forecast Timestamp' : timestamp } ) temp_data . update ( { 'Timestep' : timestep } ) temp_df = pd . DataFrame ( [ temp_data ] ) stats_df = stats_df . append ( temp_df , ignore_index = True ) logging . info ( '\ndone with zonal statistics, rounding values, writing to a csv file' ) stats_df = stats_df . round ( { 'max' : 1 , 'mean' : 1 } ) stats_df . to_csv ( stat_file , index = False ) logging . info ( 'deleting the resampled tiffs directory' ) shutil . rmtree ( resampleds ) return
Script to calculate average precip over FFGS polygon shapefile
Dependencies: datetime, os, pandas, rasterstats
Script to calculate average precip over FFGS polygon shapefile Dependencies : datetime os pandas rasterstats
"""
    Script to calculate average precip over FFGS polygon shapefile
    Dependencies: datetime, os, pandas, rasterstats
    """ # Define app workspace and sub-paths # check that there are resampled tiffs to do zonal statistics on # List all Resampled GeoTIFFs # do zonal statistics for each resampled tiff file and put it in the stats dataframe # for each stat that you get out, write it to the dataframe # write the resulting dataframe to a csv # delete the resampled tiffs now that we dont need them
wrksppath
timestamp
region
model