Esempio n. 1
0
def do_mstransform(infile, outdir, min_freq, max_freq, bottom_edge, width_freq=15.625):
    """
    Perform the MS_TRANSFORM step

    :param infile:
    :param outdir:
    :param min_freq:
    :param max_freq:
    :param bottom_edge:
    :param width_freq:
    :return:
    """
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    spw_range = freq_map(min_freq, max_freq, bottom_edge)
    LOG.info('spw_range: {0}'.format(spw_range))
    if spw_range.startswith('-1') or spw_range.endswith('-1'):
        LOG.info('The spw_range is {0} which is outside the spectral window '.format(spw_range))
    else:
        step_freq = max_freq - min_freq
        no_chan = int(step_freq * 1000.0 / width_freq)  # MHz/kHz!!

        outfile = os.path.join(outdir, 'vis_{0}~{1}'.format(min_freq, max_freq))
        LOG.info('working on: {0}'.format(outfile))
        if os.path.exists(outfile):
            shutil.rmtree(outfile)
        try:
            # dump_all()
            mstransform(
                vis=infile,
                outputvis=outfile,
                regridms=True,
                restfreq='1420.405752MHz',
                mode='frequency',
                nchan=no_chan,
                outframe='lsrk',
                interpolation='linear',
                veltype='radio',
                start='{0}MHz'.format(min_freq),
                width='{0}kHz'.format(width_freq),
                spw=spw_range,
                combinespws=True,
                nspw=1,
                createmms=False,
                datacolumn="data"
            )

        except Exception:
            LOG.exception('*********\nmstransform exception:\n***********')
Esempio n. 2
0
def do_cvel(infile, outdir, backup_dir, min_freq, max_freq, step_freq, width_freq, spec_window, obsId):
    """
    Adapted from loop.split.py with changes
    (1) deal with (max_freq - min_freq) % step_freq > 0
    (2) some debug info
    (3) obsId parameter for marking split_done

    If spec_window is blank ('') then freq_map is called to define the spw selection
    """
    if not os.path.exists(outdir):
        os.system('mkdir ' + outdir)
    if not os.path.exists(backup_dir):
        os.system('mkdir ' + backup_dir)

    steps = (max_freq - min_freq) / step_freq
    rem = (max_freq - min_freq) % step_freq
    if rem:
        steps += 1
    freq1 = min_freq
    freq2 = min_freq + step_freq
    bottom_edge = re.search('_[0-9]{3}_',infile)
    if bottom_edge:
        bedge = bottom_edge.group(0)
        bedge = int(bedge[1:4])

    if not sel_freq:
        steps = 1

    for i in range(steps):
        if sel_freq:
            #if (rem and (i == steps - 1)):
            #    freq_range = '%d~%d' % (min_freq + i * step_freq, max_freq)
            #else:
            #    freq_range = str(freq1) + '~' + str(freq2)
            if rem and i == steps - 1:
                freq_range = '%d~%d' % (min_freq + i * step_freq, max_freq)
                cvel_freq_range = '%f~%f' % (min_freq - 2 + i * step_freq, max_freq + 2)
            else:
                freq_range = str(freq1) + '~' + str(freq2)
                cvel_freq_range =  str(int(freq1-2)) + '~' + str(int(freq2+2))
            spw_range = spec_window + ':' + freq_range + 'MHz'
            cvel_spw_range = spec_window + ':' + cvel_freq_range + 'MHz'
            # spanning spectral windows and selecting freq fails
            # so use freq_map
            # THEREFORE ~10 lines above are IGNORED!!
            cvel_spw_range = freq_map(freq1, freq2, bedge)
        else:
            freq_range = 'min~max'
            spw_range = spec_window

        # If no spw is given then calculate from the max and min range
        if spec_window == '':
            spw_range = freq_map(freq1, freq2)

        no_chan = int(step_freq * 1000.0 / width_freq)  # MHz/kHz!!
        # I think I always want this with cvel.

        outfile = outdir + 'vis_' + freq_range
        backupfile = backup_dir + 'vis_' + freq_range
        if not debug:
            if os.path.exists(outfile):
                shutil.rmtree(outfile)
            if os.path.exists(backupfile):
                shutil.rmtree(backupfile)
            print 'working on: ' + outfile
            try:
                # dump_all()
                mstransform(
                    vis=infile,
                    outputvis=outfile,
                    regridms=True,
                    restfreq='1420.405752MHz',
                    mode='frequency',
                    nchan=no_chan,
                    outframe='lsrk',
                    interpolation='linear',
                    veltype='radio',
                    start=str(freq1) + 'MHz',
                    width=str(width_freq) + 'kHz',
                    spw=cvel_spw_range,
                    combinespws=True,
                    nspw=1,
                    createmms=False,
                    datacolumn="data")

                # cvel(vis=infile,
                # outputvis=outfile,
                #       restfreq='1420.405752MHz',
                #       mode='frequency',
                #       nchan=no_chan,
                #       outframe='lsrk',
                #       interpolation='linear',
                #       veltype='radio',
                #       start=str(freq1)+'MHz',
                #       width=str(width_freq)+'kHz',
                #       spw=spw_range)
            except Exception, spEx:
                print '*********\nSplit exception: %s\n***********' % str(spEx)
        else:
            msg = '''
mstransform(vis=%s,
outputvis=%s,
start=%s,
width=%s,
spw=%s,
nchan=%d)'''.format(infile, outfile, str(freq1)+'MHz', width_freq, cvel_spw_range, no_chan)
            print msg

        freq1 = freq1 + step_freq
        freq2 = freq2 + step_freq
Esempio n. 3
0
def extract_continuum(in_file=None,
                      out_file=None,
                      lines_to_flag=None,
                      gal='',
                      spw='',
                      vsys=0.0,
                      vwidth=500.,
                      do_statwt=True,
                      do_collapse=True,
                      quiet=False,
                      collapse_width=10000):
    """
    Extract a continuum measurement set, flagging any specified lines,
    reweighting using statwt, and then binning the continuum channels. 
    """

    sol_kms = 2.99e5

    # Set up the input file

    if os.path.isdir(in_file) == False:
        if quiet == False:
            print("Input file not found: " + in_file)
        return

    # pull the parameters from the galaxy in the mosaic file

#     if gal != None:
#       mosaic_parms = read_mosaic_key()
#       if mosaic_parms.has_key(gal):
#           vsys = mosaic_parms[gal]['vsys']
#           vwidth = mosaic_parms[gal]['vwidth']
#
# set the list of lines to flag

    if lines_to_flag == None:
        lines_to_flag = line_list.lines_co + line_list.lines_13co + line_list.lines_c18o + line_list.lines_cn

    # Make a continuum copy of the science data

    os.system('rm -rf ' + out_file)
    os.system('rm -rf ' + out_file + '.flagversions')

    #    command = 'cp -r -H '+in_file+' '+out_file
    #    print command
    #    var = os.system(command)
    #    print var
    #
    split(vis=in_file,
          outputvis=out_file,
          datacolumn='DATA',
          keepflags=False,
          field=gal,
          spw=spw)

    os.system('rm -rf ' + out_file + '.temp_copy')

    command = 'mv ' + out_file + ' ' + out_file + '.temp_copy'
    print command
    var = os.system(command)

    mstransform(vis=out_file + '.temp_copy',
                outputvis=out_file,
                outframe='Bary',
                datacolumn='DATA')

    # Figure out the line channels and flag them

    vm = au.ValueMapping(out_file)

    spw_flagging_string = ''
    first = True
    for spw in vm.spwInfo.keys():
        this_spw_string = str(spw) + ':0'
        if first:
            spw_flagging_string += this_spw_string
            first = False
        else:
            spw_flagging_string += ',' + this_spw_string

    for line in lines_to_flag:
        rest_linefreq_ghz = line_list.line_list[line]

        shifted_linefreq_hz = rest_linefreq_ghz * (1. - vsys / sol_kms) * 1e9
        hi_linefreq_hz = rest_linefreq_ghz * (
            1. - (vsys - vwidth / 2.0) / sol_kms) * 1e9
        lo_linefreq_hz = rest_linefreq_ghz * (
            1. - (vsys + vwidth / 2.0) / sol_kms) * 1e9

        spw_list = au.getScienceSpwsForFrequency(out_file, shifted_linefreq_hz)
        if spw_list == []:
            continue

        print "Found overlap for " + line
        for this_spw in spw_list:
            freq_ra = vm.spwInfo[this_spw]['chanFreqs']
            chan_ra = np.arange(len(freq_ra))
            to_flag = (freq_ra >= lo_linefreq_hz) * (freq_ra <= hi_linefreq_hz)
            to_flag[np.argmin(np.abs(freq_ra - shifted_linefreq_hz))]
            low_chan = np.min(chan_ra[to_flag])
            hi_chan = np.max(chan_ra[to_flag])
            this_spw_string = str(this_spw) + ':' + str(low_chan) + '~' + str(
                hi_chan)
            if first:
                spw_flagging_string += this_spw_string
                first = False
            else:
                spw_flagging_string += ',' + this_spw_string

    print "... proposed flagging " + spw_flagging_string

    if spw_flagging_string != '':
        flagdata(
            vis=out_file,
            spw=spw_flagging_string,
        )

    # Here - this comman needs to be examined and refined in CASA
    # 5.6.1 to see if it can be sped up. Right now things are
    # devastatingly slow.
    if do_statwt:
        print "... deriving empirical weights using STATWT."
        statwt(
            vis=out_file,
            timebin='0.001s',
            slidetimebin=False,
            chanbin='spw',
            statalg='classic',
            datacolumn='DATA',
        )

    if do_collapse:
        print "... Binning the continuum channels"

        os.system('rm -rf ' + out_file + '.temp_copy')
        os.system('rm -rf ' + out_file + '.temp_copy.flagversions')

        command = 'mv ' + out_file + ' ' + out_file + '.temp_copy'
        print command
        var = os.system(command)
        print var

        command = 'mv ' + out_file + '.flagversions ' + out_file + '.temp_copy.flagversions'
        print command
        var = os.system(command)
        print var

        split(vis=out_file + '.temp_copy',
              outputvis=out_file,
              width=collapse_width,
              datacolumn='DATA',
              keepflags=False)

        os.system('rm -rf ' + out_file + '.temp_copy')
        os.system('rm -rf ' + out_file + '.temp_copy.flagversions')

    os.system('rm -rf ' + out_file + '.temp_copy')
    os.system('rm -rf ' + out_file + '.flagversions')

    return
Esempio n. 4
0
timestr = dt.strftime('%Y-%m-%dT%H-%M-%S')

ms_dir = '/mnt/Data/data1/20131122_941_6_FINAL_PRODUCTS/13B-266.sb27261805.eb28549602.56618.334173599535_calibrated_deepfield.ms'

output_vis = '/mnt/output/Chiles/split_vis/{0}'.format(timestr)

os.makedirs(output_vis)
gap = 4

start_time = datetime.datetime.now()
for i in range(1):
    mstransform(vis=ms_dir,
                outputvis='{0}/vis_{1}~{2}'.format(output_vis, 1020 + i * gap, 1024 + i * gap),
                start='{0}MHz'.format(1020 + i * gap),
                width='15.625kHz',
                spw='2~2',
                nchan=256,
                regridms=True,
                restfreq='1420.405752MHz',
                mode='frequency',
                outframe='lsrk',
                interpolation='linear',
                veltype='radio',
                combinespws=True,
                nspw=1,
                createmms=False,
                datacolumn="data")

end_time = datetime.datetime.now()
print 'Time taken:', end_time, start_time, end_time - start_time
Esempio n. 5
0
ms_dir = '/mnt/Data/data1/20131122_941_6_FINAL_PRODUCTS/13B-266.sb27261805.eb28549602.56618.334173599535_calibrated_deepfield.ms'

output_vis = '/mnt/output/Chiles/split_vis/{0}'.format(timestr)

os.makedirs(output_vis)
gap = 4

start_time = datetime.datetime.now()
for i in range(1):
    mstransform(vis=ms_dir,
                outputvis='{0}/vis_{1}~{2}'.format(output_vis, 1020 + i * gap,
                                                   1024 + i * gap),
                start='{0}MHz'.format(1020 + i * gap),
                width='15.625kHz',
                spw='2~2',
                nchan=256,
                regridms=True,
                restfreq='1420.405752MHz',
                mode='frequency',
                outframe='lsrk',
                interpolation='linear',
                veltype='radio',
                combinespws=True,
                nspw=1,
                createmms=False,
                datacolumn="data")

end_time = datetime.datetime.now()
print 'Time taken:', end_time, start_time, end_time - start_time
Esempio n. 6
0
                        nu_0=nu_0,
                        nterms_angle=1,
                        nterms_frac=1,
                        nu_min=min_freqs,
                        nu_max=max_freqs)

    fluxtable = polcal_obj.setModelFluxScale(pol_source_object=p_source2,
                                             field=leakagefield,
                                             gaintable=complexgaintable,
                                             referencefield=polanglefield,
                                             transferfield=leakagefield,
                                             nu_0=nu_0)

    kcrosstable = polcal_obj.solveCrossHandDelays(refantmode="strict",
                                                  channels="13~115")

    leakagetable = polcal_obj.calibrateLeakage(solint='inf,0.5MHz', minsnr=3.0)
    polcal_obj.plotLeakage(plotdir="./")

    polangletable = polcal_obj.calibratePolAngle(solint='inf,0.5MHz',
                                                 minsnr=3.0)

    polcal_obj.applySolutions()
    polcal_obj.finalPlots()
    mstransform(vis=inputvis,
                outputvis='A1314.ms',
                datacolumn='corrected',
                field=target)
    #mstransform(vis=inputvis, outputvis='A1314_statwt.ms', datacolumn='corrected', field=target)
    #statwt(vis='A1314_statwt.ms', datacolumn='data', timebin='0.001s')
Esempio n. 7
0
def do_cvel(infile, outdir, backup_dir, min_freq, max_freq, step_freq,
            width_freq, spec_window, obsId):
    """
    Adapted from loop.split.py with changes
    (1) deal with (max_freq - min_freq) % step_freq > 0
    (2) some debug info
    (3) obsId parameter for marking split_done

    If spec_window is blank ('') then freq_map is called to define the spw selection
    """
    if not os.path.exists(outdir):
        os.system('mkdir ' + outdir)
    if not os.path.exists(backup_dir):
        os.system('mkdir ' + backup_dir)

    steps = (max_freq - min_freq) / step_freq
    rem = (max_freq - min_freq) % step_freq
    if rem:
        steps += 1
    freq1 = min_freq
    freq2 = min_freq + step_freq
    bottom_edge = re.search('_[0-9]{3}_', infile)
    if bottom_edge:
        bedge = bottom_edge.group(0)
        bedge = int(bedge[1:4])

    if not sel_freq:
        steps = 1

    for i in range(steps):
        if sel_freq:
            #if (rem and (i == steps - 1)):
            #    freq_range = '%d~%d' % (min_freq + i * step_freq, max_freq)
            #else:
            #    freq_range = str(freq1) + '~' + str(freq2)
            if rem and i == steps - 1:
                freq_range = '%d~%d' % (min_freq + i * step_freq, max_freq)
                cvel_freq_range = '%f~%f' % (min_freq - 2 + i * step_freq,
                                             max_freq + 2)
            else:
                freq_range = str(freq1) + '~' + str(freq2)
                cvel_freq_range = str(int(freq1 - 2)) + '~' + str(
                    int(freq2 + 2))
            spw_range = spec_window + ':' + freq_range + 'MHz'
            cvel_spw_range = spec_window + ':' + cvel_freq_range + 'MHz'
            # spanning spectral windows and selecting freq fails
            # so use freq_map
            # THEREFORE ~10 lines above are IGNORED!!
            cvel_spw_range = freq_map(freq1, freq2, bedge)
        else:
            freq_range = 'min~max'
            spw_range = spec_window

        # If no spw is given then calculate from the max and min range
        if spec_window == '':
            spw_range = freq_map(freq1, freq2)

        no_chan = int(step_freq * 1000.0 / width_freq)  # MHz/kHz!!
        # I think I always want this with cvel.

        outfile = outdir + 'vis_' + freq_range
        backupfile = backup_dir + 'vis_' + freq_range
        if not debug:
            if os.path.exists(outfile):
                shutil.rmtree(outfile)
            if os.path.exists(backupfile):
                shutil.rmtree(backupfile)
            print 'working on: ' + outfile
            try:
                # dump_all()
                mstransform(vis=infile,
                            outputvis=outfile,
                            regridms=True,
                            restfreq='1420.405752MHz',
                            mode='frequency',
                            nchan=no_chan,
                            outframe='lsrk',
                            interpolation='linear',
                            veltype='radio',
                            start=str(freq1) + 'MHz',
                            width=str(width_freq) + 'kHz',
                            spw=cvel_spw_range,
                            combinespws=True,
                            nspw=1,
                            createmms=False,
                            datacolumn="data")

                # cvel(vis=infile,
                # outputvis=outfile,
                #       restfreq='1420.405752MHz',
                #       mode='frequency',
                #       nchan=no_chan,
                #       outframe='lsrk',
                #       interpolation='linear',
                #       veltype='radio',
                #       start=str(freq1)+'MHz',
                #       width=str(width_freq)+'kHz',
                #       spw=spw_range)
            except Exception, spEx:
                print '*********\nSplit exception: %s\n***********' % str(spEx)
        else:
            msg = '''
mstransform(vis=%s,
outputvis=%s,
start=%s,
width=%s,
spw=%s,
nchan=%d)'''.format(infile, outfile,
                    str(freq1) + 'MHz', width_freq, cvel_spw_range, no_chan)
            print msg

        freq1 = freq1 + step_freq
        freq2 = freq2 + step_freq