casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TempImage.h
Go to the documentation of this file.
00001 //# TempImage.h: Temporary astronomical images
00002 //# Copyright (C) 1998,1999,2000,2001,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: TempImage.h 20299 2008-04-03 05:56:44Z gervandiepen $
00027 
00028 #ifndef IMAGES_TEMPIMAGE_H
00029 #define IMAGES_TEMPIMAGE_H
00030 
00031 
00032 //# Includes
00033 #include <images/Images/ImageInterface.h>
00034 #include <lattices/Lattices/TiledShape.h>
00035 #include <lattices/Lattices/TempLattice.h>
00036 
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 // <summary>
00041 // Temporary astronomical images.
00042 // </summary>
00043 
00044 // <use visibility=export>
00045 
00046 // <reviewed reviewer="" date="" tests="tTempImage.cc" demos="">
00047 // </reviewed>
00048 
00049 // <prerequisite>
00050 //   <li> <linkto class=CoordinateSystem>CoordinateSystem</linkto>
00051 //   <li> <linkto class=ImageInterface>ImageInterface</linkto>
00052 //   <li> <linkto class=TempLattice>TempLattice</linkto>
00053 // </prerequisite>
00054 
00055 // <etymology>
00056 // The TempImage name comes from its role as the Image class for temporary
00057 // storage.
00058 // </etymology>
00059 
00060 // <synopsis> 
00061 // The class <src>TempImage</src> is useful for storing temporary images
00062 // for which it is not known whether they can be held in memory.
00063 // It uses class <linkto class=TempLattice>TempLattice</linkto> to
00064 // hold the image in memory when it is small enough. Otherwise it is
00065 // held in a temporary file. Similarly to <src>TempLattice</src>
00066 // one can give the maximum memory to use to control when the image
00067 // can be held in memory.
00068 // <br>
00069 // The other Image information like coordinates, units, and miscinfo
00070 // is held in member variables and disappears when the TempImage object
00071 // is destructed.
00072 // <p>
00073 // It is possibly to temporarily close a TempImage, which only takes effect
00074 // when it is created as a PagedArray. In this way it is possible to reduce
00075 // the number of open files in case a lot of TempImage objects are used.
00076 // A temporarily closed TempImage will be reopened automatically when needed.
00077 // It can also be reopened explicitly.
00078 // </synopsis> 
00079 
00080 // <example>
00081 // <srcblock>
00082 // </srcblock>
00083 // </example>
00084 
00085 // <motivation>
00086 // The size of astronomical data can be very large.  The ability to fit an 
00087 // entire image into random access memory cannot be guaranteed.  Paging from 
00088 // disk pieces of the image appeared to be the way to deal with this problem.
00089 // </motivation>
00090 
00091 //# <todo asof="1998/10/27">
00092 //#  <li> Maybe move applyMask, maskPtr_p, etc to base class ImageInterface
00093 //# </todo>
00094 
00095 
00096 template<class T> class TempImage: public ImageInterface<T>
00097 {
00098 public: 
00099   // The default constructor creates an empty image.
00100   TempImage();
00101 
00102   // Construct a temporary Image from shape and coordinate information.
00103   // If the image is sufficiently small, it is kept in memory.
00104   // Otherwise it is kept in a temporary disk table. It can
00105   // be forced to disk by setting maxMemoryinMB=0.
00106   // The algorithm is the same as in class
00107   // <linkto class=TempLattice>TempLattice</linkto>.
00108   TempImage (const TiledShape& mapShape,
00109              const CoordinateSystem& coordinateInfo,
00110              Int maxMemoryInMB=-1);
00111 
00112   TempImage (const TiledShape& mapShape,
00113              const CoordinateSystem& coordinateInfo,
00114              Double maxMemoryInMB);
00115 
00116   // Copy constructor (reference semantics).
00117   TempImage (const TempImage<T>& other);
00118 
00119   // Destructor
00120   ~TempImage();
00121 
00122   // Assignment operator (reference semantics).
00123   TempImage<T>& operator= (const TempImage<T>& other);
00124 
00125   // Make a copy of the object (reference semantics).
00126   virtual ImageInterface<T>* cloneII() const;
00127 
00128   // Get the image type (returns name of derived class).
00129   virtual String imageType() const;
00130 
00131   // Is the TempImage paged to disk?
00132   virtual Bool isPaged() const;
00133 
00134   // Can the lattice data be referenced as an array section?
00135   virtual Bool canReferenceArray() const;
00136 
00137   // Is the TempImage writable?
00138   virtual Bool isWritable() const;
00139 
00140   // Set the default pixelmask to the mask with the given name
00141   // (which has to exist in the "masks" group).
00142   // If the image table is writable, the setting is persistent by writing
00143   // the name as a keyword.
00144   // If the given regionName is the empty string,
00145   // the default pixelmask is unset.
00146   virtual void setDefaultMask (const String& maskName);
00147 
00148   // Delete the pixel mask attached to the TempImage.
00149   // Does nothing if there isn't one
00150   void removeMask()
00151     { setDefaultMask (""); }
00152 
00153   // Use the mask as specified.
00154   // If a mask was already in use, it is replaced by the new one.
00155   virtual void useMask (MaskSpecifier = MaskSpecifier());
00156 
00157   // Remove a region/mask belonging to the image from the given group
00158   // (which can be Any).
00159   // If a mask removed is the default mask, the image gets unmasked.
00160   // <br>Optionally an exception is thrown if the region does not exist.
00161   virtual void removeRegion (const String& name,
00162                              RegionHandler::GroupType = RegionHandler::Any,
00163                              Bool throwIfUnknown = True);
00164 
00165   // Attach a mask to the TempImage.
00166   // It replaces a probably already attached mask.
00167   // It has to have the same shape as the image.
00168   virtual void attachMask (const Lattice<Bool>& mask);
00169 
00170   // It a mask attached to the image?
00171   virtual Bool isMasked() const;
00172 
00173   // Does the image object use a pixelmask?
00174   // This is similar to <src>isMasked()</src>.
00175   virtual Bool hasPixelMask() const;
00176 
00177   // Get access to the pixelmask used.
00178   // An exception is thrown if the image does not use a pixelmask.
00179   // <group>
00180   virtual const Lattice<Bool>& pixelMask() const;
00181   virtual Lattice<Bool>& pixelMask();
00182   // </group>
00183 
00184   // Get a section of the mask.
00185   // It throws an exception if there is no mask.
00186   virtual Bool doGetMaskSlice (Array<Bool>& buffer, const Slicer& section);
00187 
00188   // Flush the data.
00189   virtual void flush();
00190 
00191   // Close the TempImage temporarily (if it is paged to disk).
00192   // Note that a possible mask is not closed.
00193   // It'll be reopened automatically when needed or when
00194   // <src>reopen</src> is called explicitly.
00195   virtual void tempClose();
00196 
00197   // If needed, reopen a temporarily closed TempLattice.
00198   virtual void reopen();
00199 
00200   // Function which changes the shape of the image (N.B. the data is thrown 
00201   // away - the Image will be filled with nonsense afterwards)
00202   virtual void resize (const TiledShape& newShape);
00203   
00204   // Return the name of the current TempImage object.
00205   // It is always "Temporary_Image"
00206   virtual String name (Bool stripPath=False) const;
00207 
00208   // Return the shape of the image
00209   virtual IPosition shape() const;
00210 
00211   // Function which sets all of the elements in the Lattice to a value.
00212   virtual void set (const T& value);
00213 
00214   // Replace every element, x, of the lattice with the result of f(x).
00215   // You must pass in the address of the function -- so the function
00216   // must be declared and defined in the scope of your program.  
00217   // Both versions of apply require a function that accepts a single 
00218   // argument of type T (the Lattice template actual type) and returns
00219   // a result of the same type.  The first apply expects a function with
00220   // an argument passed by value; the second expects the argument to
00221   // be passed by const reference.  The first form ought to run faster
00222   // for the built-in types, which may be an issue for large images
00223   // stored in memory, where disk access is not an issue.
00224   // <group>
00225   virtual void apply (T (*function)(T));
00226   virtual void apply (T (*function)(const T&));
00227   virtual void apply (const Functional<T,T>& function);
00228   // </group>
00229     
00230   // Get or put a single pixel.
00231   // Note that the function operator () can also be used to get a pixel.
00232   // <group>
00233   virtual T getAt (const IPosition& where) const;
00234   virtual void putAt (const T& value, const IPosition& where);
00235   // </group>
00236 
00237   // This is the implementations of the letters for the envelope Iterator
00238   // class <note> Not for public use </note>
00239   virtual LatticeIterInterface<T>* makeIter
00240                            (const LatticeNavigator& navigator,
00241                             Bool useRef) const;
00242 
00243   // Returns the maximum recommended number of pixels for a cursor.
00244   // This is the number of pixels in a tile. 
00245   virtual uInt advisedMaxPixels() const;
00246 
00247   // Help the user pick a cursor for most efficient access.
00248   virtual IPosition doNiceCursorShape (uInt maxPixels) const;
00249 
00250   // Maximum size - not necessarily all used. In pixels.
00251   virtual uInt maximumCacheSize() const;
00252 
00253   // Set the maximum (allowed) cache size as indicated.
00254   virtual void setMaximumCacheSize (uInt howManyPixels);
00255 
00256   // Set the cache size as to "fit" the indicated path.
00257   virtual void setCacheSizeFromPath (const IPosition& sliceShape,
00258                                      const IPosition& windowStart,
00259                                      const IPosition& windowLength,
00260                                      const IPosition& axisPath);
00261     
00262   // Set the actual cache size for this Array to be be big enough for the
00263   // indicated number of tiles. This cache is not shared with PagedArrays
00264   // in other rows and is always clipped to be less than the maximum value
00265   // set using the setMaximumCacheSize member function.
00266   // tiles. Tiles are cached using a first in first out algorithm. 
00267   virtual void setCacheSizeInTiles (uInt howManyTiles);
00268 
00269   // Clears and frees up the caches, but the maximum allowed cache size is 
00270   // unchanged from when setCacheSize was called
00271   virtual void clearCache();
00272 
00273   // Report on cache success.
00274   virtual void showCacheStatistics (ostream& os) const;
00275 
00276   // Check for symmetry in data members.
00277   virtual Bool ok() const;
00278 
00279 protected:
00280   // Get the region used (it always returns 0).
00281   virtual const LatticeRegion* getRegionPtr() const;
00282 
00283   // Function which extracts an array from the map.
00284   virtual Bool doGetSlice (Array<T>& buffer, const Slicer& theSlice);
00285   
00286   // Function to replace the values in the map with soureBuffer.
00287   virtual void doPutSlice (const Array<T>& sourceBuffer,
00288                            const IPosition& where,
00289                            const IPosition& stride);
00290 
00291 
00292 private:  
00293   void applyMaskSpecifier (const MaskSpecifier&);
00294   void applyMask (const String& maskName);
00295 
00296   TempLattice<T>* mapPtr_p;
00297   Lattice<Bool>*  maskPtr_p;
00298 
00299   //# Make members of parent class known.
00300 public:
00301   using ImageInterface<T>::logger;
00302   using ImageInterface<T>::coordinates;
00303   using ImageInterface<T>::getDefaultMask;
00304   using ImageInterface<T>::hasRegion;
00305   using ImageInterface<T>::getImageRegionPtr;
00306   using ImageInterface<T>::setCoordinateInfo;
00307 protected:
00308   using ImageInterface<T>::setCoordsMember;
00309 };
00310 
00311 
00312 
00313 } //# NAMESPACE CASA - END
00314 
00315 #ifndef CASACORE_NO_AUTO_TEMPLATES
00316 #include <images/Images/TempImage.tcc>
00317 #endif //# CASACORE_NO_AUTO_TEMPLATES
00318 #endif