Exemplo n.º 1
0
def sdfit(infile=None, datacolumn=None, antenna=None, field=None, spw=None,
           timerange=None, scan=None, pol=None, intent=None,
           timeaverage=None, timebin=None, timespan=None,
           polaverage=None, polaveragemode=None,
           fitfunc=None, fitmode=None, nfit=None, thresh=None, avg_limit=None,
           minwidth=None, edge=None, outfile=None, overwrite=None):
    casalog.origin('sdfit')

    try:
        if os.path.exists(outfile):
            if overwrite:
                os.system('rm -rf %s' % outfile)
            else:
                raise ValueError(outfile + ' exists.')
        if (fitmode not in  ['list', 'auto']):
            raise ValueError, "fitmode='%s' is not supported yet" % fitmode
        if (spw == ''): spw = '*'

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                    baseline=antenna, time=timerange, 
                                    scan=scan)

        sdms.open(infile)
        sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                           antenna=antenna, timerange=timerange, 
                           scan=scan, polarization=pol, intent=intent)

        tempfile = 'temp_sdfit_'+str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
        if os.path.exists(tempfile):
            tempfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempfile):
                raise Exception('temporary file ' + tempfile + ' exists...')
        tempoutfile = tempfile + '_temp_output_ms'
        if os.path.exists(tempoutfile):
            tempoutfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempoutfile):
                raise Exception('temporary ms file ' + tempoutfile + ' exists...')
        
        if fitmode=='auto': nfit = [-1]
        num_fit_str = str(',').join(map(str, nfit))

        sdms.fit_line(datacolumn=datacolumn, spw=spw, pol=pol, 
                      timeaverage=timeaverage, timebin=timebin, timespan=timespan,
                      polaverage=polaverage, polaveragemode=polaveragemode,
                      fitfunc=fitfunc, nfit=num_fit_str,
                      linefinding=(fitmode=='auto'), threshold=thresh,
                      avg_limit=avg_limit, minwidth=minwidth, edge=edge,
                      tempfile=tempfile, tempoutfile=tempoutfile)

        if os.path.exists(tempfile):
            return get_results(tempfile, fitfunc, nfit, outfile)
        else:
            raise Exception('temporary file was unexpectedly not created.')

    except Exception, instance:
        raise Exception, instance
Exemplo n.º 2
0
def sdfit(infile=None, datacolumn=None, antenna=None, field=None, spw=None,
           timerange=None, scan=None, pol=None, intent=None,
           timebin=None, timespan=None,
           polaverage=None, 
           fitfunc=None, fitmode=None, nfit=None, thresh=None, avg_limit=None,
           minwidth=None, edge=None, outfile=None, overwrite=None):
    casalog.origin('sdfit')

    try:
        if os.path.exists(outfile):
            if overwrite:
                os.system('rm -rf %s' % outfile)
            else:
                raise ValueError(outfile + ' exists.')
        if (fitmode not in  ['list', 'auto']):
            raise ValueError, "fitmode='%s' is not supported yet" % fitmode
        if (spw == ''): spw = '*'

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                    baseline=antenna, time=timerange, 
                                    scan=scan)

        sdms.open(infile)
        sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                           antenna=antenna, timerange=timerange, 
                           scan=scan, polarization=pol, intent=intent)

        tempfile = 'temp_sdfit_'+str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
        if os.path.exists(tempfile):
            tempfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempfile):
                raise Exception('temporary file ' + tempfile + ' exists...')
        tempoutfile = tempfile + '_temp_output_ms'
        if os.path.exists(tempoutfile):
            tempoutfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempoutfile):
                raise Exception('temporary ms file ' + tempoutfile + ' exists...')
        
        if fitmode=='auto': nfit = [-1]
        num_fit_str = str(',').join(map(str, nfit))

        sdms.fit_line(datacolumn=datacolumn, spw=spw, pol=pol, 
                      timebin=timebin, timespan=timespan,
                      polaverage=polaverage, 
                      fitfunc=fitfunc, nfit=num_fit_str,
                      linefinding=(fitmode=='auto'), threshold=thresh,
                      avg_limit=avg_limit, minwidth=minwidth, edge=edge,
                      tempfile=tempfile, tempoutfile=tempoutfile)

        if os.path.exists(tempfile):
            return get_results(tempfile, fitfunc, nfit, outfile)
        else:
            raise Exception('temporary file was unexpectedly not created.')

    except Exception, instance:
        raise Exception, instance
Exemplo n.º 3
0
def tsdfit(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None,
           fitfunc=None, fitmode=None, nfit=None, thresh=None, min_nchan=None, avg_limit=None, box_size=None,
           edge=None, outfile=None, overwrite=None):
    casalog.origin('tsdbaseline')

    try:
        if os.path.exists(outfile):
            if overwrite:
                os.system('rm -rf %s' % outfile)
            else:
                raise ValueError(outfile + ' exists.')
        if (fitmode != 'list'):
            raise ValueError, "fitmode='%s' is not supported yet" % fitmode
        if (spw == ''): spw = '*'

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                    baseline=str(antenna), time=timerange, 
                                    scan=scan)

        sdms.open(infile)
        sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                           antenna=str(antenna), timerange=timerange, 
                           scan=scan)

        tempfile = 'temp_tsdfit_'+str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
        if os.path.exists(tempfile):
            tempfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempfile):
                raise Exception('temporary file ' + tempfile + ' exists...')
        tempoutfile = tempfile + '_temp_output_ms'
        if os.path.exists(tempoutfile):
            tempoutfile += str(datetime.datetime.fromtimestamp(time.time())).replace('-','').replace(' ','').replace(':','')
            if os.path.exists(tempoutfile):
                raise Exception('temporary ms file ' + tempoutfile + ' exists...')

        sdms.fit_line(datacolumn=datacolumn, spw=spw, pol=pol, fitfunc=fitfunc,
                      nfit=str(nfit)[1:-1].replace(' ', ''),
                      tempfile=tempfile, tempoutfile=tempoutfile)

        if os.path.exists(tempfile):
            return get_results(tempfile, fitfunc, nfit, outfile)
        else:
            raise Exception('temporary file was unexpectedly not created.')

    except Exception, instance:
        raise Exception, instance
Exemplo n.º 4
0
def sdbaseline(infile=None,
               datacolumn=None,
               antenna=None,
               field=None,
               spw=None,
               timerange=None,
               scan=None,
               pol=None,
               intent=None,
               maskmode=None,
               thresh=None,
               avg_limit=None,
               minwidth=None,
               edge=None,
               blmode=None,
               dosubtract=None,
               blformat=None,
               bloutput=None,
               bltable=None,
               blfunc=None,
               order=None,
               npiece=None,
               applyfft=None,
               fftmethod=None,
               fftthresh=None,
               addwn=None,
               rejwn=None,
               clipthresh=None,
               clipniter=None,
               blparam=None,
               verbose=None,
               showprogress=None,
               minnrow=None,
               outfile=None,
               overwrite=None):

    casalog.origin('sdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam

        if (spw == ''): spw = '*'

        if (blmode == 'apply'):
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile,
                                        spw=spw,
                                        field=field,
                                        baseline=antenna,
                                        time=timerange,
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field,
                               antenna=antenna,
                               timerange=timerange,
                               scan=scan,
                               polarization=pol,
                               intent=intent)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()

            restore_sorted_table_keyword(infile, sorttab_info)

        elif (blmode == 'fit'):

            if (blfunc == 'sinusoid'):
                addwn = sdutil.parse_wavenumber_param(addwn)
                rejwn = sdutil.parse_wavenumber_param(rejwn)
                check_fftthresh(fftthresh)

            blformat, bloutput = prepare_for_blformat_bloutput(
                infile, blformat, bloutput, overwrite)

            output_bloutput_text_header(blformat, bloutput, blfunc, maskmode,
                                        infile, outfile)

            if (blfunc == 'variable'):
                sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile,
                                        spw=spw,
                                        field=field,
                                        baseline=antenna,
                                        time=timerange,
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field,
                               antenna=antenna,
                               timerange=timerange,
                               scan=scan,
                               polarization=pol,
                               intent=intent)
            params, func = prepare_for_baselining(
                blfunc=blfunc,
                datacolumn=datacolumn,
                outfile=outfile,
                bloutput=','.join(bloutput),
                dosubtract=dosubtract,
                spw=spw,
                pol=pol,
                linefinding=(maskmode == 'auto'),
                threshold=thresh,
                avg_limit=avg_limit,
                minwidth=minwidth,
                edge=edge,
                order=order,
                npiece=npiece,
                applyfft=applyfft,
                fftmethod=fftmethod,
                fftthresh=fftthresh,
                addwn=addwn,
                rejwn=rejwn,
                clip_threshold_sigma=clipthresh,
                num_fitting_max=clipniter + 1,
                blparam=blparam)
            func(**params)
            sdms.close()

            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance
Exemplo n.º 5
0
def tsdbaseline(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None, maskmode=None, thresh=None, avg_limit=None, minwidth=None, edge=None, blmode=None, dosubtract=None, blformat=None, bloutput=None, bltable=None, blfunc=None, order=None, npiece=None, applyfft=None, fftmethod=None, fftthresh=None, addwn=None, rejwn=None, clipthresh=None, clipniter=None, blparam=None, verify=None, verbose=None, showprogress=None, minnrow=None, outfile=None, overwrite=None):

    casalog.origin('tsdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam
        
        if (spw == ''): spw = '*'

        if blmode == 'apply':
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            with sdutil.tbmanager(infile + '/DATA_DESCRIPTION') as tb:
                spw_ids = tb.getcol('SPECTRAL_WINDOW_ID')
            with sdutil.tbmanager(infile + '/ANTENNA') as tb:
                ant_ids = range(tb.nrows())
            with sdutil.tbmanager(infile + '/FEED') as tb:
                feed_ids = numpy.unique(tb.getcol('FEED_ID'))

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=str(antenna), time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=str(antenna), timerange=timerange, 
                               scan=scan)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()
            
            restore_sorted_table_keyword(infile, sorttab_info)
            
        elif blmode == 'fit':
            blout_exists = False
            if (isinstance(bloutput, str) and os.path.exists(bloutput)):
                blout_exists = True
            elif isinstance(bloutput, list):
                for blout in bloutput:
                    if os.path.exists(blout):
                        blout_exists = True
                        break
            if blout_exists and not overwrite:
                raise ValueError, "file(s) specified in bloutput exists."

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=str(antenna), time=timerange, 
                                        scan=scan)

            if blfunc == 'variable':
                sorttab_info = remove_sorted_table_keyword(infile)
        
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=str(antenna), timerange=timerange, 
                               scan=scan)

            
            #if(outfile==''):
            #    outfile=infile


            

            

#            if isinstance(blformat, str):
#                if('' == blformat):
#                    #blformat=',,'
#                    bloutput=',,'
#                    print 'blformat'
#                if('text' == blformat):
#                    bloutput_tmp = infile + '_blparam.txt'
#                    bloutput = ',' + bloutput_tmp + ','
#                    if os.path.exists(bloutput):
#                        raise Exception(fname + ' exists.') 





            new_bloutput=[]


            if isinstance(blformat, str):
                blformat = [blformat]
        
            

            if isinstance(bloutput, str):
                bloutput = [bloutput] 




            if isinstance(blformat, list):
                if('' in blformat):
                    blformat = ',,'
                #elif(len(blformat) > 3 ):
                #    raise ValueError, "The maximum size of blformat must be three."
                elif(len(blformat) != len(bloutput)):
                    raise ValueError, "The size of blfomat must be the same as that of bloutput."
                elif [key for key, val in Counter(blformat).items() if val>1]:
                    raise ValueError, "Same multiple blformats are not allowed."
                else:
                    if('csv' in blformat):
                        if(''!= bloutput[blformat.index('csv')]):
                            fname=bloutput[blformat.index('csv')]
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.') 
                            new_bloutput.append(bloutput[blformat.index('csv')])
                        else:
                            fname= infile + '_blparam.csv'
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists. ')######################################################
                            new_bloutput.append(infile + '_blparam.csv')
                    else:
                         new_bloutput.append('')

                    

                    

                    if('text' in blformat):
                        if(''!= bloutput[blformat.index('text')]):
                            new_bloutput.append(bloutput[blformat.index('text')])
                        
                            fname = bloutput[blformat.index('text')]

                            
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')


                            f = open(fname, "w")

                            blf = blfunc.lower()
                            mm = maskmode.lower()
                            if blf == 'poly':
                                ftitles = ['Fit order']
                            elif blf == 'chebyshev':
                                ftitles = ['Fit order']
                            elif blf == 'cspline':
                                ftitles = ['nPiece']
                                #print 'task 2'
                            else: # sinusoid
                                ftitles = ['applyFFT', 'fftMethod', 'fftThresh', 'addWaveN', 'rejWaveN']
                            if mm == 'auto':
                                mtitles = ['Threshold', 'avg_limit', 'Edge']
                            elif mm == 'list':
                                mtitles = []
                            else: # interact
                                mtitles = []
                            ctitles = ['clipThresh', 'clipNIter']

                            #fkeys = getattr(self, '%s_keys'%(blfunc))
                            #mkeys = getattr(self, '%s_keys'%(maskmode))
                            
                            outfile2=''
                            if(outfile==''): 
                                outfile2 = infile
                            else:
                                outfile2 = outfile
                            

                            info = [['Source Table', infile],
                                    ['Output File', outfile2]]
                                    #['Function', blfunc],
                                    #['Fit order', order]] 
                            #for i in xrange(len(ftitles)):
                            #    info.append([ftitles[i],getattr(self,fkeys[i])])
                            #if blf != 'poly':
                            #    for i in xrange(len(ctitles)):
                            #        info.append([ctitles[i],clip_keys[i]])
                            info.append(['Mask mode', maskmode])
                            #for i in xrange(len(mtitles)):
                            #    info.append([mtitles[i],getattr(mkeys[i])])

                            separator = "#"*60 + "\n"

                            f.write(separator)
                            for i in xrange(len(info)):
                                f.write('%12s: %s\n'%tuple(info[i]))
                            f.write(separator)
                            f.close()

                        
                        
                        else:
                            #new_bloutput.append(infile + '_blparam.txt')
                            new_bloutput.append(infile + '_blparam.txt')
                            fname2  = infile + '_blparam.txt'
                            if not overwrite and os.path.exists(fname2):
                                raise Exception(fname2 + ' exists.')

                            f = open(fname2, "w")

                            blf = blfunc.lower()
                            mm = maskmode.lower()
                            if blf == 'poly':
                                ftitles = ['Fit order']
                            elif blf == 'chebyshev':
                                ftitles = ['Fit order']
                            elif blf == 'cspline':
                                ftitles = ['nPiece']
                            else: # sinusoid
                                ftitles = ['applyFFT', 'fftMethod', 'fftThresh', 'addWaveN', 'rejWaveN']
                            if mm == 'auto':
                                mtitles = ['Threshold', 'avg_limit', 'Edge']
                            elif mm == 'list':
                                mtitles = []
                            else: # interact
                                mtitles = []
                            ctitles = ['clipThresh', 'clipNIter']

                            #fkeys = getattr(self, '%s_keys'%(blfunc))
                            #mkeys = getattr(self, '%s_keys'%(maskmode))

                            outfile2=''
                            if(outfile==''):
                                outfile2 = infile

                            info = [['Source Table', infile],
                                    ['Output File', outfile]]
                                    #['Function', blfunc],
                                    #['Fit order', order]] 
                            #for i in xrange(len(ftitles)):
                            #    info.append([ftitles[i],getattr(self,fkeys[i])])
                            #if blf != 'poly':
                            #    for i in xrange(len(ctitles)):
                            #        info.append([ctitles[i],clip_keys[i]])
                            info.append(['Mask mode', maskmode])
                            #for i in xrange(len(mtitles)):
                            #    info.append([mtitles[i],getattr(mkeys[i])])

                            separator = "#"*60 + "\n"

                            f.write(separator)
                            for i in xrange(len(info)):
                                f.write('%12s: %s\n'%tuple(info[i]))
                            f.write(separator)
                            f.close()       
                    
                   
                    else:
                        new_bloutput.append('')



                    if('table' in blformat):
                        #print 'blformat->',type(blformat), blformat
                        #print "blformat.index('table')", blformat.index('table')
                        #print "bloutput[blformat.index('table')])", bloutput[blformat.index('table')]

                        if(''!= bloutput[blformat.index('table')]):
                            fname = bloutput[blformat.index('table')]
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')#############################################

                            new_bloutput.append(bloutput[blformat.index('table')])
                       
                        else:
                            fname = infile + '_blparam.bltable'
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')#############################################
                                print ''
                            new_bloutput.append(fname)



                    else:
                        new_bloutput.append('')

                    blformat = ",".join(blformat)
                    bloutput = ",".join(new_bloutput)

            
            #print 'task blformat',type(blformat), blformat
            #print 'task bloutput',type(bloutput), bloutput
                



            params, func = prepare_for_baselining(blfunc=blfunc,
                                                  datacolumn=datacolumn,
                                                  outfile=outfile,
                                                  bltable=bloutput, # remove this line once text/csv output becomes available (2015/7/1 WK)
                                                  blformat=blformat,
                                                  bloutput=bloutput,
                                                  dosubtract=dosubtract,
                                                  spw=spw,
                                                  pol=pol,
                                                  order=order,
                                                  npiece=npiece,
                                                  blparam=blparam,
                                                  clip_threshold_sigma=clipthresh,
                                                  num_fitting_max=clipniter+1,
                                                  linefinding=(maskmode=='auto'),
                                                  threshold=thresh,
                                                  avg_limit=avg_limit,
                                                  minwidth=minwidth,
                                                  edge=edge)
            if overwrite:
                if os.path.exists(outfile):
                    os.system('rm -rf %s' % outfile)
                for bloutfile in new_bloutput:
                    if os.path.exists(bloutfile):
                        os.system('rm -rf %s' % bloutfile)

            #print params
            if(blformat != ',,'):
                func(**params)
            
            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance
Exemplo n.º 6
0
def tsdbaseline(infile=None,
                datacolumn=None,
                antenna=None,
                field=None,
                spw=None,
                timerange=None,
                scan=None,
                pol=None,
                maskmode=None,
                thresh=None,
                avg_limit=None,
                minwidth=None,
                edge=None,
                blmode=None,
                dosubtract=None,
                blformat=None,
                bloutput=None,
                bltable=None,
                blfunc=None,
                order=None,
                npiece=None,
                applyfft=None,
                fftmethod=None,
                fftthresh=None,
                addwn=None,
                rejwn=None,
                clipthresh=None,
                clipniter=None,
                blparam=None,
                verify=None,
                verbose=None,
                showprogress=None,
                minnrow=None,
                outfile=None,
                overwrite=None):

    casalog.origin('tsdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam

        if (spw == ''): spw = '*'

        if blmode == 'apply':
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            with sdutil.tbmanager(infile + '/DATA_DESCRIPTION') as tb:
                spw_ids = tb.getcol('SPECTRAL_WINDOW_ID')
            with sdutil.tbmanager(infile + '/ANTENNA') as tb:
                ant_ids = range(tb.nrows())
            with sdutil.tbmanager(infile + '/FEED') as tb:
                feed_ids = numpy.unique(tb.getcol('FEED_ID'))

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile,
                                        spw=spw,
                                        field=field,
                                        baseline=str(antenna),
                                        time=timerange,
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field,
                               antenna=str(antenna),
                               timerange=timerange,
                               scan=scan)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()

            restore_sorted_table_keyword(infile, sorttab_info)

        elif blmode == 'fit':
            blout_exists = False
            if (isinstance(bloutput, str) and os.path.exists(bloutput)):
                blout_exists = True
            elif isinstance(bloutput, list):
                for blout in bloutput:
                    if os.path.exists(blout):
                        blout_exists = True
                        break
            if blout_exists and not overwrite:
                raise ValueError, "file(s) specified in bloutput exists."

            selection = ms.msseltoindex(vis=infile,
                                        spw=spw,
                                        field=field,
                                        baseline=str(antenna),
                                        time=timerange,
                                        scan=scan)

            if blfunc == 'variable':
                sorttab_info = remove_sorted_table_keyword(infile)

            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field,
                               antenna=str(antenna),
                               timerange=timerange,
                               scan=scan)

            #if(outfile==''):
            #    outfile=infile

            #            if isinstance(blformat, str):
            #                if('' == blformat):
            #                    #blformat=',,'
            #                    bloutput=',,'
            #                    print 'blformat'
            #                if('text' == blformat):
            #                    bloutput_tmp = infile + '_blparam.txt'
            #                    bloutput = ',' + bloutput_tmp + ','
            #                    if os.path.exists(bloutput):
            #                        raise Exception(fname + ' exists.')

            new_bloutput = []

            if isinstance(blformat, str):
                blformat = [blformat]

            if isinstance(bloutput, str):
                bloutput = [bloutput]

            if isinstance(blformat, list):
                if ('' in blformat):
                    blformat = ',,'
                #elif(len(blformat) > 3 ):
                #    raise ValueError, "The maximum size of blformat must be three."
                elif (len(blformat) != len(bloutput)):
                    raise ValueError, "The size of blfomat must be the same as that of bloutput."
                elif [
                        key for key, val in Counter(blformat).items()
                        if val > 1
                ]:
                    raise ValueError, "Same multiple blformats are not allowed."
                else:
                    if ('csv' in blformat):
                        if ('' != bloutput[blformat.index('csv')]):
                            fname = bloutput[blformat.index('csv')]
                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')
                            new_bloutput.append(
                                bloutput[blformat.index('csv')])
                        else:
                            fname = infile + '_blparam.csv'
                            if not overwrite and os.path.exists(fname):
                                raise Exception(
                                    fname + ' exists. '
                                )  ######################################################
                            new_bloutput.append(infile + '_blparam.csv')
                    else:
                        new_bloutput.append('')

                    if ('text' in blformat):
                        if ('' != bloutput[blformat.index('text')]):
                            new_bloutput.append(
                                bloutput[blformat.index('text')])

                            fname = bloutput[blformat.index('text')]

                            if not overwrite and os.path.exists(fname):
                                raise Exception(fname + ' exists.')

                            f = open(fname, "w")

                            blf = blfunc.lower()
                            mm = maskmode.lower()
                            if blf == 'poly':
                                ftitles = ['Fit order']
                            elif blf == 'chebyshev':
                                ftitles = ['Fit order']
                            elif blf == 'cspline':
                                ftitles = ['nPiece']
                                #print 'task 2'
                            else:  # sinusoid
                                ftitles = [
                                    'applyFFT', 'fftMethod', 'fftThresh',
                                    'addWaveN', 'rejWaveN'
                                ]
                            if mm == 'auto':
                                mtitles = ['Threshold', 'avg_limit', 'Edge']
                            elif mm == 'list':
                                mtitles = []
                            else:  # interact
                                mtitles = []
                            ctitles = ['clipThresh', 'clipNIter']

                            #fkeys = getattr(self, '%s_keys'%(blfunc))
                            #mkeys = getattr(self, '%s_keys'%(maskmode))

                            outfile2 = ''
                            if (outfile == ''):
                                outfile2 = infile
                            else:
                                outfile2 = outfile

                            info = [['Source Table', infile],
                                    ['Output File', outfile2]]
                            #['Function', blfunc],
                            #['Fit order', order]]
                            #for i in xrange(len(ftitles)):
                            #    info.append([ftitles[i],getattr(self,fkeys[i])])
                            #if blf != 'poly':
                            #    for i in xrange(len(ctitles)):
                            #        info.append([ctitles[i],clip_keys[i]])
                            info.append(['Mask mode', maskmode])
                            #for i in xrange(len(mtitles)):
                            #    info.append([mtitles[i],getattr(mkeys[i])])

                            separator = "#" * 60 + "\n"

                            f.write(separator)
                            for i in xrange(len(info)):
                                f.write('%12s: %s\n' % tuple(info[i]))
                            f.write(separator)
                            f.close()

                        else:
                            #new_bloutput.append(infile + '_blparam.txt')
                            new_bloutput.append(infile + '_blparam.txt')
                            fname2 = infile + '_blparam.txt'
                            if not overwrite and os.path.exists(fname2):
                                raise Exception(fname2 + ' exists.')

                            f = open(fname2, "w")

                            blf = blfunc.lower()
                            mm = maskmode.lower()
                            if blf == 'poly':
                                ftitles = ['Fit order']
                            elif blf == 'chebyshev':
                                ftitles = ['Fit order']
                            elif blf == 'cspline':
                                ftitles = ['nPiece']
                            else:  # sinusoid
                                ftitles = [
                                    'applyFFT', 'fftMethod', 'fftThresh',
                                    'addWaveN', 'rejWaveN'
                                ]
                            if mm == 'auto':
                                mtitles = ['Threshold', 'avg_limit', 'Edge']
                            elif mm == 'list':
                                mtitles = []
                            else:  # interact
                                mtitles = []
                            ctitles = ['clipThresh', 'clipNIter']

                            #fkeys = getattr(self, '%s_keys'%(blfunc))
                            #mkeys = getattr(self, '%s_keys'%(maskmode))

                            outfile2 = ''
                            if (outfile == ''):
                                outfile2 = infile

                            info = [['Source Table', infile],
                                    ['Output File', outfile]]
                            #['Function', blfunc],
                            #['Fit order', order]]
                            #for i in xrange(len(ftitles)):
                            #    info.append([ftitles[i],getattr(self,fkeys[i])])
                            #if blf != 'poly':
                            #    for i in xrange(len(ctitles)):
                            #        info.append([ctitles[i],clip_keys[i]])
                            info.append(['Mask mode', maskmode])
                            #for i in xrange(len(mtitles)):
                            #    info.append([mtitles[i],getattr(mkeys[i])])

                            separator = "#" * 60 + "\n"

                            f.write(separator)
                            for i in xrange(len(info)):
                                f.write('%12s: %s\n' % tuple(info[i]))
                            f.write(separator)
                            f.close()

                    else:
                        new_bloutput.append('')

                    if ('table' in blformat):
                        #print 'blformat->',type(blformat), blformat
                        #print "blformat.index('table')", blformat.index('table')
                        #print "bloutput[blformat.index('table')])", bloutput[blformat.index('table')]

                        if ('' != bloutput[blformat.index('table')]):
                            fname = bloutput[blformat.index('table')]
                            if not overwrite and os.path.exists(fname):
                                raise Exception(
                                    fname + ' exists.'
                                )  #############################################

                            new_bloutput.append(
                                bloutput[blformat.index('table')])

                        else:
                            fname = infile + '_blparam.bltable'
                            if not overwrite and os.path.exists(fname):
                                raise Exception(
                                    fname + ' exists.'
                                )  #############################################
                                print ''
                            new_bloutput.append(fname)

                    else:
                        new_bloutput.append('')

                    blformat = ",".join(blformat)
                    bloutput = ",".join(new_bloutput)

            #print 'task blformat',type(blformat), blformat
            #print 'task bloutput',type(bloutput), bloutput

            params, func = prepare_for_baselining(
                blfunc=blfunc,
                datacolumn=datacolumn,
                outfile=outfile,
                bltable=
                bloutput,  # remove this line once text/csv output becomes available (2015/7/1 WK)
                blformat=blformat,
                bloutput=bloutput,
                dosubtract=dosubtract,
                spw=spw,
                pol=pol,
                order=order,
                npiece=npiece,
                blparam=blparam,
                clip_threshold_sigma=clipthresh,
                num_fitting_max=clipniter + 1,
                linefinding=(maskmode == 'auto'),
                threshold=thresh,
                avg_limit=avg_limit,
                minwidth=minwidth,
                edge=edge)
            if overwrite:
                if os.path.exists(outfile):
                    os.system('rm -rf %s' % outfile)
                for bloutfile in new_bloutput:
                    if os.path.exists(bloutfile):
                        os.system('rm -rf %s' % bloutfile)

            #print params
            if (blformat != ',,'):
                func(**params)

            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance
Exemplo n.º 7
0
def tsdbaseline(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None, intent=None, maskmode=None, thresh=None, avg_limit=None, minwidth=None, edge=None, blmode=None, dosubtract=None, blformat=None, bloutput=None, bltable=None, blfunc=None, order=None, npiece=None, 
applyfft=None, fftmethod=None, fftthresh=None, 
addwn=None, rejwn=None, clipthresh=None, clipniter=None, blparam=None, verbose=None, showprogress=None, minnrow=None, outfile=None, overwrite=None):

    casalog.origin('tsdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam
        
        if (spw == ''): spw = '*'

        if (blmode == 'apply'):
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=antenna, timerange=timerange, 
                               scan=scan, polarization=pol, intent=intent)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()
            
            restore_sorted_table_keyword(infile, sorttab_info)
            
        elif (blmode == 'fit'):

            #if(blfunc=='sinusoid'):
            #    addwn=sdutil._parse_wn(addwn)
            #    rejwn=sdutil._parse_wn(rejwn)
      

            sep=['-','<=','<','>=','>']
            addwn_tmp=[]

            if(blfunc=='sinusoid'):
                if(type(addwn)==str):
                    for isep in sep:
                        if(addwn.find(isep) != -1 and isep=='-'):
                            for i in range(int(addwn.split('-')[1].strip())+1):
                                addwn_tmp.append(i)
                            for i in range(int(addwn.split('-')[0].strip())):
                                addwn_tmp.remove(i)
                        
                        if(addwn.find(isep) != -1 and isep=='<'):
                            if(addwn.split('<')[0].strip()=='' and type(int(addwn.split('<')[1].strip()))==int):
                                for i in  range(int(addwn.split('<')[1].strip())):
                                    addwn_tmp.append(i)
                                break
                            elif(addwn.split('<')[1].strip()=='' and type(int(addwn.split('<')[0].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='<')
                                break
                            elif(addwn.split('<=')[1].strip()=='' and type(int(addwn.split('<=')[0].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='<=')
                                break
                        if(addwn.find(isep) != -1 and isep=='>'):
                            if(addwn.split('>=')[0].strip()=='' and type(int(addwn.split('>=')[1].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='>=')
                                break
                            elif(addwn.split('>')[1].strip()=='' and type(int(addwn.split('>')[0].strip()))==int):
                                for i in  range(int(addwn.split('>')[0].strip())):
                                    addwn_tmp.append(i)
                                break
                            elif(addwn.split('>')[0].strip()=='' and type(int(addwn.split('>')[1].strip()))==int):
                                addwn_tmp=addwn_string_greaterthan(infile, addwn, spw, inequality='>')
                                break
                            
                #print addwn_tmp
                    addwn=addwn_tmp

            blformat, bloutput = prepare_for_blformat_bloutput(infile, blformat, bloutput, overwrite)

            output_bloutput_text_header(blformat, bloutput,
                                        blfunc, maskmode,
                                        infile, outfile)
            
            if (blfunc == 'variable'):
                sorttab_info = remove_sorted_table_keyword(infile)
        
            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field, antenna=antenna,
                               timerange=timerange, scan=scan,
                               polarization=pol, intent=intent)
            params, func = prepare_for_baselining(blfunc=blfunc,
                                                  datacolumn=datacolumn,
                                                  outfile=outfile,
                                                  bloutput=','.join(bloutput),
                                                  dosubtract=dosubtract,
                                                  spw=spw,
                                                  pol=pol,
                                                  order=order,
                                                  npiece=npiece,
                                                  blparam=blparam,
                                                  clip_threshold_sigma=clipthresh,
                                                  num_fitting_max=clipniter+1,
                                                  linefinding=(maskmode=='auto'),
                                                  threshold=thresh,
                                                  avg_limit=avg_limit,
                                                  minwidth=minwidth,
                                                  edge=edge,
                                                  applyfft=applyfft,###############
                                                  fftmethod=fftmethod,############
                                                  fftthresh=fftthresh,############
                                                  addwn=addwn,###################
                                                  rejwn=rejwn)####################
            func(**params)
            sdms.close()
            
            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance
Exemplo n.º 8
0
def sdbaseline(infile=None, datacolumn=None, antenna=None, field=None, spw=None, timerange=None, scan=None, pol=None, intent=None, maskmode=None, thresh=None, avg_limit=None, minwidth=None, edge=None, blmode=None, dosubtract=None, blformat=None, bloutput=None, bltable=None, blfunc=None, order=None, npiece=None, applyfft=None, fftmethod=None, fftthresh=None, addwn=None, rejwn=None, clipthresh=None, clipniter=None, blparam=None, verbose=None, showprogress=None, minnrow=None, outfile=None, overwrite=None):

    casalog.origin('sdbaseline')
    try:
        if (outfile == '') or not isinstance(outfile, str):
            print("type=%s, value=%s" % (type(outfile), str(outfile)))
            raise ValueError, "outfile name is empty."
        if os.path.exists(outfile) and not overwrite:
            raise Exception(outfile + ' exists.')
        if (maskmode == 'interact'):
            raise ValueError, "maskmode='%s' is not supported yet" % maskmode
        if (blfunc == 'variable' and not os.path.exists(blparam)):
            raise ValueError, "input file '%s' does not exists" % blparam
        
        if (spw == ''): spw = '*'

        if (blmode == 'apply'):
            if not os.path.exists(bltable):
                raise ValueError, "file specified in bltable '%s' does not exist." % bltable

            sorttab_info = remove_sorted_table_keyword(infile)

            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection), field=field, 
                               antenna=antenna, timerange=timerange, 
                               scan=scan, polarization=pol, intent=intent)
            sdms.apply_baseline_table(bltable=bltable,
                                      datacolumn=datacolumn,
                                      spw=spw,
                                      outfile=outfile)
            sdms.close()
            
            restore_sorted_table_keyword(infile, sorttab_info)
            
        elif (blmode == 'fit'):

            if(blfunc == 'sinusoid'):
                addwn = sdutil.parse_wavenumber_param(addwn)
                rejwn = sdutil.parse_wavenumber_param(rejwn)
                check_fftthresh(fftthresh)

            blformat, bloutput = prepare_for_blformat_bloutput(infile, blformat, bloutput, overwrite)

            output_bloutput_text_header(blformat, bloutput,
                                        blfunc, maskmode,
                                        infile, outfile)
            
            if (blfunc == 'variable'):
                sorttab_info = remove_sorted_table_keyword(infile)
        
            if overwrite and os.path.exists(outfile) and (infile != outfile):
                os.system('rm -rf %s' % outfile)

            selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
                                        baseline=antenna, time=timerange, 
                                        scan=scan)
            sdms.open(infile)
            sdms.set_selection(spw=sdutil.get_spwids(selection),
                               field=field, antenna=antenna,
                               timerange=timerange, scan=scan,
                               polarization=pol, intent=intent)
            params, func = prepare_for_baselining(blfunc=blfunc,
                                                  datacolumn=datacolumn,
                                                  outfile=outfile,
                                                  bloutput=','.join(bloutput),
                                                  dosubtract=dosubtract,
                                                  spw=spw,
                                                  pol=pol,
                                                  linefinding=(maskmode=='auto'),
                                                  threshold=thresh,
                                                  avg_limit=avg_limit,
                                                  minwidth=minwidth,
                                                  edge=edge,
                                                  order=order,
                                                  npiece=npiece,
                                                  applyfft=applyfft,
                                                  fftmethod=fftmethod,
                                                  fftthresh=fftthresh,
                                                  addwn=addwn,
                                                  rejwn=rejwn,
                                                  clip_threshold_sigma=clipthresh,
                                                  num_fitting_max=clipniter+1,
                                                  blparam=blparam)
            func(**params)
            sdms.close()
            
            if (blfunc == 'variable'):
                restore_sorted_table_keyword(infile, sorttab_info)

    except Exception, instance:
        raise Exception, instance