Example #1
0
def open_ms(vis):
    (ms, ) = gentools(['ms'])
    ms.open(vis)
    try:
        yield ms
    finally:
        ms.close()
Example #2
0
def open_table(vis):
    (tb, ) = gentools(['tb'])
    tb.open(vis)
    try:
        yield tb
    finally:
        tb.close()
Example #3
0
def msmoments(infile, moments, antenna, field, spw, includemask, excludemask,
              outfile, overwrite):

    retms = None
    casalog.origin('msmoments')

    # CAS-5410 Use private tools inside task scripts
    ms = gentools(['ms'])[0]

    try:
        if ((not overwrite) and (len(moments) == 1)):
            if (os.path.exists(outfile)):
                raise Exception(outfile + " exists.")
        ms.open(infile)
        retms = ms.moments(moments=moments,
                           antenna=antenna,
                           field=field,
                           spw=spw,
                           includemask=includemask,
                           excludemask=excludemask,
                           outfile=outfile,
                           overwrite=overwrite)
        ms.close()
        retms.close()
    except Exception, instance:
        ms.close()
        if retms: retms.close()
        print '*** Error ***', instance
        raise Exception, instance
Example #4
0
 def __execute_imaging(self):
     #if pointingcolumn.upper()=="OFFSET":
     #   pointingcolumn="POINTING_OFFSET"
     #print "pointingcolumn=",pointingcolumn
     #print "Imaging...."
     casalog.post( "pointingcolumn used: %s" % self.pointingcolumn )
     casalog.post( "Imaging...." )
     self.open_imager(self.infile)
     self.imager.selectvis(spw=self.spw, baseline=self.antsel)
     #self.imager.selectvis(field=0, spw=self.spw, baseline=self.antsel)
     #self.imager.selectvis(vis=infile, field=0, spw=spw, baseline=antsel)
     #self.imager.defineimage(nx=nx, ny=ny, cellx=cellx, celly=celly,  phasecenter=phasecenter, spw=0, stokes=stokes, movingsource=ephemsrcname)
     self.imager.defineimage(nx=self.nx, ny=self.ny, cellx=self.cellx, celly=self.celly, phasecenter=self.phasecenter, spw=self.spw, stokes=self.stokes, movingsource=self.ephemsrcname)
     #self.imager.defineimage(nx=nx, ny=ny, cellx=cellx, celly=celly,  phasecenter=phasecenter, spw=0, stokes=stokes, movingsource=ephemsrcname,restfreq=1.14e11)
     self.imager.setoptions(ftmachine='sd', gridfunction=self.gridfunction)
     #self.imager.setsdoptions(convsupport=5)
     self.imager.setsdoptions(pointingcolumntouse=self.pointingcolumn, convsupport=-1, truncate=-1, gwidth=-1, jwidth=-1)
     self.imager.makeimage(type='singledish-observed', image=self.outfile)
     #self.imager.makeimage(type='singledish', image=self.outfile)
     # create temporal weight image for masking
     weightfile_temp = self.outfile+".weight."+str(time.time()).replace('.', '_')
     self.imager.makeimage(type='coverage', image=weightfile_temp)
     self.close_imager()
     # Handle mask
     my_ia = gentools(['ia'])[0]
     my_ia.open(self.outfile)
     my_ia.calcmask("'%s'>%f" % (weightfile_temp,0.0), asdefault=True)
     my_ia.close()
     shutil.rmtree(weightfile_temp)
Example #5
0
def open_ia(imagename):
    (ia, ) = gentools(['ia'])
    ia.open(imagename)
    try:
        yield ia
    finally:
        ia.close()
Example #6
0
def splitant(filename, outprefix='',overwrite=False, getpt=True):
    """
    Split Measurement set by antenna name, save data as a scantables,
    and return a list of filename. Note that frequency reference frame
    is imported as it is in Measurement set.
    Notice this method can only be available from CASA.
    Prameter
       filename:    the name of Measurement set to be read.
       outprefix:   the prefix of output scantable name.
                    the names of output scantable will be
                    outprefix.antenna1, outprefix.antenna2, ....
                    If not specified, outprefix = filename is assumed.
       overwrite    If the file should be overwritten if it exists.
                    The default False is to return with warning
                    without writing the output. USE WITH CARE.
       getpt        Whether to import direction from MS/POINTING
                    table or not. Default is True (import direction).
    """
    # Import the table toolkit from CASA
    from taskinit import gentools
    from asap.scantable import is_ms
    tb = gentools(['tb'])[0]
    # Check the input filename
    if isinstance(filename, str):
        import os.path
        filename = os.path.expandvars(filename)
        filename = os.path.expanduser(filename)
        if not os.path.exists(filename):
            s = "File '%s' not found." % (filename)
            raise IOError(s)
        # check if input file is MS
        if not is_ms(filename):
            s = "File '%s' is not a Measurement set." % (filename)
            raise IOError(s)
    else:
        s = "The filename should be string. "
        raise TypeError(s)
    # Check out put file name
    outname=''
    if len(outprefix) > 0: prefix=outprefix+'.'
    else:
        prefix=filename.rstrip('/')
    # Now do the actual splitting.
    outfiles=[]
    tb.open(tablename=filename,nomodify=True)
    ant1=tb.getcol('ANTENNA1',0,-1,1)
    anttab=tb.getkeyword('ANTENNA').lstrip('Table: ')
    tb.close()
    tb.open(tablename=anttab,nomodify=True)
    nant=tb.nrows()
    antnames=tb.getcol('NAME',0,nant,1)
    tb.close()
    for antid in set(ant1):
        scan=scantable(filename,average=False,antenna=int(antid),getpt=getpt)
        outname=prefix+antnames[antid]+'.asap'
        scan.save(outname,format='ASAP',overwrite=overwrite)
        del scan
        outfiles.append(outname)
    return outfiles
Example #7
0
 def _is_empty_caltable(self, **params):
     (tb, ) = gentools(['tb'])
     tb.open(params['outfile'])
     try:
         nrow = tb.nrows()
     finally:
         tb.close()
     self.assertEqual(nrow, 0)
Example #8
0
 def _is_empty_caltable(self, **params):
     (tb,) = gentools(['tb'])
     tb.open(params['outfile'])
     try:
         nrow = tb.nrows()
     finally:
         tb.close()
     self.assertEqual(nrow, 0)
Example #9
0
    def generate(self):
        # generate Tsys table
        from sdcal_cli import sdcal_cli as sdcal
        sdcal(infile=self.infile,
              outfile=self.tsystable,
              calmode='tsys',
              overwrite=True)

        self.assertTrue(os.path.exists(self.tsystable))

        # get information from MS
        (tb, ) = gentools(['tb'])
        tb.open(self.infile)
        s = tb.getcol('FLOAT_DATA', 0, 2)
        w = tb.getcol('WEIGHT', 0, 2).reshape((2, 1, 2))
        t = tb.getcol('TIME')
        tmax = t.max()
        tmin = t.min()
        tb.close()

        # generate sky table based on Tsys table
        tb.open(self.tsystable)
        t = tb.copy(self.skytable, deep=True)
        tb.close()
        t.close()
        tb.open(self.skytable, nomodify=False)
        tb.putkeyword('VisCal', 'SDSKY_PS')
        s[:] = w
        tb.putcol('WEIGHT', s, 0, 2)
        s[:] = 1.0
        tb.putcol('FPARAM', s, 0, 2)
        tb.close()
        with open(self.skytable + '/table.info', 'r') as f:
            l = f.read()
        #print l
        l = l.replace('B TSYS', 'SDSKY_PS')
        #print l
        with open(self.skytable + '/table.info', 'w') as f:
            f.write(l)

        self.assertTrue(os.path.exists(self.skytable))

        # edit Tsys table
        tb.open(self.tsystable, nomodify=False)
        spw_id = tb.getcol('SPECTRAL_WINDOW_ID')
        spw_id[2:] = spw_id[:2]
        tb.putcol('SPECTRAL_WINDOW_ID', spw_id)
        time = tb.getcol('TIME')
        time[:2] = tmin
        time[2:] = tmax
        tb.putcol('TIME', time)
        param = tb.getcol('FPARAM')
        param[:, :, :2] = 100.0
        param[:, :, 2:] = 200.0
        tb.putcol('FPARAM', param)
        param[:] = 1.0
        tb.putcol('WEIGHT', param)
        tb.close()
Example #10
0
File: casa.py Project: UCL/purify
 def _get_table(self, name=None):
     """ A table object """
     from taskinit import gentools
     tb, = gentools(['tb'])
     if name is None:
         tb.open('%s' % self.measurement_set)
     else:
         tb.open('%s/%s' % (self.measurement_set, name))
     return tb
Example #11
0
 def _edit_tsys_spw(self, spwmap):
     (tb, ) = gentools(['tb'])
     tb.open(self.tsystable, nomodify=False)
     spw_id = tb.getcol('SPECTRAL_WINDOW_ID')
     print 'before ', spw_id
     for i in xrange(len(spw_id)):
         spw_id[i] = spwmap[spw_id[i]]
     print 'after', spw_id
     tb.putcol('SPECTRAL_WINDOW_ID', spw_id)
     tb.close()
Example #12
0
    def _generic_verify(self, **params):
        (
            tb,
            ms,
        ) = gentools(['tb', 'ms'])

        nrow_per_spw = 102

        spwsel = params['spw']
        if spwsel == '':
            nspw = 2
        else:
            infile = params['infile']
            ms.open(infile)
            try:
                ms.msselect({'spw': spwsel})
                mssel = ms.msselectedindices()
                nspw = len(mssel['spw'])
            finally:
                ms.close()

        caltable = params['outfile']
        tb.open(caltable)
        try:
            nrow = tb.nrows()
            self.assertEqual(nrow, nspw * nrow_per_spw)

            spwids = tb.getcol('SPECTRAL_WINDOW_ID')
            spwid_list = set(spwids)
            for spwid in spwid_list:
                self.assertEqual(len(spwids[spwids == spwid]), nrow_per_spw)

            # by definition, mean of gain factor becomes almost 1.0
            for spwid in spwid_list:
                t = tb.query('SPECTRAL_WINDOW_ID == %s' % (spwid))
                try:
                    fparam = t.getcol('CPARAM').real
                    flag = t.getcol('FLAG')
                finally:
                    t.close()
                ma = numpy.ma.masked_array(fparam, flag)
                mean_gain = ma.mean(axis=2)
                print mean_gain
                npol = fparam.shape[0]
                for ipol in xrange(npol):
                    if numpy.any(flag[ipol] == False):
                        self.assertTrue(abs(mean_gain[ipol] - 1.0) < 0.01)

            self._verify_param_and_flag(tb)

        finally:
            tb.close()
        pass
Example #13
0
    def get_selection_idx_for_ms(self, file_idx):
        """
        Returns a dictionary of selection indices for i-th MS in infiles

        Argument: file idx in infiles list
        """
        if file_idx < len(self.infiles) and file_idx > -1:
            vis = self.infiles[file_idx]
            field = self.get_selection_param_for_ms(file_idx, self.field)
            spw = self.get_selection_param_for_ms(file_idx, self.spw)
            spw = self.__format_spw_string(spw)
            antenna = self.get_selection_param_for_ms(file_idx, self.antenna)
            if antenna == -1: antenna = ''
            scan = self.get_selection_param_for_ms(file_idx, self.scanno)
            intent = self.get_selection_param_for_ms(file_idx, self.intent)
            my_ms = gentools(['ms'])[0]
            sel_ids = my_ms.msseltoindex(vis=vis,
                                         spw=spw,
                                         field=field,
                                         baseline=antenna,
                                         scan=scan)
            fieldid = list(
                sel_ids['field']) if len(sel_ids['field']) > 0 else -1
            baseline = self.format_ac_baseline(sel_ids['antenna1'])
            scanid = list(sel_ids['scan']) if len(sel_ids['scan']) > 0 else ""
            # SPW (need to get a list of valid spws instead of -1)
            if len(sel_ids['channel']) > 0:
                spwid = [chanarr[0] for chanarr in sel_ids['channel']]
            elif spw == "":  # No spw selection
                my_ms.open(vis)
                try:
                    spwinfo = my_ms.getspectralwindowinfo()
                except:
                    raise
                finally:
                    my_ms.close()

                spwid = [int(idx) for idx in spwinfo.keys()]
            else:
                raise RuntimeError("Invalid spw selction, %s ,for MS %d" (
                    str(spw), file_idx))

            return {
                'field': fieldid,
                'spw': spwid,
                'baseline': baseline,
                'scan': scanid,
                'intent': intent,
                'antenna1': sel_ids['antenna1']
            }
        else:
            raise ValueError, ("Invalid file index, %d" % file_idx)
Example #14
0
File: casa.py Project: UCL/purify
def purify_image(datatransform, imagename, imsize=(128, 128), overwrite=False,
        **kwargs):
    """ Creates an image using the Purify method

        Parameters:
            datatransform: DataTransform derived
                Iterator of purify data
            imagename: string
                Path to output image
            imsize: 2-tuple
                Width and height of the output image in pixels
            overwrite: bool
                Whether to overwrite existing image. Defaults to False.
            other arguments:
                See purify.SDMM
    """
    from os.path import exists, abspath
    from numpy import array
    from taskinit import gentools, casalog
    from . import SDMM

    # Input sanitizing
    if 'image_size' in kwargs:
        msg = 'Image size should be given using the imsize argument'
        raise ValueError(msg)

    imagename = abspath(imagename)
    if exists(imagename) and not overwrite:
        msg = "File %s already exist. It will not be overwritten.\n" \
                "Please delete the file or choose another filename for the" \
                " image."
        raise IOError(msg % imagename)

    casalog.post('Starting Purify task')
    scale = kwargs.pop('scale', 'default')
    sdmm = SDMM(image_size=imsize, **kwargs)


    # Contains images over all dimensions
    image = []
    for i, data in enumerate(datatransform):
        casalog.origin('Purifying plane %s' % str(i))
        channel = sdmm(data, scale=scale)
        image.append(channel)

    image = array(image, order='F')

    # Create image
    casalog.post('Creating CASA image')
    ia, = gentools(['ia'])
    ia.newimagefromarray(imagename, image.real, overwrite=overwrite)
Example #15
0
    def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2):
        if not image or not os.path.exists(image):
            raise RuntimeError, "No image found to fit."
        if os.path.exists(model):
            # CAS-5410 Use private tools inside task scripts
            cu = utilstool()
            cu.removetable([model])
        tmpia = gentools(['ia'])[0]
        modelimg = tmpia.newimagefromimage(infile=image, outfile=model)
        try:
            if tmpia.isopen(): tmpia.close()
            imshape = modelimg.shape()
            # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task.
            ndim = len(imshape)
            nx = imshape[0]
            ny = imshape[1]
            # an xy-plane can be fit simultaneously (doing per plane to save memory)
            if ndim == 3:
                get_blc = lambda i, j: [0, 0, i]
                get_trc = lambda i, j: [nx - 1, ny - 1, i]
                imshape2 = imshape[2]
                imshape3 = 1
            elif ndim == 4:
                get_blc = lambda i, j: [0, 0, i, j]
                get_trc = lambda i, j: [nx - 1, ny - 1, i, j]
                imshape2 = imshape[2]
                imshape3 = imshape[3]
            else:  # ndim == 2
                get_blc = lambda i, j: [0, 0]
                get_trc = lambda i, j: [nx - 1, ny - 1]
                imshape2 = 1
                imshape3 = 1

            for i3 in range(imshape3):
                for i2 in range(imshape2):
                    blc = get_blc(i2, i3)
                    trc = get_trc(i2, i3)
                    dslice = modelimg.getchunk(blc, trc)
                    mslice = modelimg.getchunk(blc, trc, getmask=True)
                    model = self._get_polyfit_model_array(
                        dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis,
                        order)
                    modelimg.putchunk(model, blc)

            # the fit model image itself is free from invalid pixels
            modelimg.calcmask('T', asdefault=True)
        except:
            raise
        finally:
            modelimg.close()
Example #16
0
 def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2):
     if not image or not os.path.exists(image):
         raise RuntimeError, "No image found to fit."
     if os.path.exists(model):
         # CAS-5410 Use private tools inside task scripts
         cu = utilstool()
         cu.removetable([model])
     tmpia = gentools(['ia'])[0]
     modelimg = tmpia.newimagefromimage(infile=image, outfile=model)
     try:
         if tmpia.isopen(): tmpia.close()
         imshape = modelimg.shape()
         # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task.
         ndim = len(imshape)
         nx = imshape[0]
         ny = imshape[1]
         # an xy-plane can be fit simultaneously (doing per plane to save memory)
         if ndim == 3:
             for ichan in range(imshape[2]):
                 dslice = modelimg.getchunk([0, 0, ichan],
                                            [nx - 1, ny - 1, ichan])
                 mslice = modelimg.getchunk([0, 0, ichan],
                                            [nx - 1, ny - 1, ichan],
                                            getmask=True)
                 model = self._get_polyfit_model_array(
                     dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis,
                     order)
                 modelimg.putchunk(model, [0, 0, ichan, ipol])
         elif ndim == 4:
             for ipol in range(imshape[3]):
                 for ichan in range(imshape[2]):
                     dslice = modelimg.getchunk(
                         [0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                     mslice = modelimg.getchunk(
                         [0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol],
                         getmask=True)
                     model = self._get_polyfit_model_array(
                         dslice.reshape(nx, ny), mslice.reshape(nx, ny),
                         axis, order)
                     modelimg.putchunk(model, [0, 0, ichan, ipol])
         else:
             # ndim=2 is not supported in this task.
             raise RuntimeError, "image dimension should be 3 or 4"
         # the fit model image itself is free from invalid pixels
         modelimg.calcmask('T', asdefault=True)
     except:
         raise
     finally:
         modelimg.close()
Example #17
0
 def _generic_verify(self, **params):
     (tb,ms,) = gentools(['tb','ms'])
     
     nrow_per_spw = 102
     
     spwsel = params['spw']
     if spwsel == '':
         nspw = 2
     else:
         infile = params['infile']
         ms.open(infile)
         try:
             ms.msselect({'spw': spwsel})
             mssel = ms.msselectedindices()
             nspw = len(mssel['spw'])
         finally:
             ms.close()
     
     caltable = params['outfile']
     tb.open(caltable)
     try:
         nrow = tb.nrows()
         self.assertEqual(nrow, nspw * nrow_per_spw)
         
         spwids = tb.getcol('SPECTRAL_WINDOW_ID')
         spwid_list = set(spwids)
         for spwid in spwid_list:
             self.assertEqual(len(spwids[spwids == spwid]), nrow_per_spw)
             
             
         # by definition, mean of gain factor becomes almost 1.0
         for spwid in spwid_list:
             t = tb.query('SPECTRAL_WINDOW_ID == %s'%(spwid))
             try:
                 fparam = t.getcol('CPARAM').real
                 flag = t.getcol('FLAG')
             finally:
                 t.close()
             ma = numpy.ma.masked_array(fparam, flag)
             mean_gain = ma.mean(axis=2)
             print mean_gain
             self.assertTrue(numpy.all((numpy.abs(mean_gain) - 1.0) < 0.01))
             
         
         self._verify_fparam_and_flag(tb)
         
     finally:
         tb.close()
     pass
Example #18
0
 def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2):
     if not image or not os.path.exists(image):
         raise RuntimeError, "No image found to fit."
     if os.path.exists( model ):
         # CAS-5410 Use private tools inside task scripts
         cu = utilstool()
         cu.removetable([model])
     tmpia = gentools(['ia'])[0]
     modelimg = tmpia.newimagefromimage(infile=image,outfile=model)
     try:
         if tmpia.isopen(): tmpia.close()
         imshape = modelimg.shape()
         # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task.
         ndim = len(imshape)
         nx = imshape[0]
         ny = imshape[1]
         # an xy-plane can be fit simultaneously (doing per plane to save memory)
         if ndim == 3:
             get_blc = lambda i, j: [0, 0, i]
             get_trc = lambda i, j: [nx-1, ny-1, i]
             imshape2 = imshape[2]
             imshape3 = 1
         elif ndim == 4:
             get_blc = lambda i, j: [0, 0, i, j]
             get_trc = lambda i, j: [nx-1, ny-1, i, j]
             imshape2 = imshape[2]
             imshape3 = imshape[3]
         else: # ndim == 2 
             get_blc = lambda i, j: [0,0]
             get_trc = lambda i, j: [nx-1, ny-1]
             imshape2 = 1
             imshape3 = 1
             
         for i3 in range(imshape3):
             for i2 in range(imshape2):
                 blc = get_blc(i2, i3)
                 trc = get_trc(i2, i3)
                 dslice = modelimg.getchunk(blc, trc)
                 mslice = modelimg.getchunk(blc, trc, getmask=True)
                 model = self._get_polyfit_model_array(dslice.reshape(nx,ny),
                                                       mslice.reshape(nx,ny),
                                                       axis, order)
                 modelimg.putchunk(model, blc)
             
         # the fit model image itself is free from invalid pixels
         modelimg.calcmask('T', asdefault=True)
     except: raise
     finally: modelimg.close()
Example #19
0
 def __init__(self, imagename):
     self.imagename = imagename
     # read data to storage
     ia = gentools(['ia'])[0]
     ia.open(self.imagename)
     try:
         self.image_shape = ia.shape()
         self.coordsys = ia.coordsys()
         coord_types = self.coordsys.axiscoordinatetypes()
         self.units = self.coordsys.units()
         self.names = self.coordsys.names()
         self.direction_reference = self.coordsys.referencecode('dir')[0]
         self.spectral_reference = self.coordsys.referencecode('spectral')[0]
         self.id_direction = coord_types.index('Direction')
         self.id_direction = [self.id_direction, self.id_direction+1]
         self.id_spectral = coord_types.index('Spectral')
         self.id_stokes = coord_types.index('Stokes')
         casalog.post('id_direction=%s'%(self.id_direction))
         casalog.post('id_spectral=%s'%(self.id_spectral))
         casalog.post('id_stokes=%s'%(self.id_stokes))
         self.data = ia.getchunk()
         self.mask = ia.getchunk(getmask=True)
         bottom = ia.toworld(numpy.zeros(len(self.image_shape),dtype=int), 'q')['quantity']
         top = ia.toworld(self.image_shape-1, 'q')['quantity']
         key = lambda x: '*%s'%(x+1)
         ra_min = bottom[key(self.id_direction[0])]
         ra_max = top[key(self.id_direction[0])]
         if ra_min > ra_max:
             ra_min,ra_max = ra_max,ra_min
         self.ra_min = ra_min
         self.ra_max = ra_max
         self.dec_min = bottom[key(self.id_direction[1])]
         self.dec_max = top[key(self.id_direction[1])]
         self._brightnessunit = ia.brightnessunit()
         if self.spectral_label == 'Frequency':
             refpix, refval, increment = self.spectral_axis(unit='GHz')
             self.spectral_data = numpy.array([refval+increment*(i-refpix) for i in xrange(self.nchan)])
         elif self.spectral_label == 'Velocity':
             refpix, refval, increment = self.spectral_axis(unit='km/s')
             self.spectral_data = numpy.array([refval+increment*(i-refpix) for i in xrange(self.nchan)])                
         self.stokes = self.coordsys.stokes()
     finally:
         ia.close()
Example #20
0
    def _verify_param_and_flag(self, table):
        (reftable, ) = gentools(['tb'])
        reftable.open(self.reffile)

        try:
            nrow = table.nrows()
            ref_nrow = reftable.nrows()
            self.assertEqual(nrow, ref_nrow)

            for irow in xrange(nrow):
                ref_fparam = reftable.getcell('CPARAM', irow).real
                fparam = table.getcell('CPARAM', irow).real
                self.assertTrue(numpy.all(ref_fparam == fparam))

                ref_flag = reftable.getcell('FLAG', irow)
                flag = table.getcell('FLAG', irow)
                self.assertTrue(numpy.all(ref_flag == flag))
        finally:
            reftable.close()
Example #21
0
 def _verify_fparam_and_flag(self, table):
     (reftable,) = gentools(['tb'])
     reftable.open(self.reffile)
     
     try:
         nrow = table.nrows()
         ref_nrow = reftable.nrows()
         self.assertEqual(nrow, ref_nrow)
         
         for irow in xrange(nrow):
             ref_fparam = reftable.getcell('CPARAM', irow).real
             fparam = table.getcell('CPARAM', irow).real
             self.assertTrue(numpy.all(ref_fparam == fparam))
             
             ref_flag = reftable.getcell('FLAG', irow)
             flag = table.getcell('FLAG', irow)
             self.assertTrue(numpy.all(ref_flag == flag))
     finally:
         reftable.close()
Example #22
0
 def __polynomial_fit_model(self, image=None, model=None, axis=0, order=2):
     if not image or not os.path.exists(image):
         raise RuntimeError, "No image found to fit."
     if os.path.exists(model):
         # CAS-5410 Use private tools inside task scripts
         cu = utilstool()
         cu.removetable([model])
     tmpia = gentools(["ia"])[0]
     modelimg = tmpia.newimagefromimage(infile=image, outfile=model)
     try:
         if tmpia.isopen():
             tmpia.close()
         imshape = modelimg.shape()
         # the axis order of [ra, dec, chan(, pol)] is assumed throughout the task.
         ndim = len(imshape)
         nx = imshape[0]
         ny = imshape[1]
         # an xy-plane can be fit simultaneously (doing per plane to save memory)
         if ndim == 3:
             for ichan in range(imshape[2]):
                 dslice = modelimg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                 mslice = modelimg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan], getmask=True)
                 model = self._get_polyfit_model_array(dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis, order)
                 modelimg.putchunk(model, [0, 0, ichan, ipol])
         elif ndim == 4:
             for ipol in range(imshape[3]):
                 for ichan in range(imshape[2]):
                     dslice = modelimg.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                     mslice = modelimg.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol], getmask=True)
                     model = self._get_polyfit_model_array(
                         dslice.reshape(nx, ny), mslice.reshape(nx, ny), axis, order
                     )
                     modelimg.putchunk(model, [0, 0, ichan, ipol])
         else:
             # ndim=2 is not supported in this task.
             raise RuntimeError, "image dimension should be 3 or 4"
         # the fit model image itself is free from invalid pixels
         modelimg.calcmask("T", asdefault=True)
     except:
         raise
     finally:
         modelimg.close()
Example #23
0
def create_4d_image(infile, outfile):
    (ia,) = gentools(['ia'])
    ia.open(infile)
    image_shape = ia.shape()
    try:
        if len(image_shape) < 4:
            # add degenerate axes
            axistypes = ia.coordsys().axiscoordinatetypes()
            no_stokes = 'Stokes' not in axistypes
            no_spectral = 'Spectral' not in axistypes
            stokes_axis = 'I' if no_stokes else ''
            outimage = ia.adddegaxes(outfile=outfile, spectral=no_spectral,
                                           stokes=stokes_axis)
        else:
            # generage complete copy of input image using subimage
            outimage = ia.subimage(outfile=outfile)
    finally:
        ia.close()
        
    return outimage
Example #24
0
    def _updateHistory(self, clrecs, vis):
        """
	Update history table when setSolarObjectJy is run
	"""
        #
        from taskinit import gentools
        (mytb, ) = gentools(['tb'])
        mytb.open(vis + '/HISTORY', nomodify=False)
        nrow = mytb.nrows()
        lasttime = mytb.getcol('TIME')[nrow - 1]
        rown = nrow
        #
        for ky in clrecs.keys():
            # expect only one component for each
            comp = clrecs[ky]['component0']
            srcn = ky.split('_')[0]
            ispw = ky.split('_')[1]
            mytb.addrows(1)
            appl = 'setjy'
            msg = (
                " %s: spw %s Flux:[I=%s,Q=%s,U=%s,V=%s] +/- [I=%s,Q=%s,U=%s,V=%s] Jy"
                % (srcn, ispw, comp['flux']['value'][0],
                   comp['flux']['value'][1], comp['flux']['value'][2],
                   comp['flux']['value'][3], comp['flux']['error'][0],
                   comp['flux']['error'][1], comp['flux']['error'][2],
                   comp['flux']['error'][3]))

            origin = 'setjy'
            priority = 'INFO'
            time = lasttime
            emptystrarr = numpy.array([''])
            mytb.putcell('APP_PARAMS', rown, [''])
            mytb.putcell('CLI_COMMAND', rown, [''])
            mytb.putcell('APPLICATION', rown, appl)
            mytb.putcell('MESSAGE', rown, msg)
            mytb.putcell('OBSERVATION_ID', rown, -1)
            mytb.putcell('ORIGIN', rown, origin)
            mytb.putcell('PRIORITY', rown, priority)
            mytb.putcell('TIME', rown, time)
            rown += 1
        mytb.close()
Example #25
0
def msmoments( infile, moments, antenna, field, spw, includemask, excludemask, outfile, overwrite):
    
    retms=None
    casalog.origin('msmoments')

    # CAS-5410 Use private tools inside task scripts
    ms = gentools(['ms'])[0]

    try:
        if ( (not overwrite) and (len(moments) == 1) ):
            if ( os.path.exists(outfile) ):
                raise Exception( outfile+" exists." ) 
        ms.open( infile ) 
        retms = ms.moments( moments=moments,antenna=antenna,field=field,spw=spw,includemask=includemask,excludemask=excludemask,outfile=outfile,overwrite=overwrite)
	ms.close()
        retms.close()
    except Exception, instance:
        ms.close()
        if retms: retms.close()
	print '*** Error ***',instance
	raise Exception, instance
Example #26
0
    def _updateHistory(self,clrecs,vis):
	"""
	Update history table when setSolarObjectJy is run
	"""
	# 
        from taskinit import gentools 
	(mytb,) = gentools(['tb'])
	mytb.open(vis+'/HISTORY',nomodify=False)
	nrow = mytb.nrows()
	lasttime=mytb.getcol('TIME')[nrow-1]
	rown=nrow
	#
	for ky in clrecs.keys():
	    # expect only one component for each
	    comp = clrecs[ky]['component0']
	    srcn = ky.split('_')[0]
	    ispw = ky.split('_')[1]
	    mytb.addrows(1)
	    appl='setjy' 
	    msg = (" %s: spw %s Flux:[I=%s,Q=%s,U=%s,V=%s] +/- [I=%s,Q=%s,U=%s,V=%s] Jy" %  
		(srcn, ispw, comp['flux']['value'][0], comp['flux']['value'][1], 
		 comp['flux']['value'][2], comp['flux']['value'][3],
		 comp['flux']['error'][0], comp['flux']['error'][1],comp['flux']['error'][2],
		 comp['flux']['error'][3]))

            origin='setjy'
	    priority='INFO'
	    time=lasttime
            emptystrarr=numpy.array([''])
	    mytb.putcell('APP_PARAMS', rown, [''])
	    mytb.putcell('CLI_COMMAND',rown, [''])
	    mytb.putcell('APPLICATION',rown, appl)
	    mytb.putcell('MESSAGE',rown, msg)
	    mytb.putcell('OBSERVATION_ID',rown, -1)
	    mytb.putcell('ORIGIN',rown,origin)
	    mytb.putcell('PRIORITY',rown,priority)
	    mytb.putcell('TIME',rown, time)
	    rown += 1
	mytb.close()
Example #27
0
    def inspect_caltable(self, caltable):
        # caltable must exist
        self.assertTrue(os.path.exists(caltable))

        # caltable must be a directory
        self.assertTrue(os.path.isdir(caltable))

        # caltable must be opened by table tool
        (tb, ) = gentools(['tb'])
        is_open_successful = tb.open(caltable)
        self.assertTrue(is_open_successful)

        try:
            # caltype must be "G Jones"
            caltype = tb.getkeyword('VisCal')
            self.assertEqual(caltype, "SDGAIN_OTFD")
            # paramter must be Complex
            partype = tb.getkeyword('ParType')
            self.assertEqual(partype, 'Complex')
            self.assertIn('CPARAM', tb.colnames())
        finally:
            tb.close()
Example #28
0
 def inspect_caltable(self, caltable):
     # caltable must exist
     self.assertTrue(os.path.exists(caltable))
     
     # caltable must be a directory
     self.assertTrue(os.path.isdir(caltable))
     
     # caltable must be opened by table tool
     (tb,) = gentools(['tb'])
     is_open_successful = tb.open(caltable)
     self.assertTrue(is_open_successful)
     
     try:
         # caltype must be "G Jones"
         caltype = tb.getkeyword('VisCal')
         self.assertEqual(caltype, "G Jones")
         
         # paramter must be Complex
         partype = tb.getkeyword('ParType')
         self.assertEqual(partype, 'Complex')
         self.assertIn('CPARAM', tb.colnames())
     finally:
         tb.close()
Example #29
0
    def get_selection_idx_for_ms(self, file_idx):
        """
        Returns a dictionary of selection indices for i-th MS in infiles

        Argument: file idx in infiles list
        """
        if file_idx < len(self.infiles) and file_idx > -1:
            vis = self.infiles[file_idx]
            field = self.get_selection_param_for_ms(file_idx, self.field)
            spw = self.get_selection_param_for_ms(file_idx, self.spw)
            spw = self.__format_spw_string(spw)
            antenna = self.get_selection_param_for_ms(file_idx, self.antenna)
            if antenna == -1: antenna = ''
            scan = self.get_selection_param_for_ms(file_idx, self.scanno)
            intent = self.get_selection_param_for_ms(file_idx, self.intent) 
            my_ms = gentools(['ms'])[0]
            sel_ids = my_ms.msseltoindex(vis=vis, spw=spw, field=field,
                                         baseline=antenna, scan=scan)
            fieldid = list(sel_ids['field']) if len(sel_ids['field']) > 0 else -1
            baseline = self.format_ac_baseline(sel_ids['antenna1'])
            scanid = list(sel_ids['scan']) if len(sel_ids['scan']) > 0 else ""
            # SPW (need to get a list of valid spws instead of -1)
            if len(sel_ids['channel']) > 0:
                spwid = [ chanarr[0] for chanarr in sel_ids['channel'] ]
            elif spw=="": # No spw selection
                my_ms.open(vis)
                try: spwinfo =  my_ms.getspectralwindowinfo()
                except: raise
                finally: my_ms.close()
                
                spwid = [int(idx) for idx in spwinfo.keys()]
            else:
                raise RuntimeError("Invalid spw selction, %s ,for MS %d" (str(spw), file_idx))
            
            return {'field': fieldid, 'spw': spwid, 'baseline': baseline, 'scan': scanid, 'intent': intent, 'antenna1': sel_ids['antenna1']} 
        else:
            raise ValueError, ("Invalid file index, %d" % file_idx)
Example #30
0
import numpy
import os
import time, datetime
from taskinit import gentools, casalog
import sdutil
ms, sdms, tb = gentools(['ms','sdms','tb'])


from numpy import ma, array, logical_not, logical_and


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

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

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
Example #31
0
    def setSolarObjectJy(self,
                         field,
                         spw,
                         scalebychan,
                         timerange,
                         observation,
                         scan,
                         intent,
                         useephemdir,
                         usescratch=False):
        """
	Set flux density of a solar system object using Bryan Butler's new
	python model calculation code.
        A single time stamp (first time stamp of MS after selections are applied) is
        currently used per execution. For flux observation done in a long time span
        may need to run multiple setjy with selections by time range (or scans). 
	"""
        #retval = True
        output = {}
        cleanupcomps = True  # leave generated cl files

        #from taskinit import *
        from taskinit import gentools
        qa = casac.quanta()

        (myms, mytb, mycl, myme, mycb,
         mymsmd) = gentools(['ms', 'tb', 'cl', 'me', 'cb', 'msmd'])
        # prepare parameters need to pass to the Bryan's code
        # make ms selections
        # get source name
        # get time ranges
        # get spwused and get frequency ranges

        sel = {}
        sel['field'] = field
        sel['spw'] = spw
        sel['timerange'] = timerange
        sel['observation'] = str(observation)
        sel['scan'] = scan
        sel['scanintent'] = intent

        measframes = [
            'REST', 'LSRK', 'LSRD', 'BARY', 'GEO', 'TOPO', 'GALACTO', 'LGROUP',
            'CMB'
        ]
        myms.open(self.vis)
        myms.msselect(sel, False)
        scansummary = myms.getscansummary()
        nscan = len(scansummary.keys())
        selectedids = myms.msselectedindices()
        fieldids = selectedids['field']
        obsids = selectedids['observationid']
        myms.close()

        mytb.open(self.vis + '/OBSERVATION')
        if len(obsids) == 0:
            getrow = 0
        else:
            getrow = obsids[0]
        observatory = mytb.getcell('TELESCOPE_NAME', getrow)
        mytb.close()

        mytb.open(self.vis + '/FIELD')
        colnames = mytb.colnames()
        if len(fieldids) == 0:
            fieldids = range(mytb.nrows())
    # frame reference for field position
        phdir_info = mytb.getcolkeyword("PHASE_DIR", "MEASINFO")
        if phdir_info.has_key('Ref'):
            fieldref = phdir_info['Ref']
        elif phdir_info.has_key('TabRefTypes'):
            colnames = mytb.colnames()
            for col in colnames:
                if col == 'PhaseDir_Ref':
                    fieldrefind = mytb.getcell(col, fieldids[0])
                    fieldref = phdir_info['TabRefTypes'][fieldrefind]
        else:
            fieldref = ''
        srcnames = {}
        fielddirs = {}
        ftimes = {}
        ephemid = {}
        for fid in fieldids:
            #srcnames.append(mytb.getcell('NAME',int(fid)))
            srcnames[fid] = (mytb.getcell('NAME', int(fid)))
            #fielddirs[fid]=(mytb.getcell('PHASE_DIR',int(fid)))
            ftimes[fid] = (mytb.getcell('TIME', int(fid)))
            if colnames.count('EHPHEMERIS_ID'):
                ephemid[fid] = (mytb.getcell('EPHEMERIS_ID', int(fid)))
            else:
                ephemid[fid] = -1
        mytb.close()  # close FIELD table

        # need to get a list of time
        # but for now for test just get a time centroid of the all scans
        # get time range
        # Also, this needs to be done per source

        # gather freq info from Spw table
        mytb.open(self.vis + '/SPECTRAL_WINDOW')
        nspw = mytb.nrows()
        freqcol = mytb.getvarcol('CHAN_FREQ')
        freqwcol = mytb.getvarcol('CHAN_WIDTH')
        fmeasrefcol = mytb.getcol('MEAS_FREQ_REF')
        reffreqs = mytb.getcol('REF_FREQUENCY')
        mytb.close()

        # store all parameters need to call solar_system_fd for all sources
        inparams = {}
        validfids = [
        ]  # keep track of valid fid that has data (after selection)
        # if same source name ....need handle this...
        for fid in fieldids:
            sel['field'] = str(fid)
            myms.open(self.vis)
            #reset the selection
            try:
                status = myms.msselect(sel)
            except:
                #skip this field
                continue
            if not status:
                continue

            validfids.append(fid)
            trange = myms.range('time')
            #if not inparams.has_key(srcnames[fid]):
            #  inparams[srcnames[fid]]={}
            if not inparams.has_key(fid):
                inparams[fid] = {}
                inparams[fid]['fieldname'] = srcnames[fid]

            #tc = (trange['time'][0]+trange['time'][1])/2. #in sec.
            # use first timestamp to be consistent with the ALMA Control
            # old setjy (Butler-JPL-Horizons 2010) seems to be using
            # time in FIELD... but here is first selected time in main table
            if ephemid[fid] == -1:  # keep old behavior
                tc = trange['time'][0]  #in sec.
                tmsg = "Time used for the model calculation (=first time stamp of the selected data) for field "
            else:  # must have ephem table attached...
                tc = ftimes[fid]  #in sec.
                tmsg = "Time used for the model calculation for field "

            #if inparams[srcnames[fid]].has_key('mjd'):
            #  inparams[srcnames[fid]]['mjds'][0].append([myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']])
            #else:
            #  inparams[srcnames[fid]]['mjds']=[myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']]
            if inparams[fid].has_key('mjd'):
                inparams[fid]['mjds'][0].append(
                    [myme.epoch('utc', qa.quantity(tc, 's'))['m0']['value']])
            else:
                inparams[fid]['mjds'] = [
                    myme.epoch('utc', qa.quantity(tc, 's'))['m0']['value']
                ]
            usedtime = qa.time(qa.quantity(tc, 's'), form='ymd')[0]
            self._casalog.post(tmsg + str(fid) + ":" + usedtime)

            # get a correct direction measure
            mymsmd.open(self.vis)
            dirdic = mymsmd.phasecenter(int(fid))
            mymsmd.close()

            # check if frame is J2000 and if not do the transform
            if dirdic['refer'] != 'J2000':
                myme.doframe(myme.observatory(observatory))
                myme.doframe(myme.epoch('utc', qa.quantity(tc, 's')))
                azeldir = myme.measure(dirdic, 'AZELGEO')
                dirdic = myme.measure(azeldir, 'J2000')

            fielddirs[fid] = (numpy.array([[dirdic['m0']['value']],
                                           [dirdic['m1']['value']]]))

            # somehow it gives you duplicated ids .... so need to uniquify
            selspws = list(set(myms.msselectedindices()['spw']))
            # make sure it is int rather than numpy.int32, etc.
            selspws = [int(ispw) for ispw in selspws]
            #inparams[srcnames[fid]]['spwids']= selspws if len(selspws)!=0 else range(nspw)
            inparams[fid]['spwids'] = selspws if len(selspws) != 0 else range(
                nspw)

            #create a list of freq ranges with selected spws
            # should worry about freq order???
            freqlist = []
            freqlistraw = []
            framelist = []
            #for idx in inparams[srcnames[fid]]['spwids']:
            for idx in inparams[fid]['spwids']:
                freqs = freqcol['r' + str(idx + 1)]
                freqws = freqwcol['r' + str(idx + 1)]
                fmeasref = fmeasrefcol[idx]

                #if scalebychan=T, this has to be min, max determined from
                # chan_freq(channel center)+/- chan width.
                if scalebychan:
                    # pack into list of list of list (freqlist[nf][nspw])
                    perspwfreqlist = []
                    for nf in range(len(freqs)):
                        fl = freqs[nf][0] - freqws[nf][0] / 2.
                        fh = freqs[nf][0] + freqws[nf][0] / 2.
                        perspwfreqlist.append([fl, fh])
                    freqlist.append(perspwfreqlist)
                else:
                    if (len(freqs) == 1):
                        fl = freqs[0][0] - freqws[0][0] / 2.
                        fh = freqs[0][0] + freqws[0][0] / 2.
                        freqlist.append([float(fl), float(fh)])
                    else:
                        freqlist.append([float(min(freqs)), float(max(freqs))])

                framelist.append(measframes[fmeasref])
                freqlistraw.append(
                    freqs.transpose()[0].tolist())  # data chanfreq list
            #inparams[srcnames[fid]]['freqlist']=freqlist
            #inparams[srcnames[fid]]['freqlistraw']=freqlistraw
            #inparams[srcnames[fid]]['framelist']=framelist
            #inparams[srcnames[fid]]['reffreqs']=reffreqs

            inparams[fid]['freqlist'] = freqlist
            inparams[fid]['freqlistraw'] = freqlistraw
            inparams[fid]['framelist'] = framelist
            inparams[fid]['reffreqs'] = reffreqs
            myms.close()

        # call Bryan's code
        # errcode: list of list - inner list - each values for range of freqs
        # flluxes: list of list
        # fluxerrs:
        # size: [majoraxis, minoraxis, pa]
        # direction: direction for each time stamp
        #
        #import solar_system_setjy as ss_setjy
        import solar_system_setjy as SSsetjy
        retdict = {}  # for returning flux densities?
        ss_setjy = SSsetjy.solar_system_setjy()
        #for src in srcnames:
        self.clnamelist = []
        for vfid in validfids:
            src = srcnames[vfid]
            #mjds=inparams[src]['mjds']
            mjds = inparams[vfid]['mjds']
            fluxes = []
            # call solar_system_fd() per spw (for scalebychan freqlist has an extra dimention)
            #nspwused=len(inparams[src]['freqlist'])
            nspwused = len(inparams[vfid]['freqlist'])

            # warning for many channels but it is really depends on the source
            if scalebychan:
                maxnf = 0
                for ispw in range(nspwused):
                    #nf = len(inparams[src]['freqlist'][ispw])
                    nf = len(inparams[vfid]['freqlist'][ispw])
                    maxnf = max(nf, maxnf)
                if maxnf >= 3840 and src.upper(
                ) != "MARS":  # mars shoulde be ok
                    self._casalog.post(
                        "Processing %s spw(s) and at least some of them are a few 1000 channels or more. This may take \
                            many minutes (>3min per spw for 3840 channels) in some cases. Please be patient."
                        % nspwused, "WARN")

            for i in range(nspwused):  # corresponds to n spw
                if type(freqlist[0][0]) == list:
                    #infreqs=inparams[src]['freqlist'][i]
                    infreqs = inparams[vfid]['freqlist'][i]
                else:
                    #infreqs=[inparams[src]['freqlist'][i]]
                    infreqs = [inparams[vfid]['freqlist'][i]]
                self._casalog.post(
                    "Calling solar_system_fd: %s(%s) for spw%s freqs=%s" %
                    (src, vfid, i, freqlist[i]), 'DEBUG1')
                (errcodes, subfluxes, fluxerrs, sizes, dirs)=\
                          ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, observatory=observatory, casalog=self._casalog)
                # for old code
                #(errcodes, subfluxes, fluxerrs, sizes, dirs)=\
                #   ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, casalog=self._casalog)
                # for nf freq ranges, nt mjds
                # errcodes[nf][nt], subfluxes[nf][nt], fluxerrs[nf][nt], sizes[nt],  dirs[nt]
                self._casalog.post(
                    "+++++ solar_system_fd() returned values +++++", 'DEBUG1')
                self._casalog.post(" fluxes(fds)=%s" % subfluxes, 'DEBUG1')
                self._casalog.post(" sizes=%s" % sizes, 'DEBUG1')
                self._casalog.post(" directions=%s\n" % dirs, 'DEBUG1')

                # packed fluxes for all spws
                fluxes.append(subfluxes)
            # fluxes has fluxesi[nspw][nf][nt]

        # ------------------------------------------------------------------------
        # For testing with hardcoded values without calling solar_system_fd()...
        #errcodes=[[0,0,0,0,0]]
        #fluxes=[[26.40653147,65.23839313,65.23382757,65.80638802,69.33396562]]
        #fluxerrs=[[0.0,0.0,0.0,0.0,0.0]]
        #sizes=[[3.6228991032674371,3.6228991032674371,0.0]]
        #dirs=[{'m0': {'unit': 'rad', 'value': 0.0}, 'm1': {'unit': 'rad', 'value': 0.0}, 'refer': 'J2000', 'type': 'direction'}]
        # ------------------------------------------------------------------------
        # local params for selected src
        #framelist=inparams[src]['framelist']
        #freqlist=inparams[src]['freqlist']
        #freqlistraw=inparams[src]['freqlistraw']
        #reffreqs=inparams[src]['reffreqs']
        #spwids=inparams[src]['spwids']

            framelist = inparams[vfid]['framelist']
            freqlist = inparams[vfid]['freqlist']
            freqlistraw = inparams[vfid]['freqlistraw']
            reffreqs = inparams[vfid]['reffreqs']
            spwids = inparams[vfid]['spwids']

            clrecs = odict.odict()
            labels = []
            # loop for over for multiple directions (=multiple  MJDs) for a given src
            for i in range(
                    len(dirs)
            ):  # this is currently only length of 1 since no multiple timestamps were used
                # check errcode - error code would be there per flux per time (dir)
                reterr = testerrs(errcodes[i], src)
                if reterr == 2:
                    continue

                #dirstring = [dirs[i]['refer'],qa.tos(dirs[i]['m0']),qa.tos(dirs[i]['m1'])]
                if useephemdir:
                    dirstring = [
                        dirs[i]['refer'],
                        qa.tos(dirs[i]['m0']),
                        qa.tos(dirs[i]['m1'])
                    ]
                else:
                    #dirstring = [fieldref, str(fielddirs[fieldids[0]][0][0])+'rad', str(fielddirs[fieldids[0]][1][0])+'rad']
                    # extract field direction of first id of the selected field ids
                    #dirstring = [fieldref, "%.18frad" % (fielddirs[fieldids[0]][0][0]), "%.18frad" % (fielddirs[fieldids[0]][1][0])]
                    dirstring = [
                        fieldref,
                        "%.18frad" % (fielddirs[vfid][0][0]),
                        "%.18frad" % (fielddirs[vfid][1][0])
                    ]
            #print "dirstring=",dirstring

                # setup componentlists
                # need to set per dir
                # if scalebychan=F, len(freqs) corresponds to nspw selected
                # Need to put in for-loop to create cl for each time stamp? or scan?

                #clpath='/tmp/'
                clpath = './'
                # construct cl name (multiple spws in one componentlist table)
                selreffreqs = [reffreqs[indx] for indx in spwids]
                minfreq = min(selreffreqs)
                maxfreq = max(selreffreqs)
                freqlabel = '%.3f-%.3fGHz' % (minfreq / 1.e9, maxfreq / 1.e9)
                #tmlabel = '%.1fd' % (tc/86400.)
                mjd = inparams[vfid]['mjds'][0]
                tmlabel = '%.1fd' % (mjd)
                #clabel = src+'_spw'+str(spwids[j])+'_'+freqlabel+'_'+tmlabel
                #clabel0 = src+'_'+freqlabel+'_'+tmlabel
                #pid=str(os.getpid())
                #clname = clpath+clabel0+"_"+pid+'.cl'
                clabel0 = src + '_' + freqlabel + '_' + tmlabel + "_" + os.path.basename(
                    self.vis)
                clname = clpath + clabel0 + '.cl'
                #debug
                self._casalog.post(
                    "Create componentlist: %s for vfid=%s" % (clname, vfid),
                    'DEBUG1')

                if (os.path.exists(clname)):
                    shutil.rmtree(clname)

                iiflux = []
                iinfreq = []
                # for logging/output dict - pack each freq list for each spw
                freqsforlog = []
                for j in range(len(freqlist)):  # loop over nspw
                    freqlabel = '%.3fGHz' % (reffreqs[int(spwids[j])] / 1.e9)
                    clabel = src + '_spw' + str(
                        spwids[j]) + '_' + freqlabel + '_' + tmlabel
                    #print "addcomponent...for spw=",spwids[j]," i=",i," flux=",fluxes[j][i]
                    if scalebychan:
                        index = 2.0
                        sptype = 'spectral index'
                    else:
                        index = 0.0
                        sptype = 'constant'
            # adjust to change in returned flux shape. An extra [] now seems to be gone. 2012-09-27
                    iflux = fluxes[j][i][0]
                    #print "addcomponent with flux=%s at frequency=%s" %\
                    #                    (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz')

                    # i - time stamps = 0 for now, j = a freq range
                    infreq = freqlist[j][0][0] if type(
                        freqlist[j][0]) == list else freqlist[j][0]

                    # for a single CL with multple spws
                    if j == 0: infreq0 = infreq

                    # --- version for 'multi-spws in a single comp single CL'
                    # accumulate all frequencies from all spws to a list
                    #if not scalebychan:
                    #freqlist[j] for j spw should contain a list with two freqs (min and max of
                    # of the spw and the same fluxes should be set for the freqs so that cl.setspectrum
                    # with tabular would not interpolate inside each spw
                    ##iinfreq.extend(freqlist[j])
                    #  nch = len(freqlistraw[j])
                    #  iinfreq.extend(freqlistraw[j])
                    # assigning the same flux to the two freqs
                    ##iiflux.extend([iflux,iflux])
                    #  for ich in range(nch):
                    #    iiflux.extend([iflux])
                    #else:
                    #  iiflux.extend(fluxes[j][i])
                    #  if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
                    #	  for fr in freqlist[j]:
                    #	    iinfreq.append((fr[1]+fr[0])/2)
                    #  else:
                    #    if type(freqlist[j])==list:
                    #	    iinfreq.append((freqlist[j][1]+freqlist[j][0])/2)
                    #    else:
                    #      iinfreq.append(freqlist[j])
                    #  freqsforlog.append(iinfreq)
                    # -----------------------------------------------------------------------------------

                    self._casalog.post("addcomponent with flux=%s at frequency=%s" %\
              #                    (fluxes[j][i][0],str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')

                                        (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')

                    #
                    mycl.addcomponent(iflux,
                                      fluxunit='Jy',
                                      polarization="Stokes",
                                      dir=dirstring,
                                      shape='disk',
                                      majoraxis=str(sizes[i][0]) + 'arcsec',
                                      minoraxis=str(sizes[i][1]) + 'arcsec',
                                      positionangle=str(sizes[i][2]) + 'deg',
                                      freq=[framelist[0],
                                            str(infreq) + 'Hz'],
                                      spectrumtype=sptype,
                                      index=index,
                                      label=clabel)

                    if scalebychan:
                        # use tabular form to set flux for each channel

                        # This may be redundant
                        if type(fluxes[j][i]) == list and len(
                                fluxes[j][i]) > 1:
                            if type(freqlist[j][0]) == list and len(
                                    freqlist[j][0]) > 1:
                                freqs = []
                                for fr in freqlist[j]:
                                    freqs.append((fr[1] + fr[0]) / 2)
                                clind = mycl.length() - 1
                                mycl.setspectrum(which=clind,
                                                 type='tabular',
                                                 tabularfreqs=freqs,
                                                 tabularflux=fluxes[j][0],
                                                 tabularframe=framelist[j])
            ### === per spw process end here

            # - version that put all spws into a component -
            # set a single component freq set at the the lower edge of first spw
        #mycl.addcomponent(iiflux[0],fluxunit='Jy', polarization="Stokes", dir=dirstring,
        #		 shape='disk', majoraxis=str(sizes[i][0])+'arcsec', minoraxis=str(sizes[i][1])+'arcsec',
        #	         positionangle=str(sizes[i][2])+'deg', freq=[framelist[0],str(infreq0)+'Hz'],
        #		 spectrumtype=sptype, index=index, label=clabel)
        # if it's list of fluxes try to put in tabular form
        #if type(fluxes[j][i]) ==list and len(fluxes[j][i])> 1:
        #  if len(iiflux)> 1:
        #print "framelist[j]=",framelist[j]
        #if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
        #  freqs=[]
        #  for fr in freqlist[j]:
        #    freqs.append((fr[1]+fr[0])/2)
        #else:
        #  freqs=freqlist[j]
        #clind = mycl.length() - 1
        #	mycl.setspectrum(which=clind, type='tabular', tabularfreqs=freqs, tabularflux=fluxes[j][0],
        #tabularframe=framelist[j])

        #    mycl.setspectrum(which=clind, type='tabular', tabularfreqs=iinfreq, tabularflux=iiflux,
        #                     tabularframe=framelist[0])
        #mycl.rename(clname)
        #put in a record for log output
        #clrecs[clabel] = mycl.torecord()

                clrecs[clabel0] = mycl.torecord()
                clrecs[clabel0]['allfluxinfo'] = fluxes
                clrecs[clabel0]['allfreqinfo'] = freqsforlog
                clrecs[clabel0]['spwids'] = spwids
                mycl.rename(clname)  #  - A CL per field
                mycl.close(False)  # False for not to send a warning message
                mycl.done()

                # if scratch=F check if the virtual model already exist
                # for the field if it is clear it.
                #  if j==0 and (not usescratch):
                #  mytb.open(self.vis, nomodify=False)
                #  kwds = mytb.getkeywords()
                #  modelkwd='definedmodel_field_'+str(vfid)
                #  if kwds.has_key(modelkwd):
                #    clmodname=kwds[modelkwd]
                #    mytb.removekeyword(clmodname)
                #    mytb.removekeyword(modelkwd)
                #  mytb.close()

                mycb.open(self.vis, addcorr=False, addmodel=False)
                mycb.delmod(otf=True, field=str(vfid), spw=spwids, scr=False)
                mycb.close()

                # finally, put the componentlist as model
                #tqlstr=''
                #if intent!='':
                #   tqlstr='any(STATE_ID=='+str(stateids.tolist())+')'
                # Currently still need to do per spw (see the comment below...)
                # assuming CL contains nrow = nspw components
                mycl.open(clname)
                clinrec = mycl.torecord()
                mycl.close(False)
                mycl.done()
                ncomp = clinrec['nelements']
                if ncomp != len(spwids):
                    raise Exception, "Inconsistency in generated componentlist...Please submit a bug report."
                for icomp in range(ncomp):
                    #self.im.selectvis(spw=spwids[icomp],field=field,observation=observation,time=timerange,intent=intent)
                    ok = self.im.selectvis(spw=spwids[icomp],
                                           field=vfid,
                                           observation=observation,
                                           time=timerange,
                                           intent=intent)
                    # for MMS, particular subms may not contain the particular spw, so skip for such a case
                    if ok:
                        newclinrec = {}
                        newclinrec['nelements'] = 1
                        newclinrec['component0'] = clinrec['component' +
                                                           str(icomp)]
                        mycl.fromrecord(newclinrec)
                        cdir = os.getcwd()
                        if os.access(cdir, os.W_OK):
                            tmpclpath = cdir + "/"
                        elif os.access("/tmp", os.W_OK):
                            tmpclpath = "/tmp/"
                        #tmpclpath=tmpclpath+"_tmp_setjyCLfile_"+pid
                        tmpclpath = tmpclpath + os.path.basename(
                            self.vis) + "_tmp_setjyCLfile"
                        self._casalog.post("tmpclpath=" + tmpclpath)
                        if os.path.exists(tmpclpath):
                            shutil.rmtree(tmpclpath)
                        mycl.rename(tmpclpath)
                        mycl.close(False)
                        self._casalog.post("Now tmpclpath=" + tmpclpath)
                        self.im.ft(complist=tmpclpath)

            # --------------------------------------------------------------------------------------------
            # Currently this does not work properly for some case. Need ft can handle multi-component CL
            # with the way to map which component apply to which spw
            # Unable when such functionality is implemented in im.ft()
                #self.im.selectvis(spw=spwids,field=field,observation=observation,time=timerange,intent=intent)
                #self.im.ft(complist=clname) <= e.g. im.ft(comprec) where comrec = {'spw-mapping':[...],comprecs}..
                # --------------------------------------------------------------------------------------------

                #debug: set locally saved 2010-version component list instead
                #cl2010='mod_setjy_spw0_Titan_230.543GHz55674.1d.cl'
                #print "Using complist=",cl2010
                #self.im.ft(complist=cl2010)

                #if cleanupcomps:
                # 		  shutil.rmtree(clname)
                #self.clnamelist.append(clname)
                self.clnamelist.append(clname)

            msg = "Using channel dependent " if scalebychan else "Using spw dependent "

            if clrecs == {}:
                raise Exception, "No componentlist is generated."
            self._casalog.post(msg + " flux densities")
            #self._reportoLog(clrecs,self._casalog)
            self._reportoLog2(clrecs, self._casalog)
            #self._updateHistory(clrecs,self.vis)
            self._updateHistory2(clrecs, self.vis)
            # dictionary of dictionary for each field
            retdict[vfid] = clrecs

    # ==== end of for loop over fields
    #output=self._makeRetFluxDict(retdict)
        output = self._makeRetFluxDict2(retdict)
        #return retval
        return output
Example #32
0
def splitant(filename, outprefix='', overwrite=False, getpt=True):
    """
    Split Measurement set by antenna name, save data as a scantables,
    and return a list of filename. Note that frequency reference frame
    is imported as it is in Measurement set.
    Notice this method can only be available from CASA.
    Prameter
       filename:    the name of Measurement set to be read.
       outprefix:   the prefix of output scantable name.
                    the names of output scantable will be
                    outprefix.antenna1, outprefix.antenna2, ....
                    If not specified, outprefix = filename is assumed.
       overwrite    If the file should be overwritten if it exists.
                    The default False is to return with warning
                    without writing the output. USE WITH CARE.
       getpt        Whether to import direction from MS/POINTING
                    table or not. Default is True (import direction).
    """
    # Import the table toolkit from CASA
    from taskinit import gentools
    from asap.scantable import is_ms
    tb = gentools(['tb'])[0]
    # Check the input filename
    if isinstance(filename, str):
        import os.path
        filename = os.path.expandvars(filename)
        filename = os.path.expanduser(filename)
        if not os.path.exists(filename):
            s = "File '%s' not found." % (filename)
            raise IOError(s)
        # check if input file is MS
        if not is_ms(filename):
            s = "File '%s' is not a Measurement set." % (filename)
            raise IOError(s)
    else:
        s = "The filename should be string. "
        raise TypeError(s)
    # Check out put file name
    outname = ''
    if len(outprefix) > 0: prefix = outprefix + '.'
    else:
        prefix = filename.rstrip('/')
    # Now do the actual splitting.
    outfiles = []
    tb.open(tablename=filename, nomodify=True)
    ant1 = tb.getcol('ANTENNA1', 0, -1, 1)
    anttab = tb.getkeyword('ANTENNA').lstrip('Table: ')
    tb.close()
    tb.open(tablename=anttab, nomodify=True)
    nant = tb.nrows()
    antnames = tb.getcol('NAME', 0, nant, 1)
    tb.close()
    for antid in set(ant1):
        scan = scantable(filename,
                         average=False,
                         antenna=int(antid),
                         getpt=getpt)
        outname = prefix + antnames[antid] + '.asap'
        scan.save(outname, format='ASAP', overwrite=overwrite)
        del scan
        outfiles.append(outname)
    return outfiles
Example #33
0
def sdgaincal(infile=None, calmode=None, radius=None, smooth=None, 
              antenna=None, field=None, spw=None, scan=None, intent=None, 
              applytable=None, outfile='', overwrite=False): 
    
    casalog.origin('sdgaincal')
    
    # Calibrator tool
    (mycb,) = gentools(['cb'])

    try:
        # outfile must be specified
        if (outfile == '') or not isinstance(outfile, str):
            raise ValueError, "outfile is empty."
        
        # overwrite check
        if os.path.exists(outfile) and not overwrite:
            raise RuntimeError(outfile + ' exists.')
        
        # TODO: pre-application functionality is not supported yet
        if type(applytable) == types.ListType or \
          (isinstance(applytable, str) and len(applytable) > 0):
            msg = 'Pre-application of calibration solutions is not supported yet.'
            casalog.post(msg, priority='SEVERE')
            raise RuntimeError(msg)
        
        # open MS
        if isinstance(infile, str) and os.path.exists(infile):
            mycb.open(filename=infile, compress=False, addcorr=False, addmodel=False)
        else:
            raise RuntimeError, 'infile not found - please verify the name'
        
        # select data
        if isinstance(antenna, str) and len(antenna) > 0:
            baseline = '{ant}&&&'.format(ant=antenna)
        else:
            baseline = ''
        mycb.selectvis(spw=spw, scan=scan, field=field, intent=intent, baseline=baseline)
        
        # set solve
        if calmode == 'doublecircle':
            if radius is None:
                raise RuntimeError('radius must be specified.')
            elif not isinstance(radius, str):
                rcenter = '%sarcsec'%(radius)
            else:
                try:
                    # if radius is a string only consists of numeric value without unit, 
                    # it will succeed.
                    rcenter = '%sarcsec'%(float(radius))
                except:
                    # if the above fails, it may indicate that the string contains unit
                    rcenter = radius
            mycb.setsolve(type='SDGAIN_OTFD', table=outfile, radius=rcenter, smooth=smooth)
        else:
            raise RuntimeError('Unknown calibration mode: \'{mode}\''.format(mode=calmode))

        # solve
        mycb.solve()

        ## reporting calibration solution
        #reportsolvestats(mycb.activityrec());

    except Exception, e:
        import traceback
        casalog.post(traceback.format_exc(), priority='DEBUG')
        casalog.post(errmsg(e), priority='SEVERE')
        raise e
Example #34
0
def get_table_cache():
    (mytb, ) = gentools(['tb'])
    cache = mytb.showcache()
    #print 'cache = {}'.format(cache)
    return cache
Example #35
0
###############################
#
# FLS3a HI Reduction Script
# Frequency-Switched data
# Formatted for wide screen (150
# characters wide)
#
###############################
#
import time
import os

from taskinit import gentools

(myim, myia) = gentools(['im', 'ia'])

os.system('rm -rf FLS3a_HI.image FLS3a_HI.asap FLS3b_HI.asap FLS3a_calfs')

casapath=os.environ['CASAPATH'].split()[0]
datapath=casapath+'/data/regression/ATST5/FLS3/FLS3_all_newcal_SP'
#copystring='cp -r '+datapath+' .'
#os.system(copystring)

startTime=time.time()
startProc=time.clock()

# Project: AGBT02A_007_01
# Observation: GBT(1 antennas)
# 
#   Telescope Observation Date    Observer       Project
#   GBT       [                   4.57539e+09, 4.5754e+09]Lockman        AGBT02A_007_01
Example #36
0
 def __init__( self, filename ):
     self.filename = filename.rstrip('/')
     self.polno = None
     self.beamno = None
     self.table = gentools(['tb'])[0]
     self.table.open( self.filename, nomodify=False )
Example #37
0
from taskinit import casalog, gentools, qa
import pylab as pl
import numpy
import os
import contextlib

import asap as sd

tb = gentools(['tb'])[0]

def asdatestring(mjd, digit, timeonly=False):
    datedict = qa.splitdate(qa.quantity(mjd, 'd'))
    if digit > 10 : digit = 10
    sstr_tmp = str(numpy.round(datedict['s'], digit)).split('.')
    sstr = sstr_tmp[0] + '.' + sstr_tmp[1][0:digit]
    if timeonly:
        return '%s:%s:%s'%(datedict['hour'],datedict['min'],sstr)
    else:
        return '%s/%s/%s/%s:%s:%s'%(datedict['year'],datedict['month'],datedict['monthday'],datedict['hour'],datedict['min'],sstr)

def astimerange(tmargin, mjd0, mjd1):
    digit = numpy.abs(int(numpy.floor(numpy.log10(tmargin))))
    if int(mjd0) == int(mjd1): # same date, different time
        timeonly=True
    else: # different date
        timeonly=False
    return '%s~%s'%(asdatestring(mjd0,digit), asdatestring(mjd1,digit,timeonly=timeonly))

@contextlib.contextmanager
def selection_manager(scantab, original_selection, **kwargs):
    sel = sd.selector(original_selection)
Example #38
0
def sdgaincal(infile=None,
              calmode=None,
              radius=None,
              smooth=None,
              antenna=None,
              field=None,
              spw=None,
              scan=None,
              intent=None,
              applytable=None,
              interp=None,
              spwmap=None,
              outfile='',
              overwrite=False):

    casalog.origin('sdgaincal')

    # Calibrator tool
    (mycb, ) = gentools(['cb'])

    try:
        # outfile must be specified
        if (outfile == '') or not isinstance(outfile, str):
            raise ValueError, "outfile is empty."

        # overwrite check
        if os.path.exists(outfile) and not overwrite:
            raise RuntimeError(outfile + ' exists.')

        # open MS
        if isinstance(infile, str) and os.path.exists(infile):
            #mycb.setvi(old=True)
            mycb.open(filename=infile,
                      compress=False,
                      addcorr=False,
                      addmodel=False)
        else:
            raise RuntimeError, 'infile not found - please verify the name'

        # select data
        if isinstance(antenna, str) and len(antenna) > 0:
            baseline = '{ant}&&&'.format(ant=antenna)
        else:
            baseline = ''
        mycb.selectvis(spw=spw,
                       scan=scan,
                       field=field,
                       intent=intent,
                       baseline=baseline)

        # set apply
        casalog.post('interp="{0}" spwmap={1}'.format(interp, spwmap))
        if isinstance(applytable, str):
            if len(applytable) > 0:
                thisinterp = parse_interp(interp, 0)
                thisspwmap = parse_spwmap(spwmap, 0)
                casalog.post('thisinterp="{0}" thisspwmap={1}'.format(
                    thisinterp, thisspwmap))
                mycb.setapply(table=applytable,
                              interp=thisinterp,
                              spwmap=thisspwmap)
        elif hasattr(applytable, '__iter__'):
            # list type
            for i in xrange(len(applytable)):
                table = applytable[i]
                if isinstance(table, str) and len(table) > 0:
                    thisinterp = parse_interp(interp, i)
                    thisspwmap = parse_spwmap(spwmap, i)
                    casalog.post('thisinterp="{0}" thisspwmap={1}'.format(
                        thisinterp, thisspwmap))
                    mycb.setapply(table=table,
                                  interp=thisinterp,
                                  spwmap=thisspwmap)
                else:
                    RuntimeError, 'wrong type of applytable item ({0}). it should be string'.format(
                        type(table))
        else:
            raise RuntimeError, 'wrong type of applytable ({0}). it should be string or list'.format(
                type(applytable))

        # set solve
        if calmode == 'doublecircle':
            if radius is None:
                raise RuntimeError('radius must be specified.')
            elif not isinstance(radius, str):
                rcenter = '%sarcsec' % (radius)
            else:
                try:
                    # if radius is a string only consists of numeric value without unit,
                    # it will succeed.
                    rcenter = '%sarcsec' % (float(radius))
                except:
                    # if the above fails, it may indicate that the string contains unit
                    rcenter = radius
            mycb.setsolve(type='SDGAIN_OTFD',
                          table=outfile,
                          radius=rcenter,
                          smooth=smooth)
        else:
            raise RuntimeError(
                'Unknown calibration mode: \'{mode}\''.format(mode=calmode))

        # solve
        mycb.solve()

        ## reporting calibration solution
        #reportsolvestats(mycb.activityrec());

    except Exception, e:
        import traceback
        casalog.post(traceback.format_exc(), priority='DEBUG')
        casalog.post(errmsg(e), priority='SEVERE')
        raise e
Example #39
0
import numpy
import os
import time, datetime
from taskinit import gentools, casalog
import sdutil
ms, sdms, tb = gentools(['ms','sdms','tb'])


from numpy import ma, array, logical_not, logical_and


@sdutil.sdtask_decorator
def sdfit(infile=None, datacolumn=None, antenna=None, field=None, spw=None,
           timerange=None, scan=None, pol=None, intent=None,
           timebin=None, timespan=None,
           polaverage=None, 
           fitfunc=None, fitmode=None, nfit=None, thresh=None, avg_limit=None,
           minwidth=None, edge=None, outfile=None, overwrite=None):
    casalog.origin('sdfit')

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

        selection = ms.msseltoindex(vis=infile, spw=spw, field=field, 
Example #40
0
    def compile(self):
        # imaging mode
        self.imager_param['mode'] = self.mode

        # Work on selection of the first table in sorted list
        # to get default restfreq and outframe
        imhelper = cleanhelper(self.imager, self.infiles, casalog=casalog)
        imhelper.sortvislist(self.spw, self.mode, self.width)
        self.sorted_idx = imhelper.sortedvisindx
        selection_ids = self.get_selection_idx_for_ms(self.sorted_idx[0])
        self.__update_subtable_name(self.infiles[self.sorted_idx[0]])
        # field
        fieldid = selection_ids['field'][0] if type(
            selection_ids['field']) != int else selection_ids['field']
        sourceid = -1
        self.open_table(self.field_table)
        source_ids = self.table.getcol('SOURCE_ID')
        self.close_table()
        if self.field == '' or fieldid == -1:
            sourceid = source_ids[0]
        elif fieldid >= 0 and fieldid < len(source_ids):
            sourceid = source_ids[fieldid]
        else:
            raise ValueError, "No valid field in the first MS."

        # restfreq
        if self.restfreq == '' and self.source_table != '':
            self.open_table(self.source_table)
            source_ids = self.table.getcol('SOURCE_ID')
            for i in range(self.table.nrows()):
                if sourceid == source_ids[i] \
                       and self.table.iscelldefined('REST_FREQUENCY',i) \
                       and (selection_ids['spw'] == -1 or \
                            self.table.getcell('SPECTRAL_WINDOW_ID', i) in selection_ids['spw']):
                    rf = self.table.getcell('REST_FREQUENCY', i)
                    if len(rf) > 0:
                        self.restfreq = self.table.getcell(
                            'REST_FREQUENCY', i)[0]
                        break
            self.close_table()
            casalog.post("restfreq set to %s" % self.restfreq, "INFO")
        # REST_FREQUENCY column is optional (need retry if not exists)
        self.imager_param['restfreq'] = self.restfreq

        #
        # spw (define representative spw id = spwid_ref)
        spwid_ref = selection_ids['spw'][0] if type(
            selection_ids['spw']) != int else selection_ids['spw']
        # Invalid spw selection should have handled at msselectiontoindex().
        # -1 means all spw are selected.
        self.open_table(self.spw_table)
        if spwid_ref < 0:
            for id in range(self.table.nrows()):
                if self.table.getcell('NUM_CHAN', id) > 0:
                    spwid_ref = id
                    break
            if spwid_ref < 0:
                self.close_table()
                msg = 'No valid spw id exists in the first table'
                raise ValueError, msg
        self.allchannels = self.table.getcell('NUM_CHAN', spwid_ref)
        freq_chan0 = self.table.getcell('CHAN_FREQ', spwid_ref)[0]
        freq_inc0 = self.table.getcell('CHAN_WIDTH', spwid_ref)[0]
        # in case rest frequency is not defined yet.
        if self.restfreq == '':
            self.restfreq = '%fHz' % self.table.getcell(
                'CHAN_FREQ', spwid_ref).mean()
            self.imager_param['restfreq'] = self.restfreq
            casalog.post(
                "Using mean freq of spw %d as restfreq: %s" %
                (spwid_ref, self.restfreq), "INFO")
        self.close_table()
        self.imager_param['spw'] = -1  #spwid_ref

        # outframe (force using the current frame)
        self.imager_param['outframe'] = self.outframe
        if self.outframe == '':
            if len(self.infiles) > 1:
                # The default will be 'LSRK'
                casalog.post(
                    "Multiple MS inputs. The default outframe is set to 'LSRK'"
                )
                self.imager_param['outframe'] = 'LSRK'
            else:
                # get from MS
                my_ms = gentools(['ms'])[0]
                my_ms.open(self.infiles[0])
                spwinfo = my_ms.getspectralwindowinfo()
                my_ms.close()
                del my_ms
                for key, spwval in spwinfo.items():
                    if spwval['SpectralWindowId'] == spwid_ref:
                        self.imager_param['outframe'] = spwval['Frame']
                        casalog.post("Using frequency frame of MS, '%s'" %
                                     self.imager_param['outframe'])
                        break
            if self.imager_param['outframe'] == '':
                raise Exception, "Internal error of getting frequency frame of spw=%d." % spwid_ref
        else:
            casalog.post("Using frequency frame defined by user, '%s'" %
                         self.imager_param['outframe'])
Example #41
0
class sdimaging_worker(sdutil.sdtask_template_imaging):
    def __init__(self, **kwargs):
        super(sdimaging_worker, self).__init__(**kwargs)
        self.imager_param = {}
        self.sorted_idx = []

    def parameter_check(self):
        # outfile check
        sdutil.assert_outfile_canoverwrite_or_nonexistent(
            self.outfile, 'im', self.overwrite)
        sdutil.assert_outfile_canoverwrite_or_nonexistent(
            self.outfile + '.weight', 'im', self.overwrite)
        # fix spw
        if type(self.spw) == str:
            self.spw = self.__format_spw_string(self.spw)
        # check unit of start and width
        # fix default
        if self.mode == 'channel':
            if self.start == '': self.start = 0
            if self.width == '': self.width = 1
        else:
            if self.start == 0: self.start = ''
            if self.width == 1: self.width = ''
        # fix unit
        if self.mode == 'frequency':
            myunit = 'Hz'
        elif self.mode == 'velocity':
            myunit = 'km/s'
        else:  # channel
            myunit = ''

        for name in ['start', 'width']:
            param = getattr(self, name)
            new_param = self.__format_quantum_unit(param, myunit)
            if new_param == None:
                raise ValueError, "Invalid unit for %s in mode %s: %s" % \
                      (name, self.mode, param)
            setattr(self, name, new_param)

        casalog.post("mode='%s': start=%s, width=%s, nchan=%d" % \
                     (self.mode, self.start, self.width, self.nchan))

        # check length of selection parameters
        if is_string_type(self.infiles):
            nfile = 1
            self.infiles = [self.infiles]
        else:
            nfile = len(self.infiles)

        for name in ['field', 'spw', 'antenna', 'scanno']:
            param = getattr(self, name)
            if not self.__check_selection_length(param, nfile):
                raise ValueError, "Length of %s != infiles." % (name)
        # set convsupport default
        if self.convsupport >= 0 and self.gridfunction.upper() != 'SF':
            casalog.post("user defined convsupport is ignored for %s kernel" %
                         self.gridfunction,
                         priority='WARN')
            self.convsupport = -1

    def __format_spw_string(self, spw):
        """
        Returns formatted spw selection string which is accepted by imager.
        """
        if type(spw) != str:
            raise ValueError, "The parameter should be string."
        if spw.strip() == '*': spw = ''
        # WORKAROUND for CAS-6422, i.e., ":X~Y" fails while "*:X~Y" works.
        if spw.startswith(":"): spw = '*' + spw
        return spw

    def __format_quantum_unit(self, data, unit):
        """
        Returns False if data has an unit which in not a variation of
        input unit.
        Otherwise, returns input data as a quantum string. The input
        unit is added to the return value if no unit is in data.
        """
        my_qa = qatool()
        if data == '' or my_qa.compare(data, unit):
            return data
        if my_qa.getunit(data) == '':
            casalog.post("No unit specified. Using '%s'" % unit)
            return '%f%s' % (data, unit)
        return None

    def __check_selection_length(self, data, nfile):
        """
        Returns true if data is either a string, an array with length
        1 or nfile
        """
        if not is_string_type(data) and len(data) not in [1, nfile]:
            return False
        return True

    def get_selection_param_for_ms(self, fileid, param):
        """
        Returns valid selection string for a certain ms

        Arguments
            fileid : file idx in infiles list
            param : string (array) selection value
        """
        if is_string_type(param):
            return param
        elif len(param) == 1:
            return param[0]
        else:
            return param[fileid]

    def get_selection_idx_for_ms(self, file_idx):
        """
        Returns a dictionary of selection indices for i-th MS in infiles

        Argument: file idx in infiles list
        """
        if file_idx < len(self.infiles) and file_idx > -1:
            vis = self.infiles[file_idx]
            field = self.get_selection_param_for_ms(file_idx, self.field)
            spw = self.get_selection_param_for_ms(file_idx, self.spw)
            spw = self.__format_spw_string(spw)
            antenna = self.get_selection_param_for_ms(file_idx, self.antenna)
            if antenna == -1: antenna = ''
            scan = self.get_selection_param_for_ms(file_idx, self.scanno)
            intent = self.get_selection_param_for_ms(file_idx, self.intent)
            my_ms = gentools(['ms'])[0]
            sel_ids = my_ms.msseltoindex(vis=vis,
                                         spw=spw,
                                         field=field,
                                         baseline=antenna,
                                         scan=scan)
            fieldid = list(
                sel_ids['field']) if len(sel_ids['field']) > 0 else -1
            baseline = self.format_ac_baseline(sel_ids['antenna1'])
            scanid = list(sel_ids['scan']) if len(sel_ids['scan']) > 0 else ""
            # SPW (need to get a list of valid spws instead of -1)
            if len(sel_ids['channel']) > 0:
                spwid = [chanarr[0] for chanarr in sel_ids['channel']]
            elif spw == "":  # No spw selection
                my_ms.open(vis)
                try:
                    spwinfo = my_ms.getspectralwindowinfo()
                except:
                    raise
                finally:
                    my_ms.close()

                spwid = [int(idx) for idx in spwinfo.keys()]
            else:
                raise RuntimeError("Invalid spw selction, %s ,for MS %d" (
                    str(spw), file_idx))

            return {
                'field': fieldid,
                'spw': spwid,
                'baseline': baseline,
                'scan': scanid,
                'intent': intent,
                'antenna1': sel_ids['antenna1']
            }
        else:
            raise ValueError, ("Invalid file index, %d" % file_idx)

    def format_ac_baseline(self, in_antenna):
        """ format auto-correlation baseline string from antenna idx list """
        # exact match string
        if is_string_type(in_antenna):
            if (len(in_antenna) != 0) and (in_antenna.find('&') == -1) \
                   and (in_antenna.find(';')==-1):
                in_antenna = + '&&&'
            return in_antenna
        # single integer -> list of int
        if type(in_antenna) == int:
            if in_antenna >= 0:
                in_antenna = [in_antenna]
            else:
                return -1
        # format auto-corr string from antenna idices.
        baseline = ''
        for idx in in_antenna:
            if len(baseline) > 0: baseline += ';'
            if idx >= 0:
                baseline += (str(idx) + '&&&')
        return baseline

    def compile(self):
        # imaging mode
        self.imager_param['mode'] = self.mode

        # Work on selection of the first table in sorted list
        # to get default restfreq and outframe
        imhelper = cleanhelper(self.imager, self.infiles, casalog=casalog)
        imhelper.sortvislist(self.spw, self.mode, self.width)
        self.sorted_idx = imhelper.sortedvisindx
        selection_ids = self.get_selection_idx_for_ms(self.sorted_idx[0])
        self.__update_subtable_name(self.infiles[self.sorted_idx[0]])
        # field
        fieldid = selection_ids['field'][0] if type(
            selection_ids['field']) != int else selection_ids['field']
        sourceid = -1
        self.open_table(self.field_table)
        source_ids = self.table.getcol('SOURCE_ID')
        self.close_table()
        if self.field == '' or fieldid == -1:
            sourceid = source_ids[0]
        elif fieldid >= 0 and fieldid < len(source_ids):
            sourceid = source_ids[fieldid]
        else:
            raise ValueError, "No valid field in the first MS."

        # restfreq
        if self.restfreq == '' and self.source_table != '':
            self.open_table(self.source_table)
            source_ids = self.table.getcol('SOURCE_ID')
            for i in range(self.table.nrows()):
                if sourceid == source_ids[i] \
                       and self.table.iscelldefined('REST_FREQUENCY',i) \
                       and (selection_ids['spw'] == -1 or \
                            self.table.getcell('SPECTRAL_WINDOW_ID', i) in selection_ids['spw']):
                    rf = self.table.getcell('REST_FREQUENCY', i)
                    if len(rf) > 0:
                        self.restfreq = self.table.getcell(
                            'REST_FREQUENCY', i)[0]
                        break
            self.close_table()
            casalog.post("restfreq set to %s" % self.restfreq, "INFO")
        # REST_FREQUENCY column is optional (need retry if not exists)
        self.imager_param['restfreq'] = self.restfreq

        #
        # spw (define representative spw id = spwid_ref)
        spwid_ref = selection_ids['spw'][0] if type(
            selection_ids['spw']) != int else selection_ids['spw']
        # Invalid spw selection should have handled at msselectiontoindex().
        # -1 means all spw are selected.
        self.open_table(self.spw_table)
        if spwid_ref < 0:
            for id in range(self.table.nrows()):
                if self.table.getcell('NUM_CHAN', id) > 0:
                    spwid_ref = id
                    break
            if spwid_ref < 0:
                self.close_table()
                msg = 'No valid spw id exists in the first table'
                raise ValueError, msg
        self.allchannels = self.table.getcell('NUM_CHAN', spwid_ref)
        freq_chan0 = self.table.getcell('CHAN_FREQ', spwid_ref)[0]
        freq_inc0 = self.table.getcell('CHAN_WIDTH', spwid_ref)[0]
        # in case rest frequency is not defined yet.
        if self.restfreq == '':
            self.restfreq = '%fHz' % self.table.getcell(
                'CHAN_FREQ', spwid_ref).mean()
            self.imager_param['restfreq'] = self.restfreq
            casalog.post(
                "Using mean freq of spw %d as restfreq: %s" %
                (spwid_ref, self.restfreq), "INFO")
        self.close_table()
        self.imager_param['spw'] = -1  #spwid_ref

        # outframe (force using the current frame)
        self.imager_param['outframe'] = self.outframe
        if self.outframe == '':
            if len(self.infiles) > 1:
                # The default will be 'LSRK'
                casalog.post(
                    "Multiple MS inputs. The default outframe is set to 'LSRK'"
                )
                self.imager_param['outframe'] = 'LSRK'
            else:
                # get from MS
                my_ms = gentools(['ms'])[0]
                my_ms.open(self.infiles[0])
                spwinfo = my_ms.getspectralwindowinfo()
                my_ms.close()
                del my_ms
                for key, spwval in spwinfo.items():
                    if spwval['SpectralWindowId'] == spwid_ref:
                        self.imager_param['outframe'] = spwval['Frame']
                        casalog.post("Using frequency frame of MS, '%s'" %
                                     self.imager_param['outframe'])
                        break
            if self.imager_param['outframe'] == '':
                raise Exception, "Internal error of getting frequency frame of spw=%d." % spwid_ref
        else:
            casalog.post("Using frequency frame defined by user, '%s'" %
                         self.imager_param['outframe'])

#         # antenna
#         in_antenna = self.antenna # backup for future use
#         if type(self.antenna)==int:
#             if self.antenna >= 0:
#                 self.antenna=str(self.antenna)+'&&&'
#         else:
#             if (len(self.antenna) != 0) and (self.antenna.find('&') == -1) \
#                    and (self.antenna.find(';')==-1):
#                 self.antenna = self.antenna + '&&&'

    def _configure_map_property(self):
        selection_ids = self.get_selection_idx_for_ms(self.sorted_idx[0])

        # stokes
        if self.stokes == '':
            self.stokes = 'I'
        self.imager_param['stokes'] = self.stokes

        # gridfunction

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

        # cell
        cell = self.cell
        if cell == '' or cell[0] == '':
            # Calc PB
            grid_factor = 3.
            casalog.post(
                "The cell size will be calculated using PB size of antennas in the first MS"
            )
            qPB = self._calc_PB(selection_ids['antenna1'])
            cell = '%f%s' % (qPB['value'] / grid_factor, qPB['unit'])
            casalog.post("Using cell size = PB/%4.2F = %s" %
                         (grid_factor, cell))

        (cellx, celly) = sdutil.get_cellx_celly(cell, unit='arcmin')
        self.imager_param['cellx'] = cellx
        self.imager_param['celly'] = celly

        # Calculate Pointing center and extent (if necessary)
        # return a dictionary with keys 'center', 'width', 'height'
        #imsize = self.imsize
        imsize = sdutil._to_list(self.imsize, int) or \
            sdutil._to_list(self.imsize, numpy.integer)
        if imsize is None:
            imsize = self.imsize if hasattr(self.imsize,
                                            '__iter__') else [self.imsize]
            imsize = [int(numpy.ceil(v)) for v in imsize]
            casalog.post(
                "imsize is not integers. force converting to integer pixel numbers.",
                priority="WARN")
            casalog.post("rounded-up imsize: %s --> %s" %
                         (str(self.imsize), str(imsize)))

        phasecenter = self.phasecenter
        if self.phasecenter == "" or \
               len(imsize) == 0 or imsize[0] < 1:
            map_param = self._get_pointing_extent()
            # imsize
            if len(imsize) == 0 or imsize[0] < 1:
                imsize = self._get_imsize(map_param['width'],
                                          map_param['height'], cellx, celly)
                if self.phasecenter != "":
                    casalog.post(
                        "You defined phasecenter but not imsize. The image will cover as wide area as pointing in MS extends, but be centered at phasecenter. This could result in a strange image if your phasecenter is a part from the center of pointings",
                        priority='WARN')
                if imsize[0] > 1024 or imsize[1] > 1024:
                    casalog.post(
                        "The calculated image pixel number is larger than 1024. It could take time to generate the image depending on your computer resource. Please wait...",
                        priority='WARN')

            # phasecenter
            # if empty, it should be determined here...
            if self.phasecenter == "":
                phasecenter = map_param['center']

        # imsize
        (nx, ny) = sdutil.get_nx_ny(imsize)
        self.imager_param['nx'] = nx
        self.imager_param['ny'] = ny

        # phasecenter
        self.imager_param['phasecenter'] = phasecenter

        self.imager_param['movingsource'] = self.ephemsrcname

        # channel map
        imhelper = cleanhelper(self.imager, self.infiles, casalog=casalog)
        imhelper.sortvislist(self.spw, self.mode, self.width)
        spwsel = str(',').join([str(spwid) for spwid in selection_ids['spw']])
        srestf = self.imager_param['restfreq'] if is_string_type(
            self.imager_param['restfreq']
        ) else "%fHz" % self.imager_param['restfreq']
        (imnchan, imstart, imwidth) = imhelper.setChannelizeDefault(
            self.mode, spwsel, self.field, self.nchan, self.start, self.width,
            self.imager_param['outframe'], self.veltype,
            self.imager_param['phasecenter'], srestf)
        del imhelper

        # start and width
        if self.mode == 'velocity':
            startval = [self.imager_param['outframe'], imstart]
            widthval = imwidth
        elif self.mode == 'frequency':
            startval = [self.imager_param['outframe'], imstart]
            widthval = imwidth
        else:  #self.mode==channel
            startval = int(self.start)
            widthval = int(self.width)

        if self.nchan < 0: self.nchan = self.allchannels
        self.imager_param['start'] = startval
        self.imager_param['step'] = widthval
        self.imager_param['nchan'] = imnchan  #self.nchan

    def execute(self):
        # imaging
        casalog.post("Start imaging...", "INFO")
        if len(self.infiles) == 1:
            self.open_imager(self.infiles[0])
            selection_ids = self.get_selection_idx_for_ms(0)
            spwsel = self.get_selection_param_for_ms(0, self.spw)
            if spwsel.strip() in ['', '*']: spwsel = selection_ids['spw']
            ### TODO: channel selection based on spw
            ok = self.imager.selectvis(
                field=selection_ids['field'],
                #spw=selection_ids['spw'],
                spw=spwsel,
                nchan=-1,
                start=0,
                step=1,
                baseline=selection_ids['baseline'],
                scan=selection_ids['scan'],
                intent=selection_ids['intent'])
            if not ok:
                raise ValueError, "Selection is empty: you may want to review this MS selection"
        else:
            self.close_imager()
            #self.sorted_idx.reverse()
            for idx in self.sorted_idx.__reversed__():
                name = self.infiles[idx]
                selection_ids = self.get_selection_idx_for_ms(idx)
                spwsel = self.get_selection_param_for_ms(idx, self.spw)
                if spwsel.strip() in ['', '*']: spwsel = selection_ids['spw']
                ### TODO: channel selection based on spw
                self.imager.selectvis(
                    vis=name,
                    field=selection_ids['field'],
                    #spw=selection_ids['spw'],
                    spw=spwsel,
                    nchan=-1,
                    start=0,
                    step=1,
                    baseline=selection_ids['baseline'],
                    scan=selection_ids['scan'],
                    intent=selection_ids['intent'])
                # need to do this
                self.is_imager_opened = True

        # it should be called after infiles are registered to imager
        self._configure_map_property()

        casalog.post(
            "Using phasecenter \"%s\"" % (self.imager_param['phasecenter']),
            "INFO")

        self.imager.defineimage(**self.imager_param)  #self.__get_param())
        self.imager.setoptions(ftmachine='sd', gridfunction=self.gridfunction)
        self.imager.setsdoptions(pointingcolumntouse=self.pointingcolumn,
                                 convsupport=self.convsupport,
                                 truncate=self.truncate,
                                 gwidth=self.gwidth,
                                 jwidth=self.jwidth,
                                 minweight=0.,
                                 clipminmax=self.clipminmax)
        self.imager.makeimage(type='singledish', image=self.outfile)
        weightfile = self.outfile + ".weight"
        self.imager.makeimage(type='coverage', image=weightfile)
        self.close_imager()

        if not os.path.exists(self.outfile):
            raise RuntimeError, "Failed to generate output image '%s'" % self.outfile
        if not os.path.exists(weightfile):
            raise RuntimeError, "Failed to generate weight image '%s'" % weightfile
        # Convert output images to proper output frame
        my_ia = gentools(['ia'])[0]
        my_ia.open(self.outfile)
        csys = my_ia.coordsys()
        csys.setconversiontype(spectral=csys.referencecode('spectra')[0])
        my_ia.setcoordsys(csys.torecord())

        my_ia.close()

        # Mask image pixels whose weight are smaller than minweight.
        # Weight image should have 0 weight for pixels below < minweight
        casalog.post("Start masking the map using minweight = %f" % \
                     self.minweight, "INFO")
        my_ia.open(weightfile)
        try:
            stat = my_ia.statistics(mask="'" + weightfile + "' > 0.0",
                                    robust=True)
            valid_pixels = stat['npts']
        except RuntimeError, e:
            if e.message.find('No valid data found.') >= 0:
                valid_pixels = [0]
            else:
                raise e
        if len(valid_pixels) == 0 or valid_pixels[0] == 0:
            my_ia.close()
            casalog.post(
                "All pixels weight zero. This indicates no data in MS is in image area. Mask will not be set. Please check your image parameters.",
                "WARN")
            return
        median_weight = stat['median'][0]
        weight_threshold = median_weight * self.minweight
        casalog.post("Median of weight in the map is %f" % median_weight, \
                     "INFO")
        casalog.post("Pixels in map with weight <= median(weight)*minweight = %f will be masked." % \
                     (weight_threshold),"INFO")
        ###Leaving the original logic to calculate the number of masked pixels via
        ###product of median of and min_weight (which i don't understand the logic)
        ### if one wanted to find how many pixel were masked one could easily count the
        ### number of pixels set to false
        ### e.g  after masking self.outfile below one could just do this
        ### nmasked_pixels=tb.calc('[select from "'+self.outfile+'"/mask0'+'"  giving [nfalse(PagedArray )]]')
        my_tb = gentools(['tb'])[0]
        nmask_pixels = 0
        nchan = stat['trc'][3] + 1
        casalog.filter('ERROR')  ### hide the useless message of tb.calc

        ### doing it by channel to make sure it does not go out of memory
        ####tab.calc try to load the whole chunk in ram
        for k in range(nchan):
            nmask_pixels += my_tb.calc('[select from "' + weightfile +
                                       '"  giving [ntrue(map[,,,' + str(k) +
                                       '] <=' +
                                       str(median_weight * self.minweight) +
                                       ')]]')['0'][0]
        casalog.filter()  ####set logging back to normal

        casalog.filter()  ####set logging back to normal
        imsize = numpy.product(my_ia.shape())
        my_ia.close()
        # Modify default mask
        my_ia.open(self.outfile)
        my_ia.calcmask("'%s'>%f" % (weightfile, weight_threshold),
                       asdefault=True)
        my_ia.close()
        masked_fraction = 100. * (
            1. - (imsize - nmask_pixels) / float(valid_pixels[0]))
        casalog.post("This amounts to %5.1f %% of the area with nonzero weight." % \
                    ( masked_fraction ),"INFO")
        casalog.post("The weight image '%s' is returned by this task, if the user wishes to assess the results in detail." \
                     % (weightfile), "INFO")

        # Calculate theoretical beam size
        casalog.post("Calculating image beam size.")
        if self.gridfunction.upper() not in ['SF']:
            casalog.post(
                "Beam size definition for '%s' kernel is experimental." %
                self.gridfunction,
                priority='WARN')
            casalog.post(
                "You may want to take careful look at the restoring beam in the image.",
                priority='WARN')
        my_msmd = gentools(['msmd'])[0]
        # antenna diameter and blockage
        ref_ms_idx = self.sorted_idx[0]
        ref_ms_name = self.infiles[ref_ms_idx]
        selection_ids = self.get_selection_idx_for_ms(ref_ms_idx)
        ant_idx = selection_ids['antenna1']
        diameter = self._get_average_antenna_diameter(ant_idx)
        my_msmd.open(ref_ms_name)
        ant_name = my_msmd.antennanames(ant_idx)
        my_msmd.close()
        is_alma = False
        for name in ant_name:
            if name[0:2] in ["PM", "DV", "DA", "CM"]:
                is_alma = True
                break
        blockage = "0.75m" if is_alma else "0.0m"  # unknown blockage diameter
        # output reference code
        my_ia.open(self.outfile)
        csys = my_ia.coordsys()
        my_ia.close()
        outref = csys.referencecode('direction')[0]
        cell = list(csys.increment(type='direction', format='s')['string'])
        # pointing sampling
        ref_ms_spw = self.get_selection_param_for_ms(ref_ms_idx, self.spw)
        ref_ms_field = self.get_selection_param_for_ms(ref_ms_idx, self.field)
        ref_ms_scan = self.get_selection_param_for_ms(ref_ms_idx, self.scanno)
        # xSampling, ySampling, angle = sdutil.get_ms_sampling_arcsec(ref_ms_name, spw=ref_ms_spw,
        #                                                             antenna=selection_ids['baseline'],
        #                                                             field=ref_ms_field,
        #                                                             scan=ref_ms_scan,#timerange='',
        #                                                             outref=outref)

        # obtain sampling interval for beam calculation.
        self.open_imager(ref_ms_name)
        ok = self.imager.selectvis(
            field=ref_ms_field,
            #spw=selection_ids['spw'],
            spw=ref_ms_spw,
            nchan=-1,
            start=0,
            step=1,
            baseline=selection_ids['baseline'],
            scan=ref_ms_scan,
            intent=selection_ids['intent'])
        if len(ant_idx) > 1:
            casalog.post(
                "Using only antenna %s to calculate sampling interval" %
                ant_name[0])
        ptg_samp = self.imager.pointingsampling(
            pattern='raster',
            ref=outref,
            movingsource=self.ephemsrcname,
            pointingcolumntouse=self.pointingcolumn,
            antenna=('%s&&&' % ant_name[0]))
        self.close_imager()
        my_qa = qatool()
        xSampling, ySampling = my_qa.getvalue(
            my_qa.convert(ptg_samp['sampling'], 'arcsec'))
        angle = my_qa.getvalue(my_qa.convert(ptg_samp['angle'], "deg"))[0]

        casalog.post("Detected raster sampling = [%f, %f] arcsec" %
                     (xSampling, ySampling))
        # handling of failed sampling detection
        valid_sampling = True
        sampling = [xSampling, ySampling]
        if abs(xSampling) < 2.2e-3 or not numpy.isfinite(xSampling):
            casalog.post(
                "Invalid sampling=%s arcsec. Using the value of orthogonal direction=%s arcsec"
                % (xSampling, ySampling),
                priority="WARN")
            sampling = [ySampling]
            angle = 0.0
            valid_sampling = False
        if abs(ySampling) < 1.0e-3 or not numpy.isfinite(ySampling):
            if valid_sampling:
                casalog.post(
                    "Invalid sampling=%s arcsec. Using the value of orthogonal direction=%s arcsec"
                    % (ySampling, xSampling),
                    priority="WARN")
                sampling = [xSampling]
                angle = 0.0
                valid_sampling = True
        # reduce sampling and cell if it's possible
        if len(sampling) > 1 and abs(sampling[0] -
                                     sampling[1]) <= 0.01 * abs(sampling[0]):
            sampling = [sampling[0]]
            angle = 0.0
            if cell[0] == cell[1]: cell = [cell[0]]
        if valid_sampling:
            # actual calculation of beam size
            bu = sdbeamutil.TheoreticalBeam()
            bu.set_antenna(diameter, blockage)
            bu.set_sampling(sampling, "%fdeg" % angle)
            bu.set_image_param(cell, self.restfreq, self.gridfunction,
                               self.convsupport, self.truncate, self.gwidth,
                               self.jwidth, is_alma)
            bu.summary()
            imbeam_dict = bu.get_beamsize_image()
            casalog.post("Setting image beam: major=%s, minor=%s, pa=%s" % (
                imbeam_dict['major'],
                imbeam_dict['minor'],
                imbeam_dict['pa'],
            ))
            # set beam size to image
            my_ia.open(self.outfile)
            my_ia.setrestoringbeam(**imbeam_dict)
            my_ia.close()
        else:
            #BOTH sampling was invalid
            casalog.post(
                "Could not detect valid raster sampling. Exitting without setting beam size to image",
                priority='WARN')
Example #42
0
    def execute(self):
        # imaging
        casalog.post("Start imaging...", "INFO")
        if len(self.infiles) == 1:
            self.open_imager(self.infiles[0])
            selection_ids = self.get_selection_idx_for_ms(0)
            spwsel = self.get_selection_param_for_ms(0, self.spw)
            if spwsel.strip() in ['', '*']: spwsel = selection_ids['spw']
            ### TODO: channel selection based on spw
            ok = self.imager.selectvis(
                field=selection_ids['field'],
                #spw=selection_ids['spw'],
                spw=spwsel,
                nchan=-1,
                start=0,
                step=1,
                baseline=selection_ids['baseline'],
                scan=selection_ids['scan'],
                intent=selection_ids['intent'])
            if not ok:
                raise ValueError, "Selection is empty: you may want to review this MS selection"
        else:
            self.close_imager()
            #self.sorted_idx.reverse()
            for idx in self.sorted_idx.__reversed__():
                name = self.infiles[idx]
                selection_ids = self.get_selection_idx_for_ms(idx)
                spwsel = self.get_selection_param_for_ms(idx, self.spw)
                if spwsel.strip() in ['', '*']: spwsel = selection_ids['spw']
                ### TODO: channel selection based on spw
                self.imager.selectvis(
                    vis=name,
                    field=selection_ids['field'],
                    #spw=selection_ids['spw'],
                    spw=spwsel,
                    nchan=-1,
                    start=0,
                    step=1,
                    baseline=selection_ids['baseline'],
                    scan=selection_ids['scan'],
                    intent=selection_ids['intent'])
                # need to do this
                self.is_imager_opened = True

        # it should be called after infiles are registered to imager
        self._configure_map_property()

        casalog.post(
            "Using phasecenter \"%s\"" % (self.imager_param['phasecenter']),
            "INFO")

        self.imager.defineimage(**self.imager_param)  #self.__get_param())
        self.imager.setoptions(ftmachine='sd', gridfunction=self.gridfunction)
        self.imager.setsdoptions(pointingcolumntouse=self.pointingcolumn,
                                 convsupport=self.convsupport,
                                 truncate=self.truncate,
                                 gwidth=self.gwidth,
                                 jwidth=self.jwidth,
                                 minweight=0.,
                                 clipminmax=self.clipminmax)
        self.imager.makeimage(type='singledish', image=self.outfile)
        weightfile = self.outfile + ".weight"
        self.imager.makeimage(type='coverage', image=weightfile)
        self.close_imager()

        if not os.path.exists(self.outfile):
            raise RuntimeError, "Failed to generate output image '%s'" % self.outfile
        if not os.path.exists(weightfile):
            raise RuntimeError, "Failed to generate weight image '%s'" % weightfile
        # Convert output images to proper output frame
        my_ia = gentools(['ia'])[0]
        my_ia.open(self.outfile)
        csys = my_ia.coordsys()
        csys.setconversiontype(spectral=csys.referencecode('spectra')[0])
        my_ia.setcoordsys(csys.torecord())

        my_ia.close()

        # Mask image pixels whose weight are smaller than minweight.
        # Weight image should have 0 weight for pixels below < minweight
        casalog.post("Start masking the map using minweight = %f" % \
                     self.minweight, "INFO")
        my_ia.open(weightfile)
        try:
            stat = my_ia.statistics(mask="'" + weightfile + "' > 0.0",
                                    robust=True)
            valid_pixels = stat['npts']
        except RuntimeError, e:
            if e.message.find('No valid data found.') >= 0:
                valid_pixels = [0]
            else:
                raise e
Example #43
0
import numpy
import os
from taskinit import gentools, casalog
import sdutil
from collections import Counter
ms,sdms,tb,msmd = gentools(['ms','sdms','tb', 'msmd'])

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

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

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

            sorttab_info = remove_sorted_table_keyword(infile)
Example #44
0
    def __execute_press(self):
        ###
        # Pressed-out method (Sofue & Reich 1979)
        ###
        casalog.post("Apply Pressed-out method")

        # CAS-5410 Use private tools inside task scripts
        ia = gentools(["ia"])[0]

        # mask
        self.image = ia.newimagefromimage(infile=self.infiles, outfile=self.tmpmskname)
        # back-up original mask name
        is_initial_mask = self.image.maskhandler("default")[0] != ""
        temp_maskname = "temporal"
        imshape = self.image.shape()
        nx = imshape[0]
        ny = imshape[1]
        nchan = imshape[2]
        if len(self.thresh) == 0:
            casalog.post("Use whole region")
        else:
            # mask pixels beyond thresholds
            maskstr = "mask('%s')" % self.tmpmskname
            if self.thresh[0] != self.nolimit:
                maskstr += " && '%s'>=%f" % (self.tmpmskname, self.thresh[0])
            if self.thresh[1] != self.nolimit:
                maskstr += " && '%s'<=%f" % (self.tmpmskname, self.thresh[1])
            # Need to flush to image once to calcmask ... sigh
            self.image.done()
            self.image = ia.newimage(self.tmpmskname)
            self.image.calcmask(mask=maskstr, name=temp_maskname, asdefault=True)

        # smoothing
        # bmajor = 0.0
        # bminor = 0.0
        # CAS-5410 Use private tools inside task scripts
        qa = qatool()
        if type(self.beamsize) == str:
            qbeamsize = qa.quantity(self.beamsize)
        else:
            qbeamsize = qa.quantity(self.beamsize, "arcsec")
        if type(self.smoothsize) == str:
            # bmajor = smoothsize
            # bminor = smoothsize
            qsmoothsize = qa.quantity(self.smoothsize)
        else:
            # bmajor = '%sarcsec' % (beamsize*smoothsize)
            # bminor = '%sarcsec' % (beamsize*smoothsize)
            qsmoothsize = qa.mul(qbeamsize, self.smoothsize)
        bmajor = qsmoothsize
        bminor = qsmoothsize
        # masked channels are replaced by zero and convolved here.
        self.convimage = self.image.convolve2d(outfile=self.tmpconvname, major=bmajor, minor=bminor, overwrite=True)
        self.convimage.done()
        self.convimage = ia.newimage(self.tmpconvname)

        # get dTij (original - smoothed)
        if len(imshape) == 4:
            # with polarization axis
            npol = imshape[3]
            for ichan in range(nchan):
                for ipol in range(npol):
                    pixmsk = self.image.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                    pixsmo = self.convimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                    pixsub = pixmsk - pixsmo
                    self.convimage.putchunk(pixsub, [0, 0, ichan, ipol])
        elif len(imshape) == 3:
            for ichan in range(nchan):
                # no polarization axis
                pixmsk = self.image.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                pixsmo = self.convimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                pixsub = pixmsk - pixsmo
                self.convimage.putchunk(pixsub, [0, 0, ichan])

        # polynomial fit
        fitaxis = 0
        if self.direction == 0.0:
            fitaxis = 0
        elif self.direction == 90.0:
            fitaxis = 1
        else:
            raise Exception, "Sorry, the task don't support inclined scan with respect to horizontal or vertical axis, right now."
        # Replace duplicated method ia.fitpolynomial with
        # ia.fitprofile
        # polyimage = convimage.fitpolynomial( fitfile=tmppolyname, axis=fitaxis, order=numpoly, overwrite=True )
        # polyimage.done()
        if os.path.exists(self.tmppolyname):
            # CAS-5410 Use private tools inside task scripts
            cu = utilstool()
            cu.removetable([self.tmppolyname])
        self.convimage.setbrightnessunit("K")
        # Unfortunately, ia.fitprofile is very fragile.
        # Using numpy instead for fitting with masked pixels (KS, 2014/07/02)
        # resultdic = self.convimage.fitprofile( model=self.tmppolyname, axis=fitaxis, poly=self.numpoly, ngauss=0, multifit=True, gmncomps=0 )
        self.__polynomial_fit_model(image=self.tmpmskname, model=self.tmppolyname, axis=fitaxis, order=self.numpoly)
        polyimage = ia.newimage(self.tmppolyname)
        # set back defalut mask (need to get from self.image)
        avail_mask = polyimage.maskhandler("get")
        if is_initial_mask:
            casalog.post("copying mask from %s" % (self.infiles))
            polyimage.calcmask("mask('%s')" % self.infiles, asdefault=True)
        else:  # no mask in the original image
            polyimage.calcmask("T", asdefault=True)
        if temp_maskname in avail_mask:
            polyimage.maskhandler("delete", name=temp_maskname)

        # subtract fitted image from original map
        imageorg = ia.newimage(self.infiles)
        if len(imshape) == 4:
            # with polarization axis
            npol = imshape[3]
            for ichan in range(nchan):
                for ipol in range(npol):
                    pixorg = imageorg.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                    pixpol = polyimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                    pixsub = pixorg - pixpol
                    polyimage.putchunk(pixsub, [0, 0, ichan, ipol])
        elif len(imshape) == 3:
            # no polarization axis
            for ichan in range(nchan):
                pixorg = imageorg.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                pixpol = polyimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                pixsub = pixorg - pixpol
                polyimage.putchunk(pixsub, [0, 0, ichan])

        # output
        polyimage.rename(self.outfile, overwrite=self.overwrite)

        polyimage.done()
        self.convimage.done(remove=True)
        self.image.done()
        imageorg.done()
Example #45
0
    def _updateHistory2(self, clrecs, vis):
        """
        Update history table when setSolarObjectJy is run
        (mulitple comopents in one version)
        """
        #
        from taskinit import gentools
        (mytb, ) = gentools(['tb'])
        mytb.open(vis + '/HISTORY', nomodify=False)
        nrow = mytb.nrows()
        lasttime = mytb.getcol('TIME')[nrow - 1]
        rown = nrow
        #
        clname = clrecs.keys()[0]

        #for ky in clrecs.keys():
        srcn = clname.split('_')[0]
        comprec = clrecs[clname]
        nelm = comprec['nelements']
        for icomp in range(nelm):
            comp = comprec['component' + str(icomp)]
            complab = comp['label']
            origin = 'setjy'
            priority = 'INFO'
            time = lasttime
            appl = 'setjy'
            emptystrarr = numpy.array([''])
            if nelm == 1:
                for i in range(len(comprec['spwids'])):
                    mytb.addrows(1)
                    ispw = comprec['spwids'][i]
                    iflux = comprec['allfluxinfo'][i][0][0]
                    msg = (
                        " %s: spw %s Flux:[I=%s,Q=%s,U=%s,V=%s] +/- [I=%s,Q=%s,U=%s,V=%s] Jy"
                        %
                        (srcn, ispw, iflux, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
                    mytb.putcell('APP_PARAMS', rown, [''])
                    mytb.putcell('CLI_COMMAND', rown, [''])
                    mytb.putcell('APPLICATION', rown, appl)
                    mytb.putcell('MESSAGE', rown, msg)
                    mytb.putcell('OBSERVATION_ID', rown, -1)
                    mytb.putcell('ORIGIN', rown, origin)
                    mytb.putcell('PRIORITY', rown, priority)
                    mytb.putcell('TIME', rown, time)
                    rown += 1

            else:
                ispw = complab.split('_')[1]
                mytb.addrows(1)
                msg = (
                    " %s: spw %s Flux:[I=%s,Q=%s,U=%s,V=%s] +/- [I=%s,Q=%s,U=%s,V=%s] Jy"
                    % (srcn, ispw, comp['flux']['value'][0],
                       comp['flux']['value'][1], comp['flux']['value'][2],
                       comp['flux']['value'][3], comp['flux']['error'][0],
                       comp['flux']['error'][1], comp['flux']['error'][2],
                       comp['flux']['error'][3]))

                mytb.putcell('APP_PARAMS', rown, [''])
                mytb.putcell('CLI_COMMAND', rown, [''])
                mytb.putcell('APPLICATION', rown, appl)
                mytb.putcell('MESSAGE', rown, msg)
                mytb.putcell('OBSERVATION_ID', rown, -1)
                mytb.putcell('ORIGIN', rown, origin)
                mytb.putcell('PRIORITY', rown, priority)
                mytb.putcell('TIME', rown, time)
                rown += 1
        mytb.close()
Example #46
0
jplfiles_to_repository() puts it all together, so it is most likely the
function you want.

There are various utilities like convert_radec, datestr*, get_num_from_str,
mean_radius*, and construct_tablepath defined in here as well.
"""

from glob import glob
import os
import re
import scipy.special
import time  # We can always use more time.

from taskinit import gentools, qa, casalog

me = gentools(["me"])[0]

from dict_to_table import dict_to_table

# Possible columns, as announced by their column titles.
# The data is scooped up by 'pat'.  Either use ONE group named by the column
# key, or mark it as unwanted.  '-' is not valid in group names.
# Leading and trailing whitespace will be taken care of later.
# Sample lines:
#  Date__(UT)__HR:MN     R.A.___(ICRF/J2000.0)___DEC Ob-lon Ob-lat Sl-lon Sl-lat   NP.ang   NP.dist               r        rdot            delta      deldot    S-T-O   L_s
#  2010-May-01 00:00     09 01 43.1966 +19 04 28.673 286.52  18.22 246.99  25.34 358.6230      3.44  1.661167637023  -0.5303431 1.28664311447968  15.7195833  37.3033   84.50
#
# some mod to label names and comments so that they corresponds to
# JPL-Horizons naming comvension
cols = {
    "MJD": {"header": r"Date__\(UT\)__HR:MN", "comment": "date", "pat": r"(?P<MJD>\d+-\w+-\d+ \d+:\d+)"},
Example #47
0
 def __init__(self, imagename):
     self.imagename = imagename
     # read data to storage
     ia = gentools(['ia'])[0]
     ia.open(self.imagename)
     try:
         self.image_shape = ia.shape()
         self.coordsys = ia.coordsys()
         coord_types = self.coordsys.axiscoordinatetypes()
         self.units = self.coordsys.units()
         self.names = self.coordsys.names()
         self.direction_reference = self.coordsys.referencecode('dir')[0]
         self.spectral_reference = self.coordsys.referencecode('spectral')[0]
         self.id_direction = coord_types.index('Direction')
         self.id_direction = [self.id_direction, self.id_direction+1]
         self.id_spectral = coord_types.index('Spectral')
         try:
             self.id_stokes = coord_types.index('Stokes')
             stokes_axis_exists = True
         except:
             # if no Stokes axis exists, set dummy axis at the end
             self.id_stokes = len(coord_types)
             stokes_axis_exists = False
         casalog.post('id_direction=%s'%(self.id_direction), priority='DEBUG')
         casalog.post('id_spectral=%s'%(self.id_spectral), priority='DEBUG')
         casalog.post('id_stokes=%s (%s stokes axis)'%(self.id_stokes,('real' if stokes_axis_exists else 'dummy')), 
                      priority='DEBUG')
         self.data = ia.getchunk()
         self.mask = ia.getchunk(getmask=True)
         if not stokes_axis_exists:
             # put degenerate Stokes axis at the end
             self.data = numpy.expand_dims(self.data, axis=-1)
             self.mask = numpy.expand_dims(self.mask, axis=-1)
             casalog.post('add dummy Stokes axis to data and mask since input image doesn\'t have it.')
             casalog.post('data.shape={}'.format(self.data.shape), priority='DEBUG')
             casalog.post('mask.shape={}'.format(self.mask.shape), priority='DEBUG')
         bottom = ia.toworld(numpy.zeros(len(self.image_shape),dtype=int), 'q')['quantity']
         top = ia.toworld(self.image_shape-1, 'q')['quantity']
         key = lambda x: '*%s'%(x+1)
         ra_min = bottom[key(self.id_direction[0])]
         ra_max = top[key(self.id_direction[0])]
         if ra_min > ra_max:
             ra_min,ra_max = ra_max,ra_min
         self.ra_min = ra_min
         self.ra_max = ra_max
         self.dec_min = bottom[key(self.id_direction[1])]
         self.dec_max = top[key(self.id_direction[1])]
         self._brightnessunit = ia.brightnessunit()
         if self.spectral_label == 'Frequency':
             refpix, refval, increment = self.spectral_axis(unit='GHz')
             self.spectral_data = numpy.array([refval+increment*(i-refpix) for i in xrange(self.nchan)])
         elif self.spectral_label == 'Velocity':
             refpix, refval, increment = self.spectral_axis(unit='km/s')
             self.spectral_data = numpy.array([refval+increment*(i-refpix) for i in xrange(self.nchan)])
         if stokes_axis_exists:
             self.stokes = self.coordsys.stokes()
         else:
             # if no Stokes axis exists, set ['I']
             self.stokes = ['I']
     finally:
         ia.close()
Example #48
0
    def __execute_basket_weaving(self):
        ###
        # Basket-Weaving (Emerson & Grave 1988)
        ###
        casalog.post("Apply Basket-Weaving")

        # CAS-5410 Use private tools inside task scripts
        ia = gentools(["ia"])[0]

        # initial setup
        outimage = ia.newimagefromimage(infile=self.infiles[0], outfile=self.outfile, overwrite=self.overwrite)
        imshape = outimage.shape()
        nx = imshape[0]
        ny = imshape[1]
        nchan = imshape[2]
        tmp = []
        nfile = len(self.infiles)
        for i in xrange(nfile):
            tmp.append(numpy.zeros(imshape, dtype=float))
        maskedpixel = numpy.array(tmp)
        del tmp

        # direction
        dirs = []
        if len(self.direction) == nfile:
            dirs = self.direction
        else:
            casalog.post("direction information is extrapolated.")
            for i in range(nfile):
                dirs.append(self.direction[i % len(direction)])

        # masklist
        masks = []
        if type(self.masklist) == float:
            for i in range(nfile):
                masks.append(self.masklist)
        elif type(self.masklist) == list and nfile != len(self.masklist):
            for i in range(nfile):
                masks.append(self.masklist[i % len(self.masklist)])
        for i in range(len(masks)):
            masks[i] = 0.01 * masks[i]

        # mask
        for i in range(nfile):
            self.realimage = ia.newimagefromimage(infile=self.infiles[i], outfile=self.tmprealname[i])
            self.imagimage = ia.newimagefromimage(infile=self.infiles[i], outfile=self.tmpimagname[i])
            # replace masked pixels with 0.0
            if not self.realimage.getchunk(getmask=True).all():
                casalog.post("Replacing masked pixels with 0.0 in %d-th image" % (i))
                self.realimage.replacemaskedpixels(0.0)
            self.realimage.close()
            self.imagimage.close()

        if len(self.thresh) == 0:
            casalog.post("Use whole region")
        else:
            if len(imshape) == 4:
                # with polarization axis
                npol = imshape[3]
                for i in range(nfile):
                    self.realimage = ia.newimage(self.tmprealname[i])
                    for ichan in range(nchan):
                        for ipol in range(npol):
                            pixmsk = self.realimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                            for ix in range(pixmsk.shape[0]):
                                for iy in range(pixmsk.shape[1]):
                                    if self.thresh[0] == self.nolimit:
                                        if pixmsk[ix][iy] > self.thresh[1]:
                                            maskedpixel[i][ix][iy][ichan][ipol] = pixmsk[ix][iy]
                                            pixmsk[ix][iy] = 0.0
                                    elif self.thresh[1] == self.nolimit:
                                        if pixmsk[ix][iy] < self.thresh[0]:
                                            maskedpixel[i][ix][iy][ichan][ipol] = pixmsk[ix][iy]
                                            pixmsk[ix][iy] = 0.0
                                    else:
                                        if pixmsk[ix][iy] < self.thresh[0] or pixmsk[ix][iy] > self.thresh[1]:
                                            maskedpixel[i][ix][iy][ichan][ipol] = pixmsk[ix][iy]
                                            pixmsk[ix][iy] = 0.0
                            self.realimage.putchunk(pixmsk, [0, 0, ichan, ipol])
                    self.realimage.close()
            elif len(imshape) == 3:
                # no polarization axis
                for i in range(nfile):
                    self.realimage = ia.newimage(self.tmprealname[i])
                    for ichan in range(nchan):
                        pixmsk = self.realimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                        for ix in range(pixmsk.shape[0]):
                            for iy in range(pixmsk.shape[1]):
                                if self.thresh[0] == self.nolimit:
                                    if pixmsk[ix][iy] > self.thresh[1]:
                                        maskedpixel[i][ix][iy][ichan] = pixmsk[ix][iy]
                                        pixmsk[ix][iy] = 0.0
                                elif self.thresh[1] == self.nolimit:
                                    if pixmsk[ix][iy] < self.thresh[0]:
                                        maskedpixel[i][ix][iy][ichan] = pixmsk[ix][iy]
                                        pixmsk[ix][iy] = 0.0
                                else:
                                    if pixmsk[ix][iy] < self.thresh[0] or pixmsk[ix][iy] > self.thresh[1]:
                                        maskedpixel[i][ix][iy][ichan] = pixmsk[ix][iy]
                                        pixmsk[ix][iy] = 0.0
                        self.realimage.putchunk(pixmsk, [0, 0, ichan])
                    self.realimage.close()
        maskedvalue = None
        if any(maskedpixel.flatten() != 0.0):
            maskedvalue = maskedpixel.mean(axis=0)
        del maskedpixel

        # set weight factor
        weights = numpy.ones(shape=(nfile, nx, ny), dtype=float)
        eps = 1.0e-5
        dtor = numpy.pi / 180.0
        for i in range(nfile):
            if abs(numpy.sin(dirs[i] * dtor)) < eps:
                # direction is around 0 deg
                maskw = 0.5 * nx * masks[i]
                for ix in range(nx):
                    for iy in range(ny):
                        dd = abs(float(ix) - 0.5 * (nx - 1))
                        if dd < maskw:
                            cosd = numpy.cos(0.5 * numpy.pi * dd / maskw)
                            weights[i][ix][iy] = 1.0 - cosd * cosd
                        if weights[i][ix][iy] == 0.0:
                            weights[i][ix][iy] += eps * 0.01
            elif abs(numpy.cos(dirs[i] * dtor)) < eps:
                # direction is around 90 deg
                maskw = 0.5 * ny * masks[i]
                for ix in range(nx):
                    for iy in range(ny):
                        dd = abs(float(iy) - 0.5 * (ny - 1))
                        if dd < maskw:
                            cosd = numpy.cos(0.5 * numpy.pi * dd / maskw)
                            weights[i][ix][iy] = 1.0 - cosd * cosd
                        if weights[i][ix][iy] == 0.0:
                            weights[i][ix][iy] += eps * 0.01
            else:
                maskw = 0.5 * sqrt(nx * ny) * masks[i]
                for ix in range(nx):
                    for iy in range(ny):
                        tand = numpy.tan((dirs[i] - 90.0) * dtor)
                        dd = abs(ix * tand - iy - 0.5 * (nx - 1) * tand + 0.5 * (ny - 1))
                        dd = dd / sqrt(1.0 + tand * tand)
                        if dd < maskw:
                            cosd = numpy.cos(0.5 * numpy.pi * dd / maskw)
                            weights[i][ix][iy] = 1.0 - cosd * cosd
                        if weights[i][ix][iy] == 0.0:
                            weights[i][ix][iy] += eps * 0.01
            # shift
            xshift = -((ny - 1) / 2)
            yshift = -((nx - 1) / 2)
            for ix in range(xshift, 0, 1):
                tmp = weights[i, :, 0].copy()
                weights[i, :, 0 : ny - 1] = weights[i, :, 1:ny].copy()
                weights[i, :, ny - 1] = tmp
            for iy in range(yshift, 0, 1):
                tmp = weights[i, 0:1].copy()
                weights[i, 0 : nx - 1] = weights[i, 1:nx].copy()
                weights[i, nx - 1 : nx] = tmp

        # FFT
        if len(imshape) == 4:
            # with polarization axis
            npol = imshape[3]
            for i in range(nfile):
                self.realimage = ia.newimage(self.tmprealname[i])
                self.imagimage = ia.newimage(self.tmpimagname[i])
                for ichan in range(nchan):
                    for ipol in range(npol):
                        pixval = self.realimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                        pixval = pixval.reshape((nx, ny))
                        pixfft = npfft.fft2(pixval)
                        pixfft = pixfft.reshape((nx, ny, 1, 1))
                        self.realimage.putchunk(pixfft.real, [0, 0, ichan, ipol])
                        self.imagimage.putchunk(pixfft.imag, [0, 0, ichan, ipol])
                        del pixval, pixfft
                self.realimage.close()
                self.imagimage.close()
        elif len(imshape) == 3:
            # no polarization axis
            for i in range(nfile):
                self.realimage = ia.newimage(self.tmprealname[i])
                self.imagimage = ia.newimage(self.tmpimagname[i])
                for ichan in range(nchan):
                    pixval = self.realimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                    pixval = pixval.reshape((nx, ny))
                    pixfft = npfft.fft2(pixval)
                    pixfft = pixfft.reshape((nx, ny, 1))
                    self.realimage.putchunk(pixfft.real, [0, 0, ichan])
                    self.imagimage.putchunk(pixfft.imag, [0, 0, ichan])
                    del pixval, pixfft
                self.realimage.close()
                self.imagimage.close()

        # weighted mean
        if len(imshape) == 4:
            npol = imshape[3]
            for ichan in range(nchan):
                for ipol in range(npol):
                    pixout = numpy.zeros(shape=(nx, ny), dtype=complex)
                    denom = numpy.zeros(shape=(nx, ny), dtype=float)
                    for i in range(nfile):
                        self.realimage = ia.newimage(self.tmprealname[i])
                        self.imagimage = ia.newimage(self.tmpimagname[i])
                        pixval = (
                            self.realimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                            + self.imagimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) * 1.0j
                        )
                        pixval = pixval.reshape((nx, ny))
                        pixout = pixout + pixval * weights[i]
                        denom = denom + weights[i]
                        self.realimage.close()
                        self.imagimage.close()
                    pixout = pixout / denom
                    pixout = pixout.reshape((nx, ny, 1, 1))
                    self.realimage = ia.newimage(self.tmprealname[0])
                    self.imagimage = ia.newimage(self.tmpimagname[0])
                    self.realimage.putchunk(pixout.real, [0, 0, ichan, ipol])
                    self.imagimage.putchunk(pixout.imag, [0, 0, ichan, ipol])
                    self.realimage.close()
                    self.imagimage.close()
        elif len(imshape) == 3:
            for ichan in range(nchan):
                pixout = numpy.zeros(shape=(nx, ny), dtype=complex)
                denom = numpy.zeros(shape=(nx, ny), dtype=float)
                for i in range(nfile):
                    self.realimage = ia.newimage(self.tmprealname[i])
                    self.imagimage = ia.newimage(self.tmpimagname[i])
                    pixval = self.realimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                    pixval = pixval.reshape((nx, ny))
                    pixout = pixout + pixval * weights[i]
                    denom = denom + weights[i]
                    self.realimage.close()
                    self.imagimage.close()
                pixout = pixout / denom
                pixout = pixout.reshape((nx, ny, 1))
                self.realimage = ia.newimage(self.tmprealname[0])
                self.imagimage = ia.newimage(self.tmpimagname[0])
                self.realimage.putchunk(pixout.real, [0, 0, ichan])
                self.imagimage.putchunk(pixout.imag, [0, 0, ichan])
                self.realimage.close()
                self.imagimage.close()

        # inverse FFT
        self.realimage = ia.newimage(self.tmprealname[0])
        self.imagimage = ia.newimage(self.tmpimagname[0])
        if len(imshape) == 4:
            npol = imshape[3]
            for ichan in range(nchan):
                for ipol in range(npol):
                    pixval = (
                        self.realimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol])
                        + self.imagimage.getchunk([0, 0, ichan, ipol], [nx - 1, ny - 1, ichan, ipol]) * 1.0j
                    )
                    pixval = pixval.reshape((nx, ny))
                    pixifft = npfft.ifft2(pixval)
                    pixifft = pixifft.reshape((nx, ny, 1, 1))
                    outimage.putchunk(pixifft.real, [0, 0, ichan, ipol])
                    del pixval, pixifft
        elif len(imshape) == 3:
            for ichan in range(nchan):
                pixval = (
                    self.realimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan])
                    + self.imagimage.getchunk([0, 0, ichan], [nx - 1, ny - 1, ichan]) * 1.0j
                )
                pixval = pixval.reshape((nx, ny))
                pixifft = npfft.ifft2(pixval)
                pixifft = pixifft.reshape((nx, ny, 1))
                outimage.putchunk(pixifft.real, [0, 0, ichan])
                del pixval, pixifft
        if maskedvalue is not None:
            outimage.putchunk(outimage.getchunk() + maskedvalue)
        # handling of output image mask
        maskstr = ""
        for name in self.infiles:
            if len(maskstr) > 0:
                maskstr += " || "
            maskstr += "mask('%s')" % (name)
        outimage.calcmask(maskstr, name="basketweaving", asdefault=True)

        self.realimage.close()
        self.imagimage.close()
        outimage.close()
Example #49
0
import numpy
import os
from taskinit import gentools, casalog
import sdutil
from collections import Counter
ms, sdms, tb, msmd = gentools(['ms', 'sdms', 'tb', 'msmd'])


def sdbaseline(infile=None,
               datacolumn=None,
               antenna=None,
               field=None,
               spw=None,
               timerange=None,
               scan=None,
               pol=None,
               intent=None,
               maskmode=None,
               thresh=None,
               avg_limit=None,
               minwidth=None,
               edge=None,
               blmode=None,
               dosubtract=None,
               blformat=None,
               bloutput=None,
               bltable=None,
               blfunc=None,
               order=None,
               npiece=None,
               applyfft=None,
Example #50
0
    def __execute_basket_weaving(self):
        ###
        # Basket-Weaving (Emerson & Grave 1988)
        ###
        casalog.post( 'Apply Basket-Weaving' )

        # CAS-5410 Use private tools inside task scripts
        ia = gentools(['ia'])[0]

        # initial setup
        outimage = ia.newimagefromimage( infile=self.infiles[0], outfile=self.outfile, overwrite=self.overwrite )
        imshape_out = outimage.shape()
        ndim_out = len(imshape_out)
        coordsys = outimage.coordsys()
        axis_types = coordsys.axiscoordinatetypes()
        # direction axis should always exist
        try:
            direction_axis0 = axis_types.index('Direction')
            direction_axis1 = axis_types[direction_axis0+1:].index('Direction') + direction_axis0 + 1
        except IndexError:
            raise RuntimeError('Direction axes don\'t exist.')
        nx = imshape_out[direction_axis0]
        ny = imshape_out[direction_axis1]
        tmp=[]
        nfile = len(self.infiles)
        for i in xrange(nfile):
            tmp.append(numpy.zeros(imshape_out,dtype=float))
        maskedpixel=numpy.array(tmp)
        del tmp

        # direction
        dirs = []
        if len(self.direction) == nfile:
            dirs = self.direction
        else:
            casalog.post( 'direction information is extrapolated.' )
            for i in range(nfile):
                dirs.append(self.direction[i%len(self.direction)])

        # masklist
        masks = []
        if type(self.masklist) == float:
            for i in range(nfile):
                masks.append( self.masklist )
        elif type(self.masklist) == list and nfile != len(self.masklist):
            for i in range(nfile):
                masks.append( self.masklist[i%len(self.masklist)] )
        for i in range(len(masks)):
            masks[i] = 0.01 * masks[i]
        
        # mask
        for i in range(nfile):
            self.realimage = create_4d_image(self.infiles[i], self.tmprealname[i])
            self.imagimage = self.realimage.subimage(outfile=self.tmpimagname[i])
            
            # replace masked pixels with 0.0
            if not self.realimage.getchunk(getmask=True).all():
                casalog.post("Replacing masked pixels with 0.0 in %d-th image" % (i))
                self.realimage.replacemaskedpixels(0.0)
            self.realimage.close()
            self.imagimage.close()
          
        # Below working images are all 4D regardless of dimension of input images  
        # image shape for temporary images (always 4D)
        ia.open(self.tmprealname[0])
        imshape = ia.shape()
        ndim = len(imshape)
        ia.close()

        if len(self.thresh) == 0:
            casalog.post( 'Use whole region' )
        else:
            for i in range(nfile):
                self.realimage = ia.newimage( self.tmprealname[i] )
                for iaxis2 in range(imshape[2]):
                    for iaxis3 in range(imshape[3]):
                        pixmsk = self.realimage.getchunk( [0,0,iaxis2,iaxis3], [nx-1,ny-1,iaxis2,iaxis3])
                        for ix in range(pixmsk.shape[0]):
                            for iy in range(pixmsk.shape[1]):
                                if self.thresh[0] == self.nolimit:
                                    if pixmsk[ix][iy] > self.thresh[1]:
                                        maskedpixel[i][ix][iy][iaxis2][iaxis3]=pixmsk[ix][iy]
                                        pixmsk[ix][iy] = 0.0
                                elif self.thresh[1] == self.nolimit:
                                    if pixmsk[ix][iy] < self.thresh[0]:
                                        maskedpixel[i][ix][iy][iaxis2][iaxis3]=pixmsk[ix][iy]
                                        pixmsk[ix][iy] = 0.0
                                else:
                                    if pixmsk[ix][iy] < self.thresh[0] or pixmsk[ix][iy] > self.thresh[1]:
                                        maskedpixel[i][ix][iy][iaxis2][iaxis3]=pixmsk[ix][iy]
                                        pixmsk[ix][iy] = 0.0
                        self.realimage.putchunk( pixmsk, [0,0,iaxis2,iaxis3] )
                self.realimage.close()
        maskedvalue=None
        if any(maskedpixel.flatten()!=0.0):
                maskedvalue=maskedpixel.mean(axis=0)
        del maskedpixel

        # set weight factor
        weights = numpy.ones( shape=(nfile,nx,ny), dtype=float )
        eps = 1.0e-5
        dtor = numpy.pi / 180.0
        for i in range(nfile):
            if abs(numpy.sin(dirs[i]*dtor)) < eps:
                # direction is around 0 deg
                maskw = 0.5 * nx * masks[i] 
                for ix in range(nx):
                    for iy in range(ny):
                        dd = abs( float(ix) - 0.5 * (nx-1) )
                        if dd < maskw:
                            cosd = numpy.cos(0.5*numpy.pi*dd/maskw)
                            weights[i][ix][iy] = 1.0 - cosd * cosd
                        if weights[i][ix][iy] == 0.0:
                            weights[i][ix][iy] += eps*0.01
            elif abs(numpy.cos(dirs[i]*dtor)) < eps:
                # direction is around 90 deg
                maskw = 0.5 * ny * masks[i]
                for ix in range(nx):
                    for iy in range(ny):
                        dd = abs( float(iy) - 0.5 * (ny-1) )
                        if dd < maskw:
                            cosd = numpy.cos(0.5*numpy.pi*dd/maskw)
                            weights[i][ix][iy] = 1.0 - cosd * cosd
                        if weights[i][ix][iy] == 0.0:
                            weights[i][ix][iy] += eps*0.01
            else:
                maskw = 0.5 * numpy.sqrt( nx * ny ) * masks[i]
                for ix in range(nx):
                    for iy in range(ny):
                        tand = numpy.tan((dirs[i]-90.0)*dtor)
                        dd = abs( ix * tand - iy - 0.5 * (nx-1) * tand + 0.5 * (ny-1) )
                        dd = dd / numpy.sqrt( 1.0 + tand * tand )
                        if dd < maskw:
                            cosd = numpy.cos(0.5*numpy.pi*dd/maskw)
                            weights[i][ix][iy] = 1.0 - cosd * cosd
                        if weights[i][ix][iy] == 0.0:
                            weights[i][ix][iy] += eps*0.01 
            # shift
            xshift = -((ny-1)/2)
            yshift = -((nx-1)/2)
            for ix in range(xshift,0,1):
                tmp = weights[i,:,0].copy()
                weights[i,:,0:ny-1] = weights[i,:,1:ny].copy()
                weights[i,:,ny-1] = tmp
            for iy in range(yshift,0,1):
                tmp = weights[i,0:1].copy()
                weights[i,0:nx-1] = weights[i,1:nx].copy()
                weights[i,nx-1:nx] = tmp

        # FFT
        for i in range(nfile):
            self.realimage = ia.newimage( self.tmprealname[i] )
            self.imagimage = ia.newimage( self.tmpimagname[i] )
            for iaxis2 in range(imshape[2]):
                for iaxis3 in range(imshape[3]):
                    pixval = self.realimage.getchunk( [0,0,iaxis2,iaxis3], [nx-1,ny-1,iaxis2,iaxis3] )
                    pixval = pixval.reshape((nx,ny))
                    pixfft = npfft.fft2( pixval )
                    pixfft = pixfft.reshape((nx,ny,1,1))
                    self.realimage.putchunk( pixfft.real, [0,0,iaxis2,iaxis3] )
                    self.imagimage.putchunk( pixfft.imag, [0,0,iaxis2,iaxis3] )
                    del pixval, pixfft
            self.realimage.close()
            self.imagimage.close()

        # weighted mean
        for ichan in range(imshape[2]):
            for iaxis3 in range(imshape[3]):
                pixout = numpy.zeros( shape=(nx,ny), dtype=complex )
                denom = numpy.zeros( shape=(nx,ny), dtype=float )
                for i in range(nfile):
                    self.realimage = ia.newimage( self.tmprealname[i] )
                    self.imagimage = ia.newimage( self.tmpimagname[i] )
                    pixval = self.realimage.getchunk( [0,0,ichan,iaxis3], [nx-1,ny-1,ichan,iaxis3] ) \
                        + self.imagimage.getchunk( [0,0,ichan,iaxis3], [nx-1,ny-1,ichan,iaxis3] ) * 1.0j
                    pixval = pixval.reshape((nx,ny))
                    pixout = pixout + pixval * weights[i]
                    denom = denom + weights[i]
                    self.realimage.close()
                    self.imagimage.close()
                pixout = pixout / denom
                pixout = pixout.reshape((nx,ny,1,1))
                self.realimage = ia.newimage( self.tmprealname[0] )
                self.imagimage = ia.newimage( self.tmpimagname[0] )
                self.realimage.putchunk( pixout.real, [0,0,ichan,iaxis3] )
                self.imagimage.putchunk( pixout.imag, [0,0,ichan,iaxis3] )
                self.realimage.close()
                self.imagimage.close()

        # inverse FFT
        self.realimage = ia.newimage( self.tmprealname[0] )
        self.imagimage = ia.newimage( self.tmpimagname[0] )
        for ichan in range(imshape[2]):
            for iaxis3 in range(imshape[3]):
                pixval = self.realimage.getchunk( [0,0,ichan,iaxis3], [nx-1,ny-1,ichan,iaxis3] ) \
                    + self.imagimage.getchunk( [0,0,ichan,iaxis3], [nx-1,ny-1,ichan,iaxis3] ) * 1.0j
                pixval = pixval.reshape((nx,ny))
                pixifft = npfft.ifft2( pixval )
                pixifft = pixifft.reshape((nx,ny,1,1))
                self.realimage.putchunk(pixifft.real, blc=[0,0,ichan,iaxis3])
                del pixval, pixifft
        if maskedvalue is not None:
            self.realimage.putchunk(self.realimage.getchunk()+maskedvalue)
            
        # put result into outimage
        chunk = self.realimage.getchunk()
        outimage.putchunk(chunk.reshape(imshape_out))
        # handling of output image mask
        maskstr = ""
        for name in self.infiles:
            if len(maskstr) > 0: maskstr += " || "
            maskstr += ("mask('%s')" % (name))
        outimage.calcmask(maskstr,name="basketweaving",asdefault=True)
        
        self.realimage.close()
        self.imagimage.close()
        outimage.close()
Example #51
0
                    
jplfiles_to_repository() puts it all together, so it is most likely the
function you want.

There are various utilities like convert_radec, datestr*, get_num_from_str,
mean_radius*, and construct_tablepath defined in here as well.
"""

from glob import glob
import os
import re
import scipy.special
import time  # We can always use more time.

from taskinit import gentools, qa, casalog
me = gentools(['me'])[0]

from dict_to_table import dict_to_table

# Possible columns, as announced by their column titles.
# The data is scooped up by 'pat'.  Either use ONE group named by the column
# key, or mark it as unwanted.  '-' is not valid in group names.
# Leading and trailing whitespace will be taken care of later.
# Sample lines:
#  Date__(UT)__HR:MN     R.A.___(ICRF/J2000.0)___DEC Ob-lon Ob-lat Sl-lon Sl-lat   NP.ang   NP.dist               r        rdot            delta      deldot    S-T-O   L_s
#  2010-May-01 00:00     09 01 43.1966 +19 04 28.673 286.52  18.22 246.99  25.34 358.6230      3.44  1.661167637023  -0.5303431 1.28664311447968  15.7195833  37.3033   84.50
#
# some mod to label names and comments so that they corresponds to
# JPL-Horizons naming comvension
cols = {
    'MJD': {
Example #52
0
    def __execute_press(self):
        ###
        # Pressed-out method (Sofue & Reich 1979)
        ###
        casalog.post( 'Apply Pressed-out method' )

        # CAS-5410 Use private tools inside task scripts
        ia = gentools(['ia'])[0]

        # mask
        self.image = ia.newimagefromimage(infile=self.infiles,outfile=self.tmpmskname)
        # back-up original mask name
        is_initial_mask = (self.image.maskhandler('default')[0] != '')
        temp_maskname = "temporal"
        imshape = self.image.shape()
        ndim = len(imshape)
        nx = imshape[0]
        ny = imshape[1]
        if len(self.thresh) == 0:
            casalog.post( 'Use whole region' )
        else:
            # mask pixels beyond thresholds
            maskstr = ("mask('%s')" % self.tmpmskname)
            if self.thresh[0] != self.nolimit:
                maskstr += (" && '%s'>=%f" % (self.tmpmskname, self.thresh[0]))
            if self.thresh[1] != self.nolimit:
                maskstr += (" && '%s'<=%f" % (self.tmpmskname, self.thresh[1]))
            # Need to flush to image once to calcmask ... sigh
            self.image.done()
            self.image = ia.newimage( self.tmpmskname )
            self.image.calcmask(mask=maskstr, name=temp_maskname, asdefault=True)

        # smoothing
        #bmajor = 0.0
        #bminor = 0.0
        # CAS-5410 Use private tools inside task scripts
        qa = qatool()
        if type(self.beamsize) == str:
            qbeamsize = qa.quantity(self.beamsize)
        else:
            qbeamsize = qa.quantity(self.beamsize,'arcsec')
        if type(self.smoothsize) == str:
            #bmajor = smoothsize
            #bminor = smoothsize
            qsmoothsize = qa.quantity(self.smoothsize)
        else:
            #bmajor = '%sarcsec' % (beamsize*smoothsize)
            #bminor = '%sarcsec' % (beamsize*smoothsize)
            qsmoothsize = qa.mul(qbeamsize,self.smoothsize)
        bmajor = qsmoothsize
        bminor = qsmoothsize
        pa = qa.quantity(0.0, 'deg')
        # masked channels are replaced by zero and convolved here.
        self.convimage = self.image.convolve2d( outfile=self.tmppolyname, major=bmajor, minor=bminor, pa=pa, 
                                                overwrite=True )
        self.convimage.done()

        # get dTij (original - smoothed)
        self.convimage = ia.imagecalc(outfile=self.tmpconvname, 
                                      pixels='"{org}" - "{conv}"'.format(org=self.tmpmskname,
                                                                         conv=self.tmppolyname),
                                      overwrite=True)

        # polynomial fit
        fitaxis = 0
        if self.direction == 0.0:
            fitaxis = 0
        elif self.direction == 90.0:
            fitaxis = 1
        else:
            raise Exception, "Sorry, the task don't support inclined scan with respect to horizontal or vertical axis, right now."
        # Replace duplicated method ia.fitpolynomial with
        # ia.fitprofile 
        #polyimage = convimage.fitpolynomial( fitfile=tmppolyname, axis=fitaxis, order=numpoly, overwrite=True )
        #polyimage.done()
        if os.path.exists( self.tmppolyname ):
            # CAS-5410 Use private tools inside task scripts
            cu = utilstool()
            cu.removetable([self.tmppolyname])
        self.convimage.setbrightnessunit('K')
        # Unfortunately, ia.fitprofile is very fragile.
        # Using numpy instead for fitting with masked pixels (KS, 2014/07/02)
        #resultdic = self.convimage.fitprofile( model=self.tmppolyname, axis=fitaxis, poly=self.numpoly, ngauss=0, multifit=True, gmncomps=0 )
        self.__polynomial_fit_model(image=self.tmpmskname, model=self.tmppolyname,axis=fitaxis, order=self.numpoly)
        polyimage = ia.newimage( self.tmppolyname )
        # set back defalut mask (need to get from self.image)
        avail_mask = polyimage.maskhandler('get')
        if is_initial_mask:
            casalog.post("copying mask from %s"%(self.infiles))
            polyimage.calcmask("mask('%s')"%self.infiles,asdefault=True)
        else: #no mask in the original image
            polyimage.calcmask('T', asdefault=True)
        if temp_maskname in avail_mask:
            polyimage.maskhandler('delete', name=temp_maskname)

        # subtract fitted image from original map
        subtracted = ia.imagecalc(outfile=self.outfile, 
                                  pixels='"{org}" - "{fit}"'.format(org=self.infiles,
                                                                    fit=self.tmppolyname),
                                  overwrite=self.overwrite)
        subtracted.done()

        # finalization
        polyimage.done(remove=True)
        self.convimage.done(remove=True)
        self.image.done()
Example #53
0
    def setSolarObjectJy(self,field,spw,scalebychan, timerange,observation, scan, intent, useephemdir, usescratch=False):
	"""
	Set flux density of a solar system object using Bryan Butler's new
	python model calculation code.
        A single time stamp (first time stamp of MS after selections are applied) is
        currently used per execution. For flux observation done in a long time span
        may need to run multiple setjy with selections by time range (or scans). 
	"""
	#retval = True 
        output = {}
	cleanupcomps = True # leave generated cl files 

        #from taskinit import * 
        from taskinit import gentools 
        qa = casac.quanta()
 

	(myms, mytb, mycl, myme, mycb, mymsmd) = gentools(['ms','tb','cl','me', 'cb','msmd'])
	# prepare parameters need to pass to the Bryan's code
	# make ms selections
	# get source name
	# get time ranges
	# get spwused and get frequency ranges

	sel={}
	sel['field']=field
	sel['spw']=spw
	sel['timerange']=timerange
	sel['observation']=str(observation)
	sel['scan']=scan
        sel['scanintent']=intent

	measframes=['REST','LSRK','LSRD','BARY','GEO','TOPO','GALACTO','LGROUP','CMB']
	myms.open(self.vis)
	myms.msselect(sel,False)
	scansummary=myms.getscansummary()
	nscan=len(scansummary.keys())
        selectedids=myms.msselectedindices()
	fieldids=selectedids['field']
        obsids=selectedids['observationid']
	myms.close()
	  
        mytb.open(self.vis+'/OBSERVATION')
        if len(obsids)==0:
          getrow=0
        else:
          getrow=obsids[0]
        observatory=mytb.getcell('TELESCOPE_NAME',getrow)
        mytb.close()
 
	mytb.open(self.vis+'/FIELD')
        colnames = mytb.colnames()
	if len(fieldids)==0:
	  fieldids = range(mytb.nrows())
        # frame reference for field position
        phdir_info=mytb.getcolkeyword("PHASE_DIR","MEASINFO")
        if phdir_info.has_key('Ref'):
          fieldref=phdir_info['Ref']
        elif phdir_info.has_key('TabRefTypes'):
          colnames=mytb.colnames()
          for col in colnames:
            if col=='PhaseDir_Ref':
                fieldrefind=mytb.getcell(col,fieldids[0])
                fieldref=phdir_info['TabRefTypes'][fieldrefind]
        else:
          fieldref=''
	srcnames={}
        fielddirs={}
        ftimes={}
        ephemid={}
	for fid in fieldids:
	  #srcnames.append(mytb.getcell('NAME',int(fid)))
	  srcnames[fid]=(mytb.getcell('NAME',int(fid)))
          #fielddirs[fid]=(mytb.getcell('PHASE_DIR',int(fid)))
          ftimes[fid]=(mytb.getcell('TIME',int(fid)))
          if colnames.count('EPHEMERIS_ID'):
              ephemid[fid]=(mytb.getcell('EPHEMERIS_ID',int(fid))) 
          else:
              ephemid[fid]=-1
	mytb.close() # close FIELD table

	# need to get a list of time
	# but for now for test just get a time centroid of the all scans
	# get time range
	# Also, this needs to be done per source

	# gather freq info from Spw table
	mytb.open(self.vis+'/SPECTRAL_WINDOW')
	nspw = mytb.nrows()
	freqcol=mytb.getvarcol('CHAN_FREQ')
	freqwcol=mytb.getvarcol('CHAN_WIDTH')
	fmeasrefcol=mytb.getcol('MEAS_FREQ_REF')
	reffreqs=mytb.getcol('REF_FREQUENCY')
	mytb.close()

	# store all parameters need to call solar_system_fd for all sources
	inparams={}
        validfids = [] # keep track of valid fid that has data (after selection) 
        # if same source name ....need handle this...
	for fid in fieldids:
	  sel['field']=str(fid)
	  myms.open(self.vis)
          #reset the selection
	  try:
            status=myms.msselect(sel)
          except:
            #skip this field
            continue 
          if not status:
            continue 

 
          validfids.append(fid)
	  trange=myms.range('time')
	  #if not inparams.has_key(srcnames[fid]):
          #  inparams[srcnames[fid]]={}
	  if not inparams.has_key(fid):
            inparams[fid]={}
            inparams[fid]['fieldname']=srcnames[fid]

	  #tc = (trange['time'][0]+trange['time'][1])/2. #in sec. 
          # use first timestamp to be consistent with the ALMA Control
          # old setjy (Butler-JPL-Horizons 2010) seems to be using
          # time in FIELD... but here is first selected time in main table
          if ephemid[fid]==-1: # keep old behavior
              tc = trange['time'][0] #in sec.
              tmsg = "Time used for the model calculation (=first time stamp of the selected data) for field "
          else: # must have ephem table attached...
              tc = ftimes[fid]#in sec.
              tmsg = "Time used for the model calculation for field "
       
	  #if inparams[srcnames[fid]].has_key('mjd'):
          #  inparams[srcnames[fid]]['mjds'][0].append([myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']])
          #else:
          #  inparams[srcnames[fid]]['mjds']=[myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']]
	  if inparams[fid].has_key('mjd'):
            inparams[fid]['mjds'][0].append([myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']])
          else:
            inparams[fid]['mjds']=[myme.epoch('utc',qa.quantity(tc,'s'))['m0']['value']]
          usedtime = qa.time(qa.quantity(tc,'s'),form='ymd')[0]
          self._casalog.post(tmsg+str(fid)+":"+usedtime)

          # get a correct direction measure
          mymsmd.open(self.vis)
          dirdic = mymsmd.phasecenter(int(fid))
          mymsmd.close()

          # check if frame is J2000 and if not do the transform
          if dirdic['refer'] != 'J2000':
              myme.doframe(myme.observatory(observatory))
              myme.doframe(myme.epoch('utc', qa.quantity(tc,'s')))
              azeldir = myme.measure(dirdic,'AZELGEO')
              dirdic=myme.measure(azeldir,'J2000')
       
          fielddirs[fid]=(numpy.array([[dirdic['m0']['value']],[dirdic['m1']['value']]]))

          # somehow it gives you duplicated ids .... so need to uniquify
	  selspws= list(set(myms.msselectedindices()['spw']))
          # make sure it is int rather than numpy.int32, etc.
          selspws = [int(ispw) for ispw in selspws]
	  #inparams[srcnames[fid]]['spwids']= selspws if len(selspws)!=0 else range(nspw) 
	  inparams[fid]['spwids']= selspws if len(selspws)!=0 else range(nspw) 

	  #create a list of freq ranges with selected spws
	  # should worry about freq order???
	  freqlist=[]
          freqlistraw=[]
	  framelist=[]
	  #for idx in inparams[srcnames[fid]]['spwids']:
	  for idx in inparams[fid]['spwids']:
	    freqs=freqcol['r'+str(idx+1)]
	    freqws=freqwcol['r'+str(idx+1)]
	    fmeasref=fmeasrefcol[idx]
	 
	    #if scalebychan=T, this has to be min, max determined from
	    # chan_freq(channel center)+/- chan width.
	    if scalebychan:
              # pack into list of list of list (freqlist[nf][nspw])
	      perspwfreqlist=[]
	      for nf in range(len(freqs)):
		fl = freqs[nf][0]-freqws[nf][0]/2.
		fh = freqs[nf][0]+freqws[nf][0]/2.
		perspwfreqlist.append([fl,fh])
	      freqlist.append(perspwfreqlist)
	    else:
	      if (len(freqs)==1): 
		fl = freqs[0][0]-freqws[0][0]/2.
		fh = freqs[0][0]+freqws[0][0]/2.
		freqlist.append([float(fl),float(fh)])
	      else:
		freqlist.append([float(min(freqs)),float(max(freqs))])   
	 
	    framelist.append(measframes[fmeasref])
            freqlistraw.append(freqs.transpose()[0].tolist()) # data chanfreq list
	  #inparams[srcnames[fid]]['freqlist']=freqlist
	  #inparams[srcnames[fid]]['freqlistraw']=freqlistraw
	  #inparams[srcnames[fid]]['framelist']=framelist
	  #inparams[srcnames[fid]]['reffreqs']=reffreqs

	  inparams[fid]['freqlist']=freqlist
	  inparams[fid]['freqlistraw']=freqlistraw
	  inparams[fid]['framelist']=framelist
	  inparams[fid]['reffreqs']=reffreqs
          myms.close()

             
	# call Bryan's code
	# errcode: list of list - inner list - each values for range of freqs
	# flluxes: list of list 
	# fluxerrs:
	# size: [majoraxis, minoraxis, pa]
	# direction: direction for each time stamp
        # 
	#import solar_system_setjy as ss_setjy
	import solar_system_setjy as SSsetjy 
	retdict={} # for returning flux densities?
        ss_setjy=SSsetjy.solar_system_setjy()
	#for src in srcnames:
        self.clnamelist=[]
	for vfid in validfids:
          src=srcnames[vfid]
	  #mjds=inparams[src]['mjds']
	  mjds=inparams[vfid]['mjds']
	  fluxes=[]
          # call solar_system_fd() per spw (for scalebychan freqlist has an extra dimention)
          #nspwused=len(inparams[src]['freqlist'])
          nspwused=len(inparams[vfid]['freqlist'])

          # warning for many channels but it is really depends on the source
          if scalebychan:
            maxnf=0
            for ispw in range(nspwused):
              #nf = len(inparams[src]['freqlist'][ispw])
              nf = len(inparams[vfid]['freqlist'][ispw])
              maxnf = max(nf,maxnf)
            if maxnf >= 3840 and src.upper()!="MARS": # mars shoulde be ok
              self._casalog.post("Processing %s spw(s) and at least some of them are a few 1000 channels or more. This may take \
                            many minutes (>3min per spw for 3840 channels) in some cases. Please be patient." % nspwused,"WARN")
              
	  for i in range(nspwused): # corresponds to n spw
	    if type(freqlist[0][0])==list:
	      #infreqs=inparams[src]['freqlist'][i]
	      infreqs=inparams[vfid]['freqlist'][i]
	    else:
	      #infreqs=[inparams[src]['freqlist'][i]]
	      infreqs=[inparams[vfid]['freqlist'][i]]
            self._casalog.post("Calling solar_system_fd: %s(%s) for spw%s freqs=%s" % (src, vfid,i,freqlist[i]),'DEBUG1')
	    (errcodes, subfluxes, fluxerrs, sizes, dirs)=\
               ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, observatory=observatory, casalog=self._casalog)
            # for old code
	    #(errcodes, subfluxes, fluxerrs, sizes, dirs)=\
            #   ss_setjy.solar_system_fd(source_name=src, MJDs=mjds, frequencies=infreqs, casalog=self._casalog)
          # for nf freq ranges, nt mjds
          # errcodes[nf][nt], subfluxes[nf][nt], fluxerrs[nf][nt], sizes[nt],  dirs[nt]
            self._casalog.post("+++++ solar_system_fd() returned values +++++", 'DEBUG1')
            self._casalog.post(" fluxes(fds)=%s" % subfluxes, 'DEBUG1')
            self._casalog.post(" sizes=%s" % sizes, 'DEBUG1')
            self._casalog.post(" directions=%s\n" % dirs, 'DEBUG1')

          # packed fluxes for all spws 
	    fluxes.append(subfluxes)    
          # fluxes has fluxesi[nspw][nf][nt]

	  # ------------------------------------------------------------------------
	  # For testing with hardcoded values without calling solar_system_fd()...
	  #errcodes=[[0,0,0,0,0]]
	  #fluxes=[[26.40653147,65.23839313,65.23382757,65.80638802,69.33396562]]
	  #fluxerrs=[[0.0,0.0,0.0,0.0,0.0]]
	  #sizes=[[3.6228991032674371,3.6228991032674371,0.0]]
	  #dirs=[{'m0': {'unit': 'rad', 'value': 0.0}, 'm1': {'unit': 'rad', 'value': 0.0}, 'refer': 'J2000', 'type': 'direction'}]
	  # ------------------------------------------------------------------------
          # local params for selected src
	  #framelist=inparams[src]['framelist']
	  #freqlist=inparams[src]['freqlist']
	  #freqlistraw=inparams[src]['freqlistraw']
	  #reffreqs=inparams[src]['reffreqs']
	  #spwids=inparams[src]['spwids']

	  framelist=inparams[vfid]['framelist']
	  freqlist=inparams[vfid]['freqlist']
	  freqlistraw=inparams[vfid]['freqlistraw']
	  reffreqs=inparams[vfid]['reffreqs']
	  spwids=inparams[vfid]['spwids']

	  clrecs=odict.odict()
	  labels = []
	  # loop for over for multiple directions (=multiple  MJDs) for a given src
	  for i in range(len(dirs)):  # this is currently only length of 1 since no multiple timestamps were used
	    # check errcode - error code would be there per flux per time (dir)  
	    reterr=testerrs(errcodes[i],src) 
	    if reterr == 2: 
	      continue
	  
	    #dirstring = [dirs[i]['refer'],qa.tos(dirs[i]['m0']),qa.tos(dirs[i]['m1'])]
            if useephemdir:
	        dirstring = [dirs[i]['refer'],qa.tos(dirs[i]['m0']),qa.tos(dirs[i]['m1'])]
                dirmsg = "Using the source position from the ephemeris table in the casa data directory: "
            else:
                #dirstring = [fieldref, str(fielddirs[fieldids[0]][0][0])+'rad', str(fielddirs[fieldids[0]][1][0])+'rad']
                # extract field direction of first id of the selected field ids
                #dirstring = [fieldref, "%.18frad" % (fielddirs[fieldids[0]][0][0]), "%.18frad" % (fielddirs[fieldids[0]][1][0])]
                #dirstring = [fieldref, "%.18frad" % (fielddirs[vfid][0][0]), "%.18frad" % (fielddirs[vfid][1][0])]
                # by now the direction should be in J2000
                dirstring = ['J2000', "%.18frad" % (fielddirs[vfid][0][0]), "%.18frad" % (fielddirs[vfid][1][0])]
                dirmsg = "Using the source position in the MS (or in the attached ephemeris table, if any): "
            
            self._casalog.post(dirmsg+str(dirstring))

	    # setup componentlists
	    # need to set per dir
	    # if scalebychan=F, len(freqs) corresponds to nspw selected
            # Need to put in for-loop to create cl for each time stamp? or scan?

	    #clpath='/tmp/'
	    clpath='./'
            # construct cl name (multiple spws in one componentlist table)
            selreffreqs = [reffreqs[indx] for indx in spwids]
            minfreq = min(selreffreqs)
            maxfreq = max(selreffreqs)
	    freqlabel = '%.3f-%.3fGHz' % (minfreq/1.e9, maxfreq/1.e9)
	    #tmlabel = '%.1fd' % (tc/86400.)
	    mjd=inparams[vfid]['mjds'][0]
	    tmlabel = '%.1fd' % (mjd)
	    #clabel = src+'_spw'+str(spwids[j])+'_'+freqlabel+'_'+tmlabel
	    #clabel0 = src+'_'+freqlabel+'_'+tmlabel
            #pid=str(os.getpid())
	    #clname = clpath+clabel0+"_"+pid+'.cl'
	    clabel0 = src+'_'+freqlabel+'_'+tmlabel+"_"+os.path.basename(self.vis)
	    clname = clpath+clabel0+'.cl'
            #debug
            self._casalog.post("Create componentlist: %s for vfid=%s" % (clname,vfid),'DEBUG1')
	      
	    if (os.path.exists(clname)):
              shutil.rmtree(clname)

            iiflux=[]
            iinfreq=[]
            # for logging/output dict - pack each freq list for each spw
            freqsforlog=[]
	    for j in range(len(freqlist)): # loop over nspw
	      freqlabel = '%.3fGHz' % (reffreqs[int(spwids[j])]/1.e9)
              clabel=src+'_spw'+str(spwids[j])+'_'+freqlabel+'_'+tmlabel
	      #print "addcomponent...for spw=",spwids[j]," i=",i," flux=",fluxes[j][i]
	      if scalebychan:
		index= 2.0
		sptype = 'spectral index'
	      else:
		index= 0.0
		sptype = 'constant'
              # adjust to change in returned flux shape. An extra [] now seems to be gone. 2012-09-27
              iflux=fluxes[j][i][0]
              #print "addcomponent with flux=%s at frequency=%s" %\
              #                    (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz')

              # i - time stamps = 0 for now, j = a freq range
              infreq=freqlist[j][0][0] if type(freqlist[j][0])==list else freqlist[j][0]

              # for a single CL with multple spws
              if j ==0: infreq0=infreq 

              # --- version for 'multi-spws in a single comp single CL'
              # accumulate all frequencies from all spws to a list
              #if not scalebychan:
                #freqlist[j] for j spw should contain a list with two freqs (min and max of
                # of the spw and the same fluxes should be set for the freqs so that cl.setspectrum
                # with tabular would not interpolate inside each spw 
                ##iinfreq.extend(freqlist[j])
              #  nch = len(freqlistraw[j])
              #  iinfreq.extend(freqlistraw[j])
                # assigning the same flux to the two freqs
                ##iiflux.extend([iflux,iflux])
              #  for ich in range(nch):
              #    iiflux.extend([iflux])
              #else:
              #  iiflux.extend(fluxes[j][i])
	      #  if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
	      #	  for fr in freqlist[j]:
	      #	    iinfreq.append((fr[1]+fr[0])/2)
	      #  else:
              #    if type(freqlist[j])==list:
	      #	    iinfreq.append((freqlist[j][1]+freqlist[j][0])/2)
              #    else:
	      #      iinfreq.append(freqlist[j])
              #  freqsforlog.append(iinfreq)
              # -----------------------------------------------------------------------------------
              
              self._casalog.post("addcomponent with flux=%s at frequency=%s" %\
              #                    (fluxes[j][i][0],str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')
                                  (iflux,str(reffreqs[int(spwids[j])]/1.e9)+'GHz'), 'INFO1')
            
              #  
	      mycl.addcomponent(iflux,fluxunit='Jy', polarization="Stokes", dir=dirstring,
              		 shape='disk', majoraxis=str(sizes[i][0])+'arcsec', minoraxis=str(sizes[i][1])+'arcsec', 
                        positionangle=str(sizes[i][2])+'deg', freq=[framelist[0],str(infreq)+'Hz'], 
	      		 spectrumtype=sptype, index=index, label=clabel)

              if scalebychan:
                # use tabular form to set flux for each channel
                
                # This may be redundant 
                if type(fluxes[j][i]) ==list and len(fluxes[j][i])> 1:
                  if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
                    freqs=[]
                    for fr in freqlist[j]:
                      freqs.append((fr[1]+fr[0])/2)
                    clind = mycl.length() - 1
                    mycl.setspectrum(which=clind, type='tabular', tabularfreqs=freqs, tabularflux=fluxes[j][0],
	                             tabularframe=framelist[j])
            ### === per spw process end here

          # - version that put all spws into a component -
          # set a single component freq set at the the lower edge of first spw 
	  #mycl.addcomponent(iiflux[0],fluxunit='Jy', polarization="Stokes", dir=dirstring,
	  #		 shape='disk', majoraxis=str(sizes[i][0])+'arcsec', minoraxis=str(sizes[i][1])+'arcsec', 
	  #	         positionangle=str(sizes[i][2])+'deg', freq=[framelist[0],str(infreq0)+'Hz'], 
	  #		 spectrumtype=sptype, index=index, label=clabel)
          # if it's list of fluxes try to put in tabular form
	  #if type(fluxes[j][i]) ==list and len(fluxes[j][i])> 1:
	#  if len(iiflux)> 1:
	        #print "framelist[j]=",framelist[j]
	        #if type(freqlist[j][0])==list and len(freqlist[j][0])>1:
		#  freqs=[]
		#  for fr in freqlist[j]:
		#    freqs.append((fr[1]+fr[0])/2)
		#else:
		#  freqs=freqlist[j]
                #clind = mycl.length() - 1
	#	mycl.setspectrum(which=clind, type='tabular', tabularfreqs=freqs, tabularflux=fluxes[j][0],
			   #tabularframe=framelist[j])

        #    mycl.setspectrum(which=clind, type='tabular', tabularfreqs=iinfreq, tabularflux=iiflux,
	#                     tabularframe=framelist[0])
	      #mycl.rename(clname)
	      #put in a record for log output
	      #clrecs[clabel] = mycl.torecord()
        
            clrecs[clabel0] = mycl.torecord()
            clrecs[clabel0]['allfluxinfo']=fluxes
            clrecs[clabel0]['allfreqinfo']=freqsforlog
            clrecs[clabel0]['spwids']=spwids
	    mycl.rename(clname) #  - A CL per field
            mycl.close(False) # False for not to send a warning message
	    mycl.done()

              # if scratch=F check if the virtual model already exist
              # for the field if it is clear it.
            #  if j==0 and (not usescratch): 
              #  mytb.open(self.vis, nomodify=False)
              #  kwds = mytb.getkeywords()
              #  modelkwd='definedmodel_field_'+str(vfid)
              #  if kwds.has_key(modelkwd):
              #    clmodname=kwds[modelkwd]
              #    mytb.removekeyword(clmodname)
              #    mytb.removekeyword(modelkwd)
              #  mytb.close()
             
            mycb.open(self.vis,addcorr=False,addmodel=False)
            mycb.delmod(otf=True,field=str(vfid),spw=spwids, scr=False)
            mycb.close()

	      # finally, put the componentlist as model
              #tqlstr=''
              #if intent!='':
              #   tqlstr='any(STATE_ID=='+str(stateids.tolist())+')'
            # Currently still need to do per spw (see the comment below...)
            # assuming CL contains nrow = nspw components
            mycl.open(clname)
            clinrec = mycl.torecord()
            mycl.close(False)
            mycl.done()
            ncomp = clinrec['nelements']
            if ncomp != len(spwids):
               raise Exception, "Inconsistency in generated componentlist...Please submit a bug report."
            for icomp in range(ncomp): 
	      #self.im.selectvis(spw=spwids[icomp],field=field,observation=observation,time=timerange,intent=intent)
	      ok = self.im.selectvis(spw=spwids[icomp],field=vfid,observation=observation,time=timerange,intent=intent)
              # for MMS, particular subms may not contain the particular spw, so skip for such a case
              if ok: 
                  newclinrec = {}
                  newclinrec['nelements']=1
                  newclinrec['component0']=clinrec['component'+str(icomp)]
                  mycl.fromrecord(newclinrec)
                  cdir = os.getcwd()
                  if os.access(cdir,os.W_OK):
                      tmpclpath = cdir+"/"
                  elif os.access("/tmp",os.W_OK):
                      tmpclpath = "/tmp/"
                  #tmpclpath=tmpclpath+"_tmp_setjyCLfile_"+pid
                  tmpclpath=tmpclpath+os.path.basename(self.vis)+"_tmp_setjyCLfile"
                  if os.path.exists(tmpclpath):
                      shutil.rmtree(tmpclpath)
                  mycl.rename(tmpclpath)
                  mycl.close(False)
	          self.im.ft(complist=tmpclpath)
             

            # --------------------------------------------------------------------------------------------
            # Currently this does not work properly for some case. Need ft can handle multi-component CL
            # with the way to map which component apply to which spw
            # Unable when such functionality is implemented in im.ft()
	    #self.im.selectvis(spw=spwids,field=field,observation=observation,time=timerange,intent=intent)
	    #self.im.ft(complist=clname) <= e.g. im.ft(comprec) where comrec = {'spw-mapping':[...],comprecs}..
            # --------------------------------------------------------------------------------------------

              #debug: set locally saved 2010-version component list instead
              #cl2010='mod_setjy_spw0_Titan_230.543GHz55674.1d.cl'
              #print "Using complist=",cl2010
	      #self.im.ft(complist=cl2010)

	      #if cleanupcomps:          
              # 		  shutil.rmtree(clname)
              #self.clnamelist.append(clname)
            self.clnamelist.append(clname)

	  msg="Using channel dependent " if scalebychan else "Using spw dependent "
       
          if clrecs=={}:
            raise Exception, "No componentlist is generated."
	  self._casalog.post(msg+" flux densities")
	  #self._reportoLog(clrecs,self._casalog)
	  self._reportoLog2(clrecs,self._casalog)
	  #self._updateHistory(clrecs,self.vis)
	  self._updateHistory2(clrecs,self.vis)
          # dictionary of dictionary for each field
          retdict[vfid]=clrecs 
        # ==== end of for loop over fields		 
        #output=self._makeRetFluxDict(retdict)
        output=self._makeRetFluxDict2(retdict)
	#return retval
	return output
                    
jplfiles_to_repository() puts it all together, so it is most likely the
function you want.

There are various utilities like convert_radec, datestr*, get_num_from_str,
mean_radius*, and construct_tablepath defined in here as well.
"""

from glob import glob
import os
import re
import scipy.special
import time                  # We can always use more time.

from taskinit import gentools, qa, casalog
me = gentools(['me'])[0]

from dict_to_table import dict_to_table

# Possible columns, as announced by their column titles.
# The data is scooped up by 'pat'.  Either use ONE group named by the column
# key, or mark it as unwanted.  '-' is not valid in group names.
# Leading and trailing whitespace will be taken care of later.
# Sample lines:
#  Date__(UT)__HR:MN     R.A.___(ICRF/J2000.0)___DEC Ob-lon Ob-lat Sl-lon Sl-lat   NP.ang   NP.dist               r        rdot            delta      deldot    S-T-O   L_s
#  2010-May-01 00:00     09 01 43.1966 +19 04 28.673 286.52  18.22 246.99  25.34 358.6230      3.44  1.661167637023  -0.5303431 1.28664311447968  15.7195833  37.3033   84.50
# 
# some mod to label names and comments so that they corresponds to
# JPL-Horizons naming comvension
cols = {
    'MJD': {'header': r'Date__\(UT\)__HR:MN',
Example #55
0
    def _updateHistory2(self,clrecs,vis):
        """
        Update history table when setSolarObjectJy is run
        (mulitple comopents in one version)
        """
        #
        from taskinit import gentools
        (mytb,) = gentools(['tb'])
        mytb.open(vis+'/HISTORY',nomodify=False)
        nrow = mytb.nrows()
        lasttime=mytb.getcol('TIME')[nrow-1]
        rown=nrow
        #
        clname = clrecs.keys()[0]

        #for ky in clrecs.keys():
        srcn = clname.split('_')[0]
        comprec = clrecs[clname]
        nelm = comprec['nelements']
        for icomp in range(nelm): 
            comp = comprec['component'+str(icomp)]
            complab = comp['label']
            origin='setjy'
            priority='INFO'
            time=lasttime
            appl='setjy'
            emptystrarr=numpy.array([''])
            if nelm==1:
              for i in range(len(comprec['spwids'])):
                mytb.addrows(1)
                ispw=comprec['spwids'][i]
                iflux = comprec['allfluxinfo'][i][0][0]
                msg = (" %s: spw %s Flux:[I=%s,Q=%s,U=%s,V=%s] +/- [I=%s,Q=%s,U=%s,V=%s] Jy" %
                       (srcn, ispw, iflux, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
                mytb.putcell('APP_PARAMS', rown, [''])
                mytb.putcell('CLI_COMMAND',rown, [''])
                mytb.putcell('APPLICATION',rown, appl)
                mytb.putcell('MESSAGE',rown, msg)
                mytb.putcell('OBSERVATION_ID',rown, -1)
                mytb.putcell('ORIGIN',rown,origin)
                mytb.putcell('PRIORITY',rown,priority)
                mytb.putcell('TIME',rown, time)
                rown += 1
              
            else: 
                ispw = complab.split('_')[1]
                mytb.addrows(1)
                msg = (" %s: spw %s Flux:[I=%s,Q=%s,U=%s,V=%s] +/- [I=%s,Q=%s,U=%s,V=%s] Jy" %
                       (srcn, ispw, comp['flux']['value'][0], comp['flux']['value'][1],
                        comp['flux']['value'][2], comp['flux']['value'][3],
                        comp['flux']['error'][0], comp['flux']['error'][1],comp['flux']['error'][2],
                        comp['flux']['error'][3]))

                mytb.putcell('APP_PARAMS', rown, [''])
                mytb.putcell('CLI_COMMAND',rown, [''])
                mytb.putcell('APPLICATION',rown, appl)
                mytb.putcell('MESSAGE',rown, msg)
                mytb.putcell('OBSERVATION_ID',rown, -1)
                mytb.putcell('ORIGIN',rown,origin)
                mytb.putcell('PRIORITY',rown,priority)
                mytb.putcell('TIME',rown, time)
                rown += 1
        mytb.close()
import numpy as np
import pdb
from taskinit import gentools
ms = gentools(['ms'])[0]
ia = gentools(['ia'])[0]
#qa = gentools(['qa'])[0]
#execfile('fixImage_fixedcoord.py')

emerlin_ost_dir = '/local/scratch/btunbrid/work/im3shape_work/COSMOS/Radio_noise_simulations/output/'

def rescale_image(image_array,new_max):
  image_max = image_array.max()
  factor = float( new_max / image_max)
  image_array = np.multiply(image_array,factor)
  return image_array

def fixImage(datafile,
             centre_freq=1.4, #GHz
             bandwidth=2., # in fits header 2.5e+07 quoted 25000000
#GHz 37.5 MHz for 
             n_channels=7,
             peakvalue='0.024',#'0.0092034321',
             peakunit='Jy',
             pixel_scale=0.2, #pixel/arcmin
		#Central image is positioned at 150.119166668, 2.20573611111#'0h8m49.4s',
             Ra_central='10h02m28.67s',
             Dec_central='+02d38m28.64s',
             ost_template_image=emerlin_ost_dir+'ost_template.image',
             input_casa_image='simulated_VLA_large_casa.image'):
  '''Kludgey function to give an image from GalSim the correct co-ordinate
  system for observation with CASA.