def test_flex_shift(): # Dummy slf # Read spectra obj_spec = readspec(data_path('obj_lrisb_600_sky.fits')) arx_file = os.path.join(data.Paths.sky_spec, 'sky_LRISb_600.fits') arx_spec = readspec(arx_file) arx_lines = arc.detect_lines(arx_spec.flux.value) # Call flex_dict = flexure.spec_flex_shift(obj_spec, arx_spec, arx_lines, mxshft=60) # # Apply # from scipy import interpolate # print(flex_dict['shift']) # npix = len(obj_spec.wavelength) # x = np.linspace(0., 1., npix) # f = interpolate.interp1d(x, obj_spec.wavelength.value, bounds_error=False, # fill_value="extrapolate") # new_wave = f(x+flex_dict['shift']/(npix-1)) # # from matplotlib import pyplot # pyplot.plot(arx_spec.wavelength, arx_spec.flux) # pyplot.plot(obj_spec.wavelength, obj_spec.flux) # pyplot.plot(new_wave, obj_spec.flux) # pyplot.show() assert np.abs(flex_dict['shift'] - 43.7) < 0.1
def plot_spec_and_models(spec_filename, models_filenames='all'): """Plots several models in on top of a single spectrum. Parameters ---------- """ if models_filenames == 'all': models = glob.glob("*_inspect.fits") else: models = models_filenames spec = readspec(spec_filename) models_specs = [readspec(model) for model in models] plt.figure() if spec.co_is_set: spec.normalize(co=spec.co) plt.plot(spec.wavelength, spec.flux, 'k', drawstyle='steps-mid') plt.plot(spec.wavelength, spec.sig, 'g', drawstyle='steps-mid') for model_s in models_specs: plt.plot(model_s.wavelength, model_s.flux, label=model_s.filename.split('_inspect')[0]) plt.legend(ncol=5) plt.ylim(-0.2, 2.1)
def test_read_table(): t = Table([(1, 2, 3), (1, 2, 3), (1, 2, 3)], names=['WAVE', 'FLUX', 'ERR']) spec = io.readspec(t) np.testing.assert_allclose(spec.wavelength[0].value, 1) np.testing.assert_allclose(spec.flux[0], 1) # Read table with non standard tags t = Table([(1, 2, 3), (1, 2, 3), (1, 2, 3)], names=['dumb_wave', 'dumb_flux', 'dumb_sig']) spec = io.readspec(t, wave_tag='dumb_wave', flux_tag='dumb_flux', sig_tag='dumb_sig') np.testing.assert_allclose(spec.wavelength[0].value, 1) np.testing.assert_allclose(spec.flux[0], 1) # More.. t = Table([(1, 2, 3), (1, 2, 3), (1, 2, 3)], names=['dumb_wave', 'dumb_flux', 'dumb_ivar']) spec = io.readspec(t, wave_tag='dumb_wave', flux_tag='dumb_flux', ivar_tag='dumb_ivar') t = Table([(1, 2, 3), (1, 2, 3), (1, 2, 3)], names=['dumb_wave', 'dumb_flux', 'dumb_var']) spec = io.readspec(t, wave_tag='dumb_wave', flux_tag='dumb_flux', var_tag='dumb_var')
def test_write_fits(): spec = io.readspec(data_path('UM184_nF.fits')) # Write. Should be replaced with tempfile.TemporaryFile spec.write_to_fits(data_path('tmp.fits')) spec2 = io.readspec(data_path('tmp.fits')) # check a round trip works np.testing.assert_allclose(spec.dispersion, spec2.dispersion)
def abslines_from_VPfile(parfile,specfile=None,ra=None,dec=None): ''' Takes a joebvp parameter file and builds a list of linetools AbsLines from the measurements therein. Parameters ---------- parfile : str Name of the parameter file in the joebvp format ra : float, optional Right Ascension of the QSO in decimal degrees dec : float, optional Declination of the QSO in decimal degress Returns ------- abslinelist: list List of AbsLine objects ''' from linetools.spectralline import AbsLine from linetools.lists.linelist import LineList import astropy.units as u llist = LineList('ISM') if specfile!=None: spec=readspec(specfile) # Allow spectrum file to be declared in call linetab = ascii.read(parfile) # Read parameters from file linetab['restwave']=linetab['restwave']*u.AA abslinelist = [] # Initiate list to populate for i,row in enumerate(linetab): ### Check to see if errors for this line are defined colerr,berr,velerr=get_errors(linetab,i) ### Adjust velocity limits according to centroid errors and limits from file vcentmin = row['vel']-velerr vcentmax = row['vel']+velerr v1 = vcentmin + row['vlim1'] v2 = vcentmax + row['vlim2'] line=AbsLine(row['restwave']*u.AA, z=row['zsys'],closest=True, linelist=llist) vlims=[v1,v2]*u.km/u.s line.limits.set(vlims) ### Set other parameters line.attrib['logN'] = row['col'] line.attrib['sig_logN'] = colerr line.attrib['b'] = row['bval'] * u.km/u.s line.attrib['sig_b'] = berr * u.km/u.s line.attrib['vel'] = row['vel'] * u.km/u.s ### Attach the spectrum to this AbsLine but check first to see if this one is same as previous if specfile==None: if i==0: spec=readspec(row['specfile']) elif row['specfile']!=linetab['specfile'][i-1]: spec=readspec(row['specfile']) else: pass line.analy['spec']=spec ### Add it to the list and go on abslinelist.append(line) return abslinelist
def main(*args, **kwargs): """ Runs the XSpecGui on an input file """ import argparse parser = argparse.ArgumentParser(description='Parse') parser.add_argument("file", type=str, help="Spectral file") parser.add_argument("skyfile", type=str, help="Archived PYPIT sky file (e.g. paranal_sky.fits") parser.add_argument("--exten", type=int, help="FITS extension") parser.add_argument("--optimal", default=False, help="Show Optimal? Default is boxcar", action="store_true") parser.add_argument("--scale_user", default=1., type=float, help="Scale user spectrum by a factor") pargs = parser.parse_args() from matplotlib import pyplot as plt # Extension exten = (pargs.exten if hasattr(pargs, 'exten') else 0) #scale = (pargs.scale_user if hasattr(pargs, 'scale_user') else 1.) # Read spec keywords ikwargs = {} if pargs.optimal: ikwargs['wave_tag'] = 'opt_wave' ikwargs['flux_tag'] = 'opt_sky' else: ikwargs['wave_tag'] = 'box_wave' ikwargs['flux_tag'] = 'box_sky' # Load user file user_sky = readspec(pargs.file, exten=exten, **ikwargs) # Load sky spec arx_sky = readspec(sky_path + pargs.skyfile) # Plot plt.clf() plt.plot(user_sky.wavelength, user_sky.flux * pargs.scale_user, 'k-', label='user') plt.plot(arx_sky.wavelength, arx_sky.flux, 'b-', label='archive') legend = plt.legend(loc='upper left', scatterpoints=1, borderpad=0.3, handletextpad=0.3, fontsize='small', numpoints=1) plt.show()
def test_write_fits(spec, spec2): # Write. Should be replaced with tempfile.TemporaryFile spec.write_to_fits(data_path("tmp.fits")) specin = io.readspec(data_path("tmp.fits")) # check a round trip works np.testing.assert_allclose(spec.dispersion, specin.dispersion) # ESI spec2.write_to_fits(data_path("tmp2.fits")) specin2 = io.readspec(data_path("tmp2.fits")) # check a round trip works np.testing.assert_allclose(spec2.dispersion, specin2.dispersion)
def test_write_fits(spec, spec2): # Write. Should be replaced with tempfile.TemporaryFile spec.write_to_fits(data_path('tmp.fits')) specin = io.readspec(data_path('tmp.fits')) # check a round trip works np.testing.assert_allclose(spec.wavelength, specin.wavelength) # ESI spec2.write_to_fits(data_path('tmp2.fits')) specin2 = io.readspec(data_path('tmp2.fits')) # check a round trip works np.testing.assert_allclose(spec2.wavelength, specin2.wavelength)
def test_readwrite_metadata(): spec = io.readspec(data_path('UM184_nF.fits')) d = {'a':1, 'b':'abc', 'c':3.2, 'd':np.array([1,2,3]), 'e':dict(a=1,b=2)} spec.meta.update(d) spec.write_to_fits(data_path('tmp.fits')) spec2 = io.readspec(data_path('tmp.fits')) assert spec2.meta['a'] == d['a'] assert spec2.meta['b'] == d['b'] np.testing.assert_allclose(spec2.meta['c'], d['c']) np.testing.assert_allclose(spec2.meta['d'], d['d']) assert spec2.meta['e'] == d['e']
def test_readwrite_meta_as_dicts(spec): sp = XSpectrum1D.from_tuple((np.array([5,6,7]), np.ones(3), np.ones(3)*0.1)) sp.meta['headers'][0] = dict(a=1, b='abc') sp2 = XSpectrum1D.from_tuple((np.array([8,9,10]), np.ones(3), np.ones(3)*0.1)) sp2.meta['headers'][0] = dict(c=2, d='efg') spec = ltsu.collate([sp,sp2]) # Write spec.write_to_fits(data_path('tmp.fits')) spec.write_to_hdf5(data_path('tmp.hdf5')) # Read and test newspec = io.readspec(data_path('tmp.hdf5')) assert newspec.meta['headers'][0]['a'] == 1 assert newspec.meta['headers'][0]['b'] == 'abc' newspec2 = io.readspec(data_path('tmp.fits')) assert 'METADATA' in newspec2.meta['headers'][0].keys()
def test_hdf5(specm): import h5py # Write. Should be replaced with tempfile.TemporaryFile specm.write_to_hdf5(data_path('tmp.hdf5')) # specread = io.readspec(data_path('tmp.hdf5')) # check a round trip works np.testing.assert_allclose(specm.wavelength, specread.wavelength) # Add to existing file tmp2 = h5py.File(data_path('tmp2.hdf5'), 'w') foo = tmp2.create_group('boxcar') specm.add_to_hdf5(tmp2, path='/boxcar/') tmp2.close() # check a round trip works spec3 = io.readspec(data_path('tmp2.hdf5'), path='/boxcar/') np.testing.assert_allclose(specm.wavelength, spec3.wavelength)
def spec_plot(spec_fil, ID_path=None, ambig_fil=None, dwv=25., outfil='tmp.pdf'): mpl.rcParams['font.family'] = 'stixgeneral' from matplotlib.backends.backend_pdf import PdfPages import matplotlib.gridspec as gridspec ''' spec_fil: string Filename of spectrum outfil: string ('tmp.pdf') Filename for output dwv: float (25.) Ang per window ''' npx = 1 npy = 4 # Read spectrum spec = lsio.readspec(spec_file) # Start plot pp = PdfPages(outfil)
def test_flex_shift(): if not os.getenv('PYPIT'): pass else: # Dummy slf arut.dummy_settings() settings.argflag['reduce']['flexure']['maxshift'] = 50 slf = arut.dummy_self() # Read spectra obj_spec = lsio.readspec(data_path('obj_lrisb_600_sky.fits')) arx_file = pypit.__path__[0]+'/data/sky_spec/sky_LRISb_600.fits' arx_spec = lsio.readspec(arx_file) # Call #msgs._debug['flexure'] = True flex_dict = arwave.flex_shift(slf, 1, obj_spec, arx_spec) assert np.abs(flex_dict['shift'] - 43.7) < 0.1
def coadd_cos_from_x1dfiles(filenames, wv_array=None, A_pix=0.01 * u.AA): spec_list = [] #TODO: mask out x1d spectral regions with bad values. for filename in filenames: sp = readspec(filename) import pdb pdb.set_trace() # mask = spec_list += [sp] # spec_list contains all individual spectra specs = collate(spec_list) # now all in a single XSpectrum1D object #rebin if wv_array is None: # bring them to a unique native wavelength grid using PYPIT A_pix = A_pix.to("AA").value cat_wave = arco.new_wave_grid(specs.data['wave'], wave_method='pixel', A_pix=A_pix) else: cat_wave = wv_array.to('AA').value specs = specs.rebin(cat_wave * u.AA, all=True, do_sig=True, masking='none', grow_bad_sig=True) # estimate weights for coaddition (PYPYT) sn2, weights = arco.sn_weight(specs) # coaddition spec1d = arco.one_d_coadd(specs, weights) return spec1d
def model_profile(spec, fitpars, instr=None, gratings=None, lsfranges=None, cen_wave=None, lps=None, slits=None): '''Produce Voigt profile model from parameters and instrumental setup for convolving proper line spread function. Note Parameters ---------- spec : string or XSpectrum1D The spectrum to be fitted with the input lines fitpars : list of lists The joebvp parameter array that includes line measurements instr : list of str, optional Instruments used to observe the spectral regions defined in lsfranges gratings : list of str, optional Gratings used for each spectral region defined in lsfranges lsfranges : array of size len(instr)x2 Beginning and ending wavelengths of spectral ranges for each setup cen_wave : list of str, optional Central wavelength setting for each spectral region defined in lsfranges lps : list of str, optional Lifetime positions of detector (particularly for COS) slits : list of str, optional Slit setting for each spectral region defined in lsfranges Returns ------- wavelength : Quantity Wavelength array from spec profile : array Voigt profile model ''' ### Setup LSF parameters if provided if instr is not None: cfg.instr = instr cfg.lsfranges = lsfranges cfg.gratings = gratings cfg.cen_wave = cen_wave cfg.lps = lps cfg.slits = slits ### Deal with alternate input types if isinstance(spec, str): specobj = readspec(spec) else: specobj = spec makevoigt.get_lsfs() #import pdb; pdb.set_trace() cfg.wave = specobj.wavelength.value cfg.spectrum = specobj profile = makevoigt.cosvoigt(cfg.wave, fitpars) return specobj.wavelength, profile
def test_sep_files(): spec = io.readspec(data_path('UM184_nF.fits')) idl = ascii.read(data_path('UM184.dat.gz'), names=['wave', 'flux', 'sig']) np.testing.assert_allclose(spec.wavelength.value, idl['wave']) np.testing.assert_allclose(spec.sig, idl['sig'], atol=2e-3, rtol=0) assert spec.wavelength.unit == u.Unit('AA')
def test_attrib(): spec = io.readspec(data_path('UM184_nF.fits')) # np.testing.assert_allclose(spec.wvmin.value, 3056.6673905210096) np.testing.assert_allclose(spec.wvmax.value, 9205.255609841855) #assert spec.npix == 15024 -- With masking on assert spec.npix == 16582
def test_write_ascii(spec): # Write. Should be replaced with tempfile.TemporaryFile spec.write_to_ascii(data_path('tmp.ascii')) # specb = io.readspec(data_path('tmp.ascii')) # check a round trip works np.testing.assert_allclose(spec.wavelength, specb.wavelength)
def test_write_ascii(spec): # Write. Should be replaced with tempfile.TemporaryFile spec.write_to_ascii(data_path("tmp.ascii")) # spec2 = io.readspec(data_path("tmp.ascii")) # check a round trip works np.testing.assert_allclose(spec.dispersion, spec2.dispersion)
def test_aodm_absline(): # Init CIV 1548 abslin = AbsLine('CIV 1548', z=2.9304) # Set spectrum abslin.analy['spec'] = lsio.readspec( data_path('UM184_nF.fits')) # Fumagalli+13 MagE spectrum abslin.limits.set([6080.78, 6087.82] * u.AA) #abslin.analy['wvlim'] = [6080.78, 6087.82]*u.AA # abslin.measure_aodm() N, sig_N, flgN = [abslin.attrib[key] for key in ['N', 'sig_N', 'flag_N']] np.testing.assert_allclose(N.value, 76369981945649.38) assert N.unit == 1 / u.cm**2 assert flgN == 1 # Now velocity limits abslin.setz(2.92929) abslin.limits.set((-150., 150.) * u.km / u.s) # abslin.measure_aodm() N, sig_N, flgN = [abslin.attrib[key] for key in ['N', 'sig_N', 'flag_N']] np.testing.assert_allclose(N.value, 80410608889125.64) return
def compare_s2n(pp,lrdx_sciobj,pypit_boxfile, iso): '''Compare boxcar S/N ''' # Read/Load pypit_boxspec = lsio.readspec(pypit_boxfile) # Read LowRedux sig = np.sqrt(lrdx_sciobj['MASK_BOX']/(lrdx_sciobj['SIVAR_BOX'] + (lrdx_sciobj['MASK_BOX']==0))) lwrdx_boxspec = XSpectrum1D.from_tuple( (lrdx_sciobj['WAVE_BOX'], lrdx_sciobj['FLUX_BOX'], sig) ) # Plot plt.clf() fig = plt.figure(figsize=(16,7)) fig.suptitle("Instr={:s}, Setup={:s} :: Boxcar S/N for {:s} :: PYPIT ({:s})".format(iso[0], iso[1], iso[2], pypit.version), fontsize=18.) ax = plt.gca() ymax = np.median(pypit_boxspec.flux)*2. # PYPIT gdpy = pypit_boxspec.sig > 0. pys2n = pypit_boxspec.flux[gdpy]/pypit_boxspec.sig[gdpy] ax.plot(pypit_boxspec.dispersion[gdpy],pys2n, 'k-', drawstyle='steps', label='PYPIT') # LowRedux gdlx = lwrdx_boxspec.sig > 0. ax.plot(lwrdx_boxspec.dispersion[gdlx], lwrdx_boxspec.flux[gdlx]/lwrdx_boxspec.sig[gdlx], '-', color='blue', label='LowRedux') # Axes ax.set_xlim(np.min(pypit_boxspec.dispersion.value), np.max(pypit_boxspec.dispersion.value)) ax.set_ylim(0.,np.median(pys2n)*2.) ax.set_xlabel('Wavelength',fontsize=17.) ax.set_ylabel('S/N per pixel',fontsize=17.) # Legend legend = plt.legend(loc='upper right', borderpad=0.3, handletextpad=0.3, fontsize='x-large') # Finish plt.tight_layout(pad=0.2,h_pad=0.,w_pad=0.1,rect=[0, 0.03, 1, 0.95]) pp.savefig(bbox_inches='tight') plt.close()
def init_conti_full(self): print("Initializing the continuum") spec = self.spec_widg.orig_spec spec.select = self.model_spec # Just in case, but this should already be the case # Full Model (LLS+continuum) self.full_model = XSpectrum1D.from_tuple( (spec.wavelength, np.ones(len(spec.wavelength)))) self.conti_dict = pycc.init_conti_dict( Norm=float(np.median(spec.flux.value)), piv_wv=1215. * (1 + self.zqso), #piv_wv2=915.*(1+zqso), igm='True') # Read Telfer and apply IGM if self.template is not None: tspec = lsi.readspec(self.template) # assume wavelengths tspec = XSpectrum1D.from_tuple( (tspec.wavelength.value * (1 + self.zqso), tspec.flux.value)) else: tspec = pycq.get_telfer_spec( zqso=self.zqso, igm=(self.conti_dict['igm'] == 'True')) # Rebin self.continuum = tspec.rebin(spec.wavelength) # Reset pivot wave self.conti_dict['piv_wv'] = 915. * (1 + self.zqso) #self.conti_dict['piv_wv'] = 1215.*(1+zqso) #self.conti_dict['piv_wv2'] = 915.*(1+zqso) self.base_continuum = self.continuum.flux self.update_conti() self.spec_widg.continuum = self.continuum
def test_hi_lya(): # Simple system (without an absline) dla1 = DLASystem.from_json( data_path('J010311.38+131616.7_z2.309_ESI.json')) dla1.zabs = 4. dla1.NHI = 21. dla2 = DLASystem.from_json( data_path('J010311.38+131616.7_z2.309_ESI.json')) dla2.zabs = 3.5 dla2.NHI = 20.5 spec_fil = linetools.__path__[0] + '/spectra/tests/files/PH957_f.fits' spec = lsio.readspec(spec_fil) # Just lya model, lya_lines = pyasu.hi_model([dla1, dla2], spec, lya_only=True) ipx = np.argmin(np.abs(spec.wavelength.value - (1 + dla1.zabs) * 1215.67)) assert model.flux[ipx].value < 1e-4 ipx2 = np.argmin( np.abs(spec.wavelength.value - (1 + dla1.zabs) * 1025.7222)) assert model.flux[ipx2].value > 0.99 # Lyman series model2, lyman_lines = pyasu.hi_model([dla1, dla2], spec) assert len(lyman_lines) > 10 assert model2.flux[ipx2].value < 1e-4 # LLS model3, lyman_lines = pyasu.hi_model([dla1, dla2], spec, add_lls=True) assert model3.flux[0].value < 1e-4
def coadd_cos_from_x1dfiles(filenames, wv_array=None, A_pix=0.01*u.AA): spec_list = [] #TODO: mask out x1d spectral regions with bad values. for filename in filenames: sp = readspec(filename) import pdb; pdb.set_trace() # mask = spec_list += [sp] # spec_list contains all individual spectra specs = collate(spec_list) # now all in a single XSpectrum1D object #rebin if wv_array is None: # bring them to a unique native wavelength grid using PYPIT A_pix = A_pix.to("AA").value cat_wave = arco.new_wave_grid(specs.data['wave'], wave_method='pixel', A_pix=A_pix) else: cat_wave = wv_array.to('AA').value specs = specs.rebin(cat_wave*u.AA, all=True, do_sig=True, masking='none',grow_bad_sig=True) # estimate weights for coaddition (PYPYT) sn2, weights = arco.sn_weight(specs) # coaddition spec1d = arco.one_d_coadd(specs, weights) return spec1d
def test_coadd_stis_from_x1dfiles(plot=False): spec_old = readspec("./data/m5zng1_stis/m5zng1_stis.fits") # co-add independently of old filenames = glob.glob("./data/m5zng1_stis/o6n*_x1d.fits") spec_new = coadd.coadd_stis_from_x1dfiles(filenames, wv_array=None) spec_new_rebinned = spec_new.rebin(spec_old.wavelength, do_sig=True, grow_bad_sig=True) if plot: plt.plot(spec_old.wavelength, spec_old.flux, drawstyle='steps-mid', color='k') plt.plot(spec_old.wavelength, spec_old.sig, drawstyle='steps-mid', color='g') plt.plot(spec_new.wavelength, spec_new.flux, drawstyle='steps-mid', color='b') plt.plot(spec_new.wavelength, spec_new.sig, drawstyle='steps-mid', color='y') spec_new.write_to_fits("new.fits")
def mk_comp(ctype,vlim=[-300.,300]*u.km/u.s,add_spec=False, use_rand=True, add_trans=False, zcomp=2.92939, b=20*u.km/u.s): # Read a spectrum Spec if add_spec: xspec = lsio.readspec(lt_path+'/spectra/tests/files/UM184_nF.fits') else: xspec = None # AbsLines if ctype == 'HI': all_trans = ['HI 1215', 'HI 1025'] elif ctype == 'SiII': all_trans = ['SiII 1260', 'SiII 1304', 'SiII 1526', 'SiII 1808'] if add_trans: all_trans += ['SiII 1193'] abslines = [] for trans in all_trans: iline = AbsLine(trans, z=zcomp) if use_rand: rnd = np.random.rand() else: rnd = 0. iline.attrib['logN'] = 13.3 + rnd iline.attrib['sig_logN'] = 0.15 iline.attrib['flag_N'] = 1 iline.attrib['b'] = b iline.analy['spec'] = xspec iline.limits.set(vlim) _,_ = ltaa.linear_clm(iline.attrib) # Loads N, sig_N abslines.append(iline) # Component abscomp = AbsComponent.from_abslines(abslines) return abscomp, abslines
def test_slice(): spec = io.readspec(data_path('UM184_nF.fits')) spec2 = io.readspec(data_path('PH957_f.fits')) spec3 = spec2.copy() # Collate to make a multispec spectrum mspec = lsu.collate([spec, spec2, spec3]) # Array newspec = mspec[np.array([0, 1])] # Test assert newspec.nspec == 2 assert not newspec.co_is_set # Int newspec2 = mspec[1] assert newspec2.nspec == 1 # Slice newspec3 = mspec[0:2] assert newspec3.nspec == 2
def test_gauss_smooth(): spec = io.readspec(data_path('UM184_nF.fits')) # Smooth smth_spec = spec.gauss_smooth(4.) # Test np.testing.assert_allclose(smth_spec.flux[3000].value, 0.8288110494613) assert smth_spec.flux.unit == spec.flux.unit
def test_relvel(): spec = io.readspec(data_path('UM184_nF.fits')) # Velocity velo = spec.relative_vel(5000.*u.AA) # Test np.testing.assert_allclose(velo[6600].value, -3716.441360213781) assert velo.unit == (u.km/u.s)
def specr(): # Replace bad pixels (for rebinning) data = io.readspec(data_path('UM184_nF.fits'), masking='edges') sig = data.sig.value bad_pix = sig <= 0. sig[bad_pix] = 1. data.sig = sig return data
def test_readwrite_meta_as_dicts(spec): sp = XSpectrum1D.from_tuple((np.array([5, 6, 7]), np.ones(3), np.ones(3) * 0.1)) sp.meta['headers'][0] = dict(a=1, b='abc') sp2 = XSpectrum1D.from_tuple( (np.array([8, 9, 10]), np.ones(3), np.ones(3) * 0.1)) sp2.meta['headers'][0] = dict(c=2, d='efg') spec = ltsu.collate([sp, sp2]) # Write spec.write_to_fits(data_path('tmp.fits')) spec.write_to_hdf5(data_path('tmp.hdf5')) # Read and test newspec = io.readspec(data_path('tmp.hdf5')) assert newspec.meta['headers'][0]['a'] == 1 assert newspec.meta['headers'][0]['b'] == 'abc' newspec2 = io.readspec(data_path('tmp.fits')) assert 'METADATA' in newspec2.meta['headers'][0].keys()
def test_slice(): spec = io.readspec(data_path('UM184_nF.fits')) spec2 = io.readspec(data_path('PH957_f.fits')) spec3 = spec2.copy() # Collate to make a multispec spectrum mspec = lsu.collate([spec,spec2,spec3]) # Array newspec = mspec[np.array([0,1])] # Test assert newspec.nspec == 2 assert not newspec.co_is_set # Int newspec2 = mspec[1] assert newspec2.nspec == 1 # Slice newspec3 = mspec[0:2] assert newspec3.nspec == 2
def __init__(self,specfilename,parfilename=None,wave1=None,wave2=None,numchunks=8,parent=None): QtWidgets.QMainWindow.__init__(self, parent) #super(Main,self).__init__() self.setupUi(self) ### Initialize stuff self.line_dict = {} self.fitpars = None self.parinfo = None self.linecmts = None self.wave1 = wave1 self.wave2 = wave2 self.numchunks = numchunks self.spls = [] self.labeltog = 1 self.pixtog = 0 self.restog = 1 self.fitconvtog = 0 self.lastclick=1334. ### Read in spectrum and list of lines to fit self.specfilename=specfilename self.spectrum = readspec(specfilename) self.wave=self.spectrum.wavelength.value self.normflux=self.spectrum.flux/self.spectrum.co self.normsig=self.spectrum.sig/self.spectrum.co cfg.spectrum = self.spectrum cfg.wave=self.wave cfg.normflux=self.normflux cfg.filename=self.specfilename if not parfilename==None: self.initialpars(parfilename) ### Connect signals to slots self.fitButton.clicked.connect(self.fitlines) self.fitConvBox.clicked.connect(self.togfitconv) self.boxLineLabel.clicked.connect(self.toglabels) self.boxFitpix.clicked.connect(self.togfitpix) self.boxResiduals.clicked.connect(self.togresiduals) self.loadParsButton.clicked.connect(self.openParFileDialog) self.addLineButton.clicked.connect(self.addLineDialog) self.writeParsButton.clicked.connect(self.writeParFileDialog) self.writeModelButton.clicked.connect(self.writeModelFileDialog) self.writeModelCompButton.clicked.connect(self.writeModelCompFileDialog) self.quitButton.clicked.connect(self.quitGui) ### Initialize spectral plots fig=Figure(figsize=(5,3)) self.fig=fig self.initplot(fig) ### Initialize side plot sidefig=Figure(figsize=(5.85,3.75)) self.sidefig = sidefig self.addsidempl(self.sidefig) self.sideplot(self.lastclick) #Dummy initial cenwave setting
def __init__(self,specfilename,parfilename=None,wave1=None,wave2=None,numchunks=8,parent=None): QtWidgets.QMainWindow.__init__(self, parent) #super(Main,self).__init__() self.setupUi(self) ### Initialize stuff self.line_dict = {} self.fitpars = None self.parinfo = None self.linecmts = None self.wave1 = wave1 self.wave2 = wave2 self.numchunks = numchunks self.spls = [] self.labeltog = 1 self.pixtog = 0 self.restog = 1 self.fitconvtog = 0 self.lastclick=1334. ### Read in spectrum and list of lines to fit self.specfilename=specfilename self.spectrum = readspec(specfilename) self.wave=self.spectrum.wavelength.value self.normflux=self.spectrum.flux/self.spectrum.co self.normsig=self.spectrum.sig/self.spectrum.co cfg.spectrum = self.spectrum cfg.wave=self.wave cfg.normflux=self.normflux cfg.filename=self.specfilename if not parfilename==None: self.initialpars(parfilename) ### Connect signals to slots self.fitButton.clicked.connect(self.fitlines) self.fitConvBox.clicked.connect(self.togfitconv) self.boxLineLabel.clicked.connect(self.toglabels) self.boxFitpix.clicked.connect(self.togfitpix) self.boxResiduals.clicked.connect(self.togresiduals) self.loadParsButton.clicked.connect(self.openParFileDialog) self.addLineButton.clicked.connect(self.addLineDialog) self.writeParsButton.clicked.connect(self.writeParFileDialog) self.writeModelButton.clicked.connect(self.writeModelFileDialog) self.writeModelCompButton.clicked.connect(self.writeModelCompFileDialog) self.quitButton.clicked.connect(self.quitGui) ### Initialize spectral plots fig=Figure() self.fig=fig self.initplot(fig) ### Initialize side plot sidefig=Figure(figsize=(5.25,2)) self.sidefig = sidefig self.addsidempl(self.sidefig) self.sideplot(self.lastclick) #Dummy initial cenwave setting
def read_spec(row): # Filename coord = SkyCoord(ra=row['RA_GROUP'], dec=row['DEC_GROUP'], unit='deg') filename = 'J{:s}{:s}.txt'.format(coord.ra.to_string(unit=u.hour,sep='',pad=True)[0:4], coord.dec.to_string(sep='',pad=True,alwayssign=True)[0:5]) # Read spec = lsio.readspec(path+filename) # Return return filename, spec
def test_addnoise(): spec = io.readspec(data_path('UM184_nF.fits')) # spec.add_noise(seed=12) np.testing.assert_allclose(spec.flux[1000], 0.44806158542633057) # With S/N input spec.add_noise(seed=19,s2n=10.) np.testing.assert_allclose(spec.flux[1000], 0.24104823059199412)
def test_rebin(): spec = io.readspec(data_path('UM184_nF.fits')) # Rebin new_wv = np.arange(3000., 9000., 5) * u.AA newspec = spec.rebin(new_wv) # Test np.testing.assert_allclose(newspec.flux[1000], 0.9999280967617779) assert newspec.flux.unit == u.dimensionless_unscaled
def json_eqw(json_file, fits_file, outfile, overwrite=False): ion_name = [] restwave = [] col = [] col_err = [] flag = [] zcomp = [] if not os.path.isfile(json_file): return np.array(ion_name), np.array(restwave), np.array(col), \ np.array(col_err), np.array(flag), np.array(zcomp) if not os.path.isfile(outfile) or overwrite == True: out = open(outfile, 'w') out.write('#ion_name, restwave, logN, logN_err, flag_N, zcomp\n') with open(json_file) as data_file: igmg_dict = json.load(data_file) comp_list = [] for ii, key in enumerate(igmg_dict['cmps'].keys()): comp = AbsComponent.from_dict(igmg_dict['cmps'][key], chk_sep=False, chk_data=False, chk_vel=False) comp_list += [comp] spec = readspec(fits_file) for i, cc in enumerate(comp_list): for j, al in enumerate(cc._abslines): al.analy['spec'] = spec al.measure_ew() al.measure_aodm() ion_name.append(al.ion_name) restwave.append(al.wrest.value) col.append(al.attrib['logN']) sig_logN = al.attrib['sig_logN'] if np.isnan(sig_logN): sig_logN = 0.0 col_err.append(sig_logN) flag.append(al.attrib['flag_N']) zcomp.append(al.z) out.write('%s\t%f\t%e\t%e\t%d\t%0.6f\n'%(al.ion_name, al.wrest.value, \ al.attrib['logN'], sig_logN, al.attrib['flag_N'], al.z)) out.close() if os.path.isfile(outfile) and len(open(outfile).readlines()) > 1: ion_name = np.loadtxt(outfile, unpack=True, skiprows=1, usecols=0, dtype='str') restwave, col, col_err, flag, zcomp = \ np.loadtxt(outfile, unpack = True, skiprows = 1, usecols = (1, 2, 3, 4, 5)) return np.array(ion_name), np.array(restwave), np.array(col), np.array( col_err), np.array(flag), np.array(zcomp)
def compare_skyspec(pp,sciobj,pypit_skyboxfile, iso): '''Compare traces Parameters: ---------- pp: Pdf obj sciobj: Table LowRedux sciobj table iso: tuple instr, setup, obj strings ''' # Load pypit_skyspec = lsio.readspec(pypit_skyboxfile) # lwrdx_skywv = sciobj['WAVE_BOX'] lwrdx_skyfx = sciobj['SKY_BOX'] lwrdx_skywv = arwv.vactoair(lwrdx_skywv*u.AA) # plt.clf() plt.figure(figsize=(16,8)) gs = gridspec.GridSpec(2, 1) # Full ax = plt.subplot(gs[0]) # PYPIT ax.plot(pypit_skyspec.dispersion, pypit_skyspec.flux, 'k-', label='PYPIT', drawstyle='steps') # LowRedux ax.plot(lwrdx_skywv, lwrdx_skyfx/(2.*sciobj['BOX_RAD']), '-', color='blue', label='LowRedux') # Axes ax.set_xlim(np.min(pypit_skyspec.dispersion.value),np.max(pypit_skyspec.dispersion.value)) ax.set_ylim(0.,np.max(pypit_skyspec.flux)) ax.set_xlabel('Wavelength',fontsize=17.) ax.set_ylabel('Sky (Counts/pix)',fontsize=17.) # Legend legend = plt.legend(loc='upper left', borderpad=0.3, handletextpad=0.3, fontsize='x-large') # ZOOM axz = plt.subplot(gs[1]) # PYPIT axz.plot(pypit_skyspec.dispersion, pypit_skyspec.flux, 'k-', label='PYPIT', drawstyle='steps-mid') # LowRedux axz.plot(lwrdx_skywv, lwrdx_skyfx/(2.*sciobj['BOX_RAD']), '-', color='blue', label='LowRedux') # Axes zlim = np.array([7200., 7700.])*u.AA axz.set_xlim(zlim.value) ymx = np.max( pypit_skyspec.flux[np.where((pypit_skyspec.dispersion>zlim[0])&(pypit_skyspec.dispersion<zlim[1]))]) axz.set_ylim(0.,ymx) axz.set_xlabel('Wavelength',fontsize=17.) axz.set_ylabel('Sky (Counts/pix)',fontsize=17.) # Legend legend = plt.legend(loc='upper right', borderpad=0.3, handletextpad=0.3, fontsize='x-large') ax.set_title("Instr={:s}, Setup={:s} :: Sky Spectra for {:s} :: PYPIT ({:s})".format(iso[0], iso[1], iso[2], pypit.version), fontsize=18.) # Finish plt.tight_layout(pad=0.2,h_pad=0.,w_pad=0.1) pp.savefig(bbox_inches='tight') plt.close()
def main(*args, **kwargs): """ Runs the continuum fitter """ import argparse parser = argparse.ArgumentParser(description='GUI to fit a continuum to a spectrum') parser.add_argument("file", type=str, help="Input spectral file (FITS, ASCII, etc.)") parser.add_argument("outfil", type=str, help="Output, normalized spectrum filename; FITS [can be the same]") parser.add_argument("--redshift", type=float, help="Redshift of the Source") parser.add_argument("--wchunk", type=float, help="Width of a 'chunk' (Ang)") parser.add_argument("--native", default=True, action='store_true', help="Do not mask input spectrum") parser.add_argument("--specdb", type=str, help="Input file is specdb. Input (ra,dec,group) in this order without spaces") #parser.add_argument("-exten", type=int, help="FITS extension") pargs = parser.parse_args() from linetools.guis import specdbutils from linetools.spectra import io as lsio # Read spectrum if pargs.native: masking = 'none' else: masking = 'edges' # Load from simple file if pargs.specdb is None: xspec = lsio.readspec(pargs.file, masking=masking) else: # Load specdb spdb = specdbutils.load_specb(pargs.file) if spdb is None: print("You have not yet installed specdb!!") print("Exiting..") return # Parse ra,dec,group = pargs.specdb.split(',') # Load xspec = specdbutils.load_xspec(spdb, float(ra), float(dec), group=group, masking=masking) kwrds = {} if pargs.wchunk is not None: kwrds['dw'] = pargs.wchunk # Redshift if pargs.redshift is not None: kwrds['kind'] = 'QSO' kwrds['redshift'] = pargs.redshift # Run print("WARNING: QUIT with q keystroke, not by clicking to kill.") xspec.fit_continuum(**kwrds) # Output xspec.write_to_fits(pargs.outfil)
def test_readwrite_metadata(spec): d = {"a": 1, "b": "abc", "c": 3.2, "d": np.array([1, 2, 3]), "e": dict(a=1, b=2)} spec.meta.update(d) spec.write_to_fits(data_path("tmp.fits")) spec2 = io.readspec(data_path("tmp.fits")) assert spec2.meta["a"] == d["a"] assert spec2.meta["b"] == d["b"] np.testing.assert_allclose(spec2.meta["c"], d["c"]) np.testing.assert_allclose(spec2.meta["d"], d["d"]) assert spec2.meta["e"] == d["e"]
def test_box_smooth(): spec = io.readspec(data_path('UM184_nF.fits')) # Smooth newspec3 = spec.box_smooth(3) np.testing.assert_allclose(newspec3.flux[4000], 0.9967582821846008) assert newspec3.flux.unit == u.dimensionless_unscaled newspec5 = spec.box_smooth(5) np.testing.assert_allclose(newspec5.flux[3000], 1.086308240890503)
def from_file(self, ifile): ''' From file Parameters ---------- ifile : str Filename ''' slf = lsio.readspec(ifile) return slf
def from_file(self, ifile, **kwargs): """ From file Parameters ---------- ifile : str Filename """ slf = lsio.readspec(ifile, **kwargs) return slf
def main(args): import os from pkg_resources import resource_filename from matplotlib import pyplot as plt from linetools.spectra.io import readspec # Path to archived sky spectra sky_path = os.path.join(resource_filename('pypeit', 'data'), 'sky_spec') # Extension exten = args.exten if hasattr(args, 'exten') else 0 # Read spec keywords ikwargs = {} if args.optimal: ikwargs['wave_tag'] = 'opt_wave' ikwargs['flux_tag'] = 'opt_sky' else: ikwargs['wave_tag'] = 'box_wave' ikwargs['flux_tag'] = 'box_sky' # Load user file user_sky = readspec(args.file, exten=exten, **ikwargs) # Load sky spec arx_sky = readspec(sky_path + args.skyfile) # Plot plt.clf() plt.plot(user_sky.wavelength, user_sky.flux * args.scale_user, 'k-', label='user') plt.plot(arx_sky.wavelength, arx_sky.flux, 'b-', label='archive') legend = plt.legend(loc='upper left', scatterpoints=1, borderpad=0.3, handletextpad=0.3, fontsize='small', numpoints=1) plt.show()
def read_spec(row): # Filename coord = SkyCoord(ra=row['RA_GROUP'], dec=row['DEC_GROUP'], unit='deg') filename = 'J{:s}{:s}.txt'.format( coord.ra.to_string(unit=u.hour, sep='', pad=True)[0:4], coord.dec.to_string(sep='', pad=True, alwayssign=True)[0:5]) # Read spec = lsio.readspec(path + filename) # Return return filename, spec
def test_model_abs(): # Simple system (without an absline) dla = DLASystem.from_json(data_path('J010311.38+131616.7_z2.309_ESI.json')) spec_fil = linetools.__path__[0]+'/spectra/tests/files/PH957_f.fits' spec = lsio.readspec(spec_fil) model, lya_lines = dla.model_abs(spec) # import pdb; pdb.set_trace() # Check core ipx = np.argmin(np.abs(spec.wavelength.value-(1+dla.zabs)*1215.67)) assert model.flux[ipx].value < 1e-4
def plotspec(args): """Plot spectrum files Parameters ---------- filenames : list of str Spectrum filenames """ from linetools.spectra.io import readspec import warnings import matplotlib.pyplot as plt import matplotlib as mpl warnings.simplefilter('ignore', mpl.mplDeprecation) spec_cache = {} fig = plt.figure(figsize=(10,5)) fig.subplots_adjust(left=0.07, right=0.95, bottom=0.11) ax = fig.add_subplot(111) i = 0 quit = False print("#### Use left and right arrow keys to navigate, 'Q' to quit ####") while quit is False: filename = args.filenames[i] if filename not in spec_cache: spec_cache[filename] = readspec(filename) sp = spec_cache[filename] ax.cla() sp.plot(show=False) ax.set_xlabel(str(sp.wavelength.unit)) ax.set_title(filename) if args.redshift is not None: from linetools.lists.linelist import LineList ll = LineList('Strong') #import pdb ;pdb.set_trace() wlines = ll._data['wrest'] * (1 + args.redshift) y0, y1 = ax.get_ylim() ax.vlines(wlines.to(sp.wavelength.unit).value, y0, y1, linestyle='dotted') while True: plt.waitforbuttonpress() if sp._plotter.last_keypress == 'right': i += 1 i = min(i, len(args.filenames) - 1) # Note this only breaks out of the inner while loop break elif sp._plotter.last_keypress == 'left': i -= 1 i = max(i, 0) break elif sp._plotter.last_keypress == 'Q': quit = True break
def test_sep_files(): """ Separate flux/error files""" spec = io.readspec(data_path('UM184_nF.fits')) idl = ascii.read(data_path('UM184.dat.gz'), names=['wave', 'flux', 'sig']) #np.testing.assert_allclose(spec.wavelength.value, idl['wave']) np.testing.assert_allclose(spec.data['wave'][spec.select], idl['wave']) np.testing.assert_allclose(spec.data['sig'][spec.select], idl['sig'], atol=2e-3, rtol=0) assert spec.wavelength.unit == u.Unit('AA')
def test_dla_XY(): spec_fil = linetools.__path__[0]+'/spectra/tests/files/PH957_f.fits' spec = lsio.readspec(spec_fil) dla = DLASystem.from_json(data_path('J010311.38+131616.7_z2.309_ESI.json')) # dla.measure_aodm(spec=spec) dla.update_component_colm() dla.fill_ionN() dla.XY = RelAbund.from_ionclm_table((1,21.37,0.08), dla._ionN) tbl = dla.XY.table() assert len(tbl) == 8
def main(*args, **kwargs): """ Runs the XSpecGui on an input file """ import argparse parser = argparse.ArgumentParser(description='Parse') parser.add_argument("file", type=str, help="Spectral file") parser.add_argument("skyfile", type=str, help="Archived PYPIT sky file (e.g. paranal_sky.fits") parser.add_argument("--exten", type=int, help="FITS extension") parser.add_argument("--optimal", default=False, help="Show Optimal? Default is boxcar", action="store_true") parser.add_argument("--scale_user", default=1., type=float, help="Scale user spectrum by a factor") pargs = parser.parse_args() from matplotlib import pyplot as plt # Extension exten = (pargs.exten if hasattr(pargs, 'exten') else 0) #scale = (pargs.scale_user if hasattr(pargs, 'scale_user') else 1.) # Read spec keywords ikwargs = {} if pargs.optimal: ikwargs['wave_tag'] = 'opt_wave' ikwargs['flux_tag'] = 'opt_sky' else: ikwargs['wave_tag'] = 'box_wave' ikwargs['flux_tag'] = 'box_sky' # Load user file user_sky = readspec(pargs.file, exten=exten, **ikwargs) # Load sky spec arx_sky = readspec(sky_path+pargs.skyfile) # Plot plt.clf() plt.plot(user_sky.wavelength, user_sky.flux*pargs.scale_user, 'k-', label='user') plt.plot(arx_sky.wavelength, arx_sky.flux, 'b-', label='archive') legend = plt.legend(loc='upper left', scatterpoints=1, borderpad=0.3, handletextpad=0.3, fontsize='small', numpoints=1) plt.show()
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 test_measurekin_absline(): # Test Simple kinematics abslin = AbsLine('NiII 1741',z=2.307922) # Set spectrum abslin.analy['spec'] = lsio.readspec(data_path('PH957_f.fits')) abslin.limits.set([-70., 70.]*u.km/u.s) # Measure Kin abslin.measure_kin() np.testing.assert_allclose(abslin.attrib['kin']['Dv'].value, 75.) np.testing.assert_allclose(abslin.attrib['kin']['fedg'], 0.20005782376000183)
def main(*args, **kwargs): """ Runs the continuum fitter """ import argparse parser = argparse.ArgumentParser( description='GUI to fit a continuum to a spectrum') parser.add_argument("file", type=str, help="Input spectral file (FITS, ASCII, etc.)") parser.add_argument( "outfil", type=str, help="Output, normalized spectrum filename; FITS [can be the same]") parser.add_argument("--redshift", type=float, help="Redshift of the Source") parser.add_argument("--wchunk", type=float, help="Width of a 'chunk' (Ang)") parser.add_argument("--native", default=True, action='store_true', help="Do not mask input spectrum") #parser.add_argument("-exten", type=int, help="FITS extension") pargs = parser.parse_args() from linetools.guis.xspecgui import XSpecGui from linetools.spectra import io as lsio # Read spectrum if pargs.native: masking = 'none' else: masking = 'edges' xspec = lsio.readspec(pargs.file, masking=masking) kwrds = {} if pargs.wchunk is not None: kwrds['dw'] = pargs.wchunk # Redshift if pargs.redshift is not None: kwrds['kind'] = 'QSO' kwrds['redshift'] = pargs.redshift # Run print("WARNING: QUIT with q keystroke, not by clicking to kill.") xspec.fit_continuum(**kwrds) # Output xspec.write_to_fits(pargs.outfil)