source: Sophya/trunk/ArchTOIPipe/Kernel/fitstoiwtr.cc@ 1725

Last change on this file since 1725 was 1712, checked in by aubourg, 24 years ago

pb flags uint_8/long

File size: 4.4 KB
Line 
1#include "fitstoiwtr.h"
2#include "toimanager.h"
3#include <sched.h>
4
5
6extern void fits_lock();
7extern void fits_unlock();
8
9FITSTOIWriter::FITSTOIWriter(string fn) {
10 fname = fn;
11 fstatus = 0;
12 nCols = 1;
13
14 // Open file
15 remove(fname.c_str());
16 fits_lock();
17 fits_create_file(&fptr,fname.c_str(),&fstatus);
18 fits_unlock();
19 name = "wtr";
20
21 totnscount = 0;
22}
23
24FITSTOIWriter::~FITSTOIWriter() {
25}
26
27
28void FITSTOIWriter::addInput(string name, TOI* toi, bool withFlag) {
29 declareInput(name);
30 int iTOI = fwinputs.size();
31 fwinputs.push_back(toi);
32 colsinput[iTOI] = pair<int,bool>(nCols, withFlag);
33 nCols++;
34 if (withFlag) nCols++;
35 toi->addConsumer(this);
36}
37
38void FITSTOIWriter::afterinit()
39{
40 if (inTOIs) delete[] inTOIs;
41 inTOIs = new (TOI*[fwinputs.size()]);
42
43 for (int i=0; i<fwinputs.size(); i++) {
44 inTOIs[i] = fwinputs[i];
45 }
46
47}
48
49void FITSTOIWriter::run() {
50 cout << "fitstoiwriter running" << endl;
51 // init done here
52 afterinit();
53 inited=true;
54
55 int ndata = inIx.size();
56 //int ncols = inIx.size() * (outFlags ? 2 : 1) + 1;
57
58 char** colnames = new (char*[nCols]);
59 char** coltypes = new (char*[nCols]);
60 char** colunits = new (char*[nCols]);
61
62 colnames[0] = "sampleNum";
63 coltypes[0] = "1D";
64 colunits[0] = "integer";
65
66 cout << " FITSTOIWriter::run() - Creating output FITS file: "
67 << fname << endl;
68
69 string * coln = new string[nCols];
70 for (map<string, int>::iterator ii = inIx.begin(); ii != inIx.end(); ii++) {
71 int jTOI = (*ii).second;
72 pair<int,bool> p = colsinput[jTOI];
73 int ck = p.first;
74 coln[ck] = (*ii).first;
75 colnames[ck] = const_cast<char*>(coln[ck].c_str());
76 cout << " Column[" << ck << "] Name=" << coln[ck] << endl;
77 coltypes[ck] = "1D";
78 colunits[ck] = "double";
79 if (p.second) {
80 ck++;
81 coln[ck] = "fg_" + coln[ck-1];
82 colnames[ck] = const_cast<char*>(coln[ck].c_str());
83 cout << " Column[" << ck << "] -Flag- Name=" << coln[ck] << endl;
84 coltypes[ck] = "1J";
85 colunits[ck] = "UInt_8Flag";
86 }
87 }
88
89 fits_lock();
90 fits_create_tbl(fptr, BINARY_TBL, 0, nCols, colnames, coltypes, colunits, NULL, &fstatus);
91 fits_write_date(fptr, &fstatus);
92 fits_unlock();
93
94 delete[] colunits;
95 delete[] coltypes;
96 delete[] colnames;
97 delete[] coln;
98
99 // Add headers ?
100
101 // loop
102
103 int fitsLine = 1;
104 int snb = getMinIn();
105 int sne = getMaxIn();
106
107 double* tabdata = new double[ndata];
108 uint_8* tabflag = new uint_8[ndata];
109 long* tabflagl = (long*) tabflag; // il faut uint_8 == long,
110 // c'est long long dans sophya
111 bool* tabck = new bool[ndata];
112 int i;
113 for(i=0; i<ndata; i++) {
114 tabdata[i] = -9.e19; // $CHECK$ - Reza valeur par defaut !
115 tabflag[i] = 0; // $CHECK$ - Reza valeur par defaut !
116 tabck[i] = checkInputTOIIndex(i);
117 }
118
119 for (int sn = snb; sn <= sne; sn++) {
120 // if ((sn%2000 == 0) || (sn<snb+5))
121 // cout << " DBG-A-FitsWriter::run()" << sn << endl;
122 try {
123 uint_8 out_flg;
124 double out_val;
125 for (i=0; i<ndata; i++) {
126 if (tabck[i]) {
127 getData(i,sn, out_val, out_flg);
128 tabdata[i] = out_val;
129 tabflag[i] = out_flg & 0xFFFFFFFF;
130 }
131 }
132 wontNeedBefore(sn);
133 // if ((sn%2000 == 0) || (sn<snb+5))
134 //cout << " DBG-B-FitsWriter::run()" << sn << endl;
135 double xx = sn;
136 fits_lock();
137 fits_write_col_dbl(fptr, 1, fitsLine, 1, 1, &xx, &fstatus);
138
139 for (i=0; i<ndata; i++) {
140 pair<int,bool> p = colsinput[i];
141 fits_write_col_dbl(fptr, p.first+1, fitsLine, 1, 1, tabdata+i, &fstatus);
142 if (p.second) {
143 long f = tabflag[i];
144 fits_write_col_lng(fptr, p.first+2, fitsLine, 1, 1, &f, &fstatus);
145 // fits_write_col_lng(fptr, p.first+2, fitsLine, 1, 1, tabflagl+i, &fstatus);
146 }
147 if (fstatus != 0) {
148 cerr << "fitstoiwtr error sn = " << sn << " i = "<< i << endl;
149 fits_report_error(stderr, fstatus);
150 abort();
151 }
152 }
153 fits_unlock();
154 if (fitsLine%50==0) {
155 sched_yield();
156 }
157 // if ((sn%2000 == 0) || (sn<snb+5))
158 // cout << " DBG-C-FitsWriter::run()" << sn << " line=" << fitsLine << endl;
159 fitsLine++; totnscount++;
160 } catch (PException e) {
161 cout << "fitstoiwtr exception " << e.Msg() << endl;
162 continue;
163 }
164 }
165
166 delete[] tabdata;
167 delete[] tabflag;
168 delete[] tabck;
169
170 fits_lock();
171 fits_close_file(fptr, &fstatus);
172 fits_report_error(stderr, fstatus);
173 fits_unlock();
174 cout << "fitstoiwriter done fitsLine= " << fitsLine << endl;
175}
176
177
178
179
180
181
182
183
Note: See TracBrowser for help on using the repository browser.