MSTransformDataHandler.h

Go to the documentation of this file.
00001 //# MSTransformDataHandler.h: This file contains the interface definition of the MSTransformDataHandler class.
00002 //#
00003 //#  CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
00004 //#  Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
00005 //#  Copyright (C) European Southern Observatory, 2011, All rights reserved.
00006 //#
00007 //#  This library is free software; you can redistribute it and/or
00008 //#  modify it under the terms of the GNU Lesser General Public
00009 //#  License as published by the Free software Foundation; either
00010 //#  version 2.1 of the License, or (at your option) any later version.
00011 //#
00012 //#  This library is distributed in the hope that it will be useful,
00013 //#  but WITHOUT ANY WARRANTY, without even the implied warranty of
00014 //#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 //#  Lesser General Public License for more details.
00016 //#
00017 //#  You should have received a copy of the GNU Lesser General Public
00018 //#  License along with this library; if not, write to the Free Software
00019 //#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00020 //#  MA 02111-1307  USA
00021 //# $Id: $
00022 
00023 #ifndef MSTransformDataHandler_H_
00024 #define MSTransformDataHandler_H_
00025 
00026 // Measurement Set
00027 #include <ms/MeasurementSets.h>
00028 
00029 // Measurement Set Selection
00030 #include <ms/MSSel/MSSelection.h>
00031 #include <ms/MSSel/MSSelectionTools.h>
00032 
00033 // Needed by setupMS
00034 #include <tables/Tables.h>
00035 #include <ms/MeasurementSets/MSTileLayout.h>
00036 
00037 // Needed by copyTable
00038 #include <tables/Tables/PlainTable.h>
00039 
00040 // OS methods needed by fillSubTables
00041 #include <casa/OS/Timer.h>
00042 #include <casa/OS/Path.h>
00043 #include <casa/OS/Directory.h>
00044 
00045 
00046 namespace casa { //# NAMESPACE CASA - BEGIN
00047 
00048 //  MSTransformDataHandler definition
00049 class MSTransformDataHandler
00050 {
00051 
00052 public:
00053 
00054         enum asdmStManUseAlternatives
00055         {
00056                 DONT,
00057                 USE_FOR_DATA,
00058                 USE_FOR_DATA_WEIGHT_SIGMA_FLAG
00059         };
00060 
00061         MSTransformDataHandler( String& theMS, Table::TableOption option,
00062                                                         Bool virtualModelCol=False, Bool virtualCorrectedCol=False,
00063                                                         Bool reindex=True);
00064         MSTransformDataHandler( MeasurementSet& ms,
00065                                                         Bool virtualModelCol=False, Bool virtualCorrectedCol=False,
00066                                                         Bool reindex=True);
00067         ~MSTransformDataHandler();
00068 
00069         // Declared static because it's used in setupMS().
00070         // colNameList is internally up-cased, so it is not const or passed by reference.
00071         static const Vector<MS::PredefinedColumns>& parseColumnNames(String colNameList);
00072 
00073         // This version uses the MeasurementSet to check what columns are present,
00074         // i.e. it makes col=="all" smarter, and it is not necessary to call
00075         // verifyColumns() after calling this.  Unlike the other version, it knows
00076         // about FLOAT_DATA and LAG_DATA.  It throws an exception if a
00077         // _specifically_ requested column is absent.
00078         static const Vector<MS::PredefinedColumns>& parseColumnNames(   String colNameList, const MeasurementSet& ms,
00079                                                                                                                                         Bool virtualModelCol=False,Bool virtualCorrectedCol=False);
00080 
00081         // Helper function for parseColumnNames().  Converts col to a list of
00082         // MS::PredefinedColumnss, and returns the # of recognized data columns.
00083         // static because parseColumnNames() is static.
00084         static uInt dataColStrToEnums(const String& col,Vector<MS::PredefinedColumns>& colvec);
00085 
00086         // Selection method using msselection syntax. Time is not handled by this method.
00087         Bool setmsselect(       const String& spw = "", const String& field = "",
00088                                                 const String& baseline = "", const String& scan = "",
00089                                                 const String& uvrange = "", const String& taql = "",
00090                                                 const Vector<Int>& step = Vector<Int> (1, 1),
00091                                                 const String& subarray = "", const String& correlation = "",
00092                                                 const String& intent = "", const String& obs = "",
00093                         const String& feed = "");
00094 
00095         // Select source or field
00096         Bool selectSource(const Vector<Int>& fieldid);
00097 
00098         // Select spw and channels for each spw.
00099         Bool selectSpw(const String& spwstr, const Vector<Int>& steps);
00100 
00101         // Returns the set (possibly empty) of spectral windows that are
00102         // in SPW but not listed in ms's DATA_DESCRIPTION sub-table.
00103         // (This happens with certain calibration/hardware setups.)
00104         static std::set<Int> findBadSpws(MeasurementSet& ms, Vector<Int> spwv);
00105 
00106         // Select Antennas to split out
00107         void selectAntenna(const Vector<Int>& antennaids,const Vector<String>& antennaSel);
00108 
00109         // Helper function for selectAntenna()
00110         static Bool pickAntennas(       Vector<Int>& selected_antennaids,
00111                                                                 Vector<String>& selected_antenna_strs,
00112                                                                 const Vector<Int>& antennaids,
00113                                                                 const Vector<String>& antennaSel);
00114 
00115         // Select array IDs to use.
00116         void selectArray(const String& subarray);
00117 
00118         // Setup polarization selection (for now, only from available correlations - no Stokes transformations.)
00119         Bool selectCorrelations(const String& corrstr);
00120 
00121         // Fills outToIn[pol] with a map from output correlation index to input
00122         // correlation index, for each input polID pol.
00123         // It does not yet check the appropriateness of the correlation selection
00124         // string, so ignore the return value for now.  outToIn[pol] defaults to
00125         // an empty Vector if no correlations are selected for pol.
00126         // That is not the same as the default "select everything in ms".
00127         static Bool getCorrMaps(        MSSelection& mssel,
00128                                                                 const MeasurementSet& ms,
00129                                                                 Vector<Vector<Int> >& outToIn,
00130                                                                 const Bool areSelecting = false);
00131 
00132         // Select time parameters
00133         void selectTime(Double timeBin=-1.0, String timerng="");
00134 
00135         //Method to make the basic structure of the MS
00136         //
00137         //TileShape of size 1 can have 2 values [0], and [1] ...these are used in to
00138         //determine the tileshape by using MSTileLayout. Otherwise it has to be a
00139         //vector size 3 e.g [4, 15, 351] => a tile shape of 4 stokes, 15 channels 351
00140         //rows.
00141         //
00142         // combine sets combine_p.  (Columns to ignore while time averaging.)
00143         //
00144         Bool makeMSBasicStructure(      String& msname,
00145                                                                 String& whichDataCol,
00146                                                                 const Vector<Int>& tileShape = Vector<Int> (1, 0),
00147                                                                 const String& combine = "",
00148                                                                 Table::TableOption option=Table::New);
00149 
00150         Bool isAllColumns(const Vector<MS::PredefinedColumns>& colNames);
00151 
00152         // Method that returns the selected ms (?! - but it's Boolean - RR)
00153         Bool makeSelection();
00154 
00155         // This sets up a default new ms
00156         // Declared static as it can be (and is) called directly from outside
00157         // Therefore it is not dependent on any member variable.
00158         static MeasurementSet* setupMS( const String& msname, const Int nchan,
00159                                                                         const Int npol, const String& telescop,
00160                                                                         const Vector<MS::PredefinedColumns>& colNamesTok,
00161                                                                         const Int obstype = 0, const Bool compress = False,
00162                                                                         const asdmStManUseAlternatives asdmStManUse = DONT,
00163                                                                         Table::TableOption option=Table::New);
00164 
00165         // Same as above except allowing manual tileshapes
00166         static MeasurementSet* setupMS( const String& msname, const Int nchan,
00167                                                                         const Int npol, const Vector<MS::PredefinedColumns>& colNamesTok,
00168                                                                         const Vector<Int>& tileShape = Vector<Int> (1, 0),
00169                                                                         const Bool compress = False,
00170                                                                         const asdmStManUseAlternatives asdmStManUse = DONT,
00171                                                                         Table::TableOption option=Table::New);
00172 
00173 
00174         // The output MS must have (at least?) 1 of DATA, FLOAT_DATA, or LAG_DATA.
00175         // MODEL_DATA or CORRECTED_DATA will be converted to DATA if necessary.
00176         // jagonzal (CAS-5327): The implementation has to go here because a member function cannot have static linkage
00177         static Bool mustConvertToData(  const uInt nTok,const Vector<MS::PredefinedColumns>& datacols)
00178         {
00179                 return (nTok == 1) && (datacols[0] != MS::FLOAT_DATA) && (datacols[0] != MS::LAG_DATA);
00180         }
00181 
00182         // A customized version of MS::createDefaultSubtables().
00183         static void createSubtables(MeasurementSet& ms, Table::TableOption option);
00184 
00185         // Sub-table fillers.
00186         Bool fillSubTables(const Vector<MS::PredefinedColumns>& colNames);
00187         Bool fillFieldTable();
00188         Bool fillDDTables(); // Includes spw and pol
00189 
00190         Bool fillPolTable();
00191         Bool fillDDITable();
00192         Bool fillSPWTable();
00193 
00194         // Add optional columns to outTab if present in inTab and possColNames.
00195         // beLazy should only be true if outTab is in its default state.
00196         // Returns the number of added columns.
00197         static uInt addOptionalColumns(const Table& inTab, Table& outTab,const Bool beLazy=false);
00198 
00199         // Sets up sourceRelabel_p for mapping input SourceIDs (if any) to output
00200         // ones.  Must be called after fieldid_p is set and before calling
00201         // fillFieldTable() or copySource().
00202         void relabelSources();
00203 
00204         // Adds and copies inTab to msOut_p without any filtering.
00205         // tabName is the table "type", i.e. POINTING or SYSPOWER
00206         // without the preceding path.
00207         //
00208         // If noRows is True, the structure will be setup but no
00209         // rows will be copied  (useful for filtering).
00210         void copySubtable(const String& tabName, const Table& inTab,const Bool noRows = False);
00211 
00212         // Sets mapper to to a map from the distinct values of inv,
00213         // in increasing order, to 0, 1, 2, ..., mapper.size() - 1.
00214         static void make_map(std::map<Int, Int>& mapper, const Vector<Int>& inv);
00215 
00216         Bool copyPointing();
00217         // Sets up the stub of a POINTING, enough to create an MSColumns.
00218         void setupNewPointing();
00219 
00220         Bool copySource();
00221         Bool copyAntenna();
00222         Bool copyFeed();
00223         Bool copyFlag_Cmd();
00224         Bool copyHistory();
00225         Bool copyObservation();
00226         Bool copyProcessor();
00227         Bool copyState();
00228         Bool copySyscal();
00229         Bool copyWeather();
00230         void copyMainTableKeywords (TableRecord& outKeys,
00231                         const TableRecord& inKeys);
00232 
00233         Int getProcessorId(Int dataDescriptionId, String msname);
00234 
00235         // This falls between copyGenericSubtables() and the copiers for standard
00236         // sub-tables like copyFeed().  It is for optional sub-tables like CALDEVICE
00237         // and SYSPOWER which can be watched for by name and may need their
00238         // ANTENNA_ID and SPECTRAL_WINDOW_ID columns re-mapped.
00239         // (Technically FEED_ID, too, if split ever starts re-mapping feeds.)
00240         //
00241         // It must be called BEFORE copyGenericSubtables()!
00242         //
00243         Bool filterOptSubtable(const String& subtabname);
00244 
00245         Bool copyGenericSubtables();
00246 
00247         // To consolidate several sub-tables when dealing with MMS
00248         static Bool mergeSpwSubTables(Vector<String> filenames);
00249         static Bool mergeDDISubTables(Vector<String> filenames);
00250         static Bool mergeFeedSubTables(Vector<String> filenames, Vector<uInt> mapSubmsSpwid);
00251         static Bool mergeSourceSubTables(Vector<String> filenames, Vector<uInt> mapSubmsSpwid);
00252         static Bool mergeSyscalSubTables(Vector<String> filenames, Vector<uInt> mapSubmsSpwid);
00253         static Bool mergeFreqOffsetTables(Vector<String> filenames, Vector<uInt> mapSubmsSpwid);
00254         static Bool mergeCalDeviceSubtables(Vector<String> filenames, Vector<uInt> mapSubmsSpwid);
00255         static Bool mergeSysPowerSubtables(Vector<String> filenames, Vector<uInt> mapSubmsSpwid);
00256 // -----------------------------------------------------------------------
00257 //
00258 // -----------------------------------------------------------------------
00259         //template <class T>  Bool MSTransformDataHandler::columnOk (ArrayColumn<T> column)
00260         template <class T>  static Bool columnOk(ArrayColumn<T> column)
00261         {
00262              Bool ret;
00263              // jagonzal (CAS-6206): ndimColumn only returns >0 is there is the array column has fixed size
00264              if (column.isNull()==false and column.hasContent()==true and column.ndim(0) > 0)
00265                {
00266                  ret = True;
00267                }
00268              else
00269                {
00270                  ret = False;
00271                }
00272 
00273              return ret;
00274         }
00275 
00276 // -----------------------------------------------------------------------
00277 //
00278 // -----------------------------------------------------------------------
00279         //  template <class T>  Bool MSTransformDataHandler::columnOk (ScalarColumn<T> column)
00280         template <class T>  static Bool columnOk(ScalarColumn<T> column)
00281         {
00282               Bool ret;
00283               if (column.isNull()==false and column.hasContent()==true)
00284                 {
00285                   ret = True;
00286                 }
00287               else
00288                 {
00289                   ret = False;
00290                 }
00291               
00292               return ret;
00293         }
00294 
00295 
00296 
00297         // Accesors for the MS objects
00298         MeasurementSet * getInputMS() {return &ms_p;};
00299         MeasurementSet * getSelectedInputMS() {return &mssel_p;};
00300         MeasurementSet * getOutputMS() {return &msOut_p;};
00301         ROMSColumns * getSelectedInputMSColumns() {return mscIn_p;};
00302         MSColumns * getOutputMSColumns() {return msc_p;};
00303 
00304         // Accesors for the Re-mapper objects
00305         map<Int, Int> & getStateRemapper() {return stateRemapper_p;};
00306         Vector<Int> & getAntennaRemapper() {return antNewIndex_p;};
00307         map<Int, vector<Int>> & getDroppedChannelsMap() {return spwDropChannelMap_p;};
00308         map<Int,map < Int, vector<Int> > > & getSelectedChannelsMap() {return spwSelectedChannelMap_p;};
00309 
00310         // Accesors for additional parameters
00311         void setVirtualModelCol(Bool virtualModelCol) {virtualModelCol_p = virtualModelCol;};
00312         void setVirtualCorrectedCol(Bool virtualCorrectedCol) {virtualCorrectedCol_p = virtualCorrectedCol;};
00313         void setReindex(Bool reindex) {reindex_p = reindex;};
00314 
00315 protected:
00316         // copy ephemeris table and reindex field table if requested
00317         Bool copyEphemerisTable(MSFieldColumns & msField);
00318 
00319         // Initialized* by ctors.  (Maintain order both here and in ctors.)
00320         //  * not necessarily to anything useful.
00321         MeasurementSet ms_p, mssel_p;
00322         MSColumns * msc_p; // columns of msOut_p
00323         ROMSColumns * mscIn_p;
00324         Bool keepShape_p, // Iff true, each output array has the
00325                         // same shape as the corresponding input one.
00326                         // sameShape_p,             // Iff true, the shapes of the arrays do not
00327                         //                      // vary with row number.
00328                         antennaSel_p; // Selecting by antenna?
00329         Double timeBin_p;
00330         String scanString_p, // Selects scans by #number#.  Historically named.
00331                         intentString_p, // Selects scans by string.  scanString_p was taken.
00332                         obsString_p, // String for observationID selection.
00333                         uvrangeString_p, taqlString_p, feedString_p;
00334         String timeRange_p, arrayExpr_p, corrString_p;
00335         String combine_p; // Should time averaging not split bins by
00336         // scan #, observation, and/or state ID?
00337         // Must be lowercase at all times.
00338         Int fitorder_p; // The polynomial order for continuum fitting.
00339         // If < 0 (default), continuum subtraction is
00340         // not done.
00341         String fitspw_p; // Selection string for line-free channels.
00342         String fitoutspw_p; // Selection string for output channels if doing
00343         // continuum subtraction.
00344 
00345         // Uninitialized by ctors.
00346         MeasurementSet msOut_p;
00347         Vector<Int> spw_p, // The input spw corresponding to each output spw.
00348                         spw_uniq_p, // Uniquified version of spw_p.
00349                          nchan_p, // The # of output channels for each range.
00350                         totnchan_p, // The # of output channels for each output spw.
00351                         chanStart_p, // 1st input channel index in a selection.
00352                         chanEnd_p, // last input channel index in a selection.
00353                         chanStep_p, // Increment between input chans, i.e. if 3, only every third
00354                         // input channel will be used.
00355                         widths_p, // # of input chans per output chan for each range.
00356                         ncorr_p, // The # of output correlations for each DDID.
00357                         inNumChan_p, // The # of input channels for each spw.
00358                         inNumCorr_p; // The # of input correlations for each DDID.
00359 
00360         map<Int,vector<Int> > spwDropChannelMap_p;
00361         map<Int,map < Int, vector<Int> > > spwSelectedChannelMap_p;
00362 
00363         Vector<Int> fieldid_p;
00364         Vector<Int> spwRelabel_p, fieldRelabel_p, sourceRelabel_p;
00365         Vector<Int> oldDDSpwMatch_p;
00366         Vector<String> antennaSelStr_p;
00367         Vector<Int> antennaId_p;
00368         Vector<Int> antIndexer_p;
00369         Vector<Int> antNewIndex_p;
00370 
00371         Vector<Int> selObsId_p; // List of selected OBSERVATION_IDs.
00372         Vector<Int> polID_p; // Map from input DDID to input polID, filled in fillDDTables().
00373         Vector<Int> spw2ddid_p;
00374 
00375         // inCorrInd = outPolCorrToInCorrMap_p[polID_p[ddID]][outCorrInd]
00376         Vector<Vector<Int> > inPolOutCorrToInCorrMap_p;
00377 
00378         std::map<Int, Int> stateRemapper_p;
00379 
00380         Vector<Vector<Slice> > chanSlices_p; // Used by VisIterator::selectChannel()
00381         Vector<Slice> corrSlice_p;
00382         Vector<Vector<Slice> > corrSlices_p; // Used by VisIterator::selectCorrelation()
00383         Matrix<Double> selTimeRanges_p;
00384 
00385         Bool virtualModelCol_p; // CAS-5348 (jagonzal): Make virtual MODEL data column real
00386         Bool virtualCorrectedCol_p; //CAS-7286 (jagonzal): Make virtual CORRECTED data column real
00387         Bool reindex_p; // jagonzal: In order not to re-index asub-tables
00388 
00389 };
00390 
00391 } //# NAMESPACE CASA - END
00392 
00393 #endif /* MSTransformDataHandler_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1