// @(#)root/graf2d:$Id$
// Author: Claudi Martinez, July 19th 2010

/*************************************************************************
 * Copyright (C) 1995-2010, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TFITS
#define ROOT_TFITS

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TFITS                                                                //
//                                                                      //
// Interface to FITS astronomical files.                                //
// Please, see TFITS.cxx for info about implementation                  //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif
#ifndef ROOT_TMatrixDfwd
#include "TMatrixDfwd.h"
#endif
#ifndef ROOT_TVectorDfwd
#include "TVectorDfwd.h"
#endif

class TArrayI;
class TArrayD;
class TH1;
class TImage;
class TImagePalette;
class TObjArray;

class TFITSHDU : public TNamed {

private:
   void _release_resources();
   void _initialize_me();

public:
   enum EHDUTypes {         // HDU types
      kImageHDU,
      kTableHDU
   };

   enum EColumnTypes {     // Column data types
      kRealNumber,
      kString,
      kRealVector
   };

   struct HDURecord {       // FITS HDU record
      TString fKeyword;
      TString fValue;
      TString fComment;
   };

   struct Column {               //Information of a table column
      TString            fName;      // Column's name
      enum EColumnTypes  fType;      // Column's data type
      Int_t              fDim;       // When cells contain real number vectors, this field indicates
                                     // the dimension of this vector (number of components), being 1 for scalars.
   };

   union Cell {                 //Table cell contents
      Char_t       *fString;
      Double_t      fRealNumber;
      Double_t     *fRealVector;
   };

protected:
   TString             fFilePath;         // Path to HDU's file including filter
   TString             fBaseFilePath;     // Path to HDU's file excluding filter
   struct HDURecord   *fRecords;          // HDU metadata records
   Int_t               fNRecords;         // Number of records
   enum EHDUTypes      fType;             // HDU type
   TString             fExtensionName;    // Extension Name
   Int_t               fNumber;           // HDU number (1=PRIMARY)
   TArrayI            *fSizes;            // Image sizes in each dimension (when fType == kImageHDU)
   TArrayD            *fPixels;           // Image pixels (when fType == kImageHDU)
   struct Column      *fColumnsInfo;      // Information about columns (when fType == kTableHDU)
   Int_t               fNColumns;         // Number of columns (when fType == kTableHDU)
   Int_t               fNRows;            // Number of rows (when fType == kTableHDU)
   union  Cell        *fCells;            // Table cells (when fType == kTableHDU). Cells are ordered in the following way:
                                          // fCells[0..fNRows-1] -> cells of column 0
                                          // fCells[fNRows..2*fNRows-1] -> cells of column 1
                                          // fCells[2*fNRows..3*fNRows-1] -> cells of column 2
                                          // fCells[(fNColumns-1)*fNRows..fNColumns*fNRows-1] -> cells of column fNColumns-1


   Bool_t            LoadHDU(TString& filepath_filter);
   static void       CleanFilePath(const char *filepath_with_filter, TString &dst);
   void              PrintHDUMetadata(const Option_t *opt="") const;
   void              PrintFileMetadata(const Option_t *opt="") const;
   void              PrintColumnInfo(const Option_t *) const;
   void              PrintFullTable(const Option_t *) const;

public:
   TFITSHDU(const char *filepath_with_filter);
   TFITSHDU(const char *filepath, Int_t extension_number);
   TFITSHDU(const char *filepath, const char *extension_name);
   ~TFITSHDU();

   //Metadata access methods
   Int_t              GetRecordNumber() const { return fNRecords; }
   struct HDURecord  *GetRecord(const char *keyword);
   TString&           GetKeywordValue(const char *keyword);
   void               Print(const Option_t *opt="") const;

   //Image readers
   TH1               *ReadAsHistogram();
   TImage            *ReadAsImage(Int_t layer = 0, TImagePalette *pal = 0);
   TMatrixD          *ReadAsMatrix(Int_t layer = 0, Option_t *opt="");
   TVectorD          *GetArrayRow(UInt_t row);
   TVectorD          *GetArrayColumn(UInt_t col);

   //Table readers
   Int_t              GetTabNColumns() const { return fNColumns; }
   Int_t              GetTabNRows()    const { return fNRows; }
   Int_t              GetColumnNumber(const char *colname);
   const TString&     GetColumnName(Int_t colnum);
   TObjArray         *GetTabStringColumn(Int_t colnum);
   TObjArray         *GetTabStringColumn(const char *colname);
   TVectorD          *GetTabRealVectorColumn(Int_t colnum);
   TVectorD          *GetTabRealVectorColumn(const char *colname);
   TVectorD          *GetTabRealVectorCell(Int_t rownum, Int_t colnum);
   TVectorD          *GetTabRealVectorCell(Int_t rownum, const char *colname);
   TObjArray         *GetTabRealVectorCells(Int_t colnum);
   TObjArray         *GetTabRealVectorCells(const char *colname);

   //Misc
   void               Draw(Option_t *opt="");
   Bool_t             Change(const char *filter);
   Bool_t             Change(Int_t extension_number);


   ClassDef(TFITSHDU,0)  // Class interfacing FITS HDUs
};


#endif
 TFITS.h:1
 TFITS.h:2
 TFITS.h:3
 TFITS.h:4
 TFITS.h:5
 TFITS.h:6
 TFITS.h:7
 TFITS.h:8
 TFITS.h:9
 TFITS.h:10
 TFITS.h:11
 TFITS.h:12
 TFITS.h:13
 TFITS.h:14
 TFITS.h:15
 TFITS.h:16
 TFITS.h:17
 TFITS.h:18
 TFITS.h:19
 TFITS.h:20
 TFITS.h:21
 TFITS.h:22
 TFITS.h:23
 TFITS.h:24
 TFITS.h:25
 TFITS.h:26
 TFITS.h:27
 TFITS.h:28
 TFITS.h:29
 TFITS.h:30
 TFITS.h:31
 TFITS.h:32
 TFITS.h:33
 TFITS.h:34
 TFITS.h:35
 TFITS.h:36
 TFITS.h:37
 TFITS.h:38
 TFITS.h:39
 TFITS.h:40
 TFITS.h:41
 TFITS.h:42
 TFITS.h:43
 TFITS.h:44
 TFITS.h:45
 TFITS.h:46
 TFITS.h:47
 TFITS.h:48
 TFITS.h:49
 TFITS.h:50
 TFITS.h:51
 TFITS.h:52
 TFITS.h:53
 TFITS.h:54
 TFITS.h:55
 TFITS.h:56
 TFITS.h:57
 TFITS.h:58
 TFITS.h:59
 TFITS.h:60
 TFITS.h:61
 TFITS.h:62
 TFITS.h:63
 TFITS.h:64
 TFITS.h:65
 TFITS.h:66
 TFITS.h:67
 TFITS.h:68
 TFITS.h:69
 TFITS.h:70
 TFITS.h:71
 TFITS.h:72
 TFITS.h:73
 TFITS.h:74
 TFITS.h:75
 TFITS.h:76
 TFITS.h:77
 TFITS.h:78
 TFITS.h:79
 TFITS.h:80
 TFITS.h:81
 TFITS.h:82
 TFITS.h:83
 TFITS.h:84
 TFITS.h:85
 TFITS.h:86
 TFITS.h:87
 TFITS.h:88
 TFITS.h:89
 TFITS.h:90
 TFITS.h:91
 TFITS.h:92
 TFITS.h:93
 TFITS.h:94
 TFITS.h:95
 TFITS.h:96
 TFITS.h:97
 TFITS.h:98
 TFITS.h:99
 TFITS.h:100
 TFITS.h:101
 TFITS.h:102
 TFITS.h:103
 TFITS.h:104
 TFITS.h:105
 TFITS.h:106
 TFITS.h:107
 TFITS.h:108
 TFITS.h:109
 TFITS.h:110
 TFITS.h:111
 TFITS.h:112
 TFITS.h:113
 TFITS.h:114
 TFITS.h:115
 TFITS.h:116
 TFITS.h:117
 TFITS.h:118
 TFITS.h:119
 TFITS.h:120
 TFITS.h:121
 TFITS.h:122
 TFITS.h:123
 TFITS.h:124
 TFITS.h:125
 TFITS.h:126
 TFITS.h:127
 TFITS.h:128
 TFITS.h:129
 TFITS.h:130
 TFITS.h:131
 TFITS.h:132
 TFITS.h:133
 TFITS.h:134
 TFITS.h:135
 TFITS.h:136
 TFITS.h:137
 TFITS.h:138
 TFITS.h:139
 TFITS.h:140
 TFITS.h:141
 TFITS.h:142
 TFITS.h:143
 TFITS.h:144
 TFITS.h:145
 TFITS.h:146
 TFITS.h:147