| 1 | #include <map>
|
|---|
| 2 | #include <vector>
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <fstream>
|
|---|
| 5 |
|
|---|
| 6 | #include <CCfits/CCfits>
|
|---|
| 7 |
|
|---|
| 8 | using namespace std;
|
|---|
| 9 |
|
|---|
| 10 | void writeValuesFromFits(vector<int>& offsets,ofstream& targetFile, unsigned char* fitsBuffer, vector<string> dumpList, map<string, CCfits::Column*>& colMap)
|
|---|
| 11 | {
|
|---|
| 12 | targetFile.precision(20);
|
|---|
| 13 | map<string, CCfits::Column*>::iterator it;
|
|---|
| 14 | for (it=colMap.begin(); it != colMap.end(); it++)
|
|---|
| 15 | {
|
|---|
| 16 | bool found = false;
|
|---|
| 17 | for (vector<string>::iterator jt=dumpList.begin(); jt != dumpList.end(); jt++)
|
|---|
| 18 | {
|
|---|
| 19 | if (it->first == *jt)
|
|---|
| 20 | {
|
|---|
| 21 | found = true;
|
|---|
| 22 | break;
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 | if (!found)
|
|---|
| 26 | continue;
|
|---|
| 27 | int offset = offsets[it->second->index()-1];
|
|---|
| 28 | const char* charSrc = reinterpret_cast<char*>(&fitsBuffer[offset]);
|
|---|
| 29 | unsigned char copyBuffer[30];//max size of a single variable
|
|---|
| 30 | for (int width = 0; width<it->second->width(); width++)
|
|---|
| 31 | {
|
|---|
| 32 | switch (it->second->type())
|
|---|
| 33 | {
|
|---|
| 34 | case CCfits::Tbyte:
|
|---|
| 35 | targetFile << *charSrc;
|
|---|
| 36 | charSrc += sizeof(char);
|
|---|
| 37 | break;
|
|---|
| 38 | case CCfits::Tushort:
|
|---|
| 39 | reverse_copy(charSrc, charSrc+sizeof(unsigned short), copyBuffer);
|
|---|
| 40 | targetFile << *reinterpret_cast<const unsigned short*>(copyBuffer);
|
|---|
| 41 | charSrc += sizeof(char);
|
|---|
| 42 | break;
|
|---|
| 43 | case CCfits::Tshort:
|
|---|
| 44 | reverse_copy(charSrc, charSrc+sizeof(short), copyBuffer);
|
|---|
| 45 | targetFile << *reinterpret_cast<const short*>(copyBuffer);
|
|---|
| 46 | charSrc += sizeof(char);
|
|---|
| 47 | break;
|
|---|
| 48 | case CCfits::Tuint:
|
|---|
| 49 | reverse_copy(charSrc, charSrc+sizeof(unsigned int), copyBuffer);
|
|---|
| 50 | //warning suppressed in gcc4.0.2
|
|---|
| 51 | targetFile << *reinterpret_cast<unsigned int*>(copyBuffer);
|
|---|
| 52 | charSrc += sizeof(int);
|
|---|
| 53 | break;
|
|---|
| 54 | case CCfits::Tint:
|
|---|
| 55 | reverse_copy(charSrc, charSrc+sizeof(int), copyBuffer);
|
|---|
| 56 | targetFile << *reinterpret_cast<int*>(copyBuffer);
|
|---|
| 57 | charSrc += sizeof(int);
|
|---|
| 58 | break;
|
|---|
| 59 | case CCfits::Tulong:
|
|---|
| 60 | reverse_copy(charSrc, charSrc+sizeof(unsigned long), copyBuffer);
|
|---|
| 61 | targetFile << *reinterpret_cast<unsigned long*>(copyBuffer);
|
|---|
| 62 | charSrc += sizeof(int);
|
|---|
| 63 | break;
|
|---|
| 64 | case CCfits::Tlong:
|
|---|
| 65 | reverse_copy(charSrc, charSrc+sizeof(long), copyBuffer);
|
|---|
| 66 | targetFile << *reinterpret_cast<long*>(copyBuffer);
|
|---|
| 67 | charSrc += sizeof(int);
|
|---|
| 68 | break;
|
|---|
| 69 | case CCfits::Tlonglong:
|
|---|
| 70 | reverse_copy(charSrc, charSrc+sizeof(long long), copyBuffer);
|
|---|
| 71 | targetFile << *reinterpret_cast<long long*>(copyBuffer);
|
|---|
| 72 | charSrc += sizeof(long long);
|
|---|
| 73 | break;
|
|---|
| 74 | case CCfits::Tfloat:
|
|---|
| 75 | reverse_copy(charSrc, charSrc+sizeof(float), copyBuffer);
|
|---|
| 76 | targetFile << *reinterpret_cast<float*>(copyBuffer);
|
|---|
| 77 | charSrc += sizeof(float);
|
|---|
| 78 | break;
|
|---|
| 79 | case CCfits::Tdouble:
|
|---|
| 80 | reverse_copy(charSrc, charSrc+sizeof(double), copyBuffer);
|
|---|
| 81 | targetFile << *reinterpret_cast<double*>(copyBuffer);
|
|---|
| 82 | charSrc += sizeof(double);
|
|---|
| 83 | break;
|
|---|
| 84 | case CCfits::Tnull:
|
|---|
| 85 | case CCfits::Tbit:
|
|---|
| 86 | case CCfits::Tlogical:
|
|---|
| 87 | case CCfits::Tstring:
|
|---|
| 88 | case CCfits::Tcomplex:
|
|---|
| 89 | case CCfits::Tdblcomplex:
|
|---|
| 90 | case CCfits::VTbit:
|
|---|
| 91 | case CCfits::VTbyte:
|
|---|
| 92 | case CCfits::VTlogical:
|
|---|
| 93 | case CCfits::VTushort:
|
|---|
| 94 | case CCfits::VTshort:
|
|---|
| 95 | case CCfits::VTuint:
|
|---|
| 96 | case CCfits::VTint:
|
|---|
| 97 | case CCfits::VTulong:
|
|---|
| 98 | case CCfits::VTlong:
|
|---|
| 99 | case CCfits::VTlonglong:
|
|---|
| 100 | case CCfits::VTfloat:
|
|---|
| 101 | case CCfits::VTdouble:
|
|---|
| 102 | case CCfits::VTcomplex:
|
|---|
| 103 | case CCfits::VTdblcomplex:
|
|---|
| 104 | cout << "Data type not implemented yet." << endl;
|
|---|
| 105 | return;
|
|---|
| 106 | break;
|
|---|
| 107 | default:
|
|---|
| 108 | cout << "THIS SHOULD NEVER BE REACHED" << endl;
|
|---|
| 109 | return;
|
|---|
| 110 | }//switch
|
|---|
| 111 | targetFile << " ";
|
|---|
| 112 | }//width loop
|
|---|
| 113 | }//iterator over the columns
|
|---|
| 114 | targetFile << endl;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // --------------------------------------------------------------------------
|
|---|
| 118 | //
|
|---|
| 119 | //! Calculates the required buffer size for reading one row of the current table.
|
|---|
| 120 | //! Also calculates the offsets to all the columns
|
|---|
| 121 | //
|
|---|
| 122 | vector<int> CalculateBufferSize(map<string, CCfits::Column*>& colMap)
|
|---|
| 123 | {
|
|---|
| 124 | vector<int> result;
|
|---|
| 125 | map<int,int> sizes;
|
|---|
| 126 | int size = 0;
|
|---|
| 127 |
|
|---|
| 128 | for (map<string, CCfits::Column*>::iterator it=colMap.begin(); it != colMap.end(); it++)
|
|---|
| 129 | {
|
|---|
| 130 | int width = it->second->width();
|
|---|
| 131 | switch (it->second->type())
|
|---|
| 132 | {
|
|---|
| 133 | case CCfits::Tbyte:
|
|---|
| 134 | case CCfits::Tushort:
|
|---|
| 135 | case CCfits::Tshort:
|
|---|
| 136 | sizes[it->second->index()] = sizeof(char)*width;
|
|---|
| 137 | break;
|
|---|
| 138 | case CCfits::Tuint:
|
|---|
| 139 | case CCfits::Tint:
|
|---|
| 140 | sizes[it->second->index()] = sizeof(int)*width;
|
|---|
| 141 | break;
|
|---|
| 142 | case CCfits::Tulong:
|
|---|
| 143 | case CCfits::Tlong:
|
|---|
| 144 | sizes[it->second->index()] = sizeof(int)*width;
|
|---|
| 145 | break;
|
|---|
| 146 | case CCfits::Tlonglong:
|
|---|
| 147 | sizes[it->second->index()] = sizeof(long long)*width;
|
|---|
| 148 | break;
|
|---|
| 149 | case CCfits::Tfloat:
|
|---|
| 150 | sizes[it->second->index()] = sizeof(float)*width;
|
|---|
| 151 | break;
|
|---|
| 152 | case CCfits::Tdouble:
|
|---|
| 153 | sizes[it->second->index()] = sizeof(double)*width;
|
|---|
| 154 | break;
|
|---|
| 155 | case CCfits::Tnull:
|
|---|
| 156 | case CCfits::Tbit:
|
|---|
| 157 | case CCfits::Tlogical:
|
|---|
| 158 | case CCfits::Tstring:
|
|---|
| 159 | case CCfits::Tcomplex:
|
|---|
| 160 | case CCfits::Tdblcomplex:
|
|---|
| 161 | case CCfits::VTbit:
|
|---|
| 162 | case CCfits::VTbyte:
|
|---|
| 163 | case CCfits::VTlogical:
|
|---|
| 164 | case CCfits::VTushort:
|
|---|
| 165 | case CCfits::VTshort:
|
|---|
| 166 | case CCfits::VTuint:
|
|---|
| 167 | case CCfits::VTint:
|
|---|
| 168 | case CCfits::VTulong:
|
|---|
| 169 | case CCfits::VTlong:
|
|---|
| 170 | case CCfits::VTlonglong:
|
|---|
| 171 | case CCfits::VTfloat:
|
|---|
| 172 | case CCfits::VTdouble:
|
|---|
| 173 | case CCfits::VTcomplex:
|
|---|
| 174 | case CCfits::VTdblcomplex:
|
|---|
| 175 | cout << "Data type not implemented yet." << endl;
|
|---|
| 176 | return vector<int>();
|
|---|
| 177 | break;
|
|---|
| 178 | default:
|
|---|
| 179 | cout << "THIS SHOULD NEVER BE REACHED" << endl;
|
|---|
| 180 | return vector<int>();
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | //calculate the offsets in the vector.
|
|---|
| 184 | int checkIndex = 1;
|
|---|
| 185 | for (map<int,int>::iterator it=sizes.begin(); it != sizes.end(); it++)
|
|---|
| 186 | {
|
|---|
| 187 | result.push_back(size);
|
|---|
| 188 | size += it->second;
|
|---|
| 189 | if (it->first != checkIndex)
|
|---|
| 190 | {
|
|---|
| 191 | cout << "Expected index " << checkIndex << " found " << it->first << endl;
|
|---|
| 192 | }
|
|---|
| 193 | checkIndex++;
|
|---|
| 194 | }
|
|---|
| 195 | result.push_back(size);
|
|---|
| 196 | return result;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | int main(int argc, const char** argv)
|
|---|
| 200 | {
|
|---|
| 201 | //set the names of the file and table to be loaded
|
|---|
| 202 | string fileNameToLoad = "test.fits";
|
|---|
| 203 | string tableNameToLoad = "FACT-TIME_ETIENNE";
|
|---|
| 204 | //set the vector of columns to be dumped
|
|---|
| 205 | vector<string> columnsToDump;
|
|---|
| 206 | columnsToDump.push_back("Data0");
|
|---|
| 207 | columnsToDump.push_back("Data1");
|
|---|
| 208 | //set the name of the output text file
|
|---|
| 209 | string outputFile = "output.txt";
|
|---|
| 210 |
|
|---|
| 211 | //load the fits file
|
|---|
| 212 | CCfits::FITS* file = NULL;
|
|---|
| 213 | try
|
|---|
| 214 | {
|
|---|
| 215 | file = new CCfits::FITS(fileNameToLoad);
|
|---|
| 216 | }
|
|---|
| 217 | catch (CCfits::FitsException e)
|
|---|
| 218 | {
|
|---|
| 219 | cout << "Could not open FITS file " << fileNameToLoad << " reason: " << e.message() << endl;
|
|---|
| 220 | return -1;
|
|---|
| 221 | }
|
|---|
| 222 | //check if the selected table indeed exists in the loaded file. If so, load it. Otherwise display the existing tables
|
|---|
| 223 | CCfits::Table* table;
|
|---|
| 224 | const multimap< string, CCfits::ExtHDU * > extMap = file->extension();
|
|---|
| 225 | if (extMap.find(tableNameToLoad) == extMap.end())
|
|---|
| 226 | {
|
|---|
| 227 | cout << "Could not open table " << tableNameToLoad << ". Tables in file are: " << endl;
|
|---|
| 228 | for (std::multimap<string, CCfits::ExtHDU*>::const_iterator it=extMap.begin(); it != extMap.end(); it++)
|
|---|
| 229 | cout << it->first << " ";
|
|---|
| 230 | cout << endl;
|
|---|
| 231 | delete file;
|
|---|
| 232 | return -1;
|
|---|
| 233 | }
|
|---|
| 234 | else
|
|---|
| 235 | table = dynamic_cast<CCfits::Table*>(extMap.find(tableNameToLoad)->second);
|
|---|
| 236 | int numRows = table->rows();
|
|---|
| 237 | //check that the given column names are indeed part of that table
|
|---|
| 238 | map<string, CCfits::Column*> colMap = table->column();
|
|---|
| 239 | if (columnsToDump.size() != 0)
|
|---|
| 240 | {
|
|---|
| 241 | for (vector<string>::iterator it=columnsToDump.begin(); it!= columnsToDump.end(); it++)
|
|---|
| 242 | {
|
|---|
| 243 | if (colMap.find(*it) == colMap.end())
|
|---|
| 244 | {
|
|---|
| 245 | cout << "Config-given dump list contains invalid entry " << *it << endl;
|
|---|
| 246 | delete file;
|
|---|
| 247 | return -1;
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | //dump the requested columns
|
|---|
| 252 | table->makeThisCurrent();
|
|---|
| 253 | vector<int> offsets = CalculateBufferSize(colMap);
|
|---|
| 254 | int size = offsets[offsets.size()-1];
|
|---|
| 255 | offsets.pop_back();
|
|---|
| 256 | unsigned char* fitsBuffer = new unsigned char[size];
|
|---|
| 257 |
|
|---|
| 258 | ofstream targetFile(outputFile.c_str());
|
|---|
| 259 | int status = 0;
|
|---|
| 260 |
|
|---|
| 261 | for (int i=1;i<=table->rows(); i++)
|
|---|
| 262 | {
|
|---|
| 263 | fits_read_tblbytes(file->fitsPointer(), i, 1, size, fitsBuffer, &status);
|
|---|
| 264 | if (status)
|
|---|
| 265 | {
|
|---|
| 266 | cout << "An error occurred while reading fits row #" << i << " error code: " << status << endl;
|
|---|
| 267 | for (unsigned int j=0;j<offsets.size(); j++)
|
|---|
| 268 | cout << offsets[j] << " ";
|
|---|
| 269 | cout << endl;
|
|---|
| 270 | }
|
|---|
| 271 | writeValuesFromFits(offsets, targetFile, fitsBuffer, columnsToDump, colMap);
|
|---|
| 272 | }
|
|---|
| 273 | delete file;
|
|---|
| 274 | return 0;
|
|---|
| 275 | }
|
|---|