00001 //# UtilsTVI.h: This file contains the interface definition of the MSTransformManager 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 UtilsTVI_H_ 00024 #define UtilsTVI_H_ 00025 00026 // casacore containers 00027 #include <casacore/casa/Arrays/Cube.h> 00028 #include <casacore/casa/Arrays/Matrix.h> 00029 #include <casacore/casa/Arrays/Vector.h> 00030 #include <casacore/casa/Containers/Record.h> 00031 00032 // Measurement Set 00033 #include <casacore/ms/MeasurementSets/MeasurementSet.h> 00034 00035 // Standard Lib 00036 #include <map> 00037 #include <float.h> 00038 00039 // NOTE: See implementation include below 00040 00041 00042 namespace casa { //# NAMESPACE CASA - BEGIN 00043 00044 namespace vi { //# NAMESPACE VI - BEGIN 00045 00046 00048 // DataCubeHolderBase class 00050 00051 class DataCubeHolderBase 00052 { 00053 00054 public: 00055 00056 DataCubeHolderBase() {} 00057 virtual ~DataCubeHolderBase() {} 00058 virtual DataCubeHolderBase * selfReference() = 0; 00059 virtual void setMatrixIndex(uInt matrixIndex) = 0; 00060 virtual void setVectorIndex(uInt vectorIndex) = 0; 00061 uInt getMatrixIndex(); 00062 uInt getVectorIndex(); 00063 IPosition & getCubeShape(); 00064 IPosition & getMatrixShape(); 00065 IPosition & getVectorShape(); 00066 00067 protected: 00068 00069 uInt matrixIndex_p; 00070 uInt vectorIndex_p; 00071 IPosition cubeShape_p; 00072 IPosition matrixShape_p; 00073 IPosition vectorShape_p; 00074 }; 00075 00077 // DataCubeHolder class 00079 00080 template <class T> class DataCubeHolder : public DataCubeHolderBase 00081 { 00082 00083 public: 00084 00085 DataCubeHolder(Cube<T> &dataCube) 00086 { 00087 cube_p.reference(dataCube); 00088 cubeShape_p = cube_p.shape(); 00089 } 00090 00091 DataCubeHolder(const Cube<T> &dataCube) 00092 { 00093 cube_p.reference(dataCube); 00094 cubeShape_p = cube_p.shape(); 00095 } 00096 00097 DataCubeHolder(Matrix<T> &dataMatrix) 00098 { 00099 matrix_p.reference(dataMatrix); 00100 matrixShape_p = matrix_p.shape(); 00101 } 00102 00103 DataCubeHolder(const Matrix<T> &dataMatrix) 00104 { 00105 matrix_p.reference(dataMatrix); 00106 matrixShape_p = matrix_p.shape(); 00107 } 00108 00109 DataCubeHolder(Vector<T> &dataVector) 00110 { 00111 vector_p.reference(dataVector); 00112 vectorShape_p = vector_p.shape(); 00113 } 00114 00115 DataCubeHolder(const Vector<T> &dataVector) 00116 { 00117 vector_p.reference(dataVector); 00118 vectorShape_p = vector_p.shape(); 00119 } 00120 00121 Matrix<T> & getMatrix() {return matrix_p;} 00122 Vector<T> & getVector() {return vector_p;} 00123 00124 void setMatrixIndex(uInt matrixIndex) 00125 { 00126 matrix_p.resize(); // Resize to 0 to avoid shape conformance problems 00127 matrixIndex_p = matrixIndex; 00128 matrix_p.reference(cube_p.xyPlane(matrixIndex)); 00129 matrixShape_p = matrix_p.shape(); 00130 00131 return; 00132 } 00133 00134 void setVectorIndex(uInt vectorIndex) 00135 { 00136 vector_p.resize(); // Resize to 0 to avoid shape conformance problems 00137 vectorIndex_p = vectorIndex; 00138 vector_p.reference(matrix_p.row(vectorIndex)); 00139 vectorShape_p = vector_p.shape(); 00140 00141 return; 00142 } 00143 00144 DataCubeHolderBase * selfReference() 00145 { 00146 DataCubeHolder<T> *selfRef = new DataCubeHolder<T>(cube_p); 00147 return static_cast<DataCubeHolderBase*>(selfRef); 00148 } 00149 00150 00151 protected: 00152 00153 Cube<T> cube_p; 00154 Matrix<T> matrix_p; 00155 Vector<T> vector_p; 00156 }; 00157 00159 // DataCubeMap class 00161 00162 class DataCubeMap 00163 { 00164 00165 public: 00166 00167 DataCubeMap(); 00168 DataCubeMap(DataCubeMap& other); 00169 ~DataCubeMap(); 00170 00171 void add(MS::PredefinedColumns key,DataCubeHolderBase* dataCubeHolder); 00172 void add(MS::PredefinedColumns key,DataCubeHolderBase &dataCubeHolder); 00173 00174 Bool present(MS::PredefinedColumns key); 00175 00176 template <class T> Vector<T> & getVector(MS::PredefinedColumns key) 00177 { 00178 DataCubeHolder<T> *dataCubeHolder = static_cast< DataCubeHolder<T>* >(dataCubeMap_p[key]); 00179 return dataCubeHolder->getVector(); 00180 } 00181 00182 template <class T> Matrix<T> & getMatrix(MS::PredefinedColumns key) 00183 { 00184 DataCubeHolder<T> *dataCubeHolder = static_cast< DataCubeHolder<T>* >(dataCubeMap_p[key]); 00185 return dataCubeHolder->getVector(); 00186 } 00187 00188 void setMatrixIndex(uInt rowIndex); 00189 void setVectorIndex(uInt vectorIndex); 00190 00191 IPosition & getCubeShape(); 00192 IPosition & getMatrixShape(); 00193 IPosition & getVectorShape(); 00194 00195 size_t nelements(); 00196 00197 00198 protected: 00199 00200 std::map<MS::PredefinedColumns, DataCubeHolderBase*> dataCubeMap_p; 00201 std::map<MS::PredefinedColumns, DataCubeHolderBase*>::iterator dataCubeMapIter_p; 00202 }; 00203 00204 00206 // Convenience methods 00208 00209 inline Float weightToSigma (Float weight) 00210 { 00211 return weight > FLT_MIN ? 1.0 / std::sqrt (weight) : -1.0; 00212 } 00213 00214 inline Float sigmaToWeight (Float sigma) 00215 { 00216 return sigma > FLT_MIN ? 1.0 / (sigma * sigma) : 0.0; 00217 } 00218 00219 void accumulateWeightCube ( const Cube<Float> &weightCube, 00220 const Cube<Bool> &flags, 00221 Matrix<Float> &result); 00222 00223 void accumulateWeightMatrix ( const Matrix<Float> &weightMatrix, 00224 const Matrix<Bool> &flags, 00225 Vector<Float> &result); 00226 00227 void accumulateFlagCube ( const Cube<Bool> &flagCube, 00228 Vector<Bool> &flagRow); 00229 00230 00231 00232 } //# NAMESPACE VI - END 00233 00234 } //# NAMESPACE CASA - END 00235 00236 00237 #endif /* UtilsTVI_H_ */ 00238