Example #1
0
def calfs(scantab,
          scannos=[],
          smooth=1,
          tsysval=0.0,
          tauval=0.0,
          tcalval=0.0,
          verify=False):
    """
    Calibrate GBT frequency switched data.
    Adopted from GBTIDL getfs.
    Currently calfs identify the scans as frequency switched data if source
    type enum is fson and fsoff. The data must contains 'CAL' signal
    on/off in each integration. To identify 'CAL' on state, the source type 
    enum of foncal and foffcal need to be present.

    Parameters:
        scantab:       scantable
        scannos:       list of scan numbers
        smooth:        optional box smoothing order for the reference
                       (default is 1 = no smoothing)
        tsysval:       optional user specified Tsys (default is 0.0,
                       use Tsys in the data)
        tauval:        optional user specified Tau
        verify:        Verify calibration if true
    """
    varlist = vars()
    from asap._asap import stmath
    from asap._asap import srctype
    stm = stmath()
    stm._setinsitu(False)

    #    check = scantab.get_scan('*_fs*')
    #    if check is None:
    #        msg = "The input data appear to contain no Nod observing mode data."
    #        raise TypeError(msg)
    s = scantab.get_scan(scannos)
    del scantab

    resspec = scantable(stm._dofs(s, scannos, smooth, tsysval, tauval,
                                  tcalval))
    ###
    if verify:
        # get data
        ssub = s.get_scan(scannos)
        #ssubon = ssub.get_scan('*calon')
        #ssuboff = ssub.get_scan('*[^calon]')
        sel = selector()
        sel.set_types([srctype.foncal, srctype.foffcal])
        ssub.set_selection(sel)
        ssubon = ssub.copy()
        ssub.set_selection()
        sel.reset()
        sel.set_types([srctype.fson, srctype.fsoff])
        ssub.set_selection(sel)
        ssuboff = ssub.copy()
        ssub.set_selection()
        sel.reset()
        import numpy
        precal = {}
        postcal = []
        keys = ['fs', 'fs_calon', 'fsr', 'fsr_calon']
        types = [srctype.fson, srctype.foncal, srctype.fsoff, srctype.foffcal]
        ifnos = list(ssub.getifnos())
        polnos = list(ssub.getpolnos())
        for i in range(2):
            #ss=ssuboff.get_scan('*'+keys[2*i])
            ll = []
            for j in range(len(ifnos)):
                for k in range(len(polnos)):
                    sel.set_ifs(ifnos[j])
                    sel.set_polarizations(polnos[k])
                    sel.set_types(types[2 * i])
                    try:
                        #ss.set_selection(sel)
                        ssuboff.set_selection(sel)
                    except:
                        continue
                    ll.append(numpy.array(ss._getspectrum(0)))
                    sel.reset()
                    #ss.set_selection()
                    ssuboff.set_selection()
            precal[keys[2 * i]] = ll
            #del ss
            #ss=ssubon.get_scan('*'+keys[2*i+1])
            ll = []
            for j in range(len(ifnos)):
                for k in range(len(polnos)):
                    sel.set_ifs(ifnos[j])
                    sel.set_polarizations(polnos[k])
                    sel.set_types(types[2 * i + 1])
                    try:
                        #ss.set_selection(sel)
                        ssubon.set_selection(sel)
                    except:
                        continue
                    ll.append(numpy.array(ss._getspectrum(0)))
                    sel.reset()
                    #ss.set_selection()
                    ssubon.set_selection()
            precal[keys[2 * i + 1]] = ll
            #del ss
        #sig=resspec.get_scan('*_fs')
        #ref=resspec.get_scan('*_fsr')
        sel.set_types(srctype.fson)
        resspec.set_selection(sel)
        sig = resspec.copy()
        resspec.set_selection()
        sel.reset()
        sel.set_type(srctype.fsoff)
        resspec.set_selection(sel)
        ref = resspec.copy()
        resspec.set_selection()
        sel.reset()
        for k in range(len(polnos)):
            for j in range(len(ifnos)):
                sel.set_ifs(ifnos[j])
                sel.set_polarizations(polnos[k])
                try:
                    sig.set_selection(sel)
                    postcal.append(numpy.array(sig._getspectrum(0)))
                except:
                    ref.set_selection(sel)
                    postcal.append(numpy.array(ref._getspectrum(0)))
                sel.reset()
                resspec.set_selection()
        del sel
        # plot
        asaplog.post()
        asaplog.push(
            'Plot only first spectrum for each [if,pol] pairs to verify calibration.'
        )
        asaplog.post('WARN')
        p = new_asaplot()
        rcp('lines', linewidth=1)
        #nr=min(6,len(ifnos)*len(polnos))
        nr = len(ifnos) / 2 * len(polnos)
        titles = []
        btics = []
        if nr > 3:
            asaplog.post()
            asaplog.push('Only first 3 [if,pol] pairs are plotted.')
            asaplog.post('WARN')
            nr = 3
        p.set_panels(rows=nr, cols=3, nplots=3 * nr, ganged=False)
        for i in range(3 * nr):
            b = False
            if i >= 3 * nr - 3:
                b = True
            btics.append(b)
        for i in range(nr):
            p.subplot(3 * i)
            p.color = 0
            title = 'raw data IF%s,%s POL%s' % (
                ifnos[2 * int(i / len(polnos))],
                ifnos[2 * int(i / len(polnos)) + 1], polnos[i % len(polnos)])
            titles.append(title)
            #p.set_axes('title',title,fontsize=40)
            ymin = 1.0e100
            ymax = -1.0e100
            nchan = s.nchan(ifnos[2 * int(i / len(polnos))])
            edge = int(nchan * 0.01)
            for j in range(4):
                spmin = min(precal[keys[j]][i][edge:nchan - edge])
                spmax = max(precal[keys[j]][i][edge:nchan - edge])
                ymin = min(ymin, spmin)
                ymax = max(ymax, spmax)
            for j in range(4):
                if i == 0:
                    p.set_line(label=keys[j])
                else:
                    p.legend()
                p.plot(precal[keys[j]][i])
            p.axes.set_ylim(ymin - 0.1 * abs(ymin), ymax + 0.1 * abs(ymax))
            if not btics[3 * i]:
                p.axes.set_xticks([])
            p.subplot(3 * i + 1)
            p.color = 0
            title = 'sig data IF%s POL%s' % (ifnos[2 * int(i / len(polnos))],
                                             polnos[i % len(polnos)])
            titles.append(title)
            #p.set_axes('title',title)
            p.legend()
            ymin = postcal[2 * i][edge:nchan - edge].min()
            ymax = postcal[2 * i][edge:nchan - edge].max()
            p.plot(postcal[2 * i])
            p.axes.set_ylim(ymin - 0.1 * abs(ymin), ymax + 0.1 * abs(ymax))
            if not btics[3 * i + 1]:
                p.axes.set_xticks([])
            p.subplot(3 * i + 2)
            p.color = 0
            title = 'ref data IF%s POL%s' % (ifnos[2 * int(i / len(polnos)) +
                                                   1], polnos[i % len(polnos)])
            titles.append(title)
            #p.set_axes('title',title)
            p.legend()
            ymin = postcal[2 * i + 1][edge:nchan - edge].min()
            ymax = postcal[2 * i + 1][edge:nchan - edge].max()
            p.plot(postcal[2 * i + 1])
            p.axes.set_ylim(ymin - 0.1 * abs(ymin), ymax + 0.1 * abs(ymax))
            if not btics[3 * i + 2]:
                p.axes.set_xticks([])
        for i in range(3 * nr):
            p.subplot(i)
            p.set_axes('title', titles[i], fontsize='medium')
        x = raw_input('Accept calibration ([y]/n): ')
        if x.upper() == 'N':
            p.quit()
            del p
            return scantab
        p.quit()
        del p
    ###
    resspec._add_history("calfs", varlist)
    return resspec
Example #2
0
         sel.set_polarizations(polnos[k])
         try:
             ress.set_selection(sel)
         except:
             continue
         postcal.append(numpy.array(ress._getspectrum(0)))
         sel.reset()
         ress.set_selection()
 del sel
 # plot
 asaplog.post()
 asaplog.push(
     'Plot only first spectrum for each [if,pol] pairs to verify calibration.'
 )
 asaplog.post('WARN')
 p = new_asaplot()
 rcp('lines', linewidth=1)
 #nr=min(6,len(ifnos)*len(polnos))
 nr = len(ifnos) * len(polnos)
 titles = []
 btics = []
 if nr < 4:
     p.set_panels(rows=nr, cols=2, nplots=2 * nr, ganged=False)
     for i in range(2 * nr):
         b = False
         if i >= 2 * nr - 2:
             b = True
         btics.append(b)
 elif nr == 4:
     p.set_panels(rows=2, cols=4, nplots=8, ganged=False)
     for i in range(2 * nr):
Example #3
0
    def plot(self, residual=False, components=None, plotparms=False, filename=None):
        """
        Plot the last fit.
        Parameters:
            residual:    an optional parameter indicating if the residual
                         should be plotted (default 'False')
            components:  a list of components to plot, e.g [0,1],
                         -1 plots the total fit. Default is to only
                         plot the total fit.
            plotparms:   Inidicates if the parameter values should be present
                         on the plot
        """
        from matplotlib import rc as rcp

        if not self.fitted:
            return
        # if not self._p or self._p.is_dead:
        if not (self._p and self._p._alive()):
            from asap.asapplotter import new_asaplot

            del self._p
            self._p = new_asaplot(rcParams["plotter.gui"])
        self._p.hold()
        self._p.clear()
        rcp("lines", linewidth=1)
        self._p.set_panels()
        self._p.palette(0)
        tlab = "Spectrum"
        xlab = "Abcissa"
        ylab = "Ordinate"
        from numpy import ma, logical_not, logical_and, array

        m = self.mask
        if self.data:
            tlab = self.data._getsourcename(self._fittedrow)
            xlab = self.data._getabcissalabel(self._fittedrow)
            if self.data._getflagrow(self._fittedrow):
                m = [False]
            else:
                m = logical_and(self.mask, array(self.data._getmask(self._fittedrow), copy=False))

            ylab = self.data._get_ordinate_label()

        colours = ["#777777", "#dddddd", "red", "orange", "purple", "green", "magenta", "cyan"]
        nomask = True
        for i in range(len(m)):
            nomask = nomask and m[i]
        if len(m) == 1:
            m = m[0]
            invm = not m
        else:
            invm = logical_not(m)
        label0 = "Masked Region"
        label1 = "Spectrum"
        if nomask:
            label0 = label1
        else:
            y = ma.masked_array(self.y, mask=m)
            self._p.palette(1, colours)
            self._p.set_line(label=label1)
            self._p.plot(self.x, y)
        self._p.palette(0, colours)
        self._p.set_line(label=label0)
        y = ma.masked_array(self.y, mask=invm)
        self._p.plot(self.x, y)
        if residual:
            self._p.palette(7)
            self._p.set_line(label="Residual")
            y = ma.masked_array(self.get_residual(), mask=invm)
            self._p.plot(self.x, y)
        self._p.palette(2)
        if components is not None:
            cs = components
            if isinstance(components, int):
                cs = [components]
            if plotparms:
                self._p.text(0.15, 0.15, str(self.get_parameters()["formatted"]), size=8)
            n = len(self.components)
            self._p.palette(3)
            for c in cs:
                if 0 <= c < n:
                    lab = self.fitfuncs[c] + str(c)
                    self._p.set_line(label=lab)
                    y = ma.masked_array(self.fitter.evaluate(c), mask=invm)

                    self._p.plot(self.x, y)
                elif c == -1:
                    self._p.palette(2)
                    self._p.set_line(label="Total Fit")
                    y = ma.masked_array(self.fitter.getfit(), mask=invm)
                    self._p.plot(self.x, y)
        else:
            self._p.palette(2)
            self._p.set_line(label="Fit")
            y = ma.masked_array(self.fitter.getfit(), mask=invm)
            self._p.plot(self.x, y)
        xlim = [min(self.x), max(self.x)]
        self._p.axes.set_xlim(xlim)
        self._p.set_axes("xlabel", xlab)
        self._p.set_axes("ylabel", ylab)
        self._p.set_axes("title", tlab)
        self._p.release()
        if not rcParams["plotter.gui"]:
            self._p.save(filename)
Example #4
0
def calfs(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False):
    """
    Calibrate GBT frequency switched data.
    Adopted from GBTIDL getfs.
    Currently calfs identify the scans as frequency switched data if source
    type enum is fson and fsoff. The data must contains 'CAL' signal
    on/off in each integration. To identify 'CAL' on state, the source type 
    enum of foncal and foffcal need to be present.

    Parameters:
        scantab:       scantable
        scannos:       list of scan numbers
        smooth:        optional box smoothing order for the reference
                       (default is 1 = no smoothing)
        tsysval:       optional user specified Tsys (default is 0.0,
                       use Tsys in the data)
        tauval:        optional user specified Tau
        verify:        Verify calibration if true
    """
    varlist = vars()
    from asap._asap import stmath
    from asap._asap import srctype
    stm = stmath()
    stm._setinsitu(False)

#    check = scantab.get_scan('*_fs*')
#    if check is None:
#        msg = "The input data appear to contain no Nod observing mode data."
#        raise TypeError(msg)
    s = scantab.get_scan(scannos)
    del scantab

    resspec = scantable(stm._dofs(s, scannos, smooth, tsysval,tauval,tcalval))
    ###
    if verify:
        # get data
        ssub = s.get_scan(scannos)
        #ssubon = ssub.get_scan('*calon')
        #ssuboff = ssub.get_scan('*[^calon]')
        sel = selector()
        sel.set_types( [srctype.foncal,srctype.foffcal] )
        ssub.set_selection( sel )
        ssubon = ssub.copy()
        ssub.set_selection()
        sel.reset()
        sel.set_types( [srctype.fson,srctype.fsoff] )
        ssub.set_selection( sel )
        ssuboff = ssub.copy()
        ssub.set_selection()
        sel.reset()
        import numpy
        precal={}
        postcal=[]
        keys=['fs','fs_calon','fsr','fsr_calon']
        types=[srctype.fson,srctype.foncal,srctype.fsoff,srctype.foffcal]
        ifnos=list(ssub.getifnos())
        polnos=list(ssub.getpolnos())
        for i in range(2):
            #ss=ssuboff.get_scan('*'+keys[2*i])
            ll=[]
            for j in range(len(ifnos)):
                for k in range(len(polnos)):
                    sel.set_ifs(ifnos[j])
                    sel.set_polarizations(polnos[k])
                    sel.set_types(types[2*i])
                    try:
                        #ss.set_selection(sel)
                        ssuboff.set_selection(sel)
                    except:
                        continue
                    ll.append(numpy.array(ss._getspectrum(0)))
                    sel.reset()
                    #ss.set_selection()
                    ssuboff.set_selection()
            precal[keys[2*i]]=ll
            #del ss
            #ss=ssubon.get_scan('*'+keys[2*i+1])
            ll=[]
            for j in range(len(ifnos)):
                for k in range(len(polnos)):
                    sel.set_ifs(ifnos[j])
                    sel.set_polarizations(polnos[k])
                    sel.set_types(types[2*i+1])
                    try:
                        #ss.set_selection(sel)
                        ssubon.set_selection(sel)
                    except:
                        continue
                    ll.append(numpy.array(ss._getspectrum(0)))
                    sel.reset()
                    #ss.set_selection()
                    ssubon.set_selection()
            precal[keys[2*i+1]]=ll
            #del ss
        #sig=resspec.get_scan('*_fs')
        #ref=resspec.get_scan('*_fsr')
        sel.set_types( srctype.fson )
        resspec.set_selection( sel )
        sig=resspec.copy()
        resspec.set_selection()
        sel.reset()
        sel.set_type( srctype.fsoff )
        resspec.set_selection( sel )
        ref=resspec.copy()
        resspec.set_selection()
        sel.reset()
        for k in range(len(polnos)):
            for j in range(len(ifnos)):
                sel.set_ifs(ifnos[j])
                sel.set_polarizations(polnos[k])
                try:
                    sig.set_selection(sel)
                    postcal.append(numpy.array(sig._getspectrum(0)))
                except:
                    ref.set_selection(sel)
                    postcal.append(numpy.array(ref._getspectrum(0)))
                sel.reset()
                resspec.set_selection()
        del sel
        # plot
        asaplog.post()
        asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
        asaplog.post('WARN')
        p=new_asaplot()
        rcp('lines', linewidth=1)
        #nr=min(6,len(ifnos)*len(polnos))
        nr=len(ifnos)/2*len(polnos)
        titles=[]
        btics=[]
        if nr>3:
            asaplog.post()
            asaplog.push('Only first 3 [if,pol] pairs are plotted.')
            asaplog.post('WARN')
            nr=3
        p.set_panels(rows=nr,cols=3,nplots=3*nr,ganged=False)
        for i in range(3*nr):
            b=False
            if i >= 3*nr-3:
                b=True
            btics.append(b)
        for i in range(nr):
            p.subplot(3*i)
            p.color=0
            title='raw data IF%s,%s POL%s' % (ifnos[2*int(i/len(polnos))],ifnos[2*int(i/len(polnos))+1],polnos[i%len(polnos)])
            titles.append(title)
            #p.set_axes('title',title,fontsize=40)
            ymin=1.0e100
            ymax=-1.0e100
            nchan=s.nchan(ifnos[2*int(i/len(polnos))])
            edge=int(nchan*0.01)
            for j in range(4):
                spmin=min(precal[keys[j]][i][edge:nchan-edge])
                spmax=max(precal[keys[j]][i][edge:nchan-edge])
                ymin=min(ymin,spmin)
                ymax=max(ymax,spmax)
            for j in range(4):
                if i==0:
                    p.set_line(label=keys[j])
                else:
                    p.legend()
                p.plot(precal[keys[j]][i])
            p.axes.set_ylim(ymin-0.1*abs(ymin),ymax+0.1*abs(ymax))
            if not btics[3*i]:
                p.axes.set_xticks([])
            p.subplot(3*i+1)
            p.color=0
            title='sig data IF%s POL%s' % (ifnos[2*int(i/len(polnos))],polnos[i%len(polnos)])
            titles.append(title)
            #p.set_axes('title',title)
            p.legend()
            ymin=postcal[2*i][edge:nchan-edge].min()
            ymax=postcal[2*i][edge:nchan-edge].max()
            p.plot(postcal[2*i])
            p.axes.set_ylim(ymin-0.1*abs(ymin),ymax+0.1*abs(ymax))
            if not btics[3*i+1]:
                p.axes.set_xticks([])
            p.subplot(3*i+2)
            p.color=0
            title='ref data IF%s POL%s' % (ifnos[2*int(i/len(polnos))+1],polnos[i%len(polnos)])
            titles.append(title)
            #p.set_axes('title',title)
            p.legend()
            ymin=postcal[2*i+1][edge:nchan-edge].min()
            ymax=postcal[2*i+1][edge:nchan-edge].max()
            p.plot(postcal[2*i+1])
            p.axes.set_ylim(ymin-0.1*abs(ymin),ymax+0.1*abs(ymax))
            if not btics[3*i+2]:
                p.axes.set_xticks([])
        for i in range(3*nr):
            p.subplot(i)
            p.set_axes('title',titles[i],fontsize='medium')
        x=raw_input('Accept calibration ([y]/n): ' )
        if x.upper() == 'N':
            p.quit()
            del p
            return scantab
        p.quit()
        del p
    ###
    resspec._add_history("calfs",varlist)
    return resspec
Example #5
0
     for k in range(len(polnos)):
         sel.set_ifs(ifnos[j])
         sel.set_polarizations(polnos[k])
         try:
             ress.set_selection(sel)
         except:
             continue
         postcal.append(numpy.array(ress._getspectrum(0)))
         sel.reset()
         ress.set_selection()
 del sel
 # plot
 asaplog.post()
 asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
 asaplog.post('WARN')
 p=new_asaplot()
 rcp('lines', linewidth=1)
 #nr=min(6,len(ifnos)*len(polnos))
 nr=len(ifnos)*len(polnos)
 titles=[]
 btics=[]
 if nr<4:
     p.set_panels(rows=nr,cols=2,nplots=2*nr,ganged=False)
     for i in range(2*nr):
         b=False
         if i >= 2*nr-2:
             b=True
         btics.append(b)
 elif nr==4:
     p.set_panels(rows=2,cols=4,nplots=8,ganged=False)
     for i in range(2*nr):
Example #6
0
    def plot(self,
             residual=False,
             components=None,
             plotparms=False,
             filename=None):
        """
        Plot the last fit.
        Parameters:
            residual:    an optional parameter indicating if the residual
                         should be plotted (default 'False')
            components:  a list of components to plot, e.g [0,1],
                         -1 plots the total fit. Default is to only
                         plot the total fit.
            plotparms:   Inidicates if the parameter values should be present
                         on the plot
        """
        from matplotlib import rc as rcp
        if not self.fitted:
            return
        #if not self._p or self._p.is_dead:
        if not (self._p and self._p._alive()):
            from asap.asapplotter import new_asaplot
            del self._p
            self._p = new_asaplot(rcParams['plotter.gui'])
        self._p.hold()
        self._p.clear()
        rcp('lines', linewidth=1)
        self._p.set_panels()
        self._p.palette(0)
        tlab = 'Spectrum'
        xlab = 'Abcissa'
        ylab = 'Ordinate'
        from numpy import ma, logical_not, logical_and, array
        m = self.mask
        if self.data:
            tlab = self.data._getsourcename(self._fittedrow)
            xlab = self.data._getabcissalabel(self._fittedrow)
            if self.data._getflagrow(self._fittedrow):
                m = [False]
            else:
                m = logical_and(
                    self.mask,
                    array(self.data._getmask(self._fittedrow), copy=False))

            ylab = self.data._get_ordinate_label()

        colours = [
            "#777777", "#dddddd", "red", "orange", "purple", "green",
            "magenta", "cyan"
        ]
        nomask = True
        for i in range(len(m)):
            nomask = nomask and m[i]
        if len(m) == 1:
            m = m[0]
            invm = (not m)
        else:
            invm = logical_not(m)
        label0 = 'Masked Region'
        label1 = 'Spectrum'
        if (nomask):
            label0 = label1
        else:
            y = ma.masked_array(self.y, mask=m)
            self._p.palette(1, colours)
            self._p.set_line(label=label1)
            self._p.plot(self.x, y)
        self._p.palette(0, colours)
        self._p.set_line(label=label0)
        y = ma.masked_array(self.y, mask=invm)
        self._p.plot(self.x, y)
        if residual:
            self._p.palette(7)
            self._p.set_line(label='Residual')
            y = ma.masked_array(self.get_residual(), mask=invm)
            self._p.plot(self.x, y)
        self._p.palette(2)
        if components is not None:
            cs = components
            if isinstance(components, int): cs = [components]
            if plotparms:
                self._p.text(0.15,
                             0.15,
                             str(self.get_parameters()['formatted']),
                             size=8)
            n = len(self.components)
            self._p.palette(3)
            for c in cs:
                if 0 <= c < n:
                    lab = self.fitfuncs[c] + str(c)
                    self._p.set_line(label=lab)
                    y = ma.masked_array(self.fitter.evaluate(c), mask=invm)

                    self._p.plot(self.x, y)
                elif c == -1:
                    self._p.palette(2)
                    self._p.set_line(label="Total Fit")
                    y = ma.masked_array(self.fitter.getfit(), mask=invm)
                    self._p.plot(self.x, y)
        else:
            self._p.palette(2)
            self._p.set_line(label='Fit')
            y = ma.masked_array(self.fitter.getfit(), mask=invm)
            self._p.plot(self.x, y)
        xlim = [min(self.x), max(self.x)]
        self._p.axes.set_xlim(xlim)
        self._p.set_axes('xlabel', xlab)
        self._p.set_axes('ylabel', ylab)
        self._p.set_axes('title', tlab)
        self._p.release()
        if (not rcParams['plotter.gui']):
            self._p.save(filename)