def combinespectfiles(spfile_a, spfile_b, file_ab): """ Coadd two spectra, and write output in a file Parameters ---------- spfile_a : str spfile_b : str .fits files with spectra file_ab : str output .fits file with combined spectra Returns ------- """ from linetools.spectra import io as tio from linetools.spectra import utils as spltu file_a = tio.readspec(spfile_a) file_b = tio.readspec(spfile_b) spliced_sp = spltu.splice_two(file_b, file_a, chk_units=False) spliced_sp.write(file_ab)
def load_gal_spec(self, inp): """ Load the galaxy spectrum Parameters ---------- inp : int or tuple int -- Index of the cgm_abs list tuple -- (field,gal_id) Returns ---------- spec : XSpectrum1D Splices the blue and red side for LRIS JXP on 12 Oct 2015 """ from linetools.spectra import utils as ltsu # Init cgm_abs = self[inp] # Directories galdir = self.cdir+'/Galaxies/' #fielddir = 'fields/'+cgm_abs.field+'/' #sysdir = cgm_abs.gal_id+'/spec1d/' sysname = cgm_abs.galaxy.field+'_'+cgm_abs.galaxy.gal_id # Find files lris_files = glob.glob(galdir+sysname+'*corr.fits.gz') if len(lris_files) == 0: raise ValueError('No LRIS files! {:s}'.format(galdir+sysname)) elif len(lris_files) == 2: lris_files.sort() specb = lsio.readspec(lris_files[0]) specr = lsio.readspec(lris_files[1]) spec = ltsu.splice_two(specb, specr) else: raise ValueError('Not sure what happened') # Return return spec
def load_gal_spec(self, inp): """ Load the galaxy spectrum Parameters ---------- inp : int or tuple int -- Index of the cgm_abs list tuple -- (field,gal_id) Returns ---------- spec : XSpectrum1D Splices the blue and red side for LRIS """ from linetools.spectra import utils as ltsu # Init cgm_abs = self[inp] # Directories galdir = self.cdir + '/Galaxies/' #fielddir = 'fields/'+cgm_abs.field+'/' #sysdir = cgm_abs.gal_id+'/spec1d/' sysname = cgm_abs.galaxy.field + '_' + cgm_abs.galaxy.gal_id # Find files lris_files = glob.glob(galdir + sysname + '*corr.fits.gz') if len(lris_files) == 0: raise ValueError('No LRIS files! {:s}'.format(galdir + sysname)) elif len(lris_files) == 2: lris_files.sort() specb = lsio.readspec(lris_files[0]) specr = lsio.readspec(lris_files[1]) spec = ltsu.splice_two(specb, specr) else: raise ValueError('Not sure what happened') # Return return spec
def test_splice_two(spec, spec2): spec3 = ltsu.splice_two(spec, spec2) assert spec3.npix == 18390
def read_spec(ispec, exten=None, norm=True, **kwargs): """Parse spectrum out of the input If 2 spectra are given, the 2nd is scaled to the first Parameters ---------- ispec : XSpectrum1D, str, list of files (ordered blue to red), or tuple of arrays exten : int, optional FITS extension Returns ------- spec : XSpectrum1D spec_file : str """ from linetools.spectra import xspectrum1d as lsx from linetools.spectra import utils as ltsu from astropy.utils.misc import isiterable # if isinstance(ispec,basestring): spec_fil = ispec if 'rsp_kwargs' in kwargs.keys(): spec = lsx.XSpectrum1D.from_file(spec_fil, exten=exten, **kwargs['rsp_kwargs']) else: spec = lsx.XSpectrum1D.from_file(spec_fil, exten=exten) elif isinstance(ispec, lsx.XSpectrum1D): spec = ispec spec_fil = spec.filename # Grab from Spectrum1D elif isinstance(ispec,tuple): spec = lsx.XSpectrum1D.from_tuple(ispec) spec_fil = 'none' elif isinstance(ispec,list): # Multiple file names # Loop on the files for kk,ispecf in enumerate(ispec): if isiterable(exten): iexten = exten[kk] else: iexten = exten jspec = lsx.XSpectrum1D.from_file(ispecf, exten=iexten) if kk == 0: spec = jspec _, xper1 = ltsp.get_flux_plotrange(spec.flux, perc=0.9) else: # Scale flux for convenience of plotting (sig is not scaled) _, xper2 = ltsp.get_flux_plotrange(jspec.flux, perc=0.9) scl = xper1/xper2 # Splice #from PyQt4 import QtCore #QtCore.pyqtRemoveInputHook() #pdb.set_trace() #QtCore.pyqtRestoreInputHook() spec = ltsu.splice_two(spec, jspec)#, scale=scl) # Filename spec_fil = ispec[0] spec.filename=spec_fil else: raise ValueError('Bad input to read_spec: {}'.format(type(ispec))) # Normalize? if norm: if spec.co_is_set: spec.normed=True # Demand AA for wavelength unit (unless over-ridden) if spec.wavelength.unit != u.AA: wvAA = spec.wavelength.to('AA') spec.wavelength = wvAA #from PyQt4 import QtCore #QtCore.pyqtRemoveInputHook() #pdb.set_trace() #QtCore.pyqtRestoreInputHook() # Return return spec, spec_fil