Esempio n. 1
0
    def cat(self, disk, userno):
        _userno = OSystem.PGetAIPSuser()
        OSystem.PSetAIPSuser(userno)

        try:
            num_slots = AIPSDir.PNumber(disk, userno, self.err)
        except OErr.OErr as err:
            OErr.PClear(err)
            return []

        catalog = []
        for cno in range(1, num_slots):
            entry = AIPSDir.PInfo(disk, userno, cno, self.err)
            if entry:
                dict = {}
                dict['cno'] = cno
                dict['name'] = entry[0:12].strip()
                dict['klass'] = entry[13:19].strip()
                dict['seq'] = int(entry[20:25])
                dict['type'] = entry[26:28]
                dict['date'] = entry[29:40]
                dict['time'] = entry[41:49]
                catalog.append(dict)
                pass
            continue
        OSystem.PSetAIPSuser(_userno)
        return catalog
Esempio n. 2
0
def newPACNO(disk, cno, exists, err, verbose=True):
    """
    Create and initialize an AIPS based Image structure
    
    Create, set initial access information (full image, plane at a time)
    and if exists verifies the file.
    Returns the Python Image object
    isOK member set to indicate success

    * disk     = AIPS directory number
    * cno      = AIPS catalog number
    * exists   = if true then the file is opened and closed to verify
    * err      = Python Obit Error/message stack
    * verbose  = If true any give error messages, else suppress
    """
    ################################################################
    out = ImageMF("AIPS Image")
    user = OSystem.PGetAIPSuser()
    out.isOK = True  # until proven otherwise
    # print "disk, aseq", disk, seq
    # Does it really previously exist?
    test = AIPSDir.PInfo(disk, user, cno, err)
    out.exist = test != None

    if exists:
        Obit.ImageMFSetAIPS(out.me, 2, disk, cno, user, blc, trc, err.me)
        Obit.ImagefullInstantiate(out.cast("ObitImage"), 1, err.me)
    else:
        Obit.ImageMFSetAIPS(out.me, 2, disk, cno, user, blc, trc, err.me)

    # show any errors if wanted
    if verbose and err.isErr:
        out.isOK = False
        OErr.printErrMsg(err, "Error finding AIPS catalog entry")
    elif err.isErr:
        out.isOK = False
        OErr.PClear(err)  # Clear unwanted messages

    # It work?
    if not out.isOK:
        return out

    # Add File info
    out.FileType = 'AIPS'
    out.Disk = disk
    out.Acno = cno
    # Lookup name etc
    s = AIPSDir.PInfo(disk, user, cno, err)
    # parse returned string
    Aname = s[0:12]
    Aclass = s[13:19]
    Aseq = int(s[20:25])
    Atype = s[26:28]
    out.Aname = Aname
    out.Aclass = Aclass
    out.Aseq = Aseq
    out.Otype = "Image"
    return out  # seems OK
Esempio n. 3
0
def EVLAPolnCleanup(uv, Sources, err, \
                    logfile = "",check=False, debug = False):
    """
    Zap uv data and any calibration images in AIPS

    Returns 0 on success otherwise failure

    * uv       = UV data with calibration and editing
    * Sources  = List of Source Models as created by EVLACal.EVLACalModel
    * err      = Obit error/message stack
    * logfile  = Log file for task
    * check    = Only check script
    * debug    = Run tasks debug, show input
    """
    ################################################################
    mess = "Cleanup, Zap AIPS files, check = "+str(check)
    printMess(mess, logfile)

    # Zap data
    if UV.PIsA(uv):
        mess = "Zap uv data"
        printMess(mess, logfile)
        if not check:
            uv.Zap(err) # cleanup
            if err.isErr:
                mess = "Error Zapping uv file"
                printMess(mess, logfile)
                return 1
    
    # Zap calibration images
    stokes = ["I","Q","U"]
    for sou in Sources:
        # Ignore FITS images
        if sou["CalDataType"]=="FITS":
            continue
        for s in stokes:
            klass = s+sou["CalClass"][1:]
            # Test if file exists
            cno = AIPSDir.PTestCNO(sou["CalDisk"], AIPS.userno, \
                                   sou["CalName"][0:12], klass, "MA", sou["CalSeq"], err)
            if cno>0:
                img = Image.newPAImage("img", sou["CalName"][0:12], klass, \
                                       sou["CalDisk"], sou["CalSeq"], True, err)
                if err.isErr:
                    mess = "Error finding "+s+" image for "+sou["Source"]
                    printMess(mess, logfile)
                    return 1
                if Image.PIsA(img):
                    mess = "Zap "+sou["Source"].strip()+" "+s+"Pol"
                    printMess(mess, logfile)
                    if not check:
                        img.Zap(err) # cleanup
                        del img
                    if err.isErr:
                        mess = "Error Zapping image"
                        printMess(mess, logfile)
                        return 1
            # end if exist
    # end source/stokes loop
    return 0
Esempio n. 4
0
def getname(cno, disk=Adisk):
    """
    Return Obit object for AIPS file in cno on disk

    * cno       = AIPS catalog slot number 
    * disk      = AIPS disk number
    """
    ################################################################
    Adisk = disk
    user = AIPS.AIPS.userno
    s = AIPSDir.PInfo(disk, user, cno, err)
    OErr.printErrMsg(err, "Error with AIPS catalog")
    # parse returned string
    Aname = s[0:12]
    Aclass = s[13:19]
    Aseq = int(s[20:25])
    Atype = s[26:28]
    if Atype == 'MA':
        out = Image.newPAImage("AIPS image", Aname, Aclass, disk, Aseq, True,
                               err)
        print("AIPS Image", Aname, Aclass, disk, Aseq)
    elif Atype == 'UV':
        out = UV.newPAUV("AIPS UV data", Aname, Aclass, disk, Aseq, True, err)
        print("AIPS UV", Aname, Aclass, disk, Aseq)
    out.Aname = Aname
    out.Aclass = Aclass
    out.Aseq = Aseq
    out.Atype = Atype
    out.Disk = disk
    out.Acno = cno
    return out
Esempio n. 5
0
def AUcat(disk=Adisk, first=1, last=1000):
    """
    Catalog listing of AIPS UV data files on disk disk

    * disk      = AIPS disk number to list
    * first     = lowest slot number to list
    * last      = highest slot number to list
    """
    ################################################################
    Adisk = disk
    AIPSDir.PListDir(disk, err, type=AIPSDir.UVType, first=first, last=last)
    OErr.printErrMsg(err, "Error with AIPS catalog")
Esempio n. 6
0
 def exists(self):
     # Do I exist?
     exists = False
     if self.uv.FileType == "FITS":
         exists = FITSDir.PExist(self.uv.FileName, self.uv.Disk, self._err)
     elif self.uv.FileType == "AIPS":
         user = OSystem.PGetAIPSuser()
         test = AIPSDir.PTestCNO(self.uv.Disk, user, self.uv.Aname,
                                 self.uv.Aclass, "UV", self.uv.Aseq,
                                 self._err)
         exists = test > 0
     return exists
Esempio n. 7
0
    def cat(self, disk, userno, url, AIPSdirs):
        # Init Obit if needed
        if not OSystem.PIsInit():
            popsno = 1
            if not self.err:
                self.err=OErr.OErr()
            ObitSys=OSystem.OSystem ("", popsno, userno, \
                                     len(AIPSdirs), AIPSdirs, \
                                     0, [], True, False, self.err)
            OErr.printErrMsg(self.err, "Error with Obit startup")
            doInit = True
        else:
            doInit = False
        _userno = OSystem.PGetAIPSuser()
        OSystem.PSetAIPSuser(userno)

        try:
            num_slots = AIPSDir.PNumber(disk, userno, self.err)
        except OErr.OErr as err:
            OErr.PClear(err)
            if doInit:   # Initialized Obit?
                OSystem.Shutdown(ObitSys)
                self.doInit = False
            return []

        catalog = []
        for slot in range(1, num_slots):
            entry = AIPSDir.PInfo(disk, userno, slot, self.err)
            if entry:
                catalog.append((slot, entry))
                pass
            continue
        # Restore Obit to initial state
        OSystem.PSetAIPSuser(_userno)
        if doInit:   # Initialized Obit?
            OSystem.Shutdown(ObitSys)
            self.doInit = False
        OErr.printErrMsg(self.err, "Error with AIPS Catalog")
        return catalog
Esempio n. 8
0
def Acat(disk=Adisk, first=1, last=1000):
    """
    Catalog listing of AIPS files on disk disk
    
    The class remembers the last disk accessed

    * disk      = AIPS disk number to list
    * first     = lowest slot number to list
    * last      =highest slot number to list
    """
    ################################################################
    Adisk = disk
    AIPSDir.PListDir(disk, err, first=first, last=last)
    OErr.printErrMsg(err, "Error with AIPS catalog")
Esempio n. 9
0
def AllDest(err,
            disk=None,
            Atype="  ",
            Aname="            ",
            Aclass="      ",
            Aseq=0):
    """ 
    Delete AIPS files matching a pattern. Strings use AIPS wild cards:
    
         * blank => any
         * '?'   => one of any character
         * "*"   => arbitrary string
         
    * disk      = AIPS disk number, 0=>all
    * Atype     = AIPS entry type, 'MA' or 'UV'; '  => all
    * Aname     = desired AIPS name, using AIPS wildcards, None -> don't check
    * Aclass    = desired AIPS class, using AIPS wildcards, None -> don't check
    * Aseq      = desired AIPS sequence, 0=> any
    """
    ################################################################
    if err.isErr:  # Ignore if error condition
        return
    global Adisk
    if disk == None:
        disk = Adisk
    else:
        if disk > 0:
            Adisk = disk
    # NO Confirm
    #prompt = "Do you really want to delete all AIPS "+Atype+" files on disk(s) "\
    #         +str(disk)+"\nwith names matching "+Aname+"."+Aclass+"."+str(Aseq)+ \
    #         " y/n "
    #ans = raw_input(prompt)
    #if ans.startswith('y'):
    AIPSDir.PAllDest(disk,
                     err,
                     Atype=Atype,
                     Aname=Aname,
                     Aclass=Aclass,
                     Aseq=Aseq)
    #else:
    #    print "Not confirmed"
    OErr.printErrMsg(err, "Error with destroying AIPS enreies")
Esempio n. 10
0
    def cat(self, disk, userno, url, AIPSdirs):
        # Init Obit if needed
        if not OSystem.PIsInit():
            popsno = 1
            if not self.err:
                self.err=OErr.OErr()
            ObitSys=OSystem.OSystem ("", popsno, userno, \
                                     len(AIPSdirs), AIPSdirs, \
                                     0, [], True, False, self.err)
            OErr.printErrMsg(self.err, "Error with Obit startup")
            doInit = True
        else:
            doInit = False
        _userno = OSystem.PGetAIPSuser()
        OSystem.PSetAIPSuser(userno)

        try:
            num_slots = AIPSDir.PNumber(disk, userno, self.err)
        except OErr.OErr, err:
            OErr.PClear(err)
            if doInit:   # Initialized Obit?
                OSystem.Shutdown(ObitSys)
                self.doInit = False
            return []
Esempio n. 11
0
                print exception
                mess = "TabCopy Failed retCode= "+str(taco.retCode)
                printMess(mess, logfile)
                return 1
    # end loop over tables

    # Write calibration images
    stokes = ["I","Q","U"]
    for sou in Sources:
        # Ignore FITS images
        if sou["CalDataType"]=="FITS":
            continue
        for s in stokes:
            klass = s+sou["CalClass"][1:]
            # Test if file exists
            cno = AIPSDir.PTestCNO(sou["CalDisk"], AIPS.userno, \
                                   sou["CalName"][0:12], klass, "MA", sou["CalSeq"], err)
            if cno>0:
                img = Image.newPAImage("img", sou["CalName"][0:12], klass, \
                                       sou["CalDisk"], sou["CalSeq"], True, err)
                if err.isErr:
                    mess = "Error finding "+s+" image for "+sou["Source"]
                    printMess(mess, logfile)
                    return 1
                imgfile = sou["Source"].strip()+session+s+"Pol.fits"
                EVLACal. EVLAImFITS (img, imgfile, outDisk, err, logfile=logfile)
                if err.isErr:
                    OErr.printErrMsg(err, "Error Writing image")
                    return 1
            else:
                mess = "Skip writing "+s+" image for "+sou["Source"]
                printMess(mess, logfile)
Esempio n. 12
0
File: VLACal.py Progetto: mauch/Obit
def VLAUVLoad(filename, inDisk, Aname, Aclass, Adisk, Aseq, err, logfile=''):
    """ Read FITS uvtab file into AIPS

    Read a UVTAB FITS UV data file and write an AIPS data set
    filename   = name of FITS file
    inDisk     = FITS directory number
    Aname      = AIPS name of file
    Aclass     = AIPS class of file
    Aseq       = AIPS sequence number of file, 0=> create new
    Adisk      = FITS directory number
    err        = Python Obit Error/message stack
    logfile    = logfile for messages
    returns AIPS UV data object
    """
    ################################################################
    #
    # Checks
    if not OErr.OErrIsA(err):
        raise TypeError, "err MUST be an OErr"
    #
    # Get input
    inUV = UV.newPFUV("FITS UV DATA", filename, inDisk, True, err)
    if err.isErr:
        OErr.printErrMsg(err, "Error with FITS data")
    # Get output, create new if seq=0
    if Aseq < 1:
        OErr.printErr(err)  # Print any outstanding messages
        user = OSystem.PGetAIPSuser()
        Aseq = AIPSDir.PHiSeq(Adisk, user, Aname, Aclass, "MA", err)
        # If it already exists, increment seq
        if AIPSDir.PTestCNO(Adisk, user, Aname, Aclass, "MA", Aseq, err) > 0:
            Aseq = Aseq + 1
        OErr.PClear(err)  # Clear any message/error
    mess = "Creating AIPS UV file " + Aname + "." + Aclass + "." + str(
        Aseq) + " on disk " + str(Adisk)
    printMess(mess, logfile)
    outUV = UV.newPAUV("AIPS UV DATA", Aname, Aclass, Adisk, Aseq, False, err)
    if err.isErr:
        OErr.printErrMsg(err, "Error creating AIPS data")
    # Copy
    UV.PCopy(inUV, outUV, err)
    if err.isErr:
        OErr.printErrMsg(err, "Error copying UV data to AIPS")
    # Copy History
    inHistory = History.History("inhistory", inUV.List, err)
    outHistory = History.History("outhistory", outUV.List, err)
    History.PCopyHeader(inHistory, outHistory, err)
    # Add history
    outHistory.Open(History.READWRITE, err)
    outHistory.TimeStamp(" Start Obit uvlod", err)
    outHistory.WriteRec(
        -1, "uvlod   / FITS file " + filename + " disk " + str(inDisk), err)
    outHistory.Close(err)
    #
    # Copy Tables
    exclude = [
        "AIPS HI", "AIPS AN", "AIPS FQ", "AIPS SL", "AIPS PL", "History"
    ]
    include = []
    UV.PCopyTables(inUV, outUV, exclude, include, err)
    return outUV  # return new object
Esempio n. 13
0
def SetEnviron(AIPS_ROOT=None, AIPS_VERSION=None, OBIT_EXEC=None, \
               DA00=None, ARCH="LNX64", aipsdirs=None, fitsdirs=None):
    """ Set environment variables for AIPS, Obit and data access

    AIPS_ROOT    = name of the directory at the root of the AIPS directories
    AIPS_VERSION = name of the directory at root of specific AIPS version
    OBIT_EXEC    = name of directory with Obit bin and TDF directories
    DA00         = name of directory for AIPS DA00
    ARCH         = AIPS architecture string
    aipsdirs     = list of tuples defining AIPS data directories
                   first element is url, None = local,
                       for remote data use form 'http://mymachine.org:8000/RPC2'
                   second element is full path to AIPS data directory
    fitsdirs     = list of tuples defining FITS data directories
                   first element is url, None = local
                   second element is full path to FITS directory
    """
    # Set AIPS_ROOT and AIPS_VERSION for access to AIPS Software
    if AIPS_ROOT:
        os.environ["AIPS_ROOT"] = AIPS_ROOT  # python environment
        cmd = "export AIPS_ROOT="+AIPS_ROOT  # shell environment
        #print cmd
        z=os.system(cmd)
    if AIPS_VERSION:
        os.environ["AIPS_VERSION"] = AIPS_VERSION  # python environment
        cmd = "export AIPS_VERSION="+AIPS_VERSION  # shell environment
        #print cmd
        z=os.system(cmd)

    # Set Obit directory
    if OBIT_EXEC:
        os.environ["OBITEXEC"] = OBIT_EXEC  # python environment
        cmd = "export OBITEXEC="+OBIT_EXEC   # shell environment
        #print cmd
        z=os.system(cmd)

    # Set AIPS DA00
    if DA00:
        os.environ["DA00"] = DA00   # python environment
        cmd = "export DA00="+DA00   # shell environment
        #print cmd
        z=os.system(cmd)

    # Set AIPS DA00
    if ARCH:
        os.environ["ARCH"] = ARCH   # python environment
        cmd = "export ARCH="+ARCH   # shell environment
        #print cmd
        z=os.system(cmd)

    # Set AIPS directories
    if aipsdirs:
        for dirname in aipsdirs:
            url  = dirname[0]
            # Already known?
            known = False
            for adisk in AIPS.disks[1:]:
                if (adisk.url==url) and (adisk.dirname==dirname[1]):
                    known = True
                    break
            if not known:
                # New local or remote AIPS disk
                disk = len(AIPS.disks)
                AIPS.disks.append(AIPSDisk(url, disk, dirname[1]+'/'))
                AIPSDir.PSetDir(disk, dirname[1]+'/', err, URL=url)
                # Define environment variable for local disks
                if url==None:
                    dskno = "DA"+AIPSUtil.ehex(disk,width=2,padding='0')
                    os.environ[dskno] = dirname[1]+'/'        # Python environment
                    cmd = "export "+dskno+"="+dirname[1]+'/'  # shell environment
                    #print cmd
                    z=os.system(cmd)

    # Set NVOL so AIPS Tasks will know how many AIPS disks
    naips = len(AIPS.disks)-1
    os.environ['NVOL'] = str(naips)   # Python evironment

    # Set FITS directories (URL, disk name)
    import FITSDir  # Need Obit/python
    if fitsdirs:
        from FITS import FITSDisk
        # First is $FITS
        if len(fitsdirs)>0:
            cmd = "export FITS="+fitsdirs[0][1]+'/'
            z=os.system(cmd)                         # shell environment
            os.environ["FITS"] = fitsdirs[0][1]+'/'  # Python environment
            # Override previous?
            if len(FITS.disks)>1:
                FITS.disks[1].dirname = fitsdirs[0][1]+'/'
                FITSDir.FITSdisks[0]  = fitsdirs[0][1]
                disk = 0
                FITSDir.PSetDir(fitsdirs[0][1]+'/', disk, err, URL=url)
            else:
                url = fitsdirs[0][0]
                FITSDir.PAddDir(fitsdirs[0][1]+'/', err, URL=url)
            # Other FITS directories
            for dirname in fitsdirs[1:]:
                url  = dirname[0]
                # Already known?
                known = False
                for fdisk in FITS.disks[1:]:
                    if (fdisk.url==url) and (fdisk.dirname==dirname[1]):
                        known = True
                        break
                if not known:
                    # New local or remote AIPS disk
                    disk = len(FITS.disks)
                    FITSDir.PAddDir(dirname[1]+'/', err, URL=url)
                    # Define environment variable for local disks
                    if url==None:
                        dskno = "FITS"+AIPSUtil.ehex(disk-1,width=2,padding='0')
                        os.environ[dskno] = dirname[1]+'/'           # Python environment
                        cmd = "export "+dskno+"="+dirname[1]+'/'     # shell environment
                        #print cmd
                        z=os.system(cmd)
Esempio n. 14
0
# Test AIPS directory listing

import Obit, OSystem, OErr, AIPSDir, Image, ODisplay

# Init Obit
err=OErr.OErr()
ObitSys=OSystem.OSystem ("Imager", 1, 100, 1, ["/DATA/LUSUS_1/"], 1, ["../PythonData/"], 1, 0, err)
OErr.printErrMsg(err, "Error with Obit startup")

from Obit import Bomb

disk=1
# list directory
AIPSDir.PListDir(disk, err)

a=Image.newPACNO(1,4,True,err)
d=ODisplay.ODisplay("ObitView","ObitView",err)
ODisplay.PImage(d,a,err)



# Shutdown Obit
OErr.printErr(err)
del ObitSys
Esempio n. 15
0
def EVLAPolnToFITS(uv, outFile, outDisk, Sources, session, err, \
                      logfile = "",check=False, debug = False):
    """
    Copy new calibration and editing tables from uv to the input FITS file

    Any SN, CL, BP, PD, CP tables not on outFile are copied from uv
    uv FG table 1 is copied to outFile table 2
    Also writes AIPS calibrator images (I, Q, U)
    Returns 0 on success otherwise failure

    * uv       = UV data with calibration and editing
    * outFile  = Name of FITS file to write tables to
    * outDisk  = FITS disk for inFile, 0=>cwd
    * Sources  = List of Source Models as created by EVLACal.EVLACalModel
    * session = Name of session, used for FITS images
    * err      = Obit error/message stack
    * logfile  = Log file for task
    * check    = Only check script
    * debug    = Run tasks debug, show input
    """
    ################################################################
    mess = "Save resultant tables, calibration images"
    printMess(mess, logfile)

    # Copy tables not on inFile
    # UV Data for infile
    outUV = UV.newPFUV("FITS UV DATA", outFile, outDisk, True, err)
    if err.isErr:
        OErr.printErrMsg(err, "Error with FITS data")

    # Use TabCopy
    taco = ObitTask.ObitTask("TabCopy")
    taco.user = AIPS.userno  # This gets lost somehow
    setname(uv, taco)
    taco.taskLog = logfile
    taco.DataType = "AIPS"
    taco.outDType = "FITS"
    taco.outFile = outFile
    taco.outDisk = outDisk

    # FG
    inHi = UV.PGetHighVer(uv, "AIPS FG")
    outHi = UV.PGetHighVer(outUV, "AIPS FG")
    if outHi < 2:
        # copy uv FG table 1 to outUV FG 2 (new table)
        mess = "Copy FG 1 to FG 2"
        printMess(mess, logfile)
        taco.inTab = "AIPS FG"
        taco.inVer = 1
        taco.outVer = 2
        # Trap failure
        try:
            if not check:
                taco.g
        except Exception as exception:
            print(exception)
            mess = "TabCopy Failed retCode= " + str(taco.retCode)
            printMess(mess, logfile)
            return 1

    # Other tables
    tabs = ["BP", "CL", "SN", "PD", "CP"]
    for tt in tabs:
        ttype = "AIPS " + tt
        inHi = UV.PGetHighVer(uv, ttype)
        outHi = UV.PGetHighVer(outUV, ttype)
        mess = "Copy " + tt + " tables " + str(outHi + 1) + " to " + str(inHi)
        printMess(mess, logfile)
        for ver in range(outHi + 1, inHi + 1):
            taco.inTab = ttype
            taco.inVer = ver
            taco.outVer = ver
            # Trap failure
            try:
                if not check:
                    taco.g
            except Exception as exception:
                print(exception)
                mess = "TabCopy Failed retCode= " + str(taco.retCode)
                printMess(mess, logfile)
                return 1
    # end loop over tables

    # Write calibration images
    stokes = ["I", "Q", "U"]
    for sou in Sources:
        # Ignore FITS images
        if sou["CalDataType"] == "FITS":
            continue
        for s in stokes:
            klass = s + sou["CalClass"][1:]
            # Test if file exists
            cno = AIPSDir.PTestCNO(sou["CalDisk"], AIPS.userno, \
                                   sou["CalName"][0:12], klass, "MA", sou["CalSeq"], err)
            if cno > 0:
                img = Image.newPAImage("img", sou["CalName"][0:12], klass, \
                                       sou["CalDisk"], sou["CalSeq"], True, err)
                if err.isErr:
                    mess = "Error finding " + s + " image for " + sou["Source"]
                    printMess(mess, logfile)
                    return 1
                imgfile = sou["Source"].strip() + session + s + "Pol.fits"
                EVLACal.EVLAImFITS(img, imgfile, outDisk, err, logfile=logfile)
                if err.isErr:
                    OErr.printErrMsg(err, "Error Writing image")
                    return 1
            else:
                mess = "Skip writing " + s + " image for " + sou["Source"]
                printMess(mess, logfile)
        # end if exist
    # end loops over Stokes and source
    return 0
Esempio n. 16
0
def EVLAPolnImage(uv, err, Sources=None,  FreqID=1, seq=1, sclass="IClean", band="", \
                  doCalib=-1, gainUse=0, doBand=-1, BPVer=0,  flagVer=-1, maxPixel=50000, \
                  doPol=False, PDVer=-1,  minFlux=0.0, BIF=1, EIF=0, \
                  Stokes="I", FOV=20.0/3600.0, Robust=0, Niter=300, CleanRad=None, \
                  maxPSCLoop=0, minFluxPSC=0.1, solPInt=20.0/60.,\
                  solPMode="P", solPType= "L1", Gain=0.1, \
                  maxASCLoop=0, minFluxASC=0.5, solAInt=2.0, \
                  solAMode="A&P", solAType= "L1",  Beam=[0.0,0.0,0.0], \
                  avgPol=False, avgIF=False, minSNR = 5.0, refAnt=0, \
                  do3D=False, BLFact=0.999, BLchAvg=False, doOutlier=None, \
                  doMB=False, norder=1, maxFBW=0.05, doComRes=False, \
                  nTaper=0, Tapers=[20.0], dispURL="None", \
                  nThreads=1, noScrat=[], logfile='', check=False, debug=False):
    """
    Image a list of sources with optional selfcal
    
    Uses Imager or MFImage to image a list of sources.
    Data must be at least approximately calibrated
    Returns list of source models, None on failure

    * uv         = UV data object
    * err        = Python Obit Error/message stack
    * Sources    = Source name or list of names to use
      If an empty list all sources in uv are included
    * seq        = sequence number of output
    * sclass     = Image output class
    * band       = project band - appended to name
    * BIF        = First IF
    * CIF        = Highest IF
    * FreqID     = Frequency group identifier
    * doCalib    = Apply calibration table
    * gainUse    = CL/SN table to apply
    * doBand     = If >0.5 apply bandpass cal.
    * BPVer      = Bandpass table version
    * flagVer    = Input Flagging table version
    * doPol      = Apply polarization cal?
    * PDVer      = PD version for pol cal, -1=>use IF
    * minFlux    = minimum flux density for initial CLEAN
    * Stokes     = Stokes parameters to image
    * FOV        = Field of view to image in deg
    * Robust     = Weighting robustness parameter
    * Niter      = max no. iterations
    * Gain       = CLEAN loop gain
    * maxPixel   = Maximum number of residuals in inner CLEAN
    * CleanRad   = CLEAN radius about center or None=autoWin
    * maxPSCLoop = max. number of phase sc loops
    * minFluxPSC = Trip level for phase self cal (Jy)
    * solPInt    = Phase solution interval (min)
    * solPMode   = Phase soln mode "P", "DELA"
    * solPType   = Phase soln type
    * maxASCLoop = max. number of amp&phase sc loops
    * minFluxASC = Trip level for amp&phase self cal (Jy)
    * solAInt    = Amp&phase solution interval (min)
    * solAMode   = Amp&Phase soln mode "A&P", "P", "DELA"
    * solAType   = Amp&PPhase soln type
    * Beam       = CLEAN restoring beam
    * avgPol     = Average poln in SC?
    * avgIF      = Average IFs in SC?
    * minSNR     = minimum acceptable SNR in SC
    * refAnt     = Reference antenna
    * do3D       = Use 3D imaging?
    * doComRes   = Force common resolution in frequency? (MF)
    * BLFact     = Baseline dependent averaging factor
    * BLchAvg    = If True and BLFact>=1.0 also average channels
    * doOutlier  = Outliers from NVSS?  Yes=> 4*FOV, 1 mJy
                   None = Default, Yes if freq<6 GHz
    * doMB       = If True is wideband imaging
    * norder     = order on wideband imaging
    * maxFBW     = max. fractional wideband imaging
    * nTaper     = number of (additional) multi resolution tapers
    * Tapers     = Sizes of additional tapers in pixels
    * dispURL    = Name of image display
    * nThreads   = Max. number of threads to use
    * noScrat    = list of disks to avoid for scratch files
    * logfile    = logfile for messages
    * check      = Only check script, don't execute tasks
    * debug      = show input
    """
    ################################################################
    mess = "Image a list of sources "
    printMess(mess, logfile)

    # Tolerate missing BP table
    # Open and close image to sync with disk 
    uv.Open(UV.READONLY, err)
    uv.Close(err)
    hiBP = uv.GetHighVer("AIPS BP")
    if hiBP<=0:
        doBand = -1
    # get reference Freq
    refFreq = uv.Desc.Dict["crval"][uv.Desc.Dict["jlocf"]]
    # If list empty get all sources
    if type(Sources)==list:
        sl = Sources
    else:
        sl = [Sources]

    if len(sl)<=0:
        slist = EVLAAllSource(uv,err,logfile=logfile,check=check,debug=debug)
    else:
        slist = sl
    if doMB:
        imager = ObitTask.ObitTask("MFImage")
        imager.norder = norder
        imager.maxFBW = maxFBW
        imager.prtLv = 2
    else:
        imager = ObitTask.ObitTask("Imager")
        imager.prtLv = 2
    imager.user = AIPS.userno   # This gets lost somehow
    imager.taskLog  = logfile
    if not check:
        setname(uv,imager)
    imager.outDisk     = imager.inDisk
    imager.outName     = "_"+band
    imager.out2Name    = "_"+band
    imager.out2Disk    = imager.inDisk
    imager.outSeq      = seq
    imager.out2Seq     = seq
    imager.outClass    = sclass
    imager.BIF         = BIF
    imager.EIF         = EIF
    imager.BLFact      = BLFact
    imager.BLchAvg     = BLchAvg
    imager.flagVer     = flagVer
    imager.doCalib     = doCalib
    imager.gainUse     = gainUse
    imager.doBand      = doBand
    imager.BPVer       = BPVer
    imager.doPol       = doPol
    if "PDVer" in imager.__dict__:
        imager.PDVer = PDVer
    imager.Stokes      = Stokes
    imager.FOV         = FOV
    imager.Robust      = Robust
    imager.Niter       = Niter
    imager.Gain        = Gain
    imager.Beam        = Beam
    imager.maxPixel    = maxPixel
    imager.minFlux     = minFlux
    imager.maxPixel    = maxPixel
    imager.maxPSCLoop  = maxPSCLoop
    imager.minFluxPSC  = minFluxPSC
    imager.solPInt     = solPInt
    imager.solPMode    = solPMode
    imager.solPType    = solPType
    imager.maxASCLoop  = maxASCLoop
    imager.minFluxASC  = minFluxASC
    imager.solAInt     = solAInt
    imager.solAMode    = solAMode
    imager.solAType    = solAType
    imager.avgPol      = avgPol
    imager.avgIF       = avgIF
    imager.refAnt      = refAnt
    imager.minSNR      = minSNR
    imager.do3D        = do3D
    imager.dispURL     = dispURL
    imager.nTaper      = nTaper
    imager.Tapers      = Tapers
    if doOutlier or ((doOutlier==None) and refFreq<6.0e9):
        FWHM = (45.0 /(refFreq*1.0e-9) ) / 60.   # FWHM in deg
        imager.OutlierDist = FWHM*2.0   # Outliers from NVSS for lower frequencies
        imager.OutlierFlux = 0.01
    # Auto window or centered box
    if CleanRad:
        imager.CLEANBox=[-1,CleanRad,0,0]
    else:
        imager.autoWindow  = True
    if "doComRes" in imager.__dict__:
        imager.doComRes  = doComRes
    imager.noScrat     = noScrat
    imager.nThreads    = nThreads
    if debug:
        imager.prtLv = 5
        imager.i
        imager.debug = debug
    OK = False   # Some must work
    modelList = []   # Init output model list
    # Loop over slist
    for sou in slist:
        imager.Sources[0] = sou
        mess = "Image "+sou
        printMess(mess, logfile)
        # Trap failure
        try:
            if not check:
                imager.g
        except Exception, exception:
            print exception
            mess = "Imager Failed retCode= "+str(imager.retCode)
            printMess(mess, logfile)
            #return 1  Allow some failures
            # Cleanup image mess
            AllDest(err,Atype="MA",Aname=imager.Sources[0], disk=imager.outDisk, Aseq=imager.outSeq);
        else:
            OK = True
        # delete Imager file if not debug
        if not debug:
            out2Name = imager.Sources[0].strip()+"_"+band
            out2Name = out2Name[0:12]
            if doMB:
                out2Class = "MFImage"
            else:
                out2Class = "Imager"
            # Tolerate failures
            try:
                # Test if file exists
                cno = AIPSDir.PTestCNO(imager.out2Disk, AIPS.userno, \
                                       out2Name, out2Class, "UV", imager.out2Seq, err)
                if cno>0:
                    u = UV.newPAUV("zap", out2Name, out2Class, imager.out2Disk, imager.out2Seq, False, err)
                    if UV.PIsA(u):
                        u.Zap(err) # cleanup
                    if err.isErr:
                        mess = "Error deleting Imager work file"
                        printMess(mess, logfile)
                        #return 1
                    del u
                # Create model, add to modelList
                modelList.append(EVLACal.EVLACalModel(sou, CalName=imager.Sources[0]+imager.outName, \
                                                      CalClass=imager.outClass,        \
                                                      CalSeq=imager.outSeq,             \
                                                      CalCmethod="DFT", CalDataType="AIPS",   \
                                                      CalDisk=imager.outDisk, CalNfield=1))
            except Exception, exception:
                print exception
                mess = "Imager Cleanup Failed source= "+imager.Sources[0].strip()+"_"+band
                printMess(mess, logfile)
                OErr.PClear(err)     # Clear any message/error
                #return 1  Allow some failures
            else:
                pass
Esempio n. 17
0
def EVLAPolnFlag (uv, souModels, err, \
                  flagTab=1, timeAvg=0.0, maxAmp=1.0, \
                  doCalib=0, gainUse=0, doBand=0, BPVer=0, flagVer=-1, \
                  doPol=False, PDVer=1, nThreads=1, \
                  check=False, debug = False, noScrat=[], logfile = ""):
    """
    Subtract a polarization model from a data set and flag by residuals
    Data flagged using AutoFlag with points larger than maxAmp

    Returns task error code, 0=OK, else failed
    * uv       = UV data object to calibrate
    * souModels= List of Source Models as created by EVLACal.EVLACalModel
    * err      = Obit error/message stack
    * flagTab  = Output Flagging table version
    * timeAvg  = Time over which data is vector averaged
    * maxAmp   = maximum allowed residual, single or list per source
    #* flagSig  = Flagging level (sigma)
    #* alpha    = Smoothing parameter
    #* timeWind = MWF width (min)
    #* avgTime  = pre average time (min)
    #* avgFreq  = Averaging in frequency
    #             0 = none
    #             1 = average chAvg channels
    #             2 = average all channels
    #             3 = average all channels, IFs
    * doCalib  = Apply calibration table
    * chAvg    = Number of channels to average
    * gainUse  = CL/SN table to apply
    * doBand   = If >0.5 apply bandpass cal.
    * BPVer    = Bandpass table version
    * doPol    = If True apply poln cal
    * PDVer    = AIPS PD polarization cal table to apply
    * flagVer  = Input Flagging table version
    * check    = Only check script, don't execute tasks
    * debug    = Run tasks debug, show input
    * nThreads = Number of threads to use
    * noScrat  = list of disks to avoid for scratch files
    * logfile  = Log file for task
    """
    if len(souModels)<=0:    # Anything to work on?
        return 0
    mess =  "Flag residuals from models "
    printMess(mess, logfile)

    # Setup UVSub
    uvsub = ObitTask.ObitTask("UVSub")
    if not check:
        setname(uv,uvsub)
    uvsub.user     = AIPS.userno   # This gets lost somehow
    uvsub.taskLog  = logfile
    uvsub.nThreads = nThreads
    uvsub.noScrat  = noScrat
    uvsub.outDisk  = uv.Disk
    uvsub.flagVer  = flagVer
    uvsub.doCalib  = doCalib
    uvsub.gainUse  = gainUse
    uvsub.doBand   = doBand
    uvsub.BPVer    = BPVer
    if "PDVer" in uvsub.__dict__:
        uvsub.doPol    = doPol
        uvsub.PDVer    = PDVer
    # setup for flagging
    #flag = ObitTask.ObitTask("MednFlag")
    flag = ObitTask.ObitTask("AutoFlag")
    flag.user = AIPS.userno   # This gets lost somehow
    if not check:
        setoname(uv,flag)
    flag.taskLog  = logfile
    flag.nThreads = nThreads
    flag.flagTab  = flagTab
    flag.timeAvg  = timeAvg
    if type(maxAmp)!=list:
        flag.IClip    = [maxAmp, 0.1]
    #flag.flagSig  = flagSig
    #flag.alpha    = alpha
    #flag.timeWind = timeWind
    #flag.avgTime  = avgTime
    #flag.avgFreq  = avgFreq
    #flag.chAvg    = chAvg
    # Loop over sources
    OK = False
    i = -1
    for model in souModels:
        i += 1
        uvsub.Sources[0]= model["Source"]
        if "DataType2" in uvsub.__dict__:
            uvsub.DataType2 = model["CalDataType"]
        uvsub.in2File   = model["CalFile"]
        uvsub.in2Name   = model["CalName"][0:12]
        uvsub.in2Class  = model["CalClass"]
        uvsub.in2Seq    = model["CalSeq"] 
        uvsub.in2Disk   = model["CalDisk"]
        if "nmaps" in uvsub.__dict__:
            uvsub.nmaps     = model["CalNfield"]
        else:
            uvsub.nfield    = model["CalNfield"]
        uvsub.CCVer     = model["CalCCVer"]
        uvsub.BComp     = model["CalBComp"]
        uvsub.EComp     = model["CalEComp"]
        uvsub.Cmethod   = model["CalCmethod"]
        uvsub.Cmodel    = model["CalCmodel"]
        uvsub.Flux      = model["CalFlux"]
        if "Alpha" in uvsub.__dict__:
            uvsub.Alpha     = model["CalModelSI"]
        uvsub.modelFlux = model["CalModelFlux"]
        uvsub.modelPos  = model["CalModelPos"]
        uvsub.modelParm = model["CalModelParm"]
        # Setup scratch output file
        uvsub.outName  = model["Source"][0:12]
        uvsub.outClass = "PolFlg"
        uvsub.outSeq   = 6666
        if debug:
            uvsub.i
            uvsub.debug = debug
        # Trap failure
        try:
            mess = "Run UVSub on "+uvsub.Sources[0]+", then edit residuals above "+str(maxAmp[i])
            printMess(mess, logfile)
            if not check:
                uvsub.g
        except Exception, exception:
            print exception
            mess = "UVSub Failed retCode= "+str(uvsub.retCode)+" Source "+uvsub.Sources[0]
            printMess(mess, logfile)
            return uvsub.retCode
        else:
            OK = True
        # Now flag
        cno = AIPSDir.PTestCNO(uvsub.outDisk, AIPS.userno, \
                               uvsub.outName, uvsub.outClass, "UV", uvsub.outSeq, err)
        if cno>0:
            uvscr = UV.newPAUV("zap", uvsub.outName, uvsub.outClass, uvsub.outDisk, uvsub.outSeq, False, err)
        else:
            mess = "No subtracted data created for Source "+uvsub.Sources[0]
            printMess(mess, logfile)
            return 1
        # Flag using scratch writing input FG
        flag.Sources[0]= uvsub.Sources[0]
        if type(maxAmp)==list:
            flag.IClip    = [maxAmp[i], 0.1]
        if not check:
            setname(uvscr, flag)
        if debug:
            flag.i
            flag.debug = debug
        # Trap failure
        try:
            if not check:
                flag.g
        except Exception, exception:
            print exception
            mess = "AutoFlag Failed retCode= "+str(flag.retCode)+" Source "+flag.Sources[0]
            printMess(mess, logfile)
            return flag.retCode
Esempio n. 18
0
def newPAImage(name, Aname, Aclass, disk, seq, exists, err, verbose=False):
    """
    Create and initialize an AIPS based Image structure
    
    Create, set initial access information (full image, plane at a time)
    and if exists verifies the file.
    Returns the Python Image object
    isOK member set to indicate success

    * name     = name desired for object (labeling purposes)
    * Aname    = AIPS name of file
    * Aclass   = AIPS class of file
    * seq      = AIPS sequence number of file
    * disk     = FITS directory number
    * exists   = if true then the file is opened and closed to verify
    * err      = Python Obit Error/message stack
    * verbose  = If true any give error messages, else suppress
    """
    ################################################################
    out = ImageMF(name)
    out.isOK = True  # until proven otherwise
    cno = -1
    user = OSystem.PGetAIPSuser()
    # print "disk, aseq", disk, seq
    # Does it really previously exist?
    test = AIPSDir.PTestCNO(disk, user, Aname, Aclass, "MA", seq, err)
    out.exist = test > 0
    if exists:  # If user thinks file exists...
        if out.exist:  # If file is defined in catalog -> verify that file exists
            OErr.PLog(err, OErr.Info, Aname + " image found. Now verifying...")
            if verbose: OErr.printErr(err)
            cno = AIPSDir.PFindCNO(disk, user, Aname, Aclass, "MA", seq, err)
            Obit.ImageMFSetAIPS(out.me, 2, disk, cno, user, blc, trc, err.me)
            Obit.ImagefullInstantiate(out.cast("ObitImage"), 1, err.me)
            #print "found",Aname,Aclass,"as",cno
        else:  # If file not defined in catalog -> error
            OErr.PLog(err, OErr.Error, Aname + " image does not exist")
            out.isOK = False
    else:  # exists=False
        # Create new image entry in catalog; if image already defined, this
        # has no effect
        OErr.PLog(err, OErr.Info,
                  "Creating new image: " + Aname + ", " + Aclass)
        if verbose: OErr.printErr(err)
        cno = AIPSDir.PAlloc(disk, user, Aname, Aclass, "MA", seq, err)
        Obit.ImageMFSetAIPS(out.me, 2, disk, cno, user, blc, trc, err.me)
        #print "assigned",Aname,Aclass,"to",cno

    # show any errors if wanted
    if verbose and err.isErr:
        out.isOK = False
        OErr.printErrMsg(err, "Error creating AIPS Image object")
    elif err.isErr:
        out.isOK = False
        OErr.PClear(err)  # Clear unwanted messages
    else:
        OErr.PClear(err)  # Clear non-error messages

    # It work?
    if not out.isOK:
        return out

    # Add File info
    out.FileType = 'AIPS'
    out.Disk = disk
    out.Aname = Aname
    out.Aclass = Aclass
    out.Aseq = seq
    out.Otype = "Image"
    out.Acno = cno
    return out  # seems OK