casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef IMAGES_IMAGEFITSCONVERTER_H
28#define IMAGES_IMAGEFITSCONVERTER_H
29
30#include <casacore/casa/aips.h>
31#include <casacore/fits/FITS/fits.h>
32#include <casacore/casa/Arrays/ArrayFwd.h>
33#include <casacore/casa/Arrays/IPosition.h>
34#include <casacore/casa/BasicSL/String.h>
35#include <casacore/casa/Utilities/DataType.h>
36#include <memory>
37
38
39#ifndef WCSLIB_GETWCSTAB
40#define WCSLIB_GETWCSTAB
41#endif
42
43namespace casacore { //# NAMESPACE CASACORE - BEGIN
44
45 template<class T> class PagedImage;
46 template<class T> class ImageInterface;
47 class FitsOutput;
48 class File;
49 class ImageInfo;
50 class CoordinateSystem;
51 class RecordInterface;
52 class TableRecord;
53 class LogIO;
54 class Unit;
55 class LoggerHolder;
56 class ConstFitsKeywordList;
57 class FitsInput;
58
59 // <summary>
60 // Struct holding information derived from the image and its header
61 // </summary>
62 // <synopsis>
63 // This is a helper struct to pass information from ImageHeaderToFITS
64 // to ImageToFITSOut.
65 // </synopsis>
81
82
83 // <summary>
84 // Interconvert between Casacore Images and FITS files.
85 // </summary>
86
87 // <use visibility=export>
88
89 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
90 // </reviewed>
91
92 // <prerequisite>
93 // <li> <linkto class="PagedImage">PagedImage</linkto>
94 // <li> <linkto class="PrimaryArray">PrimaryArray</linkto> (and FITS concepts in
95 // general).
96 // </prerequisite>
97 //
98 // <synopsis>
99 // This class is a helper class that is used to interconvert between Casacore
100 // images and FITS files. This adds no functionality over the general abilities
101 // available in the underlying FITS classes, however it is a useful higher-level
102 // packaging.
103 //
104 // There are two fundamental member functions in this class.
105 // <src>FITSToImage</src> which turns a FITS file into a Casacore image, and
106 // <src>ImageToFITS</src> which does the opposite.
107 //
108 // We can read images from any HDU inside the FITS file (although this isn't
109 // well tested). Images with a quality axis (i.e. contain data and error values)
110 // are stored in the primary HDU (data) and an extension HDU (error). Other
111 // images are always written to the primary HDU.
112 //
113 // Pixels in the FITS file which are blanked are masked out (the mask
114 // is set to False) in the output image. On conversion to FITS,
115 // masked values are blanked. The mask which is read is the current
116 // default mask.
117 // </synopsis>
118 //
119 // <example>
120 // A FITS to image conversion may be accomplished as follows:
121 // <srcBlock>
122 // PagedImage<Float> *image = 0;
123 // String fitsName = "exists.fits";
124 // String imageName = "new.image";
125 // String error;
126 // Bool ok = ImageFITSConverter::FITSToImage(image, error, imageName, fitsName);
127 // if (!image) ... error ...
128 // </srcBlock>
129 // A couple of things to note:
130 // <ul>
131 // <li> If <src>ok</src> is False, the conversion failed and <src>error</src>
132 // will be set.
133 // <li> The pointer "image" is set if the conversion succeeds. If it is
134 // zero the conversion failed and <src>error</src> will contain an
135 // error message.
136 // <li> The caller is responsible for deleting the pointer <src>image</src>
137 // when the conversion is successful.
138 // </ul>
139 // Similarly, an image to FITS conversion may be accomplished as follows:
140 // <srcBlock>
141 // String imageName = argv[1];
142 // PagedImage<Float> image = ...; // An existing image from somewhere
143 // String fitsName = "new.fits";
144 // String error;
145 // Bool ok = ImageFITSConverter::ImageToFITS(error, image, fitsName);
146 // </srcBlock>
147 // A couple of similar remarks can be made about this example:
148 // <ul>
149 // <li> If <src>ok</src> is False, the conversion failed and <src>error</src>
150 // will be set.
151 // </ul>
152 // </example>
153 //
154 // <motivation>
155 // FITS files are the fundamental transport format for images in Astronomy.
156 // </motivation>
157 //
158 // <todo asof="1999/02/15">
159 // <li> It might be useful to have functions that convert between FITS
160 // and general lattices.
161 // <li> Add support for PagedImage<Complex>
162 // <li> Convert multiple images at once?
163 // <li> Allow writing FITS files to an image extension in an existing
164 // FITS file.
165 // </todo>
166
168 {
169 public:
170 const static String CASAMBM;
171
172 // Convert a FITS file to a Casacore image.
173 // <ul>
174 // <li> <src>newImage</src> will be zero if the conversion fail. If the
175 // conversion succeeds, the caller is responsible for deleting this
176 // pointer.
177 // <li> <src>error</src> will be set if the conversion fails.
178 // <li> If <src>imageName</src> is empty, a TempImage will be created,
179 // otherwise a PagedImage on disk.
180 // <li> <src>fitsName</src> must already exist (and have an image at the
181 // indicated HDU).
182 // <li> <src>whichRep</src> Zero-relative coordinate representation
183 // (Starting with wcs FITS multiple coordinate representations
184 // can be stored in a FITS file)
185 // <li> <src>whichHDU</src> Zero-relative hdu. The default is correct for
186 // a primary array, set it for an image extension. A value of -1
187 // makes the code look for the first readable HDU.
188 // <li> <src>memoryInMB</src>. Setting this to zero will result in
189 // row-by-row copying, otherwise it will attempt to with as large
190 // a chunk-size as possible, while fitting in the desired memory.
191 // <li> <src>allowOverwrite</src> If True, allow imageName to be
192 // overwritten if it already exists.
193 // <li> <src>zeroBlanks</src> If True, allow any blanked pixels are set
194 // to zero rather than NaN
195 // </ul>
197 String &error,
198 const String &imageName,
199 const String &fitsName,
200 uInt whichRep = 0,
201 Int whichHDU = 0,
202 uInt memoryInMB = 64,
203 Bool allowOverwrite=False,
204 Bool zeroBlanks=False);
205
206 // Convert a Casacore image to a FITS file.
207 // <ul>
208 // <li> <src>return</src> True if the conversion succeeds, False
209 // otherwise.
210 // <li> <src>error</src> will be set if the conversion fails.
211 // <li> <src>image</src> The image to convert.
212 // <li> <src>fitsName</src> If the name is "-" (the minus character),
213 // then write to stdout Always writes to the primary array.
214 // <li> <src>memoryInMB</src>. Setting this to zero will result in
215 // row-by-row copying, otherwise it will attempt to with as large
216 // a chunk-size as possible, while fitting in the desired memory.
217 // <li> <src>preferVelocity</src>Write a velocity primary spectral axis
218 // if possible.
219 // <li> <src>opticalVelocity</src>If writing a velocity, use the optical
220 // definition (otherwise use radio).
221 // <li> <src>BITPIX, minPix, maxPix</src>
222 // BITPIX can presently be set to -32 or 16 only. When BITPIX is
223 // 16 it will write BSCALE and BZERO into the FITS file. If minPix
224 // is greater than maxPix the minimum and maximum pixel values
225 // will be determined from the array, otherwise the supplied
226 // values will be used and pixels outside that range will be
227 // truncated to the minimum and maximum pixel values (note that
228 // this truncation does not occur for BITPIX=-32).
229 // <li> <src>allowOverwrite</src> If True, allow fitsName to be
230 // overwritten if it already exists.
231 // <li> <src>degenerateLast</src> If True, axes of length 1 will be written
232 // last to the header.
233 // <li> <src>preferWavelength</src> If True, write a wavelength primary axis.
234 // <li> <src>airWavelength</src> If True and <src>preferWavelength</src> is True write
235 // an air wavelength primary axis.
236 // <li> <src>origin</src> gives the origin, i.e., the name of the package.
237 // If empty, it defaults to "casacore-"getVersion().
238 // </ul>
239 // <group>
240 static Bool ImageToFITS(String &error,
242 const String &fitsName,
243 uInt memoryInMB = 64,
244 Bool preferVelocity = True,
245 Bool opticalVelocity = True,
246 Int BITPIX=-32,
247 Float minPix = 1.0, Float maxPix = -1.0,
248 Bool allowOverwrite=False,
249 Bool degenerateLast=False,
250 Bool verbose=True,
251 Bool stokesLast=False,
252 Bool preferWavelength=False,
253 Bool airWavelength=False,
254 const String& origin = String(),
255 Bool history=True);
258 const ImageInterface<Float> &image,
259 Bool preferVelocity = True,
260 Bool opticalVelocity = True,
261 Int BITPIX=-32,
262 Float minPix = 1.0, Float maxPix = -1.0,
263 Bool degenerateLast=False,
264 Bool verbose=True,
265 Bool stokesLast=False,
266 Bool preferWavelength=False,
267 Bool airWavelength=False,
268 Bool primHead = True,
269 Bool allowAppend = True,
270 const String& origin = String(),
271 Bool history=True);
272 // </group>
273
274 // Helper function - used to calculate a cursor appropriate for the
275 // desired memory use. It's not intended that application programmers
276 // call this, but you may if it's useful to you.
278 const IPosition &shape,
279 uInt imagePixelSize,
280 uInt fitsPixelSize,
281 uInt memoryInMB);
282
283 // Recover CoordinateSystem from header.
284 // Used keywords are removed from header and the unused ones returned
285 // in a Record for ease of use.
286 // Degenerate axes may be added to shape if needed.
288 RecordInterface& headerRec,
289 const Vector<String>& header,
290 LogIO& os, uInt whichRep,
292 Bool dropStokes);
293
294 // Recover ImageInfo from header. Used keywords are removed from header
296
297 // Recover brightness unit from header.
298 // Used keywords are removed from header.
300
301 // Recover history from FITS file keyword list into logger.
302 static void restoreHistory (LoggerHolder& logger,
304
305 // Parse header record and set MiscInfo
307 const RecordInterface& header);
308
309 // Read the BEAMS table if present and add the restoring beams to
310 // <src>info</src>.
311 static void readBeamsTable (ImageInfo& info, const String& filename,
312 const DataType type);
313
314 private:
315
316 // Put a CASA image to an opened FITS image
317 // Parameters as in "ImageToFITS". In addition:
318 // <ul>
319 // <li> <src>output</src> The FITS output to write to.
320 // <li> <src>primHead</src> Write to a primary HDU.
321 // <li> <src>allowAppend</src> Allow to append extension HDU's.
322 // </ul>
323 static Bool ImageToFITSOut (String &error, LogIO &os,
324 const ImageInterface<Float> &image,
325 FitsOutput *output, uInt memoryInMB = 64,
326 Bool preferVelocity = True,
327 Bool opticalVelocity = True,
328 Int BITPIX=-32,
329 Float minPix = 1.0, Float maxPix = -1.0,
330 Bool degenerateLast=False,
331 Bool verbose=True,
332 Bool stokesLast=False,
333 Bool preferWavelength=False,
334 Bool airWavelength=False,
335 Bool primHead=True,
336 Bool allowAppend=False,
337 const String& origin = String(),
338 Bool history=True);
339
340 // Put a CASA image with quality coordinate
341 // to an opened FITS file
342 // Parameters as in "ImageToFITS". In addition:
343 // <ul>
344 // <li> <src>output</src> The FITS output to write to.
345 // </ul>
347 LogIO &os,
349 FitsOutput *outfile,
350 uInt memoryInMB,
351 Bool preferVelocity,
352 Bool opticalVelocity,
353 Int BITPIX, Float minPix, Float maxPix,
354 Bool degenerateLast,
355 Bool verbose, Bool stokesLast,
356 Bool preferWavelength,
357 Bool airWavelength,
358 const String& origin,
359 Bool history);
360
361 // If existing, remove the file, symlink, or directory given by
362 // <src>outFile</src>. It is only removed if allowOverwrite=True.
363 // An exception (using argument outName) is thrown if the file could
364 // not be removed.
365 static Bool removeFile (String& error, const File& outFile,
366 const String& outName, Bool allowOverwrite);
367
368 // Create an open FITS file with the name given
370 const String &fitsName,
371 const Bool &allowOverwrite);
372
373
374 static void _writeBeamsTable (FitsOutput *const &outfile,
375 const ImageInfo& info);
376
377 };
378
379
380 // <summary>
381 // This class is an internal class for ImageFITSConverter.
382 // </summary>
383
384 // <use visibility=local>
385
386 // <synopsis>
387 // This class is an internal class used to implement
388 // ImageFitsConverter::FITSToImage - in particular, it has the code which
389 // is dependent on the various types (BITPIX values).
390 // </synopsis>
391 template<class HDUType> class ImageFITSConverterImpl
392 {
393 public:
394 static void FITSToImage(ImageInterface<Float> *&newImage,
395 String &error,
396 const String &newImageName,
397 const uInt whichRep,
398 HDUType &fitsImage,
399 const String& fitsFilename,
400 const DataType dataType,
401 const uInt memoryInMB = 64,
402 const Bool zeroBlanks=False);
403 };
404
405
406} //# NAMESPACE CASACORE - END
407
408#ifndef CASACORE_NO_AUTO_TEMPLATES
409#include <casacore/images/Images/ImageFITSConverter.tcc>
410#endif //# CASACORE_NO_AUTO_TEMPLATES
411
412#endif
list of read-only FITS keywords
Definition fits.h:949
linked list of FITS keywords
Definition fits.h:735
fixed-length sequential blocked FITS output
Definition fitsio.h:226
This class is an internal class for ImageFITSConverter.
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)
Interconvert between Casacore Images and FITS files.
static ImageInfo getImageInfo(RecordInterface &header)
Recover ImageInfo 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 "ImageToFITS".
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.
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.
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)
static void _writeBeamsTable(FitsOutput *const &outfile, const ImageInfo &info)
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 "ImageToFITS".
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 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.
static Unit getBrightnessUnit(RecordInterface &header, LogIO &os)
Recover brightness unit from header.
static void restoreHistory(LoggerHolder &logger, ConstFitsKeywordList &kw)
Recover history from FITS file keyword list into logger.
static Bool openFitsOutput(String &error, FitsOutput *(&openFitsOutput), const String &fitsName, const Bool &allowOverwrite)
Create an open FITS file with the name given.
static Bool extractMiscInfo(RecordInterface &miscInfo, const RecordInterface &header)
Parse header record and set MiscInfo.
static CoordinateSystem getCoordinateSystem(Int &imageType, RecordInterface &headerRec, const Vector< String > &header, LogIO &os, uInt whichRep, IPosition &shape, Bool dropStokes)
Recover CoordinateSystem from header.
static void readBeamsTable(ImageInfo &info, const String &filename, const DataType type)
Read the BEAMS table if present and add the restoring beams to info.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
short Short
Definition aipstype.h:46
unsigned int uInt
Definition aipstype.h:49
TableExprNode shape(const TableExprNode &array)
Function operating on any scalar or array resulting in a Double array containing the shape.
Definition ExprNode.h:1991
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53
std::shared_ptr< Array< Bool > > pMask