-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexomol.py
543 lines (453 loc) · 14.6 KB
/
exomol.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# This script downloads and unpacks the *.states, *.pf, *.trans
# and *.def files from wwww.exomol.com.
# And it generates the < species name >.param files
# Date: May 2019
# Author: Simon Grimm
#run with "python3 exomol.py -M id" where id is the name of the molecule
import sys
import os
import subprocess
import numpy as np
import argparse
import math
import exomol2
def getTime(url):
com = "wget -S --spider %s 2>&1 | grep -i Last-Modified" % url
date = str(subprocess.check_output(com, shell=True))
#cut string
date = date.split('Last-Modified:')[1]
date = date.split('\\n')[0]
#convert to timestamp
com = "date -d \'%s\' +%%s" % date
date1 = str(subprocess.check_output(com, shell=True))
date1 = date1.split('b\'')[1]
date1 = date1.split('\\n')[0]
print(date, date1)
return int(date1)
# PrintISO: 1: create the <species>.param files, 0: no <species>.param files are created
# DownloadFiles: 0: no download
# 1: download all files (*.def, *.pf, *.states and *.trans files)
# 2: download only *.def and *.pf files
# 3: download all file for super lines (*.def, *.pf, *.super)
def main(M, DownloadFiles, PrintISO, getTimeStamp, Temp):
if(getTimeStamp == 1 and DownloadFiles == 0):
print("Error, whe using getTimeStamp, then DownloadFiles must be 1 or 2")
exit()
print("Molecule = %s" % M)
P = ""
s = 0 #file range
ntcol = 0 #columns in transition files
npfcol = 0 #columns in partition function file
dL = ""
dn = ""
dg = 0 #number of digits in .trans files ranges
exfile = "Exomol_species.dat"
date = 0
date1 = 0
#check if Exomol file exists and create it otherwise
exists = os.path.isfile(exfile)
if(exists == 0):
exomol2.main()
M0, P0, s0, nn0, dg0 = np.loadtxt(exfile, usecols=(2,3,4,5,6), unpack=True, dtype=np.str)
if(M == "SH_X-X_A-X"):
P = M
s = 0
nn = 1
dg = 0
else:
#M = "1H2-16O__BT2"
P = P0[M0 == M][0]
s = int(s0[M0 == M][0])
nn = int(nn0[M0 == M][0])
dg = int(dg0[M0 == M][0])
if(M == '75As-1H3__CYT18'):
dg = 5
print(M, P, s, nn, dg)
if(DownloadFiles == 2):
#download *.def and *.pf files, don't exit when files are missing
com = "wget http://exomol.com/db/%s/%s.def" % (P, M)
er=os.system(com)
if(er != 0):
print("Error in download .def file")
com = "wget http://exomol.com/db/%s/%s.pf" % (P, M)
er=os.system(com)
if(er != 0):
print("Error in download .pf file")
#return 0
if(DownloadFiles == 1):
#downlaod *.def, *.pf and *.states files, exit when files are missing
com = "wget http://exomol.com/db/%s/%s.def" % (P, M)
er=os.system(com)
if(er != 0):
print("Error in download .def file")
exit()
com = "wget http://exomol.com/db/%s/%s.pf" % (P, M)
er=os.system(com)
if(er != 0):
print("Error in download .pf file")
exit()
print("er", er)
com = "wget http://exomol.com/db/%s/%s.states.bz2" % (P, M)
er=os.system(com)
if(er == 0):
com = "bzip2 -d %s.states.bz2" % M
er=os.system(com)
else:
com = "wget http://exomol.com/db/%s/%s.states" % (P, M)
er=os.system(com)
if(er != 0):
print("Error in download .states file")
exit()
if(DownloadFiles == 3):
#downlaod *.def and *.pf files, exit when files are missing
com = "wget http://exomol.com/db/%s/%s.def" % (P, M)
er=os.system(com)
if(er != 0):
print("Error in download .def file")
exit()
com = "wget http://exomol.com/db/%s/%s.pf" % (P, M)
er=os.system(com)
if(er != 0):
print("Error in download .pf file")
exit()
if(getTimeStamp == 1):
url = "http://exomol.com/db/%s/%s.states.bz2" % (P, M)
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
else:
url = "http://exomol.com/db/%s/%s.states" % (P, M)
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
#check if .pf file exists
exists = os.path.isfile("%s.pf" % M)
if(exists == 0):
print("Error, partition file not found: %s.pf" % M)
return 0
#determine the number of columns in the partition file
with open("%s.pf" % M) as pfFile:
line = pfFile.readline()
npfcol = len(line.split())
print("number of columns in pf file", npfcol)
#check if .def file exists
exists = os.path.isfile("%s.def" % M)
if(exists == 0):
print("Error, definition file not found: %s.def" % M)
if(M == "SH_X-X_A-X"):
n = 1
mass = 32.9798960322
dL = 0.07
dn = 0.5
version = 0
nuMax = 39000.0
else:
return 0
else:
n = 1
nuMax = 0.0
with open("%s.def" % M) as defFile:
for line in defFile:
if not "No. of transition files" in line:
continue
n = int(line.split()[0])
print(line)
print(n)
with open("%s.def" % M) as defFile:
for line in defFile:
if not "Isotopologue mass" in line:
continue
mass = line.split()[0]
print(line)
print(mass)
with open("%s.def" % M) as defFile:
for line in defFile:
if not "Default value of Lorentzian half-width for all lines" in line:
continue
dL = line.split()[0]
print(line)
print(dL)
with open("%s.def" % M) as defFile:
for line in defFile:
if not "Default value of temperature exponent for all lines" in line:
continue
dn = line.split()[0]
print(line)
print(dn)
with open("%s.def" % M) as defFile:
for line in defFile:
if not "Maximum wavenumber" in line:
continue
nuMax = line.split()[0]
print(line)
print(nuMax)
with open("%s.def" % M) as defFile:
for line in defFile:
if not "Version number with format" in line:
continue
version = line.split()[0]
print(line)
print(version)
if(n == 1):
s = int(math.ceil(float(nuMax)))
warning1 = 0
if(len(dL) == 0):
warning1 = 1
dL = 0.07
warning2 = 0
if(len(dn) == 0):
warning2 = 1
dn = 0.5
#correct now wrong number of files
#if(M == "12C-1H3-37Cl__OYT"):
# n=64
if(n != nn):
print("Error, number of .trans files do not agree %d %d" % (n, nn))
return 0
if(nuMax == 0.0):
print("Error, Maximum wavenumber in def file not found")
return 0
l=np.zeros(n, dtype=int)
jarray=np.zeros(n+1, dtype=int)
for nu in range(n + 1):
jarray[nu] = nu * s
transFile = ''
if(DownloadFiles == 3):
#used for super lines
n = 1
for nu in range(n):
print("------Download file %d from %d -----" % (nu, n))
if(M == "1H2-16O__BT2"):
jarray = [0, 250, 500, 750, 1000, 1500, 2000, 2250, 2750, 3500, 4500, 5500, 7000, 9000, 14000, 20000, 30000]
transFile = "%s__%05d-%05d.trans" % (M, jarray[nu], jarray[nu+1])
if(DownloadFiles == 1):
com = "wget http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
er=os.system(com)
if(er != 0):
print("Error in download .trans file")
exit()
else:
com = "bzip2 -d %s.bz2" % (transFile)
er=os.system(com)
if(getTimeStamp == 1):
url = "http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
elif(M == "1H-2H-16O__VTT"):
jarray = [0, 250, 500, 750, 1000, 1500, 2000, 2250, 2750, 3500, 4500, 5500, 7000, 9000, 14000, 20000, 26000]
transFile = "%s__%05d-%05d.trans" % (M, jarray[nu], jarray[nu+1])
if(DownloadFiles == 1):
com = "wget http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
er=os.system(com)
if(er != 0):
print("Error in download .trans file")
exit()
else:
com = "bzip2 -d %s.bz2" % (transFile)
er=os.system(com)
if(getTimeStamp == 1):
url = "http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
else:
if(DownloadFiles == 3):
#super lines
nuMaxs = int(math.ceil(float(nuMax)))
transFile = "%s__%05d-%05d__%dK.super" % (M, 0, nuMaxs, Temp)
print('Download superlines', transFile)
com = "wget http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
url = "http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
er=os.system(com)
if(er == 0):
com = "bzip2 -d %s.bz2" % transFile
er=os.system(com)
if(er != 0):
print("Error in download .super file")
exit()
jarray[1] = nuMaxs
l[0]=int(subprocess.check_output(['wc', '-l', "%s" % transFile]).split()[0])
else:
if(n > 1 or M == "75As-1H3__CYT18"):
#multiple transition files
transFile = "%s__%05d-%05d.trans" % (M, nu * s, nu * s + s)
transFile4 = "%s__%04d-%04d.trans" % (M, nu * s, nu * s + s)
if(DownloadFiles == 1):
#trans files with only 4 digits
if(dg == 4):
com = "wget http://www.exomol.com/db/%s/%s.bz2" % (P, transFile4)
url = "http://www.exomol.com/db/%s/%s.bz2" % (P, transFile4)
elif(dg == 5):
com = "wget http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
url = "http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
else:
print("Error, number of digits not valid")
return 0
er=os.system(com)
if(er != 0):
print("Error in download .trans file")
exit()
if(dg == 4):
com = "mv %s.bz2 %s.bz2" % (transFile4, transFile)
er=os.system(com)
com = "bzip2 -d %s.bz2" % (transFile)
er=os.system(com)
if(getTimeStamp == 1):
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
else:
url = "http://www.exomol.com/db/%s/%s" % (P, transFile)
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
else:
#only 1 transition file
transFile = "%s.trans" % (M)
if(DownloadFiles == 1):
com = "wget http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
er=os.system(com)
if(er == 0):
com = "bzip2 -d %s.bz2" % transFile
er=os.system(com)
else:
com = "wget http://www.exomol.com/db/%s/%s" % (P, transFile)
er=os.system(com)
if(er != 0):
print("Error in download .trans file")
exit()
if(getTimeStamp == 1):
url = "http://www.exomol.com/db/%s/%s.bz2" % (P, transFile)
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
else:
url = "http://www.exomol.com/db/%s/%s" % (P, transFile)
#first check if file exists
com = "wget -S --spider %s" % url
er=os.system(com)
#print("er", er)
if(er == 0):
date = getTime(url)
date1 = max(date, date1)
if(PrintISO == 1 and DownloadFiles != 3):
#check if trans file exists
exists = os.path.isfile("%s" % transFile)
if(exists == 0):
print("Error, transition file not found: %s" % transFile)
return 0
l[nu]=int(subprocess.check_output(['wc', '-l', "%s" % transFile]).split()[0])
print("nu", nu, l[nu])
#determine the number of columns in the transition files
with open("%s" % transFile) as tFile:
line = tFile.readline()
ntcol = len(line.split())
print("number of columns in trans file", ntcol)
#check the maximum wavenumber. Its sometimes wrong in the def files
if(n == 1 and ntcol == 4):
nnuMax = 0.0
with open(transFile) as infile:
il = 0
for line in infile:
#print(line)
#print("x", line.split()[3])
if(len(line) > 1):
nnuMax = max(nnuMax, float(line.split()[ntcol - 1]))
if(il % 10000 == 0):
print("nuMax", il, nnuMax)
il += 1
print("nuMax:", nuMax, nnuMax)
jarray[1] = int(math.ceil(float(nnuMax)))
print("download finished")
if(warning1 == 1):
print("Waring, <Default value of Lorentzian half-width for all lines> not found, used dL = %g" % dL)
if(warning2 == 1):
print("Waring, <Default value of temperature exponent for all lines> not found, used dn = %g" % dn)
if(getTimeStamp == 1):
f = open(("%s.time" % M),'w')
print(date1, file = f)
if(PrintISO == 1):
if(DownloadFiles != 3):
f = open(("%s.param" % M),'w')
else:
f = open(("%s__%05d-%05d__%dK_super.param" % (M, 0, nuMaxs, Temp)),'w')
if(DownloadFiles != 3):
lStates=int(subprocess.check_output(['wc', '-l', "%s.states" % M]).split()[0])
else:
lStates = 0
if(DownloadFiles != 3):
print("Database = 2", file = f)
else:
print("Database = 20", file = f)
print("Molecule number = %d" % 1, file = f)
if(DownloadFiles != 3):
print("Name = %s" % M, file = f)
else:
print("Name = %s__%05d-%05d__%dK" % (M, 0, nuMaxs, Temp), file = f)
print("Number of Isotopologues = 1", file = f)
print("#Id Abundance Q(296K) g Molar Mass(g) partition file :", file = f)
print("0 1.0 0.0 0 %s %s.pf" % (mass, M), file = f)
print("Number of columns in partition File = %s" % npfcol, file = f)
print("Number of line/transition files = %d" % n, file = f)
print("Number of lines per file :", file = f)
for nu in range(n):
print("%d" % l[nu], file = f)
print("Line file limits :", file = f)
for nu in range(n + 1):
print("%d" % (jarray[nu]), file = f)
print("#ExoMol :", file = f)
print("Number of states = %d" % lStates, file = f)
print("Number of columns in transition files = %s" % ntcol, file = f)
print("Default value of Lorentzian half-width for all lines = %s" % dL, file = f)
print("Default value of temperature exponent for all lines = %s" % dn, file = f)
print("Version = %s" % version, file = f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-M', '--M', type=str,
help='Species', default = '')
parser.add_argument('-D', '--DownloadFiles', type=int,
help='Download Files', default = 1)
parser.add_argument('-P', '--PrintISO', type=int,
help='Print ISO', default = 1)
parser.add_argument('-Ts', '--getTimeStamp', type=int,
help='get time stamp', default = 0)
parser.add_argument('-Temp', '--Temperature', type=int,
help='Temperature', default = 100)
args = parser.parse_args()
M = args.M
DownloadFiles = args.DownloadFiles
PrintISO = args.PrintISO
getTimeStamp = args.getTimeStamp
Temp = args.Temperature
if(M == ''):
print("Error, no species specified, run python exomol.py -M <id>")
main(M, DownloadFiles, PrintISO, getTimeStamp, Temp)