def __init__(self, throughput_name, interpval=None): self.throughput_name = throughput_name # Extract bandpass unless component is a CLEAR filter. if throughput_name != conf.clear_filter: if interpval is None: self.throughput = SpectralElement.from_file(throughput_name) else: self.throughput = interpolate_spectral_element( throughput_name, interpval) else: self.throughput = None
def __init__(self, throughput_name, interpval=None): self.throughput_name = throughput_name # Extract bandpass unless component is a CLEAR filter. if throughput_name != conf.clear_filter: if interpval is None: self.throughput = SpectralElement.from_file(throughput_name) else: self.throughput = interpolate_spectral_element( throughput_name, interpval) else: self.throughput = None
def test_remote_rn_icat_k93(): sp1 = spparser.parse_spec( 'rn(icat(k93models, 5000, 0.5, 0), ' 'cracscomp$acs_f814w_hrc_006_syn.fits, 17, obmag)') _single_functioncall( sp1, SourceSpectrum, None, 'rn(k93models(T_eff=5000,metallicity=0.5,log_g=0),' 'cracscomp$acs_f814w_hrc_006_syn.fits,17.0,obmag)') k93 = catalog.grid_to_spec('k93models', 5000, 0.5, 0) bp = SpectralElement.from_file(resolve_filename( os.environ['PYSYN_CDBS'], 'comp', 'acs', 'acs_f814w_hrc_006_syn.fits')) sp2 = k93.normalize(17 * units.OBMAG, band=bp, area=conf.area) _compare_spectra(sp1, sp2)
def test_write_fits(self): outfile = os.path.join(self.outdir, 'outspec1.fits') self.obs.to_fits(outfile, trim_zero=False, pad_zero_ends=False) # Read it back in with fits.open(outfile) as pf: assert (pf[1].header['grftable'] == os.path.basename(self.obs.obsmode.gtname)) assert (pf[1].header['cmptable'] == os.path.basename(self.obs.obsmode.ctname)) obs = SpectralElement.from_file(outfile) w = self.obs.waveset np.testing.assert_allclose(obs.waveset, w) np.testing.assert_allclose(obs(w), self.obs(w))
def test_write_fits(self): outfile = os.path.join(self.outdir, 'outspec1.fits') self.obs.to_fits(outfile, trim_zero=False, pad_zero_ends=False) # Read it back in with fits.open(outfile) as pf: assert (pf[1].header['grftable'] == os.path.basename( self.obs.obsmode.gtname)) assert (pf[1].header['cmptable'] == os.path.basename( self.obs.obsmode.ctname)) obs = SpectralElement.from_file(outfile) w = self.obs.waveset np.testing.assert_allclose(obs.waveset, w) np.testing.assert_allclose(obs(w), self.obs(w))
def _convertstr(value): """Convert given filename to source spectrum or passband. This is used by the interpreter to do the conversion from string to spectrum object. """ if not isinstance(value, str): return value value = irafconvert(value) try: sp = SourceSpectrum.from_file(value) except KeyError: sp = SpectralElement.from_file(value) return sp
def p_functioncall(self, tree): # Where all the real interpreter action is. # Note that things that should only be done at the top level # are performed in :func:`interpret` defined below. """ V ::= function_call ( V LPAREN V RPAREN ) """ if not isinstance(tree[2].value, list): args = [tree[2].value] else: args = tree[2].value fname = tree[0].value metadata = {'expr': '{0}{1}'.format(fname, tuple(args))} if fname not in _SYFUNCTIONS: log.error('Unknown function: {0}'.format(fname)) self.error(fname) else: # Constant spectrum if fname == 'unit': if args[1] not in _SYFORMS: log.error('Unrecognized unit: {0}'.format(args[1])) self.error(fname) try: fluxunit = units.validate_unit(args[1]) tree.value = SourceSpectrum(ConstFlux1D, amplitude=args[0] * fluxunit, meta=metadata) except NotImplementedError as e: log.error(str(e)) self.error(fname) # Black body elif fname == 'bb': tree.value = SourceSpectrum(BlackBodyNorm1D, temperature=args[0]) # Power law elif fname == 'pl': if args[2] not in _SYFORMS: log.error('Unrecognized unit: {0}'.format(args[2])) self.error(fname) try: fluxunit = units.validate_unit(args[2]) tree.value = SourceSpectrum(PowerLawFlux1D, amplitude=1 * fluxunit, x_0=args[0], alpha=-args[1], meta=metadata) except (synexceptions.SynphotError, NotImplementedError) as e: log.error(str(e)) self.error(fname) # Box throughput elif fname == 'box': tree.value = SpectralElement(Box1D, amplitude=1, x_0=args[0], width=args[1], meta=metadata) # Source spectrum from file elif fname == 'spec': tree.value = SourceSpectrum.from_file(irafconvert(args[0])) tree.value.meta.update(metadata) # Passband elif fname == 'band': tree.value = spectrum.band(tree[2].svalue) tree.value.meta.update(metadata) # Gaussian emission line elif fname == 'em': if args[3] not in _SYFORMS: log.error('Unrecognized unit: {0}'.format(args[3])) self.error(fname) x0 = args[0] fluxunit = units.validate_unit(args[3]) totflux = units.convert_flux(x0, args[2] * fluxunit, units.PHOTLAM).value tree.value = SourceSpectrum(GaussianFlux1D, total_flux=totflux, mean=x0, fwhm=args[1]) # Catalog interpolation elif fname == 'icat': tree.value = grid_to_spec(*args) # Renormalize source spectrum elif fname == 'rn': sp = args[0] bp = args[1] fluxunit = units.validate_unit(args[3]) rnval = args[2] * fluxunit if not isinstance(sp, SourceSpectrum): sp = SourceSpectrum.from_file(irafconvert(sp)) if not isinstance(bp, SpectralElement): bp = SpectralElement.from_file(irafconvert(bp)) # Always force the renormalization to occur: prevent exceptions # in case of partial overlap. Less robust but duplicates # IRAF SYNPHOT. Force the renormalization in the case of # partial overlap, but raise an exception if the spectrum and # bandpass are entirely disjoint. try: tree.value = sp.normalize(rnval, band=bp, area=conf.area, vegaspec=spectrum.Vega) except synexceptions.PartialOverlap: tree.value = sp.normalize(rnval, band=bp, area=conf.area, vegaspec=spectrum.Vega, force=True) tree.value.warnings = { 'force_renorm': ('Renormalization exceeds the limit ' 'of the specified passband.') } tree.value.meta.update(metadata) # Redshift source spectrum (flat spectrum if fails) elif fname == 'z': sp = args[0] # ETC generates junk (i.e., 'null') sometimes if isinstance(sp, str) and sp != 'null': sp = SourceSpectrum.from_file(irafconvert(sp)) if isinstance(sp, SourceSpectrum): tree.value = sp tree.value.z = args[1] else: tree.value = SourceSpectrum(ConstFlux1D, amplitude=1) tree.value.meta.update(metadata) # Extinction elif fname == 'ebmvx': try: tree.value = spectrum.ebmvx(args[1], args[0]) except synexceptions.SynphotError as e: log.error(str(e)) self.error(fname) tree.value.meta.update(metadata) # Default else: tree.value = ('would call {0} with the following args: ' '{1}'.format(fname, repr(args)))
def _eso_etc(cls, filter_name, wavelength_unit): return SpectralElement.from_file(os.path.join( cls._EsoEtcFiltersFolder(), 'phot_%s.dat' % filter_name), wave_unit=wavelength_unit)
def _c_red_one(cls): return SpectralElement.from_file(os.path.join(cls._DetectorsFolder(), 'c_red_one.dat'), wave_unit=u.nm)
def _ccd_220(cls): return SpectralElement.from_file(os.path.join(cls._DetectorsFolder(), 'ccd220.dat'), wave_unit=u.um)
def readFilter(filterName): bp = SpectralElement.from_file(filterName, wave_unit=u.nm) return bp