def read_gbt_target(sdfitsfile, objectname, verbose=False): """ Give an object name, get all observations of that object as an 'obsblock' """ bintable = _get_bintable(sdfitsfile) whobject = bintable.data['OBJECT'] == objectname if verbose: print("Number of individual scans for Object %s: %i" % (objectname, whobject.sum())) calON = bintable.data['CAL'] == 'T' # HACK: apparently bintable.data can sometimes treat itself as scalar... if np.isscalar(calON): calON = np.array([(val in ['T', True]) for val in bintable.data['CAL']]) n_nods = np.unique(bintable.data['PROCSIZE']) blocks = {} for sampler in np.unique(bintable.data[whobject]['SAMPLER']): whsampler = bintable.data['SAMPLER'] == sampler nods = np.unique(bintable.data['PROCSEQN'][whsampler * whobject]) for nod in nods: whnod = bintable.data['PROCSEQN'] == nod for onoff in ('ON', 'OFF'): calOK = (calON - (onoff == 'OFF')) whOK = (whobject * whsampler * calOK * whnod) if whOK.sum() == 0: continue if verbose: print( "Number of spectra for sampler %s, nod %i, cal%s: %i" % (sampler, nod, onoff, whOK.sum())) crvals = bintable.data[whOK]['CRVAL1'] if len(crvals) > 1: maxdiff = np.diff(crvals).max() else: maxdiff = 0 freqres = np.max(bintable.data[whOK]['FREQRES']) if maxdiff < freqres: splist = [ read_gbt_scan(bintable, ii) for ii in np.where(whOK)[0] ] blocks[sampler + onoff + str(nod)] = pyspeckit.ObsBlock( splist, force=True) blocks[sampler + onoff + str(nod)]._arithmetic_threshold = np.diff( blocks[sampler + onoff + str(nod)].xarr).min() / 5. else: print( "Maximum frequency difference > frequency resolution: %f > %f" % (maxdiff, freqres)) return blocks
def read_alfalfa_source(savfile, sourcenumber=0): """ Create an Observation Block class for a single source in an ALFALFA 'source' IDL save file """ if type(savfile) is str and ".src" in savfile: savfile = idlsave.read(savfile) src = savfile.src[sourcenumber] header = pyfits.Header() splist = [] for spectra in src.spectra: for par in spectra.dtype.names: try: len(spectra[par]) except TypeError: header[par[:8]] = spectra[par] # assume ALFALFA spectra in Kelvin header['BUNIT'] = 'K' xarr = pyspeckit.spectrum.units.SpectroscopicAxis( spectra.velarr, refX=header['RESTFRQ'], refX_unit='MHz', unit='km/s') data = np.ma.masked_where(np.isnan(spectra.spec), spectra.spec) sp = pyspeckit.Spectrum(xarr=xarr, data=data, header=header) # the Source has a baseline presubtracted (I think) sp.baseline.baselinepars = spectra.baseline[::-1] sp.baseline.subtracted = True sp.baseline.order = len(spectra.baseline) sp.baseline.basespec = np.poly1d(sp.baseline.baselinepars)(np.arange( xarr.shape[0])) # There are multiple components in each Spectrum, but I think they are not indepedent sp.specfit.fittype = 'gaussian' sp.specfit.fitter = sp.specfit.Registry.multifitters['gaussian'] modelpars = zip(spectra['STON'], spectra['VCEN'], spectra['WIDTH']) modelerrs = zip(spectra['STON'], spectra['VCENERR_STAT'], spectra['WIDTHERR']) sp.specfit.modelpars = modelpars[0] # only use the first fit sp.specfit.fitter.mpp = modelpars[0] sp.specfit.modelerrs = modelerrs[0] sp.specfit.fitter.mpperr = modelerrs[0] sp.specfit._full_model() splist.append(sp) return pyspeckit.ObsBlock(splist)
def class_to_obsblocks(filename, telescope, line, datatuple=None, source=None, imagfreq=False, DEBUG=False, **kwargs): """ Load an entire CLASS observing session into a list of ObsBlocks based on matches to the 'telescope', 'line' and 'source' names Parameters ---------- filename : string The Gildas CLASS data file to read the spectra from. telescope : list List of telescope names to be matched. line : list List of line names to be matched. source : list (optional) List of source names to be matched. Defaults to None. imagfreq : bool Create a SpectroscopicAxis with the image frequency. """ if datatuple is None: spectra, header, indexes = read_class(filename, DEBUG=DEBUG, **kwargs) else: spectra, header, indexes = datatuple obslist = [] lastscannum = -1 spectrumlist = None for sp, hdr, ind in zip(spectra, header, indexes): hdr.update(ind) # this is slow but necessary... H = pyfits.Header() for k, v in hdr.iteritems(): if hasattr(v, "__len__") and not isinstance(v, str): if len(v) > 1: for ii, vv in enumerate(v): H.update(k[:7] + str(ii), vv) else: H.update(k, v[0]) elif pyfits.Card._comment_FSC_RE.match(str(v)) is not None: H.update(k, v) scannum = hdr['XSCAN'] if 'XTEL' in hdr and hdr['XTEL'].strip() not in telescope: continue if hdr['LINE'].strip() not in line: continue if (source is not None) and (hdr['SOURC'].strip() not in source): continue hdr.update({'RESTFREQ': hdr.get('RESTF')}) H.update('RESTFREQ', hdr.get('RESTF')) #print "Did not skip %s,%s. Scannum, last: %i,%i" % (hdr['XTEL'],hdr['LINE'],scannum,lastscannum) if scannum != lastscannum: lastscannum = scannum if spectrumlist is not None: obslist.append(pyspeckit.ObsBlock(spectrumlist)) xarr = make_axis(hdr, imagfreq=imagfreq) spectrumlist = [(pyspeckit.Spectrum(xarr=xarr, header=H, data=sp))] else: spectrumlist.append( pyspeckit.Spectrum(xarr=xarr, header=H, data=sp)) return obslist