Пример #1
0
def test_valdfile():
    """Test class to read a VALD line data file."""
    testdir = dirname(__file__)
    linelist = ValdFile(join(testdir, "testcase1.lin"))

    assert len(linelist) == 44
    assert linelist.lineformat == "short"
    assert linelist.medium == "air"
    assert linelist[0].species == "V 1"

    with pytest.raises(IOError):
        ValdFile(join(testdir, "testcase1.npy"))
Пример #2
0
def test_medium():
    """Test class to read a VALD line data file.
    """
    testdir = dirname(__file__)
    vf = ValdFile(join(testdir, "testcase1.lin"), medium="vac")

    assert vf.medium == "vac"
Пример #3
0
def main(fname_in, fname_out, linelist_format="VALD", log_file=None):
    if log_file is not None:
        start_logging(log_file)

    if linelist_format.lower() == "vald":
        linelist = ValdFile(fname_in)
    else:
        raise ValueError(
            f"Unrecognised linelist format f{linelist_format}. Expected one of ['VALD']"
        )

    data = linelist._lines.to_dict(orient="records")

    result = {
        "info": {
            "format": linelist.lineformat,
            "medium": linelist.medium,
            "citation_info": linelist.citation_info,
        },
        "data": data,
    }

    with open(fname_out, "w") as f:
        json.dump(result, f)

    return result
Пример #4
0
    def create_first_guess(
        self,
        spectrum,
        star,
        blaze,
        linelist,
        detector,
        uncs=None,
    ):
        print("Extracting stellar parameters...")

        # Create SME structure
        print("Preparing PySME structure")
        sme = SME_Structure()
        sme.wave = [wave.to_value(u.AA) for wave in spectrum.wavelength]
        sme.spec = [spec.to_value(1) for spec in spectrum.flux]
        if spectrum.uncertainty is not None:
            sme.uncs = [
                unc.array * unc.unit.to(1) for unc in spectrum.uncertainty
            ]

        sme.teff = star.teff.to_value(u.K)
        sme.logg = star.logg.to_value(1)
        sme.monh = star.monh.to_value(1)
        sme.vturb = star.vturb.to_value(u.km / u.s)

        sme.abund = "solar"
        sme.linelist = ValdFile(linelist)

        sme.atmo.source = "marcs"
        sme.atmo.method = "grid"

        nlte = None
        if nlte is not None:
            for elem, grid in nlte.items():
                sme.nlte.set_nlte(elem, grid)

        sme.cscale_flag = "none"
        sme.normalize_by_continuum = True
        sme.vrad_flag = "fix"
        sme.vrad = star.radial_velocity.to_value("km/s")

        if detector is not None:
            sme.iptype = "gauss"
            sme.ipres = detector.resolution

        # Create an initial spectrum using the nominal values
        # This also determines the radial velocity
        print(
            "Determine the radial velocity using the nominal stellar parameters"
        )
        synthesizer = Synthesizer()
        sme = synthesizer.synthesize_spectrum(sme)
        return sme
Пример #5
0
def make_minimum_structure():
    sme = SME_Struct()
    sme.teff = 5000
    sme.logg = 4.4
    sme.vmic = 1
    sme.vmac = 1
    sme.vsini = 1
    sme.abund = Abund.solar()
    sme.linelist = ValdFile("{}/testcase3.lin".format((cwd)))
    sme.atmo.source = "marcs2012p_t2.0.sav"
    sme.atmo.method = "grid"

    sme.wran = [[6436, 6444]]

    return sme
Пример #6
0
def test_short_format():
    linelist = ValdFile(join(dirname(__file__), "testcase1.lin"))

    assert linelist.lineformat == "short"
    assert len(linelist) == 44
    assert linelist.atomic is not None
    assert linelist.species is not None

    with pytest.raises(AttributeError):
        _ = linelist.lulande

    with pytest.raises(AttributeError):
        _ = linelist.extra

    assert isinstance(linelist.abund, Abund)
    assert isinstance(linelist.atmo, str)
Пример #7
0
def test_long_format():
    linelist = ValdFile(join(dirname(__file__), "testcase3.lin"))

    assert linelist.lineformat == "long"
    assert len(linelist) == 67
    assert linelist.atomic is not None
    assert linelist.species is not None
    assert linelist.lulande is not None
    assert linelist.extra is not None

    assert linelist.reference is not None
    assert linelist.error is not None
    assert linelist.term_lower is not None
    assert linelist.term_upper is not None

    assert isinstance(linelist.abund, Abund)
    assert isinstance(linelist.atmo, str)
Пример #8
0
def sme_2segments():
    cwd = dirname(__file__)

    sme = SME_Struct()
    sme.teff = 5000
    sme.logg = 4.4
    sme.vmic = 1
    sme.vmac = 1
    sme.vsini = 1
    sme.abund = Abund(monh=0, pattern="asplund2009")
    sme.linelist = ValdFile("{}/testcase1.lin".format((cwd)))
    sme.atmo.source = "marcs2012p_t2.0.sav"
    sme.atmo.method = "grid"

    sme.wran = [[6550, 6560], [6560, 6574]]

    sme.vrad_flag = "none"
    sme.cscale_flag = "none"
    return sme
Пример #9
0
def create_sme_structure(teff=5770, logg=4.4):
    examples_dir = os.path.dirname(os.path.realpath(__file__))
    in_file = os.path.join(examples_dir, "sun_6440_grid.inp")
    # Load your existing SME structure or create your own
    sme = SME.SME_Structure.load(in_file)
    sme.abund = Abund(0.05, "asplund2009")
    sme.linelist = ValdFile(os.path.join(examples_dir, "sun.lin"))
    # Change parameters if your want
    sme.vrad_flag = "none"
    sme.cscale_flag = "none"
    sme.cscale_type = "mask"
    sme.vsini = 0
    sme.vrad = 0
    # Set input parameters
    sme.teff = teff
    sme.logg = logg
    # Start SME solver
    sme = synthesize_spectrum(sme)
    return sme
Пример #10
0
    def synthesize(self, wrange, mu=None, intensities=False):
        sme = SME_Structure()
        # TODO other stellar parameters
        sme.teff = self.star.teff.to_value(u.K)
        sme.logg = self.star.logg.value
        sme.monh = self.star.monh
        sme.vturb = self.star.vturb.to_value(u.km / u.s)

        if mu is not None:
            sme.mu = mu

        sme.abund = self.abund
        sme.linelist = ValdFile(self.linelist)

        sme.atmo.source = self.atmosphere
        sme.atmo.method = "grid"

        if self.nlte is not None:
            for elem, grid in self.nlte.items():
                sme.nlte.set_nlte(elem, grid)

        sme.wran = [[wr.lower.to_value(u.AA),
                     wr.upper.to_value(u.AA)] for wr in wrange]

        sme.cscale_flag = "none"
        sme.normalize_by_continuum = self.normalize
        sme.vrad_flag = "none"

        synthesizer = Synthesizer()
        if not intensities:
            sme = synthesizer.synthesize_spectrum(sme)
            wave, spec = sme.wave, sme.synth
        else:
            sme.specific_intensities_only = True
            wave, spec, cont = synthesizer.synthesize_spectrum(sme)

        wave = [w << u.AA for w in wave]
        if self.normalize:
            spec = [s << u.one for s in spec]
        else:
            spec = [s << flux_units for s in spec]

        return wave, spec, sme.citation("bibtex")
Пример #11
0
    # this will put everything into the example dir
    target = "sun"
    examples_dir = join(dirname(realpath(__file__)), "..")
    in_file = join(examples_dir, "sun_6440_test.inp")
    out_file = join(examples_dir, f"{target}.sme")
    plot_file = join(examples_dir, f"{target}.html")
    log_file = join(examples_dir, f"{target}.log")

    # Start the logging to the file
    util.start_logging(log_file)

    # Load your existing SME structure or create your own
    sme = SME.SME_Structure.load(in_file)

    sme.abund = Abund(0, "asplund2009")
    sme.linelist = ValdFile(join(examples_dir, "sun.lin"))
    sme.nmu = 7
    sme.vrad = 0
    sme.cscale = None
    sme.vrad_flag = "none"
    sme.cscale_flag = "none"
    sme.cscale_type = "match"

    sme.atmo.source = "marcs2012.sav"

    # save_as_idl(sme, "speedtest.inp")
    # Run it once to load the atmosphere
    start = time.time()
    sme = synthesize_spectrum(sme)
    end = time.time()
    runtime = end - start
Пример #12
0
    # Get input files
    if len(sys.argv) > 1:
        in_file, vald_file, fitparameters = util.parse_args()
    else:
        examples_dir = "/DATA/ESO/HARPS/K2-3/"
        in_file = os.path.join(examples_dir, "K2-3_red_c.ech")
        vald_file = os.path.expanduser("~/Documents/IDL/SME/harps_red.lin")
        atmo_file = "marcs2012p_t1.0.sav"
        fitparameters = []

    # Load files
    sme = SME.SME_Structure.load(in_file)

    if vald_file is not None:
        vald = ValdFile(vald_file)
        sme.linelist = vald

    if atmo_file is not None:
        sme.atmo.source = atmo_file
        sme.atmo.method = "grid"
        sme.atmo.geom = "PP"

    # Choose free parameters, i.e. sme.pname
    if len(fitparameters) == 0:
        # ["teff", "logg", "monh", "abund Mg", "abund Y"]
        if sme.fitparameters is not None and len(sme.fitparameters) != 0:
            fitparameters = sme.fitparameters
        else:
            fitparameters = ["teff", "logg", "monh"]
Пример #13
0
    # Get first guess from literature values
    sme.teff = star["t_eff"].to_value("K") if "t_eff" in star else 6000
    sme.logg = star["logg"].to_value(1) if "logg" in star else 4
    monh = star["metallicity"].to_value(1) if "metallicity" in star else 0
    sme.abund = Abund(monh, "asplund2009")
    sme.vmic = (
        star["velocity_turbulence"].to_value("km/s")
        if "velocity_turbulence" in star
        else 3
    )
    # Test this
    sme.vmic = 0
    sme.vsini = 0

    # load the linelist
    sme.linelist = ValdFile(vald_file)

    # Set the atmosphere grid
    sme.atmo.source = "marcs2014.sav"
    sme.atmo.geom = "PP"

    # Add NLTE corrections
    sme.nlte.set_nlte("Al", "nlte_Al_ama51_pysme.grd")
    sme.nlte.set_nlte("Ba", "nlte_Ba_ama51_pysme.grd")
    sme.nlte.set_nlte("Ca", "nlte_Ca_ama51_pysme.grd")
    sme.nlte.set_nlte("C", "nlte_C_ama51_pysme.grd")
    sme.nlte.set_nlte("H", "nlte_H_ama51_pysme.grd")
    sme.nlte.set_nlte("K", "nlte_K_ama51_pysme.grd")
    sme.nlte.set_nlte("Li", "nlte_Li_ama51_pysme.grd")
    sme.nlte.set_nlte("Mg", "nlte_Mg_ama51_pysme.grd")
    sme.nlte.set_nlte("Mn", "nlte_Mn_ama51_pysme.grd")
Пример #14
0
    # Define the location of all your files
    # this will put everything into the example dir
    target = "sun"
    examples_dir = os.path.dirname(os.path.realpath(__file__))
    in_file = os.path.join(examples_dir, "sun_6440_grid.inp")
    out_file = os.path.join(examples_dir, f"{target}.sme")
    plot_file = os.path.join(examples_dir, f"{target}.html")
    log_file = os.path.join(examples_dir, f"{target}.log")

    # Start the logging to the file
    util.start_logging(log_file)

    # Load your existing SME structure or create your own
    sme = SME.SME_Structure.load(in_file)
    sme.abund = Abund(0, "asplund2009")
    sme.linelist = ValdFile(os.path.join(examples_dir, "sun.lin"))

    # Change parameters if your want
    sme.vsini = 0
    sme.vrad = 0.35
    sme.vrad_flag = "each"
    sme.cscale_flag = "linear"
    sme.cscale_type = "mask"

    # Define any fitparameters you want
    # For abundances use: 'abund {El}', where El is the element (e.g. 'abund Fe')
    # For linelist use: 'linelist {Nr} {p}', where Nr is the number in the
    # linelist and p is the line parameter (e.g. 'linelist 17 gflog')
    fitparameters = ["teff", "logg", "monh"]

    # Start SME solver