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