GISHydroNXT System Documentation

#*********************************************************************************
# Author:       UMD
# Date:         24-07-2018
# Modified:     n/a
# Classes:      ResetSubWatershed() 
# Functions:    n/a
# Modules:      arcpy ; os
# Comments:     n/a
#*********************************************************************************
class ResetSubWatershed(object):
    """Implementation for GISHydroNXT_addin.button5 (Button)"""
    def __init__(self):
        self.enabled = False
        self.checked = False
    def onClick(self):
        #*******************************************************************************************************
        # Remove layers and tables from data frame
        #*******************************************************************************************************
        result_addas = arcpy.GetCount_management(optfolder + "/AddasStreams.shp")
        result_outlets = arcpy.GetCount_management(optfolder + "/AddasOutlets.shp")
        count_subsheds = int(result_addas.getOutput(0)) + int(result_outlets.getOutput(0))
        subshed = optfolder + "/subshed.shp"
        arcid_list = []
        shedtab = arcpy.SearchCursor(subshed,"","","ARCID","")
        for s in shedtab:
            arcid = s.getValue("ARCID")
            arcid_list.append(str(int(arcid)))
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layers = arcpy.mapping.ListLayers(mxd, "", df)
        tables = arcpy.mapping.ListTableViews(mxd, "", df)
        for lyr in layers:
            if lyr.name == "ModStreams":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "subrivers":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "elevmerge":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "elevzones":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "subshed":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "AddasStreams":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "AddasOutlets":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "polyras":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "Outlets_temp":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "line":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "modstr":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "outlets":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "strlnk":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "StrmMerge.shp":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "subriver_subshed.shp":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "subriver_xy.shp":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "subshed_subriver.shp":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "subshed_temp":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "subsheds":
                arcpy.mapping.RemoveLayer(df, lyr)
            if lyr.name == "tmpsubwshd.shp":
                arcpy.mapping.RemoveLayer(df, lyr)
            for i in xrange(0,2 * count_subsheds,1):
                if lyr.name == "Longest_Path_Sub_" + str(i+1):
                    arcpy.mapping.RemoveLayer(df, lyr)
                if lyr.name == "xline_reach" + str(i+1):
                    arcpy.mapping.RemoveLayer(df, lyr)
                if lyr.name == "xpoint_reach" + str(i+1):
                    arcpy.mapping.RemoveLayer(df, lyr)
                if os.path.exists(optfolder + "/aux_folder/mask" + str(i)):
                    arcpy.Delete_management(optfolder + "/aux_folder/mask" + str(i),"")
                if os.path.exists(optfolder + "/xline_reach" + str(i+1) +".shp"):
                    arcpy.Delete_management(optfolder + "/xline_reach" + str(i+1) +".shp")
                if os.path.exists(optfolder + "/xpoint_reach4" + str(i+1) +".shp"):
                    arcpy.Delete_management(optfolder + "/xpoint_reach" + str(i+1) +".shp")

        for tbl in tables:
            if tbl.name == "cntable":
                arcpy.mapping.RemoveTableView(df, tbl)
            if tbl.name == "longfp":
                arcpy.mapping.RemoveTableView(df, tbl)
            if tbl.name == "slope_sheds":
                arcpy.mapping.RemoveTableView(df, tbl)
            if tbl.name == "slope_stats":
                arcpy.mapping.RemoveTableView(df, tbl)

        arcpy.RefreshTOC()
        arcpy.RefreshActiveView()

        #*******************************************************************************************************
        # Delete files from optfolder
        #*******************************************************************************************************
        # Add Outlets file names
        #********************************************
        outlets_tmp= optfolder + "/outlets_temp"
        addoutlets = optfolder + "/AddOutlets"
        outlets_usr= optfolder + "/outlets_user"
        addasstreams = optfolder + "/AddasStreams.shp"
        addasoutlets = optfolder + "/AddasOutlets.shp"

        if os.path.exists(outlets_tmp):
            arcpy.Delete_management(outlets_tmp,"")
        if os.path.exists(addoutlets):
            arcpy.Delete_management(addoutlets,"")
        if os.path.exists(outlets_usr):
            arcpy.Delete_management(outlets_usr,"")
        if os.path.exists(addasstreams):
            arcpy.Delete_management(addasstreams,"")
        if os.path.exists(addasstreams):
            arcpy.Delete_management(addasoutlets,"")
        
        # Add Streams file names
        #********************************************
        strm_merge = optfolder + "/StrmMerge.shp"
        modstr     = optfolder + "/modstr"
        modstreams = optfolder + "/modstreams"

        if os.path.exists(strm_merge):
            arcpy.Delete_management(strm_merge,"")
        if os.path.exists(modstr):
            arcpy.Delete_management(modstr,"")
        if os.path.exists(modstreams):
            arcpy.Delete_management(modstreams,"")

        # Delineate Subwatersheds file names
        #********************************************
        strlnk     = optfolder + "/strlnk"
        outlets    = optfolder + "/outlets"
        outlets_str= optfolder + "/outlets_str"
        added_outl = optfolder + "/added_outlets"
        subshed_tmp= optfolder + "/subshed_temp"
        tmpsubw    = optfolder + "/tmpsubwshd.shp"
        sub_shape  = optfolder + "/subshed.shp"
        new_strlnk = optfolder + "/newstrlnk"        
        subrivers  = optfolder + "/subrivers.shp" # still present in folder and after 3 times Reset, contain earlier polygon entries
        subriv_sub = optfolder + "/subriver_subshed.shp"
        subriv_xy  = optfolder + "/subriver_xy.shp"
        sub_subriv = optfolder + "/subshed_subriver.shp"
        sub_ras    = optfolder + "/subsheds"

        if os.path.exists(strlnk):
            arcpy.Delete_management(strlnk,"")
        if os.path.exists(outlets):
            arcpy.Delete_management(outlets,"")
        if os.path.exists(outlets_str):
            arcpy.Delete_management(outlets_str,"")
        if os.path.exists(added_outl):
            arcpy.Delete_management(added_outl,"")
        if os.path.exists(subshed_tmp):
            arcpy.Delete_management(subshed_tmp,"")
        if os.path.exists(tmpsubw):
            arcpy.Delete_management(tmpsubw,"")
        if os.path.exists(sub_shape):
            arcpy.Delete_management(sub_shape,"")
        if os.path.exists(new_strlnk):
            arcpy.Delete_management(new_strlnk,"")
        if os.path.exists(subrivers):
            arcpy.Delete_management(subrivers,"")
        if os.path.exists(subriv_sub):
            arcpy.Delete_management(subriv_sub,"")
        if os.path.exists(subriv_xy):
            arcpy.Delete_management(subriv_xy,"")
        if os.path.exists(sub_subriv):
            arcpy.Delete_management(sub_subriv,"")
        if os.path.exists(sub_ras):
            arcpy.Delete_management(sub_ras,"")

        # "Calculate Attributes" file names
        #********************************************
        if os.path.exists(optfolder + "/elevmerge.shp"):
            for i in xrange(0,no_subwatersheds,1):
                if os.path.exists(optfolder + "/Tc_subshed" + str(i)):
                    arcpy.Delete_management(optfolder + "/Tc_subshed" + str(i))
                if os.path.exists(optfolder + "/vel_meth/LongPathSub" + str(i)):
                    arcpy.Delete_management(optfolder + "/vel_meth/LongPathSub" + str(i))

        polyras    = optfolder + "/polyras"
        elevmerge  = optfolder + "/elevmerge.shp"
        elevzones  = optfolder + "/elevzones.shp"
        sub_prov   = optfolder + "/subshed_prov.shp"
        cntable    = optfolder + "/cntable.dbf"
        longfp     = optfolder + "/longfp.dbf"
        slope_shed = optfolder + "/slope_sheds.dbf"
        slope_stat = optfolder + "/slope_stats.dbf"
        line       = optfolder + "/line.shp"
        
        if os.path.exists(polyras):
            arcpy.Delete_management(polyras,"")
        if os.path.exists(elevmerge):
            arcpy.Delete_management(elevmerge,"")
        if os.path.exists(elevzones):
            arcpy.Delete_management(elevzones,"")
        if os.path.exists(sub_prov):
            arcpy.Delete_management(sub_prov,"")
        if os.path.exists(cntable):
            arcpy.Delete_management(cntable,"")
        if os.path.exists(longfp):
            arcpy.Delete_management(longfp,"")
        if os.path.exists(slope_shed):
            arcpy.Delete_management(slope_shed,"")
        if os.path.exists(slope_stat):
            arcpy.Delete_management(slope_stat,"")
        if os.path.exists(line):
            arcpy.Delete_management(line,"")
        frdischarges = optfolder + "/frdischarges.txt"
        if os.path.exists(frdischarges):
            arcpy.Delete_management(frdischarges,"")
            
        # Deleting "vel_meth" folder after deleting "LongPathSub" files above. Edited on 5/24/2017 to delete vel_meth folder
        # which is under schema lock
        vel_meth = optfolder + "/vel_meth"
        if os.path.exists(vel_meth):
            shutil.rmtree(vel_meth)

        # Delete "sub_basincomp" folder
        #********************************************
        sub_basin_folder = optfolder + "/sub_basincomp"
        if os.path.exists(sub_basin_folder):
            shutil.rmtree(sub_basin_folder)


        # Delete "rating_table" and "elev_stage_profile" folder
        #******************************************************
        rating_table_folder = optfolder + "/rating_table"
        if os.path.exists(rating_table_folder):
            shutil.rmtree(rating_table_folder)

        elev_stage = optfolder + "/elev_stage_profile"
        if os.path.exists(elev_stage):
            shutil.rmtree(elev_stage)

        #*******************************************************************************************************
        # Re-create "AddasStreams.shp" for to be used in subwatershed delineation
        # (this shapefile was created earlier during data selection step but reset
        #  button will delete it. Here it is re-created to store stream lines on run)
        #*******************************************************************************************************
        arcpy.env.addOutputsToMap = True
        spatial_reference = arcpy.Describe(optfolder + "/flowdir").spatialReference
        arcpy.CreateFeatureclass_management(optfolder, "AddasStreams.shp", "POLYLINE", "", "ENABLED", "DISABLED", spatial_reference)

        spatial_reference = arcpy.Describe(optfolder + "/flowdir").spatialReference
        arcpy.CreateFeatureclass_management(optfolder, "AddasOutlets.shp", "POINT", "", "ENABLED", "DISABLED", spatial_reference)

        #*******************************************************************************************************
        # set extent back to data selection rectangle
        #*******************************************************************************************************
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        wshExtent = df.extent
        wshExtent.XMin, wshExtent.YMin = extent.XMin, extent.YMin
        wshExtent.XMax, wshExtent.YMax = extent.XMax, extent.YMax
        df.extent = wshExtent

        # Remove precstat.txt and TR20in.txt
        #********************************************
        precstat = optfolder + "/precstat.txt"
        TR20in = optfolder + "/TR20in.txt"
        if os.path.exists(precstat):
            arcpy.Delete_management(precstat,"")
        if os.path.exists(TR20in):
            arcpy.Delete_management(TR20in,"")

        #*******************************************************************************************************        
        # turn watershed delineation OFF and basin stat ON
        #*******************************************************************************************************
        button1.enabled   = True
        tool6.enabled     = False
        tool5.enabled     = False
        tool4.enabled     = True
        button6.enabled   = True
        button5.enabled   = False
        button8.enabled   = False
        button9.enabled   = False
        button10.enabled   = False
        button11.enabled   = False
        button13.enabled  = False
        button14.enabled  = False
        button15.enabled  = False