casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Reweighter.h
Go to the documentation of this file.
00001 //# Reweighter.h: this defines Reweighter which reweights a selected part of an
00002 //# MS based on the properties of a (possibly different) selection of it.
00003 //# Copyright (C) 2011
00004 //# Associated Universities, Inc. Washington DC, USA.
00005 //#
00006 //# This library is free software; you can redistribute it and/or modify it
00007 //# under the terms of the GNU Library General Public License as published by
00008 //# the Free Software Foundation; either version 2 of the License, or (at your
00009 //# option) any later version.
00010 //#
00011 //# This library is distributed in the hope that it will be useful, but WITHOUT
00012 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 //# License for more details.
00015 //#
00016 //# You should have received a copy of the GNU Library General Public License
00017 //# along with this library; if not, write to the Free Software Foundation,
00018 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00019 //#
00020 //# Correspondence concerning AIPS++ should be addressed as follows:
00021 //#        Internet email: aips2-request@nrao.edu.
00022 //#        Postal address: AIPS++ Project Office
00023 //#                        National Radio Astronomy Observatory
00024 //#                        520 Edgemont Road
00025 //#                        Charlottesville, VA 22903-2475 USA
00026 //#
00027 //#
00028 //# $Id$
00029 #include <ms/MeasurementSets/MeasurementSet.h>
00030 #include <ms/MeasurementSets/MSColumns.h>
00031 #include <ms/MeasurementSets/MSMainEnums.h>
00032 //#include <synthesis/MSVis/VisIterator.h>
00033 #include <synthesis/MSVis/SubMS.h>
00034 #include <synthesis/MSVis/VisBufferComponents.h>
00035 #include <casa/aips.h>
00036 #include <casa/Arrays/Array.h>
00037 #include <casa/Arrays/Vector.h>
00038 //#include <casa/Utilities/CountedPtr.h>
00039 #include <map>
00040 #include <set>
00041 #include <vector>
00042 
00043 
00044 #ifndef MSVIS_REWEIGHTER_H
00045 namespace casa { //# NAMESPACE CASA - BEGIN
00046 
00047 #define MSVIS_REWEIGHTER_H
00048 
00049 // <summary>
00050 //   Reweighter reweights a selected part of an MS based on the properties of a
00051 //   (possibly different) selection of it.
00052 // </summary>
00053 
00054 // <visibility=export>
00055 
00056 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00057 // </reviewed>
00058 
00059 // <prerequisite>
00060 //   <li> MeasurementSet
00061 // </prerequisite>
00062 //
00063 // <etymology>
00064 // Reweighter ...it reweights.
00065 // </etymology>
00066 //
00067 // <synopsis>
00068 // The order of operations (as in ms::split()) is:
00069 //      ctor
00070 //      setmsselect
00071 //      selectTime
00072 //      makeReweighter
00073 // </synopsis>
00074 
00075 // These forward declarations are so the corresponding .h files don't have to
00076 // be included in this .h file, but it's only worth it if a lot of other files
00077 // include this file.
00078 class MSSelection; // #include <ms/MeasurementSets/MSSelection.h>
00079 
00080 template<class T> class ROArrayColumn;
00081 
00082 class Reweighter
00083 {
00084 public:
00085   Reweighter(const String& theMS,
00086              const Bool dorms=false,           // Reweight by rms or stddev?
00087              const uInt minsamp=1);            // Minimum # of visibilities for measuring a scatter.
00088   
00089   virtual ~Reweighter();
00090   
00091   // Select spw and channels for each spw in spwstr.
00092   // Returns true on success and false on failure.
00093   Bool selectSpw(std::set<Int>& spwset, Vector<Int>& chanStartv,
00094                  Vector<Int>& chanEndv, Vector<Int>& chanStepv,
00095                  const String& spwstr);
00096 
00097   // Setup polarization selection (for now, only from available correlations -
00098   // no Stokes transformations.)
00099   Bool selectCorrelations(const String& corrstr);
00100 
00101   //select stuff using msselection syntax ...time is left out
00102   // call it separately with timebin
00103   // Returns a success value.
00104   Bool setmsselect(const String& fitspw="", const String& outspw="",
00105                    const String& field="",  const String& baseline="",
00106                    const String& scan="",
00107                    const String& subarray="", const String& correlation="",
00108                    const String& intent="", const String& obs="");
00109 
00110   // Select source or field
00111   Bool selectSource(const Vector<Int>& fieldid);
00112   
00113   void selectAntenna(const Vector<Int>& antennaids,
00114                      const Vector<String>& antennaSel)
00115   {
00116     antennaSel_p = SubMS::pickAntennas(antennaId_p, antennaSelStr_p,
00117                                        antennaids, antennaSel);
00118   }
00119   
00120   // Select array IDs to use.
00121   void selectArray(const String& subarray) {arrayExpr_p = subarray;}
00122 
00123   //select time parameters
00124   void selectTime(Double timeBin=-1.0, String timerng="");
00125 
00126   //void selectSource(Vector<String> sourceid);
00127 
00128   // Set the selection string for line-free channels.
00129   void setFitSpw(const String& fitspw) {fitspw_p = fitspw;}
00130   // Set the selection string for output spws (not channels!).
00131   void setOutSpw(const String& outspw) {outspw_p = outspw;}
00132   
00133   // Do the reweighting!
00134   Bool reweight(String& colname, const String& combine);
00135 
00136   //void verifyColumns(const MeasurementSet& ms, const Vector<MS::PredefinedColumns>& colNames);
00137 
00138   // Fills polIDs with a map from DDID to polID, and corrTypes with a map from
00139   // polID to corrTypes.
00140   // The return value is meaningless for now.
00141   static Bool getCorrTypes(Vector<Int>& polIDs,
00142                            Vector<Vector<Int> >& corrTypes,
00143                            const MSColumns& msc);
00144   
00145  protected:
00146 
00147   //method that returns the selected ms (?! - but it's Boolean - RR)
00148   Bool makeSelection();
00149 
00150   // *** Private member functions ***
00151 
00152   // Picks a reference to DATA, MODEL_DATA, CORRECTED_DATA, or LAG_DATA out
00153   // of ms_p.  FLOAT_DATA is not included because it is not natively complex. 
00154   const ROArrayColumn<Complex>& right_column(const ROMSColumns *ms_p,
00155                                              const MS::PredefinedColumns datacol);
00156 
00157   // Sets sort to a Block of columns that a VisibilityIterator should sort by,
00158   // according to combine_p.  Columns that should never be combined in the
00159   // calling function, i.e. spw for time averaging, should be listed in
00160   // uncombinable.
00161   //
00162   // verbose: log a message on error.
00163   //
00164   // Returns whether or not there were any conflicts between combine_p and
00165   // uncombinable.
00166   Bool setSortOrder(Block<Int>& sort, const String& uncombinable="",
00167                     const Bool verbose=true) const;
00168 
00169   // Returns whether col is (not in combine_p) || in uncombinable.
00170   // Columns that should never be combined in the
00171   // calling function, i.e. spw for time averaging, should be listed in
00172   // uncombinable.
00173   //
00174   // verbose: log a message on error.
00175   //
00176   // conflict is set to true if there is a conflict between combine_p and
00177   // uncombinable.
00178   Bool shouldWatch(Bool& conflict, const String& col,
00179                    const String& uncombinable="",
00180                    const Bool verbose=true) const;
00181 
00182   void makeUnionSpw();
00183 
00184   // Defaults to DATA.
00185   MS::PredefinedColumns dataColStrToEnum(const String& col);
00186 
00187   // *** Member variables ***
00188 
00189   // Initialized* by ctors.  (Maintain order both here and in ctors.)
00190   //  * not necessarily to anything useful.
00191   MeasurementSet ms_p, mssel_p;
00192   Bool dorms_p;                 // Reweight by rms or stddev?
00193   uInt minsamp_p;               // Minimum # of visibilities for measuring a scatter.
00194   MSColumns *msc_p;
00195   Bool antennaSel_p;            // Selecting by antenna?
00196   Double timeBin_p;
00197   String scanString_p,          // Selects scans by #number#.  Historically named.
00198          intentString_p,        // Selects scans by string.  scanString_p was taken.
00199          obsString_p;            // String for observationID selection.
00200   String timeRange_p, arrayExpr_p, corrString_p;
00201   String combine_p;          // Should time averaging not split bins by
00202                              // scan #, observation, and/or state ID?
00203                              // Must be lowercase at all times.
00204   String fitspw_p;           // Selection string for line-free channels.
00205   String outspw_p;           // Selection string for ddids to reweight.
00206   Vector<Int> unionspw_p;    // All the spws in fitspw_p or outspw_p.
00207 
00208   // Uninitialized by ctors.
00209   Vector<String> antennaSelStr_p;
00210   Vector<Int> antennaId_p;
00211 
00212   Vector<Int> fieldId_p;
00213 
00214   // Map from DDID to polID, filled in getCorrTypes().
00215   Vector<Int> polIDs_p;
00216 
00217   // Map from polID to corrTypes, filled in getCorrTypes().
00218   Vector<Vector<Int> > corrTypes_p;
00219 
00220   Vector<Vector<Slice> > chanSlices_p;  // Used by VisIterator::selectChannel()
00221   Vector<Slice> corrSlice_p;
00222   Vector<Vector<Slice> > corrSlices_p;  // Used by VisIterator::selectCorrelation()
00223   Matrix<Double> selTimeRanges_p;
00224 
00225   std::set<Int> fitspwset_p, outspwset_p;
00226   Vector<Int> fitStart_p, fitEnd_p, fitStep_p;
00227 };
00228 
00229 } //# NAMESPACE CASA - END
00230 
00231 #endif
00232