<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def demToStreamNetwork(self, output_directory, raw_elevation_dem="", pit_filled_elevation_grid="", flow_dir_grid_d8="", contributing_area_grid_d8="", flow_dir_grid_dinf="", contributing_area_grid_dinf="", use_dinf=False, threshold=1000, delineate=False):
 """ This function will run all of the TauDEM processes to generate a stream network from an elevation raster. .. note:: For information about the stream reach and watershed process: http://hydrology.usu.edu/taudem/taudem5/help53/StreamReachAndWatershed.html .. note:: For information about the *threshold* parameter, see: http://hydrology.usu.edu/taudem/taudem5/help53/StreamDefinitionByThreshold.html Parameters output_directory: str Path to output generated files to. raw_elevation_dem: str, optional Path to original elevation DEM file. Required if *pit_filled_elevation_grid* is not used. pit_filled_elevation_grid: str, optional Path to pit filled elevation DEM file. Required if *raw_elevation_dem* is not used. flow_dir_grid_d8: str, optional Path to flow direction grid generated using TauDEM's D8 method. contributing_area_grid_d8: str, optional Path to contributing area grid generated using TauDEM's D8 method. flow_dir_grid_dinf: str, optional Path to flow direction grid generated using TauDEM's D-Infinity method (EXPERIMENTAL). contributing_area_grid_dinf: str, optional Path to contributing area grid generated using TauDEM's D-Infinity method (EXPERIMENTAL). use_dinf: bool, optional Use the D-Infinity method to get stream definition (EXPERIMENTAL). threshold: int, optional The stream threshold or maximum number of upstream grid cells. See above note. delineate: bool, optional If True, this will use the delineate option for theis method using TauDEM. Default is False. Here is an example of how to use this: .. code:: python from RAPIDpy.gis.taudem import TauDEM elevation_dem = '/path/to/dem.tif' output_directory = '/path/to/output/files' td = TauDEM("/path/to/scripts/TauDEM") td.demToStreamNetwork(output_directory, elevation_dem, threshold=1000) """

        time_start = datetime.utcnow()

        # FILL PITS IF NEEDED
        self.pit_filled_elevation_grid = pit_filled_elevation_grid
        if not pit_filled_elevation_grid:
            pit_filled_elevation_grid = \
                os.path.join(output_directory, 'pit_filled_elevation_grid.tif')
            self.pitRemove(raw_elevation_dem,
                           pit_filled_elevation_grid)

        # GENERATE D8 RASTERS
        self.flow_dir_grid = flow_dir_grid_d8
        if not flow_dir_grid_d8:
            flow_dir_grid_d8 = \
                os.path.join(output_directory, 'flow_dir_grid_d8.tif')
            slope_grid_d8 = os.path.join(output_directory, 'slope_grid_d8.tif')
            self.d8FlowDirection(flow_dir_grid_d8,
                                 slope_grid_d8)

        self.contributing_area_grid = contributing_area_grid_d8
        if not contributing_area_grid_d8:
            contributing_area_grid_d8 = \
                os.path.join(output_directory, 'contributing_area_grid_d8.tif')
            self.d8ContributingArea(contributing_area_grid_d8)

        stream_raster_grid = \
            os.path.join(output_directory, 'stream_raster_grid.tif')
        if use_dinf:
            log("USING DINF METHOD TO GET STREAM DEFINITION ...")
            if not flow_dir_grid_dinf:
                flow_dir_grid_dinf = \
                    os.path.join(output_directory, 'flow_dir_grid_dinf.tif')
                slope_grid_dinf = \
                    os.path.join(output_directory, 'slope_grid_dinf.tif')
                self.dinfFlowDirection(flow_dir_grid_dinf,
                                       slope_grid_dinf)
            if not contributing_area_grid_dinf:
                contributing_area_grid_dinf = \
                    os.path.join(output_directory,
                                 'contributing_area_grid_dinf.tif')
                self.dinfContributingArea(contributing_area_grid_dinf,
                                          flow_dir_grid_dinf)

            self.streamDefByThreshold(stream_raster_grid,
                                      threshold,
                                      contributing_area_grid_dinf)
        else:
            log("USING D8 METHOD TO GET STREAM DEFINITION ...")
            self.streamDefByThreshold(stream_raster_grid,
                                      threshold,
                                      contributing_area_grid_d8)

        # GENERATE STREAM NETWORK
        out_stream_order_grid = \
            os.path.join(output_directory, 'stream_order_grid.tif')
        out_network_connectivity_tree = \
            os.path.join(output_directory, 'network_connectivity_tree.txt')
        out_network_coordinates = \
            os.path.join(output_directory, 'network_coordinates.txt')
        out_stream_reach_file = \
            os.path.join(output_directory, 'stream_reach_file.shp')
        out_watershed_grid = \
            os.path.join(output_directory, 'watershed_grid.tif')
        self.streamReachAndWatershed(delineate,
                                     out_stream_order_grid,
                                     out_network_connectivity_tree,
                                     out_network_coordinates,
                                     out_stream_reach_file,
                                     out_watershed_grid)

        # convert watersed grid to shapefile
        out_watershed_shapefile = \
            os.path.join(output_directory, 'watershed_shapefile.shp')
        self.rasterToPolygon(out_watershed_grid, out_watershed_shapefile)
        log("Total time to complete: {0}".format(datetime.utcnow() -
                                                 time_start))