casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
FITSImage.h
Go to the documentation of this file.
00001 //# FITSImage.h: Class providing native access to FITS images
00002 //# Copyright (C) 2001,2002
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: FITSImage.h 20495 2009-01-19 00:50:41Z Malte.Marquarding $
00027 
00028 #ifndef IMAGES_FITSIMAGE_H
00029 #define IMAGES_FITSIMAGE_H
00030 
00031 
00032 //# Includes
00033 #include <images/Images/ImageInterface.h>
00034 #include <images/Images/MaskSpecifier.h>
00035 #include <tables/Tables/TiledFileAccess.h>
00036 #include <lattices/Lattices/TiledShape.h>
00037 #include <fits/FITS/fits.h>
00038 #include <casa/BasicSL/String.h>
00039 #include <casa/Utilities/DataType.h>
00040 
00041 #ifndef WCSLIB_GETWCSTAB
00042  #define WCSLIB_GETWCSTAB
00043 #endif
00044 
00045 namespace casa { //# NAMESPACE CASA - BEGIN
00046 
00047 //# Forward Declarations
00048 template <class T> class Array;
00049 template <class T> class Lattice;
00050 //
00051 class MaskSpecifier;
00052 class IPosition;
00053 class Slicer;
00054 class CoordinateSystem;
00055 class FITSMask;
00056 class FitsInput;
00057 
00058 
00059 // <summary>
00060 // Class providing native access to FITS images.
00061 // </summary>
00062 
00063 // <use visibility=export>
00064 
00065 // <reviewed reviewer="" date="" tests="tFITSImage.cc">
00066 // </reviewed>
00067 
00068 // <prerequisite>
00069 //   <li> <linkto class=ImageInterface>ImageInterface</linkto>
00070 //   <li> <linkto class=FITSMask>FITSMask</linkto>
00071 // </prerequisite>
00072 
00073 // <etymology>
00074 //  This class provides native access to FITS images. 
00075 //  64bit, 32bit floating point, 32 bit and 16bit integer FITS images are 
00076 //  presently supported.
00077 // </etymology>
00078 
00079 // <synopsis> 
00080 //  A FITSImage provides native access to FITS images by accessing them
00081 //  with the TiledFileAccess class.  The FITSImage is read only.
00082 //  We could implement a writable FITSImage but putting the mask
00083 //  would lose data values (uses magic blanking) and FITS is really
00084 //  meant as an interchange medium, not an internal format.
00085 //
00086 //  Because FITS uses magic value blanking, the mask is generated
00087 //  on the fly as needed.
00088 // </synopsis> 
00089 
00090 // <example>
00091 // <srcblock>
00092 //    FITSImage im("in.fits"); 
00093 //    LogIO logger(or);
00094 //    ImageStatistics<Float> stats(im, logger);
00095 //    Bool ok = stats.display();                              // Display statistics
00096 // </srcblock>
00097 // </example>
00098 
00099 // <motivation>
00100 // This provides native access to FITS images.
00101 // </motivation>
00102 
00103 //# <todo asof="2001/02/09">
00104 //# </todo>
00105 
00106 
00107 class FITSImage: public ImageInterface<Float>
00108 {
00109 public: 
00110   // Construct a FITSImage from the disk FITS file name  and extension and apply mask.
00111   explicit FITSImage(const String& name, uInt whichRep=0, uInt whichHDU=0);
00112 
00113   // Construct a FITSImage from the disk FITS file name and extension and apply mask or not.
00114   FITSImage(const String& name, const MaskSpecifier& mask, uInt whichRep=0, uInt whichHDU=0);
00115 
00116   // Copy constructor (reference semantics)
00117   FITSImage(const FITSImage& other);
00118 
00119   // Destructor does nothing
00120   virtual ~FITSImage();
00121 
00122   // Assignment (reference semantics)
00123   FITSImage& operator=(const FITSImage& other);
00124 
00125   // Function to open a FITS image (new parser)
00126   static LatticeBase* openFITSImage (const String& name,
00127                                      const MaskSpecifier&);
00128 
00129   // Register the open function.
00130   static void registerOpenFunction();
00131 
00132   // Separate any extension specification and return the pure fitsname
00133   static String get_fitsname(const String &fullname);
00134 
00135   // Get the extension index for any extension specification given in the full name
00136   static uInt get_hdunum(const String &fullname);
00137 
00138   //# ImageInterface virtual functions
00139   
00140   // Make a copy of the object with new (reference semantics).
00141   virtual ImageInterface<Float>* cloneII() const;
00142 
00143   // Get the image type (returns FITSImage).
00144   virtual String imageType() const;
00145 
00146   // Function which changes the shape of the FITSImage.
00147   // Throws an exception as FITSImage is not writable.
00148   virtual void resize(const TiledShape& newShape);
00149 
00150   //# MaskedLattice virtual functions
00151 
00152   // Has the object really a mask?  The FITSImage always
00153   // has a pixel mask and never has a region mask so this
00154   // always returns True
00155   virtual Bool isMasked() const;
00156 
00157   // FITSimage always has a pixel mask so returns True
00158   virtual Bool hasPixelMask() const;
00159 
00160   // Get access to the pixelmask.  FITSImage always has a pixel mask.
00161   // <group>
00162   virtual const Lattice<Bool>& pixelMask() const;
00163   virtual Lattice<Bool>& pixelMask();
00164   // </group>
00165 
00166   // Do the actual get of the mask data.   The return value is always 
00167   // False, thus the buffer does not reference another array.
00168   virtual Bool doGetMaskSlice (Array<Bool>& buffer, const Slicer& section);
00169 
00170   // Get the region used.  There is no region. 
00171   // Always returns 0.
00172   virtual const LatticeRegion* getRegionPtr() const;
00173 
00174  
00175   //# Lattice virtual functions
00176 
00177   // Do the actual get of the data.
00178   // Returns False as the data do not reference another Array
00179   virtual Bool doGetSlice (Array<Float>& buffer, const Slicer& theSlice);
00180 
00181   // The FITSImage is not writable, so this throws an exception.
00182   virtual void doPutSlice (const Array<Float>& sourceBuffer,
00183                            const IPosition& where,
00184                            const IPosition& stride);
00185 
00186   //# LatticeBase virtual functions
00187 
00188   // The lattice is paged to disk.
00189   virtual Bool isPaged() const;
00190 
00191   // The lattice is persistent.
00192   virtual Bool isPersistent() const;
00193 
00194   // The FITSImage is not writable.
00195   virtual Bool isWritable() const;
00196 
00197   // Returns the name of the disk file.
00198   virtual String name (Bool stripPath=False) const;
00199   
00200   // return the shape of the FITSImage
00201   virtual IPosition shape() const;
00202 
00203   // Returns the maximum recommended number of pixels for a cursor. This is
00204   // the number of pixels in a tile. 
00205   virtual uInt advisedMaxPixels() const;
00206 
00207   // Help the user pick a cursor for most efficient access if they only want
00208   // pixel values and don't care about the order or dimension of the
00209   // cursor. 
00210   virtual IPosition doNiceCursorShape (uInt maxPixels) const;
00211 
00212   // Temporarily close the image.
00213   virtual void tempClose();
00214 
00215   // Reopen a temporarily closed image.
00216   virtual void reopen();
00217 
00218   // Check class invariants.
00219   virtual Bool ok() const;
00220 
00221   // Return the (internal) data type (TpFloat or TpShort).
00222   DataType dataType () const
00223     { return dataType_p; }
00224 
00225   // Return the HDU number
00226   uInt whichHDU () const
00227     { return whichHDU_p; }
00228 
00229   // Maximum size - not necessarily all used. In pixels.
00230   virtual uInt maximumCacheSize() const;
00231 
00232   // Set the maximum (allowed) cache size as indicated.
00233   virtual void setMaximumCacheSize (uInt howManyPixels);
00234 
00235   // Set the cache size as to "fit" the indicated path.
00236   virtual void setCacheSizeFromPath (const IPosition& sliceShape,
00237                                      const IPosition& windowStart,
00238                                      const IPosition& windowLength,
00239                                      const IPosition& axisPath);
00240     
00241   // Set the actual cache size for this Array to be be big enough for the
00242   // indicated number of tiles. This cache is not shared with PagedArrays
00243   // in other rows and is always clipped to be less than the maximum value
00244   // set using the setMaximumCacheSize member function.
00245   // tiles. Tiles are cached using a first in first out algorithm. 
00246   virtual void setCacheSizeInTiles (uInt howManyTiles);
00247 
00248   // Clears and frees up the caches, but the maximum allowed cache size is 
00249   // unchanged from when setCacheSize was called
00250   virtual void clearCache();
00251 
00252   // Report on cache success.
00253   virtual void showCacheStatistics (ostream& os) const;
00254 
00255 protected:
00256 
00257   // Set the masking of values 0.0
00258   void setMaskZero(Bool filterZero);
00259 
00260 private:  
00261   String         name_p;
00262   String         fullname_p;
00263   MaskSpecifier  maskSpec_p;
00264   CountedPtr<TiledFileAccess> pTiledFile_p;
00265   Lattice<Bool>* pPixelMask_p;
00266   TiledShape     shape_p;
00267   Float          scale_p;
00268   Float          offset_p;
00269   Short          shortMagic_p;
00270   Int            longMagic_p;
00271   Bool           hasBlanks_p;
00272   DataType       dataType_p;
00273   Int64          fileOffset_p;
00274   Bool           isClosed_p;
00275   Bool           filterZeroMask_p;
00276   uInt           whichRep_p;
00277   uInt           whichHDU_p;
00278   Bool           _hasBeamsTable;
00279 
00280 
00281 // Reopen the image if needed.
00282    void reopenIfNeeded() const
00283      { if (isClosed_p) const_cast<FITSImage*>(this)->reopen(); }
00284 
00285 // Setup the object (used by constructors).
00286    void setup();
00287 
00288 // Open the image (used by setup and reopen).
00289    void open();
00290 
00291 // Fish things out of the FITS file
00292    void getImageAttributes (CoordinateSystem& cSys,
00293                             IPosition& shape, ImageInfo& info,
00294                             Unit& brightnessUnit, RecordInterface& miscInfo, 
00295                             Int& recsize, Int& recno,
00296                             FITS::ValueType& dataType,
00297                             Float& scale, Float& offset, Short& shortMagic, 
00298                             Int& longMagic, Bool& hasBlanks, const String& name,
00299                             uInt whichRep, uInt whichHDU);
00300 
00301 // Crack a primary header
00302    template <typename T>
00303    void crackHeader (CoordinateSystem& cSys, IPosition& shape, ImageInfo& imageInfo,
00304                      Unit& brightnessUnit, RecordInterface& miscInfo,
00305                      Float& scale, Float& offset, Short& magicShort,
00306                      Int& magicLong, Bool& hasBlanks, LogIO& os, FitsInput& infile,
00307                      uInt whichRep);
00308 
00309 // Crack an image extension header
00310    template <typename T>
00311    void crackExtHeader (CoordinateSystem& cSys, IPosition& shape, ImageInfo& imageInfo,
00312                         Unit& brightnessUnit, RecordInterface& miscInfo,
00313                         Float& scale, Float& offset, Short& magicShort,
00314                         Int& magicLong, Bool& hasBlanks, LogIO& os, FitsInput& infile,
00315                         uInt whichRep);
00316                      
00317 };
00318 
00319 
00320 
00321 } //# NAMESPACE CASA - END
00322 
00323 #ifndef CASACORE_NO_AUTO_TEMPLATES
00324 #include <images/Images/FITS2Image.tcc>
00325 #endif //# CASACORE_NO_AUTO_TEMPLATES
00326 
00327 #endif
00328 
00329