[1365] | 1 | #include "fitstoiwtr.h"
|
---|
| 2 | #include "toimanager.h"
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | extern void fits_lock();
|
---|
| 7 | extern void fits_unlock();
|
---|
| 8 |
|
---|
| 9 | FITSTOIWriter::FITSTOIWriter(string fn) {
|
---|
| 10 | fname = fn;
|
---|
| 11 | fstatus = 0;
|
---|
[1369] | 12 | outFlags = false;
|
---|
[1365] | 13 |
|
---|
| 14 | // Open file
|
---|
| 15 | remove(fname.c_str());
|
---|
| 16 | fits_lock();
|
---|
| 17 | fits_create_file(&fptr,fname.c_str(),&fstatus);
|
---|
| 18 | fits_unlock();
|
---|
| 19 | name = "wtr";
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | FITSTOIWriter::~FITSTOIWriter() {
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[1369] | 25 | void FITSTOIWriter::setOutFlags(bool yn) {
|
---|
| 26 | outFlags = yn;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[1365] | 29 | void FITSTOIWriter::addInput(string name, TOI* toi) {
|
---|
| 30 | declareInput(name);
|
---|
| 31 | fwinputs.push_back(toi);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | void FITSTOIWriter::run() {
|
---|
| 36 | cout << "fitstoiwriter running" << endl;
|
---|
| 37 | // init done here
|
---|
| 38 | delete[] inTOIs;
|
---|
| 39 | inTOIs = new (TOI*[fwinputs.size()]);
|
---|
| 40 |
|
---|
| 41 | for (int i=0; i<fwinputs.size(); i++) {
|
---|
| 42 | inTOIs[i] = fwinputs[i];
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[1369] | 45 | int ndata = inIx.size();
|
---|
| 46 | int ncols = inIx.size() * (outFlags ? 2 : 1) + 1;
|
---|
[1365] | 47 |
|
---|
| 48 | char** colnames = new (char*[ncols]);
|
---|
| 49 | char** coltypes = new (char*[ncols]);
|
---|
| 50 | char** colunits = new (char*[ncols]);
|
---|
| 51 |
|
---|
[1369] | 52 | colnames[0] = "samplenum";
|
---|
| 53 | coltypes[0] = "1D";
|
---|
| 54 | colunits[0] = "integer";
|
---|
| 55 |
|
---|
[1410] | 56 | for (map<string, int>::iterator ii = inIx.begin(); ii != inIx.end(); ii++) {
|
---|
| 57 | int j = (*ii).second;
|
---|
| 58 | string n = (*ii).first;
|
---|
[1369] | 59 | if (outFlags) j += j;
|
---|
| 60 | colnames[j+1] = const_cast<char*>(n.c_str());
|
---|
| 61 | coltypes[j+1] = "1D";
|
---|
| 62 | colunits[j+1] = "?";
|
---|
| 63 | if (outFlags) {
|
---|
| 64 | colnames[j+2] = const_cast<char*>((n+"_flg").c_str());
|
---|
| 65 | coltypes[j+2] = "1D";
|
---|
| 66 | colunits[j+2] = "?";
|
---|
| 67 | }
|
---|
[1365] | 68 | }
|
---|
| 69 |
|
---|
| 70 | fits_lock();
|
---|
| 71 | fits_create_tbl(fptr, BINARY_TBL, 0, ncols, colnames, coltypes, colunits, NULL, &fstatus);
|
---|
| 72 | fits_write_date(fptr, &fstatus);
|
---|
| 73 | fits_unlock();
|
---|
| 74 |
|
---|
| 75 | delete[] colunits;
|
---|
| 76 | delete[] coltypes;
|
---|
| 77 | delete[] colnames;
|
---|
| 78 |
|
---|
| 79 | // Add headers ?
|
---|
| 80 |
|
---|
| 81 | // loop
|
---|
| 82 |
|
---|
| 83 | int fitsLine = 1;
|
---|
| 84 | int snb = getMinIn();
|
---|
| 85 | int sne = getMaxIn();
|
---|
| 86 |
|
---|
| 87 | for (int sn = snb; sn <= sne; sn++) {
|
---|
| 88 | try {
|
---|
[1369] | 89 | fits_lock();
|
---|
| 90 | double xx = sn;
|
---|
| 91 | fits_write_col_dbl(fptr, 1, fitsLine, 1, 1, &xx, &fstatus);
|
---|
| 92 | fits_unlock();
|
---|
| 93 | for (int i=0; i<ndata; i++) {
|
---|
[1365] | 94 | double x = getData(i, sn);
|
---|
| 95 | fits_lock();
|
---|
[1369] | 96 | if (outFlags) {
|
---|
| 97 | fits_write_col_dbl(fptr, 2*i+2, fitsLine, 1, 1, &x, &fstatus);
|
---|
| 98 | x = getFlag(i, sn);
|
---|
| 99 | fits_write_col_dbl(fptr, 2*i+3, fitsLine, 1, 1, &x, &fstatus);
|
---|
| 100 | } else {
|
---|
| 101 | fits_write_col_dbl(fptr, i+2, fitsLine, 1, 1, &x, &fstatus);
|
---|
| 102 | }
|
---|
[1365] | 103 | if (fstatus != 0) {
|
---|
| 104 | cerr << "fitstoiwtr error sn = " << sn << " i = "<< i << endl;
|
---|
| 105 | fits_report_error(stderr, fstatus);
|
---|
| 106 | abort();
|
---|
| 107 | }
|
---|
| 108 | fits_unlock();
|
---|
| 109 | }
|
---|
| 110 | } catch (PException e) {
|
---|
| 111 | cout << "fitstoiwtr exception " << e.Msg() << endl;
|
---|
| 112 | continue;
|
---|
| 113 | }
|
---|
| 114 | fitsLine++;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | fits_lock();
|
---|
| 118 | fits_close_file(fptr, &fstatus);
|
---|
| 119 | fits_report_error(stderr, fstatus);
|
---|
| 120 | fits_unlock();
|
---|
| 121 | cout << "fitstoiwriter done" << endl;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 |
|
---|