source: trunk/UTIL/corrk_exo_k/exomol.py

Last change on this file was 3607, checked in by afalco, 19 hours ago

Pluto: minor compilatrion-related fixes.
AF

File size: 3.5 KB
Line 
1# Download exomol opacities
2# author: Aurelien Falco
3
4import numpy as np
5import os
6import wget
7from chemistry import species_name_to_common_isotopologue_name
8
9### output formats available on exomol.com
10output_formats = [
11"R15000_0.3-50mu.xsec.TauREx.h5",
12"R1000_0.3-50mu.ktable.petitRADTRANS.h5",
13"R1000_0.3-50mu.ktable.ARCiS.fits",
14"R1000_0.3-50mu.ktable.NEMESIS.kta",
15]
16
17def download_exomol(molecule=None, isotopologue=None, line_list=None, output_dir="tmp", output_format="taurex"):
18    """
19    Downloads ExoMol data for a given molecule, isotopologue, and line list in the specified output format.
20    Parameters:
21    molecule (str): The name of the molecule.
22    isotopologue (str): The isotopologue of the molecule.
23    line_list (str): The line list to download. If you don't know, leave empty
24    output_dir (str): The directory where the downloaded file will be saved.
25    output_format (str): The desired output format for the downloaded file. Among [ taurex, petitradtrans, arcis, nemesis ].
26    Example:
27    download_exomol("H2O", "1H2-16O", "BT2", "/path/to/output", "taurex")
28    """
29    if molecule is not None:
30        isotopologue = species_name_to_common_isotopologue_name(molecule)
31
32    exomol_url = "https://exomol.com/db/"
33    exfile = "Exomol_species.dat"
34
35    #check if Exomol file exists and create it otherwise
36    exists = os.path.isfile(exfile)
37    if(exists == 0):
38        # raise FileNotFoundError(f"Exomol file {exfile} not found. Run exomol2.py to create it. https://raw.githubusercontent.com/exoclime/HELIOS-K/refs/heads/master/exomol2.py")
39        wget.download("https://raw.githubusercontent.com/exoclime/HELIOS-K/refs/heads/master/exomol2.py")
40        from exomol2 import main as exomol2_main
41        exomol2_main()
42
43    isotopologue_ex, name_linelist_ex, path_ex = np.loadtxt(exfile, usecols=(1,2,3), unpack=True, dtype=str)
44    #name_linelist = "1H2-16O__BT2"
45
46    name_linelist0 = name_linelist_ex[isotopologue_ex == isotopologue]
47    path0 = path_ex[isotopologue_ex == isotopologue]
48
49    if line_list is None:
50         print("Available linelists for", molecule, "are:")
51         print(name_linelist0)
52         return
53    elif line_list == "first-found":
54        ### try to get first line list that exists
55        for name_linelist in name_linelist0:
56            if name_linelist == "first-found": continue # avoid infinite loop
57            try:
58                download_exomol(molecule, isotopologue, name_linelist, output_dir, output_format)
59                break
60            except Exception as e:
61                print(f"For {name_linelist}: {e}")
62                pass
63        return
64    else:
65        name_linelist = [i for i in name_linelist0 if line_list in i][0]
66        path = [p for i,p in enumerate(path0) if line_list in name_linelist0[i]][0]
67
68    ### get desired output format from user input
69    matching_formats = [fmt for fmt in output_formats if output_format.lower() in fmt.lower()]
70    if not matching_formats:
71        raise ValueError(f"Invalid output format: {output_format}. Valid formats are: {', '.join(output_formats)}")
72    output_format = matching_formats[0]
73
74    ### download archive and extract it
75    filename = name_linelist + "." + output_format
76    url = exomol_url + path + "/" + filename
77    print(f"URL: {url}")
78    filename = os.path.join(output_dir, filename)
79    os.makedirs(output_dir, exist_ok=True)
80
81    output_name = filename
82    if molecule is not None:
83        # if molecule is set, do not use isotopologue in output name
84        output_name = molecule + "__" + name_linelist.split("__")[1] + "." + output_format
85
86    wget.download(url, output_name)
Note: See TracBrowser for help on using the repository browser.