casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MSFlagger.h
Go to the documentation of this file.
00001 //# MSFlagger.h: this defines MSFlagger, which implement flagging/editing
00002 //# Copyright (C) 1997,1998,1999,2000
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: MSFlagger.h 20838 2009-12-01 10:10:55Z gervandiepen $
00028 
00029 #ifndef MS_MSFLAGGER_H
00030 #define MS_MSFLAGGER_H
00031 
00032 #include <casa/aips.h>
00033 #include <casa/Containers/Record.h>
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 template <class T> class Array;
00038 class MSSelector;
00039 class Table;
00040 class String;
00041 
00042 // <summary>
00043 // MSFlagger specifies selections on a MeasurementSet
00044 // </summary>
00045 
00046 // <use visibility=export>
00047 
00048 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00049 // </reviewed>
00050 
00051 // <prerequisite>
00052 //   <li> MeasurementSet
00053 //   <li> SomeOtherClass
00054 //   <li> some concept
00055 // </prerequisite>
00056 //
00057 // <etymology>
00058 // MSFlagger is a class that sets flags in an MS
00059 // </etymology>
00060 //
00061 // <synopsis>
00062 // This class is used to change the flag and flag_history columns in
00063 // a MeasurementSet. It provides functions for automated flagging based on
00064 // clipping the data that is too far from the median value.
00065 // The ms DO  uses this class to allow flagging from glish or a GUI.
00066 //
00067 // <example> <srcblock>
00068 // MSFlagger msFlagger(myMS);
00069 // </srcblock></example>
00070 // </synopsis>
00071 //
00072 // <motivation>
00073 // Flagging/editing of data is a central requirement in data processing, this
00074 // class provides some simple flagging algorithms and the code
00075 // that modifies & creates flag columns in the MS.
00076 // </motivation>
00077 //
00078 // <thrown>
00079 //    <li>
00080 //    <li>
00081 // </thrown>
00082 //
00083 // <todo asof="yyyy/mm/dd">
00084 //   <li> add this feature
00085 // </todo>
00086 
00087 class MSFlagger
00088 {
00089 public:
00090   MSFlagger();
00091   
00092   // construct from an MSSelector object
00093   MSFlagger(MSSelector& msSel);
00094   
00095   // Copy constructor
00096   MSFlagger(const MSFlagger& other);
00097   
00098   // Assignment
00099   MSFlagger& operator=(const MSFlagger& other);
00100   
00101   ~MSFlagger();
00102 
00103   // Change or Set the MS this MSFlagger refers to.
00104   void setMSSelector(MSSelector& msSel);
00105   
00106   // Fill an internal buffer with the data item requested, similar to getData
00107   // except that the data is not returned, but kept around for further
00108   // processing. Only a single DATA related quantity can be requested, the
00109   // corresponding FLAG and FLAG_ROW columns are read automatically.
00110   // Reorder the data to 4d with ifr and time axis if ifrAxis is True.
00111   Bool fillDataBuffer(const String& item, Bool ifrAxis);
00112 
00113   // Difference the data, subtracting the average over a window of
00114   // specified width and taking the absolute value. Complex quantities are
00115   // turned into the corresponding amplitude after differencing.
00116   // If doMedian==True the median difference is returned for window>2.
00117   // For a window width of one, the previous sample is
00118   // subtracted, giving a derivative like quantity.
00119   // Note that the subtraction is done on row-by-row basis for TIME
00120   // differencing, it is up to you to select a single baseline (if
00121   // you didn't use ifrAxis=True in fillDataBuffer).
00122   // Available directions are: TIME, CHANNEL
00123   // Returns statistics over the buffer: median for times and channels,
00124   // average absolute deviation over times, channels and all pixels.
00125   Record diffDataBuffer(const String& direction, Int window=1,
00126                              Bool doMedian = False);
00127 
00128   // Return the contents of the internal data buffer, including the flags
00129   // as a Record
00130   Record getDataBuffer()
00131   { return buffer_p;}
00132 
00133   // Clip the data buffer at a specified level by setting the corresponding
00134   // flags in the buffer. The cliplevel is specified as a multiple of
00135   // the average absolute deviations returned by diffDataBuffer.
00136   // A value of zero or less will skip the corresponding clip operation.
00137   // Clipping will be done repeatedly, recalculating the deviations, until
00138   // no more points are clipped.
00139   Bool clipDataBuffer(Float pixelLevel, Float timeLevel, Float channelLevel);
00140 
00141   // Replace the flags in the buffer with those in the supplied record.
00142   // This allows interactive flagging from glish to be written back to the
00143   // buffer for subsequent operations. The record should contain a
00144   // flag and flag_row field.
00145   Bool setDataBufferFlags(const Record& flags);
00146 
00147   // Write the flags in the buffer back to the table
00148   Bool writeDataBufferFlags();
00149 
00150   // Clear the internal data buffer, reclaiming memory
00151   Bool clearDataBuffer()
00152   { buffer_p=Record(); return True;}
00153 
00154   // Create the FLAG_HISTORY column and initialize it from the
00155   // FLAG_ROW and FLAG columns. Returns False if FLAG_HISTORY already exists.
00156   // The first flagging bit is filled with the flags as found in the MS,
00157   // subsequent bits can be used for user generated flags.
00158   Bool createFlagHistory(Int nHis = 2);
00159 
00160   // Apply the flags in the FLAG_HISTORY column to the FLAG and FLAG_ROW
00161   // columns. Returns False if FLAG_HISTORY doesn't exist.
00162   // The default argument will apply the currently active flag level
00163   // (as specified by the FLAG_LEVEL column keyword).
00164   // Sets the current level to the flag level restored.
00165   Bool restoreFlags(Int level=-1);
00166   
00167   // Save the current flags to the FLAG_HISTORY. Save to the currently
00168   // active level or (newLevel=True) the next highest level (if available).
00169   // Will reset the current level to the level saved to.
00170   Bool saveFlags(Bool newLevel);
00171 
00172   // Return the current flaglevel (value of FLAG_LEVEL keyword)
00173   Int flagLevel();
00174 
00175 protected:
00176   // fill the FLAG_HISTORY column from the FLAG and FLAG_ROW column
00177   void fillFlagHist(Int nHis, Int numCorr, Int numChan, Table& tab);
00178 
00179   // find the HypercubeId column for a tiled column (if any)
00180   Bool findHypercubeId(String& hyperCubeId, const String& column, 
00181                        const Table& tab);
00182 
00183   // copy the flags to the flag history
00184   void saveToFlagHist(Int level, Table& tab);
00185 
00186   // copy the flag history back to the flags
00187  void applyFlagHist(Int level, Table& tab);
00188 
00189   // get buffer statistics - med=median, ad=average absolute deviation,
00190   // T=Time, F=Frequency.
00191   void getStats(Array<Float>& medTF, Array<Float>& adTF, 
00192                 Array<Float>& medT, Array<Float>& medFmedT, 
00193                 Array<Float>& adT, Array<Float>& medF, 
00194                 Array<Float>& medTmedF, Array<Float>& adF,
00195                 const Array<Float>& diff, const Array<Bool>& flag,
00196                 const Array<Bool>& flagRow);
00197 
00198   // add the statistics to a buffer
00199   void addStats(Record& buf, const Array<Bool>& flag,
00200                 const Array<Bool> flagRow, const Array<Float>& data);
00201 
00202   // reorder from 2d to 1d (removing ifr axis)
00203   void reorderFlagRow(Array<Bool>& flagRow);
00204 
00205   // collapse array "in" (with absolute differences) 
00206   // along specified axis by taking medians by profile taking into account
00207   // the flags.
00208   void diffMedian(Array<Float>& out, const Array<Float>& in, 
00209                   Int axis, const Array<Bool>& flag);
00210 
00211   // apply the row flags to the data flags and v.v.
00212   void applyRowFlags(Array<Bool>& flag, Array<Bool>& flagRow);
00213 
00214   // check if we are attached to an MSSelector
00215   Bool check();
00216 
00217 private:
00218   MSSelector* msSel_p; 
00219   Record buffer_p;
00220 };
00221 
00222 
00223 } //# NAMESPACE CASA - END
00224 
00225 #endif
00226 
00227 
00228 
00229 
00230 
00231 
00232