Ejemplo n.º 1
0
 def make_filename(self, filename, jones_label=None):
     """
     Helper method: expands full filename a from templated filename. This uses the standard 
     str.format() function, passing in self.global_options, as well as JONES=jones_label, as keyword 
     arguments. This allows for filename templates that include strings from the global options
     dictionary, e.g. "{data[ms]}-ddid{sel[ddid]}".
     
     Args:
         filename (str): 
             the templated filename
         jones_label (str, optional):
             Jones matrix label, overrides self.jones_label if specified.
         
     Returns:
         str:
             Expanded filename
         
     """
     return expand_templated_name(filename,
                                  JONES=jones_label or self.jones_label)
Ejemplo n.º 2
0
 def __init__(self, gmfactory, ifrgain_opts, compute=True):
     """
     Initializes the IFR-based gains machinery.
     
     Args:
         gmfactory:      a GainMachine Factory is used to manage the solution databases
         ifrgain_opts:   dict of options
         compute:        if False, gains are not computed even if options ask them to
     """
     from cubical.main import expand_templated_name
     self.gmfactory = gmfactory
     load_from = expand_templated_name(ifrgain_opts['load-from'])
     save_to = expand_templated_name(ifrgain_opts['save-to'])
     self._ifrgains_per_chan = ifrgain_opts['per-chan']
     self._ifrgain = None
     self._nfreq = gmfactory.grid["freq"]
     nfreq, nant, ncorr = [
         len(gmfactory.grid[axis]) for axis in ("freq", "ant", "corr")
     ]
     if load_from:
         filename = load_from
         print(ModColor.Str(
             "applying baseline-based corrections (BBCs) from {}".format(
                 filename),
             col="green"),
               file=log(0))
         if "//" in filename:
             filename, prefix = filename.rsplit("//", 1)
         else:
             filename, prefix = filename, "BBC"
         parm = param_db.load(filename).get(prefix)
         if parm is None:
             print(ModColor.Str("  no solutions for '{}' in {}".format(
                 prefix, filename)),
                   file=log(0))
         else:
             self._ifrgain = parm.reinterpolate(
                 freq=gmfactory.grid["freq"]).filled()
             if tuple(self._ifrgain.shape) != (nfreq, nant, nant, ncorr,
                                               ncorr):
                 print(ModColor.Str(
                     "  invalid BBC shape {}, will ignore".format(
                         self._ifrgain.shape)),
                       file=log(0))
                 self._ifrgain = None
             else:
                 print("  loaded per-channel BBCs of shape {}".format(
                     filename, self._ifrgain.shape),
                       file=log(0))
                 if not self._ifrgains_per_chan:
                     print("  using one mean value across band",
                           file=log(0))
                     self._ifrgain[np.newaxis,
                                   ...] = self._ifrgain.mean(axis=0)
                 # reset off-diagonal values, if needed
                 if ifrgain_opts["apply-2x2"]:
                     print(ModColor.Str(
                         "  using full 2x2 BBCs. You'd better know what you're doing!",
                         col="green"),
                           file=log(0))
                 else:
                     self._ifrgain[..., (0, 1), (1, 0)] = 1
                     print("  using parallel-hand BBCs only", file=log(0))
     if save_to and compute:
         self._compute_2x2 = ifrgain_opts["compute-2x2"]
         # setup axes for IFR-based gains
         axes = ["freq", "ant1", "ant2", "corr1", "corr2"]
         # define the ifrgain parameter
         self._save_filename = save_to
         parm = gmfactory.define_param(self._save_filename,
                                       "BBC",
                                       1 + 0j,
                                       axes,
                                       interpolation_axes=["freq"])
         self._ifrgains_grid = {
             axis: parm.grid[i]
             for i, axis in enumerate(axes)
         }
         # initialize accumulators for M.D^H and D.D^H terms
         self._mdh_sum = np.ma.zeros(parm.shape,
                                     gmfactory.ctype,
                                     fill_value=0)
         self._ddh_sum = np.ma.zeros(parm.shape,
                                     gmfactory.ctype,
                                     fill_value=0)
         #
         print(
             "will compute & save suggested baseline-based corrections (BBCs) to {}"
             .format(self._save_filename),
             file=log(0))
         print(
             "  (these can optionally be applied in a subsequent CubiCal run)",
             file=log(0))
     else:
         self._ifrgains_grid = None