source: trunk/UTIL/corrk_exo_k/dace_to_corrk.py

Last change on this file was 3671, checked in by afalco, 5 months ago

corrk: updated scripts to match API of dace and exo_k.
Added missing atoms in exomol species.
AF

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1import exo_k as xk
2import numpy as np
3import matplotlib.pyplot as plt
4import astropy.units as u
5import astropy.constants as cst
6import glob
7from chemistry import isotopologue_to_species, species_name_to_common_isotopologue_name
8
9### choose spectral resolution for corrk
10R=10000
11wnedges=xk.wavenumber_grid_R(10000/100, 10000/.3, R)
12print("Spectral resolution asked: wn=", wnedges.min(), wnedges.max(), ", R =", R)
13
14### set data paths
15# dace_path = "datadir/cold/"
16dace_path = "datadir/hot/"
17corrk_dir = "datadir/corrk_data/"
18dir_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)
21logpgrid = None
22tgrid = 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
31molecules = {
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)
43xk.Settings().set_log_interp(False)
44xk.Settings().set_case_sensitive(True)
45
46def 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
96if __name__ == '__main__':
97    for mol in molecules:
98        dace_to_corrk(molecule=mol, isotopologue=molecules[mol], order=17)
Note: See TracBrowser for help on using the repository browser.