casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MSSelection.h
Go to the documentation of this file.
00001 //# MSSelection.h: Class to represent a selection on an MS
00002 //# Copyright (C) 1996,1997,1998,1999,2001
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 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: MSSelection.h 20750 2009-10-01 06:32:18Z Malte.Marquarding $
00028 
00029 #ifndef MS_MSSELECTION_H
00030 #define MS_MSSELECTION_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/BasicSL/String.h>
00034 #include <casa/Arrays/Vector.h>
00035 #include <measures/Measures/MEpoch.h>
00036 #include <measures/Measures/MRadialVelocity.h>
00037 #include <tables/Tables/ExprNode.h>
00038 #include <ms/MeasurementSets/MeasurementSet.h>
00039 #include <casa/Arrays/Matrix.h>
00040 #include <casa/Arrays/Cube.h>
00041 #include <ms/MeasurementSets/MSSelectionError.h>
00042 #include <ms/MeasurementSets/MSSelectionErrorHandler.h>
00043 #include <ms/MeasurementSets/MSSelectableTable.h>
00044 #include <casa/Containers/OrderedMap.h>
00045 #include <casa/Containers/MapIO.h>
00046 namespace casa { //# NAMESPACE CASA - BEGIN
00047 
00048 // <summary> 
00049 // MSSelection: Class to represent a selection on an MS
00050 // </summary>
00051 
00052 // <use visibility=export>
00053 
00054 // <reviewed reviewer="" date="" tests="" demos="">
00055 
00056 // <prerequisite>
00057 //   <li> <linkto class="MeasurementSet">MeasurementSet</linkto> module
00058 // </prerequisite>
00059 //
00060 // <etymology>
00061 // From "MeasurementSet" and "selection".
00062 // </etymology>
00063 //
00064 // <synopsis>
00065 // The MSSelection class represents a selection on a MeasurementSet (MS).
00066 // This class is used in translating MS selections represented as
00067 // selection items in the user interface, and for converting between
00068 // MS selection and pure TaQL selection.
00069 //
00070 // The purpose of this class is to provides a simple expression based
00071 // selection mechanism to both the end-user and developer wishing to
00072 // perform query operations over a measurement set.  This is
00073 // accomplished by abstracting the TaQL interface through an
00074 // adapter/translation interface which converts STaQL (Simple Table
00075 // Query Language) expressions into the equivalent table expression
00076 // form, reducing the knowledge necessary to perform powerful query
00077 // operations directly in TaQL.  It is also possible to supply pure
00078 // TaQL expression(s) as sub-expressions if required. For a complete
00079 // list of the STaQL interface refer to: <a
00080 // href="http://casa.nrao.edu/Memos/msselection/index.html">Data
00081 // Selection</a>
00082 //
00083 // The sub-expressions are interpreted in the order which they were
00084 // set.  The order however in not important - any dependency on the
00085 // order in which the expressions are evaluated is handled internally.
00086 // The result of parsing the expressions is TableExprNode (TEN).  All
00087 // TENs from sub-expressions are finally ANDed and the resultant TEN
00088 // is used to select the rows of the MS table.
00089 //
00090 // </synopsis>
00091 //
00092 // <example>
00093 // <srcblock>
00094 // // Create a MS and a MS selection
00095 // MeasurementSet ms(msName);
00096 // MSSelection select;
00097 // // Setup any sub-expressions of interest directly
00098 // // (or optionally send this information through a Record)
00099 // select.setFieldExpr("0,1");
00100 // select.setSpwExpr(">0");
00101 // // Create a table expression over a MS representing the selection
00102 // TableExprNode node = select.toTableExprNode(&ms);
00103 // // Optionally create a table and new MS based on this node
00104 // Table tablesel(ms.tableName(), Table::Update);
00105 // MeasurementSet mssel(tablesel(node, node.nrow()));
00106 // </srcblock>
00107 // </example>
00108 //
00109 // <motivation>
00110 // This class is used by the MS access classes.
00111 // </motivation>
00112 //
00113 // <todo asof="Aug/14/2009">
00114 // Generalize SpwExpressions and PolnExpressions to optionally include
00115 // DataDescription ID specifications.
00116 // </todo>
00117 
00118   class MSSelection
00119   {
00120   public:
00121     enum MSExprType {NO_EXPR = 0,
00122                      ANTENNA_EXPR,
00123                      CORR_EXPR,
00124                      FIELD_EXPR,
00125                      SPW_EXPR,
00126                      SCAN_EXPR,
00127                      ARRAY_EXPR,
00128                      TIME_EXPR,
00129                      UVDIST_EXPR,
00130                      POLN_EXPR,
00131                      STATE_EXPR,
00132                      OBSERVATION_EXPR,
00133                      TAQL_EXPR,
00134                      MAX_EXPR = TAQL_EXPR};
00135     enum MSSMode {PARSE_NOW=0, PARSE_LATE};
00136 
00137     // Default null constructor, and destructor
00138     MSSelection();
00139     virtual ~MSSelection();
00140     
00141     // Construct using an MS and the various selection expressions to
00142     // be applied to the given MS.  By default, the expressions will
00143     // be parsed immediately.  With mode=PARSE_LATE, the parsing will
00144     // be done with a call to toTableExprNode().
00145     MSSelection(const MeasurementSet& ms,
00146                 const MSSMode& mode=PARSE_NOW,
00147                 const String& timeExpr="",
00148                 const String& antennaExpr="",
00149                 const String& fieldExpr="",
00150                 const String& spwExpr="",
00151                 const String& uvDistExpr="",
00152                 const String& taqlExpr="",
00153                 const String& polnExpr="",
00154                 const String& scanExpr="",
00155                 const String& arrayExpr="",
00156                 const String& stateExpr="",
00157                 const String& observationExpr="");
00158     
00159     // Construct from a record representing a selection item at the
00160     // CLI or user interface level.  This is functionally same as the
00161     // constructor above with mode=PARSE_LATE.
00162     MSSelection(const Record& selectionItem);
00163     
00164     // Copy constructor
00165     MSSelection(const MSSelection& other);
00166     
00167     // Assignment operator
00168     MSSelection& operator=(const MSSelection& other);
00169     
00170     // Helper method for converting index vectors to expression strings
00171     static String indexExprStr(Vector<Int> index);
00172     
00173     // Helper method for converting name vectors to expression strings
00174     static String nameExprStr(Vector<String> name);
00175     
00176     // Expression accessors.  The following set*Expr() methods only
00177     // set the expressions.  Parsing is done with a call to
00178     // toTableExprNode().
00179     Bool setAntennaExpr(const String& antennaExpr);
00180     Bool setFieldExpr(const String& fieldExpr);
00181     Bool setSpwExpr(const String& spwExpr);
00182     Bool setScanExpr(const String& scanExpr);
00183     Bool setArrayExpr(const String& ArrayExpr);
00184     Bool setTimeExpr(const String& timeExpr);
00185     Bool setUvDistExpr(const String& uvDistExpr);
00186     Bool setTaQLExpr(const String& taqlExpr);
00187     Bool setPolnExpr(const String& polnExpr);
00188     Bool setStateExpr(const String& stateExpr);
00189     Bool setObservationExpr(const String& obervationExpr);
00190     
00191     // Accessor for result of parsing all of the selection
00192     // expressions.  The final TableExprNode (TEN) is the result of
00193     // ANDing the TENs for the individual expressions.
00194     TableExprNode getTEN(const MeasurementSet*ms = NULL);
00195 
00196     // Accessor for the list of the selected scan IDs.
00197     inline Vector<Int> getScanList(const MeasurementSet* ms=NULL) 
00198     {getTEN(ms); return scanIDs_p;}
00199 
00200     // Accessor for the list of the selected observation IDs.
00201     inline Vector<Int> getObservationList(const MeasurementSet* ms=NULL) 
00202     {getTEN(ms); return observationIDs_p;}
00203 
00204     // Accessor for the list of selected sub-array IDs.
00205     inline Vector<Int> getSubArrayList(const MeasurementSet* ms=NULL) 
00206     {getTEN(ms); return arrayIDs_p;}
00207     
00208     // Accessor for the list of antenna-1 of the selected baselines.
00209     // Antennas affected by the baseline negation operator have the
00210     // antenna IDs multiplied by -1.
00211     inline Vector<Int> getAntenna1List(const MeasurementSet* ms=NULL) 
00212     {// if (antenna1IDs_p.nelements() <= 0) 
00213         getTEN(ms); return antenna1IDs_p;}
00214     
00215     // Accessor for the list of antenna-2 of the selected baselines.
00216     // Antennas affected by the baseline negation operator have the
00217     // antenna IDs multiplied by -1.
00218     inline Vector<Int> getAntenna2List(const MeasurementSet* ms=NULL) 
00219     {// if (antenna2IDs_p.nelements() <= 0) 
00220         getTEN(ms); return antenna2IDs_p;}
00221     
00222     // Accessor for the list of selected baselines.  The list is a Nx2
00223     // Matrix with one row per baseline containing the antenna IDs of
00224     // the two antenna associated with the baseline.
00225     //
00226     // Baselines affected by the negation operator in the baseline
00227     // selection expression are reported with one or both the antenna
00228     // IDs multiplied by -1.  E.g. a baseline selection expression
00229     // "!1" will result in a baseline list
00230     //
00231     // [-1, 2],
00232     // [-1, 3],
00233     // [-1, 4],
00234     // ....
00235     //
00236     // The expression "!1&10" will result in a baseline list [-1,
00237     // -10].  Etc...
00238     //
00239     inline Matrix<Int> getBaselineList(const MeasurementSet* ms=NULL) 
00240     {getTEN(ms); return baselineIDs_p;}
00241     
00242     // Accessor for the list of selected field IDs.
00243     inline Vector<Int> getFieldList(const MeasurementSet* ms=NULL) 
00244     {// if (fieldIDs_p.nelements() <= 0) 
00245         getTEN(ms); return fieldIDs_p;}
00246 
00247     // Accessor for the list of selected state Obs_Modes.
00248     inline Vector<Int> getStateObsModeList(const MeasurementSet* ms=NULL) 
00249     {if (stateObsModeIDs_p.nelements() <= 0) getTEN(ms); return stateObsModeIDs_p;}
00250     
00251     // Accessor for the list of the specified time range(s) as the
00252     // start and end MJD values.  The time ranges are stored as columns,
00253     // i.e. the output Matrix is 2 x n_ranges.
00254     inline Matrix<Double> getTimeList(const MeasurementSet* ms=NULL)
00255     {getTEN(ms); return selectedTimesList_p;}
00256     
00257     // Accessor for the list of the specified uv-range(s) as the start
00258     // and end values in units used in the MS.
00259     inline Matrix<Double> getUVList(const MeasurementSet* ms=NULL) 
00260     {getTEN(ms); return selectedUVRange_p;}
00261     
00262     // Accessor for the list of user defined units for the
00263     // uv-range(s).  The uv-range(s) return by getUVList is always in
00264     // the units used in the MS.
00265     inline Vector<Bool> getUVUnitsList(const MeasurementSet* ms=NULL) 
00266     {getTEN(ms); return selectedUVUnits_p;}
00267 
00268     // Accessor for the list of the selected Spectral Window IDs.
00269     inline Vector<Int> getSpwList(const MeasurementSet* ms=NULL) 
00270     {// if (spwIDs_p.nelements() <= 0) 
00271         getTEN(ms); return spwIDs_p;}
00272     
00273     // Accessor for the table (as a nx4 Matrix) of the selected
00274     // Spectral Windows and associated ranges of selected channels.
00275     // Each row of the Matrix has the following elements:
00276     //
00277     //    SpwID StartCh StopCh Step
00278     //
00279     // where StartCh, StopCh and Step are the first and the last
00280     // selected channels and step is the step size.  If no step size
00281     // was supplied as part of the expression, the value of Step is
00282     // replaced with the value of the defaultStep parameter. Multiple
00283     // channel specifications for the same Spectral Window selection,
00284     // results in multiple rows in the Matrix. If sorted is True, the
00285     // rows of the output Matrix will be sorted by the SPW IDs (the
00286     // entries in the first column).
00287     Matrix<Int> getChanList(const MeasurementSet* ms=NULL, 
00288                             const Int defaultStep=1,
00289                             const Bool sorted=False);
00290 
00291     //
00292     // Same as getChanList, except that the channels and steps are in Hz.
00293     //    
00294     Matrix<Double> getChanFreqList(const MeasurementSet* ms=NULL, 
00295                                    const Bool sorted=False);
00296 
00297     // Accessor for the list of the selected Data Description IDs.
00298     inline Vector<Int> getDDIDList(const MeasurementSet* ms=NULL) 
00299     {if (ddIDs_p.nelements() <= 0) getTEN(ms); return ddIDs_p;}
00300 
00301     //
00302     // The key in the ordered map returned by getPolMap() is the
00303     // pol. ID. The value is a vector containing the list of in-row
00304     // indices to pick out the selected polarizations (or
00305     // equivalently, the list of indices for the vector in the
00306     // corrType column of the POLARIZATION sub-table). These are also
00307     // what the user intended (i.e., e.g. not all DD IDs due to user
00308     // POL expression might be selected due to SPW expressions).
00309     //
00310     inline OrderedMap<Int, Vector<Int> > getPolMap(const MeasurementSet* ms=NULL) 
00311     {getTEN(ms); return selectedPolMap_p;};
00312 
00313     inline OrderedMap<Int, Vector<Vector<Int> > > getCorrMap(const MeasurementSet* ms=NULL) 
00314     {getTEN(ms); return selectedSetupMap_p;};
00315 
00316     // Methods to convert the maps return by getChanList and
00317     // getCorrMap to a list of Slice which can be directly used by
00318     // Table system for in-row selection of frequency channels and
00319     // polarizations.
00320     void getChanSlices(Vector<Vector<Slice> >& chanslices, 
00321                        const MeasurementSet* ms=NULL, 
00322                        const Int defaultChanStep=1);
00323 
00324     void getCorrSlices(Vector<Vector<Slice> >& corrslices,
00325                        const MeasurementSet* ms=NULL);
00326     
00327     
00328     // Clear sub-expression and reset priority.  Default behavior is to
00329     // reset all sub-expressions.
00330     void clear(const MSExprType type=NO_EXPR);
00331     Bool exprIsNull(const MSExprType type=NO_EXPR);
00332     
00333     // Convey to the various parsers to delete the TENs they hold
00334     void deleteNodes();
00335 
00336     // Delete error handlers (mostly the internally allocated ones).
00337     void deleteErrorHandlers();
00338 
00339     // Convert to TableExprNode format (C++ interface to TaQL).  This
00340     // is now for purely backwards compatibility and ease of use.  It
00341     // internally constructs the MSSelectableTable from the supplied
00342     // MS and calls the generic version of toTableExprNode below
00343     // (which works with MSSelectableTable object).
00344     TableExprNode toTableExprNode(const MeasurementSet* ms);
00345 
00346     // Convert to TableExprNode format (C++ interface to TaQL).  The
00347     // MSSelectableTable is a pure-virtual base class which provides a
00348     // generic interface both to MeasurementSet and CalTable (in the
00349     // synthesis module) services used in MSSelection.  The actual
00350     // objects used for supplying MeasurementSet or CalTable to
00351     // MSSelection are MSInterface and CTInterface classes
00352     // respectively.  With this, MSSelection module can be used for
00353     // selection on MeasurementSet or CalTable.
00354     TableExprNode toTableExprNode(MSSelectableTable* msLike);
00355     
00356     // Return the selected MS.  The selected MS reflects only row
00357     // selections (as against in-row selections).  If outMSName != "",
00358     // the selected MS is also written to the disk (a shallow copy).
00359     //
00360     // For in-row selection, use the appropriate global function
00361     // mssSetData() MSSelectionTools.h which also returns the in-row
00362     // (corr/chan) slices that can be supplied to the VisIter object
00363     // for on-the-fly in-row selection.
00364     Bool getSelectedMS(MeasurementSet& selectedMS,
00365                        const String& outMSName="");
00366     
00367     void resetMS(const MeasurementSet& ms) {resetTEN(); ms_p=&ms;};
00368     void resetTEN() {fullTEN_p=TableExprNode();};
00369     
00370     void reset(const MeasurementSet& ms,
00371                const MSSMode& mode           = PARSE_NOW,
00372                const String& timeExpr        = "",
00373                const String& antennaExpr     = "",
00374                const String& fieldExpr       = "",
00375                const String& spwExpr         = "",
00376                const String& uvDistExpr      = "",
00377                const String& taqlExpr        = "",
00378                const String& polnExpr        = "",
00379                const String& scanExpr        = "",
00380                const String& arrayExpr       = "",
00381                const String& stateExpr       = "",
00382                const String& observationExpr = "");
00383     void reset(MSSelectableTable& msLike,
00384                const MSSMode& mode           = PARSE_NOW,
00385                const String& timeExpr        = "",
00386                const String& antennaExpr     = "",
00387                const String& fieldExpr       = "",
00388                const String& spwExpr         = "",
00389                const String& uvDistExpr      = "",
00390                const String& taqlExpr        = "",
00391                const String& polnExpr        = "",
00392                const String& scanExpr        = "",
00393                const String& arrayExpr       = "",
00394                const String& stateExpr       = "",
00395                const String& observationExpr = "");
00396     
00397     void setMaxScans(const Int& n) {maxScans_p=n;};
00398     void setMaxObs(const Int& n) {maxObs_p=n;};
00399     
00400     void setErrorHandler(const MSExprType type, MSSelectionErrorHandler* mssEH,
00401                          const Bool overRide=False);
00402 
00403     void runErrorHandler();
00404     const MeasurementSet* getMS(MSSelectableTable* msLike);
00405   private:
00406     // Set into the order of the selection expression
00407     Bool setOrder(MSSelection::MSExprType type);
00408     
00409     // Initialize from a Record representing a selection
00410     // item from the user interface or CLI
00411     void fromSelectionItem(const Record& selectionItem);
00412     
00413     // Check if record field exists and is not unset
00414     Bool definedAndSet(const Record& inpRec, const String& fieldName);
00415     
00416     // Convert an MS select string to TaQL
00417     //   const String msToTaQL(const String& msSelect) {};
00418     
00419     TableExprNode fullTEN_p;
00420     const MeasurementSet *ms_p;
00421     // Selection expressions
00422     String antennaExpr_p;
00423     String fieldExpr_p;
00424     String spwExpr_p;
00425     String scanExpr_p;
00426     String arrayExpr_p;
00427     String timeExpr_p;
00428     String uvDistExpr_p;
00429     String polnExpr_p;
00430     String taqlExpr_p;
00431     String stateExpr_p;
00432     String observationExpr_p;
00433     // Priority
00434     Vector<Int> exprOrder_p;
00435     Vector<Int> antenna1IDs_p,antenna2IDs_p,fieldIDs_p, spwIDs_p, scanIDs_p, arrayIDs_p,
00436       ddIDs_p,stateObsModeIDs_p, observationIDs_p;
00437     Matrix<Int> chanIDs_p;
00438     Matrix<Int> baselineIDs_p;
00439     Matrix<Double> selectedTimesList_p;
00440     Matrix<Double> selectedUVRange_p;
00441     Vector<Bool> selectedUVUnits_p;
00442     OrderedMap<Int, Vector<Int> > selectedPolMap_p;
00443     OrderedMap<Int, Vector<Vector<Int> > > selectedSetupMap_p;
00444     Int maxScans_p, maxObs_p, maxArray_p;
00445     MSSelectionErrorHandler* mssErrHandler_p;
00446     Bool isMS_p,toTENCalled_p;
00447   };
00448   
00449 } //# NAMESPACE CASA - END
00450 
00451 #endif
00452 
00453