00001 //# MomentsBase.h: base class for moment generator 00002 //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003 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 //# $Id: MomentsBase.h 20299 2008-04-03 05:56:44Z gervandiepen $ 00027 00028 #ifndef IMAGES_MOMENTSBASE_H 00029 #define IMAGES_MOMENTSBASE_H 00030 00031 #include <casa/aips.h> 00032 #include <coordinates/Coordinates/CoordinateSystem.h> 00033 #include <casa/Quanta/Quantum.h> 00034 #include <measures/Measures/MDoppler.h> 00035 #include <casa/Logging/LogIO.h> 00036 #include <casa/Arrays/Vector.h> 00037 #include <casa/iosfwd.h> 00038 00039 namespace casa { 00040 00041 template <class T> class MomentCalcBase; 00042 class IPosition; 00043 class String; 00044 class Unit; 00045 00046 // <summary> 00047 // This class is a base class for generating moments from an image or a spectral data. 00048 // </summary> 00049 00050 // <use visibility=export> 00051 00052 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos=""> 00053 // </reviewed> 00054 00055 // <prerequisite> 00056 // <li> <linkto class="ImageMoments">ImageMoments</linkto> 00057 // <li> <linkto class="MSMoments">MSMoments</linkto> 00058 // <li> <linkto class="LatticeApply">LatticeApply</linkto> 00059 // <li> <linkto class="MomentCalcBase">MomentCalcBase</linkto> 00060 // </prerequisite> 00061 00062 // <etymology> 00063 // This class is an abstract class to compute moments from images or spectral data. 00064 // </etymology> 00065 00066 // <synopsis> 00067 // The primary goal of MSMoments, ImageMoments, and MSMoments are to help 00068 // spectral-line astronomers analyze their multi-dimensional images or 00069 // spectral data (in the form of MeasurementSet) by generating moments of 00070 // a specified axis. ImageMoments is a specialized class to generate moments 00071 // from images, while MSMoments is designed properly for MeasurementSet input. 00072 // MSMoments class is an abstract class that is inherited by the above two 00073 // concrete classes. 00074 // MomentsBase provides interface layer to the MomentCalculators so that 00075 // functionalities in MomentCalculators can be shared with ImageMoments and 00076 // MSMoments. 00077 // The word "moment" is used loosely here. It refers to collapsing an axis 00078 // to one pixel and putting the value of that pixel (for all of the other 00079 // non-collapsed axes) to something computed from the data values along 00080 // the moment axis. For example, take an RA-DEC-Velocity cube, collapse 00081 // the velocity axis by computing the mean intensity at each RA-DEC 00082 // pixel. This class and its inheritances offer many different moments and a variety of 00083 // interactive and automatic ways to compute them. 00084 // 00085 00086 // <motivation> 00087 // MSMoments is defined so that moments can be generated from both images 00088 // and spectral data (in the form of MeasurementSet). 00089 // </motivation> 00090 00091 00092 template <class T> class MomentsBase { 00093 public: 00094 00095 // Note that if I don't put MomentCalcBase as a forward declaration 00096 // and use instead "friend class MomentCalcBase<T>" The gnu compiler 00097 // fails with a typedef problem. But I can't solve it with say 00098 // <src>typedef MomentCalcBase<T> gpp_type;</src> because you can only do a 00099 // typedef with an actual type, not a <tt>T</tt> ! 00100 friend class MomentCalcBase<T>; 00101 00102 // The <src>enum MethodTypes</src> is provided for use with the 00103 // <src>setWinFitMethod</src> function. It gives the allowed moment 00104 // methods which are available with this function. The use of these 00105 // methods is described further with the description of this function 00106 // as well as in the general documentation earlier. 00107 enum MethodTypes { 00108 // Invokes the spectral windowing method 00109 WINDOW, 00110 // Invokes Gaussian fitting 00111 FIT, 00112 NMETHODS 00113 }; 00114 00115 // This <src>enum MomentTypes</src> is provided for use with the 00116 // <src>setMoments</src> function. It gives the allowed moment 00117 // types that you can ask for. 00118 00119 enum MomentTypes { 00120 // The average intensity 00121 AVERAGE, 00122 // The integrated intensity 00123 INTEGRATED, 00124 // The intensity weighted mean coordinate (usually velocity) 00125 WEIGHTED_MEAN_COORDINATE, 00126 // The intensity weighted coordinate (usually velocity) dispersion 00127 WEIGHTED_DISPERSION_COORDINATE, 00128 // The median intensity 00129 MEDIAN, 00130 // The median coordinate (usually velocity). Treat the spectrum as 00131 // a probability distribution, generate the cumulative distribution, 00132 // and find the coordinate corresponding to the 50% value. 00133 MEDIAN_COORDINATE, 00134 // The standard deviation about the mean of the intensity 00135 STANDARD_DEVIATION, 00136 // The rms of the intensity 00137 RMS, 00138 // The absolute mean deviation of the intensity 00139 ABS_MEAN_DEVIATION, 00140 // The maximum value of the intensity 00141 MAXIMUM, 00142 // The coordinate (usually velocity) of the maximum value of the intensity 00143 MAXIMUM_COORDINATE, 00144 // The minimum value of the intensity 00145 MINIMUM, 00146 // The coordinate (usually velocity) of the minimum value of the intensity 00147 MINIMUM_COORDINATE, 00148 // Total number 00149 NMOMENTS, 00150 // Default value is the integrated intensity 00151 DEFAULT = INTEGRATED 00152 }; 00153 00154 // Destructor 00155 virtual ~MomentsBase(); 00156 00157 // Set the desired moments via an <src>Int</src> array. Each <src>Int</src> 00158 // specifies a different moment; the allowed values and their meanings 00159 // are given by the <src>enum MomentTypes</src>. A return value 00160 // of <src>False</src> indicates you asked for an out of range 00161 // moment. If you don't call this function, the default state of the 00162 // class is to request the integrated intensity. 00163 Bool setMoments (const Vector<Int>& moments); 00164 00165 // Set the moment axis (0 relative). A return value of <src>False</src> 00166 // indicates that the axis was not contained in the image. If you don't 00167 // call this function, the default state of the class is to set the 00168 // moment axis to the spectral axis if it can find one. Otherwise 00169 // an error will result. 00170 virtual Bool setMomentAxis (Int) = 0; 00171 00172 // The method by which you compute the moments is specified by calling 00173 // (or not calling) the <src>setWinFitMethod</src> and 00174 // <src>setSmoothMethod</src> functions. The default state of the class 00175 // is to compute directly on all (or some according to <src>setInExClude</src>) 00176 // of the pixels in the spectrum. Calling these functions modifies the 00177 // computational state to something more complicated. 00178 // 00179 // The <src>setWinMethod</src> function requires an <src>Int</src> array 00180 // as its argument. Each <src>Int</src> specifies a different method 00181 // that you can invoke (either separately or in combination). The 00182 // allowed values and their meanings are given by the 00183 // <src>enum MethodTypes</src>. 00184 // 00185 // Both the windowing and fitting methods have interactive modes. The 00186 // windowing method also has a fitting flavour, so if you set both 00187 // MomentsBase::WINDOW and MomentsBase::FIT, you would be invoking the 00188 // windowing method but determining the window by fitting Gaussians 00189 // automatically (as MomentsBase::INTERACTIVE) was not given. 00190 // 00191 // If you don't call this function, then neither the windowing nor fitting 00192 // methods are invoked. A return value of <src>False</src> indicates 00193 // that you asked for an illegal method. 00194 Bool setWinFitMethod(const Vector<Int>& method); 00195 00196 // This function invokes smoothing of the input image. Give <src>Int</src> 00197 // arrays for the axes (0 relative) to be smoothed and the smoothing kernel 00198 // types (use the <src>enum KernelTypes</src>) for each axis. Give a 00199 // <src>Double</src> array for the widths (full width for BOXCAR and full 00200 // width at half maximum for GAUSSIAN) in pixels of the smoothing kernels for 00201 // each axis. For HANNING smoothing, you always get the quarter-half-quarter 00202 // kernel (no matter what you might ask for). A return value of <src>False</src> 00203 // indicates that you have given an inconsistent or invalid set of smoothing 00204 // parameters. If you don't call this function the default state of the 00205 // class is to do no smoothing. The kernel types are specified with 00206 // the VectorKernel::KernelTypes enum 00207 // <group> 00208 virtual Bool setSmoothMethod( 00209 const Vector<Int>&, const Vector<Int>&, 00210 const Vector<Quantum<Double> >& 00211 ) = 0; 00212 00213 Bool setSmoothMethod( 00214 const Vector<Int>& smoothAxes, 00215 const Vector<Int>& kernelTypes, 00216 const Vector<Double>& kernelWidths 00217 ); 00218 // </group> 00219 00220 // You may specify a pixel intensity range as either one for which 00221 // all pixels in that range are included in the moment calculation, 00222 // or one for which all pixels in that range are excluded from the moment 00223 // calculations. One or the other of <src>include</src> and <src>exclude</src> 00224 // must therefore be a zero length vector if you call this function. 00225 // A return value of <src>False</src> indicates that you have given both 00226 // an <src>include</src> and an <src>exclude</src> range. If you don't call 00227 // this function, the default state of the class is to include all pixels. 00228 void setInExCludeRange( 00229 const Vector<T>& include, const Vector<T>& exclude 00230 ); 00231 00232 // This function is used to help assess whether a spectrum along the moment 00233 // axis is all noise or not. If it is all noise, there is not a lot of point 00234 // to trying to computing the moment. 00235 // <src>peakSNR</src> is the signal-to-noise ratio of the peak value in the 00236 // spectrum below which the spectrum is considered pure noise. 00237 // <src>stdDeviation</src> is the standard deviation of the noise for the 00238 // input image. 00239 // 00240 // Default values for one or the other parameter are indicated by giving zero. 00241 // 00242 // The default state of the class then is to set <src>peakSNR=3</src> 00243 // and/or to work out the noise level from a Gaussian fit to a histogram 00244 // (above 25%) of the entire image (it is very hard to get an accurate 00245 // estimate of the noise a single spectrum). 00246 void setSnr(const T& peakSNR, const T& stdDeviation); 00247 00248 // This is the output file name for the smoothed image. It can be useful 00249 // to have access this to this image when trying to get the pixel 00250 // <src>include</src> or <src>exclude</src> range correct for the smooth-clip 00251 // method. The default state of the class is to not output the smoothed image. 00252 Bool setSmoothOutName(const String& smOut); 00253 00254 // Set Velocity type. This is used for moments for which the moment axis is 00255 // a spectral axis for which the coordinate is traditionally presented in 00256 // km/s You can select the velocity definition. The default state of the 00257 // class is to use the radio definition. 00258 void setVelocityType (MDoppler::Types type); 00259 00260 // Reset argument error condition. If you specify invalid arguments to 00261 // one of the above functions, an internal flag will be set which will 00262 // prevent the <src>createMoments</src> function, which is defined in its inheritances, 00263 // from doing anything 00264 // (should you have chosen to igmore the Boolean return values of the functions). 00265 // This function allows you to reset that internal state to good. 00266 void resetError () {goodParameterStatus_p = True; error_p = "";}; 00267 00268 // Recover last error message 00269 String errorMessage() const {return error_p;}; 00270 00271 // Get CoordinateSystem 00272 virtual const CoordinateSystem& coordinates() = 0; 00273 00274 // Helper function to convert a string containing a list of desired methods to 00275 // the correct <src>Vector<Int></src> required for the <src>setWinFitMethod</src> function. 00276 // This may be usful if your user interface involves strings rather than integers. 00277 // A new value is added to the output vector (which is resized appropriately) if any of the 00278 // substrings "window", "fit" or "interactive" (actually "win", "box" and 00279 // "inter" will do) is present. 00280 static Vector<Int> toMethodTypes (const String& methods); 00281 00282 virtual IPosition getShape() const = 0; 00283 00284 Bool shouldConvertToVelocity() const { return convertToVelocity_p; } 00285 00286 protected: 00287 00288 // Constructor takes an image and a <src>LogIO</src> object for logging purposes. 00289 // You specify whether output images are automatically overwritten if pre-existing, 00290 // or whether an intercative choice dialog widget appears (overWriteOutput=F) 00291 // You may also determine whether a progress meter is displayed or not. 00292 MomentsBase( 00293 LogIO &os, Bool overWriteOutput=False, 00294 Bool showProgress=True 00295 ); 00296 00297 LogIO os_p; 00298 Bool showProgress_p; 00299 Int momentAxisDefault_p {-10}; 00300 T peakSNR_p {T(3)}; 00301 T stdDeviation_p {T(0)}; 00302 T yMin_p {T(0)}; 00303 T yMax_p {T(0)}; 00304 String out_p; 00305 String smoothOut_p {}; 00306 Bool goodParameterStatus_p {True}; 00307 Bool doWindow_p {False}; 00308 Bool doFit_p {False}; 00309 Bool doSmooth_p {False}; 00310 Bool noInclude_p {True}; 00311 Bool noExclude_p {True}; 00312 Bool fixedYLimits_p {False}; 00313 00314 Int momentAxis_p {momentAxisDefault_p}; 00315 Int worldMomentAxis_p; 00316 Vector<Int> kernelTypes_p {}; 00317 Vector<Quantity> kernelWidths_p {}; 00318 Vector<Int> moments_p {1, INTEGRATED}; 00319 Vector<T> selectRange_p {}; 00320 Vector<Int> smoothAxes_p {}; 00321 Bool overWriteOutput_p; 00322 String error_p {}; 00323 Bool convertToVelocity_p {False}; 00324 MDoppler::Types velocityType_p {MDoppler::RADIO}; 00325 00326 // Check that the combination of methods that the user has requested is valid 00327 // List a handy dandy table if not. 00328 void _checkMethod(); 00329 00330 // Take the user's data inclusion and exclusion data ranges and 00331 // generate the range and Booleans to say what sort it is 00332 void _setIncludeExclude ( 00333 Vector<T>& range, Bool& noInclude, 00334 Bool& noExclude, const Vector<T>& include, 00335 const Vector<T>& exclude 00336 ); 00337 00338 // Set the output image suffixes and units 00339 Bool _setOutThings( 00340 String& suffix, Unit& momentUnits, 00341 const Unit& imageUnits, const String& momentAxisUnits, 00342 const Int moment, Bool convertToVelocity 00343 ); 00344 00345 // Make output Coordinates 00346 CoordinateSystem _makeOutputCoordinates( 00347 IPosition& outShape, const CoordinateSystem& cSysIn, 00348 const IPosition& inShape, Int momentAxis, 00349 Bool removeAxis 00350 ); 00351 }; 00352 00353 } 00354 00355 #ifndef AIPS_NO_TEMPLATE_SRC 00356 #include <imageanalysis/ImageAnalysis/MomentsBase.tcc> 00357 #endif 00358 00359 #endif