forked from DOI-USGS/sfrmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssign_layers.py
More file actions
executable file
·152 lines (131 loc) · 5.51 KB
/
Assign_layers.py
File metadata and controls
executable file
·152 lines (131 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Program to assign layers to SFR cells based on top of streambed - streambed thickness
import numpy as np
from collections import defaultdict
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
# Input files
#need to make these filepaths relative
botsfile='BOTTOMS.dat' # GWV mat with bottom elevations for all layers
L1top='model_top.dat' # GWV mat with top elevations for layer 1
SFRmat1='SFR_GWVmat1.txt' # SFR matrix 1 from Howard Reeve's scripts
SFRmat2='SFR_GWVmat2.txt'
# Settings
thick=3 # Uniform streambed thickness
buff=1 # if STOP - thick - buffer < bottom of cell, move to layer below
Lowerbot=True # if True lowers model bottom where SFR drops below
minimum_slope=1e-6
# Outputfiles
SFRpackage='NACP2.SFR'
SFRout=SFRmat1[:-4]+'_Layers_assigned.DAT'
Layerinfo='SFR_layer_assignments.txt'
BotcorPDF='Model_bottom_adjustments.pdf' # PDF showing model bottom before and after adjustments if Lowerbot==True
print "Getting model grid elevations..."
temp=open(L1top).readlines()
ncols=len(temp[0].strip().split())
nrows=len(temp)
topdata=np.fromfile(L1top,sep=' ')
topdata=np.reshape(topdata,(nrows,ncols))
bots=np.fromfile(botsfile,sep=' ')
nlayers=int(len(bots)/np.size(topdata))
bots_rs=bots.reshape(nlayers,nrows,ncols) # apparently np convention is l,r,c
SFRinfo=np.genfromtxt(SFRmat1,delimiter=',',names=True,dtype=None)
print "Assigning layers to SFR cells..."
below_bottom=open('below_bot.csv','w')
below_bottom.write('SFRbot,ModelBot,Land_surf,cellnum,segment\n')
below_botdepths=defaultdict() # list row,column locations where SFR goes below model bottom
nbelow=0
New_Layers=[]
for i in range(len(SFRinfo)):
r=int(SFRinfo['row'][i])
c=int(SFRinfo['column'][i])
l=int(SFRinfo['layer'][i])
STOP=float(SFRinfo['top_streambed'][i])
cellbottoms=list(bots_rs[:,r-1,c-1])
for b in range(nlayers):
SFRbot=STOP-thick-buff
if (SFRbot)<cellbottoms[b]:
if b+1 < nlayers:
continue
else:
print 'Streambottom elevation=%s, Model bottom=%s at row %s, column %s, cellnum %s' %(SFRbot,cellbottoms[-1],r,c,(r-1)*ncols+c)
print 'Land surface is %s' %(topdata[r-1,c-1])
below_bottom.write('%s,%s,%s,%s,%s\n' %(SFRbot,cellbottoms[-1],topdata[r-1,c-1],(r-1)*ncols+c,SFRinfo['segment'][i]))
below_botdepths[(r-1,c-1)]=cellbottoms[-1]-SFRbot # difference between SFR bottom and model bottom
nbelow+=1
New_Layers.append(b+1)
else:
New_Layers.append(b+1)
break
below_bottom.close()
New_Layers=np.array(New_Layers)
botsnew=np.ndarray(shape=np.shape(topdata))
if Lowerbot:
print "\n\nAdjusting model bottom to accomdate SFR cells that were below..."
print "see %s\n" %(BotcorPDF)
for r in range(nrows):
for c in range(ncols):
if (r,c) in below_botdepths.keys():
print bots_rs[-1,r,c]
botsnew[r,c]=bots_rs[-1,r,c]-below_botdepths[(r,c)]
print botsnew[r,c]
else:
botsnew[r,c]=bots_rs[-1,r,c]
outarray=botsfile[:-4]+'_SFRcorr.dat'
bots_rs=np.append(bots_rs[:-1,:,:],botsnew)
bots_rs=np.reshape(bots_rs,(nlayers,nrows,ncols))
with file(outarray, 'w') as outfile:
for slice_2d in bots_rs:
np.savetxt(outfile,slice_2d,fmt='%.2f')
outfile.close()
# show bottom before and after corrections
outpdf=PdfPages(BotcorPDF)
for mat in [bots_rs[-1,:,:],botsnew]:
plt.figure()
plt.imshow(mat)
plt.colorbar()
outpdf.savefig()
outpdf.close()
# histogram of layer assignments
freq=np.histogram(list(New_Layers),range(nlayers+2)[1:])
print "\nWriting output to %s..." %(SFRout)
ofp=open(SFRout,'w')
ofp.write(','.join(SFRinfo.dtype.names)+'\n')
for i in range(len(SFRinfo)):
line=list(SFRinfo[i])[0:2]+[New_Layers[i]]+list(SFRinfo[i])[3:]
line=','.join(map(str,line))
ofp.write(line+'\n')
ofp.close()
# writeout results to SFR package file
icalc=1
nreaches=len(SFRinfo)
nseg=np.max(SFRinfo['segment'])
SFRinfo2=np.genfromtxt(SFRmat2,names=True,delimiter=',',dtype=None)
ofp=open(SFRpackage,'w')
ofp.write("#SFRpackage file generated by Assign_Layers.py\n")
ofp.write('%s %s 0 0 128390.4 0.0001 50 53 1 0 0 0\n' %(-1*nreaches,nseg,))
for i in range(len(SFRinfo)):
slope=SFRinfo['bed_slope'][i]
if slope<=minimum_slope: # one last check for negative or zero slopes
slope=minimum_slope
ofp.write('%s %s %s %s %s %s %s %s %s %s\n' %(New_Layers[i],SFRinfo['row'][i],SFRinfo['column'][i],SFRinfo['segment'][i],SFRinfo['reach'][i],SFRinfo['length_in_cell'][i],SFRinfo['top_streambed'][i],slope,thick,SFRinfo['bed_K'][i]))
ofp.write('%s 0 0 0\n' %(nseg))
for i in range(len(SFRinfo2)):
segment=SFRinfo2['segment'][i]
seg_Mat1_inds=np.where(SFRinfo['segment']==segment)
seginfo=SFRinfo[seg_Mat1_inds]
ofp.write('%s %s %s 0 0.0 0.0 0.0 0.0 %s\n' %(segment,icalc,SFRinfo2['outseg'][i],SFRinfo2['roughch'][i]))
ofp.write('%s\n' %(seginfo['width_in_cell'][0]))
ofp.write('%s\n' %(seginfo['width_in_cell'][-1]))
ofp.close()
# writeout info on Layer assignments
ofp=open(Layerinfo,'w')
ofp.write('Layer\t\tNumber of assigned reaches\n')
print '\nLayer assignments:'
for i in range(nlayers):
ofp.write('%s\t\t%s\n' %(freq[1][i],freq[0][i]))
print '%s\t\t%s\n' %(freq[1][i],freq[0][i])
ofp.close()
if not Lowerbot:
if nbelow>0:
print "Warning %s SFR streambed bottoms were below the model bottom. See below_bots.csv" %(nbelow)
print "Done!"