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