1 | import exo_k as xk |
---|
2 | import numpy as np |
---|
3 | import matplotlib.pyplot as plt |
---|
4 | import astropy.units as u |
---|
5 | import astropy.constants as cst |
---|
6 | import glob |
---|
7 | from chemistry import isotopologue_to_species, species_name_to_common_isotopologue_name |
---|
8 | |
---|
9 | ### choose spectral resolution for corrk |
---|
10 | R=10000 |
---|
11 | wnedges=xk.wavenumber_grid_R(10000/100, 10000/.3, R) |
---|
12 | print("Spectral resolution asked: wn=", wnedges.min(), wnedges.max(), ", R =", R) |
---|
13 | |
---|
14 | ### set data paths |
---|
15 | # dace_path = "datadir/cold/" |
---|
16 | dace_path = "datadir/hot/" |
---|
17 | corrk_dir = "datadir/corrk_data/" |
---|
18 | dir_out = corrk_dir+f"/R{R}_from_dace/" |
---|
19 | |
---|
20 | ### you can set your desired p and t grid here (otherwise, first molecule is used) |
---|
21 | logpgrid = None |
---|
22 | tgrid = None |
---|
23 | # ref=xk.Ktable(filename=corrk_dir + '/R500_from_R15000/CH4_R500.corrk.h5', mol="CH4") |
---|
24 | # logpgrid = ref.logpgrid |
---|
25 | # tgrid = ref.tgrid |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | ### select (output) molecules and their corresponding (input) isotopologues |
---|
30 | ### set value to 'None' to get common isotopologue |
---|
31 | molecules = { |
---|
32 | "CO": "12C-16O", |
---|
33 | "CO2": None, |
---|
34 | # "H2O": "1H2-16O", |
---|
35 | # # "C2H2":"12C2-1H2", |
---|
36 | # # "C2H4":"12C2-1H4", |
---|
37 | # "CH4": "12C-1H4", |
---|
38 | # "TiO": "48Ti-16O", |
---|
39 | # "VO": "51V-16O", |
---|
40 | } |
---|
41 | |
---|
42 | # xk.Settings().set_mks(True) |
---|
43 | xk.Settings().set_log_interp(False) |
---|
44 | xk.Settings().set_case_sensitive(True) |
---|
45 | |
---|
46 | def dace_to_corrk(molecule=None, isotopologue=None, order=17, logpgrid=logpgrid, tgrid=tgrid): |
---|
47 | if molecule is None: |
---|
48 | molecule=isotopologue_to_species(isotopologue) |
---|
49 | elif isotopologue is None: |
---|
50 | isotopologue=species_name_to_common_isotopologue_name(molecule) |
---|
51 | print(molecule, isotopologue) |
---|
52 | |
---|
53 | # use first-found linelist |
---|
54 | try: |
---|
55 | mol_corrk = glob.glob(dace_path+f'{isotopologue}__*/')[0] |
---|
56 | except IndexError: |
---|
57 | print(f"Isotopologue {isotopologue} not found in {dace_path}") |
---|
58 | return |
---|
59 | |
---|
60 | print(f"Reading: {mol}") |
---|
61 | hr = xk.Hires_spectrum(glob.glob(mol_corrk+"/*.bin")[0], helios=True) |
---|
62 | print(f"Initial spectral resolution = {hr.Nw}") |
---|
63 | |
---|
64 | tmp_ktab=xk.hires_to_ktable(path=mol_corrk, helios=True, wnedges=wnedges, mol=molecule, order=order) # test conversion from a whole folder to tmp |
---|
65 | |
---|
66 | tmp_ktab.kdata = tmp_ktab.kdata * 10 * xk.Molar_mass().fetch(molecule) / cst.N_A |
---|
67 | # cm^2/g * 10000/1000 * kg/mol / (molecule/mol) -> m^2/molecule |
---|
68 | tmp_ktab.kdata_unit = "m^2/molecule" |
---|
69 | tmp_ktab.remove_zeros() |
---|
70 | |
---|
71 | |
---|
72 | if logpgrid is None or tgrid is None: # use first molecule to set p and t grid |
---|
73 | logpgrid = tmp_ktab.logpgrid |
---|
74 | tgrid = tmp_ktab.tgrid |
---|
75 | |
---|
76 | if (tmp_ktab.logpgrid != logpgrid).any() or (tmp_ktab.tgrid != tgrid).any(): |
---|
77 | print("Initial P grid:", tmp_ktab.pgrid) |
---|
78 | print("Initial T grid:", tmp_ktab.tgrid) |
---|
79 | print(f"Remapping {mol}") |
---|
80 | print("New P grid:", np.power(logpgrid,10)) |
---|
81 | print("New T grid:", tgrid) |
---|
82 | tmp_ktab.remap_logPT(logp_array=logpgrid, t_array=tgrid) |
---|
83 | |
---|
84 | print(tmp_ktab) |
---|
85 | print(f"Writing: {mol}") |
---|
86 | tmp_ktab.write_hdf5(dir_out+f"{molecule}_corrk_dace_{R}.h5") |
---|
87 | |
---|
88 | # %matplotlib notebook |
---|
89 | p_plot=1e5 # in Pa |
---|
90 | t_plot=300 |
---|
91 | fig,ax=plt.subplots(figsize=(9,6.5)) |
---|
92 | tmp_ktab.plot_spectrum(ax,p=p_plot,t=t_plot,g=1.,yscale='log',xscale='log',label='g=1') |
---|
93 | fig.tight_layout() |
---|
94 | plt.savefig(dir_out+f"corrk_dace_R{R}_{molecule}_p{p_plot}_t{t_plot}.pdf") |
---|
95 | |
---|
96 | if __name__ == '__main__': |
---|
97 | for mol in molecules: |
---|
98 | dace_to_corrk(molecule=mol, isotopologue=molecules[mol], order=17) |
---|