Skip to content

Prepare Individual Items

Alan D. Snow edited this page Jun 13, 2017 · 3 revisions

This is the fine grained method. To begin, initialize the AutoRoutePrepare class with the location of the AutoRoute executable, the path to your elevation raster, and the path to your stream network shapefile.

from AutoRoutePy.prepare import AutoRoutePrepare
import os

autoroute_executable_location = '/AutoRoute/src/autoroute'
input_dir = '/autoroute-io/input/watershed_directory/sub_folder'
stream_info_file = os.path.join(input_dir,'stream_info.txt')
river_network_file = '/path/to/river_network.shp'


arp = AutoRoutePrepare(autoroute_executable_location,
                       os.path.join(input_dir, 'elevation.dt2'),
                       stream_info_file,
                       river_network_file)

Next, to connect the elevation DEM to the stream network, you need to rasterize the stream network based on the elevation DEM.

out_rasterized_streamfile = os.path.join(input_dir, 'rasterized_streamfile.tif')

arp.rasterize_stream_shapefile(out_rasterized_streamfile,
                               stream_id='HydroID') #the attribute name of the stream ID used for RAPID

Then, create your stream info file and add stream direction and slope to it.

arp.generate_stream_info_file_with_direction(out_rasterized_streamfile,
                                             search_radius=1) #distance to search for stream direction in meters

arp.append_slope_to_stream_info_file('HydroID', #the attribute name of the stream ID used for RAPID
                                     'Avg_Slope') #the attribute name of the stream slope

(Optional) Prepare Manning's N Raster

Based on a land use raster and a land use table, you can generate a Manning’s N raster to use with AutoRoute.

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.reproject_raster 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 AutoRoutePrepare
import os

autoroute_executable_location = '/AutoRoute/src/autoroute'
input_dir = '/autoroute-io/input/watershed-directorysub_area-directory'
reprojected_land_use_raster = '/autoroute_prepare/watershed-directory/NLCD2011_LC_repr.tif'
land_use_to_mannning_n_table = '/AutoRoute/mannings_n_tabes/AR_Manning_n_for_NLCD_LOW.txt'

arp = AutoRoutePrepare(autoroute_executable_location,
                       os.path.join(input_dir, 'elevation.dt2'))
#Method to generate manning_n file from DEM, Land Use Raster, and Manning N Table with new AutoRoute
arp.generate_manning_n_raster(land_use_raster=reprojected_land_use_raster,
                              input_manning_n_table=land_use_to_mannning_n_table,
                              output_manning_n_raster=os.path.join(main_dir, 'manning_n.tif'),
                              default_manning_n=0.035)

Adding Streamflow to AutoRoute Simulation

This is the code that starts the process off:

from AutoRoutePy.prepare import AutoRoutePrepare
import os

input_dir = '/autoroute-io/input/watershed-directorysub_area-directory'
reprojected_land_use_raster = '/autoroute_prepare/watershed-directory/NLCD2011_LC_repr.tif'
river_network_file = '/path/to/river_network.shp'

arp = AutoRoutePrepare("", #autoroute_executable_location, - don't need for this step
                       "", #dem path - don't need for this step
                       os.path.join(input_dir, 'stream_info.txt')
                       river_network_file)

From there, you have three options:

Create AutoRoute input from single RAPID output

This function grabs the peak within the specified time period. If no time period is specified, it searches the whole dataset.

from datetime import datetime
rapid_output_file = '/path/to/Qout_file.nc'


#OPTION 1: Search for Peak with Date Search
#NOTE: Date search will only work with CF compliant RAPID files
date_peak_search_start = datetime(1980,5,1)
date_peak_search_end = datetime(1980,8,30)

arp.append_streamflow_from_rapid_output(rapid_output_file,
                                        date_peak_search_start, #optional
                                        date_peak_search_end) #optional

#OPTION 2: Search for Peak in Entire Time Series
arp.append_streamflow_from_rapid_output(rapid_output_file)

Create AutoRoute input from return period file

If you created a return period file from RAPIDpy, you can add the peak flows from each return period to the stream_info.txt.

NOTE: Valid return period options: return_period_20, return_period_10, return_period_2, max_flow

return_period_file = "/path/to/return_periods.nc"
return_period = "return_period_20"
arp.append_streamflow_from_return_period_file(return_period_file, 
                                              return_period)

Create AutoRoute input from shapefile field

If you already have a shapefile with the flow associated with each river segment, you can use that as a method for input into the stream_info.txt file.

stream_id_field = "HydroID"
streamflow_field = "StreamFlow"

arp.append_streamflow_from_stream_shapefile(stream_id_field, 
                                            streamflow_field)