Esempio n. 1
0
def __correct_feed_convention(ms):
    from pyrap.tables import table as tbl
    import numpy as np
    with tbl("%s::FEED" % ms, readonly=False) as t:
        ang = t.getcol("RECEPTOR_ANGLE")
        ang[:, 0] = -np.pi / 2
        ang[:, 1] = -np.pi / 2
        t.putcol("RECEPTOR_ANGLE", ang)
        log.info("Receptor angle rotated by -90deg")
Esempio n. 2
0
def test_flag_count(flagged_ms, tol):
    """ Tests for flag count less than tolerance """
    with tbl("::".join((flagged_ms, "FIELD"))) as t:
        fnames = t.getcol("NAME")

    with tbl(flagged_ms) as t:
        fid = t.getcol("FIELD_ID")
        flag = t.getcol("FLAG")

    flag_sel = flag[fid == fnames.index("3C286")]
    count_flagged_3c286 = np.nansum(flag_sel, axis=(0, 1, 2))
    flagged_ratio = count_flagged_3c286 / flag_sel.size
    print("Percent flagged for 3C286: %.3f%%" % (100. * flagged_ratio))
    assert flagged_ratio < tol

    flag_sel = flag[fid == fnames.index("PKS1934-63")]
    count_flagged_1934 = np.nansum(flag_sel, axis=(0, 1, 2))
    flagged_ratio = count_flagged_1934 / flag_sel.size
    print("Percent flagged for PKS1934-63: %.3f%%" % (100. * flagged_ratio))
    assert flagged_ratio < tol
Esempio n. 3
0
def __extract_fields(ms, field_list, field_ids):
    from pyrap.tables import table as tbl
    with tbl("%s/%s::FIELD" % (MSDIR, ms)) as fields:
        fnames = fields.getcol("NAME")
    if len(set([field_list[f]
                for f in field_list]).difference(set(fnames))) != 0:
        raise ValueError(
            "Fields {0:s} selected but only {1:s} available.".format(
                ", ".join(
                    ["'{0:s}'".format(field_list[f]) for f in field_list]),
                ", ".join(["'{0:s}'".format(f) for f in fnames])))
    field_ids += [fnames.index(field_list[f]) for f in field_list]
Esempio n. 4
0
def test_bandwidth_flagged(flagged_ms, tol):
    """ Tests for total bandwidth flagged less than tolerance """
    with tbl("::".join((flagged_ms, "FIELD"))) as t:
        fnames = t.getcol("NAME")

    with tbl(flagged_ms) as t:
        fid = t.getcol("FIELD_ID")
        data = t.getcol("DATA")

    data_sel = data[fid == fnames.index("3C286"), :, 0]
    count_flagged_3c286 = np.nansum(data_sel, axis=0) > 0
    flagged_ratio = count_flagged_3c286.sum() / data_sel.shape[1]
    print("Percent bandwidth flagged for 3C286: %.3f%%" %
          (100. * flagged_ratio))
    assert flagged_ratio < tol

    data_sel = data[fid == fnames.index("PKS1934-63"), :, 0]
    count_flagged_1934 = np.nansum(data_sel, axis=0) > 0
    flagged_ratio = count_flagged_1934.sum() / data_sel.shape[1]
    print("Percent bandwidth flagged for PKS1934-63: %.3f%%" %
          (100. * flagged_ratio))
    assert flagged_ratio < tol
Esempio n. 5
0
def test_max_chisq(flagged_ms, tol):
    """ Tests for improvement in max chisq per correlation """
    with tbl("::".join((flagged_ms, "FIELD"))) as t:
        fnames = t.getcol("NAME")

    with tbl(flagged_ms) as t:
        fid = t.getcol("FIELD_ID")
        flag = t.getcol("FLAG")
        data = t.getcol("DATA")

    abs_data_sel = np.abs(data[fid == fnames.index("3C286")])
    diff = (abs_data_sel - np.nanmean(abs_data_sel, axis=0))**2
    chisq_unflagged_3c286 = np.nanmax(diff, axis=(0, 1))
    corrs_str = ",".join(["{0:.3f}".format(f) for f in chisq_unflagged_3c286])
    print("Max Chi^2 unflagged 3C286: [%s]" % corrs_str)

    abs_data_sel = np.abs(data[fid == fnames.index("PKS1934-63")])
    diff = (abs_data_sel - np.nanmean(abs_data_sel, axis=0))**2
    chisq_unflagged_1934 = np.nanmax(diff, axis=(0, 1))
    corrs_str = ",".join(["{0:.3f}".format(f) for f in chisq_unflagged_1934])
    print("Max Chi^2 unflagged PKS B1934-638: [%s]" % corrs_str)

    # flag data
    data[flag] = np.nan

    abs_data_sel = np.abs(data[fid == fnames.index("3C286")])
    diff = (abs_data_sel - np.nanmean(abs_data_sel, axis=0))**2
    chisq_flagged_3c286 = np.nanmax(diff, axis=(0, 1))
    corrs_str = ",".join(["{0:.3f}".format(f) for f in chisq_flagged_3c286])
    print("Max Chi^2 flagged 3C286: [%s]" % corrs_str)

    abs_data_sel = np.abs(data[fid == fnames.index("PKS1934-63")])
    diff = (abs_data_sel - np.nanmean(abs_data_sel, axis=0))**2
    chisq_flagged_1934 = np.nanmax(diff, axis=(0, 1))
    corrs_str = ",".join(["{0:.3f}".format(f) for f in chisq_flagged_1934])
    print("Max Chi^2 flagged PKS B1934-638: [%s]" % corrs_str)

    assert all(chisq_unflagged_3c286 > chisq_flagged_3c286 * tol)
    assert all(chisq_unflagged_1934 > chisq_flagged_1934 * tol)
Esempio n. 6
0
 def clip_delays(vis, clipminmax):
     with tbl(os.path.join(OUTPUT, vis), readonly=False) as t:
         fl = t.getcol("FLAG")
         d = t.getcol("FPARAM")
         prevflagged = np.sum(fl) * 100.0 / fl.size
         fl = np.logical_or(
             fl,
             np.logical_or(d.real > np.max(clipminmax),
                           d.real < np.min(clipminmax)))
         t.putcol("FLAG", fl)
         currentflagged = np.sum(fl) * 100.0 / fl.size
         vermeerkat.log.info(
             "Flagged {0:.2f}%, up from previous {1:.2f}%".format(
                 currentflagged, prevflagged))
Esempio n. 7
0
            def taper_weigh(ms):
                from pyrap.tables import table as tbl
                with tbl(ms, readonly=False) as t:
                    uvw = t.getcol("UVW")
                    max_uv = np.sqrt(np.max(uvw[:, 0]**2 + uvw[:, 1]**2))
                    taper = lambda u, v, a, b, gamma: (1.0 / (1 + np.exp(
                        (np.sqrt(u**2 + v**2) - b) /
                        (2.0 * max_uv / gamma))) + 1.0 / (1 + np.exp(
                            (-np.sqrt(u**2 + v**2) + a) /
                            (2.0 * max_uv / gamma))) + 1.0 / (1 + np.exp(
                                (-np.sqrt(u**2 + v**2) - b) /
                                (2.0 * max_uv / gamma))) + 1.0 / (1 + np.exp(
                                    (np.sqrt(u**2 + v**2) + a) /
                                    (2.0 * max_uv / gamma)))) - 2.0

                    weight = t.getcol("WEIGHT")
                    weight_new = weight.copy()
                    tp_weight = taper(
                        uvw[:, 0],
                        uvw[:, 1],
                        taper_inner_cut,  # inner cut
                        taper_outer_cut,  # outer cut
                        taper_gamma)
                    weight_new *= tp_weight[:, None]
                    import matplotlib
                    matplotlib.use('Agg')
                    from matplotlib import pyplot as plt
                    import os
                    from scipy.interpolate import griddata
                    plt.figure()
                    x = np.linspace(np.min(uvw), np.max(uvw), 1024)
                    xx, xy = np.meshgrid(x, x, sparse=False)
                    ###grid = griddata(uvw[:, 0:2], tp_weight, (xx, xy), method="linear")
                    grid = taper(xx, xy, taper_inner_cut, taper_outer_cut,
                                 taper_gamma)
                    plt.imshow(grid,
                               cmap="magma",
                               extent=[
                                   np.min(xx),
                                   np.max(xx),
                                   np.max(xx),
                                   np.min(xx)
                               ])
                    plt.xlabel("u (m)")
                    plt.ylabel("v (m)")
                    plt.savefig(
                        os.path.join(OUTPUT, "uvtaper_{0:d}.png".format(ti)))

                    t.putcol("WEIGHT", weight_new)
                    t.close()
Esempio n. 8
0
MSDIR = args.msdir_directory
OUTPUT = args.output_directory
vermeerkat.log.info(
    "Directory '{0:s}' is used as input directory".format(INPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as output directory".format(OUTPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as msdir directory".format(MSDIR))

PREFIX = args.msprefix
ZEROGEN_DATA = PREFIX + ".ms"

vermeerkat.log.info(
    "Dataset '{0:s}' to be used throughout".format(ZEROGEN_DATA))

with tbl(os.path.join(MSDIR, ZEROGEN_DATA) + "::FIELD", ack=False) as t:
    field_names = t.getcol("NAME")
    FDB = {fn: str(fni) for fni, fn in enumerate(field_names)}


def flistr():
    vermeerkat.log.info("The following fields are available:")
    for f in FDB:
        vermeerkat.log.info("\t '{0:s}' index {1:s}".format(f, FDB[f]))
    sys.exit(0)


vermeerkat.init_inputdir(INPUT,
                         dont_prompt=args.dont_prompt,
                         dont_clean=args.dont_reinitialize_input_dir)
Esempio n. 9
0
    def create_K_table(cls, db, gname, outname="K"):
        """
                Write real-valued K-Jones table
                
                Args:
                    db: a cubical pickled_db instance
                    gname: name of pickled_db solutions to export
                    outname: suffix of exported CASA gaintable
            """
        if six.PY3:
            log.error(
                "Gaintables cannot be written in Python 3 mode due to current casacore implementation issues"
            )
            return
        if np.prod(db[gname].shape) == 0:
            log.warn("No %s solutions. Will not write CASA table" % gname)
            return
        paramerrs = cls.__check_param_err(db, gname)
        assert db[gname].axis_labels == (
            'dir', 'time', 'freq', 'ant',
            'corr'), "DB table in unrecognized format"

        ddids = db.sel_ddids
        ndir = len(db[gname].grid[db[gname].ax.dir])
        ntime = len(db[gname].grid[db[gname].ax.time])
        nant = len(db[gname].grid[db[gname].ax.ant])
        ncorr = len(db[gname].grid[db[gname].ax.corr])
        nddids = len(db.sel_ddids)
        nrow = ndir * ntime * \
                nant * len(ddids)
        assert ncorr == 2, "Expected diagnonal Jones matrix"

        cls.init_empty(db,
                       db.filename + ".%s.casa" % outname,
                       db[gname].grid[db[gname].ax.freq],
                       db[gname].grid[db[gname].ax.ant],
                       field_ndir=ndir,
                       is_complex=False,
                       viscal_label="K Jones")

        with tbl(str(db.filename + ".%s.casa" % outname),
                 ack=False,
                 readonly=False) as t:
            t.addrows(nrows=nrow)
            for iddid, ddid in enumerate(db.sel_ddids):
                spwid = db.ddid_spw_map[ddid]
                minfreq = np.min(db.spwchanfreq[spwid] -
                                 0.5 * db.spwchanwidth[spwid])
                maxfreq = np.max(db.spwchanfreq[spwid] +
                                 0.5 * db.spwchanwidth[spwid])
                ddsolfreqindx = np.argwhere(
                    np.logical_and(
                        db[gname].grid[db[gname].ax.freq] >= minfreq,
                        db[gname].grid[db[gname].ax.freq] <= maxfreq))
                # note -- CASA K table delays are in nanoseconds. This presumes delays in the cubical tables are already denormalized into seconds
                params = np.swapaxes(
                    db[gname].get_cube()[:, :, ddsolfreqindx, :, :], 2,
                    3).reshape(ndir * ntime * nant, len(ddsolfreqindx),
                               ncorr) * 1.0e9
                paramerrs = np.swapaxes(
                    paramerrs[:, :, ddsolfreqindx, :, :], 2, 3).reshape(
                        ndir * ntime * nant, len(ddsolfreqindx), ncorr) * 1.0e9
                flags = np.ma.getmaskarray(params)
                fieldid = np.repeat(
                    np.arange(ndir),
                    ntime * nant)  # dir (marked as field) is slowest varying
                time = np.repeat(
                    np.tile(db[gname].grid[db[gname].ax.time], ndir), nant)
                ant1 = np.tile(np.arange(nant),
                               ndir * ntime)  # FK ndir * ntime blocks
                #ant2 can be the same - it is not used unless specifying mueller matricies
                nrowsdd = ntime * nant * ndir
                t.putcol("TIME", time, startrow=nrowsdd * iddid)
                t.putcol("FIELD_ID", fieldid, startrow=nrowsdd * iddid)
                t.putcol(
                    "SPECTRAL_WINDOW_ID",
                    np.ones(ant1.shape) * iddid,
                    startrow=nrowsdd * iddid
                )  # new spectral window with freq range for these sols
                t.putcol("ANTENNA1", ant1, startrow=nrowsdd * iddid)
                t.putcol("ANTENNA2",
                         np.ones(ant1.shape) * -1,
                         startrow=nrowsdd * iddid)
                t.putcol("INTERVAL",
                         np.zeros(ant1.shape),
                         startrow=nrowsdd *
                         iddid)  #TODO: unclear from MEMO 229
                t.putcol("SCAN_NUMBER",
                         np.ones(ant1.shape) * -1,
                         startrow=nrowsdd *
                         iddid)  #TODO this FK info is not available yet @oms
                t.putcol("OBSERVATION_ID",
                         np.ones(ant1.shape) * -1,
                         startrow=nrowsdd *
                         iddid)  #TODO this FK info is not available yet @oms
                t.putcol("FPARAM",
                         np.ma.getdata(params),
                         startrow=nrowsdd * iddid)
                t.putcol("PARAMERR",
                         np.ma.getdata(paramerrs),
                         startrow=nrowsdd * iddid)
                t.putcol("FLAG", flags, startrow=nrowsdd * iddid)
                t.putcol("SNR",
                         np.ones(params.shape) * np.inf,
                         startrow=nrowsdd *
                         iddid)  #TODO this is not available @oms
                t.putcol("WEIGHT",
                         np.ones(params.shape),
                         startrow=nrowsdd *
                         iddid)  #TODO this is not available @oms
Esempio n. 10
0
    def init_empty(cls,
                   db,
                   filename,
                   solfreqs,
                   solants,
                   field_ndir=1,
                   is_complex=True,
                   viscal_label="B Jones"):
        """
                Initialize empty calibration table with metadata
                
                Args:
                    db: a cubical pickled_db instance
                    filename: filename of CASA gaintable to be written
                    solfreqs: centre frequencies of solutions to be stored in table
                    solants: Names from Cubical database antenna axis, used to order solutions
                    field_ndir: Number of solvable directions in field
                    is_complex: Solutions are complex or real-valued
                    viscal_label: Sets viscal property of CASA table - used as identifier in CASA
            """
        if six.PY3:
            log.error(
                "Gaintables cannot be written in Python 3 mode due to current casacore implementation issues"
            )
            return
        if os.path.exists(filename):
            if os.path.isfile(filename):
                log.error(
                    "CASA calibration table destination already exists but is not a directory. Will not remove."
                )
                return
            else:
                log.info(
                    "Destination CASA gain table '%s' exists. Will overwrite."
                    % filename)
                shutil.rmtree(filename)  # CASA convention is to overwrite

        basedir = os.path.dirname(filename) or '.'
        subprocess.check_output(
            ["tar", "zxvf", BLANK_TABLE_TARBALL, "-C", basedir])
        os.rename(os.path.join(basedir, BLANK_TABLE_NAME), filename)

        antorder = [db.antnames.index(an) for an in solants]
        with tbl("%s::ANTENNA" % str(filename), ack=False,
                 readonly=False) as t:
            t.addrows(nrows=len(db.anttype))
            t.putcol("OFFSET", db.antoffset[antorder])
            t.putcol("POSITION", db.antpos[antorder])
            t.putcol("TYPE", np.array(db.anttype)[antorder])
            t.putcol("DISH_DIAMETER", db.antdishdiam[antorder])
            t.putcol("FLAG_ROW", db.antflagrow[antorder])
            t.putcol("MOUNT", np.array(db.antmount)[antorder])
            t.putcol("NAME", np.array(db.antnames)[antorder])
            t.putcol("STATION", np.array(db.antstation)[antorder])

        assert "field" in db.metadata, "Solver field not passed in metadata. This is a bug"
        assert type(db.metadata["field"]
                    ) is int, "Currently only supports single field"
        selfield = np.arange(len(db.fieldname)) == db.metadata["field"]
        with tbl("%s::FIELD" % str(filename), ack=False, readonly=False) as t:
            t.addrows(nrows=field_ndir)
            t.putcol("DELAY_DIR",
                     np.tile(db.fielddelaydirs[selfield], (field_ndir, 1)))
            t.putcol("PHASE_DIR",
                     np.tile(db.fieldphasedirs[selfield], (field_ndir, 1)))
            t.putcol("REFERENCE_DIR",
                     np.tile(db.fieldrefdir[selfield], (field_ndir, 1)))
            t.putcol(
                "CODE",
                np.tile(np.array(db.fieldcode)[selfield], (field_ndir, 1)))
            t.putcol("FLAG_ROW",
                     np.tile(db.fieldflagrow[selfield], (field_ndir, 1)))
            t.putcol(
                "NAME",
                np.array(
                    map(str, [
                        "%s_DIR_%d" % (f, fdi) for fdi, f in
                        enumerate([db.fieldname[np.where(selfield)[0][0]]] *
                                  field_ndir)
                    ])).T)
            t.putcol(
                "SOURCE_ID",
                np.tile(db.fieldsrcid[selfield],
                        (field_ndir, 1)) + np.arange(field_ndir).T)
            t.putcol("TIME", np.tile(db.fieldtime[selfield], (field_ndir, 1)))

        with tbl("%s::OBSERVATION" % str(filename), ack=False,
                 readonly=False) as t:
            t.addrows(nrows=len(db.obsobserver))
            (len(db.obstimerange) != 0) and t.putcol("TIME_RANGE",
                                                     db.obstimerange)
            (len(db.obslog) != 0) and t.putcol("LOG", db.obslog)
            (len(db.obsschedule) != 0) and t.putcol("SCHEDULE", db.obsschedule)
            (len(db.obsflagrow) != 0) and t.putcol("FLAG_ROW", db.obsflagrow)
            (len(db.obsobserver) != 0) and t.putcol("OBSERVER", db.obsobserver)
            (len(db.obsproject) != 0) and t.putcol("PROJECT", db.obsproject)
            (len(db.obsreleasedate) != 0) and t.putcol("RELEASE_DATE",
                                                       db.obsreleasedate)
            (len(db.obstelescopename) != 0) and t.putcol(
                "TELESCOPE_NAME", db.obstelescopename)

        with tbl("%s::SPECTRAL_WINDOW" % str(filename),
                 ack=False,
                 readonly=False) as t:
            t.addrows(nrows=len(db.sel_ddids))
            # Per DDID determine solution spacing in frequency
            for iddid, ddid in enumerate(db.sel_ddids):
                spwid = db.ddid_spw_map[ddid]
                minfreq = np.min(db.spwchanfreq[spwid] -
                                 0.5 * db.spwchanwidth[spwid])
                maxfreq = np.max(db.spwchanfreq[spwid] +
                                 0.5 * db.spwchanwidth[spwid])
                ddsolfreqs = solfreqs[np.logical_and(solfreqs >= minfreq,
                                                     solfreqs <= maxfreq)]
                # assume linearly-spaced solutions for N - 1 solutions along grid and last solution
                # may have different spacing if solution interval does not exactly divide number of channels
                if ddsolfreqs.size > 1:
                    ch0 = np.min(db.spwchanfreq)
                    chN = np.max(db.spwchanfreq)
                    ddsolwidthmax = 2 * (ddsolfreqs[0] - ch0)
                    ddsolwidth = np.ones(ddsolfreqs.size) * ddsolwidthmax
                    ddsolwidth[-1] = ddsolwidthmax + (
                        chN - (ddsolfreqs.size * ddsolwidthmax + ch0))
                else:
                    ddsolwidth = db.spwtotalbandwidth

                t.putcell("MEAS_FREQ_REF", iddid, db.spwmeasfreq[spwid])
                t.putcell("CHAN_FREQ", iddid, ddsolfreqs)
                t.putcell("REF_FREQUENCY", iddid, db.spwreffreq[spwid])
                t.putcell("CHAN_WIDTH", iddid, ddsolwidth)
                t.putcell("EFFECTIVE_BW", iddid,
                          ddsolwidth)  # TODO: this may not be true
                t.putcell("RESOLUTION", iddid, db.spwresolution[spwid])
                t.putcell("FLAG_ROW", iddid, db.spwflagrow[spwid])
                t.putcell("FREQ_GROUP", iddid, db.spwfreqgroup[spwid])
                t.putcell("FREQ_GROUP_NAME", iddid, db.spwfreqgroupname[spwid])
                t.putcell("IF_CONV_CHAIN", iddid, db.spwifconvchain[spwid])
                t.putcell("NAME", iddid, str(db.spwname[spwid]))
                t.putcell("NET_SIDEBAND", iddid, db.spwnetsideband[spwid])
                t.putcell("NUM_CHAN", iddid, ddsolfreqs.size)
                t.putcell("TOTAL_BANDWIDTH", iddid, maxfreq - minfreq)

        with tbl(str(filename), ack=False, readonly=False) as t:
            t.putkeyword("ParType", "Complex" if is_complex else "Float")
            t.putkeyword("VisCal", viscal_label)

            if not is_complex:
                cdesc = t.getcoldesc("CPARAM")
                cdesc["valueType"] = "float"
                t.addcols({"FPARAM": cdesc})
                t.removecols("CPARAM")
Esempio n. 11
0
def fixms(msname):
    """
        Runs an operation similar to the CASA fixvis task
        Recomputes UVW coordinates with casacore for the predicted
        az-elev delay projections given a dataset with antenna ICRS
        positions and a time centroid column.

        Note: This operation CANNOT be applied blockwise due
        to a casacore.measures threadsafety issue
    """
    with tbl(msname + "::ANTENNA", ack=False) as t:
        apos = t.getcol("POSITION")
        aposcoldesc = t.getcoldesc("POSITION")
        posunits = aposcoldesc["keywords"]["QuantumUnits"]
        posframe = aposcoldesc["keywords"]["MEASINFO"]["Ref"]

    with tbl(msname + "::FIELD", ack=False) as t:
        if not np.all(t.getcol("NUM_POLY") == 0):
            raise RuntimeError(
                "Does not support time-variable reference centres")
        fnames = t.getcol("NAME")
        field_stop_ctrs = t.getcol("PHASE_DIR")
        fieldcoldesc = t.getcoldesc("PHASE_DIR")
        stopctr_units = fieldcoldesc["keywords"]["QuantumUnits"]
        stopctr_epoch = fieldcoldesc["keywords"]["MEASINFO"]["Ref"]

    with tbl(msname, ack=False) as t:
        a1 = t.getcol("ANTENNA1")
        a2 = t.getcol("ANTENNA2")
        uvw = t.getcol("UVW")
        field_id = t.getcol("FIELD_ID")
        ddid = t.getcol("DATA_DESC_ID")
        time = t.getcol("TIME_CENTROID")
        timecoldesc = t.getcoldesc("TIME_CENTROID")
        time_TZ = timecoldesc["keywords"]["MEASINFO"]["Ref"]
        time_unit = timecoldesc["keywords"]["QuantumUnits"][0]

    logger.info("Computing UVW coordinates for output dataset... WAIT")
    new_uvw = np.zeros_like(uvw, dtype=uvw.dtype)
    for fi in range(len(fnames)):
        fsel = field_id == fi
        padded_uvw = synthesize_uvw(station_ECEF=apos,
                                    time=time[fsel],
                                    a1=a1[fsel],
                                    a2=a2[fsel],
                                    phase_ref=field_stop_ctrs[fi],
                                    stopctr_units=stopctr_units,
                                    time_TZ=time_TZ,
                                    time_unit=time_unit,
                                    stopctr_epoch=stopctr_epoch,
                                    posframe=posframe,
                                    posunits=posunits)
        new_uvw[fsel] = dense2sparce_uvw(a1=a1[fsel],
                                         a2=a2[fsel],
                                         time=time[fsel],
                                         ddid=ddid[fsel],
                                         padded_uvw=padded_uvw["UVW"])
        logger.info("\t {} / {} fields completed".format(fi + 1, len(fnames)))

    logger.info("Writing computed UVW coordinates to output dataset")
    with tbl(msname, ack=False, readonly=False) as t:
        t.lock()  # workaround dask-ms bug not releasing user locks
        t.putcol("UVW", new_uvw)
        t.unlock()
Esempio n. 12
0
 def create_B_table(cls, db, gname, outname = "B", diag=True):
     """
         Write diagonal B-Jones caltable
         
         Args:
             db: a cubical pickled_db instance
             gname: name of pickled_db solutions to export
             outname: suffix of exported CASA gaintable
             diag: Write out diagonal of Jones matrix if true, off-diagonal (leakage) terms otherwise.
     """
     if np.prod(db[gname].shape) == 0:
         log.warn("No %s solutions. Will not write CASA table" % gname)
         return
     assert db[gname].shape == db[gname + ".err"].shape, "PARAM err shape does not match PARAM shape, this is a bug"
     assert db[gname].axis_labels == ('dir', 'time', 'freq', 'ant', 'corr1', 'corr2'), "DB table in unrecognized format"
     
     ddids = db.sel_ddids
     ndir = len(db[gname].grid[db[gname].ax.dir])
     ntime = len(db[gname].grid[db[gname].ax.time])
     nant = len(db[gname].grid[db[gname].ax.ant])
     ncorr1 = len(db[gname].grid[db[gname].ax.corr1])
     ncorr2 = len(db[gname].grid[db[gname].ax.corr2])
     nddids = len(db.sel_ddids)
     nrow = ndir * ntime * \
             nant * len(ddids)
     assert ncorr1 == ncorr2 and ncorr1 == 2, "Expected 2x2 solution, this is a bug"
     
     cls.init_empty(db, 
                    db.filename + ".%s.casa" % outname, 
                    db[gname].grid[db[gname].ax.freq],
                    db[gname].grid[db[gname].ax.ant],
                    field_ndir=ndir,
                    viscal_label="B Jones" if diag else "D Jones")
     
     with tbl(db.filename + ".%s.casa" % outname, ack=False, readonly=False) as t:
         t.addrows(nrows=nrow)
         
         for iddid, ddid in enumerate(db.sel_ddids):
             spwid = db.ddid_spw_map[ddid]
             minfreq = np.min(db.spwchanfreq[spwid] - 0.5 * db.spwchanwidth[spwid])
             maxfreq = np.max(db.spwchanfreq[spwid] + 0.5 * db.spwchanwidth[spwid]) 
             ddsolfreqindx = np.argwhere(np.logical_and(db[gname].grid[db[gname].ax.freq] >= minfreq,
                                                        db[gname].grid[db[gname].ax.freq] <= maxfreq))
             jones_entries = [0, 3] if diag else [1, 2] # diagonal or crosshands
             params = np.swapaxes(db[gname].get_cube()[:, :, ddsolfreqindx, :, :], 
                                  2, 3).reshape(ndir * ntime * nant, len(ddsolfreqindx), ncorr1 * ncorr2)[:, :, jones_entries]
             paramerrs = np.swapaxes(db[gname + ".err"].get_cube()[:, :, ddsolfreqindx, :, :],
                                     2, 3).reshape(ndir * ntime * nant, len(ddsolfreqindx), ncorr1 * ncorr2)[:, :, jones_entries]
             flags = np.ma.getmaskarray(params)
             fieldid = np.repeat(np.arange(ndir), ntime * nant) # dir (marked as field) is slowest varying
             time = np.repeat(np.tile(db[gname].grid[db[gname].ax.time], ndir), nant)
             ant1 = np.tile(np.arange(nant), ndir * ntime) # FK ndir * ntime blocks
             #ant2 can be the same - it is not used unless specifying mueller matricies
             nrowsdd = ntime *  nant * ndir
             t.putcol("TIME", time, startrow=nrowsdd * iddid)
             t.putcol("FIELD_ID", fieldid, startrow=nrowsdd * iddid)
             t.putcol("SPECTRAL_WINDOW_ID", np.ones(ant1.shape) * iddid, startrow=nrowsdd * iddid) # new spectral window with freq range for these sols
             t.putcol("ANTENNA1", ant1, startrow=nrowsdd * iddid)
             t.putcol("ANTENNA2", np.ones(ant1.shape) * -1, startrow=nrowsdd * iddid) 
             t.putcol("INTERVAL", np.zeros(ant1.shape), startrow=nrowsdd * iddid) #TODO: unclear from MEMO 229
             t.putcol("SCAN_NUMBER", np.ones(ant1.shape) * -1, startrow=nrowsdd * iddid) #TODO this FK info is not available yet @oms
             t.putcol("OBSERVATION_ID", np.ones(ant1.shape) * -1, startrow=nrowsdd * iddid) #TODO this FK info is not available yet @oms
             t.putcol("CPARAM", np.ma.getdata(params), startrow=nrowsdd * iddid)
             t.putcol("PARAMERR", np.ma.getdata(paramerrs), startrow=nrowsdd * iddid)
             t.putcol("FLAG", flags, startrow=nrowsdd * iddid)
             t.putcol("SNR", np.ones(params.shape) * np.inf, startrow=nrowsdd * iddid) #TODO this is not available @oms
             t.putcol("WEIGHT", np.ones(params.shape), startrow=nrowsdd * iddid) #TODO this is not available @oms
Esempio n. 13
0
#!/usr/bin/python
from pyrap.tables import table as tbl
from pyrap import tables as tbls
import re
import numpy as np

with open("/southern_calibrators.txt") as f, \
     tbl("/casa-release-4.7.0-el6/data/nrao/VLA/standards/fluxcalibrator.data", readonly=False) as fc, \
     tbl("/casa-release-4.7.0-el6/data/nrao/VLA/standards/PerleyButler2013Coeffs", readonly=False) as pb:
    line = f.readline()
    ln_no = 1
    while line:
        #discard comments
        command = line.split("//")[0]

        #empty line ?
        if command.strip() == "":
            print "Skipping line: '%s'" % line
            line = f.readline()
            ln_no += 1
            continue

        #source ?
        valset = re.match(
            r"^name=(?P<name>[0-9A-Za-z\-+_]+)[ ]+"
            r"epoch=(?P<epoch>[0-9]+)[ ]+"
            r"ra=(?P<ra>[+\-]?[0-9]+h[0-9]+m[0-9]+(?:.[0-9]+)?s)[ ]+"
            r"dec=(?P<decl>[+\-]?[0-9]+d[0-9]+m[0-9]+(?:.[0-9]+)?s)[ ]+"
            r"a=(?P<a>[+\-]?[0-9]+(?:.[0-9]+)?)[ ]+"
            r"b=(?P<b>[+\-]?[0-9]+(?:.[0-9]+)?)[ ]+"
            r"c=(?P<c>[+\-]?[0-9]+(?:.[0-9]+)?)[ ]+"
Esempio n. 14
0
INPUT = args.input_directory
MSDIR = args.msdir_directory
OUTPUT = args.output_directory
vermeerkat.log.info(
    "Directory '{0:s}' is used as input directory".format(INPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as output directory".format(OUTPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as msdir directory".format(MSDIR))

PREFIX = args.msprefix
DATASET = PREFIX + ".ms"
vermeerkat.log.info("Dataset '{0:s}' to be used throughout".format(DATASET))

with tbl(os.path.join(MSDIR, DATASET) + "::FIELD", ack=False) as t:
    field_names = t.getcol("NAME")
    FDB = {fn: str(fni) for fni, fn in enumerate(field_names)}

vermeerkat.init_inputdir(INPUT,
                         dont_prompt=args.dont_prompt,
                         dont_clean=args.dont_reinitialize_input_dir)

TARGET = [f[0] if isinstance(f, list) else f for f in args.target_field]
if len(TARGET) < 1: raise ValueError("No target specified")

vermeerkat.log.info("Will be self calibrating:")
for t in TARGET:
    vermeerkat.log.info("\t{} (field id {})".format(t, FDB[t]))

REFANT = args.ref_ant
Esempio n. 15
0
    'path prefix')

args = parser.parse_args(sys.argv[2:])

INPUT = args.input_directory
MSDIR = args.msdir_directory
OUTPUT = args.output_directory
vermeerkat.log.info(
    "Directory '{0:s}' is used as input directory".format(INPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as output directory".format(OUTPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as msdir directory".format(MSDIR))

PREFIX = args.msprefix
ZEROGEN_DATA = PREFIX + ".ms"

vermeerkat.log.info(
    "Dataset '{0:s}' to be used throughout".format(ZEROGEN_DATA))

with tbl(os.path.join(MSDIR, ZEROGEN_DATA) + "::ANTENNA", ack=False) as t:
    name = t.getcol("NAME")
    pos = t.getcol("POSITION")

vermeerkat.log.info("The following antennae are available:")
for ni, (n, p) in enumerate(zip(name, pos)):
    vermeerkat.log.info("\t {0:d}: '{1:s}' with position [{2:s}]".format(
        ni, n, ",".join(map(str, p))))

vermeerkat.log.info("---End of listr---")
Esempio n. 16
0
MSDIR = args.msdir_directory
OUTPUT = args.output_directory
vermeerkat.log.info(
    "Directory '{0:s}' is used as input directory".format(INPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as output directory".format(OUTPUT))
vermeerkat.log.info(
    "Directory '{0:s}' is used as msdir directory".format(MSDIR))

PREFIX = args.msprefix
ZEROGEN_DATA = PREFIX + ".ms"

vermeerkat.log.info(
    "Dataset '{0:s}' to be used throughout".format(ZEROGEN_DATA))

with tbl(os.path.join(MSDIR, ZEROGEN_DATA) + "::SOURCE", ack=False) as t:
    codes = t.getcol("CODE")
    intents = t.getcol("CALIBRATION_GROUP")
    SCODE = {sni: c for sni, c in enumerate(codes)}
    SINTENT = {sni: intent for sni, intent in enumerate(intents)}

with tbl(os.path.join(MSDIR, ZEROGEN_DATA) + "::STATE", ack=False) as t:
    states = t.getcol("OBS_MODE")

with tbl(os.path.join(MSDIR, ZEROGEN_DATA) + "::FIELD", ack=False) as t:
    field_names = t.getcol("NAME")
    code = t.getcol("CODE")
    sid = t.getcol("SOURCE_ID")

    FDB = {fn: str(fni) for fni, fn in enumerate(field_names)}
    FCODE = {