def read_DIS(self): DX, DY, NLAY, self.NROW, self.NCOL, i = disutil.read_meta_data(self.SFRdata.MFdis) # get layer tops/bottoms self.layer_elevs = np.zeros((NLAY+1, self.NROW, self.NCOL)) for c in range(NLAY + 1): tmp, i = disutil.read_nrow_ncol_vals(self.SFRdata.MFdis, self.NROW, self.NCOL, 'float', i) self.layer_elevs[c, :, :] = tmp # make dictionary of model top elevations by cellnum for c in range(self.NCOL): for r in range(self.NROW): cellnum = r*self.NCOL + c + 1 self.elevs_by_cellnum[cellnum] = self.layer_elevs[0, r, c]
def read_DIS(self): self.DX, self.DY, self.NLAY, self.NROW, self.NCOL, i = disutil.read_meta_data(self.SFRdata.MFdis) self.DX *= self.mult self.DY = (self.DY * self.mult)[::-1] self.delx = np.append(np.diff(self.DX), np.diff(self.DX)[-1]) # add another spacing on because the vector doesn't include distal edge of grid self.dely = np.append(np.diff(self.DY), np.diff(self.DY)[-1]) # get layer tops/bottoms self.layer_elevs = np.zeros((self.NLAY+1, self.NROW, self.NCOL)) for c in range(self.NLAY + 1): tmp, i = disutil.read_nrow_ncol_vals(self.SFRdata.MFdis, self.NROW, self.NCOL, 'float', i) self.layer_elevs[c, :, :] = tmp # make dictionary of model top elevations by cellnum for c in range(self.NCOL): for r in range(self.NROW): cellnum = r*self.NCOL + c + 1 self.elevs_by_cellnum[cellnum] = self.layer_elevs[0, r, c]
def read_DIS(self): self.DX, self.DY, self.NLAY, self.NROW, self.NCOL, i = disutil.read_meta_data( self.SFRdata.MFdis) self.DX *= self.mult self.DY = (self.DY * self.mult)[::-1] self.delx = np.append( np.diff(self.DX), np.diff(self.DX)[-1] ) # add another spacing on because the vector doesn't include distal edge of grid self.dely = np.append(np.diff(self.DY), np.diff(self.DY)[-1]) # get layer tops/bottoms self.layer_elevs = np.zeros((self.NLAY + 1, self.NROW, self.NCOL)) for c in range(self.NLAY + 1): tmp, i = disutil.read_nrow_ncol_vals(self.SFRdata.MFdis, self.NROW, self.NCOL, 'float', i) self.layer_elevs[c, :, :] = tmp # make dictionary of model top elevations by cellnum for c in range(self.NCOL): for r in range(self.NROW): cellnum = r * self.NCOL + c + 1 self.elevs_by_cellnum[cellnum] = self.layer_elevs[0, r, c]
''' import sys DISutils_path = 'D:\\ATLData\\Documents\\GitHub\\SFR' if DISutils_path not in sys.path: sys.path.append(DISutils_path) import discomb_utilities import os import numpy as np GHBfile = 'D:\\ATLData\\BadRiver\\Calibration_base\\BadRiver_GHB.tpl' DISfile = 'D:\\ATLData\\Documents\\GitHub\\SFR\\BadRiver.dis' # read in DIS information DX, DY, nlay, nrows, ncols, i = discomb_utilities.read_meta_data(DISfile) layer_elevs = np.zeros((nlay+1, nrows, ncols)) for c in range(nlay + 1): tmp, i = discomb_utilities.read_nrow_ncol_vals(DISfile, nrows, ncols, 'float', i) layer_elevs[c, :, :] = tmp # read in GHBfile header = 4 indat = open(GHBfile).readlines() if "ptf" in indat[0]: header += 1 # write new GHB file ofp = open(GHBfile+'_new', 'w') logfile = open(os.path.split(GHBfile)[0]+'\\fix_GHB_log.txt', 'w')
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')
def __init__(self, iupseg, outseg): self.reaches = dict() self.iupseg = iupseg self.outseg = outseg self.numreach = -999 self.dist_outseg = -999 self.outreach_row = -999 self.outreach_col = -999 self.inreach_row = -999 self.inreach_col = -999 self.dist_reaches = [] self.circular = 0 # first read in the model top and all layer elevations DX, DY, NLAY, NROW, NCOL, i = disutil.read_meta_data(indis) deltaX = np.max(np.diff(DX)) deltaY = np.max(np.diff(DY)) print '>>>>>>>>>>>>> All Layer elevations read in <<<<<<<<<<<<<' # read in the SFR information from inmat1 --> reaches print '>>> read in SFR information from %s' % inmat1 SFRdata1 = np.genfromtxt(inmat1, dtype=None, names=True, delimiter=',') # read in the SFR information from inmat1 --> segments print '>>> read in SFR information from %s' % inmat2 SFRdata2 = np.genfromtxt(inmat2, dtype=None, names=True, delimiter=',') numreaches = len(SFRdata1) numsegs = len(SFRdata2)
lengths = defaultdict(list) raw_elevations = defaultdict(list) min_max_elevations = defaultdict(list) for seg in new_segs: lengths[seg] = list(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').LengthFt) raw_elevations[seg] = list(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').DEM_elev) min_max_elevations[seg] = [np.min(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').Elevmin), np.max(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').Elevmax)] # smooth interior elevations elevations_sm, slopes = sm.connect_downhill(new_segs, lengths, raw_elevations, min_max_elevations, ofp) elevations_sm = raw_elevations ''' # read in elevations from dis file DX, DY, NLAY, NROW, NCOL, i = disutil.read_meta_data(DISfile) topdata, i = disutil.read_nrow_ncol_vals(DISfile, NROW, NCOL, np.float, i) print "writing {}...".format(Mat1_updated) # lookup row column from node number X, Y = np.meshgrid(np.arange(1, NCOL+1), np.arange(1, NROW+1)) cr = np.vstack([X.ravel(), Y.ravel()]) Mat1_new_dict = {} # build dict of new_segs (parallel structure to exisiting SFR dataframe) for cell in new_streamcells_df.index: if new_streamcells_df.ix[cell, 'Segment'] > 0: node = new_streamcells_df.ix[cell, MFgrid_node_attribute] c, r = cr[:, node-1] # headache! -1 needed to translate from base=1 to base=0 #sbtop = topdata[r, c] # get streambed top from model top
lengths = defaultdict(list) raw_elevations = defaultdict(list) min_max_elevations = defaultdict(list) for seg in new_segs: lengths[seg] = list(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').LengthFt) raw_elevations[seg] = list(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').DEM_elev) min_max_elevations[seg] = [np.min(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').Elevmin), np.max(new_streamcells_df[new_streamcells_df['Segment'] == seg].sort('Reach').Elevmax)] # smooth interior elevations elevations_sm, slopes = sm.connect_downhill(new_segs, lengths, raw_elevations, min_max_elevations, ofp) elevations_sm = raw_elevations ''' # read in elevations from dis file DX, DY, NLAY, NROW, NCOL, i = disutil.read_meta_data(DISfile) topdata, i = disutil.read_nrow_ncol_vals(DISfile, NROW, NCOL, np.float, i) print "writing {}...".format(Mat1_updated) # lookup row column from node number X, Y = np.meshgrid(np.arange(1, NCOL + 1), np.arange(1, NROW + 1)) cr = np.vstack([X.ravel(), Y.ravel()]) Mat1_new_dict = {} # build dict of new_segs (parallel structure to exisiting SFR dataframe) for cell in new_streamcells_df.index: if new_streamcells_df.ix[cell, 'Segment'] > 0: node = new_streamcells_df.ix[cell, MFgrid_node_attribute] c, r = cr[:, node - 1] # headache! -1 needed to translate from base=1 to base=0
requires discomb_utilities (for reading MODFLOW DIS file) ''' import sys DISutils_path = 'D:\\ATLData\\Documents\\GitHub\\SFR' if DISutils_path not in sys.path: sys.path.append(DISutils_path) import discomb_utilities import os import numpy as np GHBfile = 'D:\\ATLData\\BadRiver\\Calibration_base\\BadRiver_GHB.tpl' DISfile = 'D:\\ATLData\\Documents\\GitHub\\SFR\\BadRiver.dis' # read in DIS information DX, DY, nlay, nrows, ncols, i = discomb_utilities.read_meta_data(DISfile) layer_elevs = np.zeros((nlay + 1, nrows, ncols)) for c in range(nlay + 1): tmp, i = discomb_utilities.read_nrow_ncol_vals(DISfile, nrows, ncols, 'float', i) layer_elevs[c, :, :] = tmp # read in GHBfile header = 4 indat = open(GHBfile).readlines() if "ptf" in indat[0]: header += 1 # write new GHB file ofp = open(GHBfile + '_new', 'w')
ofp2.write("zero or negative slope at segment "+str(segnum)+' reach '+str(reach)+'! Reassigned a value of ' +str(slope_min)+'\n') # slope is likely 0 because NHD segment end points are equal; assign arbitrary slope slope=slope_min STOP2_up=STOP2[reach-1]-0.5*reach_lengths[reach-1]*slope slopes.append(slope) STOP2ups.append(STOP2_up) slopesdict[segnum]=slopes ofp.close() ofp2.close() print "saving new streamtop elevations and slopes to GWV_SFRmat1.dat file" print "Appending cellnum onto lines" print "reading in NROW, NCOL, etc from dis file: %s" %MFdis DX,DY,NLAY,NROW,NCOL,foo = dis_util.read_meta_data(MFdis) input_file=open(GWVmat1old).readlines() ofp=open(outfile,'w') ofp.write('%s,%s\n' %(input_file[0].strip(),'cellnum')) for line in input_file[1:]: line = line.strip().split(',') # add cellnum for later evaluation in Arc line.append((int(line[1])-1)*NCOL + int(line[0])) segment=int(line[6]) if segment in STOP2dict.keys(): reach=int(line[5]) linestart=','.join(map(str,line[:3])) linemid=','.join(map(str,line[5:-3])) lineend=','.join(map(str,line[-2:])) STAGE=STOP2dict[segment][reach-1]+1 STOP=STOP2dict[segment][reach-1]
if inpars.findall('.//options/watertable')[0].text.lower() == "on": watertable = "On" HDSfile=inpars.findall('.//WaterTableSettings/HDS_file')[0].text WTcolor=iinpars.findall('.//WaterTableSettings/WTColor')[0].text WTthick=float(inpars.findall('.//WaterTableSettings/WTthickness')[0].text) # read in water table values else: watertable = "Off" try: VE = float(inpars.findall('.//options/VE')[0].text) except: VE = -999 # get dimmensions DX, DY, NLAY, NROW, NCOL, i = discomb_utilities.read_meta_data(DISfile) if Xunits=='miles': DX,DY = DX/5280.0, DY/5280.0 row_interval = interval col_interval = interval # get layer tops/bottoms from DIS file layer_elevs=np.zeros((NLAY+1, NROW, NCOL)) for c in range(NLAY+1): tmp, i = discomb_utilities.read_nrow_ncol_vals(DISfile, NROW, NCOL, 'float', i) layer_elevs[c, :, :] = tmp if watertable.lower()=='on': hds = bf.HeadFile(HDSfile)
import discomb_utilities as disu plot_layers = False plot_lake = True plot_diffs = False burn_in_lakes = True infile_dis = 'MODDANE5_rch_rjh1.dis' infile_lak = 'MODDANE5_rch_rjh1bBEST.lak' # # Read in and handle the discritization file # indat = open(infile_dis,'r').readlines() print 'Reading in the meta data' DX,DY,NLAY,NROW,NCOL,i = disu.read_meta_data(indat) [X,Y]=np.meshgrid(DX,DY) print 'reading the model top and layer bottoms' # Now read the Layer TOP and BOTTOMS vertical_dis = np.zeros((NLAY+1,NROW,NCOL)) # now read the Model TOP print 'reading model top' TMP,i = disu.read_nrow_ncol_vals(indat,NROW,NCOL,np.float,i) vertical_dis[0] = np.flipud(TMP) # now read each of the other layers for k in np.arange(NLAY): clay = k+1 print 'reading bottom of layer --> %d ' %(clay)
r2 = int(inpars.findall('.//end_row')[0].text) c1 = int(inpars.findall('.//start_col')[0].text) -1 c2 = int(inpars.findall('.//end_col')[0].text) except: r1, r2 = 0, np.shape(rf)[1] c1, c2 = 0, np.shape(rf)[2] # set bounds for vector field r1, c1 = r1 + int(0.5*interval), c1 + int(0.5*interval) # make GIS folder if there isn't one if not os.path.isdir(path): os.makedirs(path) # read in model information from DIS and SPC files DX,DY,nlay,nrows,ncols,i=discomb_utilities.read_meta_data(os.path.join(path,disfile)) cellsize = DX[1] # grid must be uniform for ascii!!! # get layer tops/bottoms layer_elevs=np.zeros((nlay+1,nrows,ncols)) for c in range(nlay+1): tmp,i=discomb_utilities.read_nrow_ncol_vals(os.path.join(path,disfile),nrows,ncols,'float',i) layer_elevs[c,:,:]=tmp # make a meshgrid for each vector location X = cellsize*Coords_multiplier*(np.arange(ncols)+0.5)[c1:c2:interval] + originX Y = cellsize*Coords_multiplier*(np.arange(nrows)+0.5) + originY Y = np.flipud(Y)[r1:r2:interval] # need -0.5 above because of the flipud!!! xy = np.meshgrid(X,Y) cellnums = np.reshape(np.arange(nrows*ncols)+1, (ncols, nrows))[c1:c2:interval, r1:r2:interval]
np.savetxt(fname, array, fmt='%.6e', delimiter=' ', header=ascii_header, comments='') print fname def read_heads(path,headsfile): try: hds=bf.HeadFile(os.path.join(path,headsfile)) heads = hds.get_data(kstp=1, kper=1) except: print "\nError: Cannot read binary head-save file! {0}".format(os.path.join(path,headsfile)) quit() return heads ########### Main Program ###################################################### # read in model information from DIS and SPC files DX,DY,nlay,nrows,ncols,i=discomb_utilities.read_meta_data(os.path.join(path,disfile)) # get layer tops/bottoms layer_elevs=np.zeros((nlay+1,nrows,ncols)) for c in range(nlay+1): tmp,i=discomb_utilities.read_nrow_ncol_vals(os.path.join(path,disfile),nrows,ncols,'float',i) layer_elevs[c,:,:]=tmp # get list of K files Kfiles=[f for f in os.listdir(path) if '._k' in f] # initialize output pdf for R and K arrays pdf=PdfPages(outpdf_kp) print '\nsaving plots to %s:' %(outpdf_kp) print Rchfile,
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')
class segment: def __init__(self,iupseg,outseg): self.reaches = dict() self.iupseg = iupseg self.outseg = outseg self.numreach = -999 self.dist_outseg = -999 self.outreach_row = -999 self.outreach_col = -999 self.inreach_row = -999 self.inreach_col = -999 self.dist_reaches = [] self.circular = 0 # first read in the model top and all layer elevations DX,DY,NLAY,NROW,NCOL,i = disutil.read_meta_data(indis) deltaX = np.max(np.diff(DX)) deltaY = np.max(np.diff(DY)) print '>>>>>>>>>>>>> All Layer elevations read in <<<<<<<<<<<<<' # read in the SFR information from inmat1 --> reaches print '>>> read in SFR information from %s' %inmat1 SFRdata1 = np.genfromtxt(inmat1,dtype=None, names=True, delimiter = ',') # read in the SFR information from inmat1 --> segments print '>>> read in SFR information from %s' %inmat2 SFRdata2 = np.genfromtxt(inmat2,dtype=None, names=True, delimiter = ',') numreaches = len(SFRdata1) numsegs = len(SFRdata2)