casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ImageProxy.h
Go to the documentation of this file.
00001 //# ImageProxy.h:  Proxy interface to images
00002 //# Copyright (C) 1997-2007
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: ImageProxy.h 20966 2010-09-27 09:43:20Z gervandiepen $
00028 
00029 #ifndef IMAGES_IMAGEPROXY_H
00030 #define IMAGES_IMAGEPROXY_H
00031 
00032 //# Includes
00033 #include <images/Images/MaskSpecifier.h>
00034 #include <lattices/Lattices/LatticeBase.h>
00035 #include <lattices/Lattices/TiledShape.h>
00036 #include <casa/Utilities/CountedPtr.h>
00037 #include <casa/Containers/ValueHolder.h>
00038 #include <casa/Containers/Record.h>
00039 
00040 namespace casa {
00041 
00042   //# Forward Declarations.
00043   template<typename T> class ImageInterface;
00044   class LatticeExprNode;
00045   class CoordinateSystem;
00046 
00047   // <synopsis>
00048   // </synopsis>
00049 
00050   class ImageProxy
00051   {
00052   public:
00053     // Default constructor is needed for Boost-Python.
00054     ImageProxy();
00055 
00056     // Construct from the concatenation of the images along the axis.
00057     // The axis must be given as a Fortran-array axis.
00058     // All images must be of the same data type.
00059     ImageProxy (const Vector<String>& names, Int axis);
00060 
00061     // Construct from a string that contains an image name or image expression.
00062     // It is opened using ImageOpener.
00063     ImageProxy (const String& name, const String& mask,
00064                 const vector<ImageProxy>& images);
00065 
00066     // Construct from the concatenation of the image objects along the axis.
00067     // The axis must be given as a Fortran-array axis.
00068     // All images must be of the same data type.
00069     //# The dummy arguments are needed to let all constructors have a
00070     //# different number of arguments (for Boost-Python).
00071     ImageProxy (const vector<ImageProxy>& images, Int axis,
00072                 Int dummy1=0, Int dummy2=0);
00073 
00074     // Construct from a ValueHolder containing an Array of Float or Complex.
00075     // If the name is empty it is created as a temporary image, otherwise
00076     // as a PagedImage or HDF5Image.
00077     // If the coordinates record is empty, default coordinates are used.
00078     // A mask is created if the mask name or mask value is not empty.
00079     ImageProxy (const ValueHolder& values,
00080                 const ValueHolder& mask,
00081                 const Record& coordinates,
00082                 const String& imageName = String(),
00083                 Bool overwrite = True,
00084                 Bool asHDF5 = False,
00085                 const String& maskName = String(),
00086                 const IPosition& tileShape = IPosition());
00087 
00088     // Construct from a shape.
00089     // If the name is empty it is created as a temporary image, otherwise
00090     // as a PagedImage or HDF5Image.
00091     // If the coordinates record is empty, default coordinates are used.
00092     // A mask is created if the mask name is not empty.
00093     ImageProxy (const IPosition& shape,
00094                 const ValueHolder& value,
00095                 const Record& coordinates,
00096                 const String& imageName = String(),
00097                 Bool overwrite = True,
00098                 Bool asHDF5 = False,
00099                 const String& maskName = String(),
00100                 const IPosition& tileShape = IPosition(),
00101                 Int dummy=0);
00102 
00103     // Copy constructor (reference semantics).
00104     ImageProxy (const ImageProxy&);
00105 
00106     // Assignment (reference semantics).
00107     ImageProxy& operator= (const ImageProxy&);
00108 
00109      ~ImageProxy();
00110 
00111     // Open an image in the file/table with the given name.
00112     // The specified mask will be applied (default is default mask).
00113     // A null pointer is returned for an unknown image type.
00114     // Non-AIPS++ image types must have been registered to be known.
00115     // If not successful, try to open it as an image expression.
00116     static LatticeBase* openImageOrExpr (const String& str,
00117                                          const MaskSpecifier&,
00118                                          const Block<LatticeExprNode>& nodes);
00119 
00120     // Turn the ImageProxy into a LatticeExprNode.
00121     LatticeExprNode makeNode() const;
00122 
00123     // Is the image persistent or temporary.
00124     Bool isPersistent() const;
00125 
00126     // Get the name of the image.
00127     String name (Bool stripPath=False) const;
00128 
00129     // Get the shape of the image.
00130     IPosition shape() const;
00131 
00132     // Get the dimensionality of the image.
00133     uInt ndim() const;
00134 
00135     // Get the size of the image (nr of pixels).
00136     uInt size() const;
00137 
00138     // Get the data type of the image.
00139     String dataType() const;
00140 
00141     // Get a chunk of data.
00142     ValueHolder getData (const IPosition& blc,
00143                          const IPosition& trc, 
00144                          const IPosition& inc);
00145 
00146     // Get a chunk of the mask.
00147     ValueHolder getMask (const IPosition& blc,
00148                          const IPosition& trc, 
00149                          const IPosition& inc);
00150 
00151     // Put a chunk of data.
00152     void putData (const ValueHolder&,
00153                   const IPosition& blc,
00154                   const IPosition& inc);
00155 
00156     // Put a chunk of the mask.
00157     // The mask will be created if not present yet.
00158     // That will not be done if the entire mask is True.
00159     void putMask (const ValueHolder& value,
00160                   const IPosition& blc,
00161                   const IPosition& inc);
00162 
00163     // Does the image have a read or write lock?
00164     Bool hasLock (Bool writeLock = False);
00165 
00166     // Try to acquire a read or write lock.
00167     // nattempts=0 means wait until acquired. Otherwise every second an
00168     // attempt is done.
00169     void lock (Bool writeLock=False, Int nattempts=0);
00170 
00171     // Release the lock acquired by lock().
00172     void unlock();
00173 
00174     // Form a new (virtual) image being a subset of the image.
00175     ImageProxy subImage (const IPosition& blc,
00176                          const IPosition& trc, 
00177                          const IPosition& inc,
00178                          Bool dropDegenerate=True);
00179 
00180     // Get the brightness unit.
00181     String unit() const;
00182 
00183     // Get the coordinate system.
00184     Record coordSys() const;
00185 
00186     // Convert a pixel coordinate to world coordinates.
00187     // if <src>reverseAxes=True</src> the input and output vector will be
00188     // reversed (as needed for pyrap).
00189     Vector<Double> toWorld (const Vector<Double>& pixel,
00190                             Bool reverseAxes);
00191 
00192     //#// Convert world coordinates to pixel coordinates.
00193     //#Vector<Double> topixel (Record& value);
00194 
00195     // Get the image info.
00196     Record imageInfo() const;
00197 
00198     // Get the miscellaneous info.
00199     Record miscInfo() const;
00200 
00201     // Get the history.
00202     Vector<String> history() const;
00203 
00204     // Write the image in FITS format.
00205     // See class ImageFITSConverter for a description of the arguments.
00206     // Currently only a float image can be written to FITS.
00207     void toFits (const String& fitsfile, Bool overwrite=True,
00208                  Bool velocity=True, Bool optical=True, Int bitpix=-32,
00209                  Double minpix=1, Double maxpix=-1) const;
00210 
00211     // Write the image to an image file with the given name.
00212     // An exception is thrown if the name is the name of an already open image.
00213     void saveAs (const String& fileName, Bool overwrite=True,
00214                  Bool hdf5=False,
00215                  Bool copyMask=True, const String& newMaskName=String(),
00216                  const IPosition& newTileShape=IPosition()) const;
00217 
00218     // Return the statistics for the given axes.
00219     // E.g. fn axes 0,1 is given in a 3-dim image, the statistics are calculated
00220     // for each plane along the 3rd axis.
00221     // MinMaxValues can be given to include or exclude (4th argument) pixels
00222     // with values in the given range. If only one value is given, min=-abs(val)
00223     // and max=abs(val).
00224     // Robust statistics (Median, MedAbsDevMed, and Quartile) can be returned
00225     // too.
00226     Record statistics (const Vector<Int>& axes,
00227                        const String& mask,
00228                        const ValueHolder& minMaxValues,
00229                        Bool exclude = False,
00230                        Bool robust = False) const;
00231 
00232     // Regrid the image on the given axes to the given coordinate system.
00233     // The output is stored in the given file; it no file name is given a
00234     // temporary image is made.
00235     // If the output shape is empty, the old shape is used.
00236     // <src>replicate=True</src> means replication rather than regridding.
00237     ImageProxy regrid (const Vector<Int>& axes = Vector<Int>(),
00238                        const String& outfile = String(),
00239                        Bool overwriteOutFile = True,
00240                        const IPosition& outShape = IPosition(),
00241                        const Record& coordSys = Record(),
00242                        const String& method = "linear",
00243                        Int decimate = 10,
00244                        Bool replicate = False,
00245                        Bool doRefChange = True,
00246                        Bool forceRegrid = False);
00247 
00248     // Check and adjust blc, trc, or inc using the shape.
00249     // <group>
00250     static IPosition adjustBlc (const IPosition& blc, const IPosition& shp);
00251     static IPosition adjustTrc (const IPosition& trc, const IPosition& shp);
00252     static IPosition adjustInc (const IPosition& inc, const IPosition& shp);
00253     // </group>
00254 
00255 
00256     /*
00257 
00258     ImageProxy rotate(const String& outfile, 
00259                       const IPosition& shape, 
00260                       const Quantity& pa, Record& region, 
00261                       const String& mask, 
00262                       const String& method = "cubic", 
00263                       Int decimate = 0, 
00264                       Bool replicate = False, 
00265                       Bool dropdeg = False,
00266                       Bool overwrite = False);
00267 
00268 
00269     Bool setbrightnessunit (const String& unit);
00270 
00271     Bool setcoordsys (const Record& csys);
00272 
00273     Bool sethistory (const String& origin, const Vector<String>& history);
00274 
00275     Bool setmiscinfo (const Record& info);
00276 
00277     ImageProxy subimage(const String& outfile, Record& region, 
00278                         const String& mask, 
00279                         Bool dropdeg = False, 
00280                         Bool overwrite = False, 
00281                         Bool list = True);
00282 
00283     Vector<Double> topixel(Record& value);
00284 
00285     */
00286 
00287     LatticeBase* getLattice() const
00288       { return itsLattice.operator->(); }
00289 
00290  private:
00291     // Form an ImageProxy object from an existing image object.
00292     explicit ImageProxy (LatticeBase*);
00293 
00294     // Open the image (which can also be an expression.
00295     // Throw an exception if not succeeded.
00296     void openImage (const String& name, const String& mask,
00297                     const vector<ImageProxy>& images);
00298 
00299     // Make an image from an array or shape.
00300     template<typename T>
00301     void makeImage (const Array<T>& array, const Array<Bool>& mask,
00302                     const IPosition& shape, const Record& coordinates,
00303                     const String& fileName, Bool asHDF5,
00304                     const String& maskName,
00305                     const IPosition& tileShape);
00306 
00307     // Form a concatenated image.
00308     // <group>
00309     void concatImages         (const std::vector<ImageProxy>& images, Int axis);
00310     void concatImagesFloat    (const std::vector<ImageProxy>& images, Int axis);
00311     void concatImagesDouble   (const std::vector<ImageProxy>& images, Int axis);
00312     void concatImagesComplex  (const std::vector<ImageProxy>& images, Int axis);
00313     void concatImagesDComplex (const std::vector<ImageProxy>& images, Int axis);
00314     // </group>
00315 
00316     // Setup the pointers for the various image data types.
00317     void setup (LatticeBase* lattice);
00318 
00319     // Centre all axes except the Stokes one.
00320     void centreRefPix (CoordinateSystem& cSys, 
00321                        const IPosition& shape) const;
00322 
00323     // Put the mask and create it if needed.
00324     template<typename T>
00325     void doPutMask (ImageInterface<T>& image,
00326                     const ValueHolder& value,
00327                     const IPosition& blc,
00328                     const IPosition& inc);
00329 
00330     // Copy the image to an image (PagedImage or HDF5Image) with the given name.
00331     // A new tile shape can be given.
00332     // If the image is masked, the mask can be copied as well.
00333     template <typename T>
00334     void saveImage (const String& fileName,
00335                     Bool hdf5, Bool copyMask,
00336                     const String& newMaskName, 
00337                     const IPosition& newTileShape,
00338                     const ImageInterface<T>& image) const;
00339 
00340     // Form a tiled shape from the current shape and a possible new tile shape.
00341     TiledShape makeTiledShape (const IPosition& newTileShape,
00342                                const IPosition& shape,
00343                                const IPosition& oldTileShape=IPosition()) const;
00344 
00345     // Calculate the statistics.
00346     template<typename T>
00347     Record makeStatistics (const ImageInterface<T>& image,
00348                            const Vector<Int>& axes,
00349                            const String& mask,
00350                            const ValueHolder& minMaxValues,
00351                            Bool exclude,
00352                            Bool robust) const;
00353 
00354     // Do the actual regridding.
00355     template<typename T>
00356     ImageProxy doRegrid (const ImageInterface<T>& image,
00357                          const Vector<Int>& axes,
00358                          const String& outfile,
00359                          const IPosition& shape,
00360                          const Record& coordSys,
00361                          const String& method,
00362                          Int decimate,
00363                          Bool replicate, 
00364                          Bool doRefChange,
00365                          Bool force);
00366 
00367     // Make a coordinate system from the Record.
00368     // The cylindrical fix is applied if needed.
00369     CoordinateSystem makeCoordinateSystem (const Record& coordinates,
00370                                            const IPosition& shape) const;
00371 
00372 
00373     //# Data members.
00374     //# itsLattice is the real data; the pointers are for type convenience only.
00375     CountedPtr<LatticeBase>   itsLattice;
00376     ImageInterface<Float>*    itsImageFloat;
00377     ImageInterface<Double>*   itsImageDouble;
00378     ImageInterface<Complex>*  itsImageComplex;
00379     ImageInterface<DComplex>* itsImageDComplex;
00380     const CoordinateSystem*   itsCoordSys;
00381   };
00382 
00383 } // end namespace casa
00384 
00385 #endif