Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapmath.py

    r1920 r1827  
    11from asap.scantable import scantable
    22from asap.parameters import rcParams
    3 from asap.logging import asaplog, asaplog_post_dec
     3from asap.logging import asaplog, print_log, print_log_dec
    44from asap.selector import selector
    55from asap import asaplotgui
    66
    7 @asaplog_post_dec
     7@print_log_dec
    88def average_time(*args, **kwargs):
    99    """
     
    6767        if not isinstance(s,scantable):
    6868            msg = "Please give a list of scantables"
    69             raise TypeError(msg)
     69            if rcParams['verbose']:
     70                #print msg
     71                asaplog.push(msg)
     72                print_log('ERROR')
     73                return
     74            else:
     75                raise TypeError(msg)
    7076    if scanav: scanav = "SCAN"
    7177    else: scanav = "NONE"
     
    9096        s = scantable(stm._new_average(alignedlst, compel, mask, weight.upper(), scanav))
    9197    s._add_history("average_time",varlist)
    92 
     98    print_log()
    9399    return s
    94100
    95 @asaplog_post_dec
    96101def quotient(source, reference, preserve=True):
    97102    """
     
    114119    s = scantable(stm._quotient(source, reference, preserve))
    115120    s._add_history("quotient",varlist)
     121    print_log()
    116122    return s
    117123
    118 @asaplog_post_dec
     124@print_log_dec
    119125def dototalpower(calon, caloff, tcalval=0.0):
    120126    """
     
    132138    s = scantable(stm._dototalpower(calon, caloff, tcalval))
    133139    s._add_history("dototalpower",varlist)
     140    print_log()
    134141    return s
    135142
    136 @asaplog_post_dec
     143@print_log_dec
    137144def dosigref(sig, ref, smooth, tsysval=0.0, tauval=0.0):
    138145    """
     
    152159    s = scantable(stm._dosigref(sig, ref, smooth, tsysval, tauval))
    153160    s._add_history("dosigref",varlist)
     161    print_log()
    154162    return s
    155163
    156 @asaplog_post_dec
     164@print_log_dec
    157165def calps(scantab, scannos, smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False):
    158166    """
    159167    Calibrate GBT position switched data
    160168    Adopted from GBTIDL getps
    161     Currently calps identify the scans as position switched data if source
    162     type enum is pson or psoff. The data must contains 'CAL' signal
    163     on/off in each integration. To identify 'CAL' on state, the source type
    164     enum of poncal and poffcal need to be present in the source name field.
     169    Currently calps identify the scans as position switched data if they
     170    contain '_ps' in the source name. The data must contains 'CAL' signal
     171    on/off in each integration. To identify 'CAL' on state, the word, 'calon'
     172    need to be present in the source name field.
    165173    (GBT MS data reading process to scantable automatically append these
    166174    id names to the source names)
     
    176184        tcalval:       optional user specified Tcal (default is 0.0,
    177185                       use Tcal value in the data)
    178         verify:        Verify calibration if true
    179186    """
    180187    varlist = vars()
     
    183190##     if s is None:
    184191##         msg = "The input data appear to contain no position-switch mode data."
    185 ##         raise TypeError(msg)
     192##         if rcParams['verbose']:
     193##             #print msg
     194##             asaplog.push(msg)
     195##             print_log('ERROR')
     196##             return
     197##         else:
     198##             raise TypeError(msg)
    186199    s = scantab.copy()
    187200    from asap._asap import srctype
     
    192205    except Exception, e:
    193206        msg = "The input data appear to contain no position-switch mode data."
    194         raise TypeError(msg)
     207        if rcParams['verbose']:
     208            #print msg
     209            asaplog.push(msg)
     210            print_log('ERROR')
     211            return
     212        else:
     213            raise TypeError(msg)
    195214    s.set_selection()
    196215    sel.reset()
     
    198217    if ssub is None:
    199218        msg = "No data was found with given scan numbers!"
    200         raise TypeError(msg)
     219        if rcParams['verbose']:
     220            #print msg
     221            asaplog.push(msg)
     222            print_log('ERROR')
     223            return
     224        else:
     225            raise TypeError(msg)
    201226    #ssubon = ssub.get_scan('*calon')
    202227    #ssuboff = ssub.get_scan('*[^calon]')
     
    213238    if ssubon.nrow() != ssuboff.nrow():
    214239        msg = "mismatch in numbers of CAL on/off scans. Cannot calibrate. Check the scan numbers."
    215         raise TypeError(msg)
     240        if rcParams['verbose']:
     241            #print msg
     242            asaplog.push(msg)
     243            print_log('ERROR')
     244            return
     245        else:
     246            raise TypeError(msg)
    216247    cals = dototalpower(ssubon, ssuboff, tcalval)
    217248    #sig = cals.get_scan('*ps')
     
    229260    if sig.nscan() != ref.nscan():
    230261        msg = "mismatch in numbers of on/off scans. Cannot calibrate. Check the scan numbers."
    231         raise TypeError(msg)
     262        if rcParams['verbose']:
     263            #print msg
     264            asaplog.push(msg)
     265            print_log('ERROR')
     266            return
     267        else:
     268            raise TypeError(msg)
    232269
    233270    #for user supplied Tsys
     
    235272        if tauval<=0.0:
    236273            msg = "Need to supply a valid tau to use the supplied Tsys"
    237             raise TypeError(msg)
     274            if rcParams['verbose']:
     275                #print msg
     276                asaplog.push(msg)
     277                print_log('ERROR')
     278                return
     279            else:
     280                raise TypeError(msg)
    238281        else:
    239282            sig.recalc_azel()
    240283            ref.recalc_azel()
    241284            #msg = "Use of user specified Tsys is not fully implemented yet."
    242             #raise TypeError(msg)
     285            #if rcParams['verbose']:
     286            #    print msg
     287            #    return
     288            #else:
     289            #    raise TypeError(msg)
    243290            # use get_elevation to get elevation and
    244291            # calculate a scaling factor using the formula
     
    308355        del sel
    309356        # plot
    310         asaplog.post()
     357        print_log()
    311358        asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
    312         asaplog.post('WARN')
     359        print_log('WARN')
    313360        p=asaplotgui.asaplotgui()
    314361        #nr=min(6,len(ifnos)*len(polnos))
     
    337384                btics.append(b)
    338385        else:
    339             asaplog.post()
     386            print_log()
    340387            asaplog.push('Only first 6 [if,pol] pairs are plotted.')
    341             asaplog.post('WARN')
     388            print_log('WARN')
    342389            nr=6
    343390            for i in range(2*nr):
     
    395442    ###
    396443    ress._add_history("calps", varlist)
     444    print_log()
    397445    return ress
    398446
    399 @asaplog_post_dec
     447@print_log_dec
    400448def calnod(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False):
    401449    """
     
    410458        tauval:      optional user specified tau value (not implemented yet)
    411459        tcalval:     optional user specified Tcal value
    412         verify:       Verify calibration if true
    413460    """
    414461    varlist = vars()
     
    422469##     if s is None:
    423470##         msg = "The input data appear to contain no Nod observing mode data."
    424 ##         raise TypeError(msg)
     471##         if rcParams['verbose']:
     472##             #print msg
     473##             asaplog.push(msg)
     474##             print_log('ERROR')
     475##             return
     476##         else:
     477##             raise TypeError(msg)
    425478    s = scantab.copy()
    426479    sel = selector()
     
    430483    except Exception, e:
    431484        msg = "The input data appear to contain no Nod observing mode data."
    432         raise TypeError(msg)
     485        if rcParams['verbose']:
     486            #print msg
     487            asaplog.push(msg)
     488            print_log('ERROR')
     489            return
     490        else:
     491            raise TypeError(msg)
    433492    sel.reset()
    434493    del sel
     
    452511        #if len(scannos)>2:
    453512        #    msg = "calnod can only process a pair of nod scans at time."
    454         #    raise TypeError(msg)
     513        #    if rcParams['verbose']:
     514        #        print msg
     515        #        return
     516        #    else:
     517        #        raise TypeError(msg)
    455518        #
    456519        #if len(scannos)==2:
     
    462525        if tauval<=0.0:
    463526            msg = "Need to supply a valid tau to use the supplied Tsys"
    464             raise TypeError(msg)
     527            if rcParams['verbose']:
     528                #print msg
     529                asaplog.push(msg)
     530                print_log('ERROR')
     531                return
     532            else:
     533                raise TypeError(msg)
    465534        else:
    466535            scantab.recalc_azel()
     
    527596        del sel
    528597        # plot
    529         asaplog.post()
     598        print_log()
    530599        asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
    531         asaplog.post('WARN')
     600        print_log('WARN')
    532601        p=asaplotgui.asaplotgui()
    533602        #nr=min(6,len(ifnos)*len(polnos))
     
    556625                btics.append(b)
    557626        else:
    558             asaplog.post()
     627            print_log()
    559628            asaplog.push('Only first 6 [if,pol] pairs are plotted.')
    560             asaplog.post('WARN')
     629            print_log('WARN')
    561630            nr=6
    562631            for i in range(2*nr):
     
    614683    ###
    615684    resspec._add_history("calnod",varlist)
     685    print_log()
    616686    return resspec
    617687
    618 @asaplog_post_dec
     688@print_log_dec
    619689def calfs(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False):
    620690    """
    621691    Calibrate GBT frequency switched data.
    622692    Adopted from GBTIDL getfs.
    623     Currently calfs identify the scans as frequency switched data if source
    624     type enum is fson and fsoff. The data must contains 'CAL' signal
    625     on/off in each integration. To identify 'CAL' on state, the source type
    626     enum of foncal and foffcal need to be present in the source name field.
     693    Currently calfs identify the scans as frequency switched data if they
     694    contain '_fs' in the source name. The data must contains 'CAL' signal
     695    on/off in each integration. To identify 'CAL' on state, the word, 'calon'
     696    need to be present in the source name field.
    627697    (GBT MS data reading via scantable automatically append these
    628698    id names to the source names)
     
    636706                       use Tsys in the data)
    637707        tauval:        optional user specified Tau
    638         verify:        Verify calibration if true
    639708    """
    640709    varlist = vars()
     
    647716#    if check is None:
    648717#        msg = "The input data appear to contain no Nod observing mode data."
    649 #        raise TypeError(msg)
     718#        if rcParams['verbose']:
     719#            print msg
     720#            return
     721#        else:
     722#            raise TypeError(msg)
    650723    s = scantab.get_scan(scannos)
    651724    del scantab
     
    739812        del sel
    740813        # plot
    741         asaplog.post()
     814        print_log()
    742815        asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
    743         asaplog.post('WARN')
     816        print_log('WARN')
    744817        p=asaplotgui.asaplotgui()
    745818        #nr=min(6,len(ifnos)*len(polnos))
     
    748821        btics=[]
    749822        if nr>3:
    750             asaplog.post()
     823            print_log()
    751824            asaplog.push('Only first 3 [if,pol] pairs are plotted.')
    752             asaplog.post('WARN')
     825            print_log('WARN')
    753826            nr=3
    754827        p.set_panels(rows=nr,cols=3,nplots=3*nr,ganged=False)
     
    818891    ###
    819892    resspec._add_history("calfs",varlist)
     893    print_log()
    820894    return resspec
    821895
    822 @asaplog_post_dec
     896@print_log_dec
    823897def merge(*args):
    824898    """
     
    847921        if not isinstance(s,scantable):
    848922            msg = "Please give a list of scantables"
    849             raise TypeError(msg)
     923            if rcParams['verbose']:
     924                #print msg
     925                asaplog.push(msg)
     926                print_log('ERROR')
     927                return
     928            else:
     929                raise TypeError(msg)
    850930    s = scantable(stm._merge(lst))
    851931    s._add_history("merge", varlist)
     932    print_log()
    852933    return s
    853934
    854 @asaplog_post_dec
    855935def calibrate( scantab, scannos=[], calmode='none', verify=None ):
    856936    """
     
    866946    if ( calmode == 'nod' ):
    867947        asaplog.push( 'Calibrating nod data.' )
     948        print_log()
    868949        scal = calnod( scantab, scannos=scannos, verify=verify )
    869950    elif ( calmode == 'quotient' ):
    870951        asaplog.push( 'Calibrating using quotient.' )
     952        print_log()
    871953        scal = scantab.auto_quotient( verify=verify )
    872954    elif ( calmode == 'ps' ):
    873955        asaplog.push( 'Calibrating %s position-switched data.' % antname )
     956        print_log()
    874957        if ( antname.find( 'APEX' ) != -1 ):
    875958            scal = apexcal( scantab, scannos, calmode, verify )
     
    880963    elif ( calmode == 'fs' or calmode == 'fsotf' ):
    881964        asaplog.push( 'Calibrating %s frequency-switched data.' % antname )
     965        print_log()
    882966        if ( antname.find( 'APEX' ) != -1 ):
    883967            scal = apexcal( scantab, scannos, calmode, verify )
     
    888972    elif ( calmode == 'otf' ):
    889973        asaplog.push( 'Calibrating %s On-The-Fly data.' % antname )
     974        print_log()
    890975        scal = almacal( scantab, scannos, calmode, verify )
    891976    else:
     
    9301015    return scal
    9311016
    932 @asaplog_post_dec
    9331017def splitant(filename, outprefix='',overwrite=False):
    9341018    """
     
    9481032    """
    9491033    # Import the table toolkit from CASA
    950     import casac
    951     from asap.scantable import is_ms
    952     tbtool = casac.homefinder.find_home_by_name('tableHome')
    953     tb = tbtool.create()
     1034    try:
     1035       import casac
     1036    except ImportError:
     1037       if rcParams['verbose']:
     1038           #print "failed to load casa"
     1039           print_log()
     1040           asaplog.push("failed to load casa")
     1041           print_log('ERROR')
     1042       else: raise
     1043       return False
     1044    try:
     1045       tbtool = casac.homefinder.find_home_by_name('tableHome')
     1046       tb = tbtool.create()
     1047       tb2 = tbtool.create()
     1048    except:
     1049       if rcParams['verbose']:
     1050           #print "failed to load a table tool:\n", e
     1051           print_log()
     1052           asaplog.push("failed to load table tool")
     1053           print_log('ERROR')
     1054       else: raise
     1055       return False
    9541056    # Check the input filename
    9551057    if isinstance(filename, str):
     
    9591061        if not os.path.exists(filename):
    9601062            s = "File '%s' not found." % (filename)
     1063            if rcParams['verbose']:
     1064                print_log()
     1065                asaplog.push(s)
     1066                print_log('ERROR')
     1067                return
    9611068            raise IOError(s)
    9621069        # check if input file is MS
    963         #if not os.path.isdir(filename) \
    964         #       or not os.path.exists(filename+'/ANTENNA') \
    965         #       or not os.path.exists(filename+'/table.f1'):
    966         if not is_ms(filename):
     1070        if not os.path.isdir(filename) \
     1071               or not os.path.exists(filename+'/ANTENNA') \
     1072               or not os.path.exists(filename+'/table.f1'):
    9671073            s = "File '%s' is not a Measurement set." % (filename)
     1074            if rcParams['verbose']:
     1075                print_log()
     1076                asaplog.push(s)
     1077                print_log('ERROR')
     1078                return
    9681079            raise IOError(s)
    9691080    else:
    9701081        s = "The filename should be string. "
     1082        if rcParams['verbose']:
     1083            print_log()
     1084            asaplog.push(s)
     1085            print_log('ERROR')
     1086            return
    9711087        raise TypeError(s)
    9721088    # Check out put file name
     
    9801096    nant=tb.nrows()
    9811097    antnames=tb.getcol('NAME',0,nant,1)
     1098    antpos=tb.getcol('POSITION',0,nant,1).transpose()
    9821099    tb.close()
    9831100    tb.open(tablename=filename,nomodify=True)
    9841101    ant1=tb.getcol('ANTENNA1',0,-1,1)
    9851102    tb.close()
    986     tmpname='asapmath.splitant.tmp'
    9871103    for antid in set(ant1):
    988         tb.open(tablename=filename,nomodify=True)
    989         tbsel=tb.query('ANTENNA1 == %s && ANTENNA2 == %s'%(antid,antid),tmpname)
    990         scan=scantable(tmpname,average=False,getpt=True,antenna=int(antid))
     1104        scan=scantable(filename,average=False,getpt=True,antenna=int(antid))
    9911105        outname=prefix+antnames[antid]+'.asap'
    9921106        scan.save(outname,format='ASAP',overwrite=overwrite)
    993         tbsel.close()
    994         tb.close()
    995         del tbsel
    9961107        del scan
    9971108        outfiles.append(outname)
    998         os.system('rm -rf '+tmpname)
    999     del tb
     1109    del tb, tb2
    10001110    return outfiles
    10011111
    1002 @asaplog_post_dec
    10031112def _array2dOp( scan, value, mode="ADD", tsys=False ):
    10041113    """
     
    10191128        del stm
    10201129    elif len( value ) != nrow:
    1021         raise ValueError( 'len(value) must be 1 or conform to scan.nrow()' )
     1130        asaplog.push( 'len(value) must be 1 or conform to scan.nrow()' )
     1131        print_log( 'ERROR' )
    10221132    else:
    10231133        from asap._asap import stmath
Note: See TracChangeset for help on using the changeset viewer.