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

Last change on this file since 1996 was 1798, checked in by aubourg, 24 years ago

bugacecile

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