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

Last change on this file since 2068 was 2041, checked in by ansari, 23 years ago

Modifs fg_ --> FLAG_ ds FITSTOIReader et Writer , Reza 03/06/2002

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