meafs_code.scripts package

Submodules

meafs_code.scripts.abundance_plot module

MEAFS Abundance Analysis
Matheus J. Castro
This program generates graphics to analyse the results obtained with the abundance_fit.py
For this code work, it must be in the same folder as the abundance_fit.py file
meafs_code.scripts.abundance_plot.erase_emission_order(abund)[source]

Erase the emission order in the database.

Parameters:

abund – pandas element with the abundances for each line.

Returns:

the abundance dataframe without the emission orders.

meafs_code.scripts.abundance_plot.erase_null_abund(abund)[source]

Erase the empty slots in the database.

Parameters:

abund – pandas element with the abundances.

Returns:

the abundance dataframe without the null elements.

meafs_code.scripts.abundance_plot.folders_creation(folder)[source]

Subroutine to create necessary folders

Parameters:

folder – root folder of the results.

meafs_code.scripts.abundance_plot.get_diff(spec1, spec2)[source]

Get the difference of observed and synthetic spectrum.

Parameters:
  • spec1 – first spectrum.

  • spec2 – second spectrum.

Returns:

-1 if it finds nothing; otherwise the difference.

meafs_code.scripts.abundance_plot.get_spectrum(fit_data, conv_name, config_fl, elem, abundance_shift=0.0, cut_val=1.0)[source]

Function to get the synthetic spectrum in the desired range.

Parameters:
  • fit_data – pandas dataframe with the results.

  • conv_name – file name of the TurboSpectrum output file.

  • config_fl – file name of the TurboSpectrum configuration file.

  • elem – element to be plotted.

  • abundance_shift – overall shift in abundance, if needed.

  • cut_val – range to plot.

Returns:

the final spectrum.

meafs_code.scripts.abundance_plot.main(args)[source]

Main Routine.

Parameters:

args – command line arguments.

meafs_code.scripts.abundance_plot.plot_abund_box(abund, elements, folder, ui=None)[source]

Create the abundance box plot.

Parameters:
  • abund – pandas element with the abundances.

  • elements – elements to create histograms.

  • folder – directory to be saved.

  • ui – the main ui class in the GUI.

meafs_code.scripts.abundance_plot.plot_abund_hist(abund, elements, folder, ui=None, bins=20)[source]

Creeate histograms for abundance for each element and trace a gaussian with the mean and standard deviation.

Parameters:
  • abund – pandas element with the abundances.

  • elements – elements to create histograms.

  • folder – directory to be saved.

  • ui – the main ui class in the GUI.

  • bins – define the number of bins for the histogram.

meafs_code.scripts.abundance_plot.plot_differ_hist(abund, elements, folder, ui=None, bins=20)[source]

Plot histograms for abundance shift for each element and trace a gaussian with the mean and standard deviation

Parameters:
  • abund – pandas element with the abundances.

  • elements – elements to create histograms.

  • folder – directory to be saved.

  • ui – the main ui class in the GUI.

  • bins – define the number of bins for the histogram.

meafs_code.scripts.abundance_plot.plot_lines(obs_specs, abund, refer_fl, type_synth, folder, cut_val=0.5, abundance_shift=0.1, drop=0, ui=None)[source]

Plot the spectrum fit and the observed one

Parameters:
  • obs_specs – spectrum data.

  • abund – abundance pandas object.

  • refer_fl – file name of the reference abundances file.

  • type_synth – type of the current synthetics spectrum generator.

  • folder – directory to save.

  • cut_val – range to plot the lines.

  • abundance_shift – overall abundance shift.

  • drop – remove elements of the abund dataframe.

  • ui – the main ui class in the GUI.

meafs_code.scripts.abundance_fit module

MEAFS Abundance Fit
Matheus J. Castro
Main fit code to do all the operations with the spectrum, call the modules to generate the synthetic spectrum, plot the curves and more.
meafs_code.scripts.abundance_fit.args_menu(args)[source]

Run the arguments resolve and print them if necessary (previously necessary in the older no-gui version).

Parameters:

args – system command line arguments.

meafs_code.scripts.abundance_fit.check_order(elem)[source]
Check if the current element has any type of order written in the end of the string.
Supported types are: roman numbers (I, II, III…) and western digits (1, 2, 3…).
Parameters:

elem – the element string

Returns:

the element without the order and the order itself.

meafs_code.scripts.abundance_fit.create_basic_folder_structure(folder)[source]

Create necessary folders

Parameters:

folder – the path to create the folder.

meafs_code.scripts.abundance_fit.cur_time()[source]

Function to return the formated local current time.

Returns:

the local formated time

meafs_code.scripts.abundance_fit.fit_abundance(linelist, spec_obs, refer_fl, folder, type_synth, cut_val=None, abund_lim_df=1.0, restart=False, save_name='found_values.csv', ui=None, canvas=None, ax=None, plot_line_refer=None, opt_pars=None, repfit=2, max_iter=None, convovbound=None, contpars=None, wavebound=None, only_abund_ind=None, spec_count=None, spec_iter=None)[source]

Main function to analyse the spectrum and find the fit values for it

Parameters:
  • linelist – linelist dataframe.

  • spec_obs – spectrum data.

  • refer_fl – reference abundace dataframe.

  • folder – directory to save.

  • type_synth – type of the current synthetics spectrum generator.

  • cut_val – ranges to cut the spectrum.

  • abund_lim_df – default value of the range of the allowed abundance.

  • restart – if it should ignore past results in the database.

  • save_name – file name of the previous results.

  • ui – main GUI QT object.

  • canvas – canvas to plot.

  • ax – ax to plot.

  • plot_line_refer – array to save the reference label of the plots.

  • opt_pars – convolution, wavelength shift and continuum initial guess.

  • repfit – number of iterations of the main fit function.

  • max_iter – maximum allowed iterations of the Nelder-Mead method.

  • convovbound – range to fit the convolution.

  • contpars – the calibration values of the overall continuum fit method.

  • wavebound – range to fit the wavelength shift.

  • only_abund_ind – change only abundance without fitting other parameters at this index.

  • spec_count – defines the total number of spectra to be analysed.

  • spec_iter – defines the current spectrum index.

Returns:

dataframe with the results of the fit, the actualized ax array and the actualized plot_line_refer array.

meafs_code.scripts.abundance_fit.get_sep(file)[source]

Get the delimiter automatically from an ASCII file using Sniffer.

Parameters:

file – file name to read.

Returns:

the delimiter.

meafs_code.scripts.abundance_fit.get_specs_lims(specs, overlim_x=False, overlim_y=False, margin=0.02)[source]

Get the full spectra array upper and down limits for x and y for a full plot.

Parameters:
  • specs – list of spectra.

  • overlim_x – if extrapolate the x limits by margin or not.

  • overlim_y – if extrapolate the y limits by margin or not.

  • margin – the relative extrapolation value (0.02 = 2% margin on each side).

Returns:

the limits in two tuples (x and y).

meafs_code.scripts.abundance_fit.gui_call(spec_obs, ui, checkstate, canvas, ax, cut_val=None, plot_line_refer=None, opt_pars=None, repfit=2, abundplot=None, results_array=None, only_abund_ind=None, final_plot=False, plot_type=None, single=None)[source]

Main function to be called from the GUI.

Parameters:
  • spec_obs – spectrum data.

  • ui – main GUI QT object.

  • checkstate – QT checked state for checkboxes.

  • canvas – GUI canvas plot object.

  • ax – GUI ax plot object.

  • cut_val – range to cut the spectrum.

  • plot_line_refer – array with a list of the current available plots.

  • opt_pars – convolution, wavelength shift and continuum parameters.

  • repfit – number of iterations of the main fit function.

  • abundplot – overwrite the abundance fit and only plot the value present in this variable.

  • results_array – array for the results of the fit.

  • only_abund_ind – change only abundance without fitting other parameters at this index.

  • final_plot – if it’s meant to create the final plots instead of fitting.

  • plot_type – type of the final plot to create.

  • single – create a final plot to a single line or for all of them.

Returns:

the results of the fit, the actualized ax variable and the actualized plot_line_refer variable.

meafs_code.scripts.abundance_fit.log_write(folder, msg)[source]

Function to write a message in the log and print on the GUI/terminal.

Parameters:
  • folder – folder path of the log file.

  • msg – the message to be written.

meafs_code.scripts.abundance_fit.main(args)[source]

Main subroutine to call the functions (previously necessary in the older no-gui version).

Parameters:

args – system command line arguments.

meafs_code.scripts.abundance_fit.open_linelist_refer_fl(list_name)[source]

Open the Linelist and reference abundance file using PANDAS.

Parameters:

list_name – file name of the linelist.

Returns:

the pandas dataframe with the linelist.

meafs_code.scripts.abundance_fit.open_previous(linelist, columns_names, fl_name=PosixPath('found_values.csv'))[source]

Open the previous results and analyse it.

Parameters:
  • linelist – linelist object.

  • columns_names – names of the columns in the file.

  • fl_name – file name.

Returns:

the new linelist and the pandas dataframe with the previous results.

meafs_code.scripts.abundance_fit.open_spec_obs(observed_name, delimiter=None, increment=1)[source]

Open the observed spectrum file using PANDAS.

Parameters:
  • observed_name – file name with the spectrum.

  • delimiter – delimiter type of the file.

  • increment – increment to get the delimiter automatically (previously necessary in the older no-gui version).

Returns:

the pandas dataframe with the spectrum.

meafs_code.scripts.abundance_fit.plot_abund_nofit(elem, lamb, abundplot, refer_fl, folder, type_synth, cut_val=None, canvas=None, ax=None, plot_line_refer=None, opt_pars=None)[source]

Plot in the GUI the specified parameters.

Parameters:
  • elem – current element.

  • lamb – current wavelength.

  • abundplot – abundance to be plotted.

  • refer_fl – reference abundance dataframe.

  • folder – directory to save.

  • type_synth – type of the current synthetics spectrum generator.

  • cut_val – range to plot.

  • canvas – canvas to plot.

  • ax – ax to plot.

  • plot_line_refer – array to save the reference label of the plots.

  • opt_pars – parameters for the convolution, wavelength shift and continuum.

Returns:

the actualized plot_line_refer array.

meafs_code.scripts.abundance_fit.plot_spec(spec1, spec2, spec3, lamb, elem, folder, show=False, save=True)[source]

Plot the spectra (previously necessary in the older no-gui version).

Parameters:
  • spec1 – first spectrum data.

  • spec2 – second spectrum data.

  • spec3 – third spectrum data.

  • lamb – current wavelength.

  • elem – current element.

  • folder – directory to save the plot.

  • show – true to show with the matplotlib.pyplot.show().

  • save – true to save the spectrum.

meafs_code.scripts.abundance_fit.plot_spec_gui(specs, canvas, ax, plot_line_refer, color=None, overlim_x=False, overlim_y=False)[source]

Plot the spectrum in the GUI.

Parameters:
  • specs – spectrum data to be plotted.

  • canvas – canvas to plot.

  • ax – ax to plot.

  • plot_line_refer – array to save the reference label of the plots.

  • color – color to plot the lines.

  • overlim_x – if extrapolate the x limits by 10% or not.

  • overlim_y – if extrapolate the y limits by 10% or not.

Returns:

the actualized plot_line_refer array.

meafs_code.scripts.abundance_fit.plot_spec_ui(spec_fit_arr, folder, elem, lamb, order, ax, canvas, plot_line_refer, vline=True, save=True, overlim_x=False, overlim_y=True, enable_lim=True, color=None)[source]

Plot the current line results in the GUI.

Parameters:
  • spec_fit_arr – spectrum line data.

  • folder – directory to save.

  • elem – current element.

  • lamb – current wavelength.

  • order – current order of the line.

  • ax – ax to plot.

  • canvas – canvas to plot.

  • plot_line_refer – array to save the reference label of the plots.

  • vline – if it also plots a vertical line or not.

  • save – whether to save or not the plot in csv format.

  • overlim_x – if extrapolate the x limits by 10% or not.

  • overlim_y – if extrapolate the y limits by 10% or not.

  • enable_lim – enable or disable to change the plot range.

  • color – set the color of the line plot.

Returns:

the actualized plot_line_refer array.

meafs_code.scripts.abundance_fit.read_config(config_name)[source]

Function to read the configuration file (previously necessary in the older no-gui version).

Parameters:

config_name – configuration file name.

Returns:

linelist file name, reference abundance file name, type of the synthetics spectrum to use, TurboSpectrum configuration file name, TurboSpectrum results file name, directory to save the results, spectrum file name.

meafs_code.scripts.abundance_fit.save_spec(spec, fl_name, delimiter=',')[source]

Save spectrum in ASCII of FITS.

Parameters:
  • fl_name – file name to use.

  • spec – spectrum data.

  • delimiter – delimiter to use in ASCII formats.

meafs_code.scripts.bisec_interpol module

Bisection and \(\chi^2\)
Matheus J. Castro
v1.0
This program uses the bisection script to find position values of an array and calculates the \(\chi^2\) of two arrays.
The goal is not to run the program itself, but use it as a shared library inside python.
int bisec(float spec[], int len, float lamb)
[source]

Function that applies the bisection method.

Parameters:
  • spec[] (float*) – the array to look for.

  • len (int) – the size of the array.

  • lamb (float) – the value to find the index.

Returns:

the left index of the searched value.

float chi2(float spec1x[], float spec1y[], int len1, float spec2x[], float spec2y[], int len2)
[source]

Function to find the \(\chi^2\) of two arrays.

Parameters:
  • spec1x[] (float*) – the first 1D-array (x axis).

  • spec1y[] (float*) – the first 1D-array (y axis).

  • len1 (int) – the size of the first array.

  • spec2x[] (float*) – the second 1D-array (x axis).

  • spec2y[] (float*) – the second 1D-array (y axis).

  • len2 (int) – the size of the second array.

Returns:

the \(\chi^2\).

int main(void)
[source]

Just a warning to not run the code itself.

Returns:

int 0.

meafs_code.scripts.fit_functions module

MEAFS Fit Functions
Matheus J. Castro
Fit functions that are used, like the C library, the \(\chi^2\) calculation, bisection and more.
meafs_code.scripts.fit_functions.bisec(spec, lamb)[source]

Function to apply the bisection script to find a number position in an array.

Parameters:
  • spec – array to analyse.

  • lamb – value to look for.

Returns:

the position.

meafs_code.scripts.fit_functions.c_init(c_name)[source]

Function to initialize the C shared library

Parameters:

c_name – name of the shared C library.

Returns:

the initialized object.

meafs_code.scripts.fit_functions.chi2(spec1, spec2)[source]

Function to find the \(\chi^2\) of two arrays using the C library.

Parameters:
  • spec1 – first array.

  • spec2 – seccond array.

Returns:

the \(\chi^2\).

meafs_code.scripts.fit_functions.cut_spec(spc, lamb, cut_val=1.0)[source]

Function to restrict the array in the desired range

Parameters:
  • spc – the array.

  • lamb – the central position.

  • cut_val – the range to be restricted.

Returns:

the truncated array.

meafs_code.scripts.fit_functions.fit_continuum(spec, contpars=None, iterac=1000, method=0, contdisabled=False, medianwindow=3, hardvalue=1.0)[source]

Fit the overall continuum in the entire spectrum.

Parameters:
  • spec – spectrum data.

  • contpars – the calibration values of the Sigma-clipping method.

  • iterac – maximum number of iterations.

  • method – method to fit the continuum: 0-Chebyshev; 1-Sigma-clipping; 2-Simple Average.

  • contdisabled – disable the continuum fit and use a fixed value.

  • medianwindow – median window for the Chebyshev method.

  • hardvalue – fixed value when not continuum fit.

Returns:

the mean and the standard deviation (continuum and errors).

meafs_code.scripts.fit_functions.line_boundaries(spec, lamb, threshold=0.98, contpars=None, iterac=10000)[source]

Function to retrieve the width position of an absorption line in a spectrum.

Parameters:
  • spec – 2D spectrum.

  • lamb – central wavelength of the line.

  • threshold – threshold in percentage of the continuum to define line limits.

  • contpars – the calibration values for the continuum method.

  • iterac – maximum number of iterations for the continuum method.

Returns:

min ax max position values in the array for the line.

meafs_code.scripts.fit_functions.spec_operations(spec, lamb_desloc=0.0, continuum=1.0, convol=0.0)[source]

Function to apply lambda shift, continuum fit and convolution to the spectrum.

Parameters:
  • spec – the spectrum to be changed.

  • lamb_desloc – the shift in wavelength to be applied.

  • continuum – the continuum to be applied.

  • convol – the convolution in FWHM to be applied.

Returns:

the modified spectrum.

meafs_code.scripts.turbospec_functions module

MEAFS TurboEspectrum Functions
Matheus J. Castro
TurboSpectrum module functions.
meafs_code.scripts.turbospec_functions.change_abund_configfl(fl_name, elem, abund=None, find=True)[source]

Change the element abundance in Turbospectrum2019 configuration file or find the current value.

Parameters:
  • fl_name – file name of the TurboSpectrum configuration file.

  • elem – element to be changed.

  • abund – desired abundance.

  • find – true if it is only to get the current value.

Returns:

abundance.

meafs_code.scripts.turbospec_functions.change_spec_range_configfl(fl_name, lamb, cut_val)[source]

Change the spectrum range in Turbospectrum2019 configuration file

Parameters:
  • fl_name – file name of the TurboSpectrum configuration file.

  • lamb – central wavelength.

  • cut_val – range to be applied.

meafs_code.scripts.turbospec_functions.check_elem_configfl(fl_name, elem)[source]

Find the desired element in the Turbospectrum2019 configuration file.

Parameters:
  • fl_name – file name of the TurboSpectrum configuration file.

  • elem – element to look for.

Returns:

true if finds it; false if not.

meafs_code.scripts.turbospec_functions.optimize_abund(spec_obs_cut, config_fl, conv_name, elem, opt_pars, par, abund_lim, iterac=10)[source]

Fit of the Abundance using the minimization of the \(\chi^2\) with the Nelder-Mead method.

Parameters:
  • spec_obs_cut – spectrum data.

  • config_fl – file name of the TurboSpectrum configuration file.

  • conv_name – file name of the TurboSpectrum output file.

  • elem – element to be fitted.

  • opt_pars – the Continuum, Convolution and Wavelength Shift parameters.

  • par – initial guess for the optimization.

  • abund_lim – range to try the fit.

  • iterac – maximum allowed iterations of the Nelder-Mead method.

Returns:

the abundance, the value of the minimum \(\chi^2\) and the spectrum generated with the best parameters.

meafs_code.scripts.turbospec_functions.optimize_spec(spec_obs_cut, spec_conv, lamb, cut_val, continuum, init=None, iterac=100, convovbound=None, wavebound=None)[source]

Fit of the Convolution, Wavelength Shift and Continuum using the minimization of the \(\chi^2\) with the Nelder-Mead method.

Parameters:
  • spec_obs_cut – spectrum data.

  • spec_conv – synthetic spectrum data.

  • lamb – current wavelength.

  • cut_val – range to cut the spectrum for the convolution fit.

  • continuum – continuum value.

  • init – initial guess values for the Wavelength Shift, Continuum and Convolution.

  • iterac – maximum allowed iterations of the Nelder-Mead method.

  • convovbound – range to fit the convolution.

  • wavebound – range to fit the wavelength shift.

Returns:

the optimized parameters

meafs_code.scripts.turbospec_functions.run_configfl(config_fl)[source]

Run Turbospectrum2019.

Parameters:

config_fl – file name of the TurboSpectrum configuration file.

meafs_code.scripts.unify_plots module

MEAFS Unify generated plots in a single PDF file for each type
Matheus J. Castro
This script aims to create a single result file for each analyze type created with the abundance_fit.py and abundance_analysis.py scripts.
It uses pdflatex to generate it. You can install it following the steps for your OS distribution here: https://www.latex-project.org/get/
For this code work, it must be in the same folder as the abundance_fit.py file
meafs_code.scripts.unify_plots.main(args)[source]

Main function that read the plot files.

Parameters:

args – command line arguments.

meafs_code.scripts.unify_plots.run_pdflatex(fl_name, folder)[source]

Run the pdflatex command to generate the unified PDF.

Parameters:
  • fl_name – file name.

  • folder – directory where the file should be created.

meafs_code.scripts.unify_plots.write(fl_name, data)[source]

Write the data in a file.

Parameters:
  • fl_name – file name.

  • data – data to be written.

meafs_code.scripts.voigt_functions module

MEAFS Voigt Functions
Matheus J. Castro
Voigt, Gaussian, Lorentzian module functions.
meafs_code.scripts.voigt_functions.find_func(type_func)[source]

Determines the function to be called: Gaussian, Lorentzian or Voigt.

Parameters:

type_func – string with the name of the function.

Returns:

the function itself.

meafs_code.scripts.voigt_functions.gaussian(x, b, c, a=1, d=0)[source]

Gaussian function.

(1)\[f(x) = a \cdot \exp\left[\frac{-(x - b)^2}{c \cdot \sqrt{2 \cdot \pi}}\right] + d\]
Parameters:
  • x – can be a list or a number.

  • b – a single number.

  • c – a single number.

  • a – a single number.

  • d – a single number.

Returns:

\(f(x)\) as a number or a list.

meafs_code.scripts.voigt_functions.lorentzian(x, b, c, a=1, d=0)[source]

Lorentzian function.

(2)\[g(x) = a \cdot \exp\left[\frac{c}{\pi \cdot ((x - b)^2 + c^2)}\right] + d\]
Parameters:
  • x – can be a list or a number.

  • b – a single number.

  • c – a single number.

  • a – a single number.

  • d – a single number.

Returns:

\(g(x)\) as a number or a list.

meafs_code.scripts.voigt_functions.optimize_abund(spec_obs_cut, type_synth, lamb, opt_pars, iterac=100)[source]

Fit of the Abundance using the minimization of the \(\chi^2\) with the Nelder-Mead method.

Parameters:
  • spec_obs_cut – spectrum data.

  • type_synth – type of the function to apply.

  • lamb – current wavelength.

  • opt_pars – the Continuum, Convolution and Wavelength Shift parameters.

  • iterac – maximum allowed iterations of the Nelder-Mead method.

Returns:

the abundance, the value of the minimum \(\chi^2\) and the spectrum generated with the best parameters.

meafs_code.scripts.voigt_functions.optimize_spec(spec_obs_cut, type_synth, lamb, continuum, convovbound=None, wavebound=None, iterac=100)[source]

Fit of the Convolution and the Wavelength Shift using the minimization of the \(\chi^2\) with the Nelder-Mead method.

Parameters:
  • spec_obs_cut – spectrum data.

  • type_synth – type of the function to apply.

  • lamb – current wavelength.

  • continuum – continuum value.

  • convovbound – range to fit the convolution.

  • wavebound – range to fit the wavelength shift.

  • iterac – maximum allowed iterations of the Nelder-Mead method.

Returns:

the optimized parameters, the value of the minimum \(\chi^2\) and the spectrum generated with the best parameters.

meafs_code.scripts.voigt_functions.voigt(x, b, c, a, d)[source]

Voigt function.

\[h(x) = a \cdot f(x) \cdot g(x) + d\]

With \(f(x)\) being the (1) and \(g(x)\) being the (2).

Parameters:
  • x – can be a list or a number.

  • b – a single number.

  • c – a single number.

  • a – a single number.

  • d – a single number.

Returns:

\(h(x)\) as a number or a list.

Module contents