Ignore:
Timestamp:
May 26, 2011, 4:17:49 PM (8 years ago)
Author:
tbretz
Message:
Some performance improvements; added some comments with code to fill just a histogram or a graph - maybe for later use.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/fitsdump.cc

    r10797 r10837  
    1717class FitsDumper
    1818{
    19 
    2019public:
    2120    FitsDumper();
     
    3837
    3938    template<class T>
    40         void Write(ostream &out, const unsigned char* &ptr) const;
     39        T PtrToValue(const unsigned char* &ptr) const;
     40//    template<class T>
     41//        double PtrToDouble(const unsigned char *ptr) const;
     42//    double PtrToDouble(const unsigned char *ptr, CCfits::ValueType type) const;
    4143
    4244    /// Write a single row of the selected data
    43     int  WriteRow(ostream &, const vector<string> &, const vector<int> &, unsigned char *) const;
     45    int  WriteRow(ostream &, const vector<CCfits::Column*> &, const vector<int> &, unsigned char *) const;
    4446
    4547    bool OpenFile(const string &);        /// Open a file
     
    5456    bool Dump(const string &, const vector<string> &list, int);
    5557
     58//    bool Plot(const vector<string> &list);
    5659
    5760public:
     
    122125
    123126template<class T>
    124 void FitsDumper::Write(ostream &out, const unsigned char* &ptr) const
     127T FitsDumper::PtrToValue(const unsigned char* &ptr) const
    125128{
    126129    T t;
    127130    reverse_copy(ptr, ptr+sizeof(T), reinterpret_cast<unsigned char*>(&t));
    128     out << t;
    129 
    130131    ptr += sizeof(T);
    131 }
    132 
     132
     133    return t;
     134}
     135/*
     136template<class T>
     137double FitsDumper::PtrToDouble(const unsigned char *ptr) const
     138{
     139    T t;
     140    reverse_copy(ptr, ptr+sizeof(T), reinterpret_cast<unsigned char*>(&t));
     141    return t;
     142}
     143
     144double FitsDumper::PtrToDouble(const unsigned char *ptr, CCfits::ValueType type) const
     145{
     146    switch (type)
     147    {
     148    case CCfits::Tbyte:     return PtrToDouble<uint8_t> (ptr);
     149    case CCfits::Tushort:   return PtrToDouble<uint16_t>(ptr);
     150    case CCfits::Tuint:
     151    case CCfits::Tulong:    return PtrToDouble<uint32_t>(ptr);
     152    case CCfits::Tshort:    return PtrToDouble<int16_t> (ptr);
     153    case CCfits::Tint:
     154    case CCfits::Tlong:     return PtrToDouble<int32_t> (ptr);
     155    case CCfits::Tlonglong: return PtrToDouble<int64_t> (ptr);
     156    case CCfits::Tfloat:    return PtrToDouble<float>   (ptr);
     157    case CCfits::Tdouble:   return PtrToDouble<double>  (ptr);
     158
     159    default:
     160        throw runtime_error("Data type not implemented yet.");
     161    }
     162}
     163*/
    133164
    134165// --------------------------------------------------------------------------
     
    151182//!         the memory were the row has been loaded by cfitsio
    152183//
    153 int FitsDumper::WriteRow(ostream &out, const vector<string> &list, const vector<int> &offsets, unsigned char* fitsBuffer) const
     184int FitsDumper::WriteRow(ostream &out, const vector<CCfits::Column*> &list, const vector<int> &offsets, unsigned char* fitsBuffer) const
    154185{
    155186    int cnt = 0;
    156187
    157     for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
    158     {
    159         // Loop over all columns in our list of requested columns
    160         const CCfits::Column *col = fColMap.find(*it)->second;
     188    for (vector<CCfits::Column*>::const_iterator it=list.begin(); it!=list.end(); it++)
     189    {
     190        const CCfits::Column *col = *it;
    161191
    162192        // CCfits starts counting at 1 not 0
     
    171201            switch (col->type())
    172202            {
    173             case CCfits::Tbyte:     Write<uint8_t> (out, ptr); break;
    174             case CCfits::Tushort:   Write<uint16_t>(out, ptr); break;
     203            case CCfits::Tbyte:     out << PtrToValue<uint8_t> (ptr); break;
     204            case CCfits::Tushort:   out << PtrToValue<uint16_t>(ptr); break;
    175205            case CCfits::Tuint:
    176             case CCfits::Tulong:    Write<uint32_t>(out, ptr); break;
    177             case CCfits::Tshort:    Write<int16_t> (out, ptr); break;
     206            case CCfits::Tulong:    out << PtrToValue<uint32_t>(ptr); break;
     207            case CCfits::Tshort:    out << PtrToValue<int16_t> (ptr); break;
    178208            case CCfits::Tint:
    179             case CCfits::Tlong:     Write<int32_t> (out, ptr); break;
    180             case CCfits::Tlonglong: Write<int64_t> (out, ptr); break;
    181             case CCfits::Tfloat:    Write<float>   (out, ptr); break;
    182             case CCfits::Tdouble:   Write<double>  (out, ptr); break;
     209            case CCfits::Tlong:     out << PtrToValue<int32_t> (ptr); break;
     210            case CCfits::Tlonglong: out << PtrToValue<int64_t> (ptr); break;
     211            case CCfits::Tfloat:    out << PtrToValue<float>   (ptr); break;
     212            case CCfits::Tdouble:   out << PtrToValue<double>  (ptr); break;
    183213
    184214            default:
     
    189219            out << " ";
    190220            cnt++;
    191 
    192             if (out.fail())
    193             {
    194                 cerr << "ERROR - writing output: " << strerror(errno) << endl;
    195                 return 0;
    196             }
    197221        }
    198222    }
     
    200224    if (cnt>0)
    201225        out << endl;
     226
     227    if (out.fail())
     228    {
     229        cerr << "ERROR - writing output: " << strerror(errno) << endl;
     230        return -1;
     231    }
    202232
    203233    return cnt;
     
    420450        }
    421451
    422 
     452    // FIXME: Maybe do this when opening a table?
    423453    const vector<int> offsets = CalculateOffsets();
    424454    if (offsets.size()==0)
    425455        return false;
    426 
    427     const int size = offsets[offsets.size()-1];
    428456
    429457    ofstream out(filename=="-"?"/dev/stdout":filename);
     
    459487    out << "#" << endl;
    460488
     489
     490    // Loop over all columns in our list of requested columns
     491    vector<CCfits::Column*> columns;
     492    for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
     493        columns.push_back(fColMap.find(*it)->second);
     494
     495
     496    const int size = offsets[offsets.size()-1];
    461497    unsigned char* fitsBuffer = new unsigned char[size];
    462498
     
    470506            break;
    471507        }
    472         WriteRow(out, list, offsets, fitsBuffer);
     508        if (WriteRow(out, columns, offsets, fitsBuffer)<0)
     509        {
     510            status=1;
     511            break;
     512        }
    473513    }
    474514    delete[] fitsBuffer;
    475515
    476     return status;
    477 }
     516    return status==0;
     517}
     518
     519// --------------------------------------------------------------------------
     520//
     521//! Perform the actual dump, based on the current parameters
     522//
     523/*
     524bool FitsDumper::Plot(const vector<string> &list)
     525{
     526   for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
     527        if (fColMap.find(*it) == fColMap.end())
     528        {
     529            cerr << "WARNING - Column '" << *it << "' not found in table." << endl;
     530            return false;
     531        }
     532
     533    // FIXME: Maybe do this when opening a table?
     534    const vector<int> offsets = CalculateOffsets();
     535    if (offsets.size()==0)
     536        return false;
     537
     538    // Loop over all columns in our list of requested columns
     539    const CCfits::Column *col[3] =
     540    {
     541        list.size()>0 ? fColMap.find(list[0])->second : 0,
     542        list.size()>1 ? fColMap.find(list[1])->second : 0,
     543        list.size()>2 ? fColMap.find(list[2])->second : 0
     544    };
     545
     546    const int size = offsets[offsets.size()-1];
     547    unsigned char* fitsBuffer = new unsigned char[size];
     548
     549    const int idx = 0;
     550
     551    // CCfits starts counting at 1 not 0
     552    const size_t pos[3] =
     553    {
     554        col[0] ? offsets[col[0]->index()-1] + idx*col[0]->width()*ValueTypeToSize(col[0]->type()) : 0,
     555        col[1] ? offsets[col[1]->index()-1] + idx*col[1]->width()*ValueTypeToSize(col[1]->type()) : 0,
     556        col[2] ? offsets[col[2]->index()-1] + idx*col[2]->width()*ValueTypeToSize(col[2]->type()) : 0
     557    };
     558
     559    int status = 0;
     560    for (int i=1; i<=fTable->rows(); i++)
     561    {
     562        fits_read_tblbytes(fFile->fitsPointer(), i, 1, size, fitsBuffer, &status);
     563        if (status)
     564        {
     565            cerr << "An error occurred while reading fits row #" << i << " error code: " << status << endl;
     566            break;
     567        }
     568
     569        try
     570        {
     571            const double x[3] =
     572            {
     573                col[0] ? PtrToDouble(fitsBuffer+pos[0], col[0]->type()) : 0,
     574                col[1] ? PtrToDouble(fitsBuffer+pos[1], col[1]->type()) : 0,
     575                col[2] ? PtrToDouble(fitsBuffer+pos[2], col[2]->type()) : 0
     576            };
     577        }
     578        catch (const runtime_error &e)
     579        {
     580            cerr << e.what() << endl;
     581            status=1;
     582            break;
     583        }
     584    }
     585    delete[] fitsBuffer;
     586
     587    return status==0;
     588}
     589*/
    478590
    479591// --------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.