#SPectral ANalysis software (SPAN).
#Written by Daniele Gasparri#

"""
    Copyright (C) 2020-2025, Daniele Gasparri

    E-mail: daniele.gasparri@gmail.com

    SPAN is a GUI interface that allows to modify and analyse 1D astronomical spectra.

    1. This software is licensed **for non-commercial use only**.
    2. The source code may be **freely redistributed**, but this license notice must always be included.
    3. Any user who redistributes or uses this software **must properly attribute the original author**.
    4. The source code **may be modified** for non-commercial purposes, but any modifications must be clearly documented.
    5. **Commercial use is strictly prohibited** without prior written permission from the author.

    DISCLAIMER:
    THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    How to run: just compile the code with Python 3.X and use the pre-loaded example files
    to play with the GUI.
    Check the Python packages needed in the "readme_span.txt" to run this source code.

"""

######################################################################################
#Version histories
#Version 6.4 (20250220):
#Complete refactoring of the code, split in different modules. Use of the DataClass to store the GUI parameters and use.

#Version 6.3 (20250205):
#The spectral manipulation tasks are now grouped in a secondary panel called Spectra manipulation. Removed the GIST pipeline for copyright issues.
#Added the two component stellar fitting for the stars and kinematics task.
# Major modifications to the 'DataCube extraction' sub-program. Added the manual binning mode by drawing custom regions on the datacube.

#Version 6.2 (20250115):
#Added the option to insert a custom template set in pPXF .npz format for the 'Stellar populations and SFH' task
#Major changes to the 'cube_extract' module to improve efficiency

#Version 6.1 (20250107):
#Major change to the spectra manipulation frame. Re-arranged the spectra math tasks and introduced the possobility for the user to re-order the spectra manipulation tasks.
#Added the dust parametrization for the 'Stars and gas kinematics' tasks.

#Version 6.0 (20241118):
#Major change to the layout and the structure of the main span code. Now the code recognizes the operating system used and adapts the layout. No need to compile different files for different operating system anymore.

#Version 5.8 (20241114):
# Modified the 'Stars and gas kinematics' and the 'Stellar populations and SFH' tasks to deal with cosmological redshift galaxy spectra, fixed some minor bugs, cleaned the code and rearranged the plots generated for better clarity.
#Modified the Stellar populations and SFH task to deal either with log10 ages and linear ages.
# Changed from ASCII files to FITS 1d format the residual, emission corrected and best template spectra generated by the stellar populations and SFH task.
# Fixed a bug in the Line-strength analysis and the Lick/IDS option that caused wrong measurements when the emission correction was disabled and doppler correction enabled.
# Fixed the plots generated by the Line-strength analysis task in the 'Process all' mode.
# Added the selection of custom EMILES SSP templates also for kinematics
# Now it is possible to insert any sMILES template in the spectralTemplates/sMILES_afeh folder to work with alpha/Fe in the 'Stellar populations and SFH' task.

#Version 5.7 (20241029):
#Major changes to 'Cube extraction' to be more flexible and more user friendly. Introduced the datacube dynamic visualisation in a matplotlib window and the 'Generate mask' button that allows the user to graphically generate a spectral mask.
#Major changes to the "Stellar populations and SFH" task to include: sMILES models with alpha/Fe, dust absorption (replacing the obsolete reddening keyword of pPXF), uncertainties estimation with bootstrap simulations, auto noise estimation for the fitting so that Chi2 = 1 for non regularized fits and masking user defined spectral regions. Improved plots, now plotting also the alpha/Fe-[Z/H] weights. Saving the luminosity and mass weights.
#Stellar and gas kinematics: fixed the xshooter library that could not be used with spetra with R = constant resolution.
# Changed the Dopcor task, now called 'Doppler/z correction': now the user can correct the spectra both with radial velocity values and redshift (z).
# Adjusted the layout of the 'Cross-corr parameters' and 'Sigma parameters' windows. Added and improved the cross-correlation task: now it can consider also the cosmological redshift instead only velocity values.
# Added the help files for the 'Cube extraction'
# The user defined parameters are saved now also for the sub-programs.
# Corrected minor bugs in the system_span read_spec function and added the read_datacube function.
# Corrected minor bugs in the spec_analysis.py module

#Version 5.6 (20240824):
#Modified the "Stars and kinematics" task to include more parameters.

#Version 5.5 (20240724):
#Integrated the GPR Machine learning for Lick/IDS analysis. Corrected minor bugs.

#Version 5.4 (20240527):
#Integrated the full GIST pipeline. Added the xshooter SSP library (XSL) of Verro et al. 2022 to 'Stars and gas kinematics' and 'Stellar populations and SFH' tasks. Added the custom (E)MILES template selection for 'Stellar populations and SFH' task, the possibility to mask the emission lines and an algorithm to find automatically the best ppxf parameters for stellar populations: noise and Regul. err. Changed the format of the fits file saved in the 'Convert spectrum to' task. Corrected minor bugs.

#Version 5.3 (20240515):
#Integrated a modified version of the GIST pipeline in order to bin and extract data cube spectra. Rearranged the Open spectra frame. Changeg the names of some spectral analysis tasks.

#Version 5.2 (20240505):
#Embedd freesimplegui, improved ppxf stellar populations with more parameters and better plots.

#Version 5.1 (20240502):
#Changed Pysimplegui with Freesimplegui since pysimplegui is not free anymore. Added pixel noise to ppxf stellar populations layout and changed the file format of saved plots, from eps to high resolution png.

#Version 5.0 (20240221):
#Rearranged the spectra pre-processing framework by incorporating crop and wavelet cleaning tasks.
#Added parameters buttons to sigma clip cleaning (now dynamic cleaning), dopcor, and heliocor.
#Fixed a bug in the "Compare with" task.
#Introduced the continuum modeling task, allowing for a more refined way to model and subtract (or divide) the continuum of the spectra.

#Version 4.7 (20240201):
#Since PySimpleGUI is not free anymore, I switched to FreeSimpleGUI. Moreover FreeSimpleGUI license permits redistribution, so I embedded it in the distribution. Thank you FreeSimpleGUI guys for saving SPAN!
#Adapted the code to the new release of ppxf. Ppxf is no longer included in the code package and must be installed via pip.
#Provided the option to choose between three stellar libraries for the ppxf tasks (kinematics and stellar populations).
#Added the capability to save all parameters and load them.
#Implemented some minor changes to the layout.

#Version 4.6 (20240114):
#Included the emission correction and interpolation of the Lick/IDS indices in the EW measurement task. This provides information about the age, metallicity, and alpha-enhancement of the stellar populations of galaxies using the models of Thomas 2010.
#Implemented Monte Carlo simulations to estimate uncertainties on stellar parameters via Lick/IDS index interpolation.

#Version 4.5 (20231221):
#Added the Lick/IDS index measurements and sigma correction in the EW task.

#Version 4.4 (20230215):
#Introduced the 2D spectra extraction standalone panel (still experimental), enabling the correction and extraction of long-slit 1D spectra from a 2D fits wavelength-calibrated images.

#Version 4.3 (20230207):
#Added the plot window to display the results generated by SPAN.
#Implemented the "match rows" feature in the text editor to match and merge rows of two ASCII files, space-separated, with one column in common.

#Version 4.2 (20231203):
#Introduced the fits header editor button with three possible operations on the fits files:
#Modify the keyword of a single file.
#Modify the keywords of a list of files.
#Extract a keyword from a list of fits files.

#Version 4.1 (20231202):
#Added the blackbody fitting task.

#Version 4.0 (20231123):
#Adjusted the layout of the spectral analysis.
#Added the terminal output.
#Fixed various bugs.
#Rectified the progress bar.
#Introduced the text editor and the menu bar.

#Version 3.5 (20221120):
#Added the ppxf algorithm for kinematics and stellar populations.

#Version 3.4 (20221114):
#Included the Line(s) fitting task.
#Corrected minor bugs.

#Version 3.3 (20221109):
#Modified the Cross-correlation calculation.
#Fixed a bug that caused the program to crash if two spectral analysis tasks were selected.
#Implemented checks and existing conditions on the spectral analysis files and values.

#Version 3.2 (20221106):
#Added tooltips to the tasks/buttons for better clarification of their functions.

#Version 3.1 (20221105):
#Rearranged the GUI.
#Fixed a bug in the sigma coefficient calculation task.

#Version 3.0 (20221104):
#Implemented key values instead of increasing numbers in the GUI. This allows adding and modifying entries without changing all the value numbers.

#Version 2.95 (20220128):
#Added the calculation of the equivalent width (EW) and uncertainties also in magnitude.

#Version 2.9 (20220119):
#Incorporated SNR determination in the EW task. Now, when measuring the EW, the program also provides the SNR calculated in the pseudo-continuum bands.

#Version 2.7 (20211124):
#Adjusted the Sigma coeff and EW correct tasks to be truly standalone, with a proper cycle and progress bar.

#Version 2.6 (20211123):
#Adjusted the position of the preview result button to fit the low resolution of my screen after the upgrade of PySimpleGUI, which enlarged my window automatically and without reason.

#Version 2.5 (20211123):
#Added a progress meter for the "Process All" event.

#Version 2.4 (20211122):
#Now you can finally process just one spectrum!

#Version 2.3 (20211122):
#Added the degrade function in lambda (FWHM) to degrade the spectrum to a certain FWHM in Angstrom.

#Version 2.2 (20210927):
#Adjusted the utility menu and added the possibility to save the SNR of the spectra to a file.

#Version 2.1 (20210916):
#Rearranged the utility menu and introduced the conversion of flux from Jansky to MKS and MKS f_lambda.

#Version 2.0 (20210630):
#Added the 'compare spectra' utility.
#Improved the sigma clipping, now called the clean task.
#Rearranged the tasks in the pre-processing and processing frames.
#Enhanced the plot for velocity dispersion measurements.
#Implemented a check on the existence of the loaded spectra.

#Version 1.9 (20210626):
#Some cosmetic changes to the graphical layout.

#Version 1.8 (20210626):
#Added the save plot option in the spectral analysis frame.
#Improved plots of the sigma coeff determination task.

#Version 1.7 (20210615):
#Changed the file names of the result files to include a timestamp, preventing overwriting of older files. Changed the name of the probram.

#Version 1.6 (20200711):
#Added the task to measure the resolution of the selected spectra in the Utility tab. Some major changes to layout.

#Version 1.0 (202006015):
#Improved the read_spec function for the 1dFit spec, excluding PyAstronomy.pyasl since it is not working properly. Now the program is called SPAN and not SPEIM anymore. Welcome SPAN!
