Пример #1
0
    def read(self, file, nints, nskip, spw, selectpol, scan, datacol):
        """ Reads in Measurement Set data using CASA.
        spw is list of subbands. zero-based.
        Scan is zero-based selection based on scan order, not actual scan number.
        selectpol is list of polarization strings (e.g., ['RR','LL'])
        """
        self.file = file
        self.scan = scan
        self.nints = nints

        # get spw info. either load pickled version (if found) or make new one
        pklname = string.join(file.split(".")[:-1], ".") + "_init.pkl"
        #        pklname = pklname.split('/')[-1]  # hack to remove path and write locally
        if os.path.exists(pklname):
            print "Pickle of initializing info found. Loading..."
            pkl = open(pklname, "r")
            try:
                (self.npol_orig, self.nbl, self.blarr, self.inttime, spwinfo, scansummary) = pickle.load(pkl)
            except EOFError:
                print "Bad pickle file. Exiting..."
                return 1
            # old way, casa 3.3?
            #            scanlist = scansummary['summary'].keys()
            #            starttime_mjd = scansummary['summary'][scanlist[scan]]['0']['BeginTime']
            # new way, casa 4.0?
            scanlist = scansummary.keys()
            starttime_mjd = scansummary[scanlist[scan]]["0"]["BeginTime"]
            self.nskip = int(
                nskip * self.nbl
            )  # number of iterations to skip (for reading in different parts of buffer)
            self.npol = len(selectpol)
        else:
            print "No pickle of initializing info found. Making anew..."
            pkl = open(pklname, "wb")
            ms.open(self.file)
            spwinfo = ms.getspectralwindowinfo()
            scansummary = ms.getscansummary()

            # original (general version)
            #            scanlist = scansummary['summary'].keys()
            #            starttime_mjd = scansummary['summary'][scanlist[scan]]['0']['BeginTime']
            #            starttime0 = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+0/(24.*60*60),'d'),form=['ymd'], prec=9), 's'))
            #            stoptime0 = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+0.5/(24.*60*60), 'd'), form=['ymd'], prec=9), 's'))

            # for casa 4.0 (?) and later
            scanlist = scansummary.keys()
            # set time info
            self.inttime = scansummary[scanlist[scan]]["0"]["IntegrationTime"]
            self.inttime0 = self.inttime
            print "Initializing integration time (s):", self.inttime
            starttime_mjd = scansummary[scanlist[scan]]["0"]["BeginTime"]
            starttime0 = qa.getvalue(
                qa.convert(
                    qa.time(qa.quantity(starttime_mjd + 0 / (24.0 * 60 * 60), "d"), form=["ymd"], prec=9)[0], "s"
                )
            )[0]
            stoptime0 = qa.getvalue(
                qa.convert(
                    qa.time(qa.quantity(starttime_mjd + self.inttime / (24.0 * 60 * 60), "d"), form=["ymd"], prec=9)[0],
                    "s",
                )
            )[0]

            ms.selectinit(datadescid=0)  # initialize to initialize params
            selection = {"time": [starttime0, stoptime0]}
            ms.select(items=selection)
            da = ms.getdata([datacol, "axis_info"], ifraxis=True)
            ms.close()

            self.npol_orig = da[datacol].shape[0]
            self.nbl = da[datacol].shape[2]
            print "Initializing nbl:", self.nbl

            # good baselines
            bls = da["axis_info"]["ifr_axis"]["ifr_shortname"]
            self.blarr = n.array([[int(bls[i].split("-")[0]), int(bls[i].split("-")[1])] for i in xrange(len(bls))])
            self.nskip = int(
                nskip * self.nbl
            )  # number of iterations to skip (for reading in different parts of buffer)

            pickle.dump((self.npol_orig, self.nbl, self.blarr, self.inttime, spwinfo, scansummary), pkl)
        pkl.close()

        self.ants = n.unique(self.blarr)
        self.nants = len(n.unique(self.blarr))
        self.nants0 = len(n.unique(self.blarr))
        print "Initializing nants:", self.nants
        self.npol = len(selectpol)
        print "Initializing %d of %d polarizations" % (self.npol, self.npol_orig)

        # set desired spw
        if (len(spw) == 1) & (spw[0] == -1):
            #            spwlist = spwinfo['spwInfo'].keys()    # old way
            spwlist = spwinfo.keys()  # new way
        else:
            spwlist = spw

        self.freq_orig = n.array([])
        for spw in spwlist:
            # new way
            nch = spwinfo[str(spw)]["NumChan"]
            ch0 = spwinfo[str(spw)]["Chan1Freq"]
            chw = spwinfo[str(spw)]["ChanWidth"]
            self.freq_orig = n.concatenate((self.freq_orig, (ch0 + chw * n.arange(nch)) * 1e-9))
        # old way
        #            nch = spwinfo['spwInfo'][str(spw)]['NumChan']
        #            ch0 = spwinfo['spwInfo'][str(spw)]['Chan1Freq']
        #            chw = spwinfo['spwInfo'][str(spw)]['ChanWidth']

        self.freq = self.freq_orig[self.chans]
        self.nchan = len(self.freq)
        print "Initializing nchan:", self.nchan

        # set requested time range based on given parameters
        timeskip = self.inttime * nskip
        # new way
        starttime = qa.getvalue(
            qa.convert(
                qa.time(qa.quantity(starttime_mjd + timeskip / (24.0 * 60 * 60), "d"), form=["ymd"], prec=9)[0], "s"
            )
        )[0]
        stoptime = qa.getvalue(
            qa.convert(
                qa.time(
                    qa.quantity(starttime_mjd + (timeskip + nints * self.inttime) / (24.0 * 60 * 60), "d"),
                    form=["ymd"],
                    prec=9,
                )[0],
                "s",
            )
        )[0]
        print "First integration of scan:", qa.time(qa.quantity(starttime_mjd, "d"), form=["ymd"], prec=9)[0]
        print
        # new way
        print "Reading scan", str(scanlist[scan]), "for times", qa.time(
            qa.quantity(starttime_mjd + timeskip / (24.0 * 60 * 60), "d"), form=["hms"], prec=9
        )[0], "to", qa.time(
            qa.quantity(starttime_mjd + (timeskip + nints * self.inttime) / (24.0 * 60 * 60), "d"), form=["hms"], prec=9
        )[
            0
        ]

        # read data into data structure
        ms.open(self.file)
        ms.selectinit(datadescid=spwlist[0])  # reset select params for later data selection
        selection = {"time": [starttime, stoptime]}
        ms.select(items=selection)
        print "Reading %s column, SB %d, polarization %s..." % (datacol, spwlist[0], selectpol)
        ms.selectpolarization(selectpol)
        da = ms.getdata([datacol, "axis_info", "u", "v", "w", "flag"], ifraxis=True)
        u = da["u"]
        v = da["v"]
        w = da["w"]
        if da == {}:
            print "No data found."
            return 1
        newda = n.transpose(da[datacol], axes=[3, 2, 1, 0])  # if using multi-pol data.
        flags = n.transpose(da["flag"], axes=[3, 2, 1, 0])
        if len(spwlist) > 1:
            for spw in spwlist[1:]:
                ms.selectinit(datadescid=spw)  # reset select params for later data selection
                ms.select(items=selection)
                print "Reading %s column, SB %d, polarization %s..." % (datacol, spw, selectpol)
                ms.selectpolarization(selectpol)
                da = ms.getdata([datacol, "axis_info", "flag"], ifraxis=True)
                newda = n.concatenate((newda, n.transpose(da[datacol], axes=[3, 2, 1, 0])), axis=2)
                flags = n.concatenate((flags, n.transpose(da["flag"], axes=[3, 2, 1, 0])), axis=2)
        ms.close()

        # Initialize more stuff...
        self.nschan0 = self.nchan

        # set variables for later writing data **some hacks here**
        self.nspect0 = 1
        self.nwide0 = 0
        self.sdf0 = da["axis_info"]["freq_axis"]["resolution"][0][0] * 1e-9
        self.sdf = self.sdf0
        self.ischan0 = 1
        self.sfreq0 = da["axis_info"]["freq_axis"]["chan_freq"][0][0] * 1e-9
        self.sfreq = self.sfreq0
        self.restfreq0 = 0.0
        self.pol0 = -1  # assumes single pol?

        # Assumes MS files store uvw in meters. Corrects by mean frequency of channels in use.
        self.u = u.transpose() * self.freq_orig[0] * (1e9 / 3e8)
        self.v = v.transpose() * self.freq_orig[0] * (1e9 / 3e8)
        self.w = w.transpose() * self.freq_orig[0] * (1e9 / 3e8)

        # set integration time and time axis
        ti = da["axis_info"]["time_axis"]["MJDseconds"]
        self.reltime = ti - ti[0]

        # define relative phase center for each integration
        self.l0 = n.zeros(self.nints)
        self.m0 = n.zeros(self.nints)

        self.rawdata = newda
        self.flags = n.invert(
            flags
        )  # tests show that MS has opposite flag convention as Miriad! using complement of MS flag in tpipe.
        print "Shape of raw data, time:"
        print self.rawdata.shape, self.reltime.shape
Пример #2
0
    def __init__(self, file, nints=1000, nskip=0, ddid=-1, selectpol=['RR','LL'], scan=0, datacol='data'):
        """Initializes the class "obs". This creates new object containing data and metadata for an observation.
        It also includes functions to manipulate data and do transients analysis.
        Note that this uses CASA libraries in a way that requires it to be run from within "casapy".
        Use scan, nints, and nskip to control where to start.
        datacol specifies column of MS to read. Standard MS data columns are: 'data', 'corrected_data', 'model_data'.

        Examples of usage in python/casapy:
        import vla_tpipe
        obs = vla_tpipe.obs() -- create observation object for first file in a directory.
        print obs.data.shape -- see the structure of data read in. dimensions are (time, baseline, channel, polarization)
        results = obs.bisplc(show=1) -- create a bispectrum lightcurve and show any candidate transients. results are returned in 'return' object
        """

        # critical parameters. may need to edit these
        ants = range(28)    # set what antennas to use. default is to use "range" to specify all antennas with range(n_ant+1)
        self.file = file

        # get spw info. either load pickled version (if found) or make new one
        pklname = string.join(file.split('.')[:-1], '.') + '_init.pkl'
        if os.path.exists(pklname):
            print 'Pickle of initializing info found. Loading...'
            pkl = open(pklname, 'r')
            (self.npol_orig, self.npol, self.nbl, self.blarr, self.ants, self.nants, self.nants0, self.nskip, self.inttime, self.inttime0, spwinfo, scansummary) = pickle.load(pkl)
            scanlist = scansummary['summary'].keys()
            starttime_mjd = scansummary['summary'][scanlist[scan]]['0']['BeginTime']
            self.nskip = int(nskip*self.nbl)    # number of iterations to skip (for reading in different parts of buffer)
        else:
            print 'No pickle of initializing info found. Making anew...'
            pkl = open(pklname, 'wb')
            ms.open(self.file)
            spwinfo = ms.getspectralwindowinfo()
            scansummary = ms.getscansummary()
            scanlist = scansummary['summary'].keys()

            starttime_mjd = scansummary['summary'][scanlist[scan]]['0']['BeginTime']
            starttime0 = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+0/(24.*60*60),'d'),form=['ymd'], prec=9), 's'))
            stoptime0 = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+0.5/(24.*60*60), 'd'), form=['ymd'], prec=9), 's'))
            ms.selectinit(datadescid=0)  # initialize to initialize params
            selection = {'time': [starttime0, stoptime0], 'antenna1': ants, 'antenna2': ants}
            ms.select(items = selection)
            da = ms.getdata([datacol,'axis_info'], ifraxis=True)
            ms.close()

            self.npol_orig = da[datacol].shape[0]
            self.npol = len(selectpol)
            self.nbl = da[datacol].shape[2]
            print 'Initializing %d of %d polarizations' % (self.npol, self.npol_orig)
            print 'Initializing nbl:', self.nbl

            # good baselines
            bls = da['axis_info']['ifr_axis']['ifr_shortname']
            self.blarr = n.array([[int(bls[i].split('-')[0]),int(bls[i].split('-')[1])] for i in range(len(bls))])
            self.ants = n.unique(self.blarr)
            self.nants = len(self.ants)
            self.nants0 = len(self.ants)
            print 'Initializing nants:', self.nants
            self.nskip = int(nskip*self.nbl)    # number of iterations to skip (for reading in different parts of buffer)

            # set integration time
            ti0 = da['axis_info']['time_axis']['MJDseconds']
#            self.inttime = n.mean([ti0[i+1] - ti0[i] for i in range(len(ti0)-1)])
            self.inttime = scansummary['summary'][scanlist[scan]]['0']['IntegrationTime']
            self.inttime0 = self.inttime
            print 'Initializing integration time (s):', self.inttime

            pickle.dump((self.npol_orig, self.npol, self.nbl, self.blarr, self.ants, self.nants, self.nants0, self.nskip, self.inttime, self.inttime0, spwinfo, scansummary), pkl)
        pkl.close()

        # read in multiple subbands ("data id" in casa parlance).
        if ddid < 0:
            ddidlist = range(len(spwinfo['spwInfo']))
        else:
            ddidlist = [ddid]

        freq = n.array([])
        for ddid in ddidlist:
            nch = spwinfo['spwInfo'][str(ddid)]['NumChan']
            ch0 = spwinfo['spwInfo'][str(ddid)]['Chan1Freq']
            chw = spwinfo['spwInfo'][str(ddid)]['ChanWidth']
            freq = n.concatenate( (freq, (ch0 + chw * n.arange(nch)) * 1e-9) )

#        self.chans = n.array(range(2,62))  # can flag by omitting channels here
        self.chans = n.arange(nch*len(ddidlist))    # default is to take all chans
        self.freq = freq[self.chans]
        self.nchan = len(self.freq)
        self.track0 = [n.zeros(len(self.chans)), list(self.chans)]
        self.track0 = [n.zeros(len(self.chans)), list(self.chans)]

        # set requested time range based on given parameters
        timeskip = self.inttime*nskip
        starttime = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+timeskip/(24.*60*60),'d'),form=['ymd'], prec=9), 's'))
        stoptime = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+(timeskip+nints*self.inttime)/(24.*60*60), 'd'), form=['ymd'], prec=9), 's'))
        print 'First integration of scan:', qa.time(qa.quantity(starttime_mjd,'d'),form=['ymd'],prec=9)
        print
        print 'Reading from', qa.time(qa.quantity(starttime_mjd+timeskip/(24.*60*60),'d'),form=['hms'], prec=9), 'to', qa.time(qa.quantity(starttime_mjd+(timeskip+nints*self.inttime)/(24.*60*60), 'd'), form=['hms'], prec=9)

        # read data into data structure
        ms.open(self.file)
        ms.selectinit(datadescid=ddidlist[0])  # reset select params for later data selection
        selection = {'time': [starttime, stoptime], 'antenna1': ants, 'antenna2': ants}
        ms.select(items = selection)
        print 'Reading %s, SB %d, polarization %s...' % (datacol, ddidlist[0], selectpol)
        ms.selectpolarization(selectpol)
        da = ms.getdata([datacol,'axis_info'], ifraxis=True)
        if da == {}:
            print 'No data found.'
            return 1
        newda = n.transpose(da[datacol], axes=[3,2,1,0])  # if using multi-pol data.
        if len(ddidlist) > 1:
            for ddid in ddidlist[1:]:
                ms.selectinit(datadescid=ddid)  # reset select params for later data selection
                ms.select(items = selection)
                print 'Reading %s, SB %d, polarization %s...' % (datacol, ddid, selectpol)
                ms.selectpolarization(selectpol)
                da = ms.getdata([datacol,'axis_info'], ifraxis=True)
                newda = n.concatenate( (newda, n.transpose(da[datacol], axes=[3,2,1,0])), axis=2 )
        ms.close()

        # check pol and baseline dimensions of data
        self.npol_orig = da[datacol].shape[0]
        self.npol = len(selectpol)
        self.nbl = da[datacol].shape[2]
        print 'Initializing %d of %d polarizations' % (self.npol, self.npol_orig)
        print 'Initializing nchan:', self.nchan
        print 'Initializing nbl:', self.nbl
        self.nskip = int(nskip*self.nbl)    # number of iterations to skip (for reading in different parts of buffer)

        # create data structures
#        self.rawdata = newda[len(newda)/2:]  # hack to remove autos
        self.rawdata = newda
        self.data = self.rawdata[:,:,self.chans]   # remove channels ignored earlier
        self.dataph = (self.data.mean(axis=3).mean(axis=1)).real   # create dataph, which is sum over all baselines. akin to calculating tied-array beam (possibly without calibration)
        self.min = self.dataph.min()
        self.max = self.dataph.max()
        print 'Shape of rawdata, data:'
        print self.rawdata.shape, self.data.shape
        print 'Dataph min, max:'
        print self.min, self.max

        # set integration time and time axis
        ti = da['axis_info']['time_axis']['MJDseconds']
        self.reltime = ti - ti[0]
Пример #3
0
def blankcube(image,
              dummyMS,
              smooth=True,
              verbose=True,
              region='centerbox[[10h21m45s,18.05.14.9],[15arcmin,15arcmin]]',
              ruthless=False,
              extension='.image',
              beamround='int',
              blankThreshold=2.5,
              moments=[0]):
    '''
    Parameters
    ----------
    image : string
        Base name of image. If target image has extension other than '.image',
        change the extension keyword.

    dummyMS : string
        MS file required for blanking process in CASA

    smooth : bool, optional
        Smooth the image to a circular beam before blanking?
        Default True

    verbose : bool, optional

    region : string, optional
        region parameter featured in CASA

    ruthless : bool, optional
        Delete previous outputs from blankcube

    extension : string, optional
        Extension of the target image. Must include the '.', e.g., '.restored'
        Default '.image'

    beamround : string,float
        P

    blankThreshold : float, optional
        Initial blanking threshold of all pixels scaled by the standard
        deviation times the blankingthreshold
        Default = 2.5 (Walter et al. 2008)

    moments : list, optional
        Moments to calculate from cube. Options are 0,1,2.
        Example: [0,1,2]
        Default: [0]

    Returns
    -------
    out : null

    Examples
    --------

    '''

    from casa import immath, imsmooth, immoments
    from casa import image as ia
    from casa import imager as im
    from casa import quanta as qa
    import os
    import numpy as np

    # Delete files associated with previous runs
    if ruthless:
        os.system('rm -rf ' + image + '.smooth.blk.image')
        os.system('rm -rf ' + image + '.smooth.image')
        os.system('rm -rf ' + image + '.blk.image')
        os.system('rm -rf ' + image + '.mask')

    imageDir = './'

    # Create moment maps
    mom0, mom1, mom2 = False, False, False
    if len(moments) > 0:
        for i, moment in enumerate(moments):
            if moment == 0:
                mom0 = True
            if moment == 1:
                mom1 = True
            if moment == 2:
                mom2 = True
    if mom1 == True or mom2 == True:
        beamScale = 1.01
    elif mom0 == True:
        beamScale = 2.

    # determine beamsize of cube
    ia.open(imageDir + image + extension)
    beamsizes = np.zeros(ia.shape()[2])
    for i in range(ia.shape()[2]):
        beamsizes[i] = ia.restoringbeam(channel=i)['major']['value']
    beamsizeUnit = ia.restoringbeam(channel=i)['major']['unit']
    beamsize = qa.convert(str(beamsizes.max()) + beamsizeUnit,
                          'arcsec')['value']
    if type(beamround) == float:
        beamsize_smooth = beamround * beamsize
    else:
        beamsize_smooth = np.ceil(beamsize)  #beamsize_smooth = 1.01 * beamsize
    ia.close()

    if verbose:
        print 'Max beamsize is ' + str(beamsize) + '"'

    if not os.path.exists(image + '.blk.image'):
        # create cube for blanking
        if smooth:  # smooth to a larger beam if the user desires
            if verbose:
                print 'Convolving to ' + str(beamsize_smooth) + '"'

            imsmooth(imagename=image + extension,
                     outfile=image + '.blk.image',
                     major=str(beamsize_smooth) + 'arcsec',
                     minor=str(beamsize_smooth) + 'arcsec',
                     region=region,
                     pa='0deg',
                     targetres=True)
        else:  # do no smooth
            immath(imagename=image + extension,
                   outfile=image + '.blk.image',
                   mode='evalexpr',
                   region=region,
                   expr='IM0')

    if not os.path.exists(image + '.smooth.image'):
        # convolve cube to 2X beam for blanking
        imsmooth(imagename=image + extension,
                 outfile=image + '.smooth.image',
                 major=str(beamsize * beamScale) + 'arcsec',
                 minor=str(beamsize * beamScale) + 'arcsec',
                 pa='0deg',
                 region=region,
                 targetres=True)

    # determine threshold of cube
    ia.open(image + '.smooth.image')
    threshold = ia.statistics()['sigma'][0] * blankThreshold
    ia.close()

    # blank the cube at threshold*sigma
    ia.open(image + '.smooth.image')
    ia.calcmask(mask=image + '.smooth.image > ' + str(threshold), name='mask1')
    wait = 'waits for calcmask to close'
    ia.close()

    # hand blank the cube
    im.open(dummyMS)
    pause = None
    while pause is None:
        im.drawmask(image=image + '.smooth.image', mask=image + '.mask')
        pause = 0
    im.close

    # mask contains values of 0 and 1, change to a mask with only values of 1
    ia.open(image + '.mask')
    ia.calcmask(image + '.mask' + '>0.5')
    ia.close()

    # apply mask on smoothed image
    immath(imagename=image + '.smooth.image',
           outfile=image + '.smooth.blk.image',
           mode='evalexpr',
           mask='mask(' + image + '.mask)',
           expr='IM0')

    # apply mask on image
    ia.open(imageDir + image + '.blk.image')
    ia.maskhandler('copy', [image + '.smooth.blk.image:mask0', 'newmask'])
    ia.maskhandler('set', 'newmask')
    ia.done()

    cube = '.blk.image'  # specify name of cube for moment calculation

    # create moment 0 map
    if mom0:
        if ruthless:
            os.system('rm -rf ' + image + '.mom0.image')
        immoments(imagename=image + cube,
                  moments=[0],
                  axis='spectra',
                  chans='',
                  mask='mask(' + image + cube + ')',
                  outfile=image + '.mom0.image')

    # create moment 1 map
    if mom1:
        if ruthless:
            os.system('rm -rf ' + image + '.mom1.image')
        immoments(imagename=image + cube,
                  moments=[1],
                  axis='spectra',
                  chans='',
                  mask='mask(' + image + cube + ')',
                  outfile=image + '.mom1.image')

    # create moment 2 map
    if mom2:
        if ruthless:
            os.system('rm -rf ' + image + '.mom2.image')
        immoments(imagename=image + cube,
                  moments=[2],
                  axis='spectra',
                  chans='',
                  mask='mask(' + image + cube + ')',
                  outfile=image + '.mom2.image')

    if verbose and mom0:
        from casa import imstat
        flux = imstat(image + '.mom0.image')['flux'][0]
        ia.open(image + '.mom0.image')
        beammaj = ia.restoringbeam(channel=0)['major']['value']
        beammin = ia.restoringbeam(channel=0)['minor']['value']
        beamsizeUnit = ia.restoringbeam(channel=0)['major']['unit']
        ia.close()
        print 'Moment Image: ' + str(image) + '.mom0.image'
        print 'Beamsize: ' + str(beammaj) + '" X ' + str(beammin) + '"'
        print 'Flux: ' + str(flux) + ' Jy km/s'
Пример #4
0
    def __init__(self, filename, nints=1, nskip=0, selectpol=['XX','YY'], datacol='corrected'):
        """Initializes the class "lofar". This creates new object containing data and metadata for a set of files in a directory.
        It also includes functions to manipulate data and do some analysis, like making lightcurves, etc.
        Note that this uses CASA libraries in a way that requires it to be run from within "casapy". Other options exist elsewhere.
        Default is to read in first file (alphabetically) in the directory. Use nints and nskip to control where to start and number of files to read. this assumes that the alphabetical order is the time order.

        Examples of usage in python/casapy:
        import lofarvis
        obs = lofarvis.lofar('directory') -- create observation object for first file in a directory.
        print obs.data.shape -- see the structure of data read in. dimensions are (time, baseline, channel, polarization)
        results = obs.bisplc(show=1) -- create a bispectrum lightcurve and show any candidate transients. results are returned in 'return' object
        """

        # critical parameters. need to edit these
        ants = range(25)    # set what antennas to use. default is to use "range" to specify all antennas with range(n_ant+1)
        self.chans = n.array(range(1))  # set what channesl to use. default is to use range to select all channels.
        self.track0 = [n.zeros(len(self.chans)), self.chans]

        # open file and read a bit of data to define data structure
        self.file = filename
        print 'Reading ', self.file
        ms.open(self.file)
        spwinfo = ms.getspectralwindowinfo()
        summary = ms.summary()
        scansummary = ms.getscansummary()
        starttime_mjd = scansummary['summary']['0']['0']['BeginTime']
        starttime0 = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+0/(24.*60*60),'d'),form=['ymd'], prec=9), 's'))
        stoptime0 = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+0.5/(24.*60*60), 'd'), form=['ymd'], prec=9), 's'))
        ms.selectinit(datadescid=0)  # initialize to initialize params
        selection = {'time': [starttime0, stoptime0], 'antenna1': ants, 'antenna2': ants}
        da = ms.getdata([datacol,'axis_info', 'u', 'v', 'w'], ifraxis=True)
        ms.close()

        self.npol_orig = da[datacol].shape[0]
        self.npol = len(selectpol)
        self.nbl = da[datacol].shape[2]
        print 'Initializing %d of %d polarizations' % (self.npol, self.npol_orig)
        print 'Initializing nbl:', self.nbl

        # good baselines
        bls = da['axis_info']['ifr_axis']['ifr_shortname']
        self.blarr = n.array([[bls[i].split('-')[0],bls[i].split('-')[1]] for i in range(len(bls))])
        self.ants = n.unique(self.blarr)
        self.nants = len(self.ants)
        self.nants0 = len(self.ants)
        print 'Initializing nants:', self.nants
        self.nskip = int(nskip*self.nbl)    # number of iterations to skip (for reading in different parts of buffer)

        # set integration time
        ti0 = da['axis_info']['time_axis']['MJDseconds']
        self.inttime = scansummary['summary']['0']['0']['IntegrationTime']
        self.inttime0 = self.inttime
        print 'Initializing integration time (s):', self.inttime

        nch = spwinfo['spwInfo']['0']['NumChan']
        ch0 = spwinfo['spwInfo']['0']['Chan1Freq']
        chw = spwinfo['spwInfo']['0']['ChanWidth']
        freq = (ch0 + chw * n.arange(nch)) * 1e-9

        self.freq = freq[self.chans]
        self.nchan = len(self.freq)

        # read data into data structure. start with subband 0, then iterate over higher ones. lofar probably only has 0.
        timeskip = self.inttime*nskip
        starttime = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+timeskip/(24.*60*60),'d'),form=['ymd'], prec=9), 's'))
        stoptime = qa.getvalue(qa.convert(qa.time(qa.quantity(starttime_mjd+(timeskip+nints*self.inttime)/(24.*60*60), 'd'), form=['ymd'], prec=9), 's'))
        print 'First integration:', qa.time(qa.quantity(starttime_mjd,'d'),form=['ymd'],prec=9)
        print
        print 'Reading times', qa.time(qa.quantity(starttime_mjd+timeskip/(24.*60*60),'d'),form=['hms'], prec=9), 'to', qa.time(qa.quantity(starttime_mjd+(timeskip+nints*self.inttime)/(24.*60*60), 'd'), form=['hms'], prec=9)

        ms.open(self.file)
        ms.selectinit(datadescid=0)  # reset select params for later data selection
        selection = {'time': [starttime, stoptime], 'antenna1': ants, 'antenna2': ants}
        ms.select(items = selection)
        print 'Reading SB %d, polarization %s...' % (0, selectpol)
        ms.selectpolarization(selectpol)
        da = ms.getdata([datacol,'axis_info', 'u', 'v', 'w'], ifraxis=True)
        u = da['u']; v = da['v']; w = da['w']
        if da == {}:
            print 'No data found.'
            return 1
        newda = n.transpose(da[datacol], axes=[3,2,1,0])  # if using multi-pol data.
        ms.close()
        rawdata = newda  # array for collecting raw data

        # create data structures
        self.u = u.transpose() * (-self.freq.mean()*1e9/3e8)  # uvw are in m on ground. scale by -wavelenth to get projected lamba uvw (as in miriad?)
        self.v = v.transpose() * (-self.freq.mean()*1e9/3e8)
        self.w = w.transpose() * (-self.freq.mean()*1e9/3e8)
        self.rawdata = rawdata
        self.data = rawdata[:,:,self.chans]   # remove channels ignored earlier
        self.dataph = (self.data.mean(axis=3).mean(axis=1)).real   # create dataph, which is sum over all baselines. akin to calculating tied-array beam (possibly without calibration)
        self.min = self.dataph.min()
        self.max = self.dataph.max()
        print 'Shape of rawdata, data:'
        print self.rawdata.shape, self.data.shape
        print 'Dataph min, max:'
        print self.min, self.max
        ti = da['axis_info']['time_axis']['MJDseconds']
        self.reltime = ti - ti[0]
Пример #5
0
def blankcube(image,
              dummyMS,
              smooth=True,
              verbose=True,
              region='centerbox[[10h21m45s,18.05.14.9],[15arcmin,15arcmin]]',
              ruthless=False,
              extension='.image',
              beamround='int',
              blankThreshold=2.5,
              moments=[0]):

    '''
    Parameters
    ----------
    image : string
        Base name of image. If target image has extension other than '.image',
        change the extension keyword.

    dummyMS : string
        MS file required for blanking process in CASA

    smooth : bool, optional
        Smooth the image to a circular beam before blanking?
        Default True

    verbose : bool, optional

    region : string, optional
        region parameter featured in CASA

    ruthless : bool, optional
        Delete previous outputs from blankcube

    extension : string, optional
        Extension of the target image. Must include the '.', e.g., '.restored'
        Default '.image'

    beamround : string,float
        P

    blankThreshold : float, optional
        Initial blanking threshold of all pixels scaled by the standard
        deviation times the blankingthreshold
        Default = 2.5 (Walter et al. 2008)

    moments : list, optional
        Moments to calculate from cube. Options are 0,1,2.
        Example: [0,1,2]
        Default: [0]

    Returns
    -------
    out : null

    Examples
    --------

    '''

    from casa import immath,imsmooth,immoments
    from casa import image as ia
    from casa import imager as im
    from casa import quanta as qa
    import os
    import numpy as np

    # Delete files associated with previous runs
    if ruthless:
        os.system('rm -rf ' + image + '.smooth.blk.image')
        os.system('rm -rf ' + image + '.smooth.image')
        os.system('rm -rf ' + image + '.blk.image')
        os.system('rm -rf ' + image + '.mask')

    imageDir = './'

    # Create moment maps
    mom0,mom1,mom2 = False,False,False
    if len(moments) > 0:
        for i, moment in enumerate(moments):
            if moment == 0:
                mom0 = True
            if moment == 1:
                mom1 = True
            if moment == 2:
                mom2 = True
    if mom1 == True or mom2 == True:
        beamScale = 1.01
    elif mom0 == True:
        beamScale = 2.

    # determine beamsize of cube
    ia.open(imageDir + image + extension)
    beamsizes = np.zeros(ia.shape()[2])
    for i in range(ia.shape()[2]):
        beamsizes[i] = ia.restoringbeam(channel=i)['major']['value']
    beamsizeUnit = ia.restoringbeam(channel=i)['major']['unit']
    beamsize = qa.convert(str(beamsizes.max()) + beamsizeUnit,'arcsec')['value']
    if type(beamround) == float:
        beamsize_smooth = beamround*beamsize
    else:
        beamsize_smooth = np.ceil(beamsize)   #beamsize_smooth = 1.01 * beamsize
    ia.close()

    if verbose:
        print 'Max beamsize is ' + str(beamsize) + '"'

    if not os.path.exists(image + '.blk.image'):
        # create cube for blanking
        if smooth: # smooth to a larger beam if the user desires
            if verbose:
                print 'Convolving to ' + str(beamsize_smooth) + '"'

            imsmooth(imagename=image + extension,
                     outfile=image + '.blk.image',
                     major=str(beamsize_smooth) + 'arcsec',
                     minor=str(beamsize_smooth) + 'arcsec',
                     region=region,
                     pa='0deg',
                     targetres=True)
        else: # do no smooth
            immath(imagename=image + extension,
               outfile=image + '.blk.image',
                mode='evalexpr',
                region=region,
                expr='IM0')

    if not os.path.exists(image + '.smooth.image'):
        # convolve cube to 2X beam for blanking
        imsmooth(imagename=image + extension,
             outfile=image + '.smooth.image',
             major=str(beamsize*beamScale) + 'arcsec',
             minor=str(beamsize*beamScale) + 'arcsec',
             pa='0deg',
             region=region,
             targetres=True)

    # determine threshold of cube
    ia.open(image + '.smooth.image')
    threshold = ia.statistics()['sigma'][0] * blankThreshold
    ia.close()

    # blank the cube at threshold*sigma
    ia.open(image + '.smooth.image')
    ia.calcmask(mask=image + '.smooth.image > ' + str(threshold),
             name='mask1')
    wait = 'waits for calcmask to close'
    ia.close()

    # hand blank the cube
    im.open(dummyMS)
    pause = None
    while pause is None:
        im.drawmask(image=image + '.smooth.image',
                           mask=image + '.mask')
        pause = 0
    im.close

    # mask contains values of 0 and 1, change to a mask with only values of 1
    ia.open(image + '.mask')
    ia.calcmask(image + '.mask' + '>0.5')
    ia.close()

    # apply mask on smoothed image
    immath(imagename=image + '.smooth.image',
       outfile=image + '.smooth.blk.image',
       mode='evalexpr',
       mask='mask(' + image + '.mask)',
       expr='IM0')

    # apply mask on image
    ia.open(imageDir + image + '.blk.image')
    ia.maskhandler('copy',[image + '.smooth.blk.image:mask0', 'newmask'])
    ia.maskhandler('set','newmask')
    ia.done()

    cube = '.blk.image' # specify name of cube for moment calculation


    # create moment 0 map
    if mom0:
        if ruthless:
            os.system('rm -rf ' + image + '.mom0.image')
        immoments(imagename=image + cube,
                  moments=[0],
                  axis='spectra',
                  chans='',
                  mask='mask(' + image + cube + ')',
                  outfile=image + '.mom0.image')

    # create moment 1 map
    if mom1:
        if ruthless:
            os.system('rm -rf ' + image + '.mom1.image')
        immoments(imagename=image + cube,
                  moments=[1],
                  axis='spectra',
                  chans='',
                  mask='mask(' + image + cube + ')',
                  outfile=image + '.mom1.image')

    # create moment 2 map
    if mom2:
        if ruthless:
            os.system('rm -rf ' + image + '.mom2.image')
        immoments(imagename=image + cube,
                  moments=[2],
                  axis='spectra',
                  chans='',
                  mask='mask(' + image + cube + ')',
                  outfile=image + '.mom2.image')

    if verbose and mom0:
	from casa import imstat
        flux = imstat(image + '.mom0.image')['flux'][0]
        ia.open(image + '.mom0.image')
        beammaj = ia.restoringbeam(channel=0)['major']['value']
        beammin = ia.restoringbeam(channel=0)['minor']['value']
        beamsizeUnit = ia.restoringbeam(channel=0)['major']['unit']
        ia.close()
        print 'Moment Image: ' + str(image) + '.mom0.image'
        print 'Beamsize: ' + str(beammaj) + '" X ' + str(beammin) + '"'
        print 'Flux: ' + str(flux) + ' Jy km/s'