-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmolecule_TAPE3.py
executable file
·278 lines (228 loc) · 8.34 KB
/
molecule_TAPE3.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
#!/usr/bin/env python
# for Python 3 compatibility
from __future__ import print_function
import os, sys, glob, argparse
import subprocess as sub
sys.path.append('/home/rpernak/python/GIT_python_modules')
import utils
# GLOBAL VARIABLES
DEFBUILDDIR = '/nas/ASR/LINEFILE_BUILD_TES_V_2.0'
DEFLNFL = \
'/nas/project/rc/rc1/lnfl_local_version/lnfl_v3.1_linux_pgi_sgl'
class genTAPE3():
"""
Assuming that line files exist for each HITRAN molecule, generate
an associated binary TAPE3
"""
def __init__(self, lineFileDir=DEFBUILDDIR, \
lnflPath=DEFLNFL, wnBounds=[0.0, 200000.0], \
tape5Dir='%s/LNFL_input_files' % DEFBUILDDIR, \
tape3Dir='%s/lnfl/TAPE3_files' % DEFBUILDDIR, \
wvIso=False):
"""
Constructor for genTAPE3 class -- where are ASCII line files,
what are the executable paths, where should output files go, etc.
Inputs
Outputs
Keywords
lineFileDir -- string, full path to line file build directory
(where construction of a new line file is done
before releasing it); it is assumed that line_file/,
line_files_By_Molecule/, and lnfl/ subdirectories exist
underneath it
lnflPath -- string, full path to LNFL executable to use
wnBounds -- float array, 2-element array of starting and ending
wavenumbers for the TAPE3 files (25 cm-1 will be added to
each bound to include "relevant" line contributions)
tape5Dir -- string, path to which TAPE5 files that will be
generated are written
tape3Dir -- string, path to which TAPE3 files that will be
generated are written
wvIso -- boolean, only process the water vapor isotopologues
(each of which has a separate line file)
"""
lfMolDir = '%s/line_files_By_Molecule' % lineFileDir
pathCheck = [lineFileDir, lfMolDir, lnflPath]
for path in pathCheck: utils.file_check(path)
# set some attributes necessary for the rest of the processing
self.lineFileDir = str(lineFileDir)
self.pathLNFL = str(lnflPath)
self.mols = sorted(glob.glob('%s/*' % lfMolDir))
self.nMols = len(self.mols)
self.dirT5 = str(tape5Dir)
self.dirT3 = str(tape3Dir)
self.wnLims = list(wnBounds)
self.isoH2O = bool(wvIso)
# do the output dirs exist?
for path in [tape5Dir, tape3Dir]: self.makeDirs(path)
# end constructor
def makeDirs(self, inPath):
"""
Make a directory if it doesn't already exist
"""
if not os.path.exists(inPath): os.mkdir(inPath)
# end makeDirs()
def cleanUp(self):
"""
Remove any TAPE files that were generated
"""
tapeList = sorted(glob.glob('TAPE?'))
tapeList = ['TAPE%d' % num for num in [1, 2, 5, 6, 7, 10]]
for tape in tapeList:
if os.path.isfile(tape): os.remove(tape)
# end TAPE loop
# end cleanUp
def makeLinks(self, source, target):
"""
Make symbolic links
Inputs
source -- string, path to file to be linked
target -- string, name associated with link
"""
if os.path.exists(target): os.unlink(target)
os.symlink(source, target)
# end makeLinks()
def makeTAPE5(self):
"""
Make the TAPE5 LNFL specifications file for each molecule
"""
wn1, wn2 = self.wnLims
# loop through each HITRAN molecule and create an associated TAPE5
allT5 = []
for iMol, mol in enumerate(self.mols):
base = os.path.basename(mol)
print(base)
tape5 = 'TAPE5_%s' % base
# LNFL TAPE5 records
# (see lnfl_instructions document in LNFL release)
rec1 = '$ %s' % base
rec2 = '%10.3f%10.3f' % (wn1-25, wn2+25)
# start off with all molecules off, then turn iMol on, then
# generate a single string instead of a list of characters
# and append
rec3 = ['0'] * self.nMols
rec3[iMol] = '1'
rec3 = ''.join(rec3) + ' NBLK1 NOCPL LNOUT '
end = '%%%%%'
outDat = [rec1, rec2]
# line coupling molecules
if base in ['02_CO2', '06_CH4', '07_O2']:
rec3 = rec3.replace('NOCPL', 'MRG2')
rec4 = [' '] * self.nMols
rec4[iMol] = '1'
rec4 = ''.join(rec4)
outDat += [rec3, rec4]
else:
outDat.append(rec3)
# endif coupling
outDat.append(end)
# now write TAPE5
outFP = open(tape5, 'w')
for line in outDat: outFP.write('%s\n' % line)
outFP.close()
# copy TAPE5 to subdirectory for molecule in buildDir
target = '%s/%s' % (self.dirT5, tape5)
if os.path.exists(target):
print('WARNING: overwriting %s' % target)
# endif target check
os.rename(tape5, target)
allT5.append(target)
# end molecule loop
self.allT5 = list(allT5)
return self
# end makeTAPE5()
def runLNFL(self):
"""
Run LNFL for each molecule, thus generating a binary TAPE3
"""
if 'allT5' not in dir(self):
self.allT5 = sorted(glob.glob('%s/TAPE5_*' % self.dirT5))
# set up the input directory
self.makeLinks(self.pathLNFL, 'lnfl')
tapeStrList = ['TAPE1', 'TAPE5']
self.cleanUp()
# loop through each HITRAN molecule and create an associated TAPE5
for iMol, mol in enumerate(self.mols):
base = os.path.basename(mol)
print(base)
tape5 = self.allT5[iMol]
if self.isoH2O:
# there are multiple line files to consider for H2O
isoStr = ['01_h2o_161_only', '01_h2o_162_excl', \
'01_h2o_162_only', '01_h2o_171_only', '01_h2o_172_only', \
'01_h2o_181_only', '01_h2o_182_only']
tape1List = ['%s/%s' % (mol, iso) for iso in isoStr]
else:
tape1List = ['%s/%s' % (mol, base)]
# endif WV
# loop really only exists for H2O
for tape1 in tape1List:
tapeList = [tape1, tape5]
# grab the line coupling file if necessary
if base in ['02_CO2', '06_CH4', '07_O2']:
tape2 = '%s/lncpl_lines' % mol
tapeList.append(tape2)
tapeStrList.append('TAPE2')
# endif line coupling
# stage the files necessary for an LNFL run
for source, target in zip(tapeList, tapeStrList):
self.makeLinks(source, target)
# call LNFL and save TAPE3 to unique name
sub.call(['lnfl'])
if self.isoH2O:
tape3 = '%s/TAPE3_%s' % (mol, os.path.basename(tape1))
else:
tape3 = '%s/TAPE3_%s' % (mol, base)
# endif wv
if os.path.exists(tape3):
print('WARNING: overwriting %s' % tape3)
os.rename('TAPE3', tape3)
# clean up
self.cleanUp()
# end TAPE1 loop
#self.cleanUp()
# if we're doing WV isotopologues, *only* do them
if self.isoH2O: return
# end molecule loop
return
# end runLNFL()
# end genTAPE3
if __name__ == '__main__':
parser = argparse.ArgumentParser(\
description='Generate a TAPE5 for each HITRAN molecule.')
parser.add_argument('-dir', '--build_dir', type=str, \
default=DEFBUILDDIR, \
help='Line file build directory (which we assume contains a ' + \
'"line_files_by_Molecule/" and "LNFL_input_files" subdirs.)')
parser.add_argument('-lnfl', '--lnfl_path', type=str, \
default=DEFLNFL, \
help='Full path to LNFL executable to be used.')
parser.add_argument('-t5', '--tape5_dir', type=str, \
default='TAPE5_files', help='Directory for TAPE5 files.')
parser.add_argument('-t3', '--tape3_dir', type=str, \
default='TAPE3_files', \
help='Relative path to directory for resulting TAPE3 files.')
parser.add_argument('-wv', '--water_vapor', action='store_true', \
help='Process the separate water vapor files (i.e., one for ' + \
'each isotopologue that the JPL team wants).')
parser.add_argument('-wn', '--wavenum_lims', nargs=2, \
type=float, default=[500, 3500], \
help='Wavenumber limits (cm-1) for TAPE3 (after LNFL run)')
parser.add_argument('-5', '--only_tape5', action='store_true', \
help='Only write the TAPE5s (no LNFL run).')
parser.add_argument('-3', '--only_tape3', action='store_true', \
help='Run LNFL without first making the TAPE5s.')
args = parser.parse_args()
tape3 = genTAPE3(lineFileDir=args.build_dir, \
lnflPath=args.lnfl_path, wnBounds=args.wavenum_lims, \
tape5Dir=args.tape5_dir, tape3Dir=args.tape3_dir, \
wvIso=args.water_vapor)
if args.only_tape5:
tape3.makeTAPE5()
elif args.only_tape3:
tape3.runLNFL()
else:
tape3.makeTAPE5()
tape3.runLNFL()
# endif
# endif main()