forked from bmcguir2/simulate_lte
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate_lte.py
More file actions
executable file
·8165 lines (4712 loc) · 206 KB
/
Copy pathsimulate_lte.py
File metadata and controls
executable file
·8165 lines (4712 loc) · 206 KB
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#############################################################
# Revision History #
#############################################################
# reads in an spcat catalog and displays a spectrum
# 1.0 - Project start
# 2.0 - ipython overhaul and simplification
# 2.1 - fixes bug with status and S/C conversion
# 2.2 - fixes bug with recall and sum_stored
# 2.3 - patch for 13ch3oh
# 2.4 - fixes bug with catalogs not at 300 K
# 2.6 - adds autoset functionality
# 2.7 - speeds up gaussian simulations
# 2.8 - further speeds up gaussian simulations and normalizes spectral resolution
# 2.9 - adds labeling ability (Eupper and Quantum Numbers) to current plot
# 3.0 - adds residual plotting ability
# 3.1 - adds residual saving ability
# 3.3 - removes labels (broken on some machines); adds ability to print out line information
# 3.4 - adds ability to do Gaussian fitting of data in the program
# 3.5 - adds ability to mass-generate a p-list for gauss fitting
# 3.6 - adds ability to convert the observations from Jy/beam to K, or K to Jy/beam.
# 3.7 - adds ability to simulate double doublets from cavity FTMW
# 3.8 - adds ability to read in frequencies to plot just as single intensity lines; or to do it manually with a list. changes default plotting to steps, adds ability to switch back to lines.
# 3.9 - adds ability to plot manual catalogs with a velocity offset
# 3.91 - minor change to how plots are titled
# 4.0 - changes the simulation to perform a proper 'radiative transfer' which first calculates an opacity, and then applies the appropriate optical depth correction
# 4.1 - added a K to Jy/beam converter that wasn't there before...
# 4.2 - adds Aij to print_lines() readout
# 4.3 - fixes edge case where two lines have same frequency in catalog for print_lines()
# 4.4 - minor warning fix
# 5.0 - adds ability to do velocity stacking
# 5.1 - adds ability to correct for beam dilution
# 5.2 - adds ability to save velocity stacked spectra
# 5.3 - bug fix for noisy data without lines in peak finding
# 5.4 - minor correction to calculation of optically thick lines
# 5.5 - added ability to cut out spectra around simulated stick spectra
# 5.6 - update to autoset_limits() to not simulate where there's no data between the absolute upper and lower bounds
# 5.7 - adds flag to run simulations using the Planck scale and a beam size to convert to Jy/beam
# 6.0 - major update to Tbg handling
# 6.1 - streamlining the calculations of gaussian after the tbg updates
# 6.2 - adds sgr b2 non-thermal background option
# 6.3 - fixes bug in polynomial continuum temperature calculation
# 6.4 - added utility function for checking Tbg at a given frequency
# 6.5 - bug fix to beam dilution correction for sgr b2 non-thermal background continuum
# 6.6 - bug fix to planck conversion in the solid angle calculation
# 6.7 - added greybody continuum option
# 6.8 - update to print_lines() to show sijmu.
# 6.9 - added ability to add vibrational corrections to partition function.
# 6.10 - fixes bug when using multiple constant continuum values. Introduces 'constant' as tbg_type for this.
# 6.13 - adds ability to filter out windows from stacking that have lines in them already at the center.
# 6.14 - more robust SNR values for stacked spectra
# 6.15 - re-enabled eta for single-dish observations
# 6.16 - added flag for interferometric, allowing beam-dilution for that
# 6.17 - more robust RMS finding in velocity stacking
# 6.18 - edge of band detection to velocity stacking
# 6.19 - better flagging of lines in velocity stacking
# 6.20 - adds load_asai shortcut
# 6.21 - more accurate vibrational partition functions
# 6.22 - more functionality to sim_params
# 6.23 - custom aliases
# 6.24 - updated sim_params for tbg
# 6.25 - new partition functions for some molecules
# 6.26 - adds velocity stack postage stamp plot functionality
# 6.27 - adds ability to make postage-stamp plots
# 6.28 - makes old restore files backwards compatible; adds additional postage functionality
# 6.29 - adds harmonic progression plotting functionality
# 6.30 - adds a sum_stored_thin() option for summing optically thin spectra, and adds ability for stacking to work on summed spectra
# 6.31 - adds ability to plot errors on postage stamp plots
# 6.32 - adds ability to add markers on range plots
# 6.33 - updates flux simulation calculation; does not consider 'beam dilution' in these cases
# 6.34 - adds numpy saving and loading for observations
# 6.35 - adds creation of matched filter plot to velocity_stack
# 6.36 - saves tau information for stacking / summing
# 6.37 - adds glow calculation from loomis
# 6.38 - update to matched filter script
# 6.39 - updates to line flagging in stacking
# 6.40 - adds ability to simulate spectra based on observations
# 6.41 - fixes bug in mf script
# 6.42 - adds option to blank lines not in central portions of stacks and mfs
# 6.43 - adds option to return max mf response within range
# 6.44 - adds thioacetaldehyde partition function; fixes edge case with glow
# 6.45 - adds ability to check beam size
# 6.46 - adds utility functions for quickly checking obs for lines
# 6.47 - adds ability to set individual ylims on postage stamp plots
# 6.48 - updates matched filter to use correlate rather than convolve
#############################################################
# Preamble #
#############################################################
import sys
#Python version check
if sys.version_info.major != 3:
print("This code is written in Python 3. It will not execute in Python 2.7. Exiting (sorry).")
quit()
import numpy as np
from numpy import exp as exp
import time as tm
import warnings
from matplotlib.ticker import AutoMinorLocator
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.ticker as ticker
from matplotlib.ticker import FormatStrFormatter
import itertools
from datetime import datetime
from scipy.optimize import curve_fit
import peakutils
import math
import matplotlib.gridspec as gridspec
from scipy.optimize import minimize
import ast
from scipy import signal
matplotlib.rc('text', usetex = True)
matplotlib.rc('text.latex',preamble=r'\usepackage{cmbright}')
version = 6.48
h = 6.626*10**(-34) #Planck's constant in J*s
k = 1.381*10**(-23) #Boltzmann's constant in J/K
kcm = 0.69503476 #Boltzmann's constant in cm-1/K
ckm = 2.99792458*10**5 #speed of light in km/s
ccm = 2.99792458*10**10 #speed of light in cm/s
cm = 2.99792458*10**8 #speed of light in m/s
#############################################################
# Warning #
#############################################################
print('\n--- MAJOR CHANGE STARTING IN VERSION 6.0 ---')
print('\nThere has been a major update to the way the program treats background temperatures. It now allows for different background temperatures at different frequencies, as well as permitting functionalized backgrounds. As a result, the old system of simply setting Tbg = X will no longer function in Version 6.0+. Please see the documentation for the new protocols.')
#############################################################
# Defaults #
#############################################################
auto_update = False
first_run = True
GHz = False
interferometer = False
quietflag = False #turn on to suppress warnings about how long the simulation will take.
rms = float('-inf') #rms noise. the simulation won't simulate any lines 10x smaller than this value.
thermal = float('inf') #initial default cutoff for optically-thick lines (i.e. don't touch them unless thermal is modified.)
T = 300 #temperature for simulations. Default is 300 K.
catalog_file = None #catalog file to load in. Needs to be a string.
C = 1.0E13 #column density. Units are cm-2.
vlsr = 0.0 #vlsr offset applied to simulation. Default is 0 km/s.
ll = float('-inf') #lower limit for the simulation range. Default is none.
ul = float('inf') #upper limit for the simulation range. Default is none.
spec = None #file of a laboratory or observational spectrum to load in for comparison. Default is none.
dV = 5.0 #linewidth of the simulation. Default is 5.0 km/s.
CT = 300.0 #temperature the catalog is simulated at. Default is 300 K.
gauss = True #toggle for simulating Gaussians or a stick spectrum. Default is True.
dish_size = 100 #for use if beam corrections are desired; given in meters
source_size = 1E20 #for use if beam corrections are desired; given in arcseconds
eta = 1.0 #beam efficiency of the telescope. Set this option manually, with configure_telescope(), or with init_telescope().
npts_line = 15 #default is 15 points across each line
res_kHz = False #if res_kHz is set to True, then the resolution of the Gaussian simulation is calculated using the value for res, and units of kHz
res_kms = False #if res_kms is set to True, then the resolution of the Gaussian simulation is calculated using the value for res, and units of km/s
res = 0.01 #resolution used in Gaussian simulation if res_kHz or res_kms is set to True.
cavity_ftmw = False #if set to True, simulates doubler doublets from the cavity FTMW.
cavity_dV = 0.13 #sets the default cavity linewidth to 0.13 km/s
cavity_split = 0.826 #sets the default doppler splitting in the cavity to 0.826 km/s in each direction.
draw_style = 'steps' #can be toggled on and off for going between drawing steps and drawing lines between points using use_steps() and use_lines()
planck = False #flag to use planck scale. If planck = True is enabled, a synthesized beam size must also be provided using synth_beam = [bmaj,bmin] below.
synth_beam = ['bmaj','bmin'] #to be used with planck = True conversions. Will throw an error if you don't set it in the program.
sim = {} #dictionary to hold stored simulations
lines = {} #dictionary to hold matplotlib lines
tbg = [] #to hold background temperatures
vibs = None #This is a list of vibrational *frequencies* in units of cm-1
match_obs = False #if set to true, then the simulation will use the x-coords of the observations.
two_fwhm_only = False #if set to true, will simulate only two FWHM around each line
############ Tbg Parameters ##############
#tbg_params is to hold the actual parameters used to calculate Tbg.
#If it is a constant, then it can be passed an integer. tbg_type must be 'constant' and tbg_order must be an integer 0. These are the defaults. Other possibilities are described below.
tbg_params = 2.7
#tbg_type can be the following:
#'constant' is a constant value. If there are multiple ranges, then a value must be giving for each range.
#'poly' is a polynomial of order set by tbg_order = X, where X is the order and an integer. Tbg_params must be a list of length = X+1. So a first order polynomial needs two values [A,B] in the tbg_params: y = Ax + B.
#'power' is a power law of the form Y = Ax^B + C. tbg_params must be a list with three values [A,B,c]
#'sgrb2' invokes a special value tbg = (10**(-1.06*np.log10(frequency/1000) + 2.3))
#'greybody' is a greybody continuum, requiring parameters:
#T = tbg_params[0] #in Kelvin
#beta = tbg_params[1]
#tauref = tbg_params[2]
#taufreq = tbg_params[3] #in GHz
#major = tbg_params[4] #major axis of beam in arcsec
#minor = tbg_params[5] #minor axis of beam in arcsec
tbg_type = 'constant'
#tbg_range can contain a list of paired upper and lower limits, themselves a length 2 list, for the sets of parameters in tbg_params to be used within. If ranges are defined, any bit of the simulation not in the defined range defaults to 2.7 K. float('-inf') or float('inf') are valid range values.
tbg_range = []
#Some examples:
#To have three different frequency ranges (100000-120000 MHz, 150000-160000, and 190000-210000 MHz), all with their own tbg constants of 27, 32, and 37, the following must be input:
#tbg_params = [27,32,37]
#tbg_range = [[100000,120000],[150000,160000],[190000,210000]]
#To have a power law across the entire simulation, with Y = Ax^B + C and A = 2, B = 1.2, and C = 0:
#tbg_params = [2,1.2,0]
#tbg_type = 'power'
#To have three polynomials, of orders 1, 3, and 4, over the three different ranges above, you'd need:
#tbg_params = [[1.2,5],[1.7,1.3,2,4],[2,4,-0.7,0.8,1.2]]
#tbg_range = [[100000,120000],[150000,160000],[190000,210000]]
##########################################
vel_stacked = [] #to hold velocity-stacked spectra
int_stacked = []
vel_sim_stacked = [] #to hold velocity-stacked simulated spectra
int_sim_stacked = []
freq_obs = [] #to hold laboratory or observational spectra
int_obs = []
freq_sim = [] #to hold simulated spectra
int_sim = []
int_tau = []
freq_sum = [] #to hold combined spectra
int_sum = []
freq_man = [] #to hold manual (frequency only) spectra
int_man = []
freq_resid = [] #to hold residual spectra
int_resid = []
velocity_mf = [] #to hold matched filter spectra
intensity_mf = []
current = catalog_file
obs_name = ''
colors = itertools.cycle(['#ff8172','#514829','#a73824','#7b3626','#a8ac87','#8c8e64','#974710','#d38e20','#ce9a3a','#ae7018','#ac5b14','#64350f','#b18f59','#404040','#791304','#1f2161','#171848','#3082fe','#2c5b5e','#390083','#5c65f8','#6346fa','#3c3176','#1cf6ba','#c9bcf0','#90edfc','#3fb8ee','#b89b33','#e7d17b'])
styles = itertools.cycle(['-','--','-.',':'])
#############################################################
# Functions #
#############################################################
#read_cat reads the catalog file in
def read_cat(catalog_file):
'''
Reads in a catalog file line by line
'''
my_array = []
try:
with open(catalog_file) as input:
for line in input:
my_array.append(line)
except TypeError:
print('Specify a catalog file with catalog_file = \'x\'')
return
return my_array
#fix_pm fixes +/- quantum number issues
def fix_pm(qnarray):
if '+' or '-' in qnarray:
qnarray[qnarray == ''] = '0'
qnarray[qnarray == '+'] = '1'
qnarray[qnarray == '-'] = '2'
return qnarray
#fix_qn fixes quantum number issues
def fix_qn(old_qn):
'''
fixes quantum number issues arising from the use of alphabet characters to represent numbers in spcat
'''
new_qn = 000
if 'A' in old_qn:
new_qn = 100 + int(old_qn[1])
if 'B' in old_qn:
new_qn = 110 + int(old_qn[1])
if 'C' in old_qn:
new_qn = 120 + int(old_qn[1])
if 'D' in old_qn:
new_qn = 130 + int(old_qn[1])
if 'E' in old_qn:
new_qn = 140 + int(old_qn[1])
if 'F' in old_qn:
new_qn = 150 + int(old_qn[1])
if 'G' in old_qn:
new_qn = 160 + int(old_qn[1])
if 'H' in old_qn:
new_qn = 170 + int(old_qn[1])
if 'I' in old_qn:
new_qn = 180 + int(old_qn[1])
if 'J' in old_qn:
new_qn = 190 + int(old_qn[1])
if 'K' in old_qn:
new_qn = 200 + int(old_qn[1])
if 'L' in old_qn:
new_qn = 210 + int(old_qn[1])
if 'M' in old_qn:
new_qn = 220 + int(old_qn[1])
if 'N' in old_qn:
new_qn = 230 + int(old_qn[1])
if 'O' in old_qn:
new_qn = 240 + int(old_qn[1])
if 'P' in old_qn:
new_qn = 250 + int(old_qn[1])
if 'Q' in old_qn:
new_qn = 260 + int(old_qn[1])
if 'R' in old_qn:
new_qn = 270 + int(old_qn[1])
if 'S' in old_qn:
new_qn = 280 + int(old_qn[1])
if 'T' in old_qn:
new_qn = 290 + int(old_qn[1])
if 'U' in old_qn:
new_qn = 300 + int(old_qn[1])
if 'V' in old_qn:
new_qn = 310 + int(old_qn[1])
if 'W' in old_qn:
new_qn = 320 + int(old_qn[1])
if 'X' in old_qn:
new_qn = 330 + int(old_qn[1])
if 'Y' in old_qn:
new_qn = 340 + int(old_qn[1])
if 'Z' in old_qn:
new_qn = 350 + int(old_qn[1])
if 'a' in old_qn:
new_qn = 100 + int(old_qn[1])
if 'b' in old_qn:
new_qn = 110 + int(old_qn[1])
if 'c' in old_qn:
new_qn = 120 + int(old_qn[1])
if 'd' in old_qn:
new_qn = 130 + int(old_qn[1])
if 'e' in old_qn:
new_qn = 140 + int(old_qn[1])
if 'f' in old_qn:
new_qn = 150 + int(old_qn[1])
if 'g' in old_qn:
new_qn = 160 + int(old_qn[1])
if 'h' in old_qn:
new_qn = 170 + int(old_qn[1])
if 'i' in old_qn:
new_qn = 180 + int(old_qn[1])
if 'j' in old_qn:
new_qn = 190 + int(old_qn[1])
if 'k' in old_qn:
new_qn = 200 + int(old_qn[1])
if 'l' in old_qn:
new_qn = 210 + int(old_qn[1])
if 'm' in old_qn:
new_qn = 220 + int(old_qn[1])
if 'n' in old_qn:
new_qn = 230 + int(old_qn[1])
if 'o' in old_qn:
new_qn = 240 + int(old_qn[1])
if 'p' in old_qn:
new_qn = 250 + int(old_qn[1])
if 'q' in old_qn:
new_qn = 260 + int(old_qn[1])
if 'r' in old_qn:
new_qn = 270 + int(old_qn[1])
if 's' in old_qn:
new_qn = 280 + int(old_qn[1])
if 't' in old_qn:
new_qn = 290 + int(old_qn[1])
if 'u' in old_qn:
new_qn = 300 + int(old_qn[1])
if 'v' in old_qn:
new_qn = 310 + int(old_qn[1])
if 'w' in old_qn:
new_qn = 320 + int(old_qn[1])
if 'x' in old_qn:
new_qn = 330 + int(old_qn[1])
if 'y' in old_qn:
new_qn = 340 + int(old_qn[1])
if 'z' in old_qn:
new_qn = 350 + int(old_qn[1])
#qnarray[line] = int(new_qn)
return int(new_qn)
# splices the catalog file appropriately, then populates a numpy array with the data
def splice_array(x):
'''
splices the catalog file appropriately, then populates a numpy array with the data
'''
frequency = np.arange(len(x),dtype=np.float)
error = np.arange(len(x),dtype=np.float)
logint = np.arange(len(x),dtype=np.float)
dof = np.arange(len(x),dtype=np.int)
elower = np.arange(len(x),dtype=np.float)
gup = np.arange(len(x),dtype=np.int)
tag = np.arange(len(x),dtype=np.int)
qnformat = np.arange(len(x),dtype=np.int)
qn1 = np.arange(len(x),dtype=object)
qn2 = np.empty(len(x),dtype=object)
qn3 = np.empty(len(x),dtype=object)
qn4 = np.empty(len(x),dtype=object)
qn5 = np.empty(len(x),dtype=object)
qn6 = np.empty(len(x),dtype=object)
qn7 = np.empty(len(x),dtype=object)
qn8 = np.empty(len(x),dtype=object)
qn9 = np.empty(len(x),dtype=object)
qn10 = np.empty(len(x),dtype=object)
qn11 = np.empty(len(x),dtype=object)
qn12 = np.empty(len(x),dtype=object)
for line in range(len(x)):
frequency[line] = float(str(x[line][:13]).strip())
error[line] = float(str(x[line][13:21]).strip())
logint[line] = float(str(x[line][21:29]).strip())
dof[line] = int(str(x[line][29:31]).strip())
elower[line] = float(str(x[line][31:41]).strip())
try:
gup[line] = int(str(x[line][41:44]).strip()) if str(x[line][41:44]).strip() else ''
except ValueError:
gup[line] = fix_qn(str(x[line][41:44]))
tag[line] = int(str(x[line][44:51]).strip())
qnformat[line] = int(str(x[line][51:55]).strip())
qn1[line] = str(x[line][55:57]).strip()
qn2[line] = str(x[line][57:59]).strip()
qn3[line] = str(x[line][59:61]).strip()
qn4[line] = str(x[line][61:63]).strip()
qn5[line] = str(x[line][63:65]).strip()
qn6[line] = str(x[line][65:67]).strip()
qn7[line] = str(x[line][67:69]).strip()
qn8[line] = str(x[line][69:71]).strip()
qn9[line] = str(x[line][71:73]).strip()
qn10[line] = str(x[line][73:75]).strip()
qn11[line] = str(x[line][75:77]).strip()
qn12[line] = str(x[line][77:]).strip()
if '+' in qn1 or '-' in qn1:
qn1 = fix_pm(qn1)
if '+' in qn2 or '-' in qn2:
qn2 = fix_pm(qn2)
if '+' in qn3 or '-' in qn3:
qn3 = fix_pm(qn3)
if '+' in qn4 or '-' in qn4:
qn4 = fix_pm(qn4)
if '+' in qn5 or '-' in qn5:
qn5 = fix_pm(qn5)
if '+' in qn6 or '-' in qn6:
qn6 = fix_pm(qn6)
if '+' in qn7 or '-' in qn7:
qn7 = fix_pm(qn7)
if '+' in qn8 or '-' in qn8:
qn8 = fix_pm(qn8)
if '+' in qn9 or '-' in qn9:
qn9 = fix_pm(qn9)
if '+' in qn10 or '-' in qn10:
qn10 = fix_pm(qn10)
if '+' in qn11 or '-' in qn11:
qn11 = fix_pm(qn11)
if '+' in qn12 or '-' in qn12:
qn12 = fix_pm(qn12)
for line in range(len(qn1)):
try:
qn1[line] = int(qn1[line])
except ValueError:
qn1[line] = fix_qn(qn1[line])
for line in range(len(qn2)):
try:
qn2[line] = int(qn2[line])
except ValueError:
qn2[line] = fix_qn(qn2[line])
for line in range(len(qn3)):
try:
qn3[line] = int(qn3[line])
except ValueError:
qn3[line] = fix_qn(qn3[line])
for line in range(len(qn4)):
try:
qn4[line] = int(qn4[line])
except ValueError:
qn4[line] = fix_qn(qn4[line])
for line in range(len(qn5)):
try:
qn5[line] = int(qn5[line])
except ValueError:
qn5[line] = fix_qn(qn5[line])
for line in range(len(qn6)):
try:
qn6[line] = int(qn6[line])
except ValueError:
qn6[line] = fix_qn(qn6[line])
for line in range(len(qn7)):
try:
qn7[line] = int(qn7[line])
except ValueError:
qn7[line] = fix_qn(qn7[line])
for line in range(len(qn8)):
try:
qn8[line] = int(qn8[line])
except ValueError:
qn8[line] = fix_qn(qn8[line])
for line in range(len(qn9)):
try:
qn9[line] = int(qn9[line])
except ValueError:
qn9[line] = fix_qn(qn9[line])
for line in range(len(qn10)):
try:
qn10[line] = int(qn10[line])
except ValueError:
qn10[line] = fix_qn(qn10[line])
for line in range(len(qn11)):
try:
qn11[line] = int(qn11[line])
except ValueError:
qn11[line] = fix_qn(qn11[line])
for line in range(len(qn12)):
try:
qn12[line] = int(qn12[line])
except ValueError:
qn12[line] = fix_qn(qn12[line])
return frequency,error,logint,dof,elower,gup,tag,qnformat,qn1,qn2,qn3,qn4,qn5,qn6,qn7,qn8,qn9,qn10,qn11,qn12
#det_qns determines how many qns represent each state
def det_qns(qnformat):
'''
determines how many qns represent each state
'''
qns = int(str(qnformat[0])[-1:])
if qns > 6:
qns = 6
return qns
#calc_q will dynamically calculate a partition function whenever needed at a given T. The catalog file used must have enough lines in it to fully capture the partition function, or the result will not be accurate for Q.
def calc_q(qns,elower,qn7,qn8,qn9,qn10,qn11,qn12,T,catalog_file,vibs):
'''
Dynamically calculates a partition function whenever needed at a given T. The catalog file used must have enough lines in it to fully capture the partition function, or the result will not be accurate for Q. This is perfectly fine for the *relative* intensities of lines for a given molecule used by this program. However, absolute intensities between molecules are not remotely accurate.
'''
Q = np.float64(0.0) #Initialize a float for the partition function
if 'acetone.cat' in catalog_file.lower():
Q = 2.91296*10**(-7)*T**6 - 0.00021050085*T**5 + 0.05471337*T**4 - 5.5477*T**3 + 245.28*T**2 - 2728.3*T + 16431 #Hard code for Acetone
elif 'sh.cat' in catalog_file.lower():
Q = 0.000000012549467*T**4 - 0.000008528126823*T**3 + 0.002288160909445*T**2 + 0.069272946237033*T + 15.357239728157400
#Hard code for SH. Completely unreliable below 2.735 K or above 300 K.
elif 'nh3.cat' in catalog_file.lower():
Q = 0.11044*T**1.5025 + 2.5396
elif 'methanol.cat' in catalog_file.lower() or 'ch3oh.cat' in catalog_file.lower() or 'ch3oh_v0.cat' in catalog_file.lower() or 'ch3oh_v1.cat' in catalog_file.lower() or 'ch3oh_v2.cat' in catalog_file.lower() or 'ch3oh_vt.cat' in catalog_file.lower():
Q = 4.83410*10**-11*T**6 - 4.04024*10**-8*T**5 + 1.27624*10**-5*T**4 - 1.83807*10**-3*T**3 + 2.05911*10**-1*T**2 + 4.39632*10**-1*T -1.25670
elif '13ch3oh.cat' in catalog_file.lower():
Q = 0.000050130*T**3 + 0.076540934*T**2 + 4.317920731*T - 31.876881967
elif 'c2n.cat' in catalog_file.lower() or 'ccn.cat' in catalog_file.lower():
Q = 1.173755*10**(-11)*T**6 - 1.324086*10**(-8)*T**5 + 5.99936*10**(-6)*T**4 - 1.40473*10**(-3)*T**3 + 0.1837397*T**2 + 7.135161*T + 22.55770
elif 'ch2nh.cat' in catalog_file.lower():
Q = 1.2152*T**1.4863
elif '13ch3oh.cat' in catalog_file.lower() or 'c033502.cat' in catalog_file.lower():
Q = 0.399272*T**1.756329
elif 'aceticacid' in catalog_file.lower():
Q = 0.0009051494*T**3 + 2.3370894781*T**2 - 34.5494711437*T + 1110.8534245568
elif 'methylformate' in catalog_file.lower() and '13' not in catalog_file.lower():
Q = 3.29808*10**-8*T**5 - 2.59463*10**-5*T**4 + 5.80410*10**-3*T**3 + 1.60794*T**2 + 95.0922*T-328.468
elif 'glycolaldehyde' in catalog_file.lower() and '13' not in catalog_file.lower():
Q = 0.000501*T**3 + 0.562444*T**2 + 14.005379*T + 114.004177
elif 'h2ccs' in catalog_file.lower():
#Q = -0.00000000328379*T**5 + 0.000002934039*T**4 -0.001093668*T**3 + 0.315766*T**2 + 12.08729*T - 66.79358
Q = 3.5655362887*T**1.5 -8.3747644
elif 'ch3nh2' in catalog_file.lower():
#Q = 23.82947*T**(1.50124) #JPL DATABASE VALUE
Q = 5.957729*T**(1.501233) #Ilyushin 2014 paper value
elif 'n2h+_hfs' in catalog_file.lower():
Q = 3.2539 + 4.0233*T + 1.0014E-5*T**2
#GOTHAM PARTITION FUNCTIONS BELOW
elif 'hcn.cat' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = (0.92213*T**1.0836 + 4.3068)/3
if T == CT:
Q = 151.16
elif T > 300:
print('Warning: Extrapolating Q beyond 300 K for this molecule gets progressively iffier.')
elif T < 5:
print('Warning: Calculations for Q below 5 K are probably off by ~30%...')
elif 'hcn_hfs.cat' in catalog_file.lower():
Q = (0.92213*T**1.0836 + 4.3068)
if T == CT:
Q = 453.4944
elif T > 300:
print('Warning: Extrapolating Q beyond 300 K for this molecule gets progressively iffier.')
elif T < 5:
print('Warning: Calculations for Q below 5 K are probably off by ~30%...')
elif 'nh2cn' in catalog_file.lower():
if T > 50:
Q = 2.0081*T**1.5972 - 259.42
elif T < 50:
Q = 0.81*T**1.7753 + 2.7549
if T > 300:
print('Warning: Extrapolating Q beyond 300 K for this molecule gets progressively iffier.')
elif T < 10:
print('Warning: Extrapolating Q below 10 K for this molecule gets progressively iffier.')
elif 'nh2cho' in catalog_file.lower():
Q = 5.5769*T**1.5 - 9.2166
if T > 300:
print('Warning: Extrapolating Q beyond 300 K for this molecule gets progressively iffier.')
elif T < 10:
print('Warning: Extrapolating Q below 10 K for this molecule gets progressively iffier. Error in Q at 9.375K is ~8%.')
elif 'hc11n' in catalog_file.lower():
Q = 123.2554*T + 0.1381
elif 'hc9n' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = 71.7308577*T + 0.02203968
elif 'hc9n' in catalog_file.lower() and 'hfs' in catalog_file.lower():
Q = 3*71.7308577*T + 3*0.02203968
elif 'hc7n' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = 36.94999*T + 0.1356045
elif 'hc7n' in catalog_file.lower() and 'hfs' in catalog_file.lower():
Q = 3*36.94999*T + 3*0.1356045
elif 'hc5n' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = 15.65419*T + 0.2214
elif 'hc5n' in catalog_file.lower() and 'hfs' in catalog_file.lower():
Q = 3*15.65419*T + 0.2214
elif 'hc3n' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = 4.581898*T + 0.2833
elif 'hc3n' in catalog_file.lower() and 'hfs' in catalog_file.lower():
Q = 3*4.581898*T + 0.2833
elif 'hc2nc' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = (12.58340*T + 1.0604)/3
elif 'hc2nc' in catalog_file.lower() and 'hfs' in catalog_file.lower():
Q = 12.58340*T + 1.0604
elif 'hc4nc' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = (44.62171*T + 0.6734)/3
elif 'hc4nc' in catalog_file.lower() and 'hfs' in catalog_file.lower():
Q = 44.62171*T + 0.6734
elif 'hc6nc' in catalog_file.lower() and 'hfs' not in catalog_file.lower():
Q = (107.3126*T + 1.2714)/3
elif 'hc6nc' in catalog_file.lower() and 'hfs' in catalog_file.lower():
Q = 107.3126*T + 1.2714
elif 'propargylcyanide' in catalog_file.lower() or 'propargyl_cyanide' in catalog_file.lower():
Q = 41.542*T**1.5008 + 1.5008
elif 'pyrrole' in catalog_file.lower():
Q = 27.727*T**1.4752
if T == CT:
Q = 58328.0735
elif T > 40:
print('Warning: Extrapolating Q beyond 40 K for this molecule gets progressively iffier.')
elif 'cyclopentadiene' in catalog_file.lower():
Q = 9.7764*T**1.5 + 3.5246
if T == CT:
Q = 36251.5276
elif T > 40:
print('Warning: Extrapolating Q beyond 40 K for this molecule gets progressively iffier.')
elif '1-cyano-CPD' in catalog_file.lower():
Q = 101.0674*T**1.5 + 23.9985
if T == CT:
Q = 374492.9985