예제 #1
0
    def test_readAndParseTbuff(self):
        '''flaghelper: compare the read and parse and apply tbuff'''
        print ''
        
        # MJD in seconds of timeranges are these
        # <startTime>4891227930515540000 <endTime>4891227932453838000
        # <startTime>4891228473545856000 <endTime>4891228473731891000
        # <startTime>4891226924455911000 <endTime>4891226927502314000
        # <startTime>4891228838164987000 <endTime>4891228838418996000
        # <startTime>4891228609440808000 <endTime>4891228612489617000

        online = ["antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'",
                  "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'",
                  "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'",
                  "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'",
                  "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"]

        myinput = "antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'\n"\
                  "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'\n"\
                  "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'\n"\
                  "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'\n"\
                  "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"
        
        filename1 = 'flaghelperonline2.txt'
        create_input(myinput, filename1)
        
        # First timerange from online before padding
        origt = timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'
        
        # Apply tbuff to timeranges
        timebuffer = 1.1
        dlist1 = fh.readAndParse([filename1], tbuff=timebuffer)
        self.assertEqual(len(dlist1), 5)
        
        # Get the first padded timerange from output
        padt = dlist1[0]['timerange']
        
        # Revert the tbuff application manually
        t0,t1 = padt.split('~',1)
        startTime = qa.totime(t0)['value']
        startTimeSec = float((startTime * 24 * 3600) + timebuffer)
        startTimeSec = qa.quantity(startTimeSec, 's')
        paddedT0 = qa.time(startTimeSec,form='ymd',prec=9)[0]
        # end time
        endTime = qa.totime(t1)['value']
        endTimeSec = float((endTime * 24 * 3600) - timebuffer)
        endTimeSec = qa.quantity(endTimeSec, 's')
        paddedT1 = qa.time(endTimeSec,form='ymd',prec=9)[0]
        
        newtimerange =  paddedT0+'~'+paddedT1
        
        # Compare with the original
        self.assertEqual(origt, newtimerange)
        
        # Compare with original values from Flag.xml
        xmlt0 = float(4891227930515540000) * 1.0E-9
        xmlt1 = float(4891227932453838000) * 1.0E-9
        
        self.assertAlmostEqual(xmlt0, startTimeSec['value'], places=3)
        self.assertAlmostEqual(xmlt1, endTimeSec['value'], places=3)
예제 #2
0
파일: dspec2.py 프로젝트: hayesla/suncasa
 def format_coord(x, y):
     col = np.argmin(np.absolute(tim - x))
     row = np.argmin(np.absolute(freqghz - y))
     if col >= 0 and col < ntim and row >= 0 and row < nfreq:
         timv = tim[col]
         timstr = qa.time(qa.quantity(timv, 's'),
                          form='clean',
                          prec=9)[0]
         flux = spec_plt[row, col]
         return 'time {0} = {1}, freq = {2:.3f} GHz, flux = {3:.2f} Jy'.format(
             col, timstr, y, flux)
     else:
         return 'x = {0}, y = {1:.3f}'.format(x, y)
예제 #3
0
def read_msinfo(vis=None, msinfofile=None):
    # read MS information #
    msinfo = dict.fromkeys(['vis', 'scans', 'fieldids', 'btimes', 'btimestr', \
                            'inttimes', 'ras', 'decs'])
    ms.open(vis)
    scans = ms.getscansummary()
    scanids = sorted(scans.keys(), key=lambda x: int(x))
    nscanid = len(scanids)
    btimes = []
    btimestr = []
    etimes = []
    fieldids = []
    inttimes = []
    dirs = []
    ras = []
    decs = []
    for i in range(nscanid):
        btimes.append(scans[scanids[i]]['0']['BeginTime'])
        etimes.append(scans[scanids[i]]['0']['EndTime'])
        fieldid = scans[scanids[i]]['0']['FieldId']
        fieldids.append(fieldid)
        dir = ms.getfielddirmeas('PHASE_DIR', fieldid)
        dirs.append(dir)
        ras.append(dir['m0'])
        decs.append(dir['m1'])
        inttimes.append(scans[scanids[i]]['0']['IntegrationTime'])
    ms.close()
    btimestr = [qa.time(qa.quantity(btimes[i], 'd'), form='fits', prec=10)[0] \
                for i in range(nscanid)]
    msinfo['vis'] = vis
    msinfo['scans'] = scans
    msinfo['fieldids'] = fieldids
    msinfo['btimes'] = btimes
    msinfo['btimestr'] = btimestr
    msinfo['inttimes'] = inttimes
    msinfo['ras'] = ras
    msinfo['decs'] = decs
    if msinfofile:
        np.savez(msinfofile, vis=vis, scans=scans, fieldids=fieldids, \
                 btimes=btimes, btimestr=btimestr, inttimes=inttimes, \
                 ras=ras, decs=decs)
    return msinfo
예제 #4
0
def subvs2(vis=None,
           outputvis=None,
           timerange='',
           spw='',
           mode=None,
           subtime1=None,
           subtime2=None,
           smoothaxis=None,
           smoothtype=None,
           smoothwidth=None,
           splitsel=None,
           reverse=None,
           overwrite=None):
    """Perform vector subtraction for visibilities
    Keyword arguments:
    vis -- Name of input visibility file (MS)
            default: none; example: vis='ngc5921.ms'
    outputvis -- Name of output uv-subtracted visibility file (MS)
                  default: none; example: outputvis='ngc5921_src.ms'
    timerange -- Time range of performing the UV subtraction:
                 default='' means all times.  examples:
                 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
                 timerange = 'hh:mm:ss~hh:mm:ss'
    spw -- Select spectral window/channel.
           default = '' all the spectral channels. Example: spw='0:1~20'
    mode -- operation mode
            default 'linear' 
                mode = 'linear': use a linear fit for the background to be subtracted
                mode = 'lowpass': act as a lowpass filter---smooth the data using different
                        smooth types and window sizes. Can be performed along either time
                        or frequency axis
                mode = 'highpass': act as a highpass filter---smooth the data first, and 
                        subtract the smoothed data from the original. Can be performed along
                        either time or frequency axis
            mode = 'linear' expandable parameters:
                subtime1 -- Time range 1 of the background to be subtracted from the data 
                             default='' means all times.  format:
                             timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
                             timerange = 'hh:mm:ss~hh:mm:ss'
                subtime2 -- Time range 2 of the backgroud to be subtracted from the data
                             default='' means all times.  examples:
                             timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
                             timerange = 'hh:mm:ss~hh:mm:ss'
            mode = 'lowpass' or 'highpass' expandable parameters:
                smoothaxis -- axis of smooth
                    Default: 'time'
                    smoothaxis = 'time': smooth is along the time axis
                    smoothaxis = 'freq': smooth is along the frequency axis
                smoothtype -- type of the smooth depending on the convolving kernel
                    Default: 'flat'
                    smoothtype = 'flat': convolving kernel is a flat rectangle,
                            equivalent to a boxcar moving smooth
                    smoothtype = 'hanning': Hanning smooth kernel. See numpy.hanning
                    smoothtype = 'hamming': Hamming smooth kernel. See numpy.hamming
                    smoothtype = 'bartlett': Bartlett smooth kernel. See numpy.bartlett
                    smoothtype = 'blackman': Blackman smooth kernel. See numpy.blackman
                smoothwidth -- width of the smooth kernel
                    Default: 5
                    Examples: smoothwidth=5, meaning the width is 5 pixels
    splitsel -- True or False. default = False. If splitsel = False, then the entire input
            measurement set is copied as the output measurement set (outputvis), with 
            background subtracted at selected timerange and spectral channels. 
            If splitsel = True,then only the selected timerange and spectral channels 
            are copied into the output measurement set (outputvis).
    reverse -- True or False. default = False. If reverse = False, then the times indicated
            by subtime1 and/or subtime2 are treated as background and subtracted; If reverse
            = True, then reverse the sign of the background-subtracted data. The option can 
            be used for mapping absorptive structure.
    overwrite -- True or False. default = False. If overwrite = True and
                outputvis already exists, the selected subtime and spw in the 
                output measurment set will be replaced with background subtracted 
                visibilities

    """
    # check the visbility ms
    casalog.post('input parameters:')
    casalog.post('vis: ' + vis)
    casalog.post('outputvis: ' + outputvis)
    casalog.post('smoothaxis: ' + smoothaxis)
    casalog.post('smoothtype: ' + smoothtype)
    casalog.post('smoothwidth: ' + str(smoothwidth))
    if not outputvis or outputvis.isspace():
        raise (ValueError, 'Please specify outputvis')

    if os.path.exists(outputvis):
        if overwrite:
            print(
                "The already existing output measurement set will be updated.")
        else:
            raise (ValueError,
                   "Output MS %s already exists - will not overwrite." %
                   outputvis)
    else:
        if not splitsel:
            shutil.copytree(vis, outputvis)
        else:
            ms.open(vis, nomodify=True)
            ms.split(outputvis, spw=spw, time=timerange, whichcol='DATA')
            ms.close()

    if timerange and (type(timerange) == str):
        [btimeo, etimeo] = timerange.split('~')
        btimeosec = qa.getvalue(qa.convert(qa.totime(btimeo), 's'))
        etimeosec = qa.getvalue(qa.convert(qa.totime(etimeo), 's'))
        timebinosec = etimeosec - btimeosec
        if timebinosec < 0:
            raise Exception(
                'Negative timebin! Please check the "timerange" parameter.')
        casalog.post('Selected timerange: ' + timerange +
                     ' as the time for UV subtraction.')
    else:
        casalog.post(
            'Output timerange not specified, using the entire timerange')

    if spw and (type(spw) == str):
        spwlist = spw.split(';')
    else:
        casalog.post('spw not specified, use all frequency channels')

    # read the output data
    datams = mstool()
    datams.open(outputvis, nomodify=False)
    datamsmd = msmdtool()
    datamsmd.open(outputvis)
    spwinfod = datams.getspectralwindowinfo()
    spwinfok = spwinfod.keys()
    spwinfok.sort(key=int)
    spwinfol = [spwinfod[k] for k in spwinfok]
    for s, spi in enumerate(spwinfol):
        print('processing spectral window {}'.format(spi['SpectralWindowId']))
        datams.selectinit(reset=True)
        staql = {'time': '', 'spw': ''}
        if not splitsel:
            # outputvis is identical to input visibility, do the selection
            if timerange and (type(timerange == str)):
                staql['time'] = timerange
            if spw and (type(spw) == str):
                staql['spw'] = spwlist[s]
            if not spw and not timerange:
                # data selection is not made
                print('selecting all spws and times')
                staql['spw'] = str(spi['SpectralWindowId'])
        else:
            # outputvis is splitted, selections have already applied, select all the data
            print('split the selected spws and times')
            staql['spw'] = str(spi['SpectralWindowId'])
        datams.msselect(staql)
        orec = datams.getdata(['data', 'time', 'axis_info'], ifraxis=True)
        npol, nchan, nbl, ntim = orec['data'].shape
        print('dimension of output data', orec['data'].shape)
        casalog.post('Number of baselines: ' + str(nbl))
        casalog.post('Number of spectral channels: ' + str(nchan))
        casalog.post('Number of time pixels: ' + str(ntim))

        try:
            if mode == 'linear':
                # define and check the background time ranges
                if subtime1 and (type(subtime1) == str):
                    [bsubtime1, esubtime1] = subtime1.split('~')
                    bsubtime1sec = qa.getvalue(
                        qa.convert(qa.totime(bsubtime1), 's'))
                    esubtime1sec = qa.getvalue(
                        qa.convert(qa.totime(esubtime1), 's'))
                    timebin1sec = esubtime1sec - bsubtime1sec
                    if timebin1sec < 0:
                        raise Exception(
                            'Negative timebin! Please check the "subtime1" parameter.'
                        )
                    casalog.post('Selected timerange 1: ' + subtime1 +
                                 ' as background for uv subtraction.')
                else:
                    raise Exception(
                        'Please enter at least one timerange as the background'
                    )
                if subtime2 and (type(subtime2) == str):
                    [bsubtime2, esubtime2] = subtime2.split('~')
                    bsubtime2sec = qa.getvalue(
                        qa.convert(qa.totime(bsubtime2), 's'))
                    esubtime2sec = qa.getvalue(
                        qa.convert(qa.totime(esubtime2), 's'))
                    timebin2sec = esubtime2sec - bsubtime2sec
                    if timebin2sec < 0:
                        raise Exception(
                            'Negative timebin! Please check the "subtime2" parameter.'
                        )
                    timebin2 = str(timebin2sec) + 's'
                    casalog.post('Selected timerange 2: ' + subtime2 +
                                 ' as background for uv subtraction.')
                    # plus 1s is to ensure averaging over the entire timerange
                else:
                    casalog.post(
                        'Timerange 2 not selected, using only timerange 1 as background'
                    )

                # Select the background indicated by subtime1
                ms.open(vis, nomodify=True)
                # Select the spw id
                # ms.msselect({'time': subtime1})
                staql0 = {'time': subtime1, 'spw': ''}
                if spw and (type(spw) == str):
                    staql0['spw'] = spwlist[s]
                else:
                    staql0['spw'] = staql['spw']
                ms.msselect(staql0)
                rec1 = ms.getdata(['data', 'time', 'axis_info'], ifraxis=True)
                # print('shape of the frequency matrix ',rec1['axis_info']['freq_axis']['chan_freq'].shape)
                sz1 = rec1['data'].shape
                print('dimension of selected background 1', rec1['data'].shape)
                # the data shape is (n_pol,n_channel,n_baseline,n_time), no need to reshape
                # rec1['data']=rec1['data'].reshape(sz1[0],sz1[1],sz1[2],nspw,sz1[3]/nspw,order='F')
                # print('reshaped rec1 ', rec1['data'].shape)
                rec1avg = np.average(rec1['data'], axis=3)
                casalog.post('Averaging the visibilities in subtime1: ' +
                             subtime1)
                ms.close()
                if subtime2 and (type(subtime2) == str):
                    ms.open(vis, nomodify=True)
                    # Select the spw id
                    staql0 = {'time': subtime2, 'spw': ''}
                    if spw and (type(spw) == str):
                        staql0['spw'] = spwlist[s]
                    else:
                        staql0['spw'] = staql['spw']
                    ms.msselect(staql0)
                    rec2 = ms.getdata(['data', 'time', 'axis_info'],
                                      ifraxis=True)
                    sz2 = rec2['data'].shape
                    print('dimension of selected background 2',
                          rec2['data'].shape)
                    # rec2['data']=rec2['data'].reshape(sz2[0],sz2[1],sz2[2],nspw,sz2[3]/nspw,order='F')
                    # print('reshaped rec1 ', rec2['data'].shape)
                    rec2avg = np.average(rec2['data'], axis=3)
                    ms.close()
                    casalog.post('Averaged the visibilities in subtime2: ' +
                                 subtime2)
                if subtime1 and (not subtime2):
                    casalog.post(
                        'Only "subtime1" is defined, subtracting background defined in subtime1: '
                        + subtime1)
                    t1 = (np.amax(rec1['time']) + np.amin(rec1['time'])) / 2.
                    print('t1: ',
                          qa.time(qa.quantity(t1, 's'), form='ymd', prec=10))
                    for i in range(ntim):
                        orec['data'][:, :, :, i] -= rec1avg
                        if reverse:
                            orec['data'][:, :, :,
                                         i] = -orec['data'][:, :, :, i]
                if subtime1 and subtime2 and (type(subtime2) == str):
                    casalog.post(
                        'Both subtime1 and subtime2 are specified, doing linear interpolation between "subtime1" and "subtime2"'
                    )
                    t1 = (np.amax(rec1['time']) + np.amin(rec1['time'])) / 2.
                    t2 = (np.amax(rec2['time']) + np.amin(rec2['time'])) / 2.
                    touts = orec['time']
                    print('t1: ',
                          qa.time(qa.quantity(t1, 's'), form='ymd', prec=10))
                    print('t2: ',
                          qa.time(qa.quantity(t2, 's'), form='ymd', prec=10))
                    for i in range(ntim):
                        tout = touts[i]
                        if tout > np.amax([t1, t2]):
                            tout = np.amax([t1, t2])
                        elif tout < np.amin([t1, t2]):
                            tout = np.amin([t1, t2])
                        orec['data'][:, :, :, i] -= (rec2avg - rec1avg) * (
                            tout - t1) / (t2 - t1) + rec1avg
                        if reverse:
                            orec['data'][:, :, :,
                                         i] = -orec['data'][:, :, :, i]
            elif mode == 'highpass':
                if smoothtype != 'flat' and smoothtype != 'hanning' and smoothtype != 'hamming' and smoothtype != 'bartlett' and smoothtype != 'blackman':
                    raise Exception('Unknown smoothtype ' + str(smoothtype))
                if smoothaxis == 'time':
                    if smoothwidth <= 0 or smoothwidth >= ntim:
                        raise Exception(
                            'Specified smooth width is <=0 or >= the total number of '
                            + smoothaxis)
                    else:
                        for i in range(orec['data'].shape[0]):
                            for j in range(nchan):
                                for k in range(nbl):
                                    orec['data'][i, j,
                                                 k, :] -= signalsmooth.smooth(
                                                     orec['data'][i, j, k, :],
                                                     smoothwidth, smoothtype)
                if smoothaxis == 'freq':
                    if smoothwidth <= 0 or smoothwidth >= nchan:
                        raise Exception(
                            'Specified smooth width is <=0 or >= the total number of '
                            + smoothaxis)
                    else:
                        for i in range(orec['data'].shape[0]):
                            for j in range(nbl):
                                for k in range(ntim):
                                    orec['data'][i, :, j,
                                                 k] -= signalsmooth.smooth(
                                                     orec['data'][i, :, j, k],
                                                     smoothwidth, smoothtype)
            elif mode == 'lowpass':
                if smoothtype != 'flat' and smoothtype != 'hanning' and smoothtype != 'hamming' and smoothtype != 'bartlett' and smoothtype != 'blackman':
                    raise Exception('Unknown smoothtype ' + str(smoothtype))
                if smoothaxis == 'time':
                    if smoothwidth <= 0 or smoothwidth >= ntim:
                        raise Exception(
                            'Specified smooth width is <=0 or >= the total number of '
                            + smoothaxis)
                    else:
                        for i in range(orec['data'].shape[0]):
                            for j in range(nchan):
                                for k in range(nbl):
                                    orec['data'][i, j,
                                                 k, :] = signalsmooth.smooth(
                                                     orec['data'][i, j, k, :],
                                                     smoothwidth, smoothtype)
                if smoothaxis == 'freq':
                    if smoothwidth <= 0 or smoothwidth >= nchan:
                        raise Exception(
                            'Specified smooth width is <=0 or >= the total number of '
                            + smoothaxis)
                    else:
                        for i in range(orec['data'].shape[0]):
                            for j in range(nbl):
                                for k in range(ntim):
                                    orec['data'][i, :, j,
                                                 k] = signalsmooth.smooth(
                                                     orec['data'][i, :, j, k],
                                                     smoothwidth, smoothtype)
            else:
                raise Exception('Unknown mode' + str(mode))
        except Exception as instance:
            print('*** Error ***', instance)

        # orec['data']=orec['data'].reshape(szo[0],szo[1],szo[2],szo[3],order='F')
        # put the modified data back into the output visibility set
        del orec['time']
        del orec['axis_info']
        # ms.open(outputvis,nomodify=False)
        # if not splitsel:
        # outputvis is identical to input visibility, do the selection
        #    if timerange and (type(timerange==str)):
        #        datams.msselect({'time':timerange})
        #    if spw and (type(spw)==str):
        #        datams.selectinit(datadescid=int(spwid))
        #        nchan=int(echan)-int(bchan)+1
        #        datams.selectchannel(nchan,int(bchan),1,1)
        #    if not spw and not timerange:
        # data selection is not made
        #        datams.selectinit(datadescid=0)
        # else:
        # outputvis is splitted, selections have already applied, select all the data
        #    datams.selectinit(datadescid=0)
        datams.putdata(orec)
    datams.close()
    datamsmd.done()
예제 #5
0
def ptclean(vis, imageprefix, ncpu, twidth, doreg, overwrite, ephemfile,
            msinfofile, outlierfile, field, spw, selectdata, timerange,
            uvrange, antenna, scan, observation, intent, mode, resmooth,
            gridmode, wprojplanes, facets, cfcache, rotpainc, painc, aterm,
            psterm, mterm, wbawp, conjbeams, epjtable, interpolation, niter,
            gain, threshold, psfmode, imagermode, ftmachine, mosweight,
            scaletype, multiscale, negcomponent, smallscalebias, interactive,
            mask, nchan, start, width, outframe, veltype, imsize, cell,
            phasecenter, restfreq, stokes, weighting, robust, uvtaper,
            outertaper, innertaper, modelimage, restoringbeam, pbcor, minpb,
            usescratch, noise, npixels, npercycle, cyclefactor, cyclespeedup,
            nterms, reffreq, chaniter, flatnoise, allowchunk):
    if not (type(ncpu) is int):
        casalog.post('ncpu should be an integer')
        ncpu = 8

    if doreg:
        # check if ephemfile and msinfofile exist
        try:
            ephem = vla_prep.read_horizons(ephemfile=ephemfile)
        except ValueError:
            print("error in reading ephemeris file")
        if not os.path.isfile(msinfofile):
            print("msinfofile does not exist!")
    else:
        ephem = None

    # get number of time pixels
    ms.open(vis)
    ms.selectinit()
    timfreq = ms.getdata(['time', 'axis_info'], ifraxis=True)
    tim = timfreq['time']
    # dt = tim[1]-tim[0] #need to change to median of all time intervals
    dt = np.median(np.diff(tim))
    freq = timfreq['axis_info']['freq_axis']['chan_freq'].flatten()
    ms.close()

    if twidth < 1 or twidth > len(tim):
        casalog.post(
            'twidth not between 1 and # of time pixels in the dataset. Change to 1'
        )
        twidth = 1

    # find out the start and end time index according to the parameter timerange
    # if not defined (empty string), use start and end from the entire time of the ms
    if not timerange:
        btidx = 0
        etidx = len(tim) - 1
    else:
        try:
            (tstart, tend) = timerange.split('~')
            bt_s = qa.convert(qa.quantity(tstart, 's'), 's')['value']
            et_s = qa.convert(qa.quantity(tend, 's'), 's')['value']
            # only time is given but not date, add the date (at 0 UT) from the first record
            if bt_s < 86400. or et_s < 86400.:
                bt_s += np.fix(
                    qa.convert(qa.quantity(tim[0], 's'),
                               'd')['value']) * 86400.
                et_s += np.fix(
                    qa.convert(qa.quantity(tim[0], 's'),
                               'd')['value']) * 86400.
            btidx = np.argmin(np.abs(tim - bt_s))
            etidx = np.argmin(np.abs(tim - et_s))
            # make the indice back to those bracket by the timerange
            if tim[btidx] < bt_s:
                btidx += 1
            if tim[etidx] > et_s:
                etidx -= 1
            if etidx <= btidx:
                print "ending time must be greater than starting time"
                print "reinitiating to the entire time range"
                btidx = 0
                etidx = len(tim) - 1
        except ValueError:
            print "keyword 'timerange' has a wrong format"

    btstr = qa.time(qa.quantity(tim[btidx], 's'), prec=9, form='fits')[0]
    etstr = qa.time(qa.quantity(tim[etidx], 's'), prec=9, form='fits')[0]

    iterable = range(btidx, etidx + 1, twidth)
    print 'First time pixel: ' + btstr
    print 'Last time pixel: ' + etstr
    print str(len(iterable)) + ' images to clean...'

    res = []
    # partition
    clnpart = partial(
        clean_iter, tim, freq, vis, imageprefix, ncpu, twidth, doreg,
        overwrite, ephemfile, ephem, msinfofile, outlierfile, field, spw,
        selectdata, uvrange, antenna, scan, observation, intent, mode,
        resmooth, gridmode, wprojplanes, facets, cfcache, rotpainc, painc,
        aterm, psterm, mterm, wbawp, conjbeams, epjtable, interpolation, niter,
        gain, threshold, psfmode, imagermode, ftmachine, mosweight, scaletype,
        multiscale, negcomponent, smallscalebias, interactive, mask, nchan,
        start, width, outframe, veltype, imsize, cell, phasecenter, restfreq,
        stokes, weighting, robust, uvtaper, outertaper, innertaper, modelimage,
        restoringbeam, pbcor, minpb, usescratch, noise, npixels, npercycle,
        cyclefactor, cyclespeedup, nterms, reffreq, chaniter, flatnoise,
        allowchunk)
    timelapse = 0
    t0 = time()
    # parallelization
    para = 1
    if para:
        casalog.post('Perform clean in parallel ...')
        pool = mp.Pool(ncpu)
        # res = pool.map_async(clnpart, iterable)
        res = pool.map(clnpart, iterable)
        pool.close()
        pool.join()
    else:
        for i in iterable:
            res.append(clnpart(i))

    t1 = time()
    timelapse = t1 - t0
    print 'It took %f secs to complete' % timelapse
    # repackage this into a single dictionary
    results = {'succeeded': [], 'timestamps': [], 'imagenames': []}
    for r in res:
        results['succeeded'].append(r[0])
        results['timestamps'].append(r[1])
        results['imagenames'].append(r[2])

    return results
예제 #6
0
def clean_iter(tim, freq, vis, imageprefix, ncpu, twidth, doreg, overwrite,
               ephemfile, ephem, msinfofile, outlierfile, field, spw,
               selectdata, uvrange, antenna, scan, observation, intent, mode,
               resmooth, gridmode, wprojplanes, facets, cfcache, rotpainc,
               painc, aterm, psterm, mterm, wbawp, conjbeams, epjtable,
               interpolation, niter, gain, threshold, psfmode, imagermode,
               ftmachine, mosweight, scaletype, multiscale, negcomponent,
               smallscalebias, interactive, mask, nchan, start, width,
               outframe, veltype, imsize, cell, phasecenter, restfreq, stokes,
               weighting, robust, uvtaper, outertaper, innertaper, modelimage,
               restoringbeam, pbcor, minpb, usescratch, noise, npixels,
               npercycle, cyclefactor, cyclespeedup, nterms, reffreq, chaniter,
               flatnoise, allowchunk, btidx):
    from taskinit import ms
    from taskinit import qa
    # from  __casac__.quanta import quanta as qa
    from __main__ import default, inp
    from clean import clean
    bt = btidx  # 0
    if bt + twidth < len(tim) - 1:
        et = btidx + twidth - 1
    else:
        et = len(tim) - 1

    # tim_d = tim/3600./24.-np.fix(tim/3600./24.)

    if bt == 0:
        bt_d = tim[bt] - ((tim[bt + 1] - tim[bt]) / 2)
    else:
        bt_d = tim[bt] - ((tim[bt] - tim[bt - 1]) / 2)
    if et == (len(tim) - 1) or et == -1:
        et_d = tim[et] + ((tim[et] - tim[et - 1]) / 2)
    else:
        et_d = tim[et] + ((tim[et + 1] - tim[et]) / 2)

    #
    # bt_d=tim[bt]
    # et_d=tim[et]+0.005

    timerange = qa.time(qa.quantity(bt_d, 's'), prec=9)[0] + '~' + \
                qa.time(qa.quantity(et_d, 's'), prec=9)[0]
    tmid = (bt_d + et_d) / 2.
    btstr = qa.time(qa.quantity(bt_d, 's'), prec=9, form='fits')[0]
    print 'cleaning timerange: ' + timerange

    try:
        image0 = btstr.replace(':', '').replace('-', '')
        imname = imageprefix + image0
        if overwrite or (len(glob.glob(imname + '*')) == 0):
            # inp(taskname = 'clean')
            os.system('rm -rf {}*'.format(imname))
            clean(vis=vis,
                  imagename=imname,
                  outlierfile=outlierfile,
                  field=field,
                  spw=spw,
                  selectdata=selectdata,
                  timerange=timerange,
                  uvrange=uvrange,
                  antenna=antenna,
                  scan=scan,
                  observation=str(observation),
                  intent=intent,
                  mode=mode,
                  resmooth=resmooth,
                  gridmode=gridmode,
                  wprojplanes=wprojplanes,
                  facets=facets,
                  cfcache=cfcache,
                  rotpainc=rotpainc,
                  painc=painc,
                  psterm=psterm,
                  aterm=aterm,
                  mterm=mterm,
                  wbawp=wbawp,
                  conjbeams=conjbeams,
                  epjtable=epjtable,
                  interpolation=interpolation,
                  niter=niter,
                  gain=gain,
                  threshold=threshold,
                  psfmode=psfmode,
                  imagermode=imagermode,
                  ftmachine=ftmachine,
                  mosweight=mosweight,
                  scaletype=scaletype,
                  multiscale=multiscale,
                  negcomponent=negcomponent,
                  smallscalebias=smallscalebias,
                  interactive=interactive,
                  mask=mask,
                  nchan=nchan,
                  start=start,
                  width=width,
                  outframe=outframe,
                  veltype=veltype,
                  imsize=imsize,
                  cell=cell,
                  phasecenter=phasecenter,
                  restfreq=restfreq,
                  stokes=stokes,
                  weighting=weighting,
                  robust=robust,
                  uvtaper=uvtaper,
                  outertaper=outertaper,
                  innertaper=innertaper,
                  modelimage=modelimage,
                  restoringbeam=restoringbeam,
                  pbcor=pbcor,
                  minpb=minpb,
                  usescratch=usescratch,
                  noise=noise,
                  npixels=npixels,
                  npercycle=npercycle,
                  cyclefactor=cyclefactor,
                  cyclespeedup=cyclespeedup,
                  nterms=nterms,
                  reffreq=reffreq,
                  chaniter=chaniter,
                  flatnoise=flatnoise,
                  allowchunk=False)
            clnjunks = ['.flux', '.mask', '.model', '.psf', '.residual']
            for clnjunk in clnjunks:
                if os.path.exists(imname + clnjunk):
                    shutil.rmtree(imname + clnjunk)
        else:
            print imname + ' existed. Clean task aborted.'

        if doreg and not os.path.isfile(imname + '.fits'):
            # check if ephemfile and msinfofile exist
            if not ephem:
                print("ephemeris info does not exist!")
                return
            reftime = [timerange]
            helio = vla_prep.ephem_to_helio(msinfo=msinfofile,
                                            ephem=ephem,
                                            reftime=reftime)
            imagefile = [imname + '.image']
            fitsfile = [imname + '.fits']
            vla_prep.imreg(imagefile=imagefile,
                           fitsfile=fitsfile,
                           helio=helio,
                           toTb=False,
                           scl100=True)
            if os.path.exists(imname + '.fits'):
                return [True, btstr, imname + '.fits']
            else:
                return [False, btstr, '']
        else:
            if os.path.exists(imname + '.image'):
                return [True, btstr, imname + '.image']
            else:
                return [False, btstr, '']

    except:
        print('error in processing image: ' + btstr)
        return [False, btstr, '']
예제 #7
0
def subvs(vis='',
          outputvis='',
          timerange='',
          spw='',
          timoffset=4,
          windowlen=5,
          windowtype='hamming',
          splitsel=True,
          reverse=False,
          overwrite=False):
    """Perform vector subtraction for visibilities
    Keyword arguments:
	vis -- Name of input visibility file (MS)
				default: none; example: vis='ngc5921.ms'
	outputvis -- Name of output uv-subtracted visibility file (MS)
				default: none; example: outputvis='ngc5921_src.ms'
	timerange -- Time range of performing the UV subtraction:
				default='' means all times.  examples:
				timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
				timerange = 'hh:mm:ss~hh:mm:ss'
	spw -- Select spectral window/channel.
	windowlen -- Specify the width of window for smoothing
	windowtype --The type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
				flat window will produce a moving average smoothing.
	splitsel -- True or False. default = False. If splitsel = False, then the entire input
				measurement set is copied as the output measurement set (outputvis), with 
				background subtracted at selected timerange and spectral channels. 
				If splitsel = True,then only the selected timerange and spectral channels 
				are copied into the output measurement set (outputvis).
	reverse -- True or False. default = False. If reverse = False, then the times indicated
				by subtime1 and/or subtime2 are treated as background and subtracted; If reverse
				= True, then reverse the sign of the background-subtracted data. The option can 
				be used for mapping absorptive structure.
	overwrite -- True or False. default = False. If overwrite = True and
				outputvis already exists, the selected subtime and spw in the 
				output measurment set will be replaced with background subtracted 
				visibilities

    """
    # Get the time and frequency axis of the input ms
    # Open the ms and plot dynamic spectrum
    print 'using window length: ', windowlen
    print 'using window type: ', windowtype
    ms.open(vis, nomodify=True)
    # ms.selectinit(datadescid=0)
    timfreq = ms.getdata(['time', 'axis_info'], ifraxis=True)
    tim = timfreq['time']
    # check timerange input; default: entire timerange
    if timerange and (type(timerange) == str):
        [btimeo, etimeo] = timerange.split('~')
        btimeosec = qa.getvalue(qa.convert(qa.totime(btimeo), 's'))
        etimeosec = qa.getvalue(qa.convert(qa.totime(etimeo), 's'))
        timebinosec = etimeosec - btimeosec
        if timebinosec < 0:
            raise Exception, 'Negative timebin! Please check the "timerange" parameter.'
        else:
            casalog.post('Selected timerange: ' + timerange +
                         ' as the time for UV subtraction.')
    else:
        casalog.post(
            'Output timerange not specified, using the entire timerange')
        timerange = str(qa.time(qa.quantity(
            tim[0], 's'), prec=8)[0]) + '~' + str(
                qa.time(qa.quantity(tim[-1], 's'), prec=8)[0])
        print 'Output timerange not specified, using the entire timerange', timerange
    # check spectral window input; default: entire channels of spectral window 0
    if spw and (type(spw) == str):
        [spwid, chanran] = spw.split(':')
        [bchan, echan] = chanran.split('~')
        nchan = int(echan) - int(bchan) + 1
    else:
        casalog.post('spw not specified, use all frequency channels')
        freq = timfreq['axis_info']['freq_axis']['chan_freq'].flatten()
        nchan = len(freq)
        spwid = '0'
        bchan = '0'
        echan = str(nchan - 1)
        print 'spw not specified, use all frequency channels', spwid + ':' + bchan + '~' + str(
            nchan - 1)

    ntimergn = len(timerange)
    # To avoid memory error, split the channel into smaller segements for smoothing
    cellstep = 2
    chancell = int(nchan / cellstep)
    l = range(nchan)
    chunks = [l[x:x + cellstep] for x in xrange(0, len(l), cellstep)]
    #spwrange='0:0~'+str(chancell)
    ms.close()

    if not (timoffset and (type(timoffset) == int)):
        timoffset = int(4)

    for i in range(len(chunks)):
        spwrange = spwid + ':' + str(int(bchan) + min(chunks[i])) + '~' + str(
            int(bchan) + max(chunks[i]))
        print 'Subtracting visibility from spectral range: ', spwrange
        result2 = task_subvs_lv1.subvs(vis, outputvis, timerange, spwrange,
                                       timoffset, windowlen, windowtype,
                                       splitsel, False, True)
예제 #8
0
def clean_iter(
        tim, vis, imageprefix, imagesuffix, twidth, doreg, usephacenter,
        reftime, ephem, msinfo, toTb, overwrite, selectdata, field, spw,
        uvrange, antenna, scan, observation, intent, datacolumn, imsize, cell,
        phasecenter, stokes, projection, startmodel, specmode, reffreq, nchan,
        start, width, outframe, veltype, restfreq, interpolation, gridder,
        facets, chanchunks, wprojplanes, vptable, usepointing, mosweight,
        aterm, psterm, wbawp, conjbeams, cfcache, computepastep, rotatepastep,
        pblimit, normtype, deconvolver, scales, nterms, smallscalebias,
        restoration, restoringbeam, pbcor, outlierfile, weighting, robust,
        npixels, uvtaper, niter, gain, threshold, nsigma, cycleniter,
        cyclefactor, minpsffraction, maxpsffraction, interactive, usemask,
        mask, pbmask, sidelobethreshold, noisethreshold, lownoisethreshold,
        negativethreshold, smoothfactor, minbeamfrac, cutthreshold,
        growiterations, dogrowprune, minpercentchange, verbose, restart,
        savemodel, calcres, calcpsf, parallel, subregion, tmpdir, btidx):
    from tclean_cli import tclean_cli as tclean
    from split_cli import split_cli as split
    bt = btidx  # 0
    if bt + twidth < len(tim) - 1:
        et = btidx + twidth - 1
    else:
        et = len(tim) - 1

    if bt == 0:
        bt_d = tim[bt] - ((tim[bt + 1] - tim[bt]) / 2)
    else:
        bt_d = tim[bt] - ((tim[bt] - tim[bt - 1]) / 2)
    if et == (len(tim) - 1) or et == -1:
        et_d = tim[et] + ((tim[et] - tim[et - 1]) / 2)
    else:
        et_d = tim[et] + ((tim[et + 1] - tim[et]) / 2)

    timerange = qa.time(qa.quantity(bt_d, 's'), prec=9, form='ymd')[0] + '~' + \
                qa.time(qa.quantity(et_d, 's'), prec=9, form='ymd')[0]
    btstr = qa.time(qa.quantity(bt_d, 's'), prec=9, form='fits')[0]
    etstr = qa.time(qa.quantity(et_d, 's'), prec=9, form='fits')[0]
    print('cleaning timerange: ' + timerange)

    image0 = btstr.replace(':', '').replace('-', '')
    imname = imageprefix + image0 + imagesuffix

    # ms_tmp = tmpdir + image0 + '.ms'
    # print('checkpoint 1')
    # # split(vis=vis, outputvis=ms_tmp, field=field, scan=scan, antenna=antenna, timerange=timerange,
    # #       datacolumn=datacolumn)
    # ms.open(vis)
    # print('checkpoint 1-1')
    # ms.split(ms_tmp,field=field, scan=scan, baseline=antenna, time=timerange,whichcol=datacolumn)
    # print('checkpoint 1-2')
    # ms.close()
    # print('checkpoint 2')

    if overwrite or (len(glob.glob(imname + '*')) == 0):
        os.system('rm -rf {}*'.format(imname))
        try:
            tclean(vis=vis,
                   selectdata=selectdata,
                   field=field,
                   spw=spw,
                   timerange=timerange,
                   uvrange=uvrange,
                   antenna=antenna,
                   scan=scan,
                   observation=observation,
                   intent=intent,
                   datacolumn=datacolumn,
                   imagename=imname,
                   imsize=imsize,
                   cell=cell,
                   phasecenter=phasecenter,
                   stokes=stokes,
                   projection=projection,
                   startmodel=startmodel,
                   specmode=specmode,
                   reffreq=reffreq,
                   nchan=nchan,
                   start=start,
                   width=width,
                   outframe=outframe,
                   veltype=veltype,
                   restfreq=restfreq,
                   interpolation=interpolation,
                   gridder=gridder,
                   facets=facets,
                   chanchunks=chanchunks,
                   wprojplanes=wprojplanes,
                   vptable=vptable,
                   usepointing=usepointing,
                   mosweight=mosweight,
                   aterm=aterm,
                   psterm=psterm,
                   wbawp=wbawp,
                   conjbeams=conjbeams,
                   cfcache=cfcache,
                   computepastep=computepastep,
                   rotatepastep=rotatepastep,
                   pblimit=pblimit,
                   normtype=normtype,
                   deconvolver=deconvolver,
                   scales=scales,
                   nterms=nterms,
                   smallscalebias=smallscalebias,
                   restoration=restoration,
                   restoringbeam=restoringbeam,
                   pbcor=pbcor,
                   outlierfile=outlierfile,
                   weighting=weighting,
                   robust=robust,
                   npixels=npixels,
                   uvtaper=uvtaper,
                   niter=niter,
                   gain=gain,
                   threshold=threshold,
                   nsigma=nsigma,
                   cycleniter=cycleniter,
                   cyclefactor=cyclefactor,
                   minpsffraction=minpsffraction,
                   maxpsffraction=maxpsffraction,
                   interactive=interactive,
                   usemask=usemask,
                   mask=mask,
                   pbmask=pbmask,
                   sidelobethreshold=sidelobethreshold,
                   noisethreshold=noisethreshold,
                   lownoisethreshold=lownoisethreshold,
                   negativethreshold=negativethreshold,
                   smoothfactor=smoothfactor,
                   minbeamfrac=minbeamfrac,
                   cutthreshold=cutthreshold,
                   growiterations=growiterations,
                   dogrowprune=dogrowprune,
                   minpercentchange=minpercentchange,
                   verbose=verbose,
                   restart=restart,
                   savemodel=savemodel,
                   calcres=calcres,
                   calcpsf=calcpsf,
                   parallel=parallel)
            # print('checkpoint 3')
            if pbcor:
                clnjunks = [
                    '.flux', '.mask', '.model', '.psf', '.residual', '.pb',
                    '.sumwt', '.image'
                ]
            else:
                clnjunks = [
                    '.flux', '.mask', '.model', '.psf', '.residual', '.pb',
                    '.sumwt', '.image.pbcor'
                ]
            for clnjunk in clnjunks:
                if os.path.exists(imname + clnjunk):
                    shutil.rmtree(imname + clnjunk)
            if pbcor:
                os.system('mv {} {}'.format(imname + '.image.pbcor',
                                            imname + '.image'))
        except:
            print('error in cleaning image: ' + btstr)
            return [False, btstr, etstr, '']
    else:
        print(imname + ' exists. Clean task aborted.')

    if doreg and not os.path.isfile(imname + '.fits'):
        # ephem.keys()
        # msinfo.keys()
        try:
            # check if ephemfile and msinfofile exist
            if not ephem:
                print(
                    "ephemeris info does not exist, querying from JPL Horizons on the fly"
                )
                ephem = hf.read_horizons(vis=vis)
            if not msinfo:
                print("ms info not provided, generating one on the fly")
                msinfo = hf.read_msinfo(vis)
            hf.imreg(vis=vis,
                     ephem=ephem,
                     msinfo=msinfo,
                     timerange=timerange,
                     reftime=reftime,
                     imagefile=imname + '.image',
                     fitsfile=imname + '.fits',
                     toTb=toTb,
                     scl100=False,
                     usephacenter=usephacenter,
                     subregion=subregion)
            if os.path.exists(imname + '.fits'):
                shutil.rmtree(imname + '.image')
                return [True, btstr, etstr, imname + '.fits']
            else:
                return [False, btstr, etstr, '']
        except:
            print('error in registering image: ' + btstr)
            return [False, btstr, etstr, imname + '.image']
    else:
        if os.path.exists(imname + '.image'):
            return [True, btstr, etstr, imname + '.image']
        else:
            return [False, btstr, etstr, '']
예제 #9
0
    def test_readAndParseTbuff(self):
        '''flaghelper: compare the read and parse and apply tbuff'''
        print ''

        # MJD in seconds of timeranges are these
        # <startTime>4891227930515540000 <endTime>4891227932453838000
        # <startTime>4891228473545856000 <endTime>4891228473731891000
        # <startTime>4891226924455911000 <endTime>4891226927502314000
        # <startTime>4891228838164987000 <endTime>4891228838418996000
        # <startTime>4891228609440808000 <endTime>4891228612489617000

        online = [
            "antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'",
            "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'",
            "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'",
            "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'",
            "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"
        ]

        myinput = "antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'\n"\
                  "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'\n"\
                  "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'\n"\
                  "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'\n"\
                  "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"

        filename1 = 'flaghelperonline2.txt'
        create_input(myinput, filename1)

        # First timerange from online before padding
        origt = timerange = '2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'

        # Apply tbuff to timeranges
        timebuffer = 1.1
        dlist1 = fh.readAndParse([filename1], tbuff=timebuffer)
        self.assertEqual(len(dlist1), 5)

        # Get the first padded timerange from output
        padt = dlist1[0]['timerange']

        # Revert the tbuff application manually
        t0, t1 = padt.split('~', 1)
        startTime = qa.totime(t0)['value']
        startTimeSec = float((startTime * 24 * 3600) + timebuffer)
        startTimeSec = qa.quantity(startTimeSec, 's')
        paddedT0 = qa.time(startTimeSec, form='ymd', prec=9)[0]
        # end time
        endTime = qa.totime(t1)['value']
        endTimeSec = float((endTime * 24 * 3600) - timebuffer)
        endTimeSec = qa.quantity(endTimeSec, 's')
        paddedT1 = qa.time(endTimeSec, form='ymd', prec=9)[0]

        newtimerange = paddedT0 + '~' + paddedT1

        # Compare with the original
        self.assertEqual(origt, newtimerange)

        # Compare with original values from Flag.xml
        xmlt0 = float(4891227930515540000) * 1.0E-9
        xmlt1 = float(4891227932453838000) * 1.0E-9

        self.assertAlmostEqual(xmlt0, startTimeSec['value'], places=3)
        self.assertAlmostEqual(xmlt1, endTimeSec['value'], places=3)
예제 #10
0
def plt_dspec(specdata,
              pol='I',
              dmin=None,
              dmax=None,
              timerange=None,
              freqrange=None,
              timestr=True,
              movie=False,
              framedur=60.,
              dtframe=10.,
              goessav=None,
              goes_trange=None,
              savepng=True,
              savepdf=False):
    """
    timerange: format: ['2012/03/10/18:00:00','2012/03/10/19:00:00']
    freqrange: format: [1000.,1500.] in MHz
    movie: do a movie of dynamic spectrum?
    framedur: time range of each frame
    dtframe: time difference of consecutive frames
    goessav: provide an IDL save file from the sswidl GOES widget output
    goes_trange: plot only the specified time range for goes
    timestr: display time as strings on X-axis -- currently the times do not update themselves when zooming in
    """
    # Set up variables
    import matplotlib.pyplot as plt
    import numpy
    from numpy import log10
    from astropy.time import Time
    if pol != 'RR' and pol != 'LL' and pol != 'RRLL' and pol != 'I' and pol != 'V' and pol != 'IV':
        print "Please enter 'RR', 'LL', 'RRLL', 'I', 'V', 'IV' for pol"
        return 0

    if type(specdata) is str:
        specdata = np.load(specdata)
        bl = specdata['bl'].item()
    try:
        (npol, nbl, nfreq, ntim) = specdata['spec'].shape
        spec = specdata['spec']
        tim = specdata['tim']
        tim_ = Time(tim / 3600. / 24., format='mjd')
        tim_plt = tim_.plot_date
        freq = specdata['freq']
        if not 'bl' in vars():
            bl = specdata['bl']
    except:
        print('format of specdata not recognized. Check your input')
        return -1

    if timerange:
        if type(timerange[0]) is str:
            timerange = [
                qa.convert(qa.quantity(t), 's')['value'] for t in timerange
            ]
        tidx = np.where((tim >= timerange[0]) & (tim <= timerange[1]))[0]
    else:
        tidx = range(ntim)
    if freqrange:
        fidx = np.where((freq >= freqrange[0] * 1e6)
                        & (freq <= freqrange[1] * 1e6))[0]
    else:
        fidx = range(nfreq)

    # setup plot parameters
    print 'ploting dynamic spectrum...'
    spec_med = np.median(np.absolute(spec))
    # if not dmin:
    #    dmin = spec_med / 20.
    # if not dmax:
    #    dmax = spec_med * 5.
    # do the plot
    for b in range(nbl):
        if pol != 'RRLL' and pol != 'IV':
            if pol == 'RR':
                spec_plt = spec[0, b, :, :]
            elif pol == 'LL':
                spec_plt = spec[1, b, :, :]
            elif pol == 'I':
                spec_plt = (spec[0, b, :, :] + spec[1, b, :, :]) / 2.
            elif pol == 'V':
                spec_plt = (spec[0, b, :, :] - spec[1, b, :, :]) / 2.
            if movie:
                f = plt.figure(figsize=(16, 8), dpi=100)
                if goessav:
                    gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1])
                    gs.update(left=0.06, right=0.97, top=0.95, bottom=0.06)
                    ax1 = f.add_subplot(gs[0])
                    ax2 = f.add_subplot(gs[1])
                    if os.path.exists(goessav):
                        goes = readsav(goessav)
                        # IDL anytim 0 sec correspond to 1979 Jan 01, convert to mjd time
                        anytimbase = qa.convert(
                            qa.quantity('1979/01/01/00:00:00'), 's')['value']
                        mjdbase = goes['utbase'] + anytimbase
                        ts = goes['tarray'] + mjdbase
                        lc0 = goes['yclean'][0, :]
                        lc1 = goes['yclean'][1, :]
                else:
                    ax1 = f.add_subplot(211)
                tstart = tim[tidx[0]]
                tend = tim[tidx[-1]]
                tstartstr = qa.time(qa.quantity(tstart, 's'))[0]
                tendstr = qa.time(qa.quantity(tend, 's'))[0]
                nfrm = int((tend - tstart) / dtframe) + 1
                print 'Movie mode set. ' + str(
                    nfrm
                ) + ' frames to plot from ' + tstartstr + ' to ' + tendstr
                for i in range(nfrm):
                    if (i != 0) and (i % 10 == 0):
                        print str(i) + ' frames done'
                    timeran = [
                        tstart + i * dtframe, tstart + i * dtframe + framedur
                    ]
                    tidx1 = np.where((tim >= timeran[0])
                                     & (tim <= timeran[1]))[0]
                    tim1 = tim_[tidx1]
                    freq1 = freq[fidx] / 1e9
                    spec_plt1 = spec_plt[fidx, :][:, tidx1]
                    ax1.pcolormesh(tim1.plot_date,
                                   freq1,
                                   spec_plt1,
                                   cmap='jet',
                                   vmin=dmin,
                                   vmax=dmax)
                    ax1.set_xlim(tim1[0].plot_date, tim1[-1].plot_date)
                    ax1.set_ylim(freq1[0], freq1[-1])
                    ax1.set_ylabel('Frequency (GHz)')
                    ax1.set_title('Dynamic spectrum @ bl ' + bl.split(';')[b] +
                                  ', pol ' + pol)
                    if timestr:
                        # date_format = mdates.DateFormatter('%H:%M:%S.%f')
                        # ax1.xaxis_date()
                        # ax1.xaxis.set_major_formatter(date_format)
                        locator = AutoDateLocator()
                        ax1.xaxis.set_major_locator(locator)
                        ax1.xaxis.set_major_formatter(
                            AutoDateFormatter(locator))
                    ax1.set_autoscale_on(False)
                    if goessav:
                        if goes_trange:
                            if type(goes_trange[0]) is str:
                                goes_trange = [
                                    qa.convert(qa.quantity(t), 's')['value']
                                    for t in goes_trange
                                ]
                                idx = np.where((ts >= goes_trange[0])
                                               & (ts <= goes_trange[1]))[0]
                        else:
                            idx = range(len(ts))
                        ts_plt = ts[idx]
                        lc0_plt = lc0[idx]
                        utbase = qa.convert(qa.quantity('0001/01/01/00:00:00'),
                                            'd')['value'] + 1
                        ts_plt_d = ts_plt / 3600. / 24. - utbase
                        ax2.plot_date(ts_plt_d, lc0_plt, 'b-')
                        ax2.axvspan(tim1[0].mjd - utbase,
                                    tim1[-1].mjd - utbase,
                                    color='red',
                                    alpha=0.5)
                        ax2.set_yscale('log')
                        ax2.set_title('GOES 1-8 A')

                    tstartstr_ = tim1[0].datetime.strftime(
                        '%Y-%m-%dT%H%M%S.%f')[:-3]
                    tendstr_ = tim1[1].datetime.strftime('%H%M%S.%f')[:-3]
                    timstr = tstartstr_ + '-' + tendstr_
                    figfile = 'dspec_t' + timstr + '.png'
                    if not os.path.isdir('dspec'):
                        os.makedirs('dspec')
                    f.savefig('dspec/' + figfile)
                    plt.cla()
            else:
                f = plt.figure(figsize=(8, 4), dpi=100)
                ax = f.add_subplot(111)
                freqghz = freq / 1e9
                ax.pcolormesh(tim_plt,
                              freqghz,
                              spec_plt,
                              cmap='jet',
                              vmin=dmin,
                              vmax=dmax)
                ax.set_xlim(tim_plt[tidx[0]], tim_plt[tidx[-1]])
                ax.set_ylim(freqghz[fidx[0]], freqghz[fidx[-1]])
                try:
                    from sunpy import lightcurve
                    from sunpy.time import TimeRange, parse_time
                    t1 = tim_[tidx[0]]
                    t2 = tim_[tidx[-1]]
                    tr = TimeRange(t1.iso, t2.iso)
                    goes = lightcurve.GOESLightCurve.create(tr)
                    goes.data['xrsb'] = 2 * (np.log10(goes.data['xrsb'])) + 26
                    xx = [str(ll) for ll in np.array(goes.data.index)]
                    yy = np.array(goes.data['xrsb'])
                    ax.plot(Time(xx).mjd * 24 * 3600, yy, c='yellow')
                    rightaxis_label_time = Time(xx[-1]).mjd * 24 * 3600
                    ax.text(rightaxis_label_time, 9.6, 'A', fontsize='15')
                    ax.text(rightaxis_label_time, 11.6, 'B', fontsize='15')
                    ax.text(rightaxis_label_time, 13.6, 'C', fontsize='15')
                    ax.text(rightaxis_label_time, 15.6, 'M', fontsize='15')
                    ax.text(rightaxis_label_time, 17.6, 'X', fontsize='15')
                except:
                    pass

                def format_coord(x, y):
                    col = np.argmin(np.absolute(tim_plt - x))
                    row = np.argmin(np.absolute(freqghz - y))
                    if col >= 0 and col < ntim and row >= 0 and row < nfreq:
                        timstr = tim_[col].isot
                        flux = spec_plt[row, col]
                        return 'time {0} = {1}, freq = {2:.3f} GHz, flux = {3:.2f} Jy'.format(
                            col, timstr, y, flux)
                    else:
                        return 'x = {0}, y = {1:.3f}'.format(x, y)

                ax.format_coord = format_coord
                ax.set_ylabel('Frequency (GHz)')
                if bl:
                    ax.set_title('Dynamic spectrum @ bl ' + bl.split(';')[b] +
                                 ', pol ' + pol)
                else:
                    ax.set_title('Medium dynamic spectrum')
                if timestr:
                    # date_format = mdates.DateFormatter('%H:%M:%S.%f')
                    # ax.xaxis_date()
                    # ax.xaxis.set_major_formatter(date_format)
                    locator = AutoDateLocator()
                    ax.xaxis.set_major_locator(locator)
                    ax.xaxis.set_major_formatter(AutoDateFormatter(locator))
                ax.set_autoscale_on(False)

        else:
            f = plt.figure(figsize=(8, 6), dpi=100)
            R_plot = np.absolute(spec[0, b, :, :])
            L_plot = np.absolute(spec[1, b, :, :])
            I_plot = (R_plot + L_plot) / 2.
            V_plot = (R_plot - L_plot) / 2.
            if pol == 'RRLL':
                spec_plt_1 = R_plot
                spec_plt_2 = L_plot
                polstr = ['RR', 'LL']
            if pol == 'IV':
                spec_plt_1 = I_plot
                spec_plt_2 = V_plot
                polstr = ['I', 'V']

            ax1 = f.add_subplot(211)
            freqghz = freq / 1e9
            ax1.pcolormesh(tim_plt,
                           freqghz,
                           spec_plt_1,
                           cmap='jet',
                           vmin=dmin,
                           vmax=dmax)
            ax1.set_xlim(tim_plt[tidx[0]], tim_plt[tidx[-1]])
            ax1.set_ylim(freqghz[fidx[0]], freqghz[fidx[-1]])

            def format_coord(x, y):
                col = np.argmin(np.absolute(tim_plt - x))
                row = np.argmin(np.absolute(freqghz - y))
                if col >= 0 and col < ntim and row >= 0 and row < nfreq:
                    timstr = tim_[col].isot
                    flux = spec_plt[row, col]
                    return 'time {0} = {1}, freq = {2:.3f} GHz, flux = {3:.2f} Jy'.format(
                        col, timstr, y, flux)
                else:
                    return 'x = {0}, y = {1:.3f}'.format(x, y)

            ax1.format_coord = format_coord
            ax1.set_ylabel('Frequency (GHz)')
            if timestr:
                # date_format = mdates.DateFormatter('%H:%M:%S.%f')
                # ax1.xaxis_date()
                # ax1.xaxis.set_major_formatter(date_format)
                locator = AutoDateLocator()
                ax1.xaxis.set_major_locator(locator)
                ax1.xaxis.set_major_formatter(AutoDateFormatter(locator))
            ax1.set_title('Dynamic spectrum @ bl ' + bl.split(';')[b] +
                          ', pol ' + polstr[0])
            ax1.set_autoscale_on(False)
            ax2 = f.add_subplot(212)
            ax2.pcolormesh(tim_plt,
                           freqghz,
                           spec_plt_2,
                           cmap='jet',
                           vmin=dmin,
                           vmax=dmax)
            ax2.set_xlim(tim_plt[tidx[0]], tim_plt[tidx[-1]])
            ax2.set_ylim(freqghz[fidx[0]], freqghz[fidx[-1]])
            if timestr:
                # date_format = mdates.DateFormatter('%H:%M:%S.%f')
                # ax2.xaxis_date()
                # ax2.xaxis.set_major_formatter(date_format)
                locator = AutoDateLocator()
                ax2.xaxis.set_major_locator(locator)
                ax2.xaxis.set_major_formatter(AutoDateFormatter(locator))

            def format_coord(x, y):
                col = np.argmin(np.absolute(tim_plt - x))
                row = np.argmin(np.absolute(freqghz - y))
                if col >= 0 and col < ntim and row >= 0 and row < nfreq:
                    timstr = tim_[col].isot
                    flux = spec_plt[row, col]
                    return 'time {0} = {1}, freq = {2:.3f} GHz, flux = {3:.2f} Jy'.format(
                        col, timstr, y, flux)
                else:
                    return 'x = {0}, y = {1:.3f}'.format(x, y)

            ax2.format_coord = format_coord
            ax2.set_ylabel('Frequency (GHz)')
            ax2.set_title('Dynamic spectrum @ bl ' + bl.split(';')[b] +
                          ', pol ' + polstr[1])
            ax2.set_autoscale_on(False)
예제 #11
0
def subvs(vis=None,
          outputvis=None,
          timerange=None,
          spw=None,
          subtime1=None,
          subtime2=None,
          splitsel=True,
          reverse=False,
          overwrite=False):
    """Perform vector subtraction for visibilities
    Keyword arguments:
    vis -- Name of input visibility file (MS)
            default: none; example: vis='ngc5921.ms'
    outputvis -- Name of output uv-subtracted visibility file (MS)
                  default: none; example: outputvis='ngc5921_src.ms'
    timerange -- Time range of performing the UV subtraction:
                 default='' means all times.  examples:
                 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
                 timerange = 'hh:mm:ss~hh:mm:ss'
    spw -- Select spectral window/channel.
           default = '' all the spectral channels. Example: spw='0:1~20'
    subtime1 -- Time range 1 of the background to be subtracted from the data 
                 default='' means all times.  format:
                 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
                 timerange = 'hh:mm:ss~hh:mm:ss'
    subtime2 -- Time range 2 of the backgroud to be subtracted from the data
                 default='' means all times.  examples:
                 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
                 timerange = 'hh:mm:ss~hh:mm:ss'
    splitsel -- True or False. default = False. If splitsel = False, then the entire input
            measurement set is copied as the output measurement set (outputvis), with 
            background subtracted at selected timerange and spectral channels. 
            If splitsel = True,then only the selected timerange and spectral channels 
            are copied into the output measurement set (outputvis).
    reverse -- True or False. default = False. If reverse = False, then the times indicated
            by subtime1 and/or subtime2 are treated as background and subtracted; If reverse
            = True, then reverse the sign of the background-subtracted data. The option can 
            be used for mapping absorptive structure.
    overwrite -- True or False. default = False. If overwrite = True and
                outputvis already exists, the selected subtime and spw in the 
                output measurment set will be replaced with background subtracted 
                visibilities

    """
    #check the visbility ms
    if not outputvis or outputvis.isspace():
        raise ValueError, 'Please specify outputvis'

    if os.path.exists(outputvis):
        if overwrite:
            print "The already existing output measurement set will be updated."
        else:
            raise ValueError, "Output MS %s already exists - will not overwrite." % outputvis
    else:
        if not splitsel:
            shutil.copytree(vis, outputvis)
        else:
            ms.open(vis, nomodify=True)
            ms.split(outputvis, spw=spw, time=timerange, whichcol='DATA')
            ms.close()

    #define and check the time ranges
    if subtime1 and (type(subtime1) == str):
        [bsubtime1, esubtime1] = subtime1.split('~')
        bsubtime1sec = qa.getvalue(qa.convert(qa.totime(bsubtime1), 's'))
        esubtime1sec = qa.getvalue(qa.convert(qa.totime(esubtime1), 's'))
        timebin1sec = esubtime1sec - bsubtime1sec
        if timebin1sec < 0:
            raise Exception, 'Negative timebin! Please check the "subtime1" parameter.'
        casalog.post('Selected timerange 1: ' + subtime1 +
                     ' as background for uv subtraction.')
    else:
        raise Exception, 'Please enter at least one timerange as the background'

    if subtime2 and (type(subtime2) == str):
        [bsubtime2, esubtime2] = subtime2.split('~')
        bsubtime2sec = qa.getvalue(qa.convert(qa.totime(bsubtime2), 's'))
        esubtime2sec = qa.getvalue(qa.convert(qa.totime(esubtime2), 's'))
        timebin2sec = esubtime2sec - bsubtime2sec
        if timebin2sec < 0:
            raise Exception, 'Negative timebin! Please check the "subtime2" parameter.'
        timebin2 = str(timebin2sec) + 's'
        casalog.post('Selected timerange 2: ' + subtime2 +
                     ' as background for uv subtraction.')
        #plus 1s is to ensure averaging over the entire timerange
    else:
        casalog.post(
            'Timerange 2 not selected, using only timerange 1 as background')

    if timerange and (type(timerange) == str):
        [btimeo, etimeo] = timerange.split('~')
        btimeosec = qa.getvalue(qa.convert(qa.totime(btimeo), 's'))
        etimeosec = qa.getvalue(qa.convert(qa.totime(etimeo), 's'))
        timebinosec = etimeosec - btimeosec
        if timebinosec < 0:
            raise Exception, 'Negative timebin! Please check the "timerange" parameter.'
        casalog.post('Selected timerange: ' + timerange +
                     ' as the time for UV subtraction.')
    else:
        casalog.post(
            'Output timerange not specified, using the entire timerange')

    if spw and (type(spw) == str):
        [spwid, chanran] = spw.split(':')
        [bchan, echan] = chanran.split('~')
    else:
        casalog.post('spw not specified, use all frequency channels')

    #Select the background indicated by subtime1
    ms.open(vis, nomodify=True)
    #Select the spw id
    ms.msselect({'time': subtime1})
    if spw and (type(spw) == str):
        ms.selectinit(datadescid=int(spwid))
        nchan = int(echan) - int(bchan) + 1
        ms.selectchannel(nchan, int(bchan), 1, 1)
    rec1 = ms.getdata(['data', 'time', 'axis_info'], ifraxis=True)
    #print 'shape of the frequency matrix ',rec1['axis_info']['freq_axis']['chan_freq'].shape
    sz1 = rec1['data'].shape
    print 'dimension of selected background 1', rec1['data'].shape
    #the data shape is (n_pol,n_channel,n_baseline,n_time), no need to reshape
    #rec1['data']=rec1['data'].reshape(sz1[0],sz1[1],sz1[2],nspw,sz1[3]/nspw,order='F')
    #print 'reshaped rec1 ', rec1['data'].shape
    rec1avg = np.average(rec1['data'], axis=3)
    casalog.post('Averaging the visibilities in subtime1: ' + subtime1)
    ms.close()
    if subtime2 and (type(subtime2) == str):
        ms.open(vis, nomodify=True)
        #Select the spw id
        ms.msselect({'time': subtime2})
        if spw and (type(spw) == str):
            ms.selectinit(datadescid=0)
            nchan = int(echan) - int(bchan) + 1
            ms.selectchannel(nchan, int(bchan), 1, 1)
        rec2 = ms.getdata(['data', 'time', 'axis_info'], ifraxis=True)
        sz2 = rec2['data'].shape
        print 'dimension of selected background 2', rec2['data'].shape
        #rec2['data']=rec2['data'].reshape(sz2[0],sz2[1],sz2[2],nspw,sz2[3]/nspw,order='F')
        #print 'reshaped rec1 ', rec2['data'].shape
        rec2avg = np.average(rec2['data'], axis=3)
        ms.close()
        casalog.post('Averaged the visibilities in subtime2: ' + subtime2)

    #do UV subtraction, according to timerange and spw
    ms.open(outputvis, nomodify=False)
    if not splitsel:
        #outputvis is identical to input visibility, do the selection
        if timerange and (type(timerange == str)):
            ms.msselect({'time': timerange})
        if spw and (type(spw) == str):
            ms.selectinit(datadescid=int(spwid))
            nchan = int(echan) - int(bchan) + 1
            ms.selectchannel(nchan, int(bchan), 1, 1)
    else:
        #outputvis is splitted, selections have already applied, select all the data
        ms.selectinit(datadescid=0)
    orec = ms.getdata(['data', 'time', 'axis_info'], ifraxis=True)
    b_rows = orec['data'].shape[2]
    nchan = orec['data'].shape[1]
    #szo=orec['data'].shape
    print 'dimension of output data', orec['data'].shape
    #orec['data']=orec['data'].reshape(szo[0],szo[1],szo[2],nspw,szo[3]/nspw,order='F')
    #print 'reshaped rec1 ', orec['data'].shape
    t_rows = orec['data'].shape[3]
    casalog.post('Number of baselines: ' + str(b_rows))
    casalog.post('Number of spectral channels: ' + str(nchan))
    casalog.post('Number of time pixels: ' + str(t_rows))

    if subtime1 and (not subtime2):
        casalog.post(
            'Only "subtime1" is defined, subtracting background defined in subtime1: '
            + subtime1)
        t1 = (np.amax(rec1['time']) + np.amin(rec1['time'])) / 2.
        print 't1: ', qa.time(qa.quantity(t1, 's'), form='ymd', prec=10)
        for i in range(t_rows):
            orec['data'][:, :, :, i] -= rec1avg
            if reverse:
                orec['data'][:, :, :, i] = -orec['data'][:, :, :, i]
    if subtime1 and subtime2 and (type(subtime2) == str):
        casalog.post(
            'Both subtime1 and subtime2 are specified, doing linear interpolation between "subtime1" and "subtime2"'
        )
        t1 = (np.amax(rec1['time']) + np.amin(rec1['time'])) / 2.
        t2 = (np.amax(rec2['time']) + np.amin(rec2['time'])) / 2.
        touts = orec['time']
        print 't1: ', qa.time(qa.quantity(t1, 's'), form='ymd', prec=10)
        print 't2: ', qa.time(qa.quantity(t2, 's'), form='ymd', prec=10)
        for i in range(t_rows):
            tout = touts[i]
            if tout > np.amax([t1, t2]):
                tout = np.amax([t1, t2])
            elif tout < np.amin([t1, t2]):
                tout = np.amin([t1, t2])
            orec['data'][:, :, :,
                         i] -= (rec2avg - rec1avg) * (tout -
                                                      t1) / (t2 - t1) + rec1avg
            if reverse:
                orec['data'][:, :, :, i] = -orec['data'][:, :, :, i]

    #orec['data']=orec['data'].reshape(szo[0],szo[1],szo[2],szo[3],order='F')
    #put the modified data back into the output visibility set
    del orec['time']
    del orec['axis_info']
    ms.putdata(orec)
    ms.close()
예제 #12
0
def svplot(vis, timerange=None, spw='', workdir='./', specfile=None, bl=None, uvrange=None,
           stokes='RR,LL', dmin=None, dmax=None,
           goestime=None, reftime=None, 
           xycen=None, fov=[500.,500.], xyrange=None, restoringbeam=[''], robust=0.0,
           niter=500, imsize=[512], cell=['5.0arcsec'],interactive=False, 
           usemsphacenter=True, imagefile=None, fitsfile=None, plotaia=True,
           aiawave=171, aiafits=None, savefig=False, mkmovie=False, overwrite=True, ncpu=10, twidth=1, verbose=True):
    '''
    Required inputs:
            vis: calibrated CASA measurement set
    Important optional inputs:
            timerange: timerange for clean. Standard CASA time selection format. 
                       If not provided, use the entire range (*BE CAREFUL, COULD BE VERY SLOW*)
            spw: spectral window selection following the CASA syntax. 
                 Examples: spw='1:2~60' (spw id 1, channel range 2-60); spw='*:1.2~1.3GHz' (selects all channels within 1.2-1.3 GHz; note the *) 
            specfile: supply dynamic spectrum save file (from suncasa.utils.dspec2.get_dspec()). Otherwise
                      generate a median dynamic spectrum on the fly
    Optional inputs:
            bl: baseline to generate dynamic spectrum
            uvrange: uvrange to select baselines for generating dynamic spectrum 
            stokes: polarization of the clean image, can be 'RR,LL' or 'I,V'
            dmin,dmax: color bar parameter
            goestime: goes plot time, example ['2016/02/18 18:00:00','2016/02/18 23:00:00']
            rhessisav: rhessi savefile
            reftime: reftime for the image
            xycen: center of the image in helioprojective coordinates (HPLN/HPLT), in arcseconds. Example: [900, -150.]
            fov: field of view in arcsecs. Example: [500., 500.]
            xyrange: field of view in solar XY coordinates. Format: [[x1,x2],[y1,y2]]. Example: [[900., 1200.],[0,300]] 
                     ***NOTE: THIS PARAMETER OVERWRITES XYCEN AND FOV***
            aiawave: wave length of aia file in a
            imagefile: if imagefile provided, use it. Otherwise do clean and generate a new one.
            fitsfile: if fitsfile provided, use it. Otherwise generate a new one
            savefig: whether to save the figure
    Example:

    '''

    if xycen:
        xc, yc = xycen
        xlen, ylen = fov
        if parse_version(sunpy.__version__)>parse_version('0.8.0'):
            xyrange = [[xc - xlen / 2.0, yc - ylen / 2.0], [xc + xlen / 2.0, yc + ylen / 2.0]]
        else:
            xyrange = [[xc - xlen / 2.0, xc + xlen / 2.0], [yc - ylen / 2.0, yc + ylen / 2.0]]
    stokes_allowed = ['RR,LL', 'I,V', 'RRLL', 'IV']
    if not stokes in stokes_allowed:
        print 'wrong stokes parameter ' + str(stokes) + '. Allowed values are ' + ', '.join(stokes_allowed)
        return -1
    if stokes == 'RRLL':
        stokes = 'RR,LL'
    if stokes == 'IV':
        stokes = 'I,V'

    if vis[-1] == '/':
        vis = vis[:-1]
    if not os.path.exists(vis):
        print 'input measurement not exist'
        return -1
    if aiafits is None:
        aiafits = ''
    # split the data
    # generating dynamic spectrum
    if not os.path.exists(workdir):
        os.makedirs(workdir)
    if specfile:
        try:
            specdata = np.load(specfile)
        except:
            print('Provided dynamic spectrum file not numpy npz. Generating one from the visibility data')
            specfile = os.path.join(workdir, os.path.basename(vis) + '.dspec.npz')
            dspec_external(vis, workdir=workdir, specfile=specfile)
            specdata = np.load(specfile)  # specdata = ds.get_dspec(vis, domedian=True, verbose=True)
    else:
        print('Dynamic spectrum file not provided; Generating one from the visibility data')
        # specdata = ds.get_dspec(vis, domedian=True, verbose=True)
        specfile = os.path.join(workdir, os.path.basename(vis) + '.dspec.npz')
        dspec_external(vis, workdir=workdir, specfile=specfile)
        specdata = np.load(specfile)

    tb.open(vis)
    starttim = Time(tb.getcell('TIME', 0) / 24. / 3600., format='mjd')
    endtim = Time(tb.getcell('TIME', tb.nrows() - 1) / 24. / 3600., format='mjd')
    tb.close()
    datstr = starttim.iso[:10]

    if timerange is None or timerange == '':
        starttim1 = starttim
        endtim1 = endtim
        timerange = '{0}~{1}'.format(starttim.iso.replace('-', '/').replace(' ', '/'), endtim.iso.replace('-', '/').replace(' ', '/'))
    else:
        try:
            (tstart, tend) = timerange.split('~')
            if tstart[2] == ':':
                starttim1 = Time(datstr + 'T' + tstart)
                endtim1 = Time(datstr + 'T' + tend)
                timerange = '{0}/{1}~{0}/{2}'.format(datstr.replace('-', '/'), tstart, tend)
            else:
                starttim1 = Time(qa.quantity(tstart, 'd')['value'], format='mjd')
                endtim1 = Time(qa.quantity(tend, 'd')['value'], format='mjd')
        except ValueError:
            print "keyword 'timerange' in wrong format"
    midtime_mjd = (starttim1.mjd + endtim1.mjd) / 2.

    if vis.endswith('/'):
        vis = vis[:-1]
    visname = os.path.basename(vis)
    bt = starttim1.plot_date
    et = endtim1.plot_date

    # find out min and max frequency for plotting in dynamic spectrum
    ms.open(vis)
    metadata = ms.metadata()
    observatory = metadata.observatorynames()[0]
    spwInfo = ms.getspectralwindowinfo()
    nspw = len(spwInfo)
    if not spw:
        spw = '0~' + str(nspw - 1)
    staql = {'timerange': timerange, 'spw': spw}
    if ms.msselect(staql, onlyparse=True):
        ndx = ms.msselectedindices()
        chan_sel = ndx['channel']
        nspw = chan_sel.shape[0]
        bspw = chan_sel[0, 0]
        bchan = chan_sel[0, 1]
        espw = chan_sel[-1, 0]
        echan = chan_sel[-1, 2]
        bfreq = spwInfo[str(bspw)]['Chan1Freq'] + spwInfo[str(bspw)]['ChanWidth'] * bchan
        efreq = spwInfo[str(espw)]['Chan1Freq'] + spwInfo[str(espw)]['ChanWidth'] * echan
        bfreqghz = bfreq / 1e9
        efreqghz = efreq / 1e9
        if verbose:
            print 'selected timerange {}'.format(timerange)
            print 'selected frequency range {0:6.3f} to {1:6.3f} GHz'.format(bfreqghz, efreqghz)
    else:
        print "spw or timerange selection failed. Aborting..."
        ms.close()
        return -1
    ms.close()

    if observatory == 'EOVSA':
        print 'Provide stokes: ' + str(stokes) + '. However EOVSA has linear feeds. Force stokes to be IV'
        stokes = 'I,V'

    if mkmovie:
        plt.ioff()
        # fig = plt.figure(figsize=(12, 7.5), dpi=100)
        if fitsfile:
            pass
        else:
            if not imagefile:
                # from ptclean_cli import ptclean_cli as ptclean
                eph = hf.read_horizons(t0=Time(midtime_mjd, format='mjd'))
                if observatory == 'EOVSA' or (not usemsphacenter):
                    phasecenter = ''
                else:
                    phasecenter = 'J2000 ' + str(eph['ra'][0])[:15] + 'rad ' + str(eph['dec'][0])[:15] + 'rad'
                print 'use phasecenter: ' + phasecenter
                qlookfitsdir = os.path.join(workdir, 'qlookfits/')
                qlookfigdir = os.path.join(workdir, 'qlookimgs/')
                imresfile = os.path.join(qlookfitsdir, '{}.imres.npz'.format(os.path.basename(vis)))
                if overwrite:
                    imres = mk_qlook_image(vis, twidth=twidth, ncpu=ncpu, imagedir=qlookfitsdir, phasecenter=phasecenter, stokes=stokes,
                                           c_external=True)
                else:
                    if os.path.exists(imresfile):
                        imres = np.load(imresfile)
                        imres = imres['imres'].item()
                    else:
                        print('Image results file not found; Creating new images.')
                        imres = mk_qlook_image(vis, twidth=twidth, ncpu=ncpu, imagedir=qlookfitsdir, phasecenter=phasecenter, stokes=stokes,
                                               c_external=True)
                if not os.path.exists(qlookfigdir):
                    os.makedirs(qlookfigdir)
                plt_qlook_image(imres, figdir=qlookfigdir, specdata=specdata, verbose=True, stokes=stokes, fov=xyrange)

    else:
        spec = specdata['spec']
        (npol, nbl, nfreq, ntim) = spec.shape
        tidx = range(ntim)
        fidx = range(nfreq)
        tim = specdata['tim']
        freq = specdata['freq']
        freqghz = freq / 1e9
        spec_tim = Time(specdata['tim'] / 3600. / 24., format='mjd')
        timstrr = spec_tim.plot_date
        plt.ion()
        fig = plt.figure(figsize=(12, 7), dpi=100)
        gs1 = gridspec.GridSpec(3, 1)
        gs1.update(left=0.08, right=0.32, wspace=0.05)
        gs2 = gridspec.GridSpec(2, 2)
        gs2.update(left=0.38, right=0.98, hspace=0.02, wspace=0.02)

        spec_1 = np.absolute(spec[0, 0, :, :])
        spec_2 = np.absolute(spec[1, 0, :, :])
        if observatory == 'EVLA':
            # circular feeds
            polstr = ['RR', 'LL']
        if observatory == 'EOVSA' or observatory == 'ALMA':
            # linear feeds
            polstr = ['XX', 'YY']

        print 'plot the dynamic spectrum in pol ' + ' & '.join(polstr)
        ax1 = plt.subplot(gs1[0])
        ax1.pcolormesh(timstrr, freqghz, spec_1, cmap='jet', vmin=dmin, vmax=dmax)
        ax1.set_xlim(timstrr[tidx[0]], timstrr[tidx[-1]])
        ax1.xaxis_date()
        ax1.xaxis.set_major_formatter(DateFormatter("%H:%M:%S"))
        # ax1.set_xticklabels(['']*10)
        ax1.set_ylim(freqghz[fidx[0]], freqghz[fidx[-1]])
        ax1.set_ylabel('Frequency (GHz)', fontsize=10)
        ax1.set_title(observatory + ' ' + datstr + ' ' + polstr[0] + ' & ' + polstr[1], fontsize=12)
        ax1.set_autoscale_on(False)
        ax1.add_patch(patches.Rectangle((bt, bfreqghz), et - bt, efreqghz - bfreqghz, ec='w', fill=False))
        ax1.plot([(bt + et) / 2.], [(bfreqghz + efreqghz) / 2.], '*w', ms=12)
        for tick in ax1.get_xticklabels():
            tick.set_fontsize(8)
        for tick in ax1.get_yticklabels():
            tick.set_fontsize(8)
        ax2 = plt.subplot(gs1[1])
        ax2.pcolormesh(timstrr, freqghz, spec_2, cmap='jet', vmin=dmin, vmax=dmax)
        ax2.set_xlim(timstrr[tidx[0]], timstrr[tidx[-1]])
        ax2.xaxis_date()
        ax2.xaxis.set_major_formatter(DateFormatter("%H:%M:%S"))
        ax2.set_ylim(freqghz[fidx[0]], freqghz[fidx[-1]])
        ax2.set_ylabel('Frequency (GHz)', fontsize=10)
        for tick in ax2.get_xticklabels():
            tick.set_fontsize(8)
        for tick in ax2.get_yticklabels():
            tick.set_fontsize(8)
        ax2.set_autoscale_on(False)
        ax2.add_patch(patches.Rectangle((bt, bfreqghz), et - bt, efreqghz - bfreqghz, ec='w', fill=False))
        ax2.plot([(bt + et) / 2.], [(bfreqghz + efreqghz) / 2.], '*w', ms=12)

        # Second part: GOES plot
        if goestime:
            btgoes = goestime[0]
            etgoes = goestime[1]
        else:
            datstrg = datstr.replace('-', '/')
            btgoes = datstrg + ' ' + qa.time(qa.quantity(tim[0] - 1800, 's'), form='clean', prec=9)[0]
            etgoes = datstrg + ' ' + qa.time(qa.quantity(tim[tidx[-1] - 1] + 1800, 's'), form='clean', prec=9)[0]
        if verbose:
            print 'Acquire GOES soft X-ray data in from ' + btgoes + ' to ' + etgoes

        ax3 = plt.subplot(gs1[2])

        try:
            from sunpy import lightcurve as lc
            from sunpy.time import TimeRange
            goest = lc.GOESLightCurve.create(TimeRange(btgoes, etgoes))
        except:
            goesscript = os.path.join(workdir, 'goes.py')
            goesdatafile = os.path.join(workdir, 'goes.dat')
            os.system('rm -rf {}'.format(goesscript))
            fi = open(goesscript, 'wb')
            fi.write('import os \n')
            fi.write('from sunpy.time import TimeRange \n')
            fi.write('from sunpy import lightcurve as lc \n')
            fi.write('import pickle \n')
            fi.write('goesplottim = TimeRange("{0}", "{1}") \n'.format(btgoes, etgoes))
            fi.write('goes = lc.GOESLightCurve.create(goesplottim) \n')
            fi.write('fi2 = open("{}", "wb") \n'.format(goesdatafile))
            fi.write('pickle.dump(goes, fi2) \n')
            fi.write('fi2.close()')
            fi.close()

            try:
                os.system('python {}'.format(goesscript))
                os.system('rm -rf {}'.format(goesscript))
            except NameError:
                print "Bad input names"
            except ValueError:
                print "Bad input values"
            except:
                print "Unexpected error:", sys.exc_info()[0]
                print "Error in generating GOES light curves. Proceed without GOES..."

            if os.path.exists(goesdatafile):
                fi1 = file(goesdatafile, 'rb')
                goest = pickle.load(fi1)
                fi1.close()

        try:
            dates = mpl.dates.date2num(parse_time(goest.data.index))
            goesdif = np.diff(goest.data['xrsb'])
            gmax = np.nanmax(goesdif)
            gmin = np.nanmin(goesdif)
            ran = gmax - gmin
            db = 2.8 / ran
            goesdifp = goesdif * db + gmin + (-6)
            ax3.plot_date(dates, np.log10(goest.data['xrsb']), '-', label='1.0--8.0 $\AA$', color='red', lw=2)
            ax3.plot_date(dates[0:-1], goesdifp, '-', label='derivate', color='blue', lw=0.4)

            ax3.set_ylim([-7, -3])
            ax3.set_yticks([-7, -6, -5, -4, -3])
            ax3.set_yticklabels([r'$10^{-7}$', r'$10^{-6}$', r'$10^{-5}$', r'$10^{-4}$', r'$10^{-3}$'])
            ax3.set_title('Goes Soft X-ray', fontsize=12)
            ax3.set_ylabel('Watts m$^{-2}$')
            ax3.set_xlabel(datetime.datetime.isoformat(goest.data.index[0])[0:10])
            ax3.axvspan(dates[899], dates[dates.size - 899], alpha=0.2)

            ax2 = ax3.twinx()
            # ax2.set_yscale("log")
            ax2.set_ylim([-7, -3])
            ax2.set_yticks([-7, -6, -5, -4, -3])
            ax2.set_yticklabels(['B', 'C', 'M', 'X', ''])

            ax3.yaxis.grid(True, 'major')
            ax3.xaxis.grid(False, 'major')
            ax3.legend(prop={'size': 6})

            formatter = mpl.dates.DateFormatter('%H:%M')
            ax3.xaxis.set_major_formatter(formatter)

            ax3.fmt_xdata = mpl.dates.DateFormatter('%H:%M')
        except:
            print 'Error in downloading GOES soft X-ray data. Proceeding with out soft X-ray plot.'

        # third part
        # start to download the fits files
        if plotaia:
            if not aiafits:
                newlist = []
                items = glob.glob('*.fits')
                for names in items:
                    str1 = starttim1.iso[:4] + '_' + starttim1.iso[5:7] + '_' + starttim1.iso[8:10] + 't' + starttim1.iso[
                                                                                                            11:13] + '_' + starttim1.iso[14:16]
                    str2 = str(aiawave)
                    if names.endswith(".fits"):
                        if names.find(str1) != -1 and names.find(str2) != -1:
                            newlist.append(names)
                    newlist.append('0')
                if newlist and os.path.exists(newlist[0]):
                    aiafits = newlist[0]
                else:
                    print 'downloading the aiafits file'
                    wave1 = aiawave - 3
                    wave2 = aiawave + 3
                    t1 = Time(starttim1.mjd - 0.02 / 24., format='mjd')
                    t2 = Time(endtim1.mjd + 0.02 / 24., format='mjd')
                    try:
                        from sunpy.net import vso
                        client = vso.VSOClient()
                        qr = client.query(vso.attrs.Time(t1.iso, t2.iso), vso.attrs.Instrument('aia'), vso.attrs.Wave(wave1 * u.AA, wave2 * u.AA))
                        res = client.get(qr, path='{file}')
                    except:
                        SdoDownloadscript = os.path.join(workdir, 'SdoDownload.py')
                        os.system('rm -rf {}'.format(SdoDownloadscript))
                        fi = open(SdoDownloadscript, 'wb')
                        fi.write('from sunpy.net import vso \n')
                        fi.write('from astropy import units as u \n')
                        fi.write('client = vso.VSOClient() \n')
                        fi.write(
                            "qr = client.query(vso.attrs.Time('{0}', '{1}'), vso.attrs.Instrument('aia'), vso.attrs.Wave({2} * u.AA, {3} * u.AA)) \n".format(
                                t1.iso, t2.iso, wave1, wave2))
                        fi.write("res = client.get(qr, path='{file}') \n")
                        fi.close()

                        try:
                            os.system('python {}'.format(SdoDownloadscript))
                        except NameError:
                            print "Bad input names"
                        except ValueError:
                            print "Bad input values"
                        except:
                            print "Unexpected error:", sys.exc_info()[0]
                            print "Error in Downloading AIA fits files. Proceed without AIA..."

            # Here something is needed to check whether it has finished downloading the fits files or not

            if not aiafits:
                newlist = []
                items = glob.glob('*.fits')
                for nm in items:
                    str1 = starttim1.iso[:4] + '_' + starttim1.iso[5:7] + '_' + starttim1.iso[8:10] + 't' + starttim1.iso[
                                                                                                            11:13] + '_' + starttim1.iso[14:16]
                    str2 = str(aiawave)
                    if nm.find(str1) != -1 and nm.find(str2) != -1:
                        newlist.append(nm)
                if newlist:
                    aiafits = newlist[0]
                    print 'AIA fits ' + aiafits + ' selected'
                else:
                    print 'no AIA fits files found. Proceed without AIA'

            try:
                aiamap = smap.Map(aiafits)
            except:
                print 'error in reading aiafits. Proceed without AIA'

        # RCP or I
        ax4 = plt.subplot(gs2[0, 0])
        ax5 = plt.subplot(gs2[1, 0])
        # LCP or V
        ax6 = plt.subplot(gs2[0, 1])
        ax7 = plt.subplot(gs2[1, 1])

        if fitsfile:
            pass
        else:
            if not imagefile:
                eph = hf.read_horizons(t0=Time(midtime_mjd, format='mjd'))
                if observatory == 'EOVSA' or (not usemsphacenter):
                    print 'This is EOVSA data'
                    # use RA and DEC from FIELD ID 0
                    tb.open(vis+'/FIELD')
                    phadir = tb.getcol('PHASE_DIR').flatten()
                    tb.close()
                    ra0 = phadir[0]
                    dec0 = phadir[1]
                    if stokes == 'RRLL' or stokes == 'RR,LL':
                        print 'Provide stokes: ' + str(stokes) + '. However EOVSA has linear feeds. Force stokes to be IV'
                        stokes = 'I,V'
                else:
                    ra0 = eph['ra'][0]
                    dec0 = eph['dec'][0]

                if not xycen:
                    # use solar disk center as default
                    phasecenter = 'J2000 ' + str(ra0) + 'rad ' + str(dec0) + 'rad'
                else:
                    x0 = np.radians(xycen[0]/3600.)
                    y0 = np.radians(xycen[1]/3600.)
                    p0 = np.radians(eph['p0'][0]) # p angle in radians 
                    raoff = -((x0) * np.cos(p0) - y0 * np.sin(p0))/np.cos(eph['dec'][0])
                    decoff = (x0) * np.sin(p0) + y0 * np.cos(p0)
                    newra = ra0 + raoff
                    newdec = dec0 + decoff
                    phasecenter = 'J2000 ' + str(newra) + 'rad ' + str(newdec) + 'rad'

                imagename = os.path.join(workdir, visname + '.outim')
                if os.path.exists(imagename + '.image') or os.path.exists(imagename + '.flux'):
                    os.system('rm -rf ' + imagename + '.*')
                sto = stokes.replace(',', '')
                print 'do clean for ' + timerange + ' in spw ' + spw + ' stokes ' + sto
                print 'Original phasecenter: '+ str(ra0) + str(dec0)
                print 'use phasecenter: ' + phasecenter
                clean(vis=vis, imagename=imagename, selectdata=True, spw=spw, timerange=timerange, stokes=sto,
                      niter=niter, interactive=interactive, npercycle=50, imsize=imsize, cell=cell, restoringbeam=restoringbeam,
                      weighting='briggs', robust=robust, phasecenter=phasecenter)
                os.system('rm -rf ' + imagename + '.psf')
                os.system('rm -rf ' + imagename + '.flux')
                os.system('rm -rf ' + imagename + '.model')
                os.system('rm -rf ' + imagename + '.mask')
                os.system('rm -rf ' + imagename + '.residual')
                imagefile = imagename + '.image'
            fitsfile = imagefile + '.fits'
            hf.imreg(vis=vis, ephem=eph, imagefile=imagefile, timerange=timerange, reftime=reftime, fitsfile=fitsfile, verbose=True, overwrite=True)
        print 'fits file ' + fitsfile + ' selected'
        ax4.cla()
        ax5.cla()
        ax6.cla()
        ax7.cla()

        rfits = fitsfile
        try:
            hdulist = fits.open(rfits)
            hdu = hdulist[0]
            (npol, nf, nx, ny) = hdu.data.shape
            rmap = smap.Map(hdu.data[0, 0, :, :], hdu.header)
        except:
            print 'radio fits file not recognized by sunpy.map. Aborting...'
            return -1
        if npol > 1:
            rmap1 = smap.Map(hdu.data[0, 0, :, :], hdu.header)
            rmap2 = smap.Map(hdu.data[1, 0, :, :], hdu.header)

        XX, YY = np.meshgrid(np.arange(rmap.data.shape[1]), np.arange(rmap.data.shape[0]))
        try:
            rmapx, rmapy = rmap.pixel_to_data(XX * u.pix, YY * u.pix)
        except:
            rmapxy = rmap.pixel_to_data(XX * u.pix, YY * u.pix)
            rmapx = rmapxy.Tx
            rmapy = rmapxy.Ty

        if not xyrange:
            if xycen:
                x0 = xycen[0] * u.arcsec
                y0 = xycen[1] * u.arcsec
            if not xycen:
                row, col = rmap1.data.shape
                positon = np.nanargmax(rmap1.data)
                m, n = divmod(positon, col)
                x0 = rmap1.xrange[0] + rmap1.scale[1] * (n + 0.5) * u.pix
                y0 = rmap1.yrange[0] + rmap1.scale[0] * (m + 0.5) * u.pix
            if len(fov) == 1:
                fov=[fov]*2
            sz_x = fov[0] * u.arcsec
            sz_y = fov[1] * u.arcsec
            x1 = x0 - sz_x/2.
            x2 = x0 + sz_x/2.
            y1 = y0 - sz_y/2.
            y2 = y0 + sz_y/2.
            xyrange = [[x1.value, x2.value], [y1.value, y2.value]]
        else:
            sz_x = xyrange[0][1] - xyrange[0][0]
            sz_y = xyrange[1][1] - xyrange[1][0]


        clevels1 = np.linspace(0.2, 0.9, 5)
        if stokes.split(',')[1] == 'V':
            clevels2 = np.array([0.8, -0.6, -0.4, -0.2, 0.2, 0.4, 0.6, 0.8])
        else:
            clevels2 = np.linspace(0.2, 0.9, 5)
        if 'aiamap' in vars():
            aiamap.plot_settings['cmap'] = plt.get_cmap('binary')
            if rmap:
                title = 'AIA {0:.0f} + {1} {2:6.3f} GHz'.format(aiamap.wavelength.value, observatory, (bfreqghz + efreqghz) / 2.0)
            else:
                title = 'AIA {0:.0f}'.format(aiamap.wavelength.value)
            aiamap.plot(axes=ax4)
            ax4.set_title(title + ' ' + stokes.split(',')[0], fontsize=12)
            aiamap.draw_limb()
            aiamap.draw_grid()
            aiamap.draw_rectangle((xyrange[0][0], xyrange[1][0]) * u.arcsec, sz_x, sz_y)
            aiamap.plot(axes=ax6)
            ax6.set_title(title + ' ' + stokes.split(',')[1], fontsize=12)
            aiamap.draw_limb()
            aiamap.draw_grid()
            aiamap.draw_rectangle((xyrange[0][0], xyrange[1][0]) * u.arcsec, sz_x, sz_y)
            if rmap:
                ax4.contour(rmapx.value, rmapy.value, rmap1.data, levels=clevels1 * np.nanmax(rmap1.data), cmap=cm.jet)
                ax6.contour(rmapx.value, rmapy.value, rmap2.data, levels=clevels2 * np.nanmax(rmap2.data), cmap=cm.RdBu)
            ax4.text(0.02, 0.02, 'AIA {0:.0f} '.format(aiamap.wavelength.value) + aiamap.date.strftime('%H:%M:%S'),
                     verticalalignment='bottom', horizontalalignment='left', transform=ax4.transAxes, color='k',
                     fontsize=10)
            ax6.text(0.02, 0.02, 'AIA {0:.0f} '.format(aiamap.wavelength.value) + aiamap.date.strftime('%H:%M:%S'),
                     verticalalignment='bottom', horizontalalignment='left', transform=ax6.transAxes, color='k',
                     fontsize=10)
        else:
            title = '{0} {1:6.3f} GHz'.format(observatory, (bfreqghz + efreqghz) / 2.0)
            rmap1.plot(axes=ax4, cmap=cm.jet)
            ax4.set_title(title + ' ' + stokes.split(',')[0], fontsize=12)
            rmap1.draw_limb()
            rmap1.draw_grid()
            rmap1.draw_rectangle((xyrange[0][0], xyrange[1][0]) * u.arcsec, sz_x, sz_y)
            rmap2.plot(axes=ax6, cmap=cm.RdBu)
            ax6.set_title(title + ' ' + stokes.split(',')[1], fontsize=12)
            rmap2.draw_limb()
            rmap2.draw_grid()
            # ax4.contour(rmapx.value, rmapy.value, rmap1.data, levels=np.linspace(0.2, 0.9, 5) * np.nanmax(rmap1.data),
            #            cmap=cm.gray)
            # ax6.contour(rmapx.value, rmapy.value, rmap2.data, levels=np.linspace(0.2, 0.9, 5) * np.nanmax(rmap2.data),
            #            cmap=cm.gray)
            rmap2.draw_rectangle((xyrange[0][0], xyrange[1][0]) * u.arcsec, sz_x, sz_y)  
        ax4.set_xlim(-1200, 1200)
        ax4.set_ylim(-1200, 1200)
        ax6.set_xlim(-1200, 1200)
        ax6.set_ylim(-1200, 1200)

        try:
            subrmap1 = rmap1.submap(xyrange[0] * u.arcsec, xyrange[1] * u.arcsec)
            subrmap2 = rmap2.submap(xyrange[0] * u.arcsec, xyrange[1] * u.arcsec)
        except:
            bl = SkyCoord(xyrange[0][0] * u.arcsec, xyrange[1][0] * u.arcsec, frame=rmap1.coordinate_frame)
            tr = SkyCoord(xyrange[0][1] * u.arcsec, xyrange[1][1] * u.arcsec, frame=rmap1.coordinate_frame)
            subrmap1 = rmap1.submap(bl, tr)
            subrmap2 = rmap2.submap(bl, tr)

        XX, YY = np.meshgrid(np.arange(subrmap1.data.shape[1]), np.arange(subrmap1.data.shape[0]))
        try:
            subrmapx, subrmapy = subrmap1.pixel_to_data(XX * u.pix, YY * u.pix)
        except:
            subrmapxy = subrmap1.pixel_to_data(XX * u.pix, YY * u.pix)
            subrmapx = subrmapxy.Tx
            subrmapy = subrmapxy.Ty

        if 'aiamap' in vars():
            try:
                subaiamap = aiamap.submap(xyrange[0] * u.arcsec, xyrange[1] * u.arcsec)
            except:
                bl = SkyCoord(xyrange[0][0] * u.arcsec, xyrange[1][0] * u.arcsec, frame=aiamap.coordinate_frame)
                tr = SkyCoord(xyrange[0][1] * u.arcsec, xyrange[1][1] * u.arcsec, frame=aiamap.coordinate_frame)
                subaiamap = aiamap.submap(bl, tr)

            subaiamap.plot(axes=ax5, title='')
            subaiamap.draw_limb()
            subaiamap.draw_grid()
            subaiamap.plot(axes=ax7, title='')
            subaiamap.draw_limb()
            subaiamap.draw_grid()
            ax5.contour(subrmapx.value, subrmapy.value, subrmap1.data, levels=clevels1 * np.nanmax(subrmap1.data), cmap=cm.jet)
            ax7.contour(subrmapx.value, subrmapy.value, subrmap2.data, levels=clevels2 * np.nanmax(subrmap2.data),
                        cmap=cm.RdBu)  # subaiamap.draw_rectangle((fov[0][0], fov[1][0]) * u.arcsec, 400 * u.arcsec, 400 * u.arcsec)
        else:
            subrmap1.plot(axes=ax5, cmap=cm.jet, title='')
            subrmap1.draw_limb()
            subrmap1.draw_grid()
            subrmap2.plot(axes=ax7, cmap=cm.RdBu, title='')
            subrmap2.draw_limb()
            subrmap2.draw_grid()  # ax5.contour(subrmapx.value, subrmapy.value, subrmap1.data,  #            levels=clevels1 * np.nanmax(subrmap1.data), cmap=cm.gray)  # ax7.contour(subrmapx.value, subrmapy.value, subrmap2.data,  #            levels=clevels2 * np.nanmax(subrmap2.data), cmap=cm.gray)  # subrmap1.draw_rectangle((fov[0][0], fov[1][0]) * u.arcsec, 400 * u.arcsec, 400 * u.arcsec)  # subrmap2.draw_rectangle((fov[0][0], fov[1][0]) * u.arcsec, 400 * u.arcsec, 400 * u.arcsec)
        ax5.set_xlim(xyrange[0])
        ax5.set_ylim(xyrange[1])
        ax5.text(0.02, 0.02, observatory + ' ' + rmap.date.strftime('%H:%M:%S.%f')[:-3], verticalalignment='bottom',
                 horizontalalignment='left', transform=ax5.transAxes, color='k', fontsize=10)
        ax7.set_xlim(xyrange[0])
        ax7.set_ylim(xyrange[1])
        ax7.text(0.02, 0.02, observatory + ' ' + rmap.date.strftime('%H:%M:%S.%f')[:-3], verticalalignment='bottom',
                 horizontalalignment='left', transform=ax7.transAxes, color='k', fontsize=10)

        fig.show()
예제 #13
0
파일: plotcomp.py 프로젝트: keflavich/casa
def plotcomp(compdict, showplot=True, wantdict=False, symb=',',
             include0amp=False, include0bl=False, blunit='', bl0flux=0.0):

    """
    Given a dict including
    
    {'clist': component list,
     'objname': objname,
     'epoch': epoch,
     'shape': component shape dict, including direction.
     'freqs (GHz)': pl.array of frequencies,
     'antennalist': An array configuration file as used by simdata,
     'savedfig': False or, if specified, the filename to save the plot to,
     'standard': setjy fluxstandard type},

    and symb: One of matplotlib's codes for plot symbols: .:,o^v<>s+xDd234hH|_
          default: ',':  The smallest points I could find,

    make a plot of visibility amplitude vs. baseline length for clist at epoch.

    If antennalist is not found as is, it will look for antennalist in
    os.getenv('CASAPATH').split(' ')[0] + '/data/alma/simmos/'.
    
    showplot: Whether or not to show the plot on screen.

    If wantdict is True, it returns a dictionary with the amplitudes and
    baselines on success.  Otherwise, it returns True or False as its estimated
    success value.

    include0amp: Force the lower limit of the amplitude axis to 0.
    include0bl: Force the lower limit of the baseline length axis to 0.
    blunit: unit of the baseline length (='' used the unit in the data or klambda)
    bl0flux: Zero baseline flux
    """
    def failval():
        """
        Returns an appropriate failure value.
        Note that mydict.update(plotcomp(wantdict=True, ...)) would give a
        confusing error message if plotcomp returned False.
        """
        retval = False
        if wantdict:
            retval = {}
        return retval
    retval = failval()  # Default
    try:
        clist = compdict['clist']
        objname = compdict['objname']
        epoch = compdict['epoch']
        epstr = mepoch_to_str(epoch)
        antennalist = compdict['antennalist']

        # Read the configuration info.
        if not antennalist:
            print "compdict['antennalist'] must be set!"
            print "Try something in", os.getenv("CASAPATH").split(' ')[0] + "/data/alma/simmos/"
            return failval()
        # Try repodir if raw antennalist doesn't work.
        if not os.path.exists(antennalist):
            repodir = os.getenv("CASAPATH").split(' ')[0] + "/data/alma/simmos/"
            antennalist = repodir + antennalist

        su = simutil("")
        stnx, stny, stnz, diam, padnames, nant, telescopename = su.readantenna(antennalist)
        #print "telescopename:", telescopename

        # Check that the source is up.
        myme = metool()
        posobs = myme.observatory(telescopename)
        #print "posobs:", posobs
        myme.doframe(epoch)
        myme.doframe(posobs)
        azel = myme.measure(compdict['shape']['direction'], 'azel')
        azeldegs = tuple([qa.convert(azel[m], 'deg')['value'] for m in ('m0', 'm1')])
        casalog.post("(az, el): (%.2f, %.2f) degrees" % azeldegs)
        # riseset blabs to the logger, so introduce it now.
        casalog.post('Rise and set times of ' + objname + " from " + telescopename + ':')
        approx = ''
        if 'JPL' in compdict.get('standard', 'JPL'):
            # The object is in the Solar System or not known to be extragalactic.
            approx = "APPROXIMATE.  The times do not account for the apparent motion of "\
                     + objname + "."
            casalog.post("  (" + approx + ")")
        riset = myme.riseset(compdict['shape']['direction'])
        msg = ''
        if riset['rise'] == 'above':
            msg = objname + " is circumpolar"
        elif riset['rise'] == 'below':
            msg = objname + ' is not visible from ' + telescopename
        if msg:
            if approx:
                msg += ' around ' + mepoch_to_str(epoch)
            casalog.post(msg)
        else:
            for t in riset:
                riset[t]['str'] = mepoch_to_str(riset[t]['utc'])
            casalog.post(objname + " rises at %s and sets at %s." % (riset['rise']['str'],
                                                                     riset['set']['str']))
            tmeridian=(riset['rise']['utc']['m0']['value']+riset['set']['utc']['m0']['value'])/2.
            casalog.post(objname + ': meridian passage at ' + qa.time(str(tmeridian)+'d')[0])

        if approx:
            riset['NOTE'] = approx
        if not azel['m1']['value'] > 0.0:
            casalog.post(objname + " is not visible from " + telescopename + " at " + epstr,
                         'SEVERE')
            if wantdict:
                return riset
            else:
                return False

        # Start a temp MS.
        workingdir = os.path.abspath(os.path.dirname(clist.rstrip('/')))
        tempms = tempfile.mkdtemp(prefix=objname, dir=workingdir)

        mysm = smtool()
        mysm.open(tempms)

        su.setcfg(mysm, telescopename, stnx, stny, stnz, diam,
                  padnames, posobs)

        #print "cfg set"

        # Only 1 polarization is wanted for now.
        stokes, feeds = su.polsettings(telescopename, 'RR')
        
        casalog.post("stokes, feeds: %s, %s" % (stokes, feeds))
        fband = su.bandname(compdict['freqs (GHz)'][0])
        chaninc = 1.0
        nchan = len(compdict['freqs (GHz)'])
        if nchan > 1:
            chaninc = (compdict['freqs (GHz)'][-1] - compdict['freqs (GHz)'][0]) / (nchan - 1)
        mysm.setspwindow(spwname=fband,
                         freq=str(compdict['freqs (GHz)'][0]) + 'GHz', 
                         deltafreq=str(chaninc) + 'GHz', 
                         freqresolution='1Hz', 
                         nchannels=nchan, refcode="LSRK",
                         stokes=stokes)
        mysm.setfeed(mode=feeds, pol=[''])
        mysm.setlimits(shadowlimit=0.01, elevationlimit='10deg')
        mysm.setauto(0.0)
        mysm.setfield(sourcename=objname,
                      sourcedirection=compdict['shape']['direction'],
                      calcode="OBJ", distance='0m')
        mysm.settimes(integrationtime="1s", usehourangle=False,
                      referencetime=epoch)
        
        # this only creates blank uv entries
        mysm.observe(sourcename=objname, spwname=fband,
                   starttime="-0.5s", stoptime="0.5s", project=objname)

        mysm.setdata(fieldid=[0])
        mysm.setvp()
        casalog.post("done setting up simulation parameters")

        mysm.predict(complist=clist)        # do actual calculation of visibilities:

        mysm.close()
        casalog.post("Simulation finished.")

        mytb = tbtool()
        mytb.open(tempms)
        data = mytb.getcol('DATA')[0]       # Again, only 1 polarization for now. 
        data = abs(data)
        baselines = mytb.getcol('UVW')[:2,:]  # Drop w.
        datablunit = mytb.getcolkeywords('UVW')['QuantumUnits']
        mytb.close()
        #print "Got the data and baselines"
        shutil.rmtree(tempms)

        if datablunit[1] != datablunit[0]:
            casalog.post('The baseline units are mismatched!: %s' % datablunit,
                         'SEVERE')
            return failval()
        datablunit = datablunit[0]
        # uv dist unit in klambda or m
        if datablunit == 'm' and blunit=='klambda':
            kl = qa.constants('C')['value']/(compdict['freqs (GHz)'][0]*1e6)
            blunit = 'k$\lambda$'
        else:
            blunit = datablunit
            kl = 1.0
        pl.ioff()
        #baselines = pl.hypot(baselines[0]/kl, baselines[1]/kl)
        baselines = pl.hypot(baselines[0], baselines[1])

        #if not showplot:
        #    casalog.post('Sorry, not showing the plot is not yet implemented',
        #                 'WARN')

        if showplot: 
          pl.ion()
        pl.clf()
        pl.ioff() 
        nfreqs = len(compdict['freqs (GHz)'])
        for freqnum in xrange(nfreqs):
            freq = compdict['freqs (GHz)'][freqnum]
            casalog.post("Plotting " + str(freq) + " GHz.")
            pl.plot(baselines/kl, data[freqnum], symb, label="%.3g GHz" % freq)
            #pl.plot(baselines, data[freqnum], symb, label="%.3g GHz" % freq)
        pl.xlabel("Baseline length (" + blunit + ")")
        pl.ylabel("Visibility amplitude (Jy)")
        if include0amp:
            pl.ylim(ymin=0.0)
        if include0bl:
            pl.xlim(xmin=0.0)
        pl.suptitle(objname + " (predicted by %s)" % compdict['standard'], fontsize=14)
        #pl.suptitle(objname + " (predicted)", fontsize=14)

        # Unlike compdict['antennalist'], antennalist might have had repodir
        # prefixed to it.
        pl.title('at ' + epstr + ' for ' + os.path.basename(compdict['antennalist']), fontsize=10)
        titletxt='($%.0f^\circ$ az, $%.0f^\circ$ el)' % azeldegs
        # for comparison of old and new models - omit azeldegs as all in az~0
        if bl0flux > 0.0:
            if len(compdict['freqs (GHz)']) == 1:
                titletxt+='\n bl0 flux:%.3f Jy' % bl0flux
            else:
                titletxt+='\n bl0 flux:%.3f Jy @ %s GHz' % (bl0flux, compdict['freqs (GHz)'][0]) 
        pl.legend(loc='best', title=titletxt)
        #pl.legend(loc='best', title='($%.0f^\circ$ az, $%.0f^\circ$ el)' % azeldegs)
        y_formatter=matplotlib.ticker.ScalarFormatter(useOffset=False)
        pl.axes().yaxis.set_major_formatter(y_formatter) 
        if showplot:
          pl.ion()
          pl.draw()
        if compdict.get('savedfig'):
            pl.savefig(compdict.get('savedfig'))
            casalog.post("Saved plot to " + str(compdict.get('savedfig')))

        if wantdict:
            retval = {'amps': data,
                      'antennalist': antennalist,  # Absolute path, now.
                      'azel': azel,
                      'baselines': baselines,
                      'blunit': blunit,
                      'riseset': riset,
                      'savedfig': compdict.get('savedfig')}
        else:
            retval = True
    except Exception, instance:
        casalog.post(str(instance), 'SEVERE')
        if os.path.isdir(tempms):
            shutil.rmtree(tempms)
예제 #14
0
파일: std_uti.py 프로젝트: Wei24/py_working
def draw_goes(goestime=None, ax=None):
    if goestime:
        btgoes = goestime[0]
        etgoes = goestime[1]
    else:
        datstrg = datstr.replace('-', '/')
        btgoes = datstrg + ' ' + qa.time(
            qa.quantity(tim[0] - 1800, 's'), form='clean', prec=9)[0]
        etgoes = datstrg + ' ' + qa.time(
            qa.quantity(tim[tidx[-1] - 1] + 1800,
                        's'), form='clean', prec=9)[0]
    #if verbose:
    #    print 'Acquire GOES soft X-ray data in from ' + btgoes + ' to ' + etgoes

    #ax3 = plt.subplot(gs1[2])

    goesscript = os.path.join(workdir, 'goes.py')
    goesdatafile = os.path.join(workdir, 'goes.dat')
    os.system('rm -rf {}'.format(goesscript))
    fi = open(goesscript, 'wb')
    fi.write('import os \n')
    fi.write('from sunpy.time import TimeRange \n')
    fi.write('from sunpy import lightcurve as lc \n')
    fi.write('import pickle \n')
    fi.write('goesplottim = TimeRange("{0}", "{1}") \n'.format(btgoes, etgoes))
    fi.write('goes = lc.GOESLightCurve.create(goesplottim) \n')
    fi.write('fi2 = open("{}", "wb") \n'.format(goesdatafile))
    fi.write('pickle.dump(goes, fi2) \n')
    fi.write('fi2.close()')
    fi.close()

    try:
        os.system('python {}'.format(goesscript))
    except NameError:
        print "Bad input names"
    except ValueError:
        print "Bad input values"
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print "Error in generating GOES light curves. Proceed without GOES..."

    if os.path.exists(goesdatafile):
        fi1 = file(goesdatafile, 'rb')
        goest = pickle.load(fi1)
        fi1.close()

        dates = mpl.dates.date2num(parse_time(goest.data.index))
        goesdif = np.diff(goest.data['xrsb'])
        gmax = np.nanmax(goesdif)
        gmin = np.nanmin(goesdif)
        ran = gmax - gmin
        db = 2.8 / ran
        goesdifp = goesdif * db + gmin + (-6)
        ax.plot_date(dates,
                     np.log10(goest.data['xrsb']),
                     '-',
                     label='1.0--8.0 $\AA$',
                     color='red',
                     lw=2)
        ax.plot_date(dates[0:-1],
                     goesdifp,
                     '-',
                     label='derivate',
                     color='blue',
                     lw=0.4)

        ax.set_ylim([-7, -3])
        ax.set_yticks([-7, -6, -5, -4, -3])
        ax.set_yticklabels([
            r'$10^{-7}$', r'$10^{-6}$', r'$10^{-5}$', r'$10^{-4}$',
            r'$10^{-3}$'
        ])
        ax.set_title('Goes Soft X-ray', fontsize=12)
        ax.set_ylabel('Watts m$^{-2}$')
        ax.set_xlabel(datetime.datetime.isoformat(goest.data.index[0])[0:10])
        ax.axvspan(dates[899], dates[dates.size - 899], alpha=0.2)

        ax.yaxis.grid(True, 'major')
        ax.xaxis.grid(False, 'major')
        ax.legend(prop={'size': 6})

        formatter = mpl.dates.DateFormatter('%H:%M')
        ax.xaxis.set_major_formatter(formatter)
        ax.fmt_xdata = mpl.dates.DateFormatter('%H:%M')
예제 #15
0
def read_msinfo(vis=None, msinfofile=None, use_scan_time=True):
    import glob
    # read MS information #
    msinfo = dict.fromkeys([
        'vis', 'scans', 'fieldids', 'btimes', 'btimestr', 'inttimes', 'ras',
        'decs', 'observatory'
    ])
    ms.open(vis)
    metadata = ms.metadata()
    observatory = metadata.observatorynames()[0]
    scans = ms.getscansummary()
    scanids = sorted(scans.keys(), key=lambda x: int(x))
    nscanid = len(scanids)
    btimes = []
    btimestr = []
    etimes = []
    fieldids = []
    inttimes = []
    dirs = []
    ras = []
    decs = []
    ephem_file = glob.glob(vis + '/FIELD/EPHEM*SUN.tab')
    if ephem_file:
        print('Loading ephemeris info from {}'.format(ephem_file[0]))
        tb.open(ephem_file[0])
        col_ra = tb.getcol('RA')
        col_dec = tb.getcol('DEC')
        col_mjd = tb.getcol('MJD')
        if use_scan_time:
            from scipy.interpolate import interp1d
            f_ra = interp1d(col_mjd, col_ra)
            f_dec = interp1d(col_mjd, col_dec)
            for idx, scanid in enumerate(scanids):
                btimes.append(scans[scanid]['0']['BeginTime'])
                etimes.append(scans[scanid]['0']['EndTime'])
                fieldid = scans[scanid]['0']['FieldId']
                fieldids.append(fieldid)
                inttimes.append(scans[scanid]['0']['IntegrationTime'])
            ras = f_ra(np.array(btimes))
            decs = f_dec(np.array(btimes))
            ras = qa.convert(qa.quantity(ras, 'deg'), 'rad')
            decs = qa.convert(qa.quantity(decs, 'deg'), 'rad')
        else:
            ras = qa.convert(qa.quantity(col_ra, 'deg'), 'rad')
            decs = qa.convert(qa.quantity(col_dec, 'deg'), 'rad')
    else:
        for idx, scanid in enumerate(scanids):
            btimes.append(scans[scanid]['0']['BeginTime'])
            etimes.append(scans[scanid]['0']['EndTime'])
            fieldid = scans[scanid]['0']['FieldId']
            fieldids.append(fieldid)
            inttimes.append(scans[scanid]['0']['IntegrationTime'])
            dir = ms.getfielddirmeas('PHASE_DIR', fieldid)
            dirs.append(dir)
            ras.append(dir['m0'])
            decs.append(dir['m1'])
    ms.close()
    btimestr = [
        qa.time(qa.quantity(btimes[idx], 'd'), form='fits', prec=10)[0]
        for idx in range(nscanid)
    ]
    msinfo['vis'] = vis
    msinfo['scans'] = scans
    msinfo['fieldids'] = fieldids
    msinfo['btimes'] = btimes
    msinfo['btimestr'] = btimestr
    msinfo['inttimes'] = inttimes
    msinfo['ras'] = ras
    msinfo['decs'] = decs
    msinfo['observatory'] = observatory
    if msinfofile:
        np.savez(msinfofile,
                 vis=vis,
                 scans=scans,
                 fieldids=fieldids,
                 btimes=btimes,
                 btimestr=btimestr,
                 inttimes=inttimes,
                 ras=ras,
                 decs=decs,
                 observatory=observatory)
    return msinfo
예제 #16
0
def imreg(vis=None,
          ephem=None,
          msinfo=None,
          imagefile=None,
          timerange=None,
          reftime=None,
          fitsfile=None,
          beamfile=None,
          offsetfile=None,
          toTb=None,
          sclfactor=1.0,
          verbose=False,
          p_ang=False,
          overwrite=True,
          usephacenter=True,
          deletehistory=False,
          subregion=[],
          docompress=False):
    ''' 
    main routine to register CASA images
           Required Inputs:
               vis: STRING. CASA measurement set from which the image is derived
               imagefile: STRING or LIST. name of the input CASA image
               timerange: STRING or LIST. timerange used to generate the CASA image, must have the same length as the input images. 
                          Each element should be in CASA standard time format, e.g., '2012/03/03/12:00:00~2012/03/03/13:00:00'
           Optional Inputs:
               msinfo: DICTIONARY. CASA MS information, output from read_msinfo. If not provided, generate one from the supplied vis
               ephem: DICTIONARY. solar ephem, output from read_horizons. 
                      If not provided, query JPL Horizons based on time info of the vis (internet connection required)
               fitsfile: STRING or LIST. name of the output registered fits files
               reftime: STRING or LIST. Each element should be in CASA standard time format, e.g., '2012/03/03/12:00:00'
               offsetfile: optionally provide an offset with a series of solar x and y offsets with timestamps 
               toTb: Bool. Convert the default Jy/beam to brightness temperature?
               sclfactor: scale the image values up by its value (to compensate VLA 20 dB attenuator)
               verbose: Bool. Show more diagnostic info if True.
               usephacenter: Bool -- if True, correct for the RA and DEC in the ms file based on solar empheris.
                                     Otherwise assume the phasecenter is correctly pointed to the solar disk center
                                     (EOVSA case)
               subregion: Region selection. See 'help par.region' for details.
    Usage:
    >>> from suncasa.utils import helioimage2fits as hf
    >>> hf.imreg(vis='mydata.ms', imagefile='myimage.image', fitsfile='myimage.fits',
                 timerange='2017/08/21/20:21:10~2017/08/21/20:21:18')
    The output fits file is 'myimage.fits'

    History:
    BC (sometime in 2014): function was first wrote, followed by a number of edits by BC and SY
    BC (2019-07-16): Added checks for stokes parameter. Verified that for converting from Jy/beam to brightness temperature,
                     the convention of 2*k_b*T should always be used. I.e., for unpolarized source, stokes I, RR, LL, XX, YY, 
                     etc. in the output CASA images from (t)clean should all have same values of radio intensity 
                     (in Jy/beam) and brightness temperature (in K).

    '''

    if deletehistory:
        ms_clearhistory(vis)

    if not imagefile:
        raise ValueError('Please specify input image')
    if not timerange:
        raise ValueError('Please specify timerange of the input image')
    if type(imagefile) == str:
        imagefile = [imagefile]
    if type(timerange) == str:
        timerange = [timerange]
    if not fitsfile:
        fitsfile = [img + '.fits' for img in imagefile]
    if type(fitsfile) == str:
        fitsfile = [fitsfile]
    nimg = len(imagefile)
    if len(timerange) != nimg:
        raise ValueError(
            'Number of input images does not equal to number of timeranges!')
    if len(fitsfile) != nimg:
        raise ValueError(
            'Number of input images does not equal to number of output fits files!'
        )
    nimg = len(imagefile)
    if verbose:
        print(str(nimg) + ' images to process...')

    if reftime:  # use as reference time to find solar disk RA and DEC to register the image, but not the actual timerange associated with the image
        if type(reftime) == str:
            reftime = [reftime] * nimg
        if len(reftime) != nimg:
            raise ValueError(
                'Number of reference times does not match that of input images!'
            )
        helio = ephem_to_helio(vis,
                               ephem=ephem,
                               msinfo=msinfo,
                               reftime=reftime,
                               usephacenter=usephacenter)
    else:
        # use the supplied timerange to register the image
        helio = ephem_to_helio(vis,
                               ephem=ephem,
                               msinfo=msinfo,
                               reftime=timerange,
                               usephacenter=usephacenter)

    if toTb:
        (bmajs, bmins, bpas, beamunits,
         bpaunits) = getbeam(imagefile=imagefile, beamfile=beamfile)

    for n, img in enumerate(imagefile):
        if verbose:
            print('processing image #' + str(n) + ' ' + img)
        fitsf = fitsfile[n]
        timeran = timerange[n]
        # obtain duration of the image as FITS header exptime
        try:
            [tbg0, tend0] = timeran.split('~')
            tbg_d = qa.getvalue(qa.convert(qa.totime(tbg0), 'd'))[0]
            tend_d = qa.getvalue(qa.convert(qa.totime(tend0), 'd'))[0]
            tdur_s = (tend_d - tbg_d) * 3600. * 24.
            dateobs = qa.time(qa.quantity(tbg_d, 'd'), form='fits', prec=10)[0]
        except:
            print('Error in converting the input timerange: ' + str(timeran) +
                  '. Proceeding to the next image...')
            continue

        hel = helio[n]
        if not os.path.exists(img):
            warnings.warn('{} does not existed!'.format(img))
        else:
            if os.path.exists(fitsf) and not overwrite:
                raise ValueError(
                    'Specified fits file already exists and overwrite is set to False. Aborting...'
                )
            else:
                p0 = hel['p0']
                tb.open(img + '/logtable', nomodify=False)
                nobs = tb.nrows()
                tb.removerows([i + 1 for i in range(nobs - 1)])
                tb.close()
                ia.open(img)
                imr = ia.rotate(pa=str(-p0) + 'deg')
                if subregion is not []:
                    imr = imr.subimage(region=subregion)
                imr.tofits(fitsf, history=False, overwrite=overwrite)
                imr.close()
                imsum = ia.summary()
                ia.close()
                ia.done()

            # construct the standard fits header
            # RA and DEC of the reference pixel crpix1 and crpix2
            (imra, imdec) = (imsum['refval'][0], imsum['refval'][1])
            # find out the difference of the image center to the CASA phase center
            # RA and DEC difference in arcseconds
            ddec = degrees((imdec - hel['dec_fld'])) * 3600.
            dra = degrees((imra - hel['ra_fld']) * cos(hel['dec_fld'])) * 3600.
            # Convert into image heliocentric offsets
            prad = -radians(hel['p0'])
            dx = (-dra) * cos(prad) - ddec * sin(prad)
            dy = (-dra) * sin(prad) + ddec * cos(prad)
            if offsetfile:
                try:
                    offset = np.load(offsetfile)
                except:
                    raise ValueError(
                        'The specified offsetfile does not exist!')
                reftimes_d = offset['reftimes_d']
                xoffs = offset['xoffs']
                yoffs = offset['yoffs']
                timg_d = hel['reftime']
                ind = bisect.bisect_left(reftimes_d, timg_d)
                xoff = xoffs[ind - 1]
                yoff = yoffs[ind - 1]
            else:
                xoff = hel['refx']
                yoff = hel['refy']
            if verbose:
                print(
                    'offset of image phase center to visibility phase center (arcsec): dx={0:.2f}, dy={1:.2f}'
                    .format(dx, dy))
                print(
                    'offset of visibility phase center to solar disk center (arcsec): dx={0:.2f}, dy={1:.2f}'
                    .format(xoff, yoff))
            (crval1, crval2) = (xoff + dx, yoff + dy)
            # update the fits header to heliocentric coordinates

            hdu = pyfits.open(fitsf, mode='update')
            hdu[0].verify('fix')
            header = hdu[0].header
            dshape = hdu[0].data.shape
            ndim = hdu[0].data.ndim
            (cdelt1,
             cdelt2) = (-header['cdelt1'] * 3600., header['cdelt2'] * 3600.
                        )  # Original CDELT1, 2 are for RA and DEC in degrees
            header['cdelt1'] = cdelt1
            header['cdelt2'] = cdelt2
            header['cunit1'] = 'arcsec'
            header['cunit2'] = 'arcsec'
            header['crval1'] = crval1
            header['crval2'] = crval2
            header['ctype1'] = 'HPLN-TAN'
            header['ctype2'] = 'HPLT-TAN'
            header['date-obs'] = dateobs  # begin time of the image
            if not p_ang:
                hel['p0'] = 0
            try:
                # this works for pyfits version of CASA 4.7.0 but not CASA 4.6.0
                if tdur_s:
                    header.set('exptime', tdur_s)
                else:
                    header.set('exptime', 1.)
                header.set('p_angle', hel['p0'])
                header.set('hgln_obs', 0.)
                header.set('rsun_ref', sun.constants.radius.value)
                if sunpyver <= 1:
                    header.set(
                        'dsun_obs',
                        sun.sunearth_distance(Time(dateobs)).to(u.meter).value)
                    header.set(
                        'rsun_obs',
                        sun.solar_semidiameter_angular_size(
                            Time(dateobs)).value)
                    header.set(
                        'hglt_obs',
                        sun.heliographic_solar_center(Time(dateobs))[1].value)
                else:
                    header.set(
                        'dsun_obs',
                        sun.earth_distance(Time(dateobs)).to(u.meter).value)
                    header.set('rsun_obs',
                               sun.angular_radius(Time(dateobs)).value)
                    header.set('hglt_obs', sun.L0(Time(dateobs)).value)
            except:
                # this works for astropy.io.fits
                if tdur_s:
                    header.append(('exptime', tdur_s))
                else:
                    header.append(('exptime', 1.))
                header.append(('p_angle', hel['p0']))
                header.append(('hgln_obs', 0.))
                header.append(('rsun_ref', sun.constants.radius.value))
                if sunpyver <= 1:
                    header.append(
                        ('dsun_obs', sun.sunearth_distance(Time(dateobs)).to(
                            u.meter).value))
                    header.append(('rsun_obs',
                                   sun.solar_semidiameter_angular_size(
                                       Time(dateobs)).value))
                    header.append(('hglt_obs',
                                   sun.heliographic_solar_center(
                                       Time(dateobs))[1].value))
                else:
                    header.append(
                        ('dsun_obs',
                         sun.earth_distance(Time(dateobs)).to(u.meter).value))
                    header.append(
                        ('rsun_obs', sun.angular_radius(Time(dateobs)).value))
                    header.append(('hglt_obs', sun.L0(Time(dateobs)).value))

            # check if stokes parameter exist
            exist_stokes = False
            stokes_mapper = {
                'I': 1,
                'Q': 2,
                'U': 3,
                'V': 4,
                'RR': -1,
                'LL': -2,
                'RL': -3,
                'LR': -4,
                'XX': -5,
                'YY': -6,
                'XY': -7,
                'YX': -8
            }
            if 'CRVAL3' in header.keys():
                if header['CTYPE3'] == 'STOKES':
                    stokenum = header['CRVAL3']
                    exist_stokes = True
            if 'CRVAL4' in header.keys():
                if header['CTYPE4'] == 'STOKES':
                    stokenum = header['CRVAL4']
                    exist_stokes = True
            if exist_stokes:
                if stokenum in stokes_mapper.values():
                    stokesstr = list(stokes_mapper.keys())[list(
                        stokes_mapper.values()).index(stokenum)]
                else:
                    print('Stokes parameter {0:d} not recognized'.format(
                        stokenum))
                if verbose:
                    print('This image is in Stokes ' + stokesstr)
            else:
                print(
                    'STOKES Information does not seem to exist! Assuming Stokes I'
                )
                stokenum = 1

            # intensity units to brightness temperature
            if toTb:
                # get restoring beam info
                bmaj = bmajs[n]
                bmin = bmins[n]
                beamunit = beamunits[n]
                data = hdu[
                    0].data  # remember the data order is reversed due to the FITS convension
                keys = list(header.keys())
                values = list(header.values())
                # which axis is frequency?
                faxis = keys[values.index('FREQ')][-1]
                faxis_ind = ndim - int(faxis)
                # find out the polarization of this image
                k_b = qa.constants('k')['value']
                c_l = qa.constants('c')['value']
                # Always use 2*kb for all polarizations
                const = 2. * k_b / c_l**2
                if header['BUNIT'].lower() == 'jy/beam':
                    header['BUNIT'] = 'K'
                    header['BTYPE'] = 'Brightness Temperature'
                    for i in range(dshape[faxis_ind]):
                        nu = header['CRVAL' +
                                    faxis] + header['CDELT' + faxis] * (
                                        i + 1 - header['CRPIX' + faxis])
                        if header['CUNIT' + faxis] == 'KHz':
                            nu *= 1e3
                        if header['CUNIT' + faxis] == 'MHz':
                            nu *= 1e6
                        if header['CUNIT' + faxis] == 'GHz':
                            nu *= 1e9
                        if len(bmaj) > 1:  # multiple (per-plane) beams
                            bmajtmp = bmaj[i]
                            bmintmp = bmin[i]
                        else:  # one single beam
                            bmajtmp = bmaj[0]
                            bmintmp = bmin[0]
                        if beamunit == 'arcsec':
                            bmaj0 = np.radians(bmajtmp / 3600.)
                            bmin0 = np.radians(bmintmp / 3600.)
                        if beamunit == 'arcmin':
                            bmaj0 = np.radians(bmajtmp / 60.)
                            bmin0 = np.radians(bmintmp / 60.)
                        if beamunit == 'deg':
                            bmaj0 = np.radians(bmajtmp)
                            bmin0 = np.radians(bmintmp)
                        if beamunit == 'rad':
                            bmaj0 = bmajtmp
                            bmin0 = bmintmp
                        beam_area = bmaj0 * bmin0 * np.pi / (4. * log(2.))
                        factor = const * nu**2  # SI unit
                        jy_to_si = 1e-26
                        # print(nu/1e9, beam_area, factor)
                        factor2 = sclfactor
                        # if sclfactor:
                        #     factor2 = 100.
                        if faxis == '3':
                            data[:,
                                 i, :, :] *= jy_to_si / beam_area / factor * factor2
                        if faxis == '4':
                            data[
                                i, :, :, :] *= jy_to_si / beam_area / factor * factor2

            header = fu.headerfix(header)
            hdu.flush()
            hdu.close()

            if ndim - np.count_nonzero(np.array(dshape) == 1) > 3:
                docompress = False
                '''
                    Caveat: only 1D, 2D, or 3D images are currently supported by
                    the astropy fits compression. If a n-dimensional image data array
                    does not have at least n-3 single-dimensional entries,
                    force docompress to be False
                '''

                print(
                    'warning: The fits data contains more than 3 non squeezable dimensions. Skipping fits compression..'
                )
            if docompress:
                fitsftmp = fitsf + ".tmp.fits"
                os.system("mv {} {}".format(fitsf, fitsftmp))
                hdu = pyfits.open(fitsftmp)
                hdu[0].verify('fix')
                header = hdu[0].header
                data = hdu[0].data
                fu.write_compressed_image_fits(fitsf,
                                               data,
                                               header,
                                               compression_type='RICE_1',
                                               quantize_level=4.0)
                os.system("rm -rf {}".format(fitsftmp))
    if deletehistory:
        ms_restorehistory(vis)
    return fitsfile
예제 #17
0
def imreg(vis=None,
          ephem=None,
          msinfo=None,
          imagefile=None,
          timerange=None,
          reftime=None,
          fitsfile=None,
          beamfile=None,
          offsetfile=None,
          toTb=None,
          scl100=None,
          verbose=False,
          p_ang=False,
          overwrite=True,
          usephacenter=True,
          deletehistory=False):
    ''' 
    main routine to register CASA images
           Required Inputs:
               vis: STRING. CASA measurement set from which the image is derived
               imagefile: STRING or LIST. name of the input CASA image
               timerange: STRING or LIST. timerange used to generate the CASA image, must have the same length as the input images. 
                          Each element should be in CASA standard time format, e.g., '2012/03/03/12:00:00~2012/03/03/13:00:00'
           Optional Inputs:
               msinfo: DICTIONARY. CASA MS information, output from read_msinfo. If not provided, generate one from the supplied vis
               ephem: DICTIONARY. solar ephem, output from read_horizons. 
                      If not provided, query JPL Horizons based on time info of the vis (internet connection required)
               fitsfile: STRING or LIST. name of the output registered fits files
               reftime: STRING or LIST. Each element should be in CASA standard time format, e.g., '2012/03/03/12:00:00'
               offsetfile: optionally provide an offset with a series of solar x and y offsets with timestamps 
               toTb: Bool. Convert the default Jy/beam to brightness temperature?
               scl100: Bool. If True, scale the image values up by 100 (to compensate VLA 20 dB attenuator)
               verbose: Bool. Show more diagnostic info if True.
               usephacenter: Bool -- if True, correct for the RA and DEC in the ms file based on solar empheris.
                                     Otherwise assume the phasecenter is correctly pointed to the solar disk center
                                     (EOVSA case)
    '''
    ia = iatool()

    if deletehistory:
        msclearhistory(vis)
    if verbose:
        import time
        t0 = time.time()
        prtidx = 1
        print('point {}: {}'.format(prtidx, time.time() - t0))
        prtidx += 1

    if not imagefile:
        raise ValueError, 'Please specify input image'
    if not timerange:
        raise ValueError, 'Please specify timerange of the input image'
    if type(imagefile) == str:
        imagefile = [imagefile]
    if type(timerange) == str:
        timerange = [timerange]
    if not fitsfile:
        fitsfile = [img + '.fits' for img in imagefile]
    if type(fitsfile) == str:
        fitsfile = [fitsfile]
    nimg = len(imagefile)
    if len(timerange) != nimg:
        raise ValueError, 'Number of input images does not equal to number of timeranges!'
    if len(fitsfile) != nimg:
        raise ValueError, 'Number of input images does not equal to number of output fits files!'
    nimg = len(imagefile)
    if verbose:
        print str(nimg) + ' images to process...'

    if verbose:
        print('point {}: {}'.format(prtidx, time.time() - t0))
        prtidx += 1

    if reftime:  # use as reference time to find solar disk RA and DEC to register the image, but not the actual timerange associated with the image
        if type(reftime) == str:
            reftime = [reftime] * nimg
        if len(reftime) != nimg:
            raise ValueError, 'Number of reference times does not match that of input images!'
        helio = ephem_to_helio(vis,
                               ephem=ephem,
                               msinfo=msinfo,
                               reftime=reftime,
                               usephacenter=usephacenter)
    else:
        # use the supplied timerange to register the image
        helio = ephem_to_helio(vis,
                               ephem=ephem,
                               msinfo=msinfo,
                               reftime=timerange,
                               usephacenter=usephacenter)

    if verbose:
        print('point {}: {}'.format(prtidx, time.time() - t0))
        prtidx += 1

    for n, img in enumerate(imagefile):
        if verbose:
            print 'processing image #' + str(n)
        fitsf = fitsfile[n]
        timeran = timerange[n]
        # obtain duration of the image as FITS header exptime
        try:
            [tbg0, tend0] = timeran.split('~')
            tbg_d = qa.getvalue(qa.convert(qa.totime(tbg0), 'd'))[0]
            tend_d = qa.getvalue(qa.convert(qa.totime(tend0), 'd'))[0]
            tdur_s = (tend_d - tbg_d) * 3600. * 24.
            dateobs = qa.time(qa.quantity(tbg_d, 'd'), form='fits', prec=10)[0]
        except:
            print 'Error in converting the input timerange: ' + str(
                timeran) + '. Proceeding to the next image...'
            continue

        if verbose:
            print('point {}: {}'.format(prtidx, time.time() - t0))
            prtidx += 1

        hel = helio[n]
        if not os.path.exists(img):
            raise ValueError, 'Please specify input image'
        if os.path.exists(fitsf) and not overwrite:
            raise ValueError, 'Specified fits file already exists and overwrite is set to False. Aborting...'
        else:
            p0 = hel['p0']
            ia.open(img)
            imr = ia.rotate(pa=str(-p0) + 'deg')
            imr.tofits(fitsf, history=False, overwrite=overwrite)
            imr.close()
            imsum = ia.summary()
            ia.close()

        if verbose:
            print('point {}: {}'.format(prtidx, time.time() - t0))
            prtidx += 1

        # construct the standard fits header
        # RA and DEC of the reference pixel crpix1 and crpix2
        (imra, imdec) = (imsum['refval'][0], imsum['refval'][1])
        # find out the difference of the image center to the CASA phase center
        # RA and DEC difference in arcseconds
        ddec = degrees((imdec - hel['dec_fld'])) * 3600.
        dra = degrees((imra - hel['ra_fld']) * cos(hel['dec_fld'])) * 3600.
        # Convert into image heliocentric offsets
        prad = -radians(hel['p0'])
        dx = (-dra) * cos(prad) - ddec * sin(prad)
        dy = (-dra) * sin(prad) + ddec * cos(prad)
        if offsetfile:
            try:
                offset = np.load(offsetfile)
            except:
                raise ValueError, 'The specified offsetfile does not exist!'
            reftimes_d = offset['reftimes_d']
            xoffs = offset['xoffs']
            yoffs = offset['yoffs']
            timg_d = hel['reftime']
            ind = bisect.bisect_left(reftimes_d, timg_d)
            xoff = xoffs[ind - 1]
            yoff = yoffs[ind - 1]
        else:
            xoff = hel['refx']
            yoff = hel['refy']
        if verbose:
            print 'offset of image phase center to visibility phase center (arcsec): ', dx, dy
            print 'offset of visibility phase center to solar disk center (arcsec): ', xoff, yoff
        (crval1, crval2) = (xoff + dx, yoff + dy)
        # update the fits header to heliocentric coordinates

        if verbose:
            print('point {}: {}'.format(prtidx, time.time() - t0))
            prtidx += 1

        hdu = pyfits.open(fitsf, mode='update')

        if verbose:
            print('point {}: {}'.format(prtidx, time.time() - t0))
            prtidx += 1

        header = hdu[0].header
        (cdelt1,
         cdelt2) = (-header['cdelt1'] * 3600., header['cdelt2'] * 3600.
                    )  # Original CDELT1, 2 are for RA and DEC in degrees
        header['cdelt1'] = cdelt1
        header['cdelt2'] = cdelt2
        header['cunit1'] = 'arcsec'
        header['cunit2'] = 'arcsec'
        header['crval1'] = crval1
        header['crval2'] = crval2
        header['ctype1'] = 'HPLN-TAN'
        header['ctype2'] = 'HPLT-TAN'
        header['date-obs'] = dateobs  # begin time of the image
        if not p_ang:
            hel['p0'] = 0
        try:
            # this works for pyfits version of CASA 4.7.0 but not CASA 4.6.0
            if tdur_s:
                header.set('exptime', tdur_s)
            else:
                header.set('exptime', 1.)
            header.set('p_angle', hel['p0'])
            header.set('dsun_obs',
                       sun.sunearth_distance(Time(dateobs)).to(u.meter).value)
            header.set(
                'rsun_obs',
                sun.solar_semidiameter_angular_size(Time(dateobs)).value)
            header.set('rsun_ref', sun.constants.radius.value)
            header.set('hgln_obs', 0.)
            header.set('hglt_obs',
                       sun.heliographic_solar_center(Time(dateobs))[1].value)
        except:
            # this works for astropy.io.fits
            if tdur_s:
                header.append(('exptime', tdur_s))
            else:
                header.append(('exptime', 1.))
            header.append(('p_angle', hel['p0']))
            header.append(
                ('dsun_obs',
                 sun.sunearth_distance(Time(dateobs)).to(u.meter).value))
            header.append(
                ('rsun_obs',
                 sun.solar_semidiameter_angular_size(Time(dateobs)).value))
            header.append(('rsun_ref', sun.constants.radius.value))
            header.append(('hgln_obs', 0.))
            header.append(
                ('hglt_obs',
                 sun.heliographic_solar_center(Time(dateobs))[1].value))

        if verbose:
            print('point {}: {}'.format(prtidx, time.time() - t0))
            prtidx += 1

        # update intensity units, i.e. to brightness temperature?
        if toTb:
            # get restoring beam info
            (bmajs, bmins, bpas, beamunits,
             bpaunits) = getbeam(imagefile=imagefile, beamfile=beamfile)
            bmaj = bmajs[n]
            bmin = bmins[n]
            beamunit = beamunits[n]
            data = hdu[
                0].data  # remember the data order is reversed due to the FITS convension
            dim = data.ndim
            sz = data.shape
            keys = header.keys()
            values = header.values()
            # which axis is frequency?
            faxis = keys[values.index('FREQ')][-1]
            faxis_ind = dim - int(faxis)
            if header['BUNIT'].lower() == 'jy/beam':
                header['BUNIT'] = 'K'
                header['BTYPE'] = 'Brightness Temperature'
                for i in range(sz[faxis_ind]):
                    nu = header['CRVAL' + faxis] + header['CDELT' + faxis] * (
                        i + 1 - header['CRPIX' + faxis])
                    if header['CUNIT' + faxis] == 'KHz':
                        nu *= 1e3
                    if header['CUNIT' + faxis] == 'MHz':
                        nu *= 1e6
                    if header['CUNIT' + faxis] == 'GHz':
                        nu *= 1e9
                    if len(bmaj) > 1:  # multiple (per-plane) beams
                        bmajtmp = bmaj[i]
                        bmintmp = bmin[i]
                    else:  # one single beam
                        bmajtmp = bmaj[0]
                        bmintmp = bmin[0]
                    if beamunit == 'arcsec':
                        bmaj0 = np.radians(bmajtmp / 3600.)
                        bmin0 = np.radians(bmajtmp / 3600.)
                    if beamunit == 'arcmin':
                        bmaj0 = np.radians(bmajtmp / 60.)
                        bmin0 = np.radians(bmintmp / 60.)
                    if beamunit == 'deg':
                        bmaj0 = np.radians(bmajtmp)
                        bmin0 = np.radians(bmintmp)
                    if beamunit == 'rad':
                        bmaj0 = bmajtmp
                        bmin0 = bmintmp
                    beam_area = bmaj0 * bmin0 * np.pi / (4. * log(2.))
                    k_b = qa.constants('k')['value']
                    c_l = qa.constants('c')['value']
                    factor = 2. * k_b * nu**2 / c_l**2  # SI unit
                    jy_to_si = 1e-26
                    # print nu/1e9, beam_area, factor
                    factor2 = 1.
                    if scl100:
                        factor2 = 100.
                    if faxis == '3':
                        data[:,
                             i, :, :] *= jy_to_si / beam_area / factor * factor2
                    if faxis == '4':
                        data[
                            i, :, :, :] *= jy_to_si / beam_area / factor * factor2

        if verbose:
            print('point {}: {}'.format(prtidx, time.time() - t0))
            prtidx += 1

        hdu.flush()
        hdu.close()

        if verbose:
            print('point {}: {}'.format(prtidx, time.time() - t0))
            prtidx += 1
예제 #18
0
    def test_readAndParseIrregularTbuff(self):
        '''flaghelper: compare the read and parse and apply of irregular tbuff'''
        print ''

        # MJD in seconds of timeranges are these
        # <startTime>4891227930515540000 <endTime>4891227932453838000
        # <startTime>4891228473545856000 <endTime>4891228473731891000
        # <startTime>4891226924455911000 <endTime>4891226927502314000
        # <startTime>4891228838164987000 <endTime>4891228838418996000
        # <startTime>4891228609440808000 <endTime>4891228612489617000

        online = [
            "antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'",
            "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'",
            "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'",
            "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'",
            "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"
        ]

        myinput = "antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'\n"\
                  "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'\n"\
                  "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'\n"\
                  "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'\n"\
                  "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"

        filename1 = 'flaghelperonline2.txt'
        create_input(myinput, filename1)

        # timeranges from online before padding, for comparison later
        timeranges = []
        for cmd in online:
            a, b = cmd.split(' ')
            b = b.lstrip('timerange=')
            timeranges.append(b.strip("'"))

        # Apply 2 values of tbuff to timeranges
        timebuffer = [0.4, 0.7]
        dlist1 = fh.readAndParse([filename1], tbuff=timebuffer)
        self.assertEqual(len(dlist1), 5)

        # check the padded time ranges before and after the application
        n = 0
        for cmd in dlist1:
            padt = cmd['timerange']

            #        padt = dlist1[0]['timerange']

            # Revert the tbuff application manually
            t0, t1 = padt.split('~', 1)
            startTime = qa.totime(t0)['value']
            startTimeSec = float((startTime * 24 * 3600) + timebuffer[0])
            startTimeSec = qa.quantity(startTimeSec, 's')
            paddedT0 = qa.time(startTimeSec, form='ymd', prec=9)[0]
            # end time
            endTime = qa.totime(t1)['value']
            endTimeSec = float((endTime * 24 * 3600) - timebuffer[1])
            endTimeSec = qa.quantity(endTimeSec, 's')
            paddedT1 = qa.time(endTimeSec, form='ymd', prec=9)[0]

            newtimerange = paddedT0 + '~' + paddedT1

            # Compare with the original
            self.assertEqual(timeranges[n], newtimerange)
            n += 1
예제 #19
0
def plotcomp(compdict, showplot=True, wantdict=False, symb=',',
             include0amp=False, include0bl=False, blunit='', bl0flux=0.0):

    """
    Given a dict including
    
    {'clist': component list,
     'objname': objname,
     'epoch': epoch,
     'shape': component shape dict, including direction.
     'freqs (GHz)': pl.array of frequencies,
     'antennalist': An array configuration file as used by simdata,
     'savedfig': False or, if specified, the filename to save the plot to,
     'standard': setjy fluxstandard type},

    and symb: One of matplotlib's codes for plot symbols: .:,o^v<>s+xDd234hH|_
          default: ',':  The smallest points I could find,

    make a plot of visibility amplitude vs. baseline length for clist at epoch.

    If antennalist is not found as is, it will look for antennalist in
    os.getenv('CASAPATH').split(' ')[0] + '/data/alma/simmos/'.
    
    showplot: Whether or not to show the plot on screen.

    If wantdict is True, it returns a dictionary with the amplitudes and
    baselines on success.  Otherwise, it returns True or False as its estimated
    success value.

    include0amp: Force the lower limit of the amplitude axis to 0.
    include0bl: Force the lower limit of the baseline length axis to 0.
    blunit: unit of the baseline length (='' used the unit in the data or klambda)
    bl0flux: Zero baseline flux
    """
    def failval():
        """
        Returns an appropriate failure value.
        Note that mydict.update(plotcomp(wantdict=True, ...)) would give a
        confusing error message if plotcomp returned False.
        """
        retval = False
        if wantdict:
            retval = {}
        return retval
    retval = failval()  # Default
    try:
        clist = compdict['clist']
        objname = compdict['objname']
        epoch = compdict['epoch']
        epstr = mepoch_to_str(epoch)
        antennalist = compdict['antennalist']

        # Read the configuration info.
        if not antennalist:
            print "compdict['antennalist'] must be set!"
            print "Try something in", os.getenv("CASAPATH").split(' ')[0] + "/data/alma/simmos/"
            return failval()
        # Try repodir if raw antennalist doesn't work.
        if not os.path.exists(antennalist):
            repodir = os.getenv("CASAPATH").split(' ')[0] + "/data/alma/simmos/"
            antennalist = repodir + antennalist

        su = simutil("")
        stnx, stny, stnz, diam, padnames, telescopename, obsmeas = su.readantenna(antennalist)
        #print "telescopename:", telescopename

        # Check that the source is up.
        myme = metool()
        posobs = myme.observatory(telescopename)
        #print "posobs:", posobs
        myme.doframe(epoch)
        myme.doframe(posobs)
        azel = myme.measure(compdict['shape']['direction'], 'azel')
        azeldegs = tuple([qa.convert(azel[m], 'deg')['value'] for m in ('m0', 'm1')])
        casalog.post("(az, el): (%.2f, %.2f) degrees" % azeldegs)
        # riseset blabs to the logger, so introduce it now.
        casalog.post('Rise and set times of ' + objname + " from " + telescopename + ':')
        approx = ''
        if 'JPL' in compdict.get('standard', 'JPL'):
            # The object is in the Solar System or not known to be extragalactic.
            approx = "APPROXIMATE.  The times do not account for the apparent motion of "\
                     + objname + "."
            casalog.post("  (" + approx + ")")
        riset = myme.riseset(compdict['shape']['direction'])
        msg = ''
        if riset['rise'] == 'above':
            msg = objname + " is circumpolar"
        elif riset['rise'] == 'below':
            msg = objname + ' is not visible from ' + telescopename
        if msg:
            if approx:
                msg += ' around ' + mepoch_to_str(epoch)
            casalog.post(msg)
        else:
            for t in riset:
                riset[t]['str'] = mepoch_to_str(riset[t]['utc'])
            casalog.post(objname + " rises at %s and sets at %s." % (riset['rise']['str'],
                                                                     riset['set']['str']))
            tmeridian=(riset['rise']['utc']['m0']['value']+riset['set']['utc']['m0']['value'])/2.
            casalog.post(objname + ': meridian passage at ' + qa.time(str(tmeridian)+'d')[0])

        if approx:
            riset['NOTE'] = approx
        if not azel['m1']['value'] > 0.0:
            casalog.post(objname + " is not visible from " + telescopename + " at " + epstr,
                         'SEVERE')
            if wantdict:
                return riset
            else:
                return False

        # Start a temp MS.
        workingdir = os.path.abspath(os.path.dirname(clist.rstrip('/')))
        tempms = tempfile.mkdtemp(prefix=objname, dir=workingdir)

        mysm = smtool()
        mysm.open(tempms)

        su.setcfg(mysm, telescopename, stnx, stny, stnz, diam,
                  padnames, posobs)

        #print "cfg set"

        # Only 1 polarization is wanted for now.
        stokes, feeds = su.polsettings(telescopename, 'RR')
        
        casalog.post("stokes, feeds: %s, %s" % (stokes, feeds))
        fband = su.bandname(compdict['freqs (GHz)'][0])
        chaninc = 1.0
        nchan = len(compdict['freqs (GHz)'])
        if nchan > 1:
            chaninc = (compdict['freqs (GHz)'][-1] - compdict['freqs (GHz)'][0]) / (nchan - 1)
        mysm.setspwindow(spwname=fband,
                         freq=str(compdict['freqs (GHz)'][0]) + 'GHz', 
                         deltafreq=str(chaninc) + 'GHz', 
                         freqresolution='1Hz', 
                         nchannels=nchan, refcode="LSRK",
                         stokes=stokes)
        mysm.setfeed(mode=feeds, pol=[''])
        mysm.setlimits(shadowlimit=0.01, elevationlimit='10deg')
        mysm.setauto(0.0)
        mysm.setfield(sourcename=objname,
                      sourcedirection=compdict['shape']['direction'],
                      calcode="OBJ", distance='0m')
        mysm.settimes(integrationtime="1s", usehourangle=False,
                      referencetime=epoch)
        
        # this only creates blank uv entries
        mysm.observe(sourcename=objname, spwname=fband,
                   starttime="-0.5s", stoptime="0.5s", project=objname)

        mysm.setdata(fieldid=[0])
        mysm.setvp()
        casalog.post("done setting up simulation parameters")

        mysm.predict(complist=clist)        # do actual calculation of visibilities:

        mysm.close()
        casalog.post("Simulation finished.")

        mytb = tbtool()
        mytb.open(tempms)
        data = mytb.getcol('DATA')[0]       # Again, only 1 polarization for now. 
        data = abs(data)
        baselines = mytb.getcol('UVW')[:2,:]  # Drop w.
        datablunit = mytb.getcolkeywords('UVW')['QuantumUnits']
        mytb.close()
        #print "Got the data and baselines"
        shutil.rmtree(tempms)

        if datablunit[1] != datablunit[0]:
            casalog.post('The baseline units are mismatched!: %s' % datablunit,
                         'SEVERE')
            return failval()
        datablunit = datablunit[0]
        # uv dist unit in klambda or m
        if datablunit == 'm' and blunit=='klambda':
            kl = qa.constants('C')['value']/(compdict['freqs (GHz)'][0]*1e6)
            blunit = 'k$\lambda$'
        else:
            blunit = datablunit
            kl = 1.0
        pl.ioff()
        #baselines = pl.hypot(baselines[0]/kl, baselines[1]/kl)
        baselines = pl.hypot(baselines[0], baselines[1])

        #if not showplot:
        #    casalog.post('Sorry, not showing the plot is not yet implemented',
        #                 'WARN')

        if showplot: 
          pl.ion()
        pl.clf()
        pl.ioff() 
        nfreqs = len(compdict['freqs (GHz)'])
        for freqnum in xrange(nfreqs):
            freq = compdict['freqs (GHz)'][freqnum]
            casalog.post("Plotting " + str(freq) + " GHz.")
            pl.plot(baselines/kl, data[freqnum], symb, label="%.3g GHz" % freq)
            #pl.plot(baselines, data[freqnum], symb, label="%.3g GHz" % freq)
        pl.xlabel("Baseline length (" + blunit + ")")
        pl.ylabel("Visibility amplitude (Jy)")
        if include0amp:
            pl.ylim(ymin=0.0)
        if include0bl:
            pl.xlim(xmin=0.0)
        pl.suptitle(objname + " (predicted by %s)" % compdict['standard'], fontsize=14)
        #pl.suptitle(objname + " (predicted)", fontsize=14)

        # Unlike compdict['antennalist'], antennalist might have had repodir
        # prefixed to it.
        pl.title('at ' + epstr + ' for ' + os.path.basename(compdict['antennalist']), fontsize=10)
        titletxt='($%.0f^\circ$ az, $%.0f^\circ$ el)' % azeldegs
        # for comparison of old and new models - omit azeldegs as all in az~0
        if bl0flux > 0.0:
            if len(compdict['freqs (GHz)']) == 1:
                titletxt+='\n bl0 flux:%.3f Jy' % bl0flux
            else:
                titletxt+='\n bl0 flux:%.3f Jy @ %s GHz' % (bl0flux, compdict['freqs (GHz)'][0]) 
        pl.legend(loc='best', title=titletxt)
        #pl.legend(loc='best', title='($%.0f^\circ$ az, $%.0f^\circ$ el)' % azeldegs)
        y_formatter=matplotlib.ticker.ScalarFormatter(useOffset=False)
        pl.axes().yaxis.set_major_formatter(y_formatter) 
        if showplot:
          pl.ion()
          pl.draw()
        if compdict.get('savedfig'):
            pl.savefig(compdict.get('savedfig'))
            casalog.post("Saved plot to " + str(compdict.get('savedfig')))

        if wantdict:
            retval = {'amps': data,
                      'antennalist': antennalist,  # Absolute path, now.
                      'azel': azel,
                      'baselines': baselines,
                      'blunit': blunit,
                      'riseset': riset,
                      'savedfig': compdict.get('savedfig')}
        else:
            retval = True
    except Exception, instance:
        casalog.post(str(instance), 'SEVERE')
        if os.path.isdir(tempms):
            shutil.rmtree(tempms)
예제 #20
0
    def test_readAndParseIrregularTbuff(self):
        '''flaghelper: compare the read and parse and apply of irregular tbuff'''
        print ''
        
        # MJD in seconds of timeranges are these
        # <startTime>4891227930515540000 <endTime>4891227932453838000
        # <startTime>4891228473545856000 <endTime>4891228473731891000
        # <startTime>4891226924455911000 <endTime>4891226927502314000
        # <startTime>4891228838164987000 <endTime>4891228838418996000
        # <startTime>4891228609440808000 <endTime>4891228612489617000

        online = ["antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'",
                  "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'",
                  "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'",
                  "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'",
                  "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"]

        myinput = "antenna='DV03&&*' timerange='2013/11/15/10:25:30.516~2013/11/15/10:25:32.454'\n"\
                  "antenna='DA44&&*' timerange='2013/11/15/10:34:33.546~2013/11/15/10:34:33.732'\n"\
                  "antenna='DA46&&*' timerange='2013/11/15/10:08:44.456~2013/11/15/10:08:47.502'\n"\
                  "antenna='DV09&&*' timerange='2013/11/15/10:18:11.798~2013/11/15/10:18:13.837'\n"\
                  "antenna='DV05&&*' timerange='2013/11/15/10:40:38.165~2013/11/15/10:40:38.419'"
        
        filename1 = 'flaghelperonline2.txt'
        create_input(myinput, filename1)
        
        # timeranges from online before padding, for comparison later
        timeranges=[]
        for cmd in online:
            a,b = cmd.split(' ')
            b = b.lstrip('timerange=')
            timeranges.append(b.strip("'"))
                    
        # Apply 2 values of tbuff to timeranges
        timebuffer = [0.4, 0.7]
        dlist1 = fh.readAndParse([filename1], tbuff=timebuffer)
        self.assertEqual(len(dlist1), 5)
        
        # check the padded time ranges before and after the application
        n = 0
        for cmd in dlist1:
            padt = cmd['timerange']
            
#        padt = dlist1[0]['timerange']
        
            # Revert the tbuff application manually
            t0,t1 = padt.split('~',1)
            startTime = qa.totime(t0)['value']
            startTimeSec = float((startTime * 24 * 3600) + timebuffer[0])
            startTimeSec = qa.quantity(startTimeSec, 's')
            paddedT0 = qa.time(startTimeSec,form='ymd',prec=9)[0]
            # end time
            endTime = qa.totime(t1)['value']
            endTimeSec = float((endTime * 24 * 3600) - timebuffer[1])
            endTimeSec = qa.quantity(endTimeSec, 's')
            paddedT1 = qa.time(endTimeSec,form='ymd',prec=9)[0]
            
            newtimerange =  paddedT0+'~'+paddedT1
            
            # Compare with the original
            self.assertEqual(timeranges[n], newtimerange)
            n += 1
예제 #21
0
def ephem_to_helio(vis=None,
                   ephem=None,
                   msinfo=None,
                   reftime=None,
                   polyfit=None,
                   usephacenter=False):
    '''1. Take a solar ms database, read the scan and field information, find out the pointings (in RA and DEC)
       2. Compare with the ephemeris of the solar disk center (in RA and DEC)
       3. Generate VLA pointings in heliocentric coordinates
       inputs:
           msinfo: CASA MS information, output from read_msinfo
           ephem: solar ephem, output from read_horizons
           reftime: list of reference times (e.g., used for imaging)
                    CASA standard time format, either a single time (e.g., '2012/03/03/12:00:00'
                    or a time range (e.g., '2012/03/03/12:00:00~2012/03/03/13:00:00'. If the latter,
                    take the midpoint of the timerange for reference. If no date specified, take
                    the date of the first scan
           polyfit: ONLY works for MS database with only one source with continously tracking; 
                    not recommanded unless scan length is too long and want to have very high accuracy
           usephacenter: Bool -- if True, correct for the RA and DEC in the ms file based on solar empheris. 
                                 Otherwise assume the phasecenter is correctly pointed to the solar disk center
                                 (EOVSA case)
     
       return value:
           helio: a list of VLA pointing information
                   reftimestr: reference time, in FITS format string
                   reftime: reference time, in mjd format
                   ra: actual RA of phasecenter in the ms file at the reference time (interpolated)
                   dec: actual DEC of phasecenter in the ms file at the reference time (interpolated)
                   # CASA uses only RA and DEC of the closest field (e.g. in clean) #
                   ra_fld: right ascention of the CASA reference pointing direction
                   dec_fld: declination of the CASA reference pointing direction
                   raoff: RA offset of the phasecenter in the ms file to solar center
                   decoff: DEC offset of the phasecenter in the ms file to solar center
                   refx: heliocentric X offset of the phasecenter in the ms file to solar center
                   refy: heliocentric Y offset of the phasecenter in the ms file to solar center
    ######## Example #########
         msfile='sun_C_20140910T221952-222952.10s.cal.ms'
         ephemfile='horizons_sun_20140910.radecp'
         ephem=vla_prep.read_horizons(ephemfile=ephemfile)
         msinfo=vla_prep.read_msinfo(msfile=msfile)
         polyfit=0
         reftime = '22:25:20~22:25:40'
    '''
    if not vis or not os.path.exists(vis):
        raise ValueError, 'Please provide information of the MS database!'
    if not ephem:
        ephem = read_horizons(vis)
    if not msinfo:
        msinfo0 = read_msinfo(vis)
    else:
        if isinstance(msinfo, str):
            try:
                msinfo0 = np.load(msinfo)
            except:
                raise ValueError, 'The specified input msinfo file does not exist!'
        elif isinstance(msinfo, dict):
            msinfo0 = msinfo
        else:
            raise ValueError, 'msinfo should be either a numpy npz or a dictionary'
    print 'msinfo is derived from: ', msinfo0['vis']
    scans = msinfo0['scans']
    fieldids = msinfo0['fieldids']
    btimes = msinfo0['btimes']
    inttimes = msinfo0['inttimes']
    ras = msinfo0['ras']
    decs = msinfo0['decs']
    ra_rads = [ra['value'] for ra in ras]
    dec_rads = [dec['value'] for dec in decs]
    # fit 2nd order polynomial fits to the RAs and DECs #
    if polyfit:
        cra = np.polyfit(btimes, ra_rads, 2)
        cdec = np.polyfit(btimes, dec_rads, 2)

    # find out phase center infomation in ms according to the input time or timerange #
    if not reftime:
        raise ValueError, 'Please specify a reference time of the image'
    if type(reftime) == str:
        reftime = [reftime]
    if (not isinstance(reftime, list)):
        print 'input "reftime" is not a valid list. Abort...'

    nreftime = len(reftime)
    helio = []
    for reftime0 in reftime:
        helio0 = dict.fromkeys(['reftimestr', 'reftime', \
                                'ra', 'dec', 'ra_fld', 'dec_fld', \
                                'raoff', 'decoff', 'refx', 'refy', 'p0'])
        helio0['reftimestr'] = reftime0
        if '~' in reftime0:
            # if reftime0 is specified as a timerange
            [tbg0, tend0] = reftime0.split('~')
            tbg_d = qa.getvalue(qa.convert(qa.totime(tbg0), 'd'))[0]
            tend_d = qa.getvalue(qa.convert(qa.totime(tend0), 'd'))[0]
            tdur_s = (tend_d - tbg_d) * 3600. * 24.
            # if no date is specified, add up the date of the first scan
            if tend_d < 1.:
                if tend_d >= tbg_d:
                    tend_d += int(btimes[0])
                else:
                    tend_d += int(btimes[0]) + 1
            if tbg_d < 1.:
                tbg_d += int(btimes[0])
            tref_d = (tbg_d + tend_d) / 2.
        else:
            # if reftime0 is specified as a single value
            tref_d = qa.getvalue(qa.convert(qa.totime(reftime0), 'd'))
            # if no date is specified, add up the date of the first scan
            if tref_d < 1.:
                tref_d += int(btimes[0])
            tbg_d = tref_d
            # use the intergration time
            ind = bisect.bisect_left(btimes, tref_d)
            tdur_s = inttims[ind - 1]
        helio0['reftime'] = tref_d
        helio0['date-obs'] = qa.time(qa.quantity(tbg_d, 'd'),
                                     form='fits',
                                     prec=10)[0]
        helio0['exptime'] = tdur_s

        # find out phase center RA and DEC in the measurement set according to the reference time
        # if polyfit, then use the 2nd order polynomial coeffs
        ind = bisect.bisect_left(btimes, tref_d)
        if ind > 1:
            dt = tref_d - btimes[ind - 1]
            if ind < len(btimes):
                scanlen = btimes[ind] - btimes[ind - 1]
                (ra_b, ra_e) = (ras[ind - 1]['value'], ras[ind]['value'])
                (dec_b, dec_e) = (decs[ind - 1]['value'], decs[ind]['value'])
            if ind >= len(btimes):
                scanlen = btimes[ind - 1] - btimes[ind - 2]
                (ra_b, ra_e) = (ras[ind - 2]['value'], ras[ind - 1]['value'])
                (dec_b, dec_e) = (decs[ind - 2]['value'],
                                  decs[ind - 1]['value'])
        if ind == 1:  # only one scan exists (e.g., imported from AIPS)
            ra_b = ras[ind - 1]['value']
            ra_e = ra_b
            dec_b = decs[ind - 1]['value']
            dec_e = dec_b
            scanlen = 10.  # radom value
            dt = 0.
        if ind < 1:
            raise ValueError, 'Reference time does not fall into the scan list!'
        if polyfit:
            ra = cra[0] * tref_d**2. + cra[1] * tref_d + cra[2]
            dec = cdec[0] * tref_d**2. + cdec[1] * tref_d + cdec[2]
        # if not, use linearly interpolated RA and DEC at the beginning of this scan and next scan
        else:
            ra = ra_b + (ra_e - ra_b) / scanlen * dt
            dec = dec_b + (dec_e - dec_b) / scanlen * dt
        if ra < 0:
            ra += 2. * np.pi
        if ra_b < 0:
            ra_b += 2. * np.pi

        # compare with ephemeris from JPL Horizons
        time0 = ephem['time']
        ra0 = ephem['ra']
        dec0 = ephem['dec']
        p0 = ephem['p0']
        delta0 = ephem['delta']
        ind = bisect.bisect_left(time0, tref_d)
        dt0 = time0[ind] - time0[ind - 1]
        dt_ref = tref_d - time0[ind - 1]
        dra0 = ra0[ind] - ra0[ind - 1]
        ddec0 = dec0[ind] - dec0[ind - 1]
        dp0 = p0[ind] - p0[ind - 1]
        ddelta0 = delta0[ind] - delta0[ind - 1]
        ra0 = ra0[ind - 1] + dra0 / dt0 * dt_ref
        dec0 = dec0[ind - 1] + ddec0 / dt0 * dt_ref
        p0 = p0[ind - 1] + dp0 / dt0 * dt_ref
        delta0 = delta0[ind - 1] + ddelta0 / dt0 * dt_ref
        if ra0 < 0:
            ra0 += 2. * np.pi

        # RA and DEC offset in arcseconds
        decoff = degrees((dec - dec0)) * 3600.
        raoff = degrees((ra - ra0) * cos(dec)) * 3600.
        # Convert into heliocentric offsets
        prad = -radians(p0)
        refx = (-raoff) * cos(prad) - decoff * sin(prad)
        refy = (-raoff) * sin(prad) + decoff * cos(prad)
        helio0['ra'] = ra  # ra of the actual pointing
        helio0['dec'] = dec  # dec of the actual pointing
        helio0[
            'ra_fld'] = ra_b  # ra of the field, used as the reference in e.g., clean
        helio0[
            'dec_fld'] = dec_b  # dec of the field, used as the refenrence in e.g., clean
        helio0['raoff'] = raoff
        helio0['decoff'] = decoff
        if usephacenter:
            helio0['refx'] = refx
            helio0['refy'] = refy
        else:
            helio0['refx'] = 0.
            helio0['refy'] = 0.
        helio0['p0'] = p0
        # helio['r_sun']=np.degrees(R_sun.value/(au.value*delta0))*3600. #in arcsecs
        helio.append(helio0)
    return helio
예제 #22
0
def ptclean3(vis, imageprefix, imagesuffix, ncpu, twidth, doreg, usephacenter,
             reftime, toTb, overwrite, selectdata, field, spw, timerange,
             uvrange, antenna, scan, observation, intent, datacolumn, imsize,
             cell, phasecenter, stokes, projection, startmodel, specmode,
             reffreq, nchan, start, width, outframe, veltype, restfreq,
             interpolation, gridder, facets, chanchunks, wprojplanes, vptable,
             usepointing, mosweight, aterm, psterm, wbawp, conjbeams, cfcache,
             computepastep, rotatepastep, pblimit, normtype, deconvolver,
             scales, nterms, smallscalebias, restoration, restoringbeam, pbcor,
             outlierfile, weighting, robust, npixels, uvtaper, niter, gain,
             threshold, nsigma, cycleniter, cyclefactor, minpsffraction,
             maxpsffraction, interactive, usemask, mask, pbmask,
             sidelobethreshold, noisethreshold, lownoisethreshold,
             negativethreshold, smoothfactor, minbeamfrac, cutthreshold,
             growiterations, dogrowprune, minpercentchange, verbose, restart,
             savemodel, calcres, calcpsf, parallel, subregion):
    if not (type(ncpu) is int):
        casalog.post('ncpu should be an integer')
        ncpu = 8

    if doreg:
        # check if ephem and msinfo exist. If not, generate one on the fly
        try:
            ephem = hf.read_horizons(vis=vis)
        except ValueError:
            print("error in obtaining ephemeris")
        try:
            msinfo = hf.read_msinfo(vis)
        except ValueError:
            print("error in getting ms info")
    else:
        ephem = None
        msinfo = None

    if imageprefix:
        workdir = os.path.dirname(imageprefix)
    else:
        workdir = './'
    tmpdir = workdir + '/tmp/'
    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)
    # get number of time pixels
    ms.open(vis)
    ms.selectinit()
    timfreq = ms.getdata(['time', 'axis_info'], ifraxis=True)
    tim = timfreq['time']
    ms.close()

    if twidth < 1:
        casalog.post('twidth less than 1. Change to 1')
        twidth = 1

    if twidth > len(tim):
        casalog.post(
            'twidth greater than # of time pixels in the dataset. Change to the timerange of the entire dateset'
        )
        twidth = len(tim)
    # find out the start and end time index according to the parameter timerange
    # if not defined (empty string), use start and end from the entire time of the ms
    if not timerange:
        btidx = 0
        etidx = len(tim) - 1
    else:
        try:
            (tstart, tend) = timerange.split('~')
            bt_s = qa.convert(qa.quantity(tstart, 's'), 's')['value']
            et_s = qa.convert(qa.quantity(tend, 's'), 's')['value']
            # only time is given but not date, add the date (at 0 UT) from the first record
            if bt_s < 86400. or et_s < 86400.:
                bt_s += np.fix(
                    qa.convert(qa.quantity(tim[0], 's'),
                               'd')['value']) * 86400.
                et_s += np.fix(
                    qa.convert(qa.quantity(tim[0], 's'),
                               'd')['value']) * 86400.
            btidx = np.argmin(np.abs(tim - bt_s))
            etidx = np.argmin(np.abs(tim - et_s))
            # make the indice back to those bracket by the timerange
            if tim[btidx] < bt_s:
                btidx += 1
            if tim[etidx] > et_s:
                etidx -= 1
            if etidx <= btidx:
                print("ending time must be greater than starting time")
                print("reinitiating to the entire time range")
                btidx = 0
                etidx = len(tim) - 1
        except ValueError:
            print("keyword 'timerange' has a wrong format")

    btstr = qa.time(qa.quantity(tim[btidx], 's'), prec=9, form='fits')[0]
    etstr = qa.time(qa.quantity(tim[etidx], 's'), prec=9, form='fits')[0]

    iterable = range(btidx, etidx + 1, twidth)
    print('First time pixel: ' + btstr)
    print('Last time pixel: ' + etstr)
    print(str(len(iterable)) + ' images to clean...')

    res = []
    # partition
    clnpart = partial(
        clean_iter, tim, vis, imageprefix, imagesuffix, twidth, doreg,
        usephacenter, reftime, ephem, msinfo, toTb, overwrite, selectdata,
        field, spw, uvrange, antenna, scan, observation, intent, datacolumn,
        imsize, cell, phasecenter, stokes, projection, startmodel, specmode,
        reffreq, nchan, start, width, outframe, veltype, restfreq,
        interpolation, gridder, facets, chanchunks, wprojplanes, vptable,
        usepointing, mosweight, aterm, psterm, wbawp, conjbeams, cfcache,
        computepastep, rotatepastep, pblimit, normtype, deconvolver, scales,
        nterms, smallscalebias, restoration, restoringbeam, pbcor, outlierfile,
        weighting, robust, npixels, uvtaper, niter, gain, threshold, nsigma,
        cycleniter, cyclefactor, minpsffraction, maxpsffraction, interactive,
        usemask, mask, pbmask, sidelobethreshold, noisethreshold,
        lownoisethreshold, negativethreshold, smoothfactor, minbeamfrac,
        cutthreshold, growiterations, dogrowprune, minpercentchange, verbose,
        restart, savemodel, calcres, calcpsf, parallel, subregion, tmpdir)
    timelapse = 0
    t0 = time()
    # parallelization
    if ncpu > 1:
        import multiprocessing as mprocs
        casalog.post('Perform clean in parallel ...')
        print('Perform clean in parallel ...')
        pool = mprocs.Pool(ncpu)
        res = pool.map(clnpart, iterable)
        pool.close()
        pool.join()
    else:
        casalog.post('Perform clean in single process ...')
        print('Perform clean in single process ...')
        for i in iterable:
            res.append(clnpart(i))

    t1 = time()
    timelapse = t1 - t0
    print('It took %f secs to complete' % timelapse)
    # repackage this into a single dictionary
    results = {
        'Succeeded': [],
        'BeginTime': [],
        'EndTime': [],
        'ImageName': []
    }
    for r in res:
        results['Succeeded'].append(r[0])
        results['BeginTime'].append(r[1])
        results['EndTime'].append(r[2])
        results['ImageName'].append(r[3])

    if os.path.exists(tmpdir):
        os.system('rm -rf ' + tmpdir)

    return results
예제 #23
0
def pltvs(vis='',
          outputvis='',
          plttimerange='',
          pltspw1='',
          pltspw2='',
          pltspw3='',
          timoffset=4,
          windowlen=10,
          windowtype='hamming',
          pol='LL',
          bl='19&22'):
    ms.open(vis, nomodify=True)
    """ This function can do two steps: (1) plot the uv amplitude vs. time 
		on three selected channel and compare the original and smoothed signals.
		You can select appropriate window length and type, and the offset time
		to match the peaks and valleys for the three channel curves. (2) Confirm
		the use of specified window length, type and offset time and smooth the
		data channel by channel. The background-subtracted measurement set is 
		then generated.
    """
    timfreq = ms.getdata(['time', 'axis_info'], ifraxis=True)
    tim = timfreq['time']
    timerange = str(qa.time(qa.quantity(tim[0], 's'), prec=8)[0]) + '~' + str(
        qa.time(qa.quantity(tim[-1], 's'), prec=8)[0])
    # check plotting timerange
    if plttimerange and (type(plttimerange) == str):
        print 'plotting the specified timerange: ', plttimerange
    else:
        plttimerange = timerange
        print 'plotting the entire timerange: ', plttimerange

    if pltspw1 and (type(pltspw1) == str):
        print 'Using the specified channel 1:', pltspw1
    else:
        pltspw1 = '0:400'

    if pltspw2 and (type(pltspw2) == str):
        print 'Using the specified channel 2:', pltspw2
    else:
        pltspw2 = '0:500'

    if pltspw3 and (type(pltspw3) == str):
        print 'Using the specified channel 3:', pltspw3
    else:
        pltspw3 = '0:600'

    [spwid1, chan1] = pltspw1.split(':')
    [spwid2, chan2] = pltspw2.split(':')
    [spwid3, chan3] = pltspw3.split(':')
    chanid = [chan1, chan2, chan3]

    if not (spwid1 == spwid2 and spwid1 == spwid3):
        print 'Please use the same spectral window'
        exit()

    if not (timoffset and (type(timoffset) == int)):
        timoffset = int(4)
    # initialize the loop-out status
    status = 'n'
    while True:
        timoffset = int(
            raw_input("Please specify the offset after smoothing:"))
        windowlen = int(
            raw_input("Please specify window width for smoothing:"))
        windowtype = str(
            raw_input(
                "Please specify window type for smoothing: (e.g. 'flat', 'hanning', 'hamming', 'bartlett', 'blackman')"
            ))
        pol = str(raw_input("Please specify polarization: (e.g. RR/LL)"))
        bl = str(raw_input("Please specify baseline: (e.g. '19&22')"))
        j = 331
        for i in range(len(chanid)):
            ms.selectinit(datadescid=int(spwid1))
            if bl and (type(bl) == str):
                ms.msselect({'baseline': bl})
            if timerange and (type(timerange) == str):
                ms.msselect({'time': timerange})
            if chanid[i] and (type(chanid[i]) == str):
                ms.selectchannel(1, int(chanid[i]), 1, 1)
            specdata = ms.getdata(['data', 'time', 'axis_info'], ifraxis=True)
            if pol == 'RR':
                spec = specdata['data'][0, 0, 0, :]
            if pol == 'LL':
                spec = specdata['data'][1, 0, 0, :]

            ms.selectinit(datadescid=int(spwid1))
            if bl and (type(bl) == str):
                ms.msselect({'baseline': bl})
            if plttimerange and (type(plttimerange) == str):
                ms.msselect({'time': plttimerange})
            if chanid[i] and (type(chanid[i]) == str):
                ms.selectchannel(1, int(chanid[i]), 1, 1)
            specdata_plt = ms.getdata(['data', 'time', 'axis_info'],
                                      ifraxis=True)
            if pol == 'RR':
                spec_plt = specdata_plt['data'][0, 0, 0, :]
            if pol == 'LL':
                spec_plt = specdata_plt['data'][1, 0, 0, :]

            spec_plt_smooth = task_smooth.smooth(spec_plt, timoffset,
                                                 windowlen, windowtype)
            spec_smooth = task_smooth.smooth(spec, timoffset, windowlen,
                                             windowtype)
            spec_plt_amp = np.absolute(spec_plt)
            spec_plt_smooth_amp = np.absolute(spec_plt_smooth)
            #spec_plt_amp=sqrt(spec_plt.real**2+spec_plt.imag**2)
            #spec_plt_smooth_amp=sqrt(spec_plt_smooth.real**2+spec_plt_smooth.imag**2)
            #print len(spec)
            #print len(spec_smooth)
            #print type(spec)
            #print spec[0]
            sp1 = fft(spec)
            sp2 = fft(spec_smooth)
            sp3 = sp1 - sp2

            freq1 = fftfreq(len(sp1), d=0.001)
            freq2 = fftfreq(len(sp2), d=0.001)
            freq3 = fftfreq(len(sp3), d=0.001)
            freq1_index = np.argsort(freq1)
            freq2_index = np.argsort(freq2)
            freq3_index = np.argsort(freq3)
            #print min(freq1),max(freq1)

            subplot(j)
            plot(spec_plt_amp)
            plot(spec_plt_smooth_amp)
            title("Signal vs Time")
            j = j + 1
            subplot(j)
            plot(freq1[freq1_index], log10(sp1[freq1_index]))
            plot(freq2[freq2_index], log10(sp2[freq2_index]))
            ylim([0, 6])
            title("FFT signal vs Frequency")
            j = j + 1
            subplot(j)
            #plot(subspec)
            plot(freq3[freq3_index], log10(sp3[freq3_index]))
            ylim([0, 6])
            title("FFT smoothed signal vs Frequency")
            j = j + 1
            #print "number of original data points: ",len(spec)
            #print "number of smoothed data points: ",len(spec_smooth)

        status = str(
            raw_input("Confirm to use current parameters? (y/n/abort) "))
        if status == 'y':
            flag1 = str(
                raw_input("Smooth all the channels and time range? (y/n) "))
            if flag1 == 'y':
                smtimerange = ''
                smspw = ''
                splitsel = False
            else:
                print 'confirm using window width: ', windowlen
                print 'confirm using window type: ', windowtype
                smtimerange = str(
                    raw_input(
                        "Please specify the time range for smoothing (HH:MM:SS) :"
                    ))
                smspw = str(
                    raw_input(
                        "Please specify spectral window and channel (e.g. 0:0~1032) :"
                    ))
                splitsel = True
            break
        elif status == 'abort':
            print 'Abort background subtraction.'
            sys.exit()

    if not outputvis:
        outputvis = str(timoffset) + '_' + str(windowlen) + '_' + str(
            windowtype) + '.ms'
        print "Generating output: ", outputvis

    ms.close()
    result1 = subvs(vis, outputvis, smtimerange, smspw, timoffset, windowlen,
                    windowtype, '', '', '')