casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImageFITSConverter.h
Go to the documentation of this file.
1 //# ImageFITSConverter.h: Interconvert between Casacore Images and FITS files
2 //# Copyright (C) 1996,1999,2001,2002
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //#
27 //# $Id$
28 
29 
30 #ifndef IMAGES_IMAGEFITSCONVERTER_H
31 #define IMAGES_IMAGEFITSCONVERTER_H
32 
33 #include <casacore/casa/aips.h>
39 
40 
41 #ifndef WCSLIB_GETWCSTAB
42 #define WCSLIB_GETWCSTAB
43 #endif
44 
45 namespace casacore { //# NAMESPACE CASACORE - BEGIN
46 
47  template<class T> class PagedImage;
48  template<class T> class ImageInterface;
49  template<class T> class Vector;
50  class FitsOutput;
51  class File;
52  class ImageInfo;
53  class CoordinateSystem;
54  class RecordInterface;
55  class TableRecord;
56  class LogIO;
57  class Unit;
58  class LoggerHolder;
59  class ConstFitsKeywordList;
60  class FitsInput;
61 
62  // <summary>
63  // Struct holding information derived from the image and its header
64  // </summary>
65  // <synopsis>
66  // This is a helper struct to pass information from ImageHeaderToFITS
67  // to ImageToFITSOut.
68  // </synopsis>
77  double minPix;
78  double maxPix;
83  };
84 
85 
86  // <summary>
87  // Interconvert between Casacore Images and FITS files.
88  // </summary>
89 
90  // <use visibility=export>
91 
92  // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
93  // </reviewed>
94 
95  // <prerequisite>
96  // <li> <linkto class="PagedImage">PagedImage</linkto>
97  // <li> <linkto class="PrimaryArray">PrimaryArray</linkto> (and FITS concepts in
98  // general).
99  // </prerequisite>
100  //
101  // <synopsis>
102  // This class is a helper class that is used to interconvert between Casacore
103  // images and FITS files. This adds no functionality over the general abilities
104  // available in the underlying FITS classes, however it is a useful higher-level
105  // packaging.
106  //
107  // There are two fundamental member functions in this class.
108  // <src>FITSToImage</src> which turns a FITS file into a Casacore image, and
109  // <src>ImageToFITS</src> which does the opposite.
110  //
111  // We can read images from any HDU inside the FITS file (although this isn't
112  // well tested). Images with a quality axis (i.e. contain data and error values)
113  // are stored in the primary HDU (data) and an extension HDU (error). Other
114  // images are always written to the primary HDU.
115  //
116  // Pixels in the FITS file which are blanked are masked out (the mask
117  // is set to False) in the output image. On conversion to FITS,
118  // masked values are blanked. The mask which is read is the current
119  // default mask.
120  // </synopsis>
121  //
122  // <example>
123  // A FITS to image conversion may be accomplished as follows:
124  // <srcBlock>
125  // PagedImage<Float> *image = 0;
126  // String fitsName = "exists.fits";
127  // String imageName = "new.image";
128  // String error;
129  // Bool ok = ImageFITSConverter::FITSToImage(image, error, imageName, fitsName);
130  // if (!image) ... error ...
131  // </srcBlock>
132  // A couple of things to note:
133  // <ul>
134  // <li> If <src>ok</src> is False, the conversion failed and <src>error</src>
135  // will be set.
136  // <li> The pointer "image" is set if the conversion succeeds. If it is
137  // zero the conversion failed and <src>error</src> will contain an
138  // error message.
139  // <li> The caller is responsible for deleting the pointer <src>image</src>
140  // when the conversion is successful.
141  // </ul>
142  // Similarly, an image to FITS conversion may be accomplished as follows:
143  // <srcBlock>
144  // String imageName = argv[1];
145  // PagedImage<Float> image = ...; // An existing image from somewhere
146  // String fitsName = "new.fits";
147  // String error;
148  // Bool ok = ImageFITSConverter::ImageToFITS(error, image, fitsName);
149  // </srcBlock>
150  // A couple of similar remarks can be made about this example:
151  // <ul>
152  // <li> If <src>ok</src> is False, the conversion failed and <src>error</src>
153  // will be set.
154  // </ul>
155  // </example>
156  //
157  // <motivation>
158  // FITS files are the fundamental transport format for images in Astronomy.
159  // </motivation>
160  //
161  // <todo asof="1999/02/15">
162  // <li> It might be useful to have functions that convert between FITS
163  // and general lattices.
164  // <li> Add support for PagedImage<Complex>
165  // <li> Convert multiple images at once?
166  // <li> Allow writing FITS files to an image extension in an existing
167  // FITS file.
168  // </todo>
169 
171  {
172  public:
173  const static String CASAMBM;
174 
175  // Convert a FITS file to a Casacore image.
176  // <ul>
177  // <li> <src>newImage</src> will be zero if the conversion fail. If the
178  // conversion succeeds, the caller is responsible for deleting this
179  // pointer.
180  // <li> <src>error</src> will be set if the conversion fails.
181  // <li> If <src>imageName</src> is empty, a TempImage will be created,
182  // otherwise a PagedImage on disk.
183  // <li> <src>fitsName</src> must already exist (and have an image at the
184  // indicated HDU).
185  // <li> <src>whichRep</src> Zero-relative coordinate representation
186  // (Starting with wcs FITS multiple coordinate representations
187  // can be stored in a FITS file)
188  // <li> <src>whichHDU</src> Zero-relative hdu. The default is correct for
189  // a primary array, set it for an image extension. A value of -1
190  // makes the code look for the first readable HDU.
191  // <li> <src>memoryInMB</src>. Setting this to zero will result in
192  // row-by-row copying, otherwise it will attempt to with as large
193  // a chunk-size as possible, while fitting in the desired memory.
194  // <li> <src>allowOverwrite</src> If True, allow imageName to be
195  // overwritten if it already exists.
196  // <li> <src>zeroBlanks</src> If True, allow any blanked pixels are set
197  // to zero rather than NaN
198  // </ul>
199  static Bool FITSToImage(ImageInterface<Float>*& newImage,
200  String &error,
201  const String &imageName,
202  const String &fitsName,
203  uInt whichRep = 0,
204  Int whichHDU = 0,
205  uInt memoryInMB = 64,
206  Bool allowOverwrite=False,
207  Bool zeroBlanks=False);
208 
209  // Convert a Casacore image to a FITS file.
210  // <ul>
211  // <li> <src>return</src> True if the conversion succeeds, False
212  // otherwise.
213  // <li> <src>error</src> will be set if the conversion fails.
214  // <li> <src>image</src> The image to convert.
215  // <li> <src>fitsName</src> If the name is "-" (the minus character),
216  // then write to stdout Always writes to the primary array.
217  // <li> <src>memoryInMB</src>. Setting this to zero will result in
218  // row-by-row copying, otherwise it will attempt to with as large
219  // a chunk-size as possible, while fitting in the desired memory.
220  // <li> <src>preferVelocity</src>Write a velocity primary spectral axis
221  // if possible.
222  // <li> <src>opticalVelocity</src>If writing a velocity, use the optical
223  // definition (otherwise use radio).
224  // <li> <src>BITPIX, minPix, maxPix</src>
225  // BITPIX can presently be set to -32 or 16 only. When BITPIX is
226  // 16 it will write BSCALE and BZERO into the FITS file. If minPix
227  // is greater than maxPix the minimum and maximum pixel values
228  // will be determined from the array, otherwise the supplied
229  // values will be used and pixels outside that range will be
230  // truncated to the minimum and maximum pixel values (note that
231  // this truncation does not occur for BITPIX=-32).
232  // <li> <src>allowOverwrite</src> If True, allow fitsName to be
233  // overwritten if it already exists.
234  // <li> <src>degenerateLast</src> If True, axes of length 1 will be written
235  // last to the header.
236  // <li> <src>preferWavelength</src> If True, write a wavelength primary axis.
237  // <li> <src>airWavelength</src> If True and <src>preferWavelength</src> is True write
238  // an air wavelength primary axis.
239  // <li> <src>origin</src> gives the origin, i.e., the name of the package.
240  // If empty, it defaults to "casacore-"getVersion().
241  // </ul>
242  // <group>
243  static Bool ImageToFITS(String &error,
244  ImageInterface<Float> &image,
245  const String &fitsName,
246  uInt memoryInMB = 64,
247  Bool preferVelocity = True,
248  Bool opticalVelocity = True,
249  Int BITPIX=-32,
250  Float minPix = 1.0, Float maxPix = -1.0,
251  Bool allowOverwrite=False,
252  Bool degenerateLast=False,
253  Bool verbose=True,
254  Bool stokesLast=False,
255  Bool preferWavelength=False,
256  Bool airWavelength=False,
257  const String& origin = String(),
258  Bool history=True);
259  static Bool ImageHeaderToFITS(String &error,
260  ImageFITSHeaderInfo& fhi,
261  const ImageInterface<Float> &image,
262  Bool preferVelocity = True,
263  Bool opticalVelocity = True,
264  Int BITPIX=-32,
265  Float minPix = 1.0, Float maxPix = -1.0,
266  Bool degenerateLast=False,
267  Bool verbose=True,
268  Bool stokesLast=False,
269  Bool preferWavelength=False,
270  Bool airWavelength=False,
271  Bool primHead = True,
272  Bool allowAppend = True,
273  const String& origin = String(),
274  Bool history=True);
275  // </group>
276 
277  // Helper function - used to calculate a cursor appropriate for the
278  // desired memory use. It's not intended that application programmers
279  // call this, but you may if it's useful to you.
280  static IPosition copyCursorShape(String &report,
281  const IPosition &shape,
282  uInt imagePixelSize,
283  uInt fitsPixelSize,
284  uInt memoryInMB);
285 
286  // Recover CoordinateSystem from header.
287  // Used keywords are removed from header and the unused ones returned
288  // in a Record for ease of use.
289  // Degenerate axes may be added to shape if needed.
290  static CoordinateSystem getCoordinateSystem (Int& imageType,
291  RecordInterface& headerRec,
292  const Vector<String>& header,
293  LogIO& os, uInt whichRep,
294  IPosition& shape,
295  Bool dropStokes);
296 
297  // Recover ImageInfo from header. Used keywords are removed from header
298  static ImageInfo getImageInfo (RecordInterface& header);
299 
300  // Recover brightness unit from header.
301  // Used keywords are removed from header.
302  static Unit getBrightnessUnit (RecordInterface& header, LogIO& os);
303 
304  // Recover history from FITS file keyword list into logger.
305  static void restoreHistory (LoggerHolder& logger,
307 
308  // Parse header record and set MiscInfo
309  static Bool extractMiscInfo (RecordInterface& miscInfo,
310  const RecordInterface& header);
311 
312  // Read the BEAMS table if present and add the restoring beams to
313  // <src>info</src>.
314  static void readBeamsTable (ImageInfo& info, const String& filename,
315  const DataType type);
316 
317  private:
318 
319  // Put a CASA image to an opened FITS image
320  // Parameters as in "ImageToFITS". In addition:
321  // <ul>
322  // <li> <src>output</src> The FITS output to write to.
323  // <li> <src>primHead</src> Write to a primary HDU.
324  // <li> <src>allowAppend</src> Allow to append extension HDU's.
325  // </ul>
326  static Bool ImageToFITSOut (String &error, LogIO &os,
327  const ImageInterface<Float> &image,
328  FitsOutput *output, uInt memoryInMB = 64,
329  Bool preferVelocity = True,
330  Bool opticalVelocity = True,
331  Int BITPIX=-32,
332  Float minPix = 1.0, Float maxPix = -1.0,
333  Bool degenerateLast=False,
334  Bool verbose=True,
335  Bool stokesLast=False,
336  Bool preferWavelength=False,
337  Bool airWavelength=False,
338  Bool primHead=True,
339  Bool allowAppend=False,
340  const String& origin = String(),
341  Bool history=True);
342 
343  // Put a CASA image with quality coordinate
344  // to an opened FITS file
345  // Parameters as in "ImageToFITS". In addition:
346  // <ul>
347  // <li> <src>output</src> The FITS output to write to.
348  // </ul>
349  static Bool QualImgToFITSOut (String &error,
350  LogIO &os,
351  ImageInterface<Float> &image,
352  FitsOutput *outfile,
353  uInt memoryInMB,
354  Bool preferVelocity,
355  Bool opticalVelocity,
356  Int BITPIX, Float minPix, Float maxPix,
357  Bool degenerateLast,
358  Bool verbose, Bool stokesLast,
359  Bool preferWavelength,
360  Bool airWavelength,
361  const String& origin,
362  Bool history);
363 
364  // If existing, remove the file, symlink, or directory given by
365  // <src>outFile</src>. It is only removed if allowOverwrite=True.
366  // An exception (using argument outName) is thrown if the file could
367  // not be removed.
368  static Bool removeFile (String& error, const File& outFile,
369  const String& outName, Bool allowOverwrite);
370 
371  // Create an open FITS file with the name given
372  static Bool openFitsOutput (String &error, FitsOutput *(&openFitsOutput),
373  const String &fitsName,
374  const Bool &allowOverwrite);
375 
376 
377  static void _writeBeamsTable (FitsOutput *const &outfile,
378  const ImageInfo& info);
379 
380  };
381 
382 
383  // <summary>
384  // This class is an internal class for ImageFITSConverter.
385  // </summary>
386 
387  // <use visibility=local>
388 
389  // <synopsis>
390  // This class is an internal class used to implement
391  // ImageFitsConverter::FITSToImage - in particular, it has the code which
392  // is dependent on the various types (BITPIX values).
393  // </synopsis>
394  template<class HDUType> class ImageFITSConverterImpl
395  {
396  public:
397  static void FITSToImage(ImageInterface<Float> *&newImage,
398  String &error,
399  const String &newImageName,
400  const uInt whichRep,
401  HDUType &fitsImage,
402  const String& fitsFilename,
403  const DataType dataType,
404  const uInt memoryInMB = 64,
405  const Bool zeroBlanks=False);
406  };
407 
408 
409 } //# NAMESPACE CASACORE - END
410 
411 #ifndef CASACORE_NO_AUTO_TEMPLATES
412 #include <casacore/images/Images/ImageFITSConverter.tcc>
413 #endif //# CASACORE_NO_AUTO_TEMPLATES
414 
415 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
A 1-D Specialization of the Array class.
int Int
Definition: aipstype.h:50
std::vector< double > Vector
Definition: ds9context.h:24
static void _writeBeamsTable(FitsOutput *const &outfile, const ImageInfo &info)
static Bool extractMiscInfo(RecordInterface &miscInfo, const RecordInterface &header)
Parse header record and set MiscInfo.
static void readBeamsTable(ImageInfo &info, const String &filename, const DataType type)
Read the BEAMS table if present and add the restoring beams to info.
Interconvert between Casacore Images and FITS files.
This class is an internal class for ImageFITSConverter.
Struct holding information derived from the image and its header.
static Bool openFitsOutput(String &error, FitsOutput *(&openFitsOutput), const String &fitsName, const Bool &allowOverwrite)
Create an open FITS file with the name given.
virtual casacore::String type() const
Implements RegionShape::type.
Definition: RegionShapes.h:548
ostream-like interface to creating log messages.
Definition: LogIO.h:167
CountedPtr< Array< Bool > > pMask
short Short
Definition: aipstype.h:48
static Bool FITSToImage(ImageInterface< Float > *&newImage, String &error, const String &imageName, const String &fitsName, uInt whichRep=0, Int whichHDU=0, uInt memoryInMB=64, Bool allowOverwrite=False, Bool zeroBlanks=False)
Convert a FITS file to a Casacore image.
defines physical units
Definition: Unit.h:189
static Bool removeFile(String &error, const File &outFile, const String &outName, Bool allowOverwrite)
If existing, remove the file, symlink, or directory given by outFile.
static ImageInfo getImageInfo(RecordInterface &header)
Recover ImageInfo from header.
Referenced counted pointer for constant data.
Definition: VisModelData.h:42
static Unit getBrightnessUnit(RecordInterface &header, LogIO &os)
Recover brightness unit from header.
static Bool QualImgToFITSOut(String &error, LogIO &os, ImageInterface< Float > &image, FitsOutput *outfile, uInt memoryInMB, Bool preferVelocity, Bool opticalVelocity, Int BITPIX, Float minPix, Float maxPix, Bool degenerateLast, Bool verbose, Bool stokesLast, Bool preferWavelength, Bool airWavelength, const String &origin, Bool history)
Put a CASA image with quality coordinate to an opened FITS file Parameters as in &quot;ImageToFITS&quot;.
static Bool ImageToFITS(String &error, ImageInterface< Float > &image, const String &fitsName, uInt memoryInMB=64, Bool preferVelocity=True, Bool opticalVelocity=True, Int BITPIX=-32, Float minPix=1.0, Float maxPix=-1.0, Bool allowOverwrite=False, Bool degenerateLast=False, Bool verbose=True, Bool stokesLast=False, Bool preferWavelength=False, Bool airWavelength=False, const String &origin=String(), Bool history=True)
Convert a Casacore image to a FITS file.
double Double
Definition: aipstype.h:55
Class to get file information and a base for other file classes.
Definition: File.h:101
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
list of read-only FITS keywords
Definition: fits.h:955
static CoordinateSystem getCoordinateSystem(Int &imageType, RecordInterface &headerRec, const Vector< String > &header, LogIO &os, uInt whichRep, IPosition &shape, Bool dropStokes)
Recover CoordinateSystem from header.
float Float
Definition: aipstype.h:54
const Bool False
Definition: aipstype.h:44
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape...
Definition: ExprNode.h:1944
virtual Origin origin() const =0
ABSTRACT METHODS //.
static Bool ImageToFITSOut(String &error, LogIO &os, const ImageInterface< Float > &image, FitsOutput *output, uInt memoryInMB=64, Bool preferVelocity=True, Bool opticalVelocity=True, Int BITPIX=-32, Float minPix=1.0, Float maxPix=-1.0, Bool degenerateLast=False, Bool verbose=True, Bool stokesLast=False, Bool preferWavelength=False, Bool airWavelength=False, Bool primHead=True, Bool allowAppend=False, const String &origin=String(), Bool history=True)
Put a CASA image to an opened FITS image Parameters as in &quot;ImageToFITS&quot;.
fixed-length sequential blocked FITS output
Definition: fitsio.h:228
linked list of FITS keywords
Definition: fits.h:741
static Bool ImageHeaderToFITS(String &error, ImageFITSHeaderInfo &fhi, const ImageInterface< Float > &image, Bool preferVelocity=True, Bool opticalVelocity=True, Int BITPIX=-32, Float minPix=1.0, Float maxPix=-1.0, Bool degenerateLast=False, Bool verbose=True, Bool stokesLast=False, Bool preferWavelength=False, Bool airWavelength=False, Bool primHead=True, Bool allowAppend=True, const String &origin=String(), Bool history=True)
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Abstract base class for Record classes.
Class holding a hierarchy of loggers.
Definition: LoggerHolder.h:120
static void FITSToImage(ImageInterface< Float > *&newImage, String &error, const String &newImageName, const uInt whichRep, HDUType &fitsImage, const String &fitsFilename, const DataType dataType, const uInt memoryInMB=64, const Bool zeroBlanks=False)
static void restoreHistory(LoggerHolder &logger, ConstFitsKeywordList &kw)
Recover history from FITS file keyword list into logger.
Miscellaneous information related to an image.
Definition: ImageInfo.h:92
const Bool True
Definition: aipstype.h:43
static IPosition copyCursorShape(String &report, const IPosition &shape, uInt imagePixelSize, uInt fitsPixelSize, uInt memoryInMB)
Helper function - used to calculate a cursor appropriate for the desired memory use.
Interconvert pixel and world coordinates.
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42