source: trunk/FACT++/src/FitsFile.h@ 14797

Last change on this file since 14797 was 12536, checked in by lyard, 13 years ago
added flushing of nightly files after each row
File size: 2.8 KB
Line 
1#ifndef FACT_FitsFile
2#define FACT_FitsFile
3
4#include <CCfits/CCfits>
5
6#include "MessageImp.h"
7#include "Time.h"
8
9class FitsFile : public MessageImp
10{
11public:
12 MessageImp &fMsg;
13
14 std::vector<std::string> fColNames;
15 std::vector<std::string> fColTypes;
16 std::vector<std::string> fColUnits;
17
18 CCfits::FITS* fFile; /// The pointer to the CCfits FITS file
19 CCfits::Table* fTable; /// The pointer to the CCfits binary table
20
21 size_t fNumRows; ///the number of rows that have been written already to the FITS file.
22 size_t fCursor;
23
24 bool fIsOwner;
25
26 int Write(const Time &time, const std::string &txt, int qos)
27 {
28 return fMsg.Write(time, txt, qos);
29 }
30
31public:
32 FitsFile(MessageImp &imp) :
33 fMsg(imp), fFile(0), fTable(0), fNumRows(0), fCursor(0)
34 {
35 }
36 ~FitsFile() { Close(); }
37
38 bool WriteDefaultKeys(const string &prgname, float version=1.0);
39
40 void AddColumn(char type, const string &name, int numElems=1, const string &unit="");
41 void AddColumn(const string &name, const string &format, const string &unit="");
42 void AddColumn(char type, const string &name, const string &unit)
43 {
44 AddColumn(type, name, 1, unit);
45 }
46
47 void ResetColumns();
48
49 bool OpenFile(const string &filename, bool allow_open=false);
50 bool SetFile(CCfits::FITS *file=0);
51 bool OpenTable(const string &tablename);
52 bool OpenNewTable(const string &tableName, int maxtry=1);
53
54 template <typename T>
55 void WriteKey(const string &name, const T &value, const string &comment)
56 {
57 if (fTable)
58 fTable->addKey(name, value, comment);
59 }
60
61 template <typename T>
62 bool WriteKeyNT(const string &name, const T &value, const string &comment)
63 {
64 if (!fTable)
65 return false;
66
67 try
68 {
69 fTable->addKey(name, value, comment);
70 }
71 catch (CCfits::FitsException e)
72 {
73 Error("CCfits::Table::addKey failed for '"+name+"' in '"+fFile->name()+'/'+fTable->name()+"': "+e.message());
74 return false;
75 }
76
77 return true;
78 }
79
80 bool AddRow();
81 bool WriteData(size_t &start, const void *ptr, size_t size);
82 bool WriteData(const void *ptr, size_t size)
83 {
84 return WriteData(fCursor, ptr, size);
85 }
86
87 template<typename T>
88 bool WriteData(const std::vector<T> &vec)
89 {
90 return WriteData(fCursor, vec.data(), vec.size()*sizeof(T));
91 }
92
93 void Close();
94
95 void Flush();
96
97 bool IsOpen() const { return fFile && fTable; }
98
99 const std::vector<std::string> &GetColumnTypes() const { return fColTypes; }
100 string GetName() const { return fFile ? fFile->name() : "<no file open>"; }
101 bool IsOwner() const { return fIsOwner; }
102
103 size_t GetDataSize() const;
104
105 size_t GetNumRows() const { return fNumRows; }
106
107};
108
109#endif
Note: See TracBrowser for help on using the repository browser.