source: trunk/FACT++/src/RowChecker.cc@ 14676

Last change on this file since 14676 was 12730, checked in by lyard, 13 years ago
rowChecker program
File size: 2.7 KB
Line 
1/*
2 * RowChecker.cc
3 *
4 * Created on: Dec 20, 2011
5 * Author: lyard
6 */
7
8#include <fstream>
9#include <cstdlib>
10#include <iostream>
11#include <cstring>
12#include <sstream>
13
14using namespace std;
15
16
17//usage RowChecker <name of file> <size of header> <size of line> <mjdref> <givenLines>
18int main(int argc, char** argv)
19{
20
21 if (argc < 6)
22 return 0;
23
24 fstream file(argv[1]);
25
26 int headLen = atoi(argv[2]);
27 int lineWidth = atoi(argv[3]);
28 double mjdRef = atof(argv[4]);
29 int numLines = atoi(argv[5]);
30
31 int totalBytes = headLen;
32 file.seekp(headLen);
33
34 char* buf = new char[lineWidth];
35
36 double currentTime = 0;
37 char timeBuf[16];
38 int realNumRows = 0;
39
40 while (file.read(buf, lineWidth))
41 {
42 timeBuf[0] = buf[7];
43 timeBuf[1] = buf[6];
44 timeBuf[2] = buf[5];
45 timeBuf[3] = buf[4];
46 timeBuf[4] = buf[3];
47 timeBuf[5] = buf[2];
48 timeBuf[6] = buf[1];
49 timeBuf[7] = buf[0];
50 currentTime = reinterpret_cast<double*>(timeBuf)[0];
51
52 if (realNumRows >= numLines)
53 {
54 if (currentTime + mjdRef > 60000 || currentTime + mjdRef < 10000)
55 break;
56 if (currentTime + mjdRef > 20000 && currentTime + mjdRef < 50000)
57 break;
58 }
59//fix the time column if required.
60 if (currentTime > 50000 && currentTime < 60000)
61 {
62 currentTime -= 40587;
63 reinterpret_cast<double*>(timeBuf)[0] = currentTime;
64 file.seekp(totalBytes);
65 file.put(timeBuf[7]);
66 file.put(timeBuf[6]);
67 file.put(timeBuf[5]);
68 file.put(timeBuf[4]);
69 file.put(timeBuf[3]);
70 file.put(timeBuf[2]);
71 file.put(timeBuf[1]);
72 file.put(timeBuf[0]);
73 file.seekp(totalBytes + lineWidth);
74 }
75
76 realNumRows++;
77 totalBytes += lineWidth;
78 }
79 //now update the number of lines of the file
80 file.close();
81 file.open(argv[1]);
82 file.seekp(2880);
83 delete[] buf;
84 buf = new char[81];
85 buf[80] = 0;
86 bool changeDone = false;
87 int seeked = 2880;
88 if (realNumRows == numLines)
89 changeDone = true;
90
91 while (file.good() && !changeDone)
92 {
93 file.read(buf, 80);
94 string str(buf);
95
96 if (str.substr(0,9) == "NAXIS2 =")
97 {
98 ostringstream ss;
99 ss << realNumRows;
100 file.seekp(seeked + 30 - ss.str().size());
101 for (int i=0;i<ss.str().size();i++)
102 file.put(ss.str()[i]);
103 changeDone = true;
104 break;
105 }
106 seeked += 80;
107 }
108 if (!changeDone)
109 cout << -1;
110 else
111 cout << realNumRows;
112 file.close();
113 return realNumRows;
114}
Note: See TracBrowser for help on using the repository browser.