Changeset 2767 for trunk/python


Ignore:
Timestamp:
02/08/13 22:23:22 (11 years ago)
Author:
WataruKawasaki
Message:

New Development: Yes

JIRA Issue: Yes CAS-4794

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): sd

Description: functions to apply/write STBaselineTable in which baseline parameters stored.


File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/scantable.py

    r2761 r2767  
    173173def pack_progress_params(showprogress, minnrow):
    174174    return str(showprogress).lower() + ',' + str(minnrow)
     175
     176def pack_blinfo(blinfo, maxirow):
     177    """\
     178    convert a dictionary or a list of dictionaries of baseline info
     179    into a list of comma-separated strings.
     180    """
     181    if isinstance(blinfo, dict):
     182        res = do_pack_blinfo(blinfo, maxirow)
     183        return [res] if res != '' else []
     184    elif isinstance(blinfo, list) or isinstance(blinfo, tuple):
     185        res = []
     186        for i in xrange(len(blinfo)):
     187            resi = do_pack_blinfo(blinfo[i], maxirow)
     188            if resi != '':
     189                res.append(resi)
     190    return res
     191
     192def do_pack_blinfo(blinfo, maxirow):
     193    """\
     194    convert a dictionary of baseline info for a spectrum into
     195    a comma-separated string.
     196    """
     197    dinfo = {}
     198    for key in ['row', 'blfunc', 'masklist']:
     199        if blinfo.has_key(key):
     200            val = blinfo[key]
     201            if key == 'row':
     202                irow = val
     203            if isinstance(val, list) or isinstance(val, tuple):
     204                slval = []
     205                for i in xrange(len(val)):
     206                    if isinstance(val[i], list) or isinstance(val[i], tuple):
     207                        for j in xrange(len(val[i])):
     208                            slval.append(str(val[i][j]))
     209                    else:
     210                        slval.append(str(val[i]))
     211                sval = ",".join(slval)
     212            else:
     213                sval = str(val)
     214
     215            dinfo[key] = sval
     216        else:
     217            raise ValueError("'"+key+"' is missing in blinfo.")
     218
     219    if irow >= maxirow: return ''
     220   
     221    for key in ['order', 'npiece', 'nwave']:
     222        if blinfo.has_key(key):
     223            val = blinfo[key]
     224            if isinstance(val, list) or isinstance(val, tuple):
     225                slval = []
     226                for i in xrange(len(val)):
     227                    slval.append(str(val[i]))
     228                sval = ",".join(slval)
     229            else:
     230                sval = str(val)
     231            dinfo[key] = sval
     232
     233    blfunc = dinfo['blfunc']
     234    fspec_keys = {'poly': 'order', 'chebyshev': 'order', 'cspline': 'npiece', 'sinusoid': 'nwave'}
     235
     236    fspec_key = fspec_keys[blfunc]
     237    if not blinfo.has_key(fspec_key):
     238        raise ValueError("'"+fspec_key+"' is missing in blinfo.")
     239
     240    clip_params_n = 0
     241    for key in ['clipthresh', 'clipniter']:
     242        if blinfo.has_key(key):
     243            clip_params_n += 1
     244            dinfo[key] = str(blinfo[key])
     245
     246    if clip_params_n == 0:
     247        dinfo['clipthresh'] = '0.0'
     248        dinfo['clipniter']  = '0'
     249    elif clip_params_n != 2:
     250        raise ValueError("both 'clipthresh' and 'clipniter' must be given for n-sigma clipping.")
     251
     252    lf_params_n = 0
     253    for key in ['thresh', 'edge', 'chan_avg_limit']:
     254        if blinfo.has_key(key):
     255            lf_params_n += 1
     256            val = blinfo[key]
     257            if isinstance(val, list) or isinstance(val, tuple):
     258                slval = []
     259                for i in xrange(len(val)):
     260                    slval.append(str(val[i]))
     261                sval = ",".join(slval)
     262            else:
     263                sval = str(val)
     264            dinfo[key] = sval
     265
     266    if lf_params_n == 3:
     267        dinfo['use_linefinder'] = 'true'
     268    elif lf_params_n == 1:
     269        dinfo['use_linefinder'] = 'false'
     270        dinfo['thresh']         = ''
     271        dinfo['edge']           = ''
     272        dinfo['chan_avg_limit'] = ''
     273    else:
     274        raise ValueError("all of 'thresh', 'edge' and 'chan_avg_limit' must be given to use linefinder.")
     275   
     276    slblinfo = [dinfo['row'], blfunc, dinfo[fspec_key], dinfo['masklist'], \
     277                dinfo['clipthresh'], dinfo['clipniter'], \
     278                dinfo['use_linefinder'], dinfo['thresh'], dinfo['edge'], dinfo['chan_avg_limit']]
     279   
     280    return ":".join(slblinfo)
     281
     282def parse_fitresult(sres):
     283    """\
     284    Parse the returned value of apply_bltable() or sub_baseline() and
     285    extract row number, the best-fit coefficients and rms, then pack
     286    them into a dictionary.
     287    The input value is generated by Scantable::packFittingResults() and
     288    formatted as 'row:coeff[0],coeff[1],..,coeff[n-1]:rms'.
     289    """
     290    res = []
     291    for i in xrange(len(sres)):
     292        (srow, scoeff, srms) = sres[i].split(":")
     293        row = int(srow)
     294        rms = float(srms)
     295        lscoeff = scoeff.split(",")
     296        coeff = []
     297        for j in xrange(len(lscoeff)):
     298            coeff.append(float(lscoeff[j]))
     299        res.append({'row': row, 'coeff': coeff, 'rms': rms})
     300
     301    return res
    175302
    176303class scantable(Scantable):
     
    25562683
    25572684    @asaplog_post_dec
     2685    def apply_bltable(self, insitu=None, inbltable=None, outbltable=None, overwrite=None):
     2686        """\
     2687        Subtract baseline based on parameters written in Baseline Table.
     2688
     2689        Parameters:
     2690            insitu:        if False a new scantable is returned.
     2691                           Otherwise, the scaling is done in-situ
     2692                           The default is taken from .asaprc (False)
     2693            inbltable:     name of input baseline table. The row number of
     2694                           scantable and that of inbltable must be
     2695                           identical.
     2696            outbltable:    name of output baseline table where baseline
     2697                           parameters and fitting results recorded.
     2698                           default is ''(no output).
     2699            overwrite:     if True, overwrites the existing baseline table
     2700                           specified in outbltable.
     2701                           default is False.
     2702        """
     2703
     2704        try:
     2705            varlist = vars()
     2706            if inbltable      is None: raise ValueError("bltable missing.")
     2707            if outbltable     is None: outbltable     = ''
     2708            if overwrite      is None: overwrite      = False
     2709
     2710            if insitu is None: insitu = rcParams['insitu']
     2711            if insitu:
     2712                workscan = self
     2713            else:
     2714                workscan = self.copy()
     2715
     2716            sres = workscan._apply_bltable(inbltable,
     2717                                           outbltable,
     2718                                           os.path.exists(outbltable),
     2719                                           overwrite)
     2720            res = parse_fitresult(sres)
     2721
     2722            workscan._add_history('apply_bltable', varlist)
     2723
     2724            if insitu:
     2725                self._assign(workscan)
     2726                return res
     2727            else:
     2728                return {'scantable': workscan, 'fitresults': res}
     2729       
     2730        except RuntimeError, e:
     2731            raise_fitting_failure_exception(e)
     2732
     2733    @asaplog_post_dec
     2734    def sub_baseline(self, insitu=None, blinfo=None, bltable=None, overwrite=None):
     2735        """\
     2736        Subtract baseline based on parameters written in the input list.
     2737
     2738        Parameters:
     2739            insitu:        if False a new scantable is returned.
     2740                           Otherwise, the scaling is done in-situ
     2741                           The default is taken from .asaprc (False)
     2742            blinfo:        baseline parameter set stored in a dictionary
     2743                           or a list of dictionary. Each dictionary
     2744                           corresponds to each spectrum and must contain
     2745                           the following keys and values:
     2746                             'row': row number,
     2747                             'blfunc': function name. available ones include
     2748                                       'poly', 'chebyshev', 'cspline' and
     2749                                       'sinusoid',
     2750                             'order': maximum order of polynomial. needed
     2751                                      if blfunc='poly' or 'chebyshev',
     2752                             'npiece': number or piecewise polynomial.
     2753                                       needed if blfunc='cspline',
     2754                             'nwave': a list of sinusoidal wave numbers.
     2755                                      needed if blfunc='sinusoid', and
     2756                             'masklist': min-max windows for channel mask.
     2757                                         the specified ranges will be used
     2758                                         for fitting.
     2759            bltable:       name of output baseline table where baseline
     2760                           parameters and fitting results recorded.
     2761                           default is ''(no output).
     2762            overwrite:     if True, overwrites the existing baseline table
     2763                           specified in bltable.
     2764                           default is False.
     2765                           
     2766        Example:
     2767            sub_baseline(blinfo=[{'row':0, 'blfunc':'poly', 'order':5,
     2768                                  'masklist':[[10,350],[352,510]]},
     2769                                 {'row':1, 'blfunc':'cspline', 'npiece':3,
     2770                                  'masklist':[[3,16],[19,404],[407,511]]}
     2771                                  ])
     2772
     2773                the first spectrum (row=0) will be fitted with polynomial
     2774                of order=5 and the next one (row=1) will be fitted with cubic
     2775                spline consisting of 3 pieces.
     2776        """
     2777
     2778        try:
     2779            varlist = vars()
     2780            if blinfo         is None: blinfo         = []
     2781            if bltable        is None: bltable        = ''
     2782            if overwrite      is None: overwrite      = False
     2783
     2784            if insitu is None: insitu = rcParams['insitu']
     2785            if insitu:
     2786                workscan = self
     2787            else:
     2788                workscan = self.copy()
     2789
     2790            nrow = workscan.nrow()
     2791
     2792            in_blinfo = pack_blinfo(blinfo=blinfo, maxirow=nrow)
     2793
     2794            print "in_blinfo=< "+ str(in_blinfo)+" >"
     2795
     2796            sres = workscan._sub_baseline(in_blinfo,
     2797                                          bltable,
     2798                                          os.path.exists(bltable),
     2799                                          overwrite)
     2800            res = parse_fitresult(sres)
     2801           
     2802            workscan._add_history('sub_baseline', varlist)
     2803
     2804            if insitu:
     2805                self._assign(workscan)
     2806                return res
     2807            else:
     2808                return {'scantable': workscan, 'fitresults': res}
     2809
     2810        except RuntimeError, e:
     2811            raise_fitting_failure_exception(e)
     2812
     2813    @asaplog_post_dec
    25582814    def calc_aic(self, value=None, blfunc=None, order=None, mask=None,
    25592815                 whichrow=None, uselinefinder=None, edge=None,
     
    26382894                          clipniter=None, plot=None,
    26392895                          getresidual=None, showprogress=None,
    2640                           minnrow=None, outlog=None, blfile=None, csvformat=None):
     2896                          minnrow=None, outlog=None,
     2897                          blfile=None, csvformat=None,
     2898                          bltable=None):
    26412899        """\
    26422900        Return a scan which has been baselined (all rows) with sinusoidal
     
    26972955                           (default is '': no file/logger output)
    26982956            csvformat:     if True blfile is csv-formatted, default is False.
     2957            bltable:       name of a baseline table where fitting results
     2958                           (coefficients, rms, etc.) are to be written.
     2959                           if given, fitting results will NOT be output to
     2960                           scantable (insitu=True) or None will be
     2961                           returned (insitu=False).
     2962                           (default is "": no table output)
    26992963
    27002964        Example:
     
    27342998            if blfile        is None: blfile        = ''
    27352999            if csvformat     is None: csvformat     = False
    2736 
    2737             if csvformat:
    2738                 scsvformat = "T"
    2739             else:
    2740                 scsvformat = "F"
     3000            if bltable       is None: bltable       = ''
     3001
     3002            sapplyfft = 'true' if applyfft else 'false'
     3003            fftinfo = ','.join([sapplyfft, fftmethod.lower(), str(fftthresh).lower()])
     3004
     3005            scsvformat = 'T' if csvformat else 'F'
    27413006
    27423007            #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method.
    2743             workscan._sinusoid_baseline(mask, applyfft, fftmethod.lower(),
    2744                                         str(fftthresh).lower(),
     3008            workscan._sinusoid_baseline(mask,
     3009                                        fftinfo,
     3010                                        #applyfft, fftmethod.lower(),
     3011                                        #str(fftthresh).lower(),
    27453012                                        workscan._parse_wn(addwn),
    27463013                                        workscan._parse_wn(rejwn),
     
    27493016                                        pack_progress_params(showprogress,
    27503017                                                             minnrow),
    2751                                         outlog, scsvformat+blfile)
     3018                                        outlog, scsvformat+blfile,
     3019                                        bltable)
    27523020            workscan._add_history('sinusoid_baseline', varlist)
    2753            
    2754             if insitu:
    2755                 self._assign(workscan)
     3021
     3022            if bltable == '':
     3023                if insitu:
     3024                    self._assign(workscan)
     3025                else:
     3026                    return workscan
    27563027            else:
    2757                 return workscan
     3028                if not insitu:
     3029                    return None
    27583030           
    27593031        except RuntimeError, e:
     
    27683040                               chan_avg_limit=None, plot=None,
    27693041                               getresidual=None, showprogress=None,
    2770                                minnrow=None, outlog=None, blfile=None, csvformat=None):
     3042                               minnrow=None, outlog=None,
     3043                               blfile=None, csvformat=None,
     3044                               bltable=None):
    27713045        """\
    27723046        Return a scan which has been baselined (all rows) with sinusoidal
     
    28493123                            (default is "": no file/logger output)
    28503124            csvformat:      if True blfile is csv-formatted, default is False.
     3125            bltable:        name of a baseline table where fitting results
     3126                            (coefficients, rms, etc.) are to be written.
     3127                            if given, fitting results will NOT be output to
     3128                            scantable (insitu=True) or None will be
     3129                            returned (insitu=False).
     3130                            (default is "": no table output)
    28513131
    28523132        Example:
     
    28863166            if blfile         is None: blfile         = ''
    28873167            if csvformat      is None: csvformat      = False
    2888 
    2889             if csvformat:
    2890                 scsvformat = "T"
    2891             else:
    2892                 scsvformat = "F"
     3168            if bltable        is None: bltable        = ''
     3169
     3170            sapplyfft = 'true' if applyfft else 'false'
     3171            fftinfo = ','.join([sapplyfft, fftmethod.lower(), str(fftthresh).lower()])
     3172
     3173            scsvformat = 'T' if csvformat else 'F'
    28933174
    28943175            #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method.
    2895             workscan._auto_sinusoid_baseline(mask, applyfft,
    2896                                              fftmethod.lower(),
    2897                                              str(fftthresh).lower(),
     3176            workscan._auto_sinusoid_baseline(mask,
     3177                                             fftinfo,
    28983178                                             workscan._parse_wn(addwn),
    28993179                                             workscan._parse_wn(rejwn),
     
    29043184                                             pack_progress_params(showprogress,
    29053185                                                                  minnrow),
    2906                                              outlog, scsvformat+blfile)
     3186                                             outlog, scsvformat+blfile, bltable)
    29073187            workscan._add_history("auto_sinusoid_baseline", varlist)
    2908            
    2909             if insitu:
    2910                 self._assign(workscan)
     3188
     3189            if bltable == '':
     3190                if insitu:
     3191                    self._assign(workscan)
     3192                else:
     3193                    return workscan
    29113194            else:
    2912                 return workscan
     3195                if not insitu:
     3196                    return None
    29133197           
    29143198        except RuntimeError, e:
     
    29193203                         clipthresh=None, clipniter=None, plot=None,
    29203204                         getresidual=None, showprogress=None, minnrow=None,
    2921                          outlog=None, blfile=None, csvformat=None):
     3205                         outlog=None, blfile=None, csvformat=None,
     3206                         bltable=None):
    29223207        """\
    29233208        Return a scan which has been baselined (all rows) by cubic spline
     
    29493234                          (default is "": no file/logger output)
    29503235            csvformat:    if True blfile is csv-formatted, default is False.
     3236            bltable:      name of a baseline table where fitting results
     3237                          (coefficients, rms, etc.) are to be written.
     3238                          if given, fitting results will NOT be output to
     3239                          scantable (insitu=True) or None will be
     3240                          returned (insitu=False).
     3241                          (default is "": no table output)
    29513242
    29523243        Example:
     
    29813272            if outlog       is None: outlog       = False
    29823273            if blfile       is None: blfile       = ''
    2983             if csvformat     is None: csvformat     = False
    2984 
    2985             if csvformat:
    2986                 scsvformat = "T"
    2987             else:
    2988                 scsvformat = "F"
     3274            if csvformat    is None: csvformat    = False
     3275            if bltable      is None: bltable      = ''
     3276
     3277            scsvformat = 'T' if csvformat else 'F'
    29893278
    29903279            #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
    2991             workscan._cspline_baseline(mask, npiece, clipthresh, clipniter,
     3280            workscan._cspline_baseline(mask, npiece,
     3281                                       clipthresh, clipniter,
    29923282                                       getresidual,
    29933283                                       pack_progress_params(showprogress,
    29943284                                                            minnrow),
    2995                                        outlog, scsvformat+blfile)
     3285                                       outlog, scsvformat+blfile,
     3286                                       bltable)
    29963287            workscan._add_history("cspline_baseline", varlist)
    2997            
    2998             if insitu:
    2999                 self._assign(workscan)
     3288
     3289            if bltable == '':
     3290                if insitu:
     3291                    self._assign(workscan)
     3292                else:
     3293                    return workscan
    30003294            else:
    3001                 return workscan
     3295                if not insitu:
     3296                    return None
    30023297           
    30033298        except RuntimeError, e:
     
    30103305                              getresidual=None, plot=None,
    30113306                              showprogress=None, minnrow=None, outlog=None,
    3012                               blfile=None, csvformat=None):
     3307                              blfile=None, csvformat=None, bltable=None):
    30133308        """\
    30143309        Return a scan which has been baselined (all rows) by cubic spline
     
    30623357                            (default is "": no file/logger output)
    30633358            csvformat:      if True blfile is csv-formatted, default is False.
     3359            bltable:        name of a baseline table where fitting results
     3360                            (coefficients, rms, etc.) are to be written.
     3361                            if given, fitting results will NOT be output to
     3362                            scantable (insitu=True) or None will be
     3363                            returned (insitu=False).
     3364                            (default is "": no table output)
    30643365
    30653366        Example:
     
    30953396            if blfile         is None: blfile         = ''
    30963397            if csvformat      is None: csvformat      = False
    3097 
    3098             if csvformat:
    3099                 scsvformat = "T"
    3100             else:
    3101                 scsvformat = "F"
     3398            if bltable        is None: bltable        = ''
     3399
     3400            scsvformat = 'T' if csvformat else 'F'
    31023401
    31033402            #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
    3104             workscan._auto_cspline_baseline(mask, npiece, clipthresh,
    3105                                             clipniter,
     3403            workscan._auto_cspline_baseline(mask, npiece,
     3404                                            clipthresh, clipniter,
    31063405                                            normalise_edge_param(edge),
    31073406                                            threshold,
     
    31093408                                            pack_progress_params(showprogress,
    31103409                                                                 minnrow),
    3111                                             outlog, scsvformat+blfile)
     3410                                            outlog,
     3411                                            scsvformat+blfile,
     3412                                            bltable)
    31123413            workscan._add_history("auto_cspline_baseline", varlist)
    3113            
    3114             if insitu:
    3115                 self._assign(workscan)
     3414
     3415            if bltable == '':
     3416                if insitu:
     3417                    self._assign(workscan)
     3418                else:
     3419                    return workscan
    31163420            else:
    3117                 return workscan
     3421                if not insitu:
     3422                    return None
    31183423           
    31193424        except RuntimeError, e:
     
    31243429                           clipthresh=None, clipniter=None, plot=None,
    31253430                           getresidual=None, showprogress=None, minnrow=None,
    3126                            outlog=None, blfile=None, csvformat=None):
     3431                           outlog=None, blfile=None, csvformat=None,
     3432                           bltable=None):
    31273433        """\
    31283434        Return a scan which has been baselined (all rows) by Chebyshev polynomials.
     
    31533459                          (default is "": no file/logger output)
    31543460            csvformat:    if True blfile is csv-formatted, default is False.
     3461            bltable:      name of a baseline table where fitting results
     3462                          (coefficients, rms, etc.) are to be written.
     3463                          if given, fitting results will NOT be output to
     3464                          scantable (insitu=True) or None will be
     3465                          returned (insitu=False).
     3466                          (default is "": no table output)
    31553467
    31563468        Example:
     
    31743486                workscan = self.copy()
    31753487
    3176             #if mask         is None: mask         = [True for i in xrange(workscan.nchan())]
    31773488            if mask         is None: mask         = []
    31783489            if order        is None: order        = 5
     
    31853496            if outlog       is None: outlog       = False
    31863497            if blfile       is None: blfile       = ''
    3187             if csvformat     is None: csvformat     = False
    3188 
    3189             if csvformat:
    3190                 scsvformat = "T"
    3191             else:
    3192                 scsvformat = "F"
     3498            if csvformat    is None: csvformat    = False
     3499            if bltable      is None: bltable      = ''
     3500
     3501            scsvformat = 'T' if csvformat else 'F'
    31933502
    31943503            #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
    3195             workscan._chebyshev_baseline(mask, order, clipthresh, clipniter,
     3504            workscan._chebyshev_baseline(mask, order,
     3505                                         clipthresh, clipniter,
    31963506                                         getresidual,
    31973507                                         pack_progress_params(showprogress,
    31983508                                                              minnrow),
    3199                                          outlog, scsvformat+blfile)
     3509                                         outlog, scsvformat+blfile,
     3510                                         bltable)
    32003511            workscan._add_history("chebyshev_baseline", varlist)
    3201            
    3202             if insitu:
    3203                 self._assign(workscan)
     3512
     3513            if bltable == '':
     3514                if insitu:
     3515                    self._assign(workscan)
     3516                else:
     3517                    return workscan
    32043518            else:
    3205                 return workscan
     3519                if not insitu:
     3520                    return None
    32063521           
    32073522        except RuntimeError, e:
     
    32143529                              getresidual=None, plot=None,
    32153530                              showprogress=None, minnrow=None, outlog=None,
    3216                               blfile=None, csvformat=None):
     3531                              blfile=None, csvformat=None, bltable=None):
    32173532        """\
    32183533        Return a scan which has been baselined (all rows) by Chebyshev polynomials.
     
    32653580                            (default is "": no file/logger output)
    32663581            csvformat:      if True blfile is csv-formatted, default is False.
     3582            bltable:        name of a baseline table where fitting results
     3583                            (coefficients, rms, etc.) are to be written.
     3584                            if given, fitting results will NOT be output to
     3585                            scantable (insitu=True) or None will be
     3586                            returned (insitu=False).
     3587                            (default is "": no table output)
    32673588
    32683589        Example:
     
    32833604                workscan = self.copy()
    32843605           
    3285             #if mask           is None: mask           = [True for i in xrange(workscan.nchan())]
    32863606            if mask           is None: mask           = []
    32873607            if order          is None: order          = 5
     
    32983618            if blfile         is None: blfile         = ''
    32993619            if csvformat      is None: csvformat      = False
    3300 
    3301             if csvformat:
    3302                 scsvformat = "T"
    3303             else:
    3304                 scsvformat = "F"
     3620            if bltable        is None: bltable        = ''
     3621
     3622            scsvformat = 'T' if csvformat else 'F'
    33053623
    33063624            #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method.
    3307             workscan._auto_chebyshev_baseline(mask, order, clipthresh,
    3308                                               clipniter,
     3625            workscan._auto_chebyshev_baseline(mask, order,
     3626                                              clipthresh, clipniter,
    33093627                                              normalise_edge_param(edge),
    33103628                                              threshold,
     
    33123630                                              pack_progress_params(showprogress,
    33133631                                                                   minnrow),
    3314                                               outlog, scsvformat+blfile)
     3632                                              outlog, scsvformat+blfile,
     3633                                              bltable)
    33153634            workscan._add_history("auto_chebyshev_baseline", varlist)
    3316            
    3317             if insitu:
    3318                 self._assign(workscan)
     3635
     3636            if bltable == '':
     3637                if insitu:
     3638                    self._assign(workscan)
     3639                else:
     3640                    return workscan
    33193641            else:
    3320                 return workscan
     3642                if not insitu:
     3643                    return None
    33213644           
    33223645        except RuntimeError, e:
     
    33243647
    33253648    @asaplog_post_dec
    3326     def poly_baseline(self, mask=None, order=None, insitu=None, plot=None,
     3649    def poly_baseline(self, insitu=None, mask=None, order=None,
     3650                      clipthresh=None, clipniter=None, plot=None,
    33273651                      getresidual=None, showprogress=None, minnrow=None,
    3328                       outlog=None, blfile=None, csvformat=None):
     3652                      outlog=None, blfile=None, csvformat=None,
     3653                      bltable=None):
    33293654        """\
    33303655        Return a scan which has been baselined (all rows) by a polynomial.
     
    33353660            mask:         an optional mask
    33363661            order:        the order of the polynomial (default is 0)
     3662            clipthresh:   Clipping threshold. (default is 3.0, unit: sigma)
     3663            clipniter:    maximum number of iteration of 'clipthresh'-sigma
     3664                          clipping (default is 0)
    33373665            plot:         plot the fit and the residual. In this each
    33383666                          indivual fit has to be approved, by typing 'y'
     
    33503678                          (default is "": no file/logger output)
    33513679            csvformat:    if True blfile is csv-formatted, default is False.
     3680            bltable:      name of a baseline table where fitting results
     3681                          (coefficients, rms, etc.) are to be written.
     3682                          if given, fitting results will NOT be output to
     3683                          scantable (insitu=True) or None will be
     3684                          returned (insitu=False).
     3685                          (default is "": no table output)
    33523686
    33533687        Example:
     
    33673701                workscan = self.copy()
    33683702
    3369             #if mask         is None: mask         = [True for i in \
    3370             #                                           xrange(workscan.nchan())]
    33713703            if mask         is None: mask         = []
    33723704            if order        is None: order        = 0
     3705            if clipthresh   is None: clipthresh   = 3.0
     3706            if clipniter    is None: clipniter    = 0
    33733707            if plot         is None: plot         = False
    33743708            if getresidual  is None: getresidual  = True
     
    33763710            if minnrow      is None: minnrow      = 1000
    33773711            if outlog       is None: outlog       = False
    3378             if blfile       is None: blfile       = ""
     3712            if blfile       is None: blfile       = ''
    33793713            if csvformat    is None: csvformat    = False
    3380 
    3381             if csvformat:
    3382                 scsvformat = "T"
    3383             else:
    3384                 scsvformat = "F"
     3714            if bltable      is None: bltable      = ''
     3715
     3716            scsvformat = 'T' if csvformat else 'F'
    33853717
    33863718            if plot:
     
    34463778                    blf.close()
    34473779            else:
    3448                 workscan._poly_baseline(mask, order, getresidual,
     3780                workscan._poly_baseline(mask, order,
     3781                                        clipthresh, clipniter, #
     3782                                        getresidual,
    34493783                                        pack_progress_params(showprogress,
    34503784                                                             minnrow),
    3451                                         outlog, scsvformat+blfile)
     3785                                        outlog, scsvformat+blfile,
     3786                                        bltable)  #
    34523787           
    34533788            workscan._add_history("poly_baseline", varlist)
     
    34623797
    34633798    @asaplog_post_dec
    3464     def auto_poly_baseline(self, mask=None, order=None, edge=None,
    3465                            threshold=None, chan_avg_limit=None,
    3466                            plot=None, insitu=None,
    3467                            getresidual=None, showprogress=None,
    3468                            minnrow=None, outlog=None, blfile=None, csvformat=None):
     3799    def auto_poly_baseline(self, insitu=None, mask=None, order=None,
     3800                           clipthresh=None, clipniter=None,
     3801                           edge=None, threshold=None, chan_avg_limit=None,
     3802                           getresidual=None, plot=None,
     3803                           showprogress=None, minnrow=None, outlog=None,
     3804                           blfile=None, csvformat=None, bltable=None):
    34693805        """\
    34703806        Return a scan which has been baselined (all rows) by a polynomial.
     
    34783814            mask:           an optional mask retreived from scantable
    34793815            order:          the order of the polynomial (default is 0)
     3816            clipthresh:     Clipping threshold. (default is 3.0, unit: sigma)
     3817            clipniter:      maximum number of iteration of 'clipthresh'-sigma
     3818                            clipping (default is 0)
    34803819            edge:           an optional number of channel to drop at
    34813820                            the edge of spectrum. If only one value is
     
    35133852                            (default is "": no file/logger output)
    35143853            csvformat:      if True blfile is csv-formatted, default is False.
     3854            bltable:        name of a baseline table where fitting results
     3855                            (coefficients, rms, etc.) are to be written.
     3856                            if given, fitting results will NOT be output to
     3857                            scantable (insitu=True) or None will be
     3858                            returned (insitu=False).
     3859                            (default is "": no table output)
    35153860
    35163861        Example:
     
    35283873                workscan = self.copy()
    35293874
    3530             #if mask           is None: mask           = [True for i in xrange(workscan.nchan())]
    35313875            if mask           is None: mask           = []
    35323876            if order          is None: order          = 0
     3877            if clipthresh     is None: clipthresh     = 3.0
     3878            if clipniter      is None: clipniter      = 0
    35333879            if edge           is None: edge           = (0, 0)
    35343880            if threshold      is None: threshold      = 3
     
    35413887            if blfile         is None: blfile         = ''
    35423888            if csvformat      is None: csvformat      = False
    3543 
    3544             if csvformat:
    3545                 scsvformat = "T"
    3546             else:
    3547                 scsvformat = "F"
     3889            if bltable        is None: bltable        = ''
     3890
     3891            scsvformat = 'T' if csvformat else 'F'
    35483892
    35493893            edge = normalise_edge_param(edge)
     
    36153959                if outblfile: blf.close()
    36163960            else:
    3617                 workscan._auto_poly_baseline(mask, order, edge, threshold,
     3961                workscan._auto_poly_baseline(mask, order,
     3962                                             clipthresh, clipniter,
     3963                                             edge, threshold,
    36183964                                             chan_avg_limit, getresidual,
    36193965                                             pack_progress_params(showprogress,
    36203966                                                                  minnrow),
    3621                                              outlog, scsvformat+blfile)
    3622 
     3967                                             outlog, scsvformat+blfile,
     3968                                             bltable)
    36233969            workscan._add_history("auto_poly_baseline", varlist)
    3624            
    3625             if insitu:
    3626                 self._assign(workscan)
     3970
     3971            if bltable == '':
     3972                if insitu:
     3973                    self._assign(workscan)
     3974                else:
     3975                    return workscan
    36273976            else:
    3628                 return workscan
     3977                if not insitu:
     3978                    return None
    36293979           
    36303980        except RuntimeError, e:
Note: See TracChangeset for help on using the changeset viewer.