casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ImageFITSConverter.h
Go to the documentation of this file.
00001 //# ImageFITSConverter.h: Interconvert between AIPS++ Images and FITS files
00002 //# Copyright (C) 1996,1999,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 //#
00027 //# $Id: ImageFITSConverter.h 21104 2011-07-15 08:06:21Z gervandiepen $
00028 
00029 
00030 #ifndef IMAGES_IMAGEFITSCONVERTER_H
00031 #define IMAGES_IMAGEFITSCONVERTER_H
00032 
00033 #include <casa/aips.h>
00034 #include <casa/BasicSL/String.h>
00035 #include <casa/Utilities/DataType.h>
00036 
00037 #ifndef WCSLIB_GETWCSTAB
00038  #define WCSLIB_GETWCSTAB
00039 #endif
00040 
00041 namespace casa { //# NAMESPACE CASA - BEGIN
00042 
00043 template<class T> class PagedImage;
00044 template<class T> class ImageInterface;
00045 template<class T> class Vector;
00046 class FitsOutput;
00047 class IPosition;
00048 class File;
00049 class ImageInfo;
00050 class CoordinateSystem;
00051 class RecordInterface;
00052 class TableRecord;
00053 class LogIO;
00054 class Unit;
00055 class LoggerHolder;
00056 class ConstFitsKeywordList;
00057 class FitsInput;
00058 
00059 // <summary>
00060 // Interconvert between AIPS++ Images and FITS files.
00061 // </summary>
00062 
00063 // <use visibility=export>
00064 
00065 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00066 // </reviewed>
00067 
00068 // <prerequisite>
00069 // <li> <linkto class="PagedImage">PagedImage</linkto>
00070 // <li> <linkto class="PrimaryArray">PrimaryArray</linkto> (and FITS concepts in
00071 //      general).
00072 // </prerequisite>
00073 //
00074 // <synopsis>
00075 // This class is a helper class that is used to interconvert between AIPS++
00076 // images and FITS files. This adds no functionality over the general abilities
00077 // available in the underlying FITS classes, however it is a useful higher-level
00078 // packaging.
00079 //
00080 // There are two fundamental member functions in this class.
00081 // <src>FITSToImage</src> which turns a FITS file into an AIPS++ image, and
00082 // <src>ImageToFITS</src> which does the opposite.
00083 //
00084 // We can read images from any HDU inside the FITS file (although this isn't
00085 // well tested). Images with a quality axis (i.e. contain data and error values)
00086 // are stored in the primary HDU (data) and an extension HDU (error). Other
00087 // images are always written to the primary HDU.
00088 //
00089 // Pixels in the FITS file which are blanked are masked out (the mask
00090 // is set to False) in the output image.   On conversion to FITS,
00091 // masked values are blanked.    The mask which is read is the current
00092 // default mask.
00093 // </synopsis>
00094 //
00095 // <example>
00096 // A FITS to image conversion may be accomplished as follows:
00097 // <srcBlock>
00098 //    PagedImage<Float> *image = 0;
00099 //    String fitsName = "exists.fits";
00100 //    String imageName = "new.image";
00101 //    String error;
00102 //    Bool ok = ImageFITSConverter::FITSToImage(image, error, imageName, fitsName);
00103 //    if (!image) ... error ...
00104 // </srcBlock>
00105 // A couple of things to note:
00106 // <ul>
00107 //    <li> If <src>ok</src> is False, the conversion failed and <src>error</src>
00108 //         will be set.
00109 //    <li> The pointer "image" is set if the conversion succeeds. If it is
00110 //         zero the conversion failed and <src>error</src> will contain an
00111 //         error message.
00112 //    <li> The caller is responsible for deleting the pointer <src>image</src>
00113 //         when the conversion is successful.
00114 // </ul>
00115 // Similarly, an image to FITS conversion may be accomplished as follows:
00116 // <srcBlock>
00117 //    String imageName = argv[1];
00118 //    PagedImage<Float> image = ...; // An existing image from somewhere
00119 //    String fitsName = "new.fits";
00120 //    String error;
00121 //    Bool ok = ImageFITSConverter::ImageToFITS(error, image, fitsName);
00122 // </srcBlock>
00123 // A couple of similar remarks can be made about this example:
00124 // <ul>
00125 //    <li> If <src>ok</src> is False, the conversion failed and <src>error</src>
00126 //         will be set.
00127 // </ul>
00128 // </example>
00129 //
00130 // <motivation>
00131 // FITS files are the fundamental transport format for images in Astronomy.
00132 // </motivation>
00133 //
00134 // <todo asof="1999/02/15">
00135 //   <li> It might be useful to have functions that convert between FITS
00136 //        and general lattices.
00137 //   <li> Add support for PagedImage<Complex>
00138 //   <li> Convert multiple images at once?
00139 //   <li> Allow writing FITS files to an image extension in an existing
00140 //        FITS file.
00141 // </todo>
00142 
00143 class ImageFITSConverter
00144 {
00145 public:
00146         const static String CASAMBM;
00147 
00148     // Convert a FITS file to an AIPS++ image.
00149     // <ul>
00150     //   <li> <src>newImage</src> will be zero if the conversion fail. If the 
00151     //        conversion succeeds, the caller is responsible for deleting this
00152     //        pointer.
00153     //   <li> <src>error</src> will be set if the conversion fails.
00154     //   <li> If <src>imageName</src> is empty, a TempImage will be created,
00155     //        otherwise a PagedImage on disk.
00156     //   <li> <src>fitsName</src> must already exist (and have an image at the
00157     //        indicated HDU).
00158     //   <li> <src>whichRep</src> Zero-relative coordinate representation
00159     //        (Starting with wcs FITS multiple coordinate representations
00160     //         can be stored in a FITS file)
00161     //   <li> <src>whichHDU</src> Zero-relative hdu. The default is correct for
00162     //        a primary array, set it for an image extension. Only zero has been
00163     //        tested.
00164     //   <li> <src>memoryInMB</src>. Setting this to zero will result in
00165     //        row-by-row copying, otherwise it will attempt to with as large
00166     //        a chunk-size as possible, while fitting in the desired memory.
00167     //   <li> <src>allowOverwrite</src> If True, allow imageName to be 
00168     //        overwritten if it already exists.
00169     //   <li> <src>zeroBlanks</src> If True, allow any blanked pixels are set
00170     //         to zero rather than NaN
00171     // </ul>
00172     static Bool FITSToImage(ImageInterface<Float>*& newImage,
00173                             String &error,
00174                             const String &imageName,
00175                             const String &fitsName, 
00176                             uInt whichRep = 0,
00177                             uInt whichHDU = 0,
00178                             uInt memoryInMB = 64,
00179                             Bool allowOverwrite=False,
00180                             Bool zeroBlanks=False);
00181 
00182 // Old version
00183 //    static Bool FITSToImageOld(ImageInterface<Float>*& newImage,
00184 //                          String &error,
00185 //                          const String &imageName,
00186 //                          const String &fitsName, 
00187 //                          uInt whichHDU = 0,
00188 //                          uInt memoryInMB = 64,
00189 //                          Bool allowOverwrite=False,
00190 //                            Bool zeroBlanks=False);
00191 
00192     // Convert an AIPS++ image to a FITS file.
00193     // <ul>
00194     //   <li> <src>return</src> True if the conversion succeeds, False 
00195     //        otherwise.
00196     //   <li> <src>error</src> will be set if the conversion fails.
00197     //   <li> <src>image</src> The image to convert.
00198     //   <li> <src>fitsName</src> If the name is "-" (the minus character), 
00199     //        then write to stdout Always writes to the primary array.
00200     //   <li> <src>memoryInMB</src>. Setting this to zero will result in
00201     //        row-by-row copying, otherwise it will attempt to with as large
00202     //        a chunk-size as possible, while fitting in the desired memory.
00203     //   <li> <src>preferVelocity</src>Write a velocity primary spectral axis
00204     //        if possible.
00205     //   <li> <src>opticalVelocity</src>If writing a velocity, use the optical
00206     //        definition (otherwise use radio).
00207     //   <li> <src>BITPIX, minPix, maxPix</src>
00208     //        BITPIX can presently be set to -32 or 16 only. When BITPIX is
00209     //        16 it will write BSCALE and BZERO into the FITS file. If minPix
00210     //        is greater than maxPix the minimum and maximum pixel values
00211     //        will be determined from the array, otherwise the supplied
00212     //        values will be used and pixels outside that range will be
00213     //        truncated to the minimum and maximum pixel values (note that
00214     //        this truncation does not occur for BITPIX=-32).
00215     //   <li> <src>allowOverwrite</src> If True, allow fitsName to be 
00216     //        overwritten if it already exists.
00217     //   <li> <src>degenerateLast</src> If True, axes of length 1 will be written
00218     //        last to the header.
00219     //   <li> <src>preferWavelength</src> If True, write a wavelength primary axis.
00220     //   <li> <src>airWavelength</src> If True and <src>preferWavelength</src> is True write
00221     //        an air wavelength primary axis.
00222     //   <li> <src>origin</src> gives the origin, i.e., the name of the package.
00223     //        If empty, it defaults to "casacore-"getVersion().
00224     //   </ul>
00225     static Bool ImageToFITS(String &error,
00226                 ImageInterface<Float> &image,
00227                 const String &fitsName,
00228                 uInt memoryInMB = 64,
00229                 Bool preferVelocity = True,
00230                 Bool opticalVelocity = True,
00231                 Int BITPIX=-32,
00232                 Float minPix = 1.0, Float maxPix = -1.0,
00233                 Bool allowOverwrite=False,
00234                 Bool degenerateLast=False,
00235                 Bool verbose=True,
00236                 Bool stokesLast=False,
00237                 Bool preferWavelength=False,
00238                 Bool airWavelength=False,
00239                 const String& origin = String(),
00240                 Bool history=True);
00241 
00242     // Helper function - used to calculate a cursor appropriate for the desired
00243     // memory use. It's not intended that application programmers call this, but
00244     // you may if it's useful to you.
00245     static IPosition copyCursorShape(String &report,
00246                                      const IPosition &shape, 
00247                                      uInt imagePixelSize,
00248                                      uInt fitsPixelSize,
00249                                      uInt memoryInMB);
00250 
00251 // Recover CoordinateSystem from header.  Used keywords are removed from header
00252 // and the unused one returned in a Record for ease of use.  Degenerate axes 
00253 // may be added to shape if needed
00254     static CoordinateSystem getCoordinateSystem (Int& imageType, RecordInterface& headerRec,
00255                                                  const Vector<String>& header,
00256                                                  LogIO& os, uInt whichRep,
00257                                                  IPosition& shape, Bool dropStokes);
00258 
00259 // Old version
00260 //    static CoordinateSystem getCoordinateSystemOld (Int& imageType, RecordInterface& header,
00261 //                                                 LogIO& os, IPosition& shape, Bool dropStokes);
00262 
00263 // Recover ImageInfo from header. Used keywords are removed from header
00264     static ImageInfo getImageInfo (RecordInterface& header);
00265 
00266 //Old version
00267 //    static ImageInfo getImageInfoOld (RecordInterface& header);
00268 
00269 // Recover brightness unit from header. Used keywords are removed from header
00270     static Unit getBrightnessUnit (RecordInterface& header, LogIO& os);
00271 // Old version
00272 //    static Unit getBrightnessUnitOld (RecordInterface& header, LogIO& os);
00273 
00274 // Recover history from FITS file keywrod list into logger
00275    static void restoreHistory (LoggerHolder& logger,
00276                                ConstFitsKeywordList& kw);
00277                                
00278 // Parse header record and set MiscInfo
00279    static Bool extractMiscInfo (RecordInterface& miscInfo, const RecordInterface& header);
00280 
00281    // read the BEAMS table if present and add the restoring beams to <src>info</src>
00282    static void readBeamsTable(
00283                    ImageInfo& info, const String& filename,
00284                    const DataType type
00285    );
00286 
00287 private:
00288 
00289 // Put a CASA image to an opened FITS image
00290 // Parameters as in "ImageToFITS". In addition:
00291 // <ul>
00292 //   <li> <src>output</src> The FITS output to write to.
00293 //   <li> <src>primHead</src> Write to a primary HDU.
00294 //   <li> <src>allowAppend</src> Allow to append extension HDU's.
00295 // </ul>
00296    static Bool ImageToFITSOut(
00297                    String &error, LogIO &os,
00298                    const ImageInterface<Float> &image,
00299                    FitsOutput *output, uInt memoryInMB = 64,
00300                  Bool preferVelocity = True,
00301                  Bool opticalVelocity = True,
00302                  Int BITPIX=-32,
00303                  Float minPix = 1.0, Float maxPix = -1.0,
00304                  Bool degenerateLast=False,
00305                  Bool verbose=True,
00306                  Bool stokesLast=False,
00307                  Bool preferWavelength=False,
00308                  Bool airWavelength=False,
00309                  Bool primHead=True,
00310                  Bool allowAppend=False,
00311                  const String& origin = String(),
00312                  Bool history=True  
00313    );
00314 
00315 // Put a CASA image with quality coordinate
00316 // to an opened FITS file
00317 // Parameters as in "ImageToFITS". In addition:
00318 // <ul>
00319 //   <li> <src>output</src> The FITS output to write to.
00320 // </ul>
00321    static Bool QualImgToFITSOut(String &error,
00322                 LogIO &os,
00323                 ImageInterface<Float> &image,
00324                 FitsOutput *outfile,
00325                 uInt memoryInMB,
00326                 Bool preferVelocity,
00327                 Bool opticalVelocity,
00328                 Int BITPIX, Float minPix, Float maxPix,
00329                 Bool degenerateLast,
00330                 Bool verbose, Bool stokesLast,
00331                 Bool preferWavelength,
00332                 Bool airWavelength,
00333                 const String& origin,
00334                 Bool history
00335    );
00336 
00337    static Bool removeFile (String& error, const File& outFile,
00338                            const String& outName, Bool allowOverwrite);
00339 
00340 // Create an open FITS file with the name given
00341    static Bool openFitsOutput(String &error, FitsOutput *(&openFitsOutput), const String &fitsName,
00342                                             const Bool &allowOverwrite);
00343 
00344 
00345      static void _writeBeamsTable(FitsOutput *const &outfile, const ImageInfo& info);
00346 
00347 };
00348 
00349 
00350 // <summary>
00351 // This class is an internal class for ImageFITSConverter.
00352 // </summary>
00353 
00354 // <use visibility=local>
00355 
00356 // <synopsis>
00357 // This class is an internal class used to implement 
00358 // ImageFitsConverter::FITSToImage - in particular, it has the code which
00359 // is dependent on the various types (BITPIX values).
00360 // </synopsis>
00361 template<class HDUType> class ImageFITSConverterImpl
00362 {
00363 public:
00364     static void FITSToImage(
00365         ImageInterface<Float> *&newImage,
00366         String &error,
00367         const String &newImageName,
00368         const uInt whichRep,
00369         HDUType &fitsImage,
00370         const String& fitsFilename,
00371         const DataType dataType,
00372         const uInt memoryInMB = 64,
00373         const Bool zeroBlanks=False
00374         );
00375 
00376 };
00377 
00378 
00379 } //# NAMESPACE CASA - END
00380 
00381 #ifndef CASACORE_NO_AUTO_TEMPLATES
00382 #include <images/Images/ImageFITSConverter.tcc>
00383 #endif //# CASACORE_NO_AUTO_TEMPLATES
00384 
00385 #endif