-
Notifications
You must be signed in to change notification settings - Fork 0
/
HAPILite.py
280 lines (217 loc) · 11.1 KB
/
HAPILite.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
#import standard libraries
import numpy as np
import matplotlib.pyplot as plt
import multiprocessing as mp
from bisect import bisect
#import custom functions
from lib.Constants import *
from lib.LineProfiles import PROFILE_DOPPLER, PROFILE_LORENTZ, PROFILE_VOIGT, PROFILE_PSEUDOVOIGT
from lib.PartitionFunction import BD_TIPS_2017_PYTHON
from lib.MolecularMass import GetMolecularMass
from lib.ReadComputeFunc import GenerateCrossSection
from lib.ErrorParser import MapError
def CalcCrossSection(Database, DataPath = "", Temp=296, P=1, Broadening="Self", \
WN_Grid=np.arange(333.33,33333.33,0.01), OmegaWing=0.0, OmegaWingHW=75.0, \
Profile="VOIGT", NCORES=-1):
"""
Parameters
----------------------------------------------------------------------------
MoleculeNameName: Name of the molecule for which the cross section is to be calculated.
DataLocation: location where the HITRAN data is located.
Temp: The temperature at which the the cross section is to be calculated
P: The pressure at which the cross-section is to be calculated
Broadening: Broadening can either be self or air
OmegaWing: The width of the WaveNumber for calculating the cross-section
OmegaWingHW: The full width half maximum for calculating the cross-section
WN_Grid: The range of wavenumber over which to calculcate the cross-section. The default value is roughly the range of the JWST spectral range.
Profile: Can be Doppler, Lorentz or Voigt.
NCORES: Use the number of cores for calculating the cross-section.
----------------------------------------------------------------------------
"""
MoleculeNumberDB, IsoNumberDB, LineCenterDB, LineIntensityDB, LowerStateEnergyDB, \
GammaSelf, GammaAir, DeltaAir, TempRatioPower, ErrorArray = Database
#Units --- HITRAN units should be true
factor = 1.0 #(P/9.869233e-7)/(cBolts*Temp) #
#Calculate the partition Function
SigmaT = BD_TIPS_2017_PYTHON(MoleculeNumberDB,IsoNumberDB,Temp)
SigmaTref = BD_TIPS_2017_PYTHON(MoleculeNumberDB,IsoNumberDB,Tref)
if "DOPPLER" in Profile.upper():
#print("Using Doppler Profile")
LINE_PROFILE = PROFILE_DOPPLER
elif "LORENTZ" in Profile.upper():
#print("Using Lorentz Profile")
LINE_PROFILE = PROFILE_LORENTZ
elif "PSEUDOVOIGT" in Profile.upper():
#print("Using the Pseudo-Voigt Profile")
LINE_PROFILE = PROFILE_PSEUDOVOIGT
elif "VOIGT" in Profile.upper():
#print("Using the Voigt Profile")
LINE_PROFILE = PROFILE_VOIGT
else:
raise("No such profile found.")
m = GetMolecularMass(MoleculeNumberDB,IsoNumberDB)*cMassMol*1000.0
#Originate in the line cross-section
Omegas = WN_Grid[:]
Tolerance = 25.0
SelectIndex = np.logical_and(LineCenterDB>min(Omegas)-Tolerance, LineCenterDB<max(Omegas)+Tolerance)
#Now slice the range
LineCenterDB = LineCenterDB[SelectIndex]
LineIntensityDB = LineIntensityDB[SelectIndex]
LowerStateEnergyDB = LowerStateEnergyDB[SelectIndex]
GammaSelf = GammaSelf[SelectIndex]
GammaAir = GammaAir[SelectIndex]
DeltaAir = DeltaAir[SelectIndex]
TempRatioPower = TempRatioPower[SelectIndex]
#check for nans in the values
NLINES = len(LineCenterDB)
Params = [P, Temp, OmegaWing, OmegaWingHW, m, SigmaT, SigmaTref, factor, Broadening]
#how may threads to implement
if NLINES<100 or NCORES==1:
print("Using single core of generating cross-section")
Xsect = GenerateCrossSection(Omegas, LineCenterDB, LineIntensityDB, LowerStateEnergyDB,\
GammaSelf, GammaAir, DeltaAir, TempRatioPower, LINE_PROFILE,\
Params)
return Xsect
else:
if NCORES == -1:
NUMCORES = mp.cpu_count()
elif NCORES>1.99:
NUMCORES = int(NCORES)
#print("Using %d cores for generating cross-section" %NUMCORES)
CPU_Pool = mp.Pool(NUMCORES)
Tasks = []
for i in range(NUMCORES):
StartIndex = int(i*NLINES/NUMCORES)
if i==NUMCORES-1:
StopIndex = -1
else:
StopIndex = int((i+1)*NLINES/NUMCORES)
#Slicing the data
LineCenterDB_Slice = LineCenterDB[StartIndex:StopIndex]
LineIntensityDB_Slice = LineIntensityDB[StartIndex:StopIndex]
LowerStateEnergyDB_Slice = LowerStateEnergyDB[StartIndex:StopIndex]
GammaSelf_Slice = GammaSelf[StartIndex:StopIndex]
GammaAir_Slice = GammaAir[StartIndex:StopIndex]
DeltaAir_Slice = DeltaAir[StartIndex:StopIndex]
TempRatioPower_Slice = TempRatioPower[StartIndex:StopIndex]
Tasks.append(CPU_Pool.apply_async(GenerateCrossSection, (Omegas, LineCenterDB_Slice, \
LineIntensityDB_Slice, LowerStateEnergyDB_Slice,\
GammaSelf_Slice, GammaAir_Slice, DeltaAir_Slice,\
TempRatioPower_Slice, LINE_PROFILE, Params)))
CPU_Pool.close()
CPU_Pool.join()
Xsect = np.zeros(len(Omegas))
for task in Tasks:
Xsect+=task.get()
return Xsect
return Xsect
def CalcCrossSectionWithError(Database, DataPath = "", Temp=296.0, P=1.0, Broadening="Self", \
WN_Grid=np.arange(0,15000,0.01), OmegaWing=0.0, OmegaWingHW=75.0,\
Profile="VOIGT", NCORES=-1, Err="0Sig"):
"""
Parameters
----------------------------------------------------------------------------
MoleculeNameName: Name of the molecule for which the cross section is to be calculated.
DataLocation: location where the HITRAN data is located.
Temp: The temperature at which the the cross section is to be calculated
P: The pressure at which the cross-section is to be calculated
Broadening:The pressure broadening can be self or other method.
OmegaWing: The width of the WaveNumber for calculating the cross-section
OmegaWingHW: The full width half maximum for calculating the cross-section
WN_Grid: The range of wavenumber over which to calculcate the cross-section
Profile: Can be Doppler, Lorentz or Voigt.
NCORES: Use the number of cores for calculating the cross-section.
Err: The error can be 0Sig, +1Sig, +2Sig, -1Sig, -2Sig
----------------------------------------------------------------------------
"""
#Considering the error
if Err.upper() == "0SIG":
ErrorSTD = 0.0
elif "-1SIG" in Err.upper():
ErrorSTD = -1.0
elif "-2SIG" in Err.upper():
ErrorSTD = -2.0
elif "1SIG" in Err.upper():
ErrorSTD = 1.0
elif "2SIG" in Err.upper():
ErrorSTD = 2.0
else:
print("The value of Err is given by:", Err)
raise Exception("Error in HAPILite.py. The values allowed for Err are 0SIG, 1SIG, 2SIG, -1SIG, -2SIG.")
#Unpack the database
MoleculeNumberDB, IsoNumberDB, LineCenterDB, LineIntensityDB, LowerStateEnergyDB, \
GammaSelf, GammaAir, DeltaAir, TempRatioPower, ErrorArray = Database
ErrorValues = MapError(ErrorArray)
#Replace nan with zero
NLINES = len(LineCenterDB)
#Units --- Not HITRAN Units
factor = 1.0#(P/9.869233e-7)/(cBolts*Temp) #
#Calculate the partition Function
SigmaT = BD_TIPS_2017_PYTHON(MoleculeNumberDB,IsoNumberDB,Temp)
SigmaTref = BD_TIPS_2017_PYTHON(MoleculeNumberDB,IsoNumberDB,Tref)
if "DOPPLER" in Profile.upper():
#print("Using Doppler Profile")
LINE_PROFILE = PROFILE_DOPPLER
elif "LORENTZ" in Profile.upper():
#print("Using Lorentz Profile")
LINE_PROFILE = PROFILE_LORENTZ
elif "PSEUDOVOIGT" in Profile.upper():
#print("Using the Pseudo-Voigt Profile")
LINE_PROFILE = PROFILE_PSEUDOVOIGT
elif "VOIGT" in Profile.upper():
#print("Using the Voigt Profile")
LINE_PROFILE = PROFILE_VOIGT
else:
raise("No such profile found.")
m = GetMolecularMass(MoleculeNumberDB,IsoNumberDB)*cMassMol*1000.0
#Originate in the line cross-section
Omegas = WN_Grid[:]
Tolerance = max(OmegaWing, OmegaWingHW*0.25)
SelectIndex = np.logical_and(LineCenterDB>min(Omegas)-Tolerance, LineCenterDB<max(Omegas)+Tolerance)
#Now slice the range
LineCenterDB = LineCenterDB[SelectIndex]+ErrorValues[SelectIndex,0]*ErrorSTD
LineIntensityDB = LineIntensityDB[SelectIndex]*(1.0+ErrorValues[SelectIndex,1]*ErrorSTD)
LowerStateEnergyDB = LowerStateEnergyDB[SelectIndex]
GammaSelf = GammaSelf[SelectIndex]*(1.0+ErrorValues[SelectIndex,3]*ErrorSTD)
GammaAir = GammaAir[SelectIndex]*(1.0+ErrorValues[SelectIndex,2]*ErrorSTD)
DeltaAir = DeltaAir[SelectIndex]*(1.0+ErrorValues[SelectIndex,5]*ErrorSTD)
TempRatioPower = TempRatioPower[SelectIndex]*(1.0+ErrorValues[SelectIndex,4]*ErrorSTD)
Params = [P, Temp, OmegaWing, OmegaWingHW, m, SigmaT, SigmaTref, factor, Broadening]
#how may threads to implement
if NCORES==1:
#print("Using single core of generating cross-section")
Xsect = GenerateCrossSection(Omegas, LineCenterDB, LineIntensityDB, LowerStateEnergyDB, GammaSelf, TempRatioPower, LINE_PROFILE, Params)
return Xsect
else:
if NCORES == -1:
NUMCORES = mp.cpu_count()
elif NCORES>1.99:
NUMCORES = int(NCORES)
#print("Using %d cores for generating cross-section" %NUMCORES)
CPU_Pool = mp.Pool(NUMCORES)
Tasks = []
for i in range(NUMCORES):
StartIndex = int(i*NLINES/NUMCORES)
if i==NUMCORES-1:
StopIndex = -1
else:
StopIndex = int((i+1)*NLINES/NUMCORES)
#Slicing the data
LineCenterDB_Slice = LineCenterDB[StartIndex:StopIndex]
LineIntensityDB_Slice = LineIntensityDB[StartIndex:StopIndex]
LowerStateEnergyDB_Slice = LowerStateEnergyDB[StartIndex:StopIndex]
GammaSelf_Slice = GammaSelf[StartIndex:StopIndex]
GammaAir_Slice = GammaAir[StartIndex:StopIndex]
DeltaAir_Slice = DeltaAir[StartIndex:StopIndex]
TempRatioPower_Slice = TempRatioPower[StartIndex:StopIndex]
Tasks.append(CPU_Pool.apply_async(GenerateCrossSection, (Omegas, LineCenterDB_Slice, \
LineIntensityDB_Slice, LowerStateEnergyDB_Slice,\
GammaSelf_Slice, GammaAir_Slice, DeltaAir_Slice,\
TempRatioPower_Slice, LINE_PROFILE, Params)))
CPU_Pool.close()
CPU_Pool.join()
Xsect = np.zeros(len(Omegas), dtype=np.float32)
for task in Tasks:
Xsect+=task.get()
return Xsect
return Xsect.astype(np.float32)