Exemplo n.º 1
0
					# LINE

linemask = spave.create_mask([2900,3150])
curr_max = spave.stats('max', linemask)		#  IF[2] = 1.275
curr_sum = spave.stats('sum', linemask)	        #  IF[2] = 90.350
curr_median = spave.stats('median', linemask)   #  IF[2] = 0.231
curr_mean = spave.stats('mean', linemask)	#  IF[2] = 0.360
											# Chans bchan    echan       Xmin      Xmax        Ymin     Ymax
											#   251  2900     3150     2900.0    3150.0    -0.20952   1.2829
											#                 Mean     Median       RMS    Variance     Area
											#              0.36219    0.23209   0.39342     0.15478   90.909
											#
					# Fitting
spave.set_unit('channel')		# set units to channel				# chan
sd.plotter.plot(spave)			# plot spectrum				
f = sd.fitter()
msk = spave.create_mask([2900,3150])	# create region around line			# gregion,[2900,3150]
f.set_function(gauss=1)			# set a single gaussian component		# ngauss,1
f.set_scan(spave, msk)			# set the data and region for the fitter	
#f.set_gauss_parameters(1.2,3100.,100.)  # set initial guess				# gparamvalues,0,[1.2,3100.,100.]
f.fit()					# auto fit					# gauss
f.plot(residual=True)			# plot residual
f.get_parameters()			# retrieve fit parameters			#
#   0: peak = 1.054 K , centre = 3037.182 channel, FWHM = 71.654 channel
#      area = 80.382 K channel
#
# Regression values of CASA 2.3(#6654)+ASAP 2.2.0(#1448)
# on 64bit REL5.2 (2008/12/01)
#   0: peak = 1.110 K , centre = 3037.176 channel, FWHM = 71.676 channel
#      area = 84.727 K channel
Exemplo n.º 2
0
    def __fit(self, scantab=None):
        # initialize fitter
        self.fitter = sd.fitter()
        if abs(self.plotlevel) > 0:
            self.__init_plot()

        dbw = 1.0
        if scantab is None:
            scantab = self.scan

        current_unit = scantab.get_unit()

        kw = {'thescan': scantab}
        if len(self.defaultmask) > 0: kw['mask'] = self.defaultmask
        self.fitter.set_scan(**kw)
        firstplot = True

        for irow in range(scantab.nrow()):
            casalog.post("start row %d" % (irow))
            # check for FLAGROW
            if scantab._getflagrow(irow):
                casalog.post("Row %d is flagged. skip fitting " % irow)
                self.fitparams.append([[0, 0, 0]])
                self.result['nfit'] += [-1]
                continue
            numlines = self.nlines[irow] if isinstance(self.nlines,list) \
                       else self.nlines

            if numlines == 0:
                self.fitparams.append([[0, 0, 0]])
                self.result['nfit'] += [-1]
                self.__warn_fit_failed(scantab, irow, 'No lines detected.')
                continue

            if (self.fitmode == 'auto'):
                # Auto mode - one comp per line region
                # Overwriting user-supplied nfit
                numfit = numlines
                comps = [1 for i in xrange(numlines)]
            else:
                # Get number of things to fit from nfit list
                comps = self.nfit if isinstance(self.nfit,
                                                list) else [self.nfit]
                # Drop extra over numlines
                numfit = min(len(comps), numlines)
            ncomps = sum(comps)

            casalog.post("Will fit %d components in %d regions" %
                         (ncomps, numfit))

            if numfit <= 0:
                self.fitparams.append([[0, 0, 0]])
                self.result['nfit'] += [-1]
                self.__warn_fit_failed(scantab, irow, 'Fit failed.')
                continue

            # Fit the line using numfit gaussians or lorentzians
            # Assume the nfit list matches maskline
            self.fitter.set_function(**{self.fitfunc: ncomps})
            if (self.doguess):
                # in auto mode, linelist will be detemined for each spectra
                # otherwise, linelist will be the same for all spectra
                if current_unit != 'channel':
                    xx = scantab._getabcissa(irow)
                    dbw = abs(xx[1] - xx[0])
                self.__initial_guess(scantab, dbw, numfit, comps, irow)
            else:
                # No guesses
                casalog.post("Fitting lines without starting guess")

            # Now fit
            self.fitter.fit(row=irow)
            fstat = self.fitter.get_parameters()

            # Check for convergence
            goodfit = (len(fstat['errors']) > 0)
            if (goodfit):
                self.__update_params(ncomps)
            else:
                # Did not converge
                self.result['nfit'] += [-ncomps]
                self.fitparams.append([[0, 0, 0]])
                self.__warn_fit_failed(scantab, irow, 'Fit failed to converge')

            # plot
            if (irow < 16 and abs(self.plotlevel) > 0):
                self.__plot(irow, goodfit, firstplot)
                firstplot = False
Exemplo n.º 3
0
# LINE

linemask = spave.create_mask([2900, 3150])
curr_max = spave.stats('max', linemask)  #  IF[2] = 1.275
curr_sum = spave.stats('sum', linemask)  #  IF[2] = 90.350
curr_median = spave.stats('median', linemask)  #  IF[2] = 0.231
curr_mean = spave.stats('mean', linemask)  #  IF[2] = 0.360
# Chans bchan    echan       Xmin      Xmax        Ymin     Ymax
#   251  2900     3150     2900.0    3150.0    -0.20952   1.2829
#                 Mean     Median       RMS    Variance     Area
#              0.36219    0.23209   0.39342     0.15478   90.909
#
# Fitting
spave.set_unit('channel')  # set units to channel				# chan
sd.plotter.plot(spave)  # plot spectrum
f = sd.fitter()
msk = spave.create_mask(
    [2900, 3150])  # create region around line			# gregion,[2900,3150]
f.set_function(gauss=1)  # set a single gaussian component		# ngauss,1
f.set_scan(spave, msk)  # set the data and region for the fitter
#f.set_gauss_parameters(1.2,3100.,100.)  # set initial guess				# gparamvalues,0,[1.2,3100.,100.]
f.fit()  # auto fit					# gauss
f.plot(residual=True)  # plot residual
f.get_parameters()  # retrieve fit parameters			#
#   0: peak = 1.054 K , centre = 3037.182 channel, FWHM = 71.654 channel
#      area = 80.382 K channel
#
# Regression values of CASA 2.3(#6654)+ASAP 2.2.0(#1448)
# on 64bit REL5.2 (2008/12/01)
#   0: peak = 1.110 K , centre = 3037.176 channel, FWHM = 71.676 channel
#      area = 84.727 K channel
Exemplo n.º 4
0
    def __execute_baseline(self):
        #print "calibration begins..."
        #select the totalpower data for antenna1,2=1 (DV01, vertex antenna)
        # use generic pylab
        if type(self.antid) == list and len(self.antid) > 2:
            #print "Assume the antenna selection is all..."
            casalog.post( "Assume the antenna selection is all..." )
        self.open_table(self.infile)
        ndat = 0
        pl.ioff()
        # determine the size of processing data
        subtb = self.table.query('any(ANTENNA1==%s && ANTENNA2==%s)' % (self.antid,self.antid))
        nr = subtb.nrows()
        #nr = len(data)*len(data[0])
        #ndatcol = numpy.zeros((npol,nr),dtype=numpy.float)
        ndatcol = subtb.getcol(self.datacol)
        (l,m,n) = ndatcol.shape
        # m (= nchan) should be 1 since the task is specifically designed for total power data
        ndatcol.reshape(l,n) 
        for np in range(len(self.selcorrtypeind)):
            pl.figure(np+1)
            pl.clf()
            pl.subplot(311)
            #pl.title(infile+' Ant:'+str(antlist.values()))
            pl.ylabel(self.datacol,fontsize='smaller')
            symbols=['b.','c.']

            #print "Arranging data by scans..."
            casalog.post( "Arranging data by scans..." )

            data = []
            flag = []
            ndat0 = 0
            ndat = 0
            if self.selnpol == 1: np = self.selpol
            pl.title(self.infile.rstrip('/')+' Ant:'+self.antnameused+' '+self.corrtypestr[np])
            casalog.post( "select %s data..." % self.corrtypestr[np] )
            for i in xrange(self.nscan):
                idata = []
                iflag = []
                #for j in xrange(nsubscan):
                for j in xrange(self.nsubscan[i]):
                    # may be need to iterate on each antenna 
                    # identify 'scan' by STATE ID
                    #selsubtb=tb.query('any(ANTENNA1==%s && ANTENNA2==%s) && SCAN_NUMBER==%s && STATE_ID==%s' % (antid,antid,scans[i],subscans[j]))
                    selsubtb = self.table.query('any(ANTENNA1==%s && ANTENNA2==%s) && SCAN_NUMBER==%s && STATE_ID==%s' % (self.antid,self.antid,self.scans[i],self.subscans[i][j]))
                    datcol = selsubtb.getcol(self.datacol)
                    flgcol = selsubtb.getcol("FLAG") # only channel flag is needed to be hanled in MS
                    selsubtb.close()
                    if self.npol > 1 and self.selnpol == 1:
                        #casalog.post( "select %s data..." % corrtypestr[selpol] )
                        rdatcol = datcol[self.selpol].real
                        flagcell = flgcol[self.selpol]
                    else:
                        rdatcol = datcol[self.selcorrtypeind[np]].real
                        flagcell = flgcol[self.selpol]
                    (m,n) = rdatcol.shape
                    if flagcell.shape != (m,n):
                        raise Exception, "Data conformation error. Shape of FLAG and %s differs." % self.datacol
                    rdatcol = rdatcol.reshape(n)
                    flagcell = flagcell.reshape(n)
                    #data.append(rdatcol)
                    idata.append(rdatcol)
                    iflag.append(flagcell)
                    ndat0 = ndat
                    ndat += len(rdatcol)
                data.append(idata)
                flag.append(iflag)
            if abs(self.plotlevel) > 0:
                pl.xlim(0,ndat)
                t1 = pl.getp(pl.gca(), 'xticklabels')
                t2 = pl.getp(pl.gca(), 'yticklabels')
                pl.setp((t1,t2), fontsize='smaller')
                mdat = 0
                for i in xrange(self.nscan):
                    #for j in xrange(nsubscan):
                    for j in xrange(self.nsubscan[i]):
                        mdat0 = mdat
                        mdat += len(data[i][j])
                        masked_data = numpy.ma.masked_array(data[i][j], flag[i][j])
                        #pl.plot(xrange(mdat0,mdat),data[i][j],symbols[subscans[j]%2])
                        #pl.plot(xrange(mdat0,mdat), data[i][j], symbols[self.subscans[i][j]%2])
                        pl.plot(xrange(mdat0,mdat), masked_data, symbols[self.subscans[i][j]%2])
                        ax = pl.gca()
                        if i == 1: 
                            leg = ax.legend(('even subscan no.', 'odd subscan no.'), numpoints=1, handletextsep=0.01) 
                            for t in leg.get_texts():
                                t.set_fontsize('small')
                pl.ion()
                pl.plot()
                #pl.draw()
            ### Calibration ############################################
            # Do a zero-th order calibration by subtracting a baseline #
            # from each scan to take out atmospheric effect.          #
            # The baseline fitting range specified by masklist must be #
            # given and is the same for all the scans.                 # 
            ############################################################
            f = sd.fitter()
            f.set_function(lpoly = self.blpoly)
            #print "Processing %s %s scans" % (corrtypestr[np], nscan)
            casalog.post( "Processing %s %s scans" % (self.corrtypestr[np], self.nscan) )
            cdat = numpy.array([])
            pl.subplot(312)
            pl.ioff()
            for i in xrange(self.nscan):
                casalog.post( "Processing Scan#=%s" % i )
                #for j in xrange(nsubscan):
                for j in xrange(self.nsubscan[i]):
                    masks = numpy.zeros(len(data[i][j]), dtype=numpy.int)
                    if self.left_mask >= len(data[i][j]) \
                           or self.right_mask >= len(data[i][j]):
                        msg = "Too large mask. All data will be used for baseline subtraction.\n The fitting may not be correct since it might confuse signal component as a background..."
                        casalog.post(msg, "WARN")
                    else:
                        msg = "Subtracting baselines, set masks for fitting at row ranges: [0,%s] and [%s,%s] " % (self.left_mask-1, len(masks)-self.right_mask, len(masks)-1)
                        casalog.post(msg, "INFO")
                    #Use edge channels for fitting excluding flagged integrations.
                    #masks[:self.left_mask] = True
                    #masks[-self.right_mask:] = True
                    masks[:self.left_mask] = (flag[i][j][:self.left_mask]==False)
                    masks[-self.right_mask:] = (flag[i][j][-self.right_mask:]==False)
                    x = xrange(len(data[i][j]))
                    if self.plotlevel > 1:
                        pl.cla()
                        pl.ioff()
                        pl.title('scan %s subscan %s'%(self.scans[i],self.subscans[i][j]))
                        #pl.plot(x, data[i][j], 'b')
                        pl.plot(x, numpy.ma.masked_array(data[i][j], flag[i][j]), 'b')
                    f.set_data(x, data[i][j], mask=masks)
                    f.fit()
                    #f.plot(residual=True, components=[1], plotparms=True)
                    fitd=f.get_fit()
                    data[i][j] = data[i][j]-fitd
                    if self.plotlevel > 1:
                        pl.ion()
                        pl.plot(xrange(len(fitd)), fitd, 'r-')
                        pl.draw()
                        pl.cla()
                        pl.ioff()
                        pl.title('scan %s subscan %s'%(self.scans[i], self.subscans[i][j]))
                        pl.ion()
                        #pl.plot(x, data[i][j], 'g')
                        pl.plot(x, numpy.ma.masked_array(data[i][j], flag[i][j]), 'g')
                        pl.draw()
                        if interactive: check=raw_input('Hit return for Next\n')
                    cdat = numpy.concatenate([cdat,data[i][j]])
            ndatcol[np] = cdat
            del data
        subtb.close()
        self.close_table()
        self.open_table(self.infile, nomodify=False)
        # put the corrected data to CORRECTED_DATA column in MS
        # assuming the data for the vertex are stored in every other
        # row starting from 2nd row for the vertex antenna....
        # For the alcatel startrow=0, for len(antid) >1,
        # it assumes all the data to be corrected. 
        if type(self.antid) == int:
            startrow = self.antid
            rowincr = self.nant
        else:
            startrow = 0
            rowincr = 1
        casalog.post( "Storing the corrected data to %s column in the MS..."%self.datacol )
        casalog.post( "Note that %s will be overwritten"%self.datacol )
        cdatm = ndatcol.reshape(self.npol,1,len(cdat))
        self.table.putcol(self.datacol, cdatm, startrow=startrow, rowincr=rowincr)
        self.close_table()
        # calibration end
        #
        # plot corrected data
        if abs(self.plotlevel) > 0:
            casalog.post( "plotting corrected data..." )
            self.__plot(subplot=313, title=False, ylabel='CORRECTED_DATA')
Exemplo n.º 5
0
    def __fit(self, scantab=None):
        # initialize fitter
        self.fitter = sd.fitter()
        if abs(self.plotlevel) > 0:
            self.__init_plot()

        dbw = 1.0
        if scantab is None:
            scantab = self.scan

        current_unit = scantab.get_unit()

        kw = {'thescan':scantab}
        if len(self.defaultmask) > 0: kw['mask'] = self.defaultmask
        self.fitter.set_scan(**kw)
        firstplot = True
    
        for irow in range(scantab.nrow()):
            casalog.post( "start row %d" % (irow) )
            # check for FLAGROW
            if scantab._getflagrow(irow):
                casalog.post( "Row %d is flagged. skip fitting " % irow)
                self.fitparams.append([[0,0,0]])
                self.result['nfit']+=[-1]
                continue
            numlines = self.nlines[irow] if isinstance(self.nlines,list) \
                       else self.nlines

            if numlines == 0:
                self.fitparams.append([[0,0,0]])
                self.result['nfit']+=[-1]
                self.__warn_fit_failed(scantab,irow,'No lines detected.')
                continue
                
            if ( self.fitmode == 'auto'):
                # Auto mode - one comp per line region
                # Overwriting user-supplied nfit
                numfit = numlines
                comps = [1 for i in xrange(numlines)]
            else:
                # Get number of things to fit from nfit list
                comps = self.nfit if isinstance(self.nfit,list) else [self.nfit]
                # Drop extra over numlines
                numfit = min(len(comps),numlines)
            ncomps = sum(comps)
        
            casalog.post( "Will fit %d components in %d regions" % (ncomps, numfit) )

            if numfit <= 0:
                self.fitparams.append([[0,0,0]])
                self.result['nfit']+=[-1]
                self.__warn_fit_failed(scantab,irow,'Fit failed.')
                continue

            # Fit the line using numfit gaussians or lorentzians
            # Assume the nfit list matches maskline
            self.fitter.set_function(**{self.fitfunc:ncomps})
            if ( self.doguess ):
                # in auto mode, linelist will be detemined for each spectra
                # otherwise, linelist will be the same for all spectra
                if current_unit != 'channel':
                    xx = scantab._getabcissa(irow)
                    dbw = abs(xx[1]-xx[0])
                self.__initial_guess(scantab,dbw,numfit,comps,irow)
            else:
                # No guesses
                casalog.post( "Fitting lines without starting guess" )

            # Now fit
            self.fitter.fit(row=irow)
            fstat = self.fitter.get_parameters()

            # Check for convergence
            goodfit = ( len(fstat['errors']) > 0 )
            if ( goodfit ):
                self.__update_params(ncomps)
            else:
                # Did not converge
                self.result['nfit'] += [-ncomps]
                self.fitparams.append([[0,0,0]])
                self.__warn_fit_failed(scantab,irow,'Fit failed to converge')

            # plot
            if (irow < 16 and abs(self.plotlevel) > 0):
                self.__plot(irow, goodfit, firstplot)
                firstplot = False