casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fitsio.h
Go to the documentation of this file.
1 //# fitsio.h:
2 //# Copyright (C) 1993,1994,1995,1996,1999,2001,2003
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 //# $Id$
27 
28 #ifndef FITS_FITSIO_H
29 #define FITS_FITSIO_H
30 
31 #include <casacore/casa/aips.h>
32 # include <casacore/fits/FITS/fits.h>
34 # include <casacore/fits/FITS/hdu.h>
35 //# include <casacore/casa/stdvector.h>
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 //<summary> sequential FITS I/O </summary>
41 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
42 // </reviewed>
43 //<synopsis>
44 // FitsIO is a base class that handles all the sequential blocked
45 // FITS I/O. Special derived classes do the input and output.
46 // No interpretation of the data is attempted here, there are
47 // special FITS classes that handle syntax and interpretation.
48 //</synopsis>
49 //<example>
50 //<srcblock>
51 // FitsInput fin("myfile.fits",FITS::Disk); // open disk file for FITS input
52 // if (fin.err() == FitsIO::IOERR) { // check if open succeeded
53 // cout << "Could not open FITS input\n";
54 // exit(0);
55 // }
56 // if (fin.rectype() == FITS::HDURecord && // test for primary array
57 // fin.hdutype() == FITS::PrimaryArrayHDU) {
58 // }
59 //</srcblock>
60 //</example>
61 
62 class FitsIO {
63  public:
64  virtual ~FitsIO();
65 
66  // error return code. Should be one of an
67  // enumerated type:
68  //
69  //# until cxx2html can handle enum() we duplicate it here
70  //
71  //<srcblock>
72  // enum FitsErrs { OK, IOERR, MISSKEY, BADBEGIN, EMPTYFILE,
73  // NOPRIMARY, BADOPER, BADEOF, MEMERR, BADBITPIX, NOAXISN,
74  // NOPCOUNT, NOGCOUNT, BADPCOUNT, BADGCOUNT, NOGROUPS,
75  // BADNAXIS, BADPRIMARY, BADSIZE, HDUERR };
76  //</srcblock>
77  //<group>
82  int err() const { return m_err_status; }
83  //</group>
84  //
85  // record size, in bytes, of a FITS block.
86  // Normally set at 2880, unless some form of blocking was used.
87  int fitsrecsize() const { return m_recsize; }
88  // is it a valid fits file (SIMPLE==T). If not, the only
89  // safest operation is to skip the data portion of the
90  // current HeaderDataUnit
91  Bool isafits() const { return m_valid_fits; }
92  // see if there may be FITS extensions present (EXTENT==T)
93  Bool isextend() const { return m_extend; }
94  // test if end of file has been reached
95  Bool eof() const { return Bool(m_rec_type == FITS::EndOfFile); }
96  // the FITS record type
97  FITS::FitsRecType rectype() const { return m_rec_type; }
98  // Header Data Unit type (e.g.
99  FITS::HDUType hdutype() const { return m_hdu_type; }
101  // return the datasize of the current HDU. This excludes
102  // the trailing end of the blocked data portion.
103  OFF_T datasize() const { return m_data_size; }
104  // data characteristics
105  Int itemsize() const { return m_item_size; }
106  // for input, size of remaining data
107  // for output, size of data written
108  OFF_T currsize() const { return m_curr_size; }
109  // get FitsKeyCardTranslator
111  // get the fitsfile pointer
112  fitsfile *getfptr() const { return m_fptr; }
113 
114  // get the size of the last skipped HDU
115  OFF_T getskipsize() const {return m_skipHDU_size;}
116 
117 
118  protected:
120 
121  fitsfile *m_fptr;
122  const int m_recsize;
123  Bool m_valid_fits; // True if SIMPLE == T
124  Bool m_extend; // True if EXTEND == T
125  Bool m_isaprimary; // True if there is a primary HDU
126  Bool m_header_done; // True if header has been processed
128  FITS::HDUType m_hdu_type; // always set
129 
130  FITSErrorHandler m_errfn; // error handler function
134 
135  char *m_curr; // pointer to current record
136  int m_bytepos; // current byte position within record
137  Int m_item_size; // data characteristics
139  //uInt m_data_size;
140  OFF_T m_data_size;
141  // for input, size of remaining data
142  // for output, size of data written
143  //uInt m_curr_size;
144  OFF_T m_curr_size;
145 
146  // for size of the last HDU skipped
148 
149  // set error message that belongs to one of the enumerated types
150  virtual void errmsg(FitsErrs, const char *) = 0;
151 
152 };
153 
154 //<summary> fixed-length sequential blocked FITS input </summary>
155 
156 class FitsInput : public FitsIO {
158  friend OFF_T HeaderDataUnit::read_all_data(char *);
159  friend int HeaderDataUnit::read_data(char *, Int);
160  friend int HeaderDataUnit::skip(uInt);
161  friend int HeaderDataUnit::skip();
162 
163  public:
164  //<group>
165  FitsInput(const char *, const FITS::FitsDevice &, int = 10,
168  ~FitsInput();
169  //</group>
170 
171  int skip_hdu();
172 
173  // skip all remaining data
174  void skip_all(FITS::HDUType);
175 
176  //int skip_hdu2();
177  // read special or unrecognizable records
178  char *read_sp();
179 
180  // get hdu header image cards as strings. By default the strings will be of
181  // variable length. You can optionally ask for them to be length 80 (padded
182  // with spaces).
184 
185  // number of physical blocks read/written
186  int blockno() const {return m_fin.blockno();}
187 
188  // number of logical records read/written
189  int recno() const {return m_fin.recno();}
190  BlockInput & getfin(){ return m_fin; } // for test use only
191 
192  // the number of hdu in this fits file
193  int getnumhdu() const {return m_thdunum;}
194 
195  private:
197  BlockInput &make_input(const char *, const FITS::FitsDevice &, int,
199 
200  // flag used for read control in errors
202  // total number of hdu in this fits file
203  int m_thdunum;
204 
205  virtual void errmsg(FitsErrs, const char *);
206  void init();
207  void read_header_rec();
210 
211  //# check if this comes out ok in cxx2html
212  // Special interface to class HeaderDataUnit
213  //<group>
214  // special way to process header
216  // read all data into a given address - all responsibility is given
217  // to the user
218  OFF_T read_all(FITS::HDUType, char *);
219  // read N bytes into address
220  int read(FITS::HDUType, char *, int );
221  // skip N bytes
222  int skip(FITS::HDUType, OFF_T);
223  //</group>
224 };
225 
226 //<summary> fixed-length sequential blocked FITS output </summary>
227 
228 class FitsOutput : public FitsIO {
230  friend int HeaderDataUnit::write_all_data(FitsOutput &, char *);
231  friend int HeaderDataUnit::write_data(FitsOutput &, char *, Int);
232 
233  public:
234  //<group>
235  FitsOutput(const char *, const FITS::FitsDevice &, int = 10,
238  ~FitsOutput();
239  //</group>
240  // used by PrimaryArray, BinaryTabelExtention etc to work with the constructor without keyword list.
241  void set_data_info( FitsKeywordList &kwl, FITS::HDUType t, FITS::ValueType dt, OFF_T ds, Int is);
242  // write a special record. For this the record type must also
243  // be to set to FITS::SpecialRecord
244  int write_sp(char *rec);
245  // check if the current hdu is done. It was private.
246  int hdu_complete() {
247  return (m_rec_type == FITS::HDURecord && m_data_size == 0);
248  }
249  BlockOutput & getfout(){ return m_fout; }
250  void setfptr( fitsfile* ffp );
252 
253  private:
256  BlockOutput &make_output(const char *, const FITS::FitsDevice &, int,
258 
259  virtual void errmsg(FitsErrs, const char *);
260 
261  int hdu_inprogress() {
263  }
264 
265  // Special interface to class HeaderDataUnit
266  //<group>
268  // write all data from address
269  int write_all(FITS::HDUType, char *, char);
270  // write N bytes from address
271  int write(FITS::HDUType, char *, Int, char);
272  //</group>
273 };
274 
275 //<summary> FITS input from disk </summary>
276 
277 class FitsDiskInput : public BlockInput {
278  public:
279  FitsDiskInput(const char *, int, int = 1,
281  ~FitsDiskInput();
282  // implements skip in terms of lseek
283  char *skip(int);
284 };
285 
286 //<summary> FITS output to disk </summary>
287 
288 class FitsDiskOutput : public BlockOutput {
289  public:
290  FitsDiskOutput(const char *, int, int = 1,
292  ~FitsDiskOutput();
293 };
294 
295 //<summary> FITS input from standard input </summary>
296 
297 class FitsStdInput : public BlockInput {
298  public:
299  FitsStdInput(int,
301  ~FitsStdInput();
302 };
303 
304 //<summary> FITS output to standard output </summary>
305 
306 class FitsStdOutput : public BlockOutput {
307  public:
308  FitsStdOutput(int,
310  ~FitsStdOutput();
311 };
312 
313 //<summary> FITS input from 9-track tape </summary>
314 
315 class FitsTape9Input : public BlockInput {
316  public:
317  FitsTape9Input(const char *, int, int = 10,
319  ~FitsTape9Input();
320 };
321 
322 //<summary> FITS output to 9-track tape </summary>
323 
324 class FitsTape9Output : public BlockOutput {
325  public:
326  FitsTape9Output(const char *, int, int = 10,
329 };
330 
331 
332 } //# NAMESPACE CASACORE - END
333 
334 # endif
A 1-D Specialization of the Array class.
Definition: ArrayFwd.h:9
Int itemsize() const
data characteristics
Definition: fitsio.h:105
FITS input from standard input.
Definition: fitsio.h:297
int Int
Definition: aipstype.h:50
BlockOutput & make_output(const char *, const FITS::FitsDevice &, int, FITSErrorHandler errhandler=FITSError::defaultHandler)
OFF_T datasize() const
return the datasize of the current HDU.
Definition: fitsio.h:103
virtual void errmsg(FitsErrs, const char *)
set error message that belongs to one of the enumerated types
Bool isafits() const
is it a valid fits file (SIMPLE==T).
Definition: fitsio.h:91
FITS::ValueType m_data_type
Definition: fitsio.h:138
FITS::HDUType hdutype() const
Header Data Unit type (e.g.
Definition: fitsio.h:99
int read(FITS::HDUType, char *, int)
read N bytes into address
int skip(FITS::HDUType, OFF_T)
skip N bytes
int m_thdunum
total number of hdu in this fits file
Definition: fitsio.h:203
Bool required_keys_only()
Definition: fitsio.h:251
virtual void errmsg(FitsErrs, const char *)
set error message that belongs to one of the enumerated types
int write(FITS::HDUType, char *, Int, char)
write N bytes from address
void setfptr(fitsfile *ffp)
bool current_hdu_type(FITS::HDUType &)
FitsOutput(const char *, const FITS::FitsDevice &, int=10, FITSErrorHandler errhandler=FITSError::defaultHandler)
FitsKeyCardTranslator & getkc()
get FitsKeyCardTranslator
Definition: fitsio.h:110
int blockno() const
number of physical blocks read/written
Definition: fitsio.h:186
FITS::ValueType datatype() const
Definition: fitsio.h:100
int write_sp(char *rec)
write a special record.
FitsStdOutput(int, FITSErrorHandler errhandler=FITSError::defaultHandler)
FitsStdInput(int, FITSErrorHandler errhandler=FITSError::defaultHandler)
Vector< String > kwlist_str(Bool length80=False)
get hdu header image cards as strings.
char * m_curr
Definition: fitsio.h:135
bool get_data_type(FITS::ValueType &)
FITS::FitsRecType rectype() const
the FITS record type
Definition: fitsio.h:97
int write_all_data(FitsOutput &, char *)
FitsInput(const char *, const FITS::FitsDevice &, int=10, FITSErrorHandler errhandler=FITSError::defaultHandler)
int get_hdr(FITS::HDUType, FitsKeywordList &)
FitsErrs
error return code.
Definition: fitsio.h:78
int getnumhdu() const
the number of hdu in this fits file
Definition: fitsio.h:193
void(* FITSErrorHandler)(const char *errMessage, FITSError::ErrorLevel severity)
Define a typedef for the handler function signature for convenience.
Definition: FITSError.h:111
HDUType
Types of FITS Header-Data Units.
Definition: fits.h:263
Bool eof() const
test if end of file has been reached
Definition: fitsio.h:95
OFF_T currsize() const
for input, size of remaining data for output, size of data written
Definition: fitsio.h:108
FITS output to 9-track tape.
Definition: fitsio.h:324
FITS output to disk.
Definition: fitsio.h:288
ValueType
FITS I/O Error message types.
Definition: fits.h:167
fixed-length blocked sequential output base class
Definition: blockio.h:175
int hdu_complete()
check if the current hdu is done.
Definition: fitsio.h:246
FITS input from 9-track tape.
Definition: fitsio.h:315
char * skip(int)
implements skip in terms of lseek
virtual void errmsg(FitsErrs, const char *)=0
set error message that belongs to one of the enumerated types
FitsKeyCardTranslator m_kc
Definition: fitsio.h:132
Bool m_header_done
Definition: fitsio.h:126
BlockInput & make_input(const char *, const FITS::FitsDevice &, int, FITSErrorHandler errhandler=FITSError::defaultHandler)
OFF_T read_all_data(char *)
int err() const
Definition: fitsio.h:82
fitsfile * getfptr() const
get the fitsfile pointer
Definition: fitsio.h:112
fitsfile * m_fptr
Definition: fitsio.h:121
Bool isextend() const
see if there may be FITS extensions present (EXTENT==T)
Definition: fitsio.h:93
virtual ~FitsIO()
FitsKeywordList m_kw
Definition: fitsio.h:133
Bool m_required_keys_only
Definition: fitsio.h:255
int hdu_inprogress()
Definition: fitsio.h:261
FitsDevice
Supported FITS Physical Devices.
Definition: fits.h:258
void skip_all(FITS::HDUType)
skip all remaining data
OFF_T m_curr_size
for input, size of remaining data for output, size of data written uInt m_curr_size; ...
Definition: fitsio.h:144
void set_data_info(FitsKeywordList &kwl, FITS::HDUType t, FITS::ValueType dt, OFF_T ds, Int is)
used by PrimaryArray, BinaryTabelExtention etc to work with the constructor without keyword list...
BlockInput & m_fin
Definition: fitsio.h:196
Bool m_got_rec
flag used for read control in errors
Definition: fitsio.h:201
Bool m_isaprimary
Definition: fitsio.h:125
FitsIO(FITSErrorHandler)
FITSErrorHandler m_errfn
Definition: fitsio.h:130
int process_header(FITS::HDUType, FitsKeywordList &)
Special interface to class HeaderDataUnit.
FitsTape9Output(const char *, int, int=10, FITSErrorHandler errhandler=FITSError::defaultHandler)
OFF_T m_data_size
uInt m_data_size;
Definition: fitsio.h:140
int fitsrecsize() const
record size, in bytes, of a FITS block.
Definition: fitsio.h:87
FITS::FitsRecType m_rec_type
Definition: fitsio.h:127
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
FitsRecType
Types of FITS Records.
Definition: fits.h:252
BlockOutput & m_fout
Definition: fitsio.h:254
FITS input from disk.
Definition: fitsio.h:277
int write_all(FITS::HDUType, char *, char)
write all data from address
const Bool False
Definition: aipstype.h:44
const int m_recsize
Definition: fitsio.h:122
FitsTape9Input(const char *, int, int=10, FITSErrorHandler errhandler=FITSError::defaultHandler)
Bool m_valid_fits
Definition: fitsio.h:123
int write_hdr(FitsKeywordList &, FITS::HDUType, FITS::ValueType, OFF_T, Int)
Special interface to class HeaderDataUnit.
FitsDiskOutput(const char *, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)
OFF_T m_skipHDU_size
for size of the last HDU skipped
Definition: fitsio.h:147
fixed-length blocked sequential input base class
Definition: blockio.h:139
int recno() const
number of logical records read/written
Definition: blockio.h:84
BlockInput & getfin()
Definition: fitsio.h:190
fixed-length sequential blocked FITS output
Definition: fitsio.h:228
fixed-length sequential blocked FITS input
Definition: fitsio.h:156
OFF_T read_all(FITS::HDUType, char *)
read all data into a given address - all responsibility is given to the user
FITS output to standard output.
Definition: fitsio.h:306
linked list of FITS keywords
Definition: fits.h:737
int read_data(char *, Int)
FITS::HDUType m_hdu_type
Definition: fitsio.h:128
FitsErrs m_err_status
Definition: fitsio.h:131
char * read_sp()
int skip_hdu2(); read special or unrecognizable records
translator between Keyword lists and fixed FITS cars
Definition: fits.h:985
int write_data(FitsOutput &, char *, Int)
OFF_T getskipsize() const
get the size of the last skipped HDU
Definition: fitsio.h:115
BlockOutput & getfout()
Definition: fitsio.h:249
int recno() const
number of logical records read/written
Definition: fitsio.h:189
int write_hdr(FitsOutput &)
write the current header
unsigned int uInt
Definition: aipstype.h:51
int blockno() const
number of physical blocks read/written
Definition: blockio.h:69
static void defaultHandler(const char *errMessage, ErrorLevel severity)
The default error handler.
sequential FITS I/O
Definition: fitsio.h:62
FitsDiskInput(const char *, int, int=1, FITSErrorHandler errhandler=FITSError::defaultHandler)