| [1738] | 1 | // ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL
 | 
|---|
 | 2 | //                               Eric Aubourg
 | 
|---|
 | 3 | //                               Christophe Magneville
 | 
|---|
 | 4 | //                               Reza Ansari
 | 
|---|
| [2187] | 5 | // $Id: fitstoiwtr.cc,v 1.22 2002-09-09 15:33:14 aubourg Exp $
 | 
|---|
| [1738] | 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;
 | 
|---|
| [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 | 
 | 
|---|
 | 31 | FITSTOIWriter::~FITSTOIWriter() {
 | 
|---|
 | 32 | }
 | 
|---|
 | 33 | 
 | 
|---|
| [1785] | 34 | void FITSTOIWriter::setImplicitSN() {
 | 
|---|
 | 35 |   implicitSN = true;
 | 
|---|
 | 36 |   nCols = 0;
 | 
|---|
 | 37 | }
 | 
|---|
| [1369] | 38 | 
 | 
|---|
| [1527] | 39 | void 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] | 49 | void 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] | 60 | void FITSTOIWriter::run() {
 | 
|---|
 | 61 |   cout << "fitstoiwriter running" << endl;
 | 
|---|
 | 62 |   // init done here
 | 
|---|
| [2187] | 63 |   if (!inited) { 
 | 
|---|
 | 64 |     afterinit();
 | 
|---|
 | 65 |     inited=true;
 | 
|---|
 | 66 |   }
 | 
|---|
| [1439] | 67 | 
 | 
|---|
| [1369] | 68 |   int ndata = inIx.size();
 | 
|---|
| [1527] | 69 |   //int ncols = inIx.size() * (outFlags ? 2 : 1) + 1;
 | 
|---|
| [1365] | 70 | 
 | 
|---|
| [1527] | 71 |   char** colnames = new (char*[nCols]);
 | 
|---|
 | 72 |   char** coltypes = new (char*[nCols]);
 | 
|---|
 | 73 |   char** colunits = new (char*[nCols]);
 | 
|---|
| [1365] | 74 | 
 | 
|---|
| [1785] | 75 |   if (!implicitSN) {
 | 
|---|
 | 76 |     colnames[0] = "sampleNum";
 | 
|---|
 | 77 |     coltypes[0] = "1D";
 | 
|---|
 | 78 |     colunits[0] = "integer";
 | 
|---|
 | 79 |   }
 | 
|---|
 | 80 |       
 | 
|---|
| [1439] | 81 |   cout << " FITSTOIWriter::run() - Creating output FITS file: "
 | 
|---|
| [1501] | 82 |        << fname << endl;
 | 
|---|
| [1439] | 83 | 
 | 
|---|
| [1527] | 84 |   string * coln = new string[nCols];
 | 
|---|
| [1410] | 85 |   for (map<string, int>::iterator ii = inIx.begin(); ii != inIx.end(); ii++) {
 | 
|---|
| [1527] | 86 |     int jTOI = (*ii).second;
 | 
|---|
 | 87 |     pair<int,bool> p = colsinput[jTOI];
 | 
|---|
 | 88 |     int ck = p.first;
 | 
|---|
| [1492] | 89 |     coln[ck] = (*ii).first;
 | 
|---|
 | 90 |     colnames[ck] = const_cast<char*>(coln[ck].c_str());  
 | 
|---|
 | 91 |     cout << " Column[" << ck << "] Name=" << coln[ck] << endl;
 | 
|---|
 | 92 |     coltypes[ck] = "1D";
 | 
|---|
 | 93 |     colunits[ck] = "double";
 | 
|---|
| [1527] | 94 |     if (p.second) {
 | 
|---|
| [1439] | 95 |       ck++;
 | 
|---|
| [2041] | 96 |       if (longfgprefix) coln[ck] = "FLAG_" + coln[ck-1]; 
 | 
|---|
 | 97 |       else coln[ck] = "fg_" + coln[ck-1]; 
 | 
|---|
| [1492] | 98 |       colnames[ck] = const_cast<char*>(coln[ck].c_str());  
 | 
|---|
 | 99 |       cout << " Column[" << ck << "] -Flag- Name=" << coln[ck] << endl;
 | 
|---|
| [1527] | 100 |       coltypes[ck] = "1J";
 | 
|---|
| [1532] | 101 |       colunits[ck] = "UInt_8Flag";
 | 
|---|
| [1369] | 102 |     }
 | 
|---|
| [1365] | 103 |   }
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |   fits_lock();
 | 
|---|
| [1527] | 106 |   fits_create_tbl(fptr, BINARY_TBL, 0, nCols, colnames, coltypes, colunits, NULL, &fstatus);
 | 
|---|
| [1365] | 107 |   fits_write_date(fptr, &fstatus);
 | 
|---|
 | 108 |   fits_unlock();
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |   delete[] colunits;
 | 
|---|
 | 111 |   delete[] coltypes;
 | 
|---|
 | 112 |   delete[] colnames;
 | 
|---|
| [1492] | 113 |   delete[] coln;
 | 
|---|
| [1365] | 114 | 
 | 
|---|
 | 115 |   // Add headers ?
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 |   // loop
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 |   int fitsLine = 1;
 | 
|---|
 | 120 |   int snb = getMinIn();
 | 
|---|
 | 121 |   int sne = getMaxIn();
 | 
|---|
 | 122 | 
 | 
|---|
| [1442] | 123 |   double* tabdata = new double[ndata];
 | 
|---|
| [1532] | 124 |   uint_8* tabflag = new uint_8[ndata]; 
 | 
|---|
 | 125 |   long* tabflagl = (long*) tabflag; // il faut uint_8 == long, 
 | 
|---|
| [1527] | 126 |                                     // c'est long long dans sophya
 | 
|---|
| [1442] | 127 |   bool* tabck = new bool[ndata];
 | 
|---|
 | 128 |   int i;
 | 
|---|
 | 129 |   for(i=0; i<ndata; i++) {
 | 
|---|
 | 130 |     tabdata[i] = -9.e19;  // $CHECK$ - Reza valeur par defaut !
 | 
|---|
| [1464] | 131 |     tabflag[i] = 0;       // $CHECK$ - Reza valeur par defaut !
 | 
|---|
| [1442] | 132 |     tabck[i] = checkInputTOIIndex(i);
 | 
|---|
 | 133 |   } 
 | 
|---|
 | 134 | 
 | 
|---|
| [1365] | 135 |   for (int sn = snb; sn <= sne; sn++) {
 | 
|---|
| [1442] | 136 |     //    if ((sn%2000 == 0) || (sn<snb+5)) 
 | 
|---|
 | 137 |     //  cout << " DBG-A-FitsWriter::run()" << sn << endl;
 | 
|---|
| [1365] | 138 |     try {
 | 
|---|
| [1532] | 139 |       uint_8 out_flg;
 | 
|---|
| [1462] | 140 |       double out_val;
 | 
|---|
| [1442] | 141 |       for (i=0; i<ndata; i++) {
 | 
|---|
 | 142 |         if (tabck[i]) {
 | 
|---|
| [1462] | 143 |           getData(i,sn, out_val, out_flg);
 | 
|---|
 | 144 |           tabdata[i] = out_val;
 | 
|---|
| [1532] | 145 |           tabflag[i] = out_flg & 0xFFFFFFFF;
 | 
|---|
| [1442] | 146 |         }
 | 
|---|
 | 147 |       }
 | 
|---|
| [1692] | 148 |       wontNeedBefore(sn);
 | 
|---|
| [1442] | 149 |       //      if ((sn%2000 == 0) || (sn<snb+5)) 
 | 
|---|
 | 150 |       //cout << " DBG-B-FitsWriter::run()" << sn << endl;
 | 
|---|
| [1785] | 151 |       
 | 
|---|
| [1798] | 152 |       fits_lock();
 | 
|---|
| [1785] | 153 |       if (!implicitSN) {
 | 
|---|
 | 154 |         double xx = sn;
 | 
|---|
 | 155 |         fits_write_col_dbl(fptr, 1, fitsLine, 1, 1, &xx, &fstatus);
 | 
|---|
 | 156 |       }
 | 
|---|
 | 157 |         
 | 
|---|
| [1442] | 158 |       for (i=0; i<ndata; i++) {
 | 
|---|
| [1527] | 159 |         pair<int,bool> p = colsinput[i];
 | 
|---|
 | 160 |         fits_write_col_dbl(fptr, p.first+1, fitsLine, 1, 1, tabdata+i, &fstatus);
 | 
|---|
 | 161 |         if (p.second) {
 | 
|---|
| [1712] | 162 |           long f = tabflag[i];
 | 
|---|
 | 163 |           fits_write_col_lng(fptr, p.first+2, fitsLine, 1, 1, &f, &fstatus);
 | 
|---|
 | 164 |           //      fits_write_col_lng(fptr, p.first+2, fitsLine, 1, 1, tabflagl+i, &fstatus);
 | 
|---|
| [1527] | 165 |         } 
 | 
|---|
| [1365] | 166 |         if (fstatus != 0) {
 | 
|---|
 | 167 |           cerr << "fitstoiwtr error sn = " << sn << " i = "<< i << endl;
 | 
|---|
 | 168 |           fits_report_error(stderr, fstatus);
 | 
|---|
 | 169 |           abort();
 | 
|---|
 | 170 |         }
 | 
|---|
 | 171 |       }
 | 
|---|
| [1442] | 172 |       fits_unlock();
 | 
|---|
| [1696] | 173 |       if (fitsLine%50==0) {
 | 
|---|
 | 174 |         sched_yield();
 | 
|---|
 | 175 |       }
 | 
|---|
| [1442] | 176 |       //           if ((sn%2000 == 0) || (sn<snb+5)) 
 | 
|---|
 | 177 |       //        cout << " DBG-C-FitsWriter::run()" << sn << " line=" << fitsLine << endl;
 | 
|---|
| [1629] | 178 |       fitsLine++;  totnscount++;
 | 
|---|
| [1365] | 179 |     } catch (PException e) {
 | 
|---|
| [1442] | 180 |     cout << "fitstoiwtr exception " << e.Msg() << endl;
 | 
|---|
 | 181 |     continue;
 | 
|---|
| [1365] | 182 |     }
 | 
|---|
 | 183 |   }
 | 
|---|
 | 184 | 
 | 
|---|
| [1442] | 185 |   delete[] tabdata;
 | 
|---|
 | 186 |   delete[] tabflag;
 | 
|---|
 | 187 |   delete[] tabck;
 | 
|---|
 | 188 | 
 | 
|---|
| [1365] | 189 |   fits_lock();
 | 
|---|
 | 190 |   fits_close_file(fptr, &fstatus);
 | 
|---|
 | 191 |   fits_report_error(stderr, fstatus);
 | 
|---|
 | 192 |   fits_unlock();
 | 
|---|
| [1442] | 193 |   cout << "fitstoiwriter done fitsLine= " << fitsLine << endl;
 | 
|---|
| [1365] | 194 | }
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 | 
 | 
|---|
| [2187] | 198 | bool FITSTOIWriter::checkSampleLimits(long &min, long &max, int pass)
 | 
|---|
 | 199 | {
 | 
|---|
 | 200 |   if (!inited) { 
 | 
|---|
 | 201 |     afterinit();
 | 
|---|
 | 202 |     inited=true;
 | 
|---|
 | 203 |   }
 | 
|---|
 | 204 |   return TOIProcessor::checkSampleLimits(min, max, pass);
 | 
|---|
 | 205 | }
 | 
|---|
| [1365] | 206 | 
 | 
|---|
 | 207 | 
 | 
|---|