casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Partition.h
Go to the documentation of this file.
00001 //# Partition.h: this defines Partition which creates an MS that is a subset of
00002 //# an MS without any changes to the subtables.
00003 //# 
00004 //# Copyright (C) 2011
00005 //# Associated Universities, Inc. Washington DC, USA.
00006 //#
00007 //# This library is free software; you can redistribute it and/or modify it
00008 //# under the terms of the GNU Library General Public License as published by
00009 //# the Free Software Foundation; either version 2 of the License, or (at your
00010 //# option) any later version.
00011 //#
00012 //# This library is distributed in the hope that it will be useful, but WITHOUT
00013 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015 //# License for more details.
00016 //#
00017 //# You should have received a copy of the GNU Library General Public License
00018 //# along with this library; if not, write to the Free Software Foundation,
00019 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00020 //#
00021 //# Correspondence concerning AIPS++ should be addressed as follows:
00022 //#        Internet email: aips2-request@nrao.edu.
00023 //#        Postal address: AIPS++ Project Office
00024 //#                        National Radio Astronomy Observatory
00025 //#                        520 Edgemont Road
00026 //#                        Charlottesville, VA 22903-2475 USA
00027 //#
00028 //#
00029 //# $Id$
00030 #include <ms/MeasurementSets/MeasurementSet.h>
00031 #include <ms/MeasurementSets/MSColumns.h>
00032 #include <ms/MeasurementSets/MSMainColumns.h>
00033 #include <ms/MeasurementSets/MSMainEnums.h>
00034 //#include <synthesis/MSVis/VisIterator.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 <synthesis/MSVis/SubMS.h>
00040 
00041 #ifndef MSVIS_PARTITION_H
00042 namespace casa { //# NAMESPACE CASA - BEGIN
00043 
00044 #define MSVIS_PARTITION_H
00045 
00046 // <summary>
00047 // Partition makes a subset of an existing MS without remapping any indices
00048 // or resizing any subtables (unlike SubMS).
00049 // </summary>
00050 
00051 // <visibility=export>
00052 
00053 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00054 // </reviewed>
00055 
00056 // <prerequisite>
00057 //   <li> MeasurementSet
00058 //   <li> SubMS
00059 // </prerequisite>
00060 //
00061 // <etymology>
00062 // Partition an MS.
00063 // </etymology>
00064 //
00065 // <synopsis>
00066 // The order of operations (as in ms::partition()) is:
00067 //      ctor
00068 //      setmsselect
00069 //      selectTime
00070 //      makePartition
00071 // </synopsis>
00072 
00073 template<class T> class ROArrayColumn;
00074 //Bool isAllColumns(const Vector<MS::PredefinedColumns>& colNames);
00075 
00076 class Partition
00077 {
00078 public:
00079   Partition(String& theMS, Table::TableOption option = Table::Old);
00080   
00081   // construct from an MS
00082   Partition(MeasurementSet& ms);
00083 
00084   virtual ~Partition();
00085   
00086   // Change or Set the MS this MSSelector refers to.
00087   void setMS(MeasurementSet& ms);
00088 
00089   // Select spw and channels for each spw.
00090   // It returns true on success and false on failure.
00091   Bool selectSpw(const String& spwstr);
00092 
00093   //select Time and time averaging or regridding
00094   //void selectTime();
00095 
00096   //select stuff using msselection syntax ...time is left out
00097   // call it separately with timebin
00098   Bool setmsselect(const String& spw="", const String& field="", 
00099                    const String& baseline="", const String& scan="",
00100                    const String& uvrange="", const String& taql="", 
00101                    const String& subarray="", const String& intent="",
00102                    const String& obs="");
00103 
00104   // Select source or field
00105   Bool selectSource(const Vector<Int>& fieldid);
00106   
00107   // Select Antennas to split out  
00108   void selectAntenna(Vector<Int>& antennaids, Vector<String>& antennaSel)
00109   {
00110     antennaSel_p = SubMS::pickAntennas(antennaId_p, antennaSelStr_p,
00111                                        antennaids, antennaSel);
00112   } 
00113 
00114   // Select array IDs to use.
00115   void selectArray(const String& subarray);
00116 
00117   //select time parameters
00118   void selectTime(Double timeBin=-1.0, String timerng="");
00119 
00120   //void selectSource(Vector<String> sourceid);
00121 
00122   //Method to set if a phase Center rotation is needed
00123   //void setPhaseCenter(Int fieldid, MDirection& newPhaseCenter);
00124 
00125   //Method to make the partition.
00126   //
00127   //TileShape of size 1 can have 2 values [0], and [1] ...these are used in to
00128   //determine the tileshape by using MSTileLayout. Otherwise it has to be a
00129   //vector size 3 e.g [4, 15, 351] => a tile shape of 4 stokes, 15 channels 351
00130   //rows.
00131   //
00132   // combine sets combine_p.  (Columns to ignore while time averaging.)
00133   //
00134   Bool makePartition(String& outname, String& whichDataCol,
00135                      const Vector<Int>& tileShape=Vector<Int>(1, 0),
00136                      const String& combine="");
00137 
00138   //Method to make a scratch partition and even in memory if posssible
00139   //Useful if temporary subselection/averaging is necessary
00140   // It'll be in memory if the basic output ms is less than half of 
00141   // memory reported by HostInfo unless forced to by user...
00142   virtual MeasurementSet* makeScratchPartition(const Vector<MS::PredefinedColumns>& whichDataCols, 
00143                                    const Bool forceInMemory=False);
00144   // In this form whichDataCol gets passed to parseColumnNames().
00145   virtual MeasurementSet* makeScratchPartition(const String& whichDataCol, 
00146                                    const Bool forceInMemory=False);
00147 
00148   // This sets up a default new ms
00149   // Declared static as it can be called directly outside of Partition.
00150   // Therefore it is not dependent on any member variable.
00151   static MeasurementSet* setupMS(const String& msname, const MeasurementSet& inms,
00152                                  const Int nchan, const Int npol, const String& telescop,
00153                                  const Vector<MS::PredefinedColumns>& colNamesTok,
00154                                  const Int obstype=0);
00155 
00156   // Same as above except allowing manual tileshapes
00157   static MeasurementSet* setupMS(const String& msname, const MeasurementSet& inms,
00158                                  const Int nchan, const Int npol,
00159                                  const Vector<MS::PredefinedColumns>& colNamesTok,
00160                                  const Vector<Int>& tileShape=Vector<Int>(1,0));
00161   
00162   void verifyColumns(const MeasurementSet& ms, const Vector<MS::PredefinedColumns>& colNames);
00163 private:
00164   //method that returns the selected ms (?! - but it's Boolean - RR)
00165   Bool makeSelection();
00166 
00167   // (Sub)table fillers.
00168   Bool fillAllTables(const Vector<MS::PredefinedColumns>& colNames);
00169   Bool fillMainTable(const Vector<MS::PredefinedColumns>& colNames);
00170 
00171   //  Bool writeDiffSpwShape(const Vector<MS::PredefinedColumns>& colNames);
00172   Bool fillAccessoryMainCols();
00173 
00174   // *** Private member functions ***
00175   Bool getDataColumn(ROArrayColumn<Complex>& data,
00176                      const MS::PredefinedColumns colName);
00177   Bool getDataColumn(ROArrayColumn<Float>& data,
00178                      const MS::PredefinedColumns colName);
00179   Bool putDataColumn(MSColumns& msc, ROArrayColumn<Complex>& data,
00180                      const MS::PredefinedColumns datacol,
00181                      const Bool writeToDataCol=False);
00182   Bool putDataColumn(MSColumns& msc, ROArrayColumn<Float>& data,
00183                      const MS::PredefinedColumns datacol,
00184                      const Bool writeToDataCol=False);
00185 
00186   // This method uses VisIter for efficient copy mode data transfer
00187   Bool copyDataFlagsWtSp(const Vector<MS::PredefinedColumns>& colNames,
00188                          const Bool writeToDataCol);
00189 
00190   // Used in a couple of places to estimate how much memory to grab.
00191   Double n_bytes() {return mssel_p.nrow() * maxnchan_p * maxncorr_p *
00192                            sizeof(Complex);}
00193 
00194   // Read the input, time average it to timeBin_p, and write the output.
00195   Bool doTimeAver(const Vector<MS::PredefinedColumns>& dataColNames);
00196 
00197   // Fills mapper[ntok] with a map from dataColumn indices to ArrayColumns in
00198   // the output.  mapper must have ntok slots!
00199   static void getDataColMap(MSMainColumns* msc, ArrayColumn<Complex>* mapper,
00200                             uInt ntok,
00201                             const Vector<MS::PredefinedColumns>& colEnums);
00202 
00203   // *** Member variables ***
00204 
00205   // Initialized* by ctors.  (Maintain order both here and in ctors.)
00206   //  * not necessarily to anything useful.
00207   MeasurementSet ms_p, mssel_p;
00208   MSMainColumns * msc_p;                // columns of msOut_p
00209   ROMSColumns * mscIn_p;
00210   Bool   antennaSel_p;          // Selecting by antenna?
00211   Double timeBin_p;
00212   String scanString_p,          // Selects scans by #number#.  Historically named.
00213          intentString_p,        // Selects scans by string.  scanString_p was taken.
00214          obsString_p,           // String for observationID selection.
00215          uvrangeString_p, taqlString_p;
00216   String timeRange_p, arrayExpr_p, corrString_p;
00217   String combine_p;          // Should time averaging not split bins by
00218                              // scan #, observation, and/or state ID?
00219                              // Must be lowercase at all times.
00220   Int maxnchan_p,    // The maximum # of channels and correlations for each
00221       maxncorr_p;    // selected DDID.  (Int because NUM_CHAN and NUM_CORR
00222                      // are Int instead of uInt.)
00223 
00224   // Uninitialized by ctors.
00225   MeasurementSet msOut_p;
00226   Vector<Int> spw_p;      // Selected spw numbers
00227   Vector<Int> fieldid_p;
00228   Vector<String> antennaSelStr_p;
00229   Vector<Int> antennaId_p;
00230   Vector<Int> arrayId_p;
00231   Matrix<Double> selTimeRanges_p;
00232 };
00233 
00234 } //# NAMESPACE CASA - END
00235 
00236 #endif
00237