-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
172 lines (150 loc) · 6.62 KB
/
run.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
import os, subprocess, shutil, sys
import argparse
def fixLineListLinks(buildDir,runDir):
# find location of line file in line parameter database
lnflFound = False
for f in os.listdir(buildDir):
if (f.find("aer_v") != -1):
lnflFound = True
break
if not lnflFound:
print("couldn't find Line Parameter Database!")
return
lineFile = f+"/"
print("fixing links to line parameter database files...")
for f in ["co2_co2_brd_param","co2_h2o_brd_param","o2_h2o_brd_param","wv_co2_brd_param"]:
link = os.path.relpath(buildDir+lineFile+"extra_brd_params/"+f,start=runDir)
if os.path.lexists(runDir+f):
os.remove(runDir+f)
os.symlink(link,runDir+f)
link = os.path.relpath(buildDir+lineFile+"spd_dep/"+"spd_dep_param",start=runDir)
if os.path.lexists(runDir+"spd_dep_param"):
os.remove(runDir+"spd_dep_param")
os.symlink(link,runDir+"spd_dep_param")
def createLineList(buildDir,runDir,runName):
print("creating line list...")
# clear TAPE files from runDir
for TAPE in ["TAPE1","TAPE2","TAPE3","TAPE5","TAPE6","TAPE10"]:
if os.path.lexists(runDir+TAPE):
print("clearing "+runDir+TAPE)
os.remove(runDir+TAPE)
# Set up link to AER line file (e.g. aer_v_3.5) that comes in the AER line parameter database
# find location of line file in line parameter database
lnflFound = False
for f in os.listdir(buildDir):
if (f.find("aer_v") != -1):
lnflFound = True
print("assuming "+buildDir+f+"/line_file/"+f+"/ is the line file")
break
if not lnflFound:
print("couldn't find line file in Line Parameter Database!")
return
lineFile = buildDir+f+"/line_file/"+f
# find location of LNFL executable
execFound = False
for f in os.listdir(buildDir+"lnfl/"):
if ((f.find("lnfl_v") != -1) and (f.find("sgl") != -1) and (f[0] != '.')):
execFound = True
print("assuming "+buildDir+"lnfl/"+f+" is the LNFL executable")
break
if not execFound:
print("couldn't find Line file in Line Parameter Database!")
return
lnflExecutable = buildDir+"lnfl/"+f
lnflExecutableLink = os.path.relpath(buildDir+"lnfl/"+f, start=runDir)
# symlink TAPE1
lineFileLink = os.path.relpath(lineFile,start=runDir)
print(lnflExecutableLink)
print(lineFileLink)
print(runDir)
print(runDir+"TAPE1")
os.symlink(lineFileLink,'./'+runDir+"TAPE1")
# symlink TAPE5
TAPE5Found = False
for f in os.listdir(runDir):
if ( (f.find("lnfl") != -1) and (f.find("TAPE5") != -1) ):
TAPE5Name = f
TAPE5Found = True
break
if not TAPE5Found:
print("could not find a TAPE5 with \'lnfl\' in filename")
exit()
TAPE5Link = os.path.relpath(runDir+TAPE5Name,start=runDir)
os.symlink(TAPE5Link,'./'+runDir+"TAPE5")
# fix links to Line Parameter Database files
fixLineListLinks(buildDir,runDir)
# generate line list
print("generating line list")
subprocess.call([lnflExecutableLink,"TAPE1"], cwd=runDir)
# save the output TAPEs with runName
for TAPE in ["TAPE3","TAPE6","TAPE7"]:
if os.path.exists(runDir+TAPE):
print("saving "+TAPE+" as "+runDir+TAPE+"_"+runName)
shutil.move(runDir+TAPE, runDir+TAPE+"_"+runName)
def runLBLRTM(buildDir,runDir,runName):
#set runtype = "lblrtm_7-8um_41000ft_030515UTC15_lat50_elev23_zenh2o7p3um_transm"
# clear TAPE files from runDir
for TAPE in ["TAPE3","TAPE5","TAPE6","TAPE7","TAPE9","TAPE10","TAPE11","TAPE12","TAPE27","TAPE28","TAPE29","TAPE30"]:
if os.path.lexists(runDir+TAPE):
print("clearing "+runDir+TAPE)
os.remove(runDir+TAPE)
# find location of LBLRTM executable
execFound = False
for f in os.listdir(buildDir+"lblrtm/"):
if ((f.find("lblrtm_v") != -1) and (f.find("sgl") != -1)):
execFound = True
print("assuming "+buildDir+"lblrtm/"+f+" is the LBLRTM executable")
break
if not execFound:
print("couldn't find LBLRTM executable")
return
lblrtmExecutable = buildDir+"lblrtm/"+f
lblrtmExecutableLink = os.path.relpath(buildDir+"lblrtm/"+f, start=runDir)
# symlink TAPE3
TAPE3Link = os.path.relpath(runDir+"TAPE3_"+runName,start=runDir)
os.symlink(TAPE3Link,runDir+"TAPE3")
# symlink TAPE5
for f in os.listdir(runDir):
if ( (f.find("lblrtm") != -1) and (f.find("TAPE5") != -1) and (f[0] != '.')):
print("assuming lblrtm's TAPE5 is "+runDir+f)
TAPE5Name = f
TAPE5Found = True
break
if not TAPE5Found:
print("could not find a TAPE5 with \'lblrtm\' in filename")
return
os.symlink(f,runDir+"TAPE5")
# run LBLRTM
print("creating transmission spectrum...")
subprocess.call([lblrtmExecutableLink], cwd=runDir)
# save the output TAPEs with runName
for TAPE in ["TAPE6","TAPE7","TAPE9","TAPE10","TAPE11","TAPE12","TAPE27","TAPE28","TAPE29","TAPE30"]:
if os.path.exists(runDir+TAPE):
print("saving "+TAPE+" as "+runDir+TAPE+"_"+runName)
shutil.move(runDir+TAPE, runDir+TAPE+"_"+runName)
if __name__ == "__main__":
# optional argument behavior (dir required):
# none: runs LNFL and LBLRTM
# --lnfl: runs LNFL only
# --lblrtm: runs LBLRTM only
parser = argparse.ArgumentParser(description='parse command flags')
parser.add_argument('--lblrtm', dest='lblrtm', help="run only LBLRTM to produce model", action='store_true')
parser.add_argument('--lnfl', dest='lnfl', help="run only LNFL to produce linefile for LBLRTM", action='store_true')
parser.add_argument("buildDir", type=str, help="LBLRTM build location")
parser.add_argument("modelDir", type=str, help="LBLRTM model directory containing TAPEs")
parser.set_defaults(lblrtm=False)
parser.set_defaults(lnfl=False)
args = parser.parse_args()
if (args.modelDir[-1] == "/"): # strip trailing / on directory if exists
runDir,runName = os.path.split(args.modelDir[:-1])
else:
runDir,runName = os.path.split(args.modelDir)
print('assuming TAPEs will be in '+runDir+'/'+runName+'/')
buildDir = args.buildDir
if args.lnfl and not args.lblrtm:
createLineList(buildDir,'./'+runDir+'/'+runName+'/',runName)
elif args.lblrtm and not args.lnfl:
runLBLRTM(buildDir,'./'+runDir+'/'+runName+'/',runName)
else:
createLineList(buildDir,runDir+'/'+runName+'/',runName)
runLBLRTM(buildDir,'./'+runDir+'/'+runName+'/',runName)