1 | // **************************************************************************
|
---|
2 | /** @class FactFits
|
---|
3 |
|
---|
4 | @brief FITS writter for the FACT project.
|
---|
5 |
|
---|
6 | The FactFits class is able to open, manage and update FITS files.
|
---|
7 |
|
---|
8 | The file columns should be given to the class before the file is openned. Once
|
---|
9 | a file has been created, the structure of its columns cannot be changed. Only
|
---|
10 | row can be added.
|
---|
11 |
|
---|
12 | This class relies on the CCfits and CFitsIO packages.
|
---|
13 |
|
---|
14 | */
|
---|
15 | // **************************************************************************
|
---|
16 | #include "Fits.h"
|
---|
17 |
|
---|
18 | #include "Time.h"
|
---|
19 | #include "Converter.h"
|
---|
20 | #include "MessageImp.h"
|
---|
21 |
|
---|
22 |
|
---|
23 | #include <sys/stat.h> //for file stats
|
---|
24 | #include <cstdio> // for file rename
|
---|
25 |
|
---|
26 | using namespace std;
|
---|
27 | using namespace CCfits;
|
---|
28 |
|
---|
29 | // --------------------------------------------------------------------------
|
---|
30 | //
|
---|
31 | //! This gives a standard variable to the file writter.
|
---|
32 | //! This variable should not be related to the DIM service being logged.
|
---|
33 | //! @param desc the description of the variable to add
|
---|
34 | //! @param dataFormat the FITS data format corresponding to the variable to add.
|
---|
35 | //! @param dataPointer the memory location where the variable is stored
|
---|
36 | //! @param numDataBytes the number of bytes taken by the variable
|
---|
37 | //
|
---|
38 | void Fits::AddStandardColumn(const Description& desc, const string &dataFormat, void* dataPointer, long unsigned int numDataBytes)
|
---|
39 | {
|
---|
40 | //check if entry already exist
|
---|
41 | for (vector<Description>::const_iterator it=fStandardColDesc.begin(); it != fStandardColDesc.end(); it++)
|
---|
42 | if (it->name == desc.name)
|
---|
43 | return;
|
---|
44 |
|
---|
45 | fStandardColDesc.push_back(desc);
|
---|
46 | fStandardFormats.push_back(dataFormat);
|
---|
47 | fStandardPointers.push_back(dataPointer);
|
---|
48 | fStandardNumBytes.push_back(numDataBytes);
|
---|
49 | }
|
---|
50 |
|
---|
51 | // --------------------------------------------------------------------------
|
---|
52 | //
|
---|
53 | //! This gives the file writter access to the DIM data
|
---|
54 | //! @param desc a vector containing the description of all the columns to log
|
---|
55 | //! @param dataFormat a vector containing the FITS data format of all the columsn to log
|
---|
56 | //! @param dataPointer the memory location where the DIM data starts
|
---|
57 | //! @param numDataBytes the number of bytes taken by the DIM data.
|
---|
58 | //! @param out Message object to use for propagating messages
|
---|
59 | //
|
---|
60 | void Fits::InitDataColumns(const vector<Description> &desc, const vector<string>& dataFormat, void* dataPointer, MessageImp* out)
|
---|
61 | {
|
---|
62 | fDataFormats = dataFormat;
|
---|
63 | fDataPointer = dataPointer;
|
---|
64 |
|
---|
65 | //we will copy this information here. It duplicates the data, which is not great, but it is the easiest way of doing it right now
|
---|
66 | if ((desc.size() == dataFormat.size()+1) || //regular service
|
---|
67 | (desc.size() == dataFormat.size()+2))//service with ending string. skipped in fits
|
---|
68 | {//services have one (or two) more description than columns. skip the first entry while copying as it describes the table itself.
|
---|
69 | fDataColDesc.clear();
|
---|
70 | fTableDesc = desc[0].comment;
|
---|
71 | if (fTableDesc.size() > 68)
|
---|
72 | {
|
---|
73 | out->Warn("Table description \"" + fTableDesc + "\" too long. truncating it (68 char max.)");
|
---|
74 | fTableDesc = fTableDesc.substr(0,68);
|
---|
75 | }
|
---|
76 | for (unsigned int i=0;i<dataFormat.size(); i++)
|
---|
77 | {
|
---|
78 | string name = desc[i+1].name;
|
---|
79 | if (name.length() > 68)
|
---|
80 | {
|
---|
81 | out->Warn("Column name " + name + "too long. truncating it (68 char max.)");
|
---|
82 | name = name.substr(0, 68);
|
---|
83 | }
|
---|
84 | string comment = desc[i+1].comment;
|
---|
85 | if ((comment.length() + name.length()) > 71)
|
---|
86 | {
|
---|
87 | out->Warn("Column " + name + " added to \"" + comment + "\" too long. truncating to fit 68 chars.");
|
---|
88 | comment = comment.substr(0,68);
|
---|
89 | }
|
---|
90 | string unit = desc[i+1].unit;
|
---|
91 | if (unit.length() > 68) unit = comment.substr(0,68);
|
---|
92 | fDataColDesc.push_back(Description(name, comment, unit));
|
---|
93 | }
|
---|
94 | return;
|
---|
95 | }
|
---|
96 |
|
---|
97 | {//if we arrived here, this means that the columns descriptions could not be parsed
|
---|
98 | ostringstream str;
|
---|
99 | str << "Expected " << dataFormat.size() << " descriptions of columns, got " << desc.size()-1 << " for service: ";
|
---|
100 | if (desc.size() > 0)
|
---|
101 | str << desc[0].name;
|
---|
102 | else
|
---|
103 | str << "<unknown>";
|
---|
104 |
|
---|
105 | out->Warn(str.str());
|
---|
106 | }
|
---|
107 |
|
---|
108 | fDataColDesc.clear();
|
---|
109 | // fDataColDesc.push_back(Description("service", "comment", "unit"));
|
---|
110 | for (unsigned int i=0;i<dataFormat.size();i++)
|
---|
111 | {
|
---|
112 | ostringstream stt;
|
---|
113 | stt << "Data" << i;
|
---|
114 | fDataColDesc.push_back(Description(stt.str(), "comment", "unit"));
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | // --------------------------------------------------------------------------
|
---|
119 | //
|
---|
120 | //! This opens the FITS file (after the columns have been passed)
|
---|
121 | //! @param fileName the filename with complete or relative path of the file to open
|
---|
122 | //! @param tableName the name of the table that will receive the logged data.
|
---|
123 | //! @param file a pointer to an existing FITS file. If NULL, file will be opened and managed internally
|
---|
124 | //! @param fitsCounter a pointer to the integer keeping track of the opened FITS files
|
---|
125 | //! @param out a pointer to the MessageImp that should be used to log errors
|
---|
126 | //! @param runNumber the runNumber for which this file is opened. 0 means nightly file.
|
---|
127 | //
|
---|
128 | bool Fits::Open(const string& fileName, const string& tableName, uint32_t* fitsCounter, MessageImp* out, int runNumber, FITS* file)
|
---|
129 | {
|
---|
130 | fRunNumber = runNumber;
|
---|
131 | fMess = out;
|
---|
132 | fFileName = fileName;
|
---|
133 |
|
---|
134 | if (fFile)
|
---|
135 | {
|
---|
136 | fMess->Error("File already open...");
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | fFile = new FitsFile(*fMess);
|
---|
141 |
|
---|
142 | if (file == NULL)
|
---|
143 | {
|
---|
144 | if (!fFile->OpenFile(fileName, true))
|
---|
145 | return false;
|
---|
146 |
|
---|
147 | fNumOpenFitsFiles = fitsCounter;
|
---|
148 | (*fNumOpenFitsFiles)++;
|
---|
149 | }
|
---|
150 | else
|
---|
151 | {
|
---|
152 | if (!fFile->SetFile(file))
|
---|
153 | return false;
|
---|
154 | }
|
---|
155 |
|
---|
156 | //concatenate the standard and data columns
|
---|
157 | //do it the inneficient way first: its easier and faster to code.
|
---|
158 | for (unsigned int i=0;i<fStandardColDesc.size();i++)
|
---|
159 | {
|
---|
160 | fFile->AddColumn(fStandardColDesc[i].name, fStandardFormats[i],
|
---|
161 | fStandardColDesc[i].unit);
|
---|
162 | }
|
---|
163 |
|
---|
164 | for (unsigned int i=0; i<fDataColDesc.size(); i++)
|
---|
165 | {
|
---|
166 | string name = fDataColDesc[i].name;
|
---|
167 | if (name.empty())
|
---|
168 | {
|
---|
169 | ostringstream stt;
|
---|
170 | stt << "Data" << i;
|
---|
171 | name = stt.str();
|
---|
172 | }
|
---|
173 | //cout << endl << "#####adding column: " << name << " " << fDataFormats[i] << " " << fDataColDesc[i].unit << endl << endl;
|
---|
174 | fFile->AddColumn(name, fDataFormats[i], fDataColDesc[i].unit);
|
---|
175 | }
|
---|
176 |
|
---|
177 | try
|
---|
178 | {
|
---|
179 | if (!fFile->OpenNewTable(tableName, 100))
|
---|
180 | {
|
---|
181 | Close();
|
---|
182 | return false;
|
---|
183 | }
|
---|
184 |
|
---|
185 | fCopyBuffer.resize(fFile->GetDataSize());
|
---|
186 | //write header comments
|
---|
187 | ostringstream str;
|
---|
188 | for (unsigned int i=0;i<fStandardColDesc.size();i++)
|
---|
189 | {
|
---|
190 | str.str("");
|
---|
191 | str << "TTYPE" << i+1;
|
---|
192 | fFile->WriteKeyNT(str.str(), fStandardColDesc[i].name, fStandardColDesc[i].comment);
|
---|
193 | str.str("");
|
---|
194 | str << "TCOMM" << i+1;
|
---|
195 | fFile->WriteKeyNT(str.str(), fStandardColDesc[i].comment, "");
|
---|
196 | }
|
---|
197 |
|
---|
198 | for (unsigned int i=0; i<fDataColDesc.size(); i++)
|
---|
199 | {
|
---|
200 | string name = fDataColDesc[i].name;
|
---|
201 | if (name.empty())
|
---|
202 | {
|
---|
203 | ostringstream stt;
|
---|
204 | stt << "Data" << i;
|
---|
205 | name = stt.str();
|
---|
206 | }
|
---|
207 | str.str("");
|
---|
208 | str << "TTYPE" << i+fStandardColDesc.size()+1;
|
---|
209 | fFile->WriteKeyNT(str.str(), name, fDataColDesc[i].comment);
|
---|
210 | str.str("");
|
---|
211 | str << "TCOMM" << i+fStandardColDesc.size()+1;
|
---|
212 | fFile->WriteKeyNT(str.str(), fDataColDesc[i].comment, "");
|
---|
213 | }
|
---|
214 | fFile->WriteKeyNT("COMMENT", fTableDesc, "");
|
---|
215 | //END COMMENTS UPDATE
|
---|
216 | return fFile->GetNumRows()==0 ? true : WriteHeaderKeys();
|
---|
217 | }
|
---|
218 | catch (const CCfits::FitsException &e)
|
---|
219 | {
|
---|
220 | fMess->Error("Opening or creating table '"+tableName+"' in '"+fileName+"': "+e.message());
|
---|
221 |
|
---|
222 | fFile->fTable = NULL;
|
---|
223 | Close();
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | // --------------------------------------------------------------------------
|
---|
229 | //
|
---|
230 | //! This writes the standard header
|
---|
231 | //
|
---|
232 | bool Fits::WriteHeaderKeys()
|
---|
233 | {
|
---|
234 | if (!fFile->fTable)
|
---|
235 | return false;
|
---|
236 |
|
---|
237 | if (!fFile->WriteDefaultKeys("datalogger"))
|
---|
238 | return false;
|
---|
239 |
|
---|
240 | if (!fFile->WriteKeyNT("TSTARTI", 0, "Time when first event received (integral part)") ||
|
---|
241 | !fFile->WriteKeyNT("TSTARTF", 0, "Time when first event received (fractional part)") ||
|
---|
242 | !fFile->WriteKeyNT("TSTOPI", 0, "Time when last event received (integral part)") ||
|
---|
243 | !fFile->WriteKeyNT("TSTOPF", 0, "Time when last event received (fractional part)") ||
|
---|
244 | !fFile->WriteKeyNT("DATE-OBS", 0, "Time when first event received") ||
|
---|
245 | !fFile->WriteKeyNT("DATE-END", 0, "Time when last event received"))
|
---|
246 | return false;
|
---|
247 |
|
---|
248 | return true;
|
---|
249 | }
|
---|
250 | void Fits::MoveFileToCorruptedFile()
|
---|
251 | {
|
---|
252 | ostringstream corruptName;
|
---|
253 | struct stat st;
|
---|
254 | int append = 0;
|
---|
255 | corruptName << fFileName << "corrupt" << append;
|
---|
256 | while (!stat(corruptName.str().c_str(), &st))
|
---|
257 | {
|
---|
258 | append++;
|
---|
259 | corruptName.str("");
|
---|
260 | corruptName << fFileName << "corrupt" << append;
|
---|
261 | }
|
---|
262 | if (rename(fFileName.c_str(), corruptName.str().c_str()) != 0)
|
---|
263 | {
|
---|
264 | ostringstream str;
|
---|
265 | str << "rename() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
266 | fMess->Error(str);
|
---|
267 | return;
|
---|
268 | }
|
---|
269 |
|
---|
270 | fMess->Message("Renamed file " + fFileName + " to " + corruptName.str());
|
---|
271 |
|
---|
272 | }
|
---|
273 | // --------------------------------------------------------------------------
|
---|
274 | //
|
---|
275 | //! This writes one line of data to the file.
|
---|
276 | //! @param conv the converter corresponding to the service being logged
|
---|
277 | //
|
---|
278 | bool Fits::Write(const Converter &conv)
|
---|
279 | {
|
---|
280 | //first copy the standard variables to the copy buffer
|
---|
281 | int shift = 0;
|
---|
282 | for (unsigned int i=0;i<fStandardNumBytes.size();i++)
|
---|
283 | {
|
---|
284 | const char *charSrc = reinterpret_cast<char*>(fStandardPointers[i]);
|
---|
285 | reverse_copy(charSrc, charSrc+fStandardNumBytes[i], fCopyBuffer.data()+shift);
|
---|
286 | shift += fStandardNumBytes[i];
|
---|
287 | }
|
---|
288 |
|
---|
289 | try
|
---|
290 | {
|
---|
291 | //now take care of the DIM data. The Converter is here for that purpose
|
---|
292 | conv.ToFits(fCopyBuffer.data()+shift, fDataPointer, fCopyBuffer.size()-shift);
|
---|
293 | }
|
---|
294 | catch (const runtime_error &e)
|
---|
295 | {
|
---|
296 | ostringstream str;
|
---|
297 | str << fFile->GetName() << ": " << e.what();
|
---|
298 | fMess->Error(str);
|
---|
299 | return false;
|
---|
300 | }
|
---|
301 |
|
---|
302 | // This is not necessary, is it?
|
---|
303 | // fFile->fTable->makeThisCurrent();
|
---|
304 |
|
---|
305 | if (!fFile->AddRow())
|
---|
306 | {
|
---|
307 | Close();
|
---|
308 | MoveFileToCorruptedFile();
|
---|
309 | return false;
|
---|
310 | }
|
---|
311 |
|
---|
312 | if (!fFile->WriteData(fCopyBuffer))
|
---|
313 | {
|
---|
314 | Close();
|
---|
315 | return false;
|
---|
316 | }
|
---|
317 |
|
---|
318 | const double tm = *reinterpret_cast<double*>(fStandardPointers[0]);
|
---|
319 |
|
---|
320 | //the first standard variable is the current MjD
|
---|
321 | if (fEndMjD==0)
|
---|
322 | {
|
---|
323 | // FIXME: Check error?
|
---|
324 | fFile->WriteKeyNT("TSTARTI", uint32_t(floor(tm)), "Time when first event received (integral part)");
|
---|
325 | fFile->WriteKeyNT("TSTARTF", fmod(tm, 1), "Time when first event received (fractional part)");
|
---|
326 | fFile->WriteKeyNT("TSTOPI", uint32_t(floor(fEndMjD)), "Time when last event received (integral part)");
|
---|
327 | fFile->WriteKeyNT("TSTOPF", fmod(fEndMjD, 1), "Time when last event received (fractional part)");
|
---|
328 |
|
---|
329 | fFile->WriteKeyNT("DATE-OBS", Time(tm+40587).Iso(),
|
---|
330 | "Time when first event received");
|
---|
331 |
|
---|
332 | fFile->WriteKeyNT("DATE-END", Time(fEndMjD+40587).Iso(),
|
---|
333 | "Time when last event received");
|
---|
334 | }
|
---|
335 |
|
---|
336 | fEndMjD = tm;
|
---|
337 |
|
---|
338 | return true;
|
---|
339 | }
|
---|
340 |
|
---|
341 | // --------------------------------------------------------------------------
|
---|
342 | //
|
---|
343 | //! This closes the currently openned FITS file.
|
---|
344 | //! it also updates the header to reflect the time of the last logged row
|
---|
345 | //
|
---|
346 | void Fits::Close()
|
---|
347 | {
|
---|
348 | if (!fFile)
|
---|
349 | return;
|
---|
350 |
|
---|
351 | if (fFile->IsOpen() && fFile->IsOwner())
|
---|
352 | {
|
---|
353 | // FIMXE: Check for error? (It is allowed that fFile is NULL)
|
---|
354 | fFile->WriteKeyNT("TSTOPI", uint32_t(floor(fEndMjD)), "Time when last event received (integral part)");
|
---|
355 | fFile->WriteKeyNT("TSTOPF", fmod(fEndMjD, 1), "Time when last event received (fractional part)");
|
---|
356 |
|
---|
357 | fFile->WriteKeyNT("DATE-END", Time(fEndMjD+40587).Iso(),
|
---|
358 | "Time when last event received");
|
---|
359 | }
|
---|
360 |
|
---|
361 | if (fFile->IsOwner())
|
---|
362 | {
|
---|
363 | if (fNumOpenFitsFiles != NULL)
|
---|
364 | (*fNumOpenFitsFiles)--;
|
---|
365 | }
|
---|
366 |
|
---|
367 | const string name = fFile->GetName();
|
---|
368 |
|
---|
369 | delete fFile;
|
---|
370 | fFile = NULL;
|
---|
371 |
|
---|
372 | fMess->Info("Closed: "+name);
|
---|
373 |
|
---|
374 | // fMess = NULL;
|
---|
375 | }
|
---|
376 |
|
---|
377 | void Fits::Flush()
|
---|
378 | {
|
---|
379 | if (!fFile)
|
---|
380 | return;
|
---|
381 |
|
---|
382 | fFile->Flush();
|
---|
383 | }
|
---|
384 | // --------------------------------------------------------------------------
|
---|
385 | //! Returns the size on the disk of the Fits file being written.
|
---|
386 | int Fits::GetWrittenSize() const
|
---|
387 | {
|
---|
388 | if (!IsOpen())
|
---|
389 | return 0;
|
---|
390 |
|
---|
391 | struct stat st;
|
---|
392 | if (stat(fFile->GetName().c_str(), &st))
|
---|
393 | return 0;
|
---|
394 |
|
---|
395 | return st.st_size;
|
---|
396 | }
|
---|
397 |
|
---|
398 | /*
|
---|
399 | To be done:
|
---|
400 | - Check the check for column names in opennewtable
|
---|
401 | - If Open return false we end in an infinite loop (at least if
|
---|
402 | the dynamic cats to Bintable fails.
|
---|
403 |
|
---|
404 | */
|
---|