forked from dan-visioni/feedback_suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.py
executable file
·364 lines (343 loc) · 13.2 KB
/
driver.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Explicit feedback for climate modeling
# Copyright (C) 2020 Ben Kravitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Driver Routines
#
# Written by Ben Kravitz ([email protected] or [email protected])
# Last updated 11 July 2019
#
# This script contains all of the under-the-hood stuff that the explicit feedback
# code suite uses.
#
# This script is written in native python format. Be careful with brackets [],
# white space, and making sure everything that needs to be a string is actually
# a string by putting it in quotes ''. All lines beginning with # are comments.
#########################################################
############### CODE BEGINS HERE ###############
############### MODIFY AT YOUR OWN RISK ###############
#########################################################
import os
import sys
import numpy
import math
import datetime
# Subroutine that converts read in text file lines into a list
def linestolist(linesraw,linelist):
delimiter = ''
for k in range(len(linelist)):
temp = linelist[k]
temp4 = list(temp)
for j in range(len(temp4)):
if temp4[j]=='\n':
del temp4[j]
temp5 = delimiter.join(temp4)
temp6 = temp5.split(',')
linelist[k] = temp6
# Subroutine that converts read in text file lines into a list
def linestolist2(linesraw,linelist):
delimiter = ''
for k in range(len(linelist)):
temp = linelist[k]
temp4 = list(temp)
for j in range(len(temp4)):
if temp4[j]=='\n':
del temp4[j]
temp5 = delimiter.join(temp4)
temp6 = temp5.split(' ')
linelist[k] = temp6
# Turns partially extracted CESM output (netcdf file containing all times but one variable)
# into a numpy array that can be passed to the controller
# infile refers to the absolute path of the file
# Note that this is hard-coded for 2-D fields (i.e., there is currently no capability for
# a vertical dimension).
def readfile(infile,varname):
os.system('ncdump '+infile+' > '+scratchdir+'/temp.txt') # hackish, but it doesn't rely on netcdf python libraries
f=open(scratchdir+'/temp.txt','r')
r=f.readlines()
f.close()
i=r.index('data:\n')
r2=r[i:]
for j in range(len(r2)):
if 'lat' in r2[j]:
c1=j
for j in range(c1,len(r2)):
if ';' in r2[j]:
c2=j
break
latstemp=r2[c1:c2+1]
lats=(''.join((''.join(''.join(latstemp).split('\n'))).split(' '))).split(',')
temp=lats[0].split('=')
lats[0]=temp[1]
temp=lats[-1].split(';')
lats[-1]=temp[0]
for j in range(len(r2)):
if 'lon' in r2[j]:
c1=j
for j in range(c1,len(r2)):
if ';' in r2[j]:
c2=j
break
lonstemp=r2[c1:c2+1]
lons=(''.join((''.join(''.join(lonstemp).split('\n'))).split(' '))).split(',')
temp=lons[0].split('=')
lons[0]=temp[1]
temp=lons[-1].split(';')
lons[-1]=temp[0]
for j in range(len(r2)):
if 'time' in r2[j] and 'bnds' not in r2[j]:
c1=j
for j in range(c1,len(r2)):
if ';' in r2[j]:
c2=j
break
timetemp=r2[c1:c2+1]
time=(''.join((''.join(''.join(timetemp).split('\n'))).split(' '))).split(',')
temp=time[0].split('=')
time[0]=temp[1]
temp=time[-1].split(';')
time[-1]=temp[0]
for j in range(len(r2)):
if varname in r2[j]:
c1=j
for j in range(c1,len(r2)):
if ';' in r2[j]:
c2=j
break
keepvals=r2[c1+1:c2+1]
keepvals2=(''.join((''.join(''.join(keepvals).split('\n'))).split(' '))).split(',')
temp=keepvals2[-1].split(';')
keepvals2[-1]=temp[0]
for j in range(len(keepvals2)):
if keepvals2[j]=='_':
keepvals2[j]=numpy.nan
nlats=len(lats)
nlons=len(lons)
ntimes=len(time)
b=numpy.asarray([float(i) for i in keepvals2])
c=b.reshape((ntimes,nlats,nlons))
lonsarray=numpy.asarray([float(i) for i in lons])
latsarray=numpy.asarray([float(i) for i in lats])
return c,latsarray,lonsarray,time
# Extracts one variable (but all times) from the raw CESM output
# Then calls the readfile subroutine
# Returned values are numpy arrays that can be fed into the controller
def extractvars(varname,varloc,runname,whichnames,archivedir,scratchdir):
os.system('mkdir -p '+scratchdir)
for k in range(len(whichnames)):
fname=whichnames[k]
preamble=len(runname+'.'+varloc+'.')
post=len('.nc')
whichtime=fname[preamble:-post]
os.system('ncks -v '+varname+' '+archivedir+'/'+fname+' '+scratchdir+'/'+varname+'_'+whichtime+'.nc')
os.system('ncrcat '+scratchdir+'/*.nc '+scratchdir+'/'+varname+'_out.nc')
vals,lats,lons,times=readfile(scratchdir+'/'+varname+'_out.nc',varname)
os.system('rm -r '+scratchdir)
return vals,lats,lons,times
# Once the controller returns the namelist values, this replaces/appends those values
# into the 'user_nl_XXX' file
# whichfile describes the filename (like 'user_nl_cam')
# invals is a list of pairs. The first item in the pair is the namelist parameter to
# be modified, and the second item is the new value.
def fixnamelist(whichfile,invals,casepath):
n=len(invals)
paramnames=[]
paramvals=[]
for i in range(n):
if i%2==0:
paramnames.append(invals[i])
else:
paramvals.append(invals[i])
print paramnames
print paramvals
f=file(casepath+'/'+whichfile)
newlines=[]
flags=n/2*[0]
for line in f:
for k in range(n/2):
if paramnames[k] in line:
line=paramnames[k]+'='+paramvals[k]+'\n'
flags[k]=1
newlines.append(line)
for k in range(len(flags)):
if flags[k]==0:
theline=paramnames[k]+'='+paramvals[k]+'\n'
newlines.append(theline)
f.close()
outfile=file(casepath+'/'+whichfile+'_mod','w')
for q in range(len(newlines)):
outfile.write(newlines[q])
outfile.close()
os.system('mv '+casepath+'/'+whichfile+'_mod '+casepath+'/'+whichfile)
# does the same thing as fixnamelist but allows for more generic strings
# it will search for the first item in the invals pair and replace the entire line with
# the second item of the pair
def fixnamelist2(whichfile,invals,casepath):
n=len(invals)
paramnames=[]
paramvals=[]
for i in range(n):
if i%2==0:
paramnames.append(invals[i])
else:
paramvals.append(invals[i])
f=file(casepath+'/'+whichfile)
newlines=[]
flags=n/2*[0]
for line in f:
for k in range(n/2):
if paramnames[k] in line:
line=paramvals[k]
flags[k]=1
newlines.append(line)
for k in range(len(flags)):
if flags[k]==0:
theline=paramvals[k]
newlines.append(theline)
f.close()
outfile=file(casepath+'/'+whichfile+'_mod','w')
for q in range(len(newlines)):
outfile.write(newlines[q])
outfile.close()
os.system('mv '+casepath+'/'+whichfile+'_mod '+casepath+'/'+whichfile)
## This modifies env_run.xml to ensure that it resubmits
#def fixenvrun(casepath):
# cwd=os.getcwd()
# os.chdir(casepath)
# os.system('./xmlchange -file env_run.xml -id RESUBMIT -val '+str(maxrest))
# os.chdir(cwd)
# When messing with days/months, sometimes the leading zero gets cut off. This puts it
# back when appropriate.
def zeropad(innum):
if innum<10:
outnum='0'+str(innum)
else:
outnum=str(innum)
return outnum
# Returns a list of file names that the script is to extract to feed into the above subroutines.
# The purpose of this routine is to figure out which times to extract (as opposed to
# doing everything that's sitting in the archive directory).
# This also calculates whether we have reached the end of the run (as specified by
# maxrest. If so, it returns a flag that will call for termination of the script.
def whichfiles(archivepaths,casepath,runname,frequency,varlocs,maxrest):
stopflag=0
outlist=[]
for k in range(len(varlocs)):
theloc=varlocs[k]
os.system('ls '+archivepaths[k]+'/*.'+varlocs[k]+'.* > '+casepath+'/archivedfiles.txt')
filesfile=open(casepath+'/archivedfiles.txt')
fileslines=filesfile.readlines()
fileslineslist=list(fileslines)
linestolist(fileslines,fileslineslist)
filesfile.close()
os.system('rm '+casepath+'/archivedfiles.txt')
thelist=fileslineslist
preamble=len(archivepaths[k]+'/'+runname+'.'+varlocs[k]+'.')
post=len('-00000.nc') # This is hard-coded. We currently do not support sub-daily timescales.
post=len('.nc')
alltimes=[]
for j in range(len(thelist)):
alltimes.append(thelist[j][0][preamble:-post])
lastone=alltimes[-1]
globalfirstone=alltimes[0]
n=len(lastone)
freqnum=int(frequency[:-1])
freqlet=frequency[-1]
if freqlet=='y':
thedelt=datetime.timedelta(days=freqnum*365)
elif freqlet=='m':
thedelt=datetime.timedelta(days=round(freqnum*365/12)) # Months don't have uniform lengths, but this is good enough.
elif freqlet=='w':
thedelt=datetime.timedelta(weeks=freqnum)
elif freqlet=='d':
thedelt=datetime.timedelta(days=freqnum)
timestoextract=[]
if n==10: # output is daily
theyear=lastone[0:4]
theday=lastone[-2:]
themonth=lastone[5:7]
lastdate=datetime.date(int(theyear),int(themonth),int(theday))
globalfirstdate=datetime.date(int(globalfirstone[0:4]),int(globalfirstone[5:7]),int(globalfirstone[-2:]))
firstdate=lastdate-thedelt+datetime.timedelta(days=1)
firstone=str(firstdate.year)+'-'+zeropad(firstdate.month)+'-'+zeropad(firstdate.day)
i=alltimes.index(firstone)
for q in range(i,len(alltimes)):
timestoextract.append(runname+'.'+varlocs[k]+'.'+alltimes[q]+'.nc')
elif n==7: # output is monthly
theyear=lastone[0:4]
themonth=lastone[-2:]
lastdate=datetime.date(int(theyear),int(themonth),1)
globalfirstdate=datetime.date(int(globalfirstone[0:4]),int(globalfirstone[5:7]),1)
firstdate=lastdate-thedelt+datetime.timedelta(days=31)
firstone=str(firstdate.year)+'-'+zeropad(firstdate.month)
i=alltimes.index(firstone)
for q in range(i,len(alltimes)):
timestoextract.append(runname+'.'+varlocs[k]+'.'+alltimes[q]+'.nc')
elif n==4: # output is annual
lastdate=datetime.date(int(theyear),1,1)
globalfirstdate=datetime.date(int(globalfirstone[0:4]),1,1)
firstdate=lastdate-thedelt+datetime.timedelta(days=365)
firstone=str(firstdate.year)
i=alltimes.index(firstone)
for q in range(i,len(alltimes)):
timestoextract.append(runname+'.'+varlocs[k]+'.'+alltimes[q]+'.nc')
outlist.append(timestoextract)
globaldelt=lastdate-globalfirstdate
if (globaldelt.days/thedelt.days)>datetime.timedelta(maxrest).days:
stopflag=1
return outlist,stopflag
def readlog(logfile):
f=open(logfile,'r')
loglines=f.readlines()
loglist=list(loglines)
linestolist2(loglines,loglist)
f.close()
loglist2=[]
for k in range(len(loglist)):
temp=[]
for j in range(len(loglist[k])):
if loglist[k][j]!='':
temp.append(loglist[k][j])
loglist2.append(temp)
return loglist2
def writelog(logfile,listtowrite):
f=open(logfile,'w')
for item in listtowrite:
for k in range(len(item)):
f.write("%s " % item[k])
f.write('\n')
f.close()
#########################################################
################## MAIN SCRIPT BELOW ##################
#########################################################
# extracts files
filestoextract,stopflag=whichfiles(archivepaths,casepath,runname,frequency,varlocs,maxrest)
if stopflag==1:
sys.exit('End of run reached')
# reads in files, one variable at a time
outvals=[]
for k in range(len(variables)):
vals,lats,lons,times=extractvars(variables[k],varlocs[k],runname,filestoextract[k],archivepaths[k],scratchdir)
outvals.append(vals)
# loads in some common analysis subroutines like area weighting or global mean averaging
execfile(maindir+"/commonroutines.py")
# executes controller
# inputs are outvals, lats, lons, times
# outputs are nlvals
execfile(pathtocontrol)
# modifies CESM namelist
# currently do not have the flexibility to specify more than one namelist file
fixnamelist2('user_nl_cam',nlvals,casepath)
# populates the namelist
os.system(casepath+'/preview_namelists --component atm')