Ejemplo n.º 1
0
    def __init__(self, ns, name, sources, stations=None, ref_station=None, tags="iono", make_log=False):
        PiercePoints.PiercePoints.__init__(self, ns, name, sources, stations, height, make_log)

        if use_lonlat:
            earth_radius = 6365
            # convert km to rad and km/h to rad/s
            W1 = Wavelength_1 / (earth_radius + height)
            W2 = Wavelength_2 / (earth_radius + height)
            Sp1 = (Speed_1 / (earth_radius + height)) / 3600
            Sp2 = (Speed_2 / (earth_radius + height)) / 3600
        else:
            # convert km to m and km/h to m/s
            W1 = Wavelength_1 * 1000
            W2 = Wavelength_2 * 1000
            Sp1 = Speed_1 / 3.6
            Sp2 = Speed_2 / 3.6
        self.ref_station = ref_station
        self._add_parm(name="TEC0", value=Meow.Parm(TEC0), tags=tags)
        self._add_parm(name="height", value=Meow.Parm(height), tags=tags)
        self._add_parm(name="Amp_1", value=Meow.Parm(Amp_1), tags=tags)
        self._add_parm(name="Amp_2", value=Meow.Parm(Amp_2), tags=tags)
        self._add_parm(name="Wavelength_1", value=Meow.Parm(W1), tags=tags)
        self._add_parm(name="Wavelength_2", value=Meow.Parm(W2), tags=tags)
        self._add_parm(name="Speed_1", value=Meow.Parm(Sp1), tags=tags)
        self._add_parm(name="Speed_2", value=Meow.Parm(Sp2), tags=tags)
        self._add_parm(name="Theta_1", value=Meow.Parm(Theta_1), tags=tags)
        self._add_parm(name="Theta_2", value=Meow.Parm(Theta_2), tags=tags)
        self.make_display_grid(W1, W2, Theta_1, Theta_2, Amp_1, Amp_2, TEC0)
Ejemplo n.º 2
0
    def compute_jones(self, nodes, stations=None, tags=None, label='', **kw):
        stations = stations or Context.array.stations()
        g_ampl_def = Meow.Parm(1)
        g_phase_def = Meow.Parm(0)
        nodes = Jones.gain_ap_matrix(nodes,
                                     g_ampl_def,
                                     g_phase_def,
                                     tags=tags,
                                     series=stations)

        # make parmgroups for phases and gains
        self.pg_phase = ParmGroup.ParmGroup(
            label + "_phase",
            nodes.search(tags="solvable phase"),
            table_name="%s_phase.fmep" % label,
            bookmark=4)
        self.pg_ampl = ParmGroup.ParmGroup(label + "_ampl",
                                           nodes.search(tags="solvable ampl"),
                                           table_name="%s_ampl.fmep" % label,
                                           bookmark=4)

        # make solvejobs
        ParmGroup.SolveJob("cal_" + label + "_phase",
                           "Calibrate %s phases" % label, self.pg_phase)
        ParmGroup.SolveJob("cal_" + label + "_ampl",
                           "Calibrate %s amplitudes" % label, self.pg_ampl)

        return nodes
Ejemplo n.º 3
0
def make_source (ns,name,l,m,I=1):
  """Makes a source with a direction, using the current option set. 
  Returns None for sources out of the "sky". 
  (l^2+m^2>1)""";
  l = math.sin(l);
  m = math.sin(m);
  if l*l + m*m <= 1:
    srcdir = Meow.LMDirection(ns,name,l,m);
    if source_spi is not None:
      freq0 = source_freq0*1e+6 if source_freq0 else Meow.Context.observation.freq0();
      spi = source_spi if source_spi_2 is None else [ source_spi,source_spi_2 ];
    else:
      spi = freq0 = None;
    if source_pol:
      Q = I*source_qi;
      U = I*source_ui;
      V = I*source_vi;
    else:
      Q = U = V = None;
    if source_type == "point":
      return Meow.PointSource(ns,name,srcdir,I=I,Q=Q,U=U,V=V,spi=spi,freq0=freq0);
    elif source_type == "gaussian":
      s1,s2 = gauss_smaj*ARCSEC,gauss_smin*ARCSEC;
      pa = gauss_pa*DEG;
      return Meow.GaussianSource(ns,name,srcdir,I=I,Q=Q,U=U,V=V,spi=spi,freq0=freq0,
          lproj=s1*math.sin(pa),mproj=s1*math.cos(pa),ratio=s2/s1);
  # else fall through and return none
  return None;
  def init_nodes (self,ns,tags=None,label=''):
    ifrs = Context.array.ifrs();
    G = ns.gain;
    if not G(*ifrs[0]).initialized():
      def1 = Meow.Parm(1,tags=tags);
      def0 = Meow.Parm(0,tags=tags);
      gains = [];
      for p,q in ifrs:
        gg = [];
        for corr in Context.correlations:
          if corr in Context.active_correlations:
            cc = corr.lower();
            gain_ri  = [ resolve_parameter(cc,G(p,q,cc,'r'),def1,tags="ifr gain real"),
                        resolve_parameter(cc,G(p,q,cc,'i'),def0,tags="ifr gain imag") ];
            gg.append(G(p,q,cc) << Meq.ToComplex(*gain_ri));
            gains += gain_ri;
          else:
            gg.append(1);
        G(p,q) << Meq.Matrix22(*gg);

      pg_ifr_ampl = ParmGroup.ParmGroup(label,gains,
                            individual_toggles=False,
                            table_name="%s.fmep"%label,bookmark=False);
      Bookmarks.make_node_folder("%s (interferometer-based gains)"%label,
        [ G(p,q) for p,q in ifrs ],sorted=True,nrow=2,ncol=2);
      ParmGroup.SolveJob("cal_%s"%label,
                        "Calibrate %s (interferometer-based gains)"%label,pg_ifr_ampl);
    return G;
Ejemplo n.º 5
0
def transient_source (ns,name,l,m,tburst,duration,Ipeak=1):
  """shortcut for making a transient pointsource with a direction. Returns None for sources out of the "sky"
  (l^2+m^2>1)""";
  l = math.sin(l);
  m = math.sin(m);
  t_rel = ns.t_rel ** (Meq.Time() - (ns.time0 << 0))
  lc = ns.lc << Meq.Exp((-Meq.Pow(t_rel-tburst,2))/(2*duration**2))
  Ilc = ns.Ilc << Ipeak * lc
  if l*l + m*m <= 1:
    srcdir = Meow.LMDirection(ns,name,l,m);
    return Meow.PointSource(ns,name,srcdir,I=Ilc);
  else:
    return None;
Ejemplo n.º 6
0
def point_source(ns, name, l, m, I=1):
    """shortcut for making a pointsource with a direction. Returns None for sources out of the "sky"
    (l^2+m^2>1)"""
    l = math.sin(l)
    m = math.sin(m)
    if l * l + m * m <= 1:
        srcdir = Meow.LMDirection(ns, name, l, m)
        if source_spi is not None:
            freq0 = source_freq0 or Meow.Context.observation.freq0()
        else:
            freq0 = None
        return Meow.PointSource(ns, name, srcdir, I=I, spi=source_spi, freq0=freq0)
    else:
        return None
Ejemplo n.º 7
0
def make_source(ns, name, az, el, I=1):
    """Makes a source with a fixed azimuth/elevation direction """
    srcdir = Meow.AzElDirection(ns, name, az, el)
    if source_spi is not None:
        freq0 = source_freq0 * 1e+6 if source_freq0 else Meow.Context.observation.freq0(
        )
    else:
        freq0 = None
    if source_pol:
        Q = I * source_qi
        U = I * source_ui
        V = I * source_vi
    else:
        Q = U = V = None
    if source_type == "point":
        return Meow.PointSource(ns,
                                name,
                                srcdir,
                                I=I,
                                Q=Q,
                                U=U,
                                V=V,
                                spi=source_spi,
                                freq0=freq0)
    elif source_type == "gaussian":
        s1, s2 = gauss_smaj * ARCSEC, gauss_smin * ARCSEC
        pa = gauss_pa * DEG
        return Meow.GaussianSource(ns,
                                   name,
                                   srcdir,
                                   I=I,
                                   Q=Q,
                                   U=U,
                                   V=V,
                                   spi=source_spi,
                                   freq0=freq0,
                                   lproj=s1 * math.sin(pa),
                                   mproj=s1 * math.cos(pa),
                                   ratio=s2 / s1)
    # else fall through and return none
    return None
Ejemplo n.º 8
0
def _define_forest (ns):

 # create Array object
  num_antennas = 36   # for ASKAP simulation
  xntd_list = [ str(i) for i in range(1,num_antennas+1) ];
  array = Meow.IfrArray(ns,xntd_list,ms_uvw=True);
  # create an Observation object
  observation = Meow.Observation(ns);
  # set global context
  Meow.Context.set(array=array,observation=observation);

  # create a source model and make list of corrupted sources
  allsky = Meow.Patch(ns,'all',observation.phase_centre);
  sources = sky_models.make_model(ns,"S");
  for src in sources:
    lm = src.direction.lm();
    E = ns.E(src.name);
    for p in array.stations():
      pa= ns.ParAngle(p) << Meq.ParAngle(observation.phase_centre.radec(), array.xyz(p))
      ns.CosPa(p) << Meq.Cos(pa)
      ns.SinPa(p) << Meq.Sin(pa)
      ns.rot_matrix(p) << Meq.Matrix22(ns.CosPa(p),-1.0 * ns.SinPa(p),ns.SinPa(p),ns.CosPa(p))
      # compute "apparent" position of source per each antenna
      lm_rot=ns.lm_rot(src.name,p) << Meq.MatrixMultiply(ns.rot_matrix(p),lm) 
      # compute E for apparent position
      tdp_voltage_response(ns,src,p,E(p),lm_rot);
    allsky.add(src.corrupt(E));

  observed = allsky.visibilities();

  # make some useful inspectors. Collect them into a list, since we need
  # to give a list of 'post' nodes to make_sinks() below
  pg = Bookmarks.Page("Inspectors",1,2);
  inspectors = [];
  inspectors.append(
    Meow.StdTrees.vis_inspector(ns.inspect_observed,observed) );
  pg.add(ns.inspect_observed,viewer="Collections Plotter");

  Meow.StdTrees.make_sinks(ns,observed,spigots=False,post=inspectors);
Ejemplo n.º 9
0
def source_list (ns,name="cps"):
  # figure out spectral index parameters
  if spectral_index is not None:
    spi_def = Meow.Parm(spectral_index);
    freq0_def  = ref_freq;
  else:
    spi_def = freq0_def = None;
  i_def = Meow.Parm(1);
  quv_def = Meow.Parm(0);

  srcdir = Meow.LMDirection(ns,name,0,0);
  src = Meow.PointSource(ns,name,srcdir,I=i_def,Q=quv_def,U=quv_def,V=quv_def,spi=spi_def,freq0=freq0_def);

  ## define a parmgroup for source parameters
  ## now make a solvejobs for the source
  #pg_src = ParmGroup("source",
              #src.coherency().search(tags="solvable"),
              #table_name="sources.mep",
              #individual=True,
              #bookmark=True);
  ## now make a solvejobs for the source
  #options.append(pg_src.make_solvejob_menu("Calibrate source fluxes"));

  return [ src ];
Ejemplo n.º 10
0
    def __init__(self,
                 ns,
                 name,
                 sources,
                 stations=None,
                 height=None,
                 make_log=False):
        MIM_model.__init__(self, ns, name, sources, stations)
        if isinstance(height, Meow.Parm):
            self._height = height
        else:
            self._height = Meow.Parm(height)

        self.ns.h << self._height.make()
        self._make_log = make_log
Ejemplo n.º 11
0
    def __init__(self,
                 ns,
                 name,
                 sources,
                 stations=None,
                 ref_station=None,
                 tags="iono",
                 make_log=False):
        PiercePoints.PiercePoints.__init__(self, ns, name, sources, stations,
                                           height, make_log)
        self.ref_station = ref_station

        for nlo in range(N_long):
            for nla in range(N_lat):
                name = "N:" + str(nlo) + ":" + str(nla)
                self._add_parm(name=name, value=Meow.Parm(0.), tags=tags)
Ejemplo n.º 12
0
 def __init__(self,
              ns,
              name,
              sources,
              stations=None,
              ref_station=None,
              tags="iono",
              make_log=False):
     PiercePoints.__init__(self, ns, name, sources, stations, height,
                           make_log)
     self.ref_station = ref_station
     for i in range(rank):
         name = "KLParm:" + str(i)
         self._add_parm(name=name,
                        value=Meow.Parm(0, tiling=record(time=1)),
                        tags=tags)
Ejemplo n.º 13
0
    def compute_jones(self,
                      nodes,
                      stations=None,
                      tags=None,
                      label='',
                      inspectors=[],
                      **kw):
        stations = stations or Context.array.stations()
        rot_def = Meow.Parm(0)
        nr = Jones.rotation_matrix(nodes("R"),
                                   rot_def,
                                   tags=tags,
                                   series=stations)
        ne = Jones.ellipticity_matrix(nodes("E"),
                                      rot_def,
                                      tags=tags,
                                      series=stations)

        for p in stations:
            nodes(p) << Meq.MatrixMultiply(nr(p), ne(p))

        # make parmgroups for phases and gains
        self.pg_rot = ParmGroup.ParmGroup(label + "_leakage",
                                          nodes.search(tags="solvable"),
                                          table_name="%s_leakage.mep" % label,
                                          bookmark=True)

        # make solvejobs
        ParmGroup.SolveJob("cal_" + label + "_leakage",
                           "Calibrate %s (leakage)" % label, self.pg_rot)

        # make inspector for parameters
        StdTrees.inspector(nodes('inspector'),
                           self.pg_rot.nodes,
                           bookmark=False)
        inspectors.append(nodes('inspector'))

        return nodes
Ejemplo n.º 14
0
def _define_forest(ns, parent=None, **kw):
    if run_purr:
        Timba.TDL.GUI.purr(mssel.msname + ".purrlog", [mssel.msname, '.'])
    # create Purr pipe
    global purrpipe
    purrpipe = Purr.Pipe.Pipe(mssel.msname)

    # get antennas from MS
    ANTENNAS = mssel.get_antenna_set(list(range(1, 15)))
    array = Meow.IfrArray(ns, ANTENNAS, mirror_uvw=False)
    stas = array.stations()
    # get phase centre from MS, setup observation
    observation = Meow.Observation(ns,
                                   phase_centre=mssel.get_phase_dir(),
                                   linear=mssel.is_linear_pol(),
                                   circular=mssel.is_circular_pol())
    Meow.Context.set(array, observation)
    # get active correlations from MS
    Meow.Context.active_correlations = mssel.get_correlations()

    # make spigot nodes
    spigots = spigots0 = outputs = array.spigots(corr=mssel.get_corr_index())

    # ...and an inspector for them
    StdTrees.vis_inspector(ns.inspector('input'),
                           spigots,
                           bookmark="Inspect input visibilities")
    inspectors = [ns.inspector('input')]
    Bookmarks.make_node_folder("Input visibilities by baseline",
                               [spigots(p, q) for p, q in array.ifrs()],
                               sorted=True,
                               ncol=2,
                               nrow=2)

    inspect_ifrs = array.ifrs()
    if do_solve:
        # filter solvable baselines by baseline length
        solve_ifrs = []
        antpos = mssel.ms_antenna_positions
        if (min_baseline or max_baseline) and antpos is not None:
            for (ip, p), (iq, q) in array.ifr_index():
                baseline = math.sqrt(
                    ((antpos[ip, :] - antpos[iq, :])**2).sum())
                if (not min_baseline or baseline > min_baseline) and \
                   (not max_baseline or baseline < max_baseline):
                    solve_ifrs.append((p, q))
        else:
            solve_ifrs = array.ifrs()
        inspect_ifrs = solve_ifrs

    # make a predict tree using the MeqMaker
    if do_solve or do_subtract:
        predict = meqmaker.make_predict_tree(ns)
        # make a ParmGroup and solve jobs for source parameters, if we have any
        if do_solve:
            parms = {}
            for src in meqmaker.get_source_list(ns):
                parms.update([(p.name, p) for p in src.get_solvables()])
            if parms:
                pg_src = ParmGroup.ParmGroup("source",
                                             list(parms.values()),
                                             table_name="sources.fmep",
                                             individual=True,
                                             bookmark=True)
                # now make a solvejobs for the source
                ParmGroup.SolveJob("cal_source", "Calibrate source model",
                                   pg_src)

    # make nodes to compute residuals
    if do_subtract:
        residuals = ns.residuals
        for p, q in array.ifrs():
            residuals(p, q) << spigots(p, q) - predict(p, q)
        outputs = residuals

    # and now we may need to correct the outputs
    if do_correct:
        if do_correct_sky:
            srcs = meqmaker.get_source_list(ns)
            sky_correct = srcs and srcs[0]
        else:
            sky_correct = None
        outputs = meqmaker.correct_uv_data(ns,
                                           outputs,
                                           sky_correct=sky_correct,
                                           inspect_ifrs=inspect_ifrs)

    # make solve trees
    if do_solve:
        # inputs to the solver are based on calibration type
        # if calibrating visibilities, feed them to condeq directly
        if cal_type == CAL.VIS:
            observed = spigots
            model = predict
        # else take ampl/phase component
        else:
            model = ns.model
            observed = ns.observed
            if cal_type == CAL.AMPL:
                for p, q in array.ifrs():
                    observed(p, q) << Meq.Abs(spigots(p, q))
                    model(p, q) << Meq.Abs(predict(p, q))
            elif cal_type == CAL.LOGAMPL:
                for p, q in array.ifrs():
                    observed(p, q) << Meq.Log(Meq.Abs(spigots(p, q)))
                    model(p, q) << Meq.Log(Meq.Abs(predict(p, q)))
            elif cal_type == CAL.PHASE:
                for p, q in array.ifrs():
                    observed(p, q) << 0
                    model(p, q) << Meq.Abs(predict(p, q)) * Meq.FMod(
                        Meq.Arg(spigots(p, q)) - Meq.Arg(predict(p, q)),
                        2 * math.pi)
            else:
                raise ValueError("unknown cal_type setting: " + str(cal_type))
        # make a solve tree
        solve_tree = StdTrees.SolveTree(ns, model, solve_ifrs=solve_ifrs)
        # the output of the sequencer is either the residuals or the spigots,
        # according to what has been set above
        outputs = solve_tree.sequencers(inputs=observed, outputs=outputs)

    # make sinks and vdm.
    # The list of inspectors must be supplied here
    inspectors += meqmaker.get_inspectors() or []
    StdTrees.make_sinks(ns, outputs, spigots=spigots0, post=inspectors)
    Bookmarks.make_node_folder("Corrected/residual visibilities by baseline",
                               [outputs(p, q) for p, q in array.ifrs()],
                               sorted=True,
                               ncol=2,
                               nrow=2)

    if not do_solve:
        if do_subtract:
            name = "Generate residuals"
            comment = "Generated residual visibilities."
        elif do_correct:
            name = "Generate corrected data"
            comment = "Generated corrected visibilities."
        else:
            name = None
        if name:
            # make a TDL job to runsthe tree
            def run_tree(mqs, parent, **kw):
                global tile_size
                purrpipe.title("Calibrating").comment(comment)
                mqs.execute(Meow.Context.vdm.name,
                            mssel.create_io_request(tile_size),
                            wait=False)

            TDLRuntimeMenu(
                name,
                TDLOption(
                    'tile_size',
                    "Tile size, in timeslots", [10, 60, 120, 240],
                    more=int,
                    doc=
                    """Input data is sliced by time, and processed in chunks (tiles) of
                  the indicated size. Larger tiles are faster, but use more memory."""
                ), TDLRuntimeJob(run_tree, name))

    # very important -- insert meqmaker's runtime options properly
    # this should come last, since runtime options may be built up during compilation.
    TDLRuntimeOptions(*meqmaker.runtime_options(nest=False))
    # insert solvejobs
    if do_solve:
        TDLRuntimeOptions(*ParmGroup.get_solvejob_options())
    # finally, setup imaging options
    imsel = mssel.imaging_selector(npix=512,
                                   arcmin=meqmaker.estimate_image_size())
    TDLRuntimeMenu("Make an image from this MS", *imsel.option_list())

    # and close meqmaker -- this exports annotations, etc
    meqmaker.close()
Ejemplo n.º 15
0
def _define_forest(ns):
    # make pynodes, xyzcomponent for sources
    ANTENNAS = mssel.get_antenna_set(list(range(1, 15)))
    array = Meow.IfrArray(ns, ANTENNAS, mirror_uvw=False)
    observation = Meow.Observation(ns)
    Meow.Context.set(array, observation)
    # make a predict tree using the MeqMaker
    if do_solve or do_subtract or not do_not_simulate:
        outputs = predict = meqmaker.make_tree(ns)

    # make a list of selected corrs
    selected_corrs = cal_corr.split(" ")

    # make spigot nodes
    if not do_not_simulate and do_add:
        spigots = spigots0 = outputs = array.spigots()
        sums = ns.sums
        for p, q in array.ifrs():
            sums(p, q) << spigots(p, q) + predict(p, q)
        outputs = sums

    # make spigot nodes
    if do_not_simulate:
        spigots = spigots0 = outputs = array.spigots()
        # make nodes to compute residuals

        # make nodes to compute residuals
        if do_subtract:
            residuals = ns.residuals
            for p, q in array.ifrs():
                residuals(p, q) << spigots(p, q) - predict(p, q)
            outputs = residuals

        # and now we may need to correct the outputs
        if do_correct:
            if do_correct_sky:
                if src_name:
                    sky_correct = src_name
                else:
                    srcs = meqmaker.get_source_list(ns)
                    sky_correct = srcs and srcs[0]

            else:
                sky_correct = None
            outputs = meqmaker.correct_uv_data(ns,
                                               outputs,
                                               sky_correct=sky_correct)

        # make solve trees
        if do_solve:
            # extract selected correlations
            if cal_corr != ALL_CORRS:
                index = [CORR_INDICES[c] for c in selected_corrs]
                for p, q in array.ifrs():
                    ns.sel_predict(p, q) << Meq.Selector(
                        predict(p, q), index=index, multi=True)
                    ns.sel_spigot(p, q) << Meq.Selector(
                        spigots(p, q), index=index, multi=True)
                spigots = ns.sel_spigot
                predict = ns.sel_predict
            model = predict
            observed = spigots

            # make a solve tree
            solve_tree = Meow.StdTrees.SolveTree(ns, model)
            # the output of the sequencer is either the residuals or the spigots,
            # according to what has been set above
            outputs = solve_tree.sequencers(inputs=observed, outputs=outputs)

    # throw in a bit of noise
    if not do_not_simulate and noise_stddev:
        # make two complex noise terms per station (x/y)
        noisedef = Meq.GaussNoise(stddev=noise_stddev)
        noise_x = ns.sta_noise('x')
        noise_y = ns.sta_noise('y')
        for p in array.stations():
            noise_x(p) << Meq.ToComplex(noisedef, noisedef)
            noise_y(p) << Meq.ToComplex(noisedef, noisedef)
        # now combine them into per-baseline noise matrices
        for p, q in array.ifrs():
            noise = ns.noise(p, q) << Meq.Matrix22(
                noise_x(p) + noise_x(q),
                noise_x(p) + noise_y(q),
                noise_y(p) + noise_x(q),
                noise_y(p) + noise_y(q))
            ns.noisy_predict(p, q) << outputs(p, q) + noise
        outputs = ns.noisy_predict
    # make sinks and vdm.
    # The list of inspectors comes in handy here
    Meow.StdTrees.make_sinks(ns,
                             outputs,
                             spigots=None,
                             post=meqmaker.get_inspectors())

    if not do_not_simulate:
        # add simulate job
        TDLRuntimeJob(job_simulate, "Simulate")
    if do_not_simulate and not do_solve:
        # add subtract or correct job
        TDLRuntimeJob(job_subtract, "Subtract or Correct the data")

    if do_not_simulate and do_solve:
        pg_iono = ParmGroup.ParmGroup("Z_iono",
                                      outputs.search(tags="solvable Z"),
                                      table_name="iono.mep",
                                      bookmark=4)
        ParmGroup.SolveJob("cal_iono", "Calibrate Ionosphere parameters ",
                           pg_iono)

    # very important -- insert meqmaker's runtime options properly
    # this should come last, since runtime options may be built up during compilation.
    # TDLRuntimeOptions(*meqmaker.runtime_options(nest=False));
    # and insert all solvejobs
    TDLRuntimeOptions(*ParmGroup.get_solvejob_options())
    # finally, setup imaging options
    imsel = mssel.imaging_selector(npix=512,
                                   arcmin=meqmaker.estimate_image_size())
    TDLRuntimeMenu("Imaging options", *imsel.option_list())
Ejemplo n.º 16
0
def source_list(ns):
    # figure out spectral index parameters
    if spectral_index is not None:
        spi_def = Meow.Parm(spectral_index)
        freq0_def = ref_freq
    else:
        spi_def = freq0_def = None
    if spectral_index1 is not None:
        spi_def1 = Meow.Parm(spectral_index1)
        freq0_def1 = ref_freq
    else:
        spi_def1 = freq0_def1 = None
    # and flux parameters
    i_def = Meow.Parm(1)
    quv_def = Meow.Parm(0)

    dir1 = Meow.Direction(ns, "3C343.1", 4.356645791155902, 1.092208429052697)
    dir0 = Meow.Direction(ns, "3C343", 4.3396003966265599, 1.0953677174056471)

    src1 = Meow.PointSource(ns,
                            "3C343.1",
                            dir1,
                            I=Meow.Parm(6.02061051),
                            Q=Meow.Parm(0.0179716185),
                            U=quv_def,
                            V=quv_def,
                            spi=spi_def1,
                            freq0=freq0_def1)
    src0 = Meow.PointSource(ns,
                            "3C343",
                            dir0,
                            I=Meow.Parm(1.83336309),
                            Q=Meow.Parm(0.0241450607),
                            U=quv_def,
                            V=quv_def,
                            spi=spi_def1,
                            freq0=freq0_def1)

    ## define a parmgroup for source parameters
    pg_src = ParmGroup.ParmGroup("source",
                                 src1.coherency().search(tags="solvable") +
                                 src0.coherency().search(tags="solvable"),
                                 table_name="sources.mep")

    ParmGroup.SolveJob("cal_sources", "Calibrate sources", pg_src)

    return [src1, src0]
Ejemplo n.º 17
0
    def source_list(self, ns, max_sources=None, **kw):
        """Reads LSM and returns a list of Meow objects.
        ns is node scope in which they will be created.
        Keyword arguments may be used to indicate which of the source attributes are to be
        created as Parms, use e.g. I=Meow.Parm(tags="flux") for this.
        The use_parms option may override this.
        """
        if self.filename is None:
            return []
        # load the sky model
        if self.lsm is None:
            self.lsm = Tigger.load(self.filename)

        # sort by brightness
        import functools
        from past.builtins import cmp
        from functools import cmp_to_key
        sources = sorted(
            self.lsm.sources,
            key=cmp_to_key(lambda a, b: cmp(b.brightness(), a.brightness())))

        # extract subset, if specified
        sources = SourceSubsetSelector.filter_subset(self.lsm_subset, sources,
                                                     self._getTagValue)
        # get nulls subset
        if self.null_subset:
            nulls = set([
                src.name for src in SourceSubsetSelector.filter_subset(
                    self.null_subset, sources)
            ])
        else:
            nulls = set()
        parm = Meow.Parm(tags="source solvable")
        # make copy of kw dict to be used for sources not in solvable set
        kw_nonsolve = dict(kw)
        # and update kw dict to be used for sources in solvable set
        # this will be a dict of lists of solvable subgroups
        parms = []
        subgroups = {}
        if self.solvable_sources:
            subgroup_order = []
            for sgname in _SubgroupOrder:
                if getattr(self, 'solve_%s' % sgname):
                    sg = subgroups[sgname] = []
                    subgroup_order.append(sgname)

        # make Meow list
        source_model = []

        for src in sources:
            is_null = src.name in nulls
            # this will be True if this source has solvable parms
            solvable = self.solvable_sources and not is_null and (
                not self.lsm_solvable_tag
                or getattr(src, self.lsm_solvable_tag, False))
            if solvable:
                # independent groups?
                if self.lsm_solve_group_tag:
                    independent_sg = sgname = "%s:%s" % (
                        self.lsm_solve_group_tag,
                        getattr(src, self.lsm_solve_group_tag, "unknown"))
                else:
                    independent_sg = ""
                    sgname = 'source:%s' % src.name
                if sgname in subgroups:
                    sgsource = subgroups[sgname]
                else:
                    sgsource = subgroups[sgname] = []
                    subgroup_order.append(sgname)
            # make dict of source parametrs: for each parameter we have a value,subgroup pair
            if is_null:
                attrs = dict(ra=src.pos.ra,
                             dec=src.pos.dec,
                             I=0,
                             Q=None,
                             U=None,
                             V=None,
                             RM=None,
                             spi=None,
                             freq0=None)
            else:
                attrs = dict(
                    ra=src.pos.ra,
                    dec=src.pos.dec,
                    I=src.flux.I,
                    Q=getattr(src.flux, 'Q', None),
                    U=getattr(src.flux, 'U', None),
                    V=getattr(src.flux, 'V', None),
                    RM=getattr(src.flux, 'rm', None),
                    freq0=getattr(src.flux, 'freq0', None)
                    or (src.spectrum and getattr(src.spectrum, 'freq0', None)),
                    spi=src.spectrum and getattr(src.spectrum, 'spi', None))
            if not is_null and isinstance(src.shape, ModelClasses.Gaussian):
                attrs['lproj'] = src.shape.ex * math.sin(src.shape.pa)
                attrs['mproj'] = src.shape.ex * math.cos(src.shape.pa)
                attrs['ratio'] = src.shape.ey / src.shape.ex
            # construct parms or constants for source attributes, depending on whether the source is solvable or not
            # If source is solvable and this particular attribute is solvable, replace
            # value in attrs dict with a Meq.Parm.
            if solvable:
                for parmname, value in list(attrs.items()):
                    sgname = _Subgroups.get(parmname, None)
                    if sgname in subgroups:
                        solvable = True
                        parm = attrs[parmname] = ns[src.name](
                            parmname) << Meq.Parm(value or 0,
                                                  tags=["solvable", sgname],
                                                  solve_group=independent_sg)
                        subgroups[sgname].append(parm)
                        sgsource.append(parm)
                        parms.append(parm)

            # construct a direction
            direction = Meow.Direction(ns,
                                       src.name,
                                       attrs['ra'],
                                       attrs['dec'],
                                       static=not solvable
                                       or not self.solve_pos)

            # construct a point source or gaussian or FITS image, depending on source shape class
            if src.shape is None or is_null:
                msrc = Meow.PointSource(ns,
                                        name=src.name,
                                        I=attrs['I'],
                                        Q=attrs['Q'],
                                        U=attrs['U'],
                                        V=attrs['V'],
                                        direction=direction,
                                        spi=attrs['spi'],
                                        freq0=attrs['freq0'],
                                        RM=attrs['RM'])
            elif isinstance(src.shape, ModelClasses.Gaussian):
                msrc = Meow.GaussianSource(ns,
                                           name=src.name,
                                           I=attrs['I'],
                                           Q=attrs['Q'],
                                           U=attrs['U'],
                                           V=attrs['V'],
                                           direction=direction,
                                           spi=attrs['spi'],
                                           freq0=attrs['freq0'],
                                           lproj=attrs['lproj'],
                                           mproj=attrs['mproj'],
                                           ratio=attrs['ratio'])
                if solvable and 'shape' in subgroups:
                    subgroups['pos'] += direction.get_solvables()
            elif isinstance(src.shape, ModelClasses.FITSImage):
                msrc = Meow.FITSImageComponent(ns,
                                               name=src.name,
                                               filename=src.shape.filename,
                                               direction=direction)
                msrc.set_options(fft_pad_factor=(src.shape.pad or 2))

            msrc.solvable = solvable

            # copy standard attributes from sub-objects
            for subobj in src.flux, src.shape, src.spectrum:
                if subobj:
                    for attr, val in src.flux.getAttributes():
                        msrc.set_attr(attr, val)
            # copy all extra attrs from source object
            for attr, val in src.getExtraAttributes():
                msrc.set_attr(attr, val)

            # make sure Iapp exists (init with I if it doesn't)
            if msrc.get_attr('Iapp', None) is None:
                msrc.set_attr('Iapp', src.flux.I)

            source_model.append(msrc)

        # if any solvable parms were made, make a parmgroup and solve job for them
        if parms:
            if os.path.isdir(self.filename):
                table_name = os.path.join(self.filename, "sources.fmep")
            else:
                table_name = os.path.splitext(self.filename)[0] + ".fmep"
            # make list of Subgroup objects for every non-empty subgroup
            sgs = []
            for sgname in subgroup_order:
                sglist = subgroups.get(sgname, None)
                if sglist:
                    sgs.append(Meow.ParmGroup.Subgroup(sgname, sglist))
            # make main parm group
            pg_src = Meow.ParmGroup.ParmGroup("source parameters",
                                              parms,
                                              subgroups=sgs,
                                              table_name=table_name,
                                              table_in_ms=False,
                                              bookmark=True)
            # now make a solvejobs for the source
            Meow.ParmGroup.SolveJob("cal_source",
                                    "Solve for source parameters", pg_src)

        return source_model
def compute_jones(Jones,
                  sources,
                  stations=None,
                  inspectors=[],
                  meqmaker=None,
                  label='R',
                  **kw):
    """Creates the Z Jones for ionospheric phase, given TECs (per source, 
  per station)."""
    stations = stations or Context.array.stations
    ns = Jones.Subscope()

    # get reference source
    if ref_source:
        # treat as index first
        dir0 = None
        try:
            dir0 = sources[int(ref_source)].direction
        except:
            pass
        # else treat as name, find in list
        if not dir0:
            for src0 in sources:
                if src0.name == ref_source:
                    dir0 = src0.direction
                    break
        # else treat as direction string
        if not dir0:
            ff = list(ref_source.split())
            if len(ff) < 2 or len(ff) > 3:
                raise RuntimeError(
                    "invalid reference dir '%s' specified for %s-Jones" %
                    (ref_source, label))
            global dm
            if not dm:
                raise RuntimeError(
                    "pyrap measures module not available, cannot use direction strings for %s-Jones"
                    % label)
            if len(ff) == 2:
                ff = ['J2000'] + ff
            # treat as direction measure
            try:
                dmdir = dm.direction(*ff)
            except:
                raise RuntimeError(
                    "invalid reference dir '%s' specified for %s-Jones" %
                    (ref_source, label))
            # convert to J2000 and make direction object
            dmdir = dm.measure(dmdir, 'J2000')
            ra, dec = dm.getvalue(dmdir)[0].get_value(), dm.getvalue(
                dmdir)[1].get_value()
            dir0 = Meow.Direction(ns, "refdir", ra, dec, static=True)
    else:
        dir0 = Context.observation.phase_centre

    # make refraction scale node
    scale = ns.scale(0) << Meq.Parm(0, tags="refraction")

    xyz0 = Context.array.xyz0()
    if coord_approx:
        # get PA, and assume it's the same over the whole field
        pa = ns.pa0 << Meq.ParAngle(dir0.radec(), xyz0)
        # second column of the Rot(-PA) matrix. Multiply this by del to get a rotation of (0,del) into the lm plane.
        # The third component (0) is for convenience, as it immediately gives us dl,dm,dn, since we assume dn~0
        rot_pa = ns.rotpa0 << Meq.Composer(Meq.Sin(pa), Meq.Cos(pa), 0)

    # el0: elevation of field centre
    el0 = dir0.el()
    if do_extinction:
        ns.inv_ext0 << Meq.Sin(el0)
        # inverse of extinction towards el0
    # station UVWs
    uvw = Context.array.uvw()
    # now loop over sources
    for isrc, src in enumerate(sources):
        # reference direction: no refraction at all
        if src.direction is dir0:
            for p in stations:
                Jones(src, p) << 1
            continue
        # dEl is source elevation minus el0
        # ddEl = scale*dEl: amount by which source refracts (negative means field is compressed)
        el = src.direction.el()
        ns.dEl(src) << el - el0
        ddel = ns.ddEl(src) << ns.dEl(src) * scale
        # get el1: refracted elevation angle
        if not coord_approx or do_extinction:
            el1 = ns.el1(src) << el + ddel
        # compute extinction component
        if do_extinction:
            # compute inverse of extinction towards the refracted direction el1
            iext = ns.inv_ext(src) << Meq.Sin(el1)
            #
            # and differential extinction is then ext1/ext0
            ext = ns.dext(src) << ns.inv_ext0 / iext
        # Compute dlmn offset in lm plane.
        if coord_approx:
            # Approximate mode: ddel is added to elevation, so to get the lm offset, we need
            # to apply Rot(PA) to the column vector (0,ddel), and then take the sine of the result.
            dlmn = ns.dlmn(src) << Meq.Sin(ddel * rot_pa)
        else:
            ns.azel1(src) << Meq.Composer(src.direction.az(), el1)
            ns.radec1(src) << Meq.RADec(ns.azel1(src), xyz0)
            ns.lmn1(src) << Meq.LMN(Context.observation.radec0(),
                                    ns.radec1(src))
            dlmn = ns.dlmn(src) << ns.lmn1(src) - src.lmn()
        # get per-station phases
        for p in stations:
            if do_extinction:
                Jones(src, p) << ext * (ns.phase(src, p) << Meq.VisPhaseShift(
                    lmn=dlmn, uvw=uvw(p)))
            else:
                Jones(src, p) << Meq.VisPhaseShift(lmn=dlmn, uvw=uvw(p))
    # make bookmarks
    srcnames = [src.name for src in sources]
    meqmaker.make_bookmark_set(Jones, [(src, p) for src in srcnames
                                       for p in stations],
                               "%s: inspector plot" % label,
                               "%s: by source-station" % label,
                               freqmean=True)
    inspectors.append(ns.inspector(label,'scale') << \
        StdTrees.define_inspector(ns.scale,[0],label=label))
    inspectors.append(ns.inspector(label,'delta-el') << \
        StdTrees.define_inspector(ns.ddEl,srcnames,label=label))
    inspectors.append(ns.inspector(label,'delta-el') << \
        StdTrees.define_inspector(ns.ddEl,srcnames,label=label))
    inspectors.append(ns.inspector(label,'dlmn') << \
        StdTrees.define_inspector(ns.dlmn,srcnames,label=label))
    if do_extinction:
        inspectors.append(ns.inspector(label,'inv-ext') << \
            StdTrees.define_inspector(ns.inv_ext,srcnames,label=label))
        inspectors.append(ns.inspector(label,'diff-ext') << \
            StdTrees.define_inspector(ns.dext,srcnames,label=label))

    # make parmgroups and solvejobs
    global pg
    pg = ParmGroup.ParmGroup(label, [scale],
                             table_name="%s.fmep" % label,
                             bookmark=False)

    # make solvejobs
    ParmGroup.SolveJob("cal_" + label,
                       "Calibrate %s (differential refraction)" % label, pg)

    return Jones
Ejemplo n.º 19
0
import Timba.dmi
import re
import tempfile
import os

import Meow
import Meow.MSUtils
import Purr.Pipe
import Timba.Apps

from Meow.MSUtils import TABLE
    
_gli = Meow.MSUtils.find_exec('glish');
if _gli:
  _GLISH = 'glish';
  Meow.dprint("Calico flagger: found %s, autoflag should be available"%_gli);
else:
  _GLISH = None;
  Meow.dprint("Calico flagger: glish not found, autoflag will not be available");

_addbitflagcol = Meow.MSUtils.find_exec('addbitflagcol');

# Various argument-formatting methods to use with the Flagger.AutoFlagger class
# below. These really should be static methods of the class, but that doesn't work
# with Python (specifically, I cannot include them into static member dicts)
def _format_nbins (bins,argname):
  if isinstance(bins,(list,tuple)) and len(bins) == 2:
    return str(list(bins));
  else:
    return "%d"%bins;
  raise TypeError,"invalid value for '%s' keyword (%s)"%(argname,bins);
Ejemplo n.º 20
0
def _define_forest(ns):
    ANTENNAS = mssel.get_antenna_set(list(range(1, 15)))
    array = Meow.IfrArray(ns, ANTENNAS, mirror_uvw=False)
    observation = Meow.Observation(ns)
    Meow.Context.set(array, observation)

    outputs = spigots = array.spigots(corr=mssel.get_corr_index(), flag_bit=1)
    Meow.Bookmarks.make_node_folder("Input visibilities by baseline",
                                    [spigots(p, q) for p, q in array.ifrs()],
                                    sorted=True,
                                    ncol=2,
                                    nrow=2)

    # extract xx/yy if asked
    if flag_xx_yy:
        outputs = ns.xxyy
        for p, q in array.ifrs():
            outputs(p, q) << Meq.Selector(
                spigots(p, q), index=[0, 3], multi=True)

    # add freq averaging if needed
    if avg_freq:
        for p, q in array.ifrs():
            ns.freqavg(p, q) << Meq.Mean(outputs(p, q),
                                         reduction_axes=['freq'])
        outputs = ns.freqavg

    # make an inspector for spigots, we'll add more to this list
    inspectors = [
        Meow.StdTrees.vis_inspector(ns.inspect('spigots'),
                                    spigots,
                                    bookmark=False)
    ]

    # flag on absolute value first
    if flag_absmax is not None or flag_absmin is not None:
        outputs = abs_clip(outputs, flag_absmax, flag_absmin)
        inspectors.append(
            Meow.StdTrees.vis_inspector(ns.inspect('abs'),
                                        outputs,
                                        bookmark=False))

    # then flag on rms
    if flag_rms is not None:
        outputs = rms_clip(outputs, flag_rms)
        inspectors.append(
            Meow.StdTrees.vis_inspector(ns.inspect('rms'),
                                        outputs,
                                        bookmark=False))

    # recreate 2x2 result (if only flagging on xx/yy)
    if flag_xx_yy:
        for p, q in array.ifrs():
            out = outputs(p, q)
            xx = ns.xx_out(p, q) << Meq.Selector(out, index=0)
            yy = ns.yy_out(p, q) << Meq.Selector(out, index=1)
            ns.make4corr(p, q) << Meq.Matrix22(xx, 0, 0, yy)
        outputs = ns.make4corr

    # merge flags across correlations if asked
    if flag_all_corrs:
        for p, q in array.ifrs():
            ns.mergecorrflags(p, q) << Meq.MergeFlags(outputs(p, q))
        outputs = ns.mergecorrflags

    # finally, merge flags with spigots (so that we can inspect output flags with original data)
    for p, q in array.ifrs():
        ns.output(p, q) << Meq.MergeFlags(spigots(p, q), outputs(p, q))
    outputs = ns.output
    inspectors.append(
        Meow.StdTrees.vis_inspector(ns.inspect('output'),
                                    outputs,
                                    bookmark=False))

    # make sinks and vdm
    Meow.StdTrees.make_sinks(ns, outputs, post=inspectors, output_col='')
    Meow.Bookmarks.make_node_folder("Output visibilities by baseline",
                                    [outputs(p, q) for p, q in array.ifrs()],
                                    sorted=True,
                                    ncol=2,
                                    nrow=2)

    # put all inspectors into bookmarks
    pg = Meow.Bookmarks.Page("Vis Inspectors", 2, 2)
    for node in inspectors:
        pg.add(node, viewer="Collections Plotter")

    # finally, setup imaging options
    imsel = mssel.imaging_selector(npix=512)
    TDLRuntimeMenu("Imaging options", *imsel.option_list())
Ejemplo n.º 21
0
  def source_list (self,ns,max_sources=None,**kw):
    """Reads LSM and returns a list of Meow objects.
    ns is node scope in which they will be created.
    Keyword arguments may be used to indicate which of the source attributes are to be
    created as Parms, use e.g. I=Meow.Parm(tags="flux") for this.
    The use_parms option may override this.
    """;
    if self.filename is None:
      return [];
    if self.lsm is None:
      self.load(ns);
    # all=1 returns unsorted list, so use a large count instead, to get a sorted list
    plist = self.lsm.queryLSM(count=9999999);
    
    # parse the beam expression
    if self.beam_expr is not None:
      try:
        beam_func = eval("lambda r,fq:"+self.beam_expr);
      except:
        raise RuntimeError("invalid beam expression");
    else:
      beam_func = None;
    
    # make list of direction,punit,I,I_apparent tuples
    parm = Meow.Parm(tags="source solvable");
    srclist = [];
    for pu in plist:
      ra,dec,I,Q,U,V,spi,freq0,RM = pu.getEssentialParms(ns);
      if self.solve_pos:
        ra = parm.new(ra);
        dec = parm.new(dec);
      direction = Meow.Direction(ns,pu.name,ra,dec,static=not self.solve_pos);
      Iapp = I;
      if beam_func is not None:
      # if phase centre is already set (i.e. static), then lmn will be computed here, and we
      # can apply a beam expression
        lmn = direction.lmn_static();
        if lmn is not None:
          r = sqrt(lmn[0]**2+lmn[1]**2);
          Iapp = I*beam_func(r,freq0*1e-9 or 1.4);  # use 1.4 GHz if ref frequency not specified
      # append to list
      srclist.append((pu.name,direction,pu,I,Iapp));
    # sort list by decreasing apparent flux
    from past.builtins import cmp
    from functools import cmp_to_key
    srclist.sort(key=cmp_to_key(lambda a,b:cmp(b[4],a[4])));
    
    srclist_full = srclist;
    # extract active subset
    srclist = self._subset_parser.apply(self.lsm_subset,srclist_full,names=[src[0] for src in srclist_full]);
    # extract solvable subset
    solve_subset = self._subset_parser.apply(self.solve_subset,srclist_full,names=[src[0] for src in srclist_full]);
    solve_subset = set([src[0] for src in solve_subset]);

    # make copy of kw dict to be used for sources not in solvable set
    kw_nonsolve = dict(kw);
    # and update kw dict to be used for sources in solvable set
    if self.solvable_sources:
      if self.solve_I:
        kw.setdefault("I",parm);
      if self.solve_Q:
        kw.setdefault("Q",parm);
      if self.solve_U:
        kw.setdefault("U",parm);
      if self.solve_V:
        kw.setdefault("V",parm);
      if self.solve_spi:
        kw.setdefault("spi",parm);
      if self.solve_RM:
        kw.setdefault("RM",parm);
      if self.solve_pos:
        kw.setdefault("ra",parm);
        kw.setdefault("dec",parm);
      if self.solve_shape:
        kw.setdefault("sx",parm);
        kw.setdefault("sy",parm);
        kw.setdefault("phi",parm);

    # make Meow list
    source_model = []

  ## Note: conversion from AIPS++ componentlist Gaussians to Gaussian Nodes
  ### eX, eY : multiply by 2
  ### eP: change sign
    for name,direction,pu,I,Iapp in srclist:
#      print "%-20s %12f %12f"%(pu.name,I,Iapp);
      src = {};
      ( src['ra'],src['dec'],
        src['I'],src['Q'],src['U'],src['V'],
        src['spi'],src['freq0'],src['RM']    ) = pu.getEssentialParms(ns)
      (eX,eY,eP) = pu.getExtParms()
      # scale 2 difference
      src['sx'] = eX*2
      src['sy'] = eY*2
      src['phi'] = -eP
      # override zero values with None so that Meow can make smaller trees
      if not src['RM']:
        src['RM'] = None;
      if not src['spi']:
        src['spi'] = None;
        if src['RM'] is None:
          src['freq0'] = None;
      ## construct parms or constants for source attributes
      ## if source is in solvable set (solvable_source_set of None means all are solvable),
      ## use the kw dict, else use the nonsolve dict for source parameters
      if name in solve_subset:
        solvable = True;
        kwdict = kw;
      else:
        solvable = False;
        kwdict = kw_nonsolve;
      for key,value in src.items():
        meowparm = kwdict.get(key);
        if isinstance(meowparm,Meow.Parm):
          src[key] = meowparm.new(value);
        elif meowparm is not None:
          src[key] = value;

      if eX or eY or eP:
        # Gaussians
        if eY:
          size,phi = [src['sx'],src['sy']],src['phi'];
        else:
          size,phi = src['sx'],None;
        src = Meow.GaussianSource(ns,name=pu.name,
                I=src['I'],Q=src['Q'],U=src['U'],V=src['V'],
                direction=direction,
                spi=src['spi'],freq0=src['freq0'],RM=src['RM'],
                size=size,phi=phi);
      else:
        src = Meow.PointSource(ns,name=pu.name,
                I=src['I'],Q=src['Q'],U=src['U'],V=src['V'],
                direction=direction,
                spi=src['spi'],freq0=src['freq0'],RM=src['RM']);
                
      # check for beam LM
      if pu._lm is not None:
        src.set_attr('beam_lm',pu._lm);
              
      src.solvable = solvable;
      src.set_attr('Iapp',Iapp);
      source_model.append(src);
      
    return source_model;