Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapmath.py

    r1827 r1920  
    11from asap.scantable import scantable
    22from asap.parameters import rcParams
    3 from asap.logging import asaplog, print_log, print_log_dec
     3from asap.logging import asaplog, asaplog_post_dec
    44from asap.selector import selector
    55from asap import asaplotgui
    66
    7 @print_log_dec
     7@asaplog_post_dec
    88def average_time(*args, **kwargs):
    99    """
     
    6767        if not isinstance(s,scantable):
    6868            msg = "Please give a list of scantables"
    69             if rcParams['verbose']:
    70                 #print msg
    71                 asaplog.push(msg)
    72                 print_log('ERROR')
    73                 return
    74             else:
    75                 raise TypeError(msg)
     69            raise TypeError(msg)
    7670    if scanav: scanav = "SCAN"
    7771    else: scanav = "NONE"
     
    9690        s = scantable(stm._new_average(alignedlst, compel, mask, weight.upper(), scanav))
    9791    s._add_history("average_time",varlist)
    98     print_log()
     92
    9993    return s
    10094
     95@asaplog_post_dec
    10196def quotient(source, reference, preserve=True):
    10297    """
     
    119114    s = scantable(stm._quotient(source, reference, preserve))
    120115    s._add_history("quotient",varlist)
    121     print_log()
    122116    return s
    123117
    124 @print_log_dec
     118@asaplog_post_dec
    125119def dototalpower(calon, caloff, tcalval=0.0):
    126120    """
     
    138132    s = scantable(stm._dototalpower(calon, caloff, tcalval))
    139133    s._add_history("dototalpower",varlist)
    140     print_log()
    141134    return s
    142135
    143 @print_log_dec
     136@asaplog_post_dec
    144137def dosigref(sig, ref, smooth, tsysval=0.0, tauval=0.0):
    145138    """
     
    159152    s = scantable(stm._dosigref(sig, ref, smooth, tsysval, tauval))
    160153    s._add_history("dosigref",varlist)
    161     print_log()
    162154    return s
    163155
    164 @print_log_dec
     156@asaplog_post_dec
    165157def calps(scantab, scannos, smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False):
    166158    """
    167159    Calibrate GBT position switched data
    168160    Adopted from GBTIDL getps
    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.
     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.
    173165    (GBT MS data reading process to scantable automatically append these
    174166    id names to the source names)
     
    184176        tcalval:       optional user specified Tcal (default is 0.0,
    185177                       use Tcal value in the data)
     178        verify:        Verify calibration if true
    186179    """
    187180    varlist = vars()
     
    190183##     if s is None:
    191184##         msg = "The input data appear to contain no position-switch mode data."
    192 ##         if rcParams['verbose']:
    193 ##             #print msg
    194 ##             asaplog.push(msg)
    195 ##             print_log('ERROR')
    196 ##             return
    197 ##         else:
    198 ##             raise TypeError(msg)
     185##         raise TypeError(msg)
    199186    s = scantab.copy()
    200187    from asap._asap import srctype
     
    205192    except Exception, e:
    206193        msg = "The input data appear to contain no position-switch mode data."
    207         if rcParams['verbose']:
    208             #print msg
    209             asaplog.push(msg)
    210             print_log('ERROR')
    211             return
    212         else:
    213             raise TypeError(msg)
     194        raise TypeError(msg)
    214195    s.set_selection()
    215196    sel.reset()
     
    217198    if ssub is None:
    218199        msg = "No data was found with given scan numbers!"
    219         if rcParams['verbose']:
    220             #print msg
    221             asaplog.push(msg)
    222             print_log('ERROR')
    223             return
    224         else:
    225             raise TypeError(msg)
     200        raise TypeError(msg)
    226201    #ssubon = ssub.get_scan('*calon')
    227202    #ssuboff = ssub.get_scan('*[^calon]')
     
    238213    if ssubon.nrow() != ssuboff.nrow():
    239214        msg = "mismatch in numbers of CAL on/off scans. Cannot calibrate. Check the scan numbers."
    240         if rcParams['verbose']:
    241             #print msg
    242             asaplog.push(msg)
    243             print_log('ERROR')
    244             return
    245         else:
    246             raise TypeError(msg)
     215        raise TypeError(msg)
    247216    cals = dototalpower(ssubon, ssuboff, tcalval)
    248217    #sig = cals.get_scan('*ps')
     
    260229    if sig.nscan() != ref.nscan():
    261230        msg = "mismatch in numbers of on/off scans. Cannot calibrate. Check the scan numbers."
    262         if rcParams['verbose']:
    263             #print msg
    264             asaplog.push(msg)
    265             print_log('ERROR')
    266             return
    267         else:
    268             raise TypeError(msg)
     231        raise TypeError(msg)
    269232
    270233    #for user supplied Tsys
     
    272235        if tauval<=0.0:
    273236            msg = "Need to supply a valid tau to use the supplied Tsys"
    274             if rcParams['verbose']:
    275                 #print msg
    276                 asaplog.push(msg)
    277                 print_log('ERROR')
    278                 return
    279             else:
    280                 raise TypeError(msg)
     237            raise TypeError(msg)
    281238        else:
    282239            sig.recalc_azel()
    283240            ref.recalc_azel()
    284241            #msg = "Use of user specified Tsys is not fully implemented yet."
    285             #if rcParams['verbose']:
    286             #    print msg
    287             #    return
    288             #else:
    289             #    raise TypeError(msg)
     242            #raise TypeError(msg)
    290243            # use get_elevation to get elevation and
    291244            # calculate a scaling factor using the formula
     
    355308        del sel
    356309        # plot
    357         print_log()
     310        asaplog.post()
    358311        asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
    359         print_log('WARN')
     312        asaplog.post('WARN')
    360313        p=asaplotgui.asaplotgui()
    361314        #nr=min(6,len(ifnos)*len(polnos))
     
    384337                btics.append(b)
    385338        else:
    386             print_log()
     339            asaplog.post()
    387340            asaplog.push('Only first 6 [if,pol] pairs are plotted.')
    388             print_log('WARN')
     341            asaplog.post('WARN')
    389342            nr=6
    390343            for i in range(2*nr):
     
    442395    ###
    443396    ress._add_history("calps", varlist)
    444     print_log()
    445397    return ress
    446398
    447 @print_log_dec
     399@asaplog_post_dec
    448400def calnod(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False):
    449401    """
     
    458410        tauval:      optional user specified tau value (not implemented yet)
    459411        tcalval:     optional user specified Tcal value
     412        verify:       Verify calibration if true
    460413    """
    461414    varlist = vars()
     
    469422##     if s is None:
    470423##         msg = "The input data appear to contain no Nod observing mode data."
    471 ##         if rcParams['verbose']:
    472 ##             #print msg
    473 ##             asaplog.push(msg)
    474 ##             print_log('ERROR')
    475 ##             return
    476 ##         else:
    477 ##             raise TypeError(msg)
     424##         raise TypeError(msg)
    478425    s = scantab.copy()
    479426    sel = selector()
     
    483430    except Exception, e:
    484431        msg = "The input data appear to contain no Nod observing mode data."
    485         if rcParams['verbose']:
    486             #print msg
    487             asaplog.push(msg)
    488             print_log('ERROR')
    489             return
    490         else:
    491             raise TypeError(msg)
     432        raise TypeError(msg)
    492433    sel.reset()
    493434    del sel
     
    511452        #if len(scannos)>2:
    512453        #    msg = "calnod can only process a pair of nod scans at time."
    513         #    if rcParams['verbose']:
    514         #        print msg
    515         #        return
    516         #    else:
    517         #        raise TypeError(msg)
     454        #    raise TypeError(msg)
    518455        #
    519456        #if len(scannos)==2:
     
    525462        if tauval<=0.0:
    526463            msg = "Need to supply a valid tau to use the supplied Tsys"
    527             if rcParams['verbose']:
    528                 #print msg
    529                 asaplog.push(msg)
    530                 print_log('ERROR')
    531                 return
    532             else:
    533                 raise TypeError(msg)
     464            raise TypeError(msg)
    534465        else:
    535466            scantab.recalc_azel()
     
    596527        del sel
    597528        # plot
    598         print_log()
     529        asaplog.post()
    599530        asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
    600         print_log('WARN')
     531        asaplog.post('WARN')
    601532        p=asaplotgui.asaplotgui()
    602533        #nr=min(6,len(ifnos)*len(polnos))
     
    625556                btics.append(b)
    626557        else:
    627             print_log()
     558            asaplog.post()
    628559            asaplog.push('Only first 6 [if,pol] pairs are plotted.')
    629             print_log('WARN')
     560            asaplog.post('WARN')
    630561            nr=6
    631562            for i in range(2*nr):
     
    683614    ###
    684615    resspec._add_history("calnod",varlist)
    685     print_log()
    686616    return resspec
    687617
    688 @print_log_dec
     618@asaplog_post_dec
    689619def calfs(scantab, scannos=[], smooth=1, tsysval=0.0, tauval=0.0, tcalval=0.0, verify=False):
    690620    """
    691621    Calibrate GBT frequency switched data.
    692622    Adopted from GBTIDL getfs.
    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.
     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.
    697627    (GBT MS data reading via scantable automatically append these
    698628    id names to the source names)
     
    706636                       use Tsys in the data)
    707637        tauval:        optional user specified Tau
     638        verify:        Verify calibration if true
    708639    """
    709640    varlist = vars()
     
    716647#    if check is None:
    717648#        msg = "The input data appear to contain no Nod observing mode data."
    718 #        if rcParams['verbose']:
    719 #            print msg
    720 #            return
    721 #        else:
    722 #            raise TypeError(msg)
     649#        raise TypeError(msg)
    723650    s = scantab.get_scan(scannos)
    724651    del scantab
     
    812739        del sel
    813740        # plot
    814         print_log()
     741        asaplog.post()
    815742        asaplog.push('Plot only first spectrum for each [if,pol] pairs to verify calibration.')
    816         print_log('WARN')
     743        asaplog.post('WARN')
    817744        p=asaplotgui.asaplotgui()
    818745        #nr=min(6,len(ifnos)*len(polnos))
     
    821748        btics=[]
    822749        if nr>3:
    823             print_log()
     750            asaplog.post()
    824751            asaplog.push('Only first 3 [if,pol] pairs are plotted.')
    825             print_log('WARN')
     752            asaplog.post('WARN')
    826753            nr=3
    827754        p.set_panels(rows=nr,cols=3,nplots=3*nr,ganged=False)
     
    891818    ###
    892819    resspec._add_history("calfs",varlist)
    893     print_log()
    894820    return resspec
    895821
    896 @print_log_dec
     822@asaplog_post_dec
    897823def merge(*args):
    898824    """
     
    921847        if not isinstance(s,scantable):
    922848            msg = "Please give a list of scantables"
    923             if rcParams['verbose']:
    924                 #print msg
    925                 asaplog.push(msg)
    926                 print_log('ERROR')
    927                 return
    928             else:
    929                 raise TypeError(msg)
     849            raise TypeError(msg)
    930850    s = scantable(stm._merge(lst))
    931851    s._add_history("merge", varlist)
    932     print_log()
    933852    return s
    934853
     854@asaplog_post_dec
    935855def calibrate( scantab, scannos=[], calmode='none', verify=None ):
    936856    """
     
    946866    if ( calmode == 'nod' ):
    947867        asaplog.push( 'Calibrating nod data.' )
    948         print_log()
    949868        scal = calnod( scantab, scannos=scannos, verify=verify )
    950869    elif ( calmode == 'quotient' ):
    951870        asaplog.push( 'Calibrating using quotient.' )
    952         print_log()
    953871        scal = scantab.auto_quotient( verify=verify )
    954872    elif ( calmode == 'ps' ):
    955873        asaplog.push( 'Calibrating %s position-switched data.' % antname )
    956         print_log()
    957874        if ( antname.find( 'APEX' ) != -1 ):
    958875            scal = apexcal( scantab, scannos, calmode, verify )
     
    963880    elif ( calmode == 'fs' or calmode == 'fsotf' ):
    964881        asaplog.push( 'Calibrating %s frequency-switched data.' % antname )
    965         print_log()
    966882        if ( antname.find( 'APEX' ) != -1 ):
    967883            scal = apexcal( scantab, scannos, calmode, verify )
     
    972888    elif ( calmode == 'otf' ):
    973889        asaplog.push( 'Calibrating %s On-The-Fly data.' % antname )
    974         print_log()
    975890        scal = almacal( scantab, scannos, calmode, verify )
    976891    else:
     
    1015930    return scal
    1016931
     932@asaplog_post_dec
    1017933def splitant(filename, outprefix='',overwrite=False):
    1018934    """
     
    1032948    """
    1033949    # Import the table toolkit from CASA
    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
     950    import casac
     951    from asap.scantable import is_ms
     952    tbtool = casac.homefinder.find_home_by_name('tableHome')
     953    tb = tbtool.create()
    1056954    # Check the input filename
    1057955    if isinstance(filename, str):
     
    1061959        if not os.path.exists(filename):
    1062960            s = "File '%s' not found." % (filename)
    1063             if rcParams['verbose']:
    1064                 print_log()
    1065                 asaplog.push(s)
    1066                 print_log('ERROR')
    1067                 return
    1068961            raise IOError(s)
    1069962        # check if input file is MS
    1070         if not os.path.isdir(filename) \
    1071                or not os.path.exists(filename+'/ANTENNA') \
    1072                or not os.path.exists(filename+'/table.f1'):
     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):
    1073967            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
    1079968            raise IOError(s)
    1080969    else:
    1081970        s = "The filename should be string. "
    1082         if rcParams['verbose']:
    1083             print_log()
    1084             asaplog.push(s)
    1085             print_log('ERROR')
    1086             return
    1087971        raise TypeError(s)
    1088972    # Check out put file name
     
    1096980    nant=tb.nrows()
    1097981    antnames=tb.getcol('NAME',0,nant,1)
    1098     antpos=tb.getcol('POSITION',0,nant,1).transpose()
    1099982    tb.close()
    1100983    tb.open(tablename=filename,nomodify=True)
    1101984    ant1=tb.getcol('ANTENNA1',0,-1,1)
    1102985    tb.close()
     986    tmpname='asapmath.splitant.tmp'
    1103987    for antid in set(ant1):
    1104         scan=scantable(filename,average=False,getpt=True,antenna=int(antid))
     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))
    1105991        outname=prefix+antnames[antid]+'.asap'
    1106992        scan.save(outname,format='ASAP',overwrite=overwrite)
     993        tbsel.close()
     994        tb.close()
     995        del tbsel
    1107996        del scan
    1108997        outfiles.append(outname)
    1109     del tb, tb2
     998        os.system('rm -rf '+tmpname)
     999    del tb
    11101000    return outfiles
    11111001
     1002@asaplog_post_dec
    11121003def _array2dOp( scan, value, mode="ADD", tsys=False ):
    11131004    """
     
    11281019        del stm
    11291020    elif len( value ) != nrow:
    1130         asaplog.push( 'len(value) must be 1 or conform to scan.nrow()' )
    1131         print_log( 'ERROR' )
     1021        raise ValueError( 'len(value) must be 1 or conform to scan.nrow()' )
    11321022    else:
    11331023        from asap._asap import stmath
Note: See TracChangeset for help on using the changeset viewer.