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

Last change on this file since 1753 was 1738, checked in by aubourg, 24 years ago

copyright

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