casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Utils.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //# Utils.h: Definition of global functions in Utils.cc
00003 //# Copyright (C) 1997,1998,1999,2000,2001,2002,2003
00004 //# Associated Universities, Inc. Washington DC, USA.
00005 //#
00006 //# This library is free software; you can redistribute it and/or modify it
00007 //# under the terms of the GNU Library General Public License as published by
00008 //# the Free Software Foundation; either version 2 of the License, or (at your
00009 //# option) any later version.
00010 //#
00011 //# This library is distributed in the hope that it will be useful, but WITHOUT
00012 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 //# License for more details.
00015 //#
00016 //# You should have received a copy of the GNU Library General Public License
00017 //# along with this library; if not, write to the Free Software Foundation,
00018 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00019 //#
00020 //# Correspondence concerning AIPS++ should be addressed as follows:
00021 //#        Internet email: aips2-request@nrao.edu.
00022 //#        Postal address: AIPS++ Project Office
00023 //#                        National Radio Astronomy Observatory
00024 //#                        520 Edgemont Road
00025 //#                        Charlottesville, VA 22903-2475 USA
00026 //#
00027 //# $Id$
00028 #include <casa/aips.h>
00029 #include <casa/Exceptions/Error.h>
00030 #include <synthesis/MSVis/VisBuffer.h>
00031 #include <casa/Quanta/Quantum.h>
00032 #include <ms/MeasurementSets/MSColumns.h>
00033 #include <ms/MeasurementSets/MSRange.h>
00034 #include <images/Images/ImageInterface.h>
00035 #include <ms/MeasurementSets/MeasurementSet.h>
00036 #include <casa/Arrays/Array.h>
00037 #include <casa/Logging/LogIO.h>
00038 
00039 #ifndef SYNTHESIS_UTILS_H
00040 #define SYNTHESIS_UTILS_H
00041 
00042 namespace casa
00043 {
00044   Int getPhaseCenter(MeasurementSet& ms, MDirection& dir0, Int whichField=-1);
00045   Bool findMaxAbsLattice(const ImageInterface<Float>& lattice,
00046                          Float& maxAbs,IPosition& posMaxAbs);
00047   Bool findMaxAbsLattice(const ImageInterface<Float>& masklat,
00048                          const Lattice<Float>& lattice,
00049                          Float& maxAbs,IPosition& posMaxAbs, 
00050                          Bool flip=False);
00051   Double getCurrentTimeStamp(const VisBuffer& vb);
00052   void makeStokesAxis(Int npol_p, Vector<String>& polType, Vector<Int>& whichStokes);
00053   Double getPA(const VisBuffer& vb);
00054   void storeImg(String fileName,ImageInterface<Complex>& theImg, Bool writeReIm=False);
00055   void storeImg(String fileName,ImageInterface<Float>& theImg);
00056   void storeArrayAsImage(String fileName, const CoordinateSystem& coords, const Array<Complex>& cf);
00057   void storeArrayAsImage(String fileName, const CoordinateSystem& coords, const Array<Float>& cf);
00058                          
00059   Bool isVBNaN(const VisBuffer& vb, String& mesg);
00060   namespace SynthesisUtils
00061   {
00062     void rotateComplexArray(LogIO& logIO, Array<Complex>& inArray, 
00063                             CoordinateSystem& inCS,
00064                             Array<Complex>& outArray, 
00065                             Double dAngleRad, 
00066                             String interpMathod=String("CUBIC"),
00067                             Bool modifyInCS=True);
00068     void findLatticeMax(const Array<Complex>& lattice,
00069                         Vector<Float>& maxAbs,
00070                         Vector<IPosition>& posMaxAbs) ;
00071     void findLatticeMax(const ImageInterface<Complex>& lattice,
00072                         Vector<Float>& maxAbs,
00073                         Vector<IPosition>& posMaxAbs) ;
00074     void findLatticeMax(const ImageInterface<Float>& lattice,
00075                         Vector<Float>& maxAbs,
00076                         Vector<IPosition>& posMaxAbs) ;
00077     inline  Int nint(const Double& v) {return (Int)std::floor(v+0.5);}
00078     inline  Int nint(const Float& v) {return (Int)std::floor(v+0.5);}
00079     inline  Bool near(const Double& d1, const Double& d2, 
00080                       const Double EPS=1E-6) 
00081     {
00082       Bool b1=(fabs(d1-d2) < EPS)?True:False;
00083       return b1;
00084     }
00085     template <class T>
00086     inline void SETVEC(Vector<T>& lhs, const Vector<T>& rhs)
00087     {lhs.resize(rhs.shape()); lhs = rhs;};
00088     template <class T>
00089     inline void SETVEC(Array<T>& lhs, const Array<T>& rhs)
00090     {lhs.resize(rhs.shape()); lhs = rhs;};
00091 
00092     template <class T>
00093     T getenv(const char *name, const T defaultVal);
00094     Float libreSpheroidal(Float nu);
00095     Double getRefFreq(const VisBuffer& vb);
00096     void makeFTCoordSys(const CoordinateSystem& coords,
00097                         const Int& convSize,
00098                         const Vector<Double>& ftRef,
00099                         CoordinateSystem& ftCoords);
00100 
00101     void expandFreqSelection(const Matrix<Double>& freqSelection,
00102                              Matrix<Double>& expandedFreqList,
00103                              Matrix<Double>& expandedConjFreqList);
00104 
00105     template <class T>
00106     void libreConvolver(Array<T>& c1, const Array<T>& c2);
00107     inline Double conjFreq(const Double& freq, const Double& refFreq) 
00108     {return sqrt(2*refFreq*refFreq - freq*freq);};
00109 
00110     Double nearestValue(const Vector<Double>& list, const Double& val, Int& index);
00111   }
00112 
00113   void getHADec(MeasurementSet& ms, const VisBuffer& vb, Double &HA, Double& RA, Double& Dec);
00115   // 
00116   // An interface class to detect changes in the VisBuffer
00117   // Exact meaning of the "change" is defined in the derived classes
00118   //
00119   struct IChangeDetector {
00120      // return True if a change occurs in the given row since the last call of update
00121      virtual Bool changed(const VisBuffer &vb, Int row) const throw(AipsError) = 0;
00122      // start looking for a change from the given row of the VisBuffer
00123      virtual void update(const VisBuffer &vb, Int row) throw(AipsError) = 0;
00124      
00125      // reset to the state which exists just after construction
00126      virtual void reset() throw(AipsError) = 0;
00127 
00128      // some derived methods, which use the abstract virtual function changed(vb,row)
00129      
00130      // return True if a change occurs somewhere in the buffer
00131      Bool changed(const VisBuffer &vb) const throw(AipsError);
00132      // return True if a change occurs somewhere in the buffer starting from row1
00133      // up to row2 (row2=-1 means up to the end of the buffer). The row number, 
00134      // where the change occurs is returned in the row2 parameter
00135      Bool changedBuffer(const VisBuffer &vb, Int row1, Int &row2) const throw(AipsError);
00136   protected:
00137      // a virtual destructor to make the compiler happy
00138      virtual ~IChangeDetector() throw(AipsError);
00139   };
00140   //
00142 
00144   //
00145   // ParAngleChangeDetector - a class to detect a change in the parallactic 
00146   //                          angle. 
00147   //
00148   class ParAngleChangeDetector : public IChangeDetector {
00149      Double pa_tolerance_p;   // a parallactic angle tolerance. If exeeded, 
00150                               // the angle is considered to be changed.
00151      Double last_pa_p;        // last value of the parallactic angle
00152   public:
00153      // The default constructor
00154      ParAngleChangeDetector():pa_tolerance_p(0.0) {};
00155      // set up the tolerance, which determines how much the position angle should
00156      // change to report the change by this class
00157      ParAngleChangeDetector(const Quantity &pa_tolerance) throw(AipsError);
00158 
00159      virtual void setTolerance(const Quantity &pa_tolerance);
00160      // reset to the state which exists just after construction
00161      virtual void reset() throw(AipsError);
00162 
00163      // return parallactic angle tolerance
00164      Quantity getParAngleTolerance() const throw(AipsError);
00165       
00166      // implementation of the base class' virtual functions
00167      
00168      // return True if a change occurs in the given row since the last call of update
00169      virtual Bool changed(const VisBuffer &vb, Int row) const throw(AipsError);
00170      // start looking for a change from the given row of the VisBuffer
00171      virtual void update(const VisBuffer &vb, Int row) throw(AipsError);
00172   };
00173 
00174   //
00176 
00177 }
00178 #endif