fitsio.h

Go to the documentation of this file.
00001 //# fitsio.h:
00002 //# Copyright (C) 1993,1994,1995,1996,1999,2001,2003
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 //# $Id$
00027 
00028 #ifndef FITS_FITSIO_H
00029 #define FITS_FITSIO_H
00030 
00031 #include <casacore/casa/aips.h>
00032 # include <casacore/fits/FITS/fits.h>
00033 # include <casacore/fits/FITS/blockio.h>
00034 # include <casacore/fits/FITS/hdu.h>
00035 //# include <casacore/casa/stdvector.h>
00036 # include <casacore/casa/Arrays/Vector.h>
00037 
00038 namespace casacore { //# NAMESPACE CASACORE - BEGIN
00039 
00040 //<summary> sequential FITS I/O </summary>
00041 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00042 // </reviewed>
00043 //<synopsis>
00044 // FitsIO is a base class that handles all the sequential blocked
00045 // FITS I/O. Special derived classes do the input and output.
00046 // No interpretation of the data is attempted here, there are 
00047 // special FITS classes that handle syntax and interpretation.
00048 //</synopsis>
00049 //<example>
00050 //<srcblock>
00051 // FitsInput fin("myfile.fits",FITS::Disk);     // open disk file for FITS input
00052 // if (fin.err() == FitsIO::IOERR) {            // check if open succeeded
00053 //     cout << "Could not open FITS input\n";
00054 //     exit(0);
00055 // }
00056 // if (fin.rectype() == FITS::HDURecord &&      // test for primary array
00057 //     fin.hdutype() == FITS::PrimaryArrayHDU) {
00058 // }
00059 //</srcblock>
00060 //</example>
00061 
00062 class FitsIO {
00063     public:
00064         virtual ~FitsIO();
00065 
00066         // error return code. Should be one of an 
00067         // enumerated type:
00068         //
00069         //#  until cxx2html can handle enum() we duplicate it here
00070         //
00071         //<srcblock>
00072         //  enum FitsErrs { OK, IOERR, MISSKEY, BADBEGIN, EMPTYFILE,
00073         //      NOPRIMARY, BADOPER, BADEOF, MEMERR, BADBITPIX, NOAXISN,
00074         //      NOPCOUNT, NOGCOUNT, BADPCOUNT, BADGCOUNT, NOGROUPS,
00075         //      BADNAXIS, BADPRIMARY, BADSIZE, HDUERR };
00076         //</srcblock>
00077         //<group>
00078         enum FitsErrs { OK, IOERR, MISSKEY, BADBEGIN, EMPTYFILE,
00079                 NOPRIMARY, BADOPER, BADEOF, MEMERR, BADBITPIX, NOAXISN,
00080                 NOPCOUNT, NOGCOUNT, BADPCOUNT, BADGCOUNT, NOGROUPS,
00081                 BADNAXIS, BADPRIMARY, BADSIZE, HDUERR };
00082         int err() const                         { return m_err_status; }
00083         //</group>
00084         //
00085         // record size, in bytes, of a FITS block. 
00086         // Normally set at 2880, unless some form of blocking was used.
00087         int fitsrecsize() const                 { return m_recsize; }
00088         // is it a valid fits file (SIMPLE==T). If not, the only
00089         // safest operation is to skip the data portion of the
00090         // current HeaderDataUnit
00091         Bool isafits() const                    { return m_valid_fits; }
00092         // see if there may be FITS extensions present (EXTENT==T)
00093         Bool isextend() const                   { return m_extend; }
00094         // test if end of file has been reached
00095         Bool eof() const         { return Bool(m_rec_type == FITS::EndOfFile); }
00096         // the FITS record type
00097         FITS::FitsRecType rectype() const       { return m_rec_type;  }
00098         // Header Data Unit type (e.g. 
00099         FITS::HDUType hdutype() const           { return m_hdu_type; }
00100         FITS::ValueType datatype() const        { return m_data_type; }
00101         // return the datasize of the current HDU. This excludes
00102         // the trailing end of the blocked data portion.
00103         OFF_T datasize() const                  { return m_data_size; }
00104         // data characteristics
00105         Int itemsize() const                    { return m_item_size; }
00106         // for input, size of remaining data
00107         // for output, size of data written
00108         OFF_T currsize() const                  { return m_curr_size; }
00109         // get FitsKeyCardTranslator
00110         FitsKeyCardTranslator& getkc(){  return m_kc;  }
00111         // get the fitsfile pointer
00112         fitsfile *getfptr() const { return m_fptr; }
00113 
00114         // get the size of the last skipped HDU
00115         OFF_T getskipsize() const {return m_skipHDU_size;}
00116 
00117 
00118    protected:
00119         FitsIO(FITSErrorHandler);
00120 
00121         fitsfile *m_fptr;
00122         const int m_recsize;
00123         Bool m_valid_fits;              // True if SIMPLE == T
00124         Bool m_extend;                     // True if EXTEND == T
00125         Bool m_isaprimary;              // True if there is a primary HDU
00126    Bool m_header_done;          // True if header has been processed
00127         FITS::FitsRecType m_rec_type;   // always set
00128         FITS::HDUType m_hdu_type;               // always set
00129 
00130         FITSErrorHandler m_errfn;               // error handler function
00131         FitsErrs m_err_status;
00132         FitsKeyCardTranslator m_kc;
00133         FitsKeywordList m_kw;
00134 
00135         char *m_curr;                   // pointer to current record
00136         int m_bytepos;                  // current byte position within record
00137         Int m_item_size;                        // data characteristics
00138         FITS::ValueType m_data_type;
00139         //uInt m_data_size;
00140         OFF_T m_data_size;              
00141         // for input, size of remaining data
00142         // for output, size of data written
00143         //uInt m_curr_size;
00144         OFF_T m_curr_size;                      
00145 
00146         // for size of the last HDU skipped
00147         OFF_T m_skipHDU_size;
00148 
00149         // set error message that belongs to one of the enumerated types
00150         virtual void errmsg(FitsErrs, const char *) = 0;
00151                                         
00152 };
00153 
00154 //<summary> fixed-length sequential blocked FITS input </summary>
00155 
00156 class FitsInput : public FitsIO {
00157         friend int HeaderDataUnit::get_hdr(FITS::HDUType, FitsKeywordList &);
00158         friend OFF_T HeaderDataUnit::read_all_data(char *);
00159         friend int HeaderDataUnit::read_data(char *, Int);
00160         friend int HeaderDataUnit::skip(uInt);
00161         friend int HeaderDataUnit::skip();
00162 
00163     public:
00164         //<group>
00165         FitsInput(const char *, const FITS::FitsDevice &, int = 10, 
00166                   FITSErrorHandler errhandler = FITSError::defaultHandler);
00167         FitsInput(FITSErrorHandler errhandler = FITSError::defaultHandler); 
00168         ~FitsInput();
00169         //</group>
00170 
00171         int skip_hdu();
00172 
00173         // skip all remaining data
00174         void skip_all(FITS::HDUType);
00175 
00176         //int skip_hdu2();
00177         // read special or unrecognizable records
00178         char *read_sp();
00179 
00180    // get hdu header image cards as strings. By default the strings will be of
00181    // variable length. You can optionally ask for them to be length 80 (padded
00182    // with spaces).
00183         Vector<String> kwlist_str(Bool length80=False);
00184 
00185    //  number of physical blocks read/written
00186    int blockno() const {return m_fin.blockno();}
00187 
00188    //  number of logical records read/written
00189    int recno() const {return m_fin.recno();}
00190    BlockInput & getfin(){ return m_fin; } // for test use only
00191 
00192    // the number of hdu in this fits file
00193    int getnumhdu() const {return m_thdunum;}
00194 
00195     private:
00196         BlockInput &m_fin;
00197         BlockInput &make_input(const char *, const FITS::FitsDevice &, int, 
00198                                FITSErrorHandler errhandler = FITSError::defaultHandler);
00199 
00200         // flag used for read control in errors
00201         Bool m_got_rec;
00202         // total number of hdu in this fits file
00203         int m_thdunum;          
00204 
00205         virtual void errmsg(FitsErrs, const char *);
00206         void init();
00207         void read_header_rec();
00208         bool current_hdu_type( FITS::HDUType &);
00209         bool get_data_type( FITS::ValueType &);
00210 
00211         //# check if this comes out ok in cxx2html
00212         // Special interface to class HeaderDataUnit
00213         //<group>
00214         // special way to process header
00215         int process_header(FITS::HDUType, FitsKeywordList &);
00216         // read all data into a given address - all responsibility is given
00217         // to the user
00218         OFF_T read_all(FITS::HDUType, char *);
00219         // read N bytes into address
00220         int read(FITS::HDUType, char *, int );
00221         // skip N bytes
00222         int skip(FITS::HDUType, OFF_T);
00223         //</group>
00224 };
00225 
00226 //<summary> fixed-length sequential blocked FITS output </summary>
00227 
00228 class FitsOutput : public FitsIO {
00229         friend int HeaderDataUnit::write_hdr(FitsOutput &);
00230         friend int HeaderDataUnit::write_all_data(FitsOutput &, char *);
00231         friend int HeaderDataUnit::write_data(FitsOutput &, char *, Int);
00232 
00233     public:
00234         //<group>
00235         FitsOutput(const char *, const FITS::FitsDevice &, int = 10, 
00236                    FITSErrorHandler errhandler = FITSError::defaultHandler);
00237         FitsOutput(FITSErrorHandler errhandler = FITSError::defaultHandler);
00238         ~FitsOutput();
00239         //</group>
00240    // used by PrimaryArray, BinaryTabelExtention etc to work with the constructor without keyword list.
00241         void set_data_info( FitsKeywordList &kwl, FITS::HDUType t, FITS::ValueType dt, OFF_T ds, Int is);
00242         // write a special record. For this the record type must also
00243         // be to set to FITS::SpecialRecord
00244         int write_sp(char *rec);
00245         // check if the current hdu is done. It was private.
00246         int hdu_complete() { 
00247             return (m_rec_type == FITS::HDURecord && m_data_size == 0);
00248    }
00249    BlockOutput & getfout(){ return m_fout; }
00250         void setfptr( fitsfile* ffp ); 
00251         Bool required_keys_only(){ return m_required_keys_only; }
00252 
00253     private:
00254         BlockOutput &m_fout;
00255         Bool m_required_keys_only;
00256         BlockOutput &make_output(const char *, const FITS::FitsDevice &, int, 
00257                                  FITSErrorHandler errhandler = FITSError::defaultHandler);
00258 
00259         virtual void errmsg(FitsErrs, const char *);
00260 
00261         int hdu_inprogress() { 
00262             return (m_rec_type == FITS::HDURecord && m_data_size > 0 && m_curr_size < m_data_size);
00263          }
00264 
00265         // Special interface to class HeaderDataUnit
00266         //<group>
00267         int write_hdr(FitsKeywordList &, FITS::HDUType, FITS::ValueType, OFF_T, Int);
00268         // write all data from address
00269         int write_all(FITS::HDUType, char *, char);
00270         // write N bytes from address
00271         int write(FITS::HDUType, char *, Int, char); 
00272         //</group>
00273 };
00274 
00275 //<summary> FITS input from disk </summary>
00276 
00277 class FitsDiskInput : public BlockInput {
00278     public:
00279         FitsDiskInput(const char *, int, int = 1, 
00280                       FITSErrorHandler errhandler = FITSError::defaultHandler);
00281         ~FitsDiskInput();
00282         // implements skip in terms of lseek
00283         char *skip(int); 
00284 };
00285 
00286 //<summary> FITS output to disk </summary>
00287 
00288 class FitsDiskOutput : public BlockOutput {
00289     public:
00290         FitsDiskOutput(const char *, int, int = 1, 
00291                        FITSErrorHandler errhandler = FITSError::defaultHandler);
00292         ~FitsDiskOutput();
00293 };
00294 
00295 //<summary> FITS input from standard input </summary>
00296 
00297 class FitsStdInput : public BlockInput {
00298     public:
00299         FitsStdInput(int, 
00300                      FITSErrorHandler errhandler = FITSError::defaultHandler);
00301         ~FitsStdInput();
00302 };
00303 
00304 //<summary> FITS output to standard output </summary>
00305 
00306 class FitsStdOutput : public BlockOutput {
00307     public:
00308         FitsStdOutput(int, 
00309                       FITSErrorHandler errhandler = FITSError::defaultHandler);
00310         ~FitsStdOutput();
00311 };
00312 
00313 //<summary> FITS input from 9-track tape </summary>
00314 
00315 class FitsTape9Input : public BlockInput {
00316     public:
00317         FitsTape9Input(const char *, int, int = 10, 
00318                        FITSErrorHandler errhandler = FITSError::defaultHandler);
00319         ~FitsTape9Input();
00320 };
00321 
00322 //<summary> FITS output to 9-track tape </summary>
00323 
00324 class FitsTape9Output : public BlockOutput {
00325     public:
00326         FitsTape9Output(const char *, int, int = 10, 
00327                         FITSErrorHandler errhandler = FITSError::defaultHandler);
00328         ~FitsTape9Output();
00329 };
00330 
00331 
00332 } //# NAMESPACE CASACORE - END
00333 
00334 # endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1