casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Calibrater.h
Go to the documentation of this file.
00001 //# Calibrater.h: High-level calibrator object; controls calibration overall
00002 //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#
00025 //# $Id: Calibrater.h,v 19.16 2006/02/14 19:46:23 gmoellen Exp $
00026 
00027 #ifndef SYNTHESIS_CALIBRATER_H
00028 #define SYNTHESIS_CALIBRATER_H
00029 
00030 #include <casa/aips.h>
00031 #include <casa/OS/Timer.h>
00032 #include <casa/Containers/Record.h>
00033 #include <ms/MeasurementSets/MeasurementSet.h>
00034 #include <measures/Measures/MRadialVelocity.h>
00035 #include <synthesis/MeasurementEquations/VisEquation.h>
00036 #include <synthesis/MeasurementComponents/VisCal.h>
00037 #include <synthesis/MeasurementComponents/SolvableVisCal.h>
00038 #include <synthesis/MeasurementComponents/VisCalGlobals.h>
00039 #include <casa/Logging/LogIO.h>
00040 #include <casa/Logging/LogSink.h>
00041 #include <ms/MeasurementSets/MSHistoryHandler.h>
00042 #include <synthesis/MSVis/VisibilityProcessing.h>
00043 
00044 
00045 namespace casa { //# NAMESPACE CASA - BEGIN
00046 
00047 // <summary>Controls the solution of calibration components (Jones Matrices)</summary>
00048 
00049 class CorrectorVp;
00050 
00051 namespace asyncio {
00052   class PrefetchColumns;
00053 }
00054 
00055 class Calibrater 
00056 {
00057 
00058   friend class CorrectorVp;
00059 
00060  public:
00061   // Default constructor
00062   Calibrater();
00063 
00064   // Copy constructor and assignment operator
00065   Calibrater(const Calibrater&);
00066   Calibrater& operator=(const Calibrater&);
00067 
00068   // Destructor
00069   ~Calibrater();
00070   
00071 
00072   // Set uv-data selection via MSSelection
00073   void selectvis(const String& time="",
00074                  const String& spw="",
00075                  const String& scan="",
00076                  const String& field="",
00077                  const String& intent="",
00078                  const String& obsIDs="",
00079                  const String& baseline="",
00080                  const String& uvrange="",
00081                  const String& chanmode="none",
00082                  const Int& nchan=1,
00083                  const Int& start=0, 
00084                  const Int& step=1,
00085                  const MRadialVelocity& mStart=MRadialVelocity(),
00086                  const MRadialVelocity& mStep=MRadialVelocity(),
00087                  const String& msSelect="");
00088 
00089   // Set up to apply calibration (using MSSelection syntax)
00090   Bool setapply (const String& type, 
00091                  const Double& t,
00092                  const String& table,
00093                  const String& spw,
00094                  const String& field,
00095                  const String& interp,
00096                  const Bool& calwt,
00097                  const Vector<Int>& spwmap,
00098                  const Vector<Double>& opacity);
00099 
00100   Bool setapply (const String& type, 
00101                  const Record& applypar);
00102 
00103   Bool setmodel(const String& modelImage);
00104   Bool setModel(const Vector<Double>& stokes);
00105 
00106   // Arrange to solve (using MSSelection syntax)
00107   Bool setsolve (const String& type, 
00108                  const String& solint,
00109                  const String& table,
00110                  const Bool append,
00111                  const Double preavg, 
00112                  const String& apmode="AP",
00113                  const Int minblperant=4,
00114                  const String& refant="",
00115                  const Bool solnorm=False,
00116                  const Float minsnr=0.0f,
00117                  const String& combine="",
00118                  const Int fillgaps=0,
00119                  const String& cfcache="",
00120                  const Double painc=360.0,
00121                  const Int fitorder=0);
00122 
00123   // Arrange to solve for BPOLY (using MSSelection syntax)
00124   Bool setsolvebandpoly(const String& table,
00125                         const Bool& append,
00126                         const String& solint,
00127                         const String& combine,
00128                         const Vector<Int>& degree,
00129                         const Bool& visnorm,
00130                         const Bool& solnorm,
00131                         const Int& maskcenter,
00132                         const Float& maskedge,
00133                         const String& refant);
00134 
00135   // Arrange to solve for GSPLINE (using MSSelection syntax)
00136   Bool setsolvegainspline(const String& table,
00137                           const Bool& append,
00138                           const String& mode,
00139                           const Double& splinetime,
00140                           const Double& preavg,
00141                           const Int& numpoint,
00142                           const Double& phasewrap,
00143                           const String& refant);
00144 
00145   Bool setsolve (const String& type, 
00146                  const Record& solvepar);
00147 
00148   // Unset all (default) or one apply calibration component
00149   Bool unsetapply(const Int& which=-1);
00150   // TBD:   Bool unsetapply(const String& type);  // by type?
00151 
00152   // Unset the solved-for calibration component
00153   Bool unsetsolve();
00154 
00155   // Reset the calibrator object
00156   Bool reset(const Bool& apply=True, 
00157              const Bool& solve=True);
00158 
00159   // Apply all setapply'd calibration components to DATA and
00160   //  deposit in the CORRECTED_DATA column
00161   Bool correct(String mode="calflag");
00162 
00163   // Apply all setapply'd calibration components to MODEL_DATA and
00164   //  deposit in the MODEL_DATA column
00165   Bool corrupt();
00166 
00167   // Solve for a given calibration component
00168   Bool solve();
00169 
00170   // Modelfit
00171   Vector<Double> modelfit(const Int& iter,
00172                           const String& stype,
00173                           const Vector<Double>& par,
00174                           const Vector<Bool>& vary,
00175                           const String& file);
00176 
00177   // Fluxscale (using MSSelection syntax for fields)
00178   void fluxscale(const String& infile, 
00179                  const String& outfile,
00180                  const String& refFields, 
00181                  const Vector<Int>& refSpwMap,
00182                  const String& tranFields,
00183                  const Bool& append,
00184                  SolvableVisCal::fluxScaleStruct& oFluxScaleFactor,
00185                  Vector<Int>& tranidx,
00186                  const String& oListFile,
00187                  const Bool& incremental,
00188                  const Int& fitorder);
00189 
00190   // Fluxscale (via field indices)
00191   void fluxscale(const String& infile, 
00192                  const String& outfile,
00193                  const Vector<Int>& refField, 
00194                  const Vector<Int>& refSpwMap,
00195                  const Vector<Int>& tranField,
00196                  const Bool& append,
00197                  SolvableVisCal::fluxScaleStruct& oFluxScaleFactor,
00198                  const String& oListFile,
00199                  const Bool& incremental,
00200                  const Int& fitorder);
00201 
00202   // Accumulate (using MSSelection syntax)
00203   void accumulate(const String& intab,
00204                   const String& incrtab,
00205                   const String& outtab,
00206                   const String& fields,
00207                   const String& calFields,
00208                   const String& interp="linear",
00209                   const Double& t=-1.0,
00210                   const Vector<Int>& spwmap=Vector<Int>(1,-1));
00211 
00212   // Generate cal table from specified values
00213   void specifycal(const String& type,
00214                   const String& caltable,
00215                   const String& time,
00216                   const String& spw,
00217                   const String& antenna,
00218                   const String& pol,
00219                   const Vector<Double>& parameter);
00220 
00221   // Smooth  calibration (using MSSelection syntax
00222   Bool smooth(const String& infile,
00223               String& outfile, 
00224               const String& smoothtype, 
00225               const Double& smoothtime,
00226               const String& fields);
00227 
00228 
00229   // List a calibration table
00230   Bool listCal(const String& infile,
00231                const String& field,
00232                const String& antenna,
00233                const String& spw,
00234                const String& listfile="",
00235                const Int& pagerows=50);
00236 
00237   // Initialize the calibrator object from an input MeasurementSet.
00238   // Optional compression of the calibration columns (MODEL_DATA,
00239   // and CORRECTED_DATA) is supported.
00240   //if addScratch=True, optionally one can just add CORRECTED_DATA by
00241   //setting addModel to False
00242   Bool initialize(MeasurementSet& inputMS, 
00243                   Bool compress=True,
00244                   Bool addScratch=True, Bool addModel=True);
00245 
00246   // Re-initialize the calibration scratch columns
00247   Bool initCalSet(const Int& calSet);
00248 
00249   // Report apply/solve state
00250   Bool state();
00251   Bool applystate();
00252   Bool solvestate();
00253 
00254   Record& getActRec() {return actRec_;};
00255 
00256   Bool cleanup();
00257 
00258   // Method to update MS HISTORY Table
00259   void writeHistory(LogIO& os, 
00260                     Bool cliCommand=False);
00261 
00262   CorrectorVp * getCorrectorVp ();
00263 
00264   // Handle caltable backward compatibility
00265   static Bool updateCalTable(const String& caltable);
00266   
00267  private:
00268   // Log functions and variables
00269   LogIO sink_p;
00270   LogIO& logSink();
00271 
00272   // Time functions and variables
00273   String timerString();
00274   Timer timer_p;
00275 
00276   VisibilityIterator::DataColumn configureForCorrection ();
00277   Bool correctUsingVpf ();
00278 
00279   // Select on channel using MSSelection
00280   void selectChannel(const String& spw);
00281 
00282   // Channel mask services
00283   void initChanMask();
00284 
00285   // Select on channel in the VisSet
00286   void selectChannel(const String& mode, 
00287                      const Int& nchan, const Int& start, const Int& step,
00288                      const MRadialVelocity& mStart,
00289                      const MRadialVelocity& mStep);
00290   
00291   // Interpret refant index
00292   Vector<Int> getRefantIdxList(const String& refant);
00293   
00294   Vector<Int> getAntIdx(const String& antenna);
00295 
00296   // Interpret field indices (MSSelection)
00297   Vector<Int> getFieldIdx(const String& fields);
00298 
00299   // Interpret spw indices (MSSelection)
00300   Vector<Int> getSpwIdx(const String& spws);
00301 
00302   // Interpret spw indices (MSSelection)
00303   Matrix<Int> getChanIdx(const String& spws);
00304   
00305   // Query apply types to see if we need to calibrate the weights
00306   Bool calWt();
00307 
00308   // Returns True if calibrator object is in a valid state
00309   Bool ok();
00310 
00311   // Given a (supplied) list of uncalibrated spws, determines and returns if there were
00312   // any, and if so sends them as a warning message to the logger.
00313   Bool summarize_uncalspws(const Vector<Bool>& uncalspw, const String& origin);
00314 
00315   // Create a VisSet for raw phase transfer if needed
00316   void getRawPhaseVisSet(Vector<Int>& spwid); 
00317 
00318   // The standard solving mechanism
00319   Bool genericGatherAndSolve();
00320 
00321   // Input MeasurementSet and derived selected MeasurementSet
00322   String msname_p;
00323   MeasurementSet* ms_p;
00324   MeasurementSet* mssel_p;
00325   MeasurementSet* msselRaw_p;
00326 
00327   // VisSet and VisEquation derived from the input MeasurementSet
00328   VisSet* vs_p;
00329   VisSet* rawvs_p;
00330   VisEquation* ve_p;
00331 
00332   // VisCals for applying and solving:
00333   PtrBlock<VisCal*> vc_p;
00334   SolvableVisCal* svc_p;
00335 
00336   // MeasurementSet selection parameters
00337   String dataMode_p;
00338   Int dataNchan_p, dataStart_p, dataStep_p;
00339   MRadialVelocity mDataStart_p, mDataStep_p;
00340 
00341   //Spline phase wrapping helper params
00342   Double phasewrap_p;
00343   Int splinepoint_p;
00344 
00345   //Used to update the MS HISTORY Table
00346   Int histLockCounter_p;
00347   MSHistoryHandler *hist_p;
00348   Table historytab_p;
00349 
00350   // channel masking 
00351   PtrBlock<Vector<Bool>*> chanmask_;
00352 
00353   // Activity record
00354   Record actRec_;
00355 
00356 };
00357 
00358 class CorrectorVp : public vpf::VisibilityProcessor {
00359 
00360 public:
00361 
00362     CorrectorVp (Calibrater * calibrater, const String & name = "Corrector");
00363     ROVisibilityIterator * getVisibilityIterator ();
00364 
00365     static const String In;
00366     static const String Out;
00367 
00368 protected:
00369 
00370     void chunkStartImpl (const vpf::SubchunkIndex &);
00371     ProcessingResult
00372     doProcessingImpl (ProcessingType processingType,
00373                       vpf::VpData & inputData,
00374                       const vpf::SubchunkIndex & subChunkIndex);
00375     void processingStartImpl ();
00376     void validateImpl ();
00377 
00378 private:
00379 
00380     Calibrater * calibrater_p;
00381     Bool calculateWeights_p;
00382     Vector<Bool> uncalibratedSpectralWindows_p;
00383     VisibilityIterator::DataColumn  whichOutputColumn_p;
00384 };
00385 
00386 
00387 
00388 
00389 } //# NAMESPACE CASA - END
00390 
00391 #endif