<commit_before>""" Module for coadding spectra, and comparing customized and default data reduction
"""

from __future__ import (print_function, absolute_import, division, unicode_literals)

import numpy as np
import glob
import pdb

from matplotlib import pyplot as plt

from astropy.table import Table
from astropy.io import fits
from pypeit.core import coadd as coadd

from linetools.spectra import utils as spltu
import linetools.spectra.xspectrum1d as xspec
from linetools.spectra import utils as ltsu

from xastropy.xutils import xdebug as xdb


def flxwave(fname1, fname2s, xlim=(1900, 1950), ylim=None, norm=True, nseg=0, figsize=(6, 5)):
    """ Plot flux vs wavelength for default and customized data reduction

    Parameters
    ----------
    fname1 : str
      a summ file that is output of the default data reduction
    fname2s : list of str
      summ files that are output ot customized data reductions.
      It is needed that these files have spectra taken with the same optical element and central wavelenght as fname1.
    xlim : float
      displayed wavelength range
    ylim : float
      dislayed flux range
    norm : bool
      normalize fluxes to the same median flux
    nseg : 0,1,2 (for segments A, B, C)
      plot flux for segment nseg

    Returns
    -------

    """

    # default data reduction
    fnm = fname1
    tbl = Table.read(fnm)
    wave1 = tbl['WAVELENGTH'][nseg]
    flx1 = tbl['FLUX'][nseg]
    err1 = tbl['ERROR'][nseg]

    plt.figure(figsize=figsize)
    plt.plot(wave1, flx1, color='blue')
    plt.plot(wave1, err1, '--', color='blue', label='previous')

    # customized data reduction
    colors = ['red', 'orange', 'limegreen']
    for i in range(len(fname2s)):
        fname2 = fname2s[i]
        fnm = fname2
        tbl = Table.read(fnm)
        wave2 = tbl['WAVELENGTH'][nseg]
        flx2 = tbl['FLUX'][nseg]
        err2 = tbl['ERROR'][nseg]

        # normalize to median flux
        if norm:
            sc = np.median(flx1) / np.median(flx2)
        else:
            sc = 1
        print(sc)
        flx2 = flx2 * sc
        err2 = err2 * sc

        # plot
        plt.plot(wave2, flx2, color=colors[i])
        plt.plot(wave2, err2, '--', color=colors[i], label='new ' + str(i+1))

    plt.xlim(xlim)
    if ylim is not None:
        plt.ylim(ylim)
    plt.xlabel('Wavelength')
    plt.ylabel('Flux')
    plt.legend()
    plt.show()






def findsp(fldpth, verbose=True):
    """ Find all NUV and FUV spectra

    Parameters
    ----------
    fldpth : str
       path to the files

    Returns
    -------
    fuvsp : array of XSpectrum1D objects
      FUV spectra
    nuvsp : array of XSpectrum1D objects
      NUV spectra

    """

    # find all spectra
    sumfiles = glob.glob(fldpth + '*sum.fits')

    fuvsp = []
    nuvsp = []
    for i in range(len(sumfiles)):
        spf = sumfiles[i]
        hdu = fits.open(spf)
        # FUV or NUV
        det = hdu[0].header['DETECTOR']
        hdu.close()

        # read data
        tbl = Table.read(spf)
        for j in range(len(tbl)):
            wave = tbl['WAVELENGTH'][j]
            flx = tbl['FLUX'][j]
            err = tbl['ERROR'][j]

            # append xspectrum
            xsp = xspec.XSpectrum1D.from_tuple((wave, flx, err))
            if det == 'NUV':
                nuvsp.append(xsp)
            elif det == 'FUV':
                fuvsp.append(xsp)
            else:
                #print('Not defined opt_elem?')
                raise IOError("Detector not well defined in ", spf)
<commit_msg>Small change in meantime: adding option that could be used not to read segment C spectra. I think these are all changes for now.<commit_after>""" Module for coadding spectra, and comparing customized and default data reduction
"""

from __future__ import (print_function, absolute_import, division, unicode_literals)

import numpy as np
import glob
import pdb

from matplotlib import pyplot as plt

from astropy.table import Table
from astropy.io import fits
from pypeit.core import coadd as coadd

from linetools.spectra import utils as spltu
import linetools.spectra.xspectrum1d as xspec
from linetools.spectra import utils as ltsu

from xastropy.xutils import xdebug as xdb


def flxwave(fname1, fname2s, xlim=(1900, 1950), ylim=None, norm=True, nseg=0, figsize=(6, 5)):
    """ Plot flux vs wavelength for default and customized data reduction

    Parameters
    ----------
    fname1 : str
      a summ file that is output of the default data reduction
    fname2s : list of str
      summ files that are output ot customized data reductions.
      It is needed that these files have spectra taken with the same optical element and central wavelenght as fname1.
    xlim : float
      displayed wavelength range
    ylim : float
      dislayed flux range
    norm : bool
      normalize fluxes to the same median flux
    nseg : 0,1,2 (for segments A, B, C)
      plot flux for segment nseg

    Returns
    -------

    """

    # default data reduction
    fnm = fname1
    tbl = Table.read(fnm)
    wave1 = tbl['WAVELENGTH'][nseg]
    flx1 = tbl['FLUX'][nseg]
    err1 = tbl['ERROR'][nseg]

    plt.figure(figsize=figsize)
    plt.plot(wave1, flx1, color='blue')
    plt.plot(wave1, err1, '--', color='blue', label='previous')

    # customized data reduction
    colors = ['red', 'orange', 'limegreen']
    for i in range(len(fname2s)):
        fname2 = fname2s[i]
        fnm = fname2
        tbl = Table.read(fnm)
        wave2 = tbl['WAVELENGTH'][nseg]
        flx2 = tbl['FLUX'][nseg]
        err2 = tbl['ERROR'][nseg]

        # normalize to median flux
        if norm:
            sc = np.median(flx1) / np.median(flx2)
        else:
            sc = 1
        print(sc)
        flx2 = flx2 * sc
        err2 = err2 * sc

        # plot
        plt.plot(wave2, flx2, color=colors[i])
        plt.plot(wave2, err2, '--', color=colors[i], label='new ' + str(i+1))

    plt.xlim(xlim)
    if ylim is not None:
        plt.ylim(ylim)
    plt.xlabel('Wavelength')
    plt.ylabel('Flux')
    plt.legend()
    plt.show()






def findsp(fldpth, verbose=True, emax=None):
    """ Find all NUV and FUV spectra

    Parameters
    ----------
    fldpth : str
       path to the files
    emax : int
      read extensions 0, 1, ..., emax-1 of sum.fits files (corresponding to segments A,B,C)

    Returns
    -------
    fuvsp : array of XSpectrum1D objects
      FUV spectra
    nuvsp : array of XSpectrum1D objects
      NUV spectra

    """

    # find all spectra
    sumfiles = glob.glob(fldpth + '*sum.fits')

    fuvsp = []
    nuvsp = []
    for i in range(len(sumfiles)):
        spf = sumfiles[i]
        hdu = fits.open(spf)
        # FUV or NUV
        det = hdu[0].header['DETECTOR']
        hdu.close()

        # read data
        tbl = Table.read(spf)
        jmax = len(tbl)
        if (emax is not None):
            if (emax < len(tbl)):
                jmax = emax
        for j in range(jmax):
            wave = tbl['WAVELENGTH'][j]
            flx = tbl['FLUX'][j]
            err = tbl['ERROR'][j]

            # append xspectrum
            xsp = xspec.XSpectrum1D.from_tuple((wave, flx, err))
            if det == 'NUV':
                nuvsp.append(xsp)
            elif det == 'FUV':
                fuvsp.append(xsp)
            else:
                raise IOError("Detector not well defined in ", spf)

{'fraction_non_alphanumeric': 0.10959089677594148, 'fraction_numerical': 0.014223787591438634, 'mean_word_length': 2.1443781942078366, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1507456', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2404, 'n_words': 731}