Skip to content

Prepare Multiprocessing Workflow

Alan D. Snow edited this page Dec 8, 2016 · 4 revisions

Folder Organization:

You will need to organize your DEM files such that your main directory has a folder for each DEM file.

AutoRoutePy has a script to convert the folder of DEMs to the correct format.

Note: It only works if all files associated with the DEM have the same base filename (Ex. elevation.tif, elevation.prj ...)

from AutoRoutePy.prepare import organize_dem

organize_dem(input_folder='/path/to/folder/with/dems', 
             output_folder='/autoroute-io/input/watershed_directory', 
             dem_ext=".tif")

Function definition:

#How to import
from AutoRoutePy.prepare import prepare_autoroute_multiprocess
#Function
prepare_autoroute_multiprocess(watershed_folder,
                               autoroute_executable_location,
                               stream_network_shapefile,
                               log_directory,
                               land_use_raster="",
                               manning_n_table="",
                               dem_extension='img',
                               river_id='COMID',
                               slope_id='SLOPE',
                               streamflow_id="",
                               default_manning_n=0.035,
                               return_period="",
                               return_period_file="",
                               rapid_output_file="",
                               date_peak_search_start=None,
                               date_peak_search_end=None,
                               num_cpus=-17)
Variable Data Type Description Default
watershed_folder String Main directory where the sub folders with elevation DEM files are stored.
autoroute_executable_location String Path to AutoRoute executable.
stream_network_shapefile String Path to stream network shapefile with unique id and slope for each stream segment.
log_directory String Path to output logs for the preparation runs.
land_use_raster String (Optional) Land use raster covering the entire watershed domain whereby Manning's n rasters can be generated for the individual tiles.
manning_n_table String (Optional) Table of values to convert land use raster values to Manning's n values. NOTE: These are available in the AutoRoute repository.
dem_extension String (Optional) This is the extension of the DEM raster you are using (e.g. tif, asc, img ...). "img"
river_id String (Optional) This is the unique identifier field in the stream network shapefile. "COMID"
slope_id String (Optional) This is the slope field in the stream network shapefile. "SLOPE"
streamflow_id String (Optional) This is the streamflow peak field in the stream network shapefile.
default_manning_n Float (Optional) This is the value of Mannning's n used in the simulation when there are missing values or no Manning's n raster. 0.035
return_period String (Optional) This is the value of the field in the return period file generated by RAPIDpy. Valid values include: 'max_flow','return_period_20','return_period_10', and 'return_period_2'.
return_period_file String (Optional) This is the path to the return period file generated by RAPIDpy.
rapid_output_file String (Optional) This is the path to a CF compliant RAPID Qout output file.
date_peak_search_start datetime (Optional) This constrains the start of the search for the peak in the RAPID Qout file.
date_peak_search_end datetime (Optional) This constrains the end of the search for the peak in the RAPID Qout file.
num_cpus Int (Optional) This is the number of CPUs to use in the process. Use all CPUs

Usage Examples:

Here are examples of using multiprocessing to prepare input for AutoRoute. This requires each elevation DEM file to be inside of its own folder within the main watershed folder.

WARNING: The land use raster must be in the same projection as your elevation raster! If it is not in the same projection, either reproject using a GIS tool or use the reproject_lu_raster function (from RAPIDpy.prepare import reproject_lu_raster).

####Mode 1: Basic (NO STREAMFLOW) In this example, it shows how to prepare inputs with just an elevation DEM and stream network shapefile.

from AutoRoutePy.prepare import prepare_autoroute_multiprocess
from multiprocessing import freeze_support #for WINDOWS 

def main():
    prepare_autoroute_multiprocess(watershed_folder='/autoroute-io/input/watershed_directory',
                                   autoroute_executable_location='/AutoRoute/src/autoroute',
                                   stream_network_shapefile='/path/to/river_network.shp',
                                   log_directory="/path/to/logs",
                                   #dem_extension='img',
                                   #river_id='COMID',
                                   #slope_id='SLOPE',
                                   #default_manning_n=0.035,
                                   num_cpus=4
                                   )
if __name__=="__main__":
    freeze_support() #for WINDOWS 
    main()

####Mode 2: Basic+Land Use (NO STREAMFLOW) WARNING: The land use raster must be in the same projection as your elevation raster! If it is not in the same projection, either reproject using a GIS tool or use this script.

from AutoRoutePy.prepare import reproject_lu_raster

dem_raster = '/autoroute-io/input/watershed-directory/sub_area-directory/elevation.img'
land_use_raster = '/autoroute_prepare/watershed-directory/NLCD2011_LC.tif'
reprojected_land_use_raster = '/autoroute_prepare/watershed-directory/NLCD2011_LC_repr.tif'

reproject_lu_raster(dem_raster, land_use_raster, reprojected_land_use_raster)

Once your land use raster is in the correct projection, use this process to generate the manning n raster.

from AutoRoutePy.prepare import prepare_autoroute_multiprocess
from multiprocessing import freeze_support #for WINDOWS 

def main():
    land_use_to_mannning_n_table = '/AutoRoute/mannings_n_tabes/AR_Manning_n_for_NLCD_LOW.txt'
    prepare_autoroute_multiprocess(watershed_folder='/autoroute-io/input/watershed_directory',
                                   autoroute_executable_location='/AutoRoute/src/autoroute',
                                   stream_network_shapefile='/path/to/river_network.shp',
                                   log_directory="/path/to/logs",
                                   land_use_raster=reprojected_land_use_raster,
                                   manning_n_table=land_use_to_mannning_n_table,
                                   #dem_extension='img',
                                   #river_id='COMID',
                                   #slope_id='SLOPE',
                                   #default_manning_n=0.035,
                                   #num_cpus=1
                                   )
if __name__=="__main__":
    freeze_support() #for WINDOWS 
    main()

####Mode 3: Streamflow Note: You can run with or without land use as shown in Mode 1 & Mode 2. This is the same for Mode 3, however only the basic mode will be shown here.

#####Mode 3.1: Streamflow from RAPID output In this mode it will search for the peak from the entire simulation unless bounds are given from date_peak_search_start or from the date_peak_search_end.

from datetime import datetime
from AutoRoutePy.prepare import prepare_autoroute_multiprocess
from multiprocessing import freeze_support #for WINDOWS 

def main():
    prepare_autoroute_multiprocess(watershed_folder='/autoroute-io/input/watershed_directory',
                                   autoroute_executable_location='/AutoRoute/src/autoroute',
                                   stream_network_shapefile='/path/to/river_network.shp',
                                   log_directory="/path/to/logs",
                                   #dem_extension='img',
                                   #river_id='COMID',
                                   #slope_id='SLOPE',
                                   #default_manning_n=0.035,
                                   rapid_output_file="/path/to/Qout.nc",
                                   #date_peak_search_start=datetime(1990, 1, 1), #optional
                                   #date_peak_search_end=datetime(1990, 3, 5), #optional
                                   #num_cpus=1
                                   )
if __name__=="__main__":
    freeze_support() #for WINDOWS 
    main()

#####Mode 3.2: Streamflow from shapefile field

from AutoRoutePy.prepare import prepare_autoroute_multiprocess
from multiprocessing import freeze_support #for WINDOWS 

def main():
    prepare_autoroute_multiprocess(watershed_folder='/autoroute-io/input/watershed_directory',
                                   autoroute_executable_location='/AutoRoute/src/autoroute',
                                   stream_network_shapefile='/path/to/river_network.shp',
                                   log_directory="/path/to/logs",
                                   #dem_extension='img',
                                   #river_id='COMID',
                                   #slope_id='SLOPE',
                                   streamflow_id='Flow_500',
                                   #default_manning_n=0.035,
                                   #num_cpus=1
                                   )
if __name__=="__main__":
    freeze_support() #for WINDOWS 
    main()

#####Mode 3.3: Streamflow from return period file

from AutoRoutePy.prepare import prepare_autoroute_multiprocess
from multiprocessing import freeze_support #for WINDOWS 

def main():
    prepare_autoroute_multiprocess(watershed_folder='/autoroute-io/input/watershed_directory',
                                   autoroute_executable_location='/AutoRoute/src/autoroute',
                                   stream_network_shapefile='/path/to/river_network.shp',
                                   log_directory="/path/to/logs",
                                   #dem_extension='img',
                                   #river_id='COMID',
                                   #slope_id='SLOPE',
                                   #default_manning_n=0.035,
                                   #NOTE: valid return_periods -> 'max_flow','return_period_20','return_period_10','return_period_2'
                                   return_period="return_period_20", 
                                   return_period_file="/path/to/return_period.nc", 
                                   #num_cpus=1
                                   )
if __name__=="__main__":
    freeze_support() #for WINDOWS 
    main()