예제 #1
0
파일: sfr_plots.py 프로젝트: aleaf/SFRmaker
    def join_SFR_out2streams(self, use_arcpy=True):

        # get model info
        try:
            DX, DY, NLAY, NROW, NCOL, i = disutil.read_meta_data(self.DISfile)
        except:
            raise IOError("Cannot read MODFLOW DIS file {0}".format(self.DISfile))

        print "\naggregating flow information by cellnum..."
        indata = open(self.SFR_out).readlines()
        for line in indata[8:]:
        
            line = line.strip().split()
            
            # Kludge! terminates with blank line (only reads stress per. 1)
            # need to add support for transient.
            if len(line) == 0:
                break

            r, c = int(line[1]), int(line[2])
            cellnum = (r-1)*NCOL + c
            seg_rch = "{0} {1}; ".format(line[3], line[4])
            flow = 0.5 * (float(line[5]) + float(line[7]))
            loss = float(line[6])
            overland = float(line[8])
            stage = float(line[11])
            depth = float(line[12])

            try:
                existingflow = self.flow_by_cellnum[cellnum]
                seg_rch_info = self.seg_rch_by_cellnum[cellnum]
            except KeyError:
                existingflow = 0
                seg_rch_info = 'segs  rchs: '

            # determine state
            if flow == 0:
                state = 'dry'
            elif loss > 0:
                state = 'losing'
            elif loss < 0:
                state = 'gaining'
            else:
                print 'Stream reach in cell {} has flow, but no interaction with aquifer.'.format(cellnum)

            self.flow_by_cellnum[cellnum] = existingflow + flow
            self.seg_rch_by_cellnum[cellnum] = seg_rch_info + seg_rch
            self.loss_by_cellnum[cellnum] = loss
            self.state_by_cellnum[cellnum] = state
            self.overland_by_cellnum[cellnum] = overland
            self.stage_by_cellnum[cellnum] = stage
            self.depth_by_cellnum[cellnum] = depth

        # write to temporary output file
        ofp = open(os.path.join(self.outpath, 'temp.csv'), 'w')
        ofp.write('{},row,column,seg_reach,flow,loss,overland,state,stage,depth\n'.format(self.node_num_attribute))
        for cn in self.flow_by_cellnum.keys():
            ofp.write('{0},{1},{2},"{3}",{4:.6e},{5},{6},{7},{8},{9}\n'.format(cn, 1, 1,
                                                                   self.seg_rch_by_cellnum[cn],
                                                                   self.flow_by_cellnum[cn],
                                                                   self.loss_by_cellnum[cn],
                                                                   self.overland_by_cellnum[cn],
                                                                   self.state_by_cellnum[cn],
                                                                   self.stage_by_cellnum[cn],
                                                                   self.depth_by_cellnum[cn]))
        ofp.close()

        outfile = os.path.join(self.outpath, "{0}.shp".format(self.SFR_out[:-4]))
        if use_arcpy:
            try:
                import arcpy
                import SFR_arcpy
            except:
                print 'module arcpy not found!'

            # make feature/table layers
            arcpy.env.workspace = self.outpath
            arcpy.env.overwriteOutput = True
            arcpy.CopyFeatures_management(self.streams_shp, self.streams_shp[:-4]+'_backup.shp')
            arcpy.MakeFeatureLayer_management(self.streams_shp[:-4]+'_backup.shp', "streams")
            arcpy.CopyRows_management(os.path.join(self.outpath, 'temp.csv'), os.path.join(self.outpath, 'temp.dbf'))


            # drop all fields except for cellnum from stream linework
            Fields = arcpy.ListFields("streams")
            Fields = [f.name for f in Fields if f.name not in ["FID", "Shape", self.node_num_attribute]]
            if len(Fields) > 0:
                arcpy.DeleteField_management("streams", Fields)

            SFR_arcpy.general_join(outfile, "streams", self.node_num_attribute, "temp.dbf", self.node_num_attribute, keep_common=True)

        else:
            import sys
            sys.path.append('../../GIS_utils')
            try:
                import GISops
            except:
                print 'GIS_utils.GISops not found!'
            GISops.join_csv2shp(self.streams_shp, self.node_num_attribute, os.path.join(self.outpath, 'temp.csv'), self.node_num_attribute, outfile, how='inner')
예제 #2
0
# make sure grid information is in ELEV --- if not, join it in
arcpy.MakeFeatureLayer_management('river_w_elevations.shp','tmpelevs')
allfields = arcpy.ListFields('tmpelevs')
rc_exists = False
for cfield in allfields:
    if 'column' in cfield.name.lower(): 
        rc_exists = True
        break
    elif 'row' in cfield.name.lower():
        rc_exists = True
        break

if rc_exists == False:
    print 'need to join river_cells.shp to %s' %ELEV
    SFR_arcpy.general_join(ELEV,'tmpelevs',"node",MFgrid,"node",True)

arcpy.Delete_management('tmpelevs')
cellrows=arcpy.SearchCursor(ELEV)
for cell in cellrows:
    cellnum=int(cell.CELLNUM)
    comidin=int(cell.comid)
    comid[cellnum].append(comidin)
    comidseen[comidin]=1
    row[cellnum]=cell.row
    column[cellnum]=cell.column
    sidelength[cellnum]=float(cell.delx)
    if float(cell.dely) < sidelength[cellnum]:
        sidelength[cellnum]=float(cell.dely)
    reachlength[cellnum].append(cell.LengthFT)
    if re.match("min",elev_type,re.I):
예제 #3
0
파일: SFR_preproc.py 프로젝트: jlthomps/SFR
arcpy.MakeFeatureLayer_management(Flowlines,"Flowlines")

# If not already, permanently join PlusflowVAA and Elevslope, then to Flowlines    
if Join:
    print "\nJoining Elevslope to PlusflowVAA...\n"
    comid1=getfield("PlusflowVAA","comid")
    comid2=getfield("Elevslope","comid")
    arcpy.JoinField_management("PlusflowVAA",comid1,"Elevslope",comid2)
else:
    print "PlusflowVAA and Elevslope already joined from previous run..."

print "Joining PlusflowVAA to NHDFlowlines...\n"    
comid1=getfield("Flowlines","comid")

# join to Flowlines, keeping only common
SFR_arcpy.general_join(Flowlines, "Flowlines",comid1,PlusflowVAA,"comid",True)

# reopen flowlines as "Flowlines" --> clunky a bit to save and reopen, but must do so
arcpy.MakeFeatureLayer_management(Flowlines,"Flowlines")

print "\n"
ofp.write('\n' + 25*'#' + '\nRemoving segments with no elevation information, and with ThinnerCod = -9..\n')
print "Removing segments with no elevation information, and with ThinnerCod = -9..."
ThinnerCod=getfield("Flowlines","thinnercod")
MaxEl=getfield("Flowlines","maxelevsmo")
comid=getfield("Flowlines","comid")
FLtable=arcpy.UpdateCursor("Flowlines")
zerocount=0
tcount=0
for segments in FLtable:
    if segments.getValue(MaxEl)==0:
예제 #4
0
    def join_SFR_out2streams(self, use_arcpy=True):

        # get model info
        try:
            DX, DY, NLAY, NROW, NCOL, i = disutil.read_meta_data(self.DISfile)
        except:
            raise IOError("Cannot read MODFLOW DIS file {0}".format(
                self.DISfile))

        print "\naggregating flow information by cellnum..."
        indata = open(self.SFR_out).readlines()
        for line in indata[8:]:

            line = line.strip().split()

            # Kludge! terminates with blank line (only reads stress per. 1)
            # need to add support for transient.
            if len(line) == 0:
                break

            r, c = int(line[1]), int(line[2])
            cellnum = (r - 1) * NCOL + c
            seg_rch = "{0} {1}; ".format(line[3], line[4])
            flow = 0.5 * (float(line[5]) + float(line[7]))
            loss = float(line[6])
            overland = float(line[8])
            stage = float(line[11])
            depth = float(line[12])

            try:
                existingflow = self.flow_by_cellnum[cellnum]
                seg_rch_info = self.seg_rch_by_cellnum[cellnum]
            except KeyError:
                existingflow = 0
                seg_rch_info = 'segs  rchs: '

            # determine state
            if flow == 0:
                state = 'dry'
            elif loss > 0:
                state = 'losing'
            elif loss < 0:
                state = 'gaining'
            else:
                print 'Stream reach in cell {} has flow, but no interaction with aquifer.'.format(
                    cellnum)

            self.flow_by_cellnum[cellnum] = existingflow + flow
            self.seg_rch_by_cellnum[cellnum] = seg_rch_info + seg_rch
            self.loss_by_cellnum[cellnum] = loss
            self.state_by_cellnum[cellnum] = state
            self.overland_by_cellnum[cellnum] = overland
            self.stage_by_cellnum[cellnum] = stage
            self.depth_by_cellnum[cellnum] = depth

        # write to temporary output file
        ofp = open(os.path.join(self.outpath, 'temp.csv'), 'w')
        ofp.write(
            '{},row,column,seg_reach,flow,loss,overland,state,stage,depth\n'.
            format(self.node_num_attribute))
        for cn in self.flow_by_cellnum.keys():
            ofp.write('{0},{1},{2},"{3}",{4:.6e},{5},{6},{7},{8},{9}\n'.format(
                cn, 1, 1, self.seg_rch_by_cellnum[cn],
                self.flow_by_cellnum[cn], self.loss_by_cellnum[cn],
                self.overland_by_cellnum[cn], self.state_by_cellnum[cn],
                self.stage_by_cellnum[cn], self.depth_by_cellnum[cn]))
        ofp.close()

        outfile = os.path.join(self.outpath,
                               "{0}.shp".format(self.SFR_out[:-4]))
        if use_arcpy:
            try:
                import arcpy
                import SFR_arcpy
            except:
                print 'module arcpy not found!'

            # make feature/table layers
            arcpy.env.workspace = self.outpath
            arcpy.env.overwriteOutput = True
            arcpy.CopyFeatures_management(
                self.streams_shp, self.streams_shp[:-4] + '_backup.shp')
            arcpy.MakeFeatureLayer_management(
                self.streams_shp[:-4] + '_backup.shp', "streams")
            arcpy.CopyRows_management(os.path.join(self.outpath, 'temp.csv'),
                                      os.path.join(self.outpath, 'temp.dbf'))

            # drop all fields except for cellnum from stream linework
            Fields = arcpy.ListFields("streams")
            Fields = [
                f.name for f in Fields
                if f.name not in ["FID", "Shape", self.node_num_attribute]
            ]
            if len(Fields) > 0:
                arcpy.DeleteField_management("streams", Fields)

            SFR_arcpy.general_join(outfile,
                                   "streams",
                                   self.node_num_attribute,
                                   "temp.dbf",
                                   self.node_num_attribute,
                                   keep_common=True)

        else:
            import sys
            sys.path.append('../../GIS_utils')
            try:
                import GISops
            except:
                print 'GIS_utils.GISops not found!'
            GISops.join_csv2shp(self.streams_shp,
                                self.node_num_attribute,
                                os.path.join(self.outpath, 'temp.csv'),
                                self.node_num_attribute,
                                outfile,
                                how='inner')