Ignore:
Timestamp:
05/05/14 09:48:00 (11 years ago)
Author:
tbretz
Message:
Cleanup includes and defines; moved CommentFromType and SizeFromType to the FITS namespace - I do not understand why that should be specific to ofits and not even static; some minor code cleanup; added a function CopyKeys which is intentionally safe if nobody alters the contents of the fist class; removed the SetKeyFromFitsString - it is a severe risk for the integrity of the FITS standard especially without any documentation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcore/ofits.h

    r17761 r17778  
    22#define MARS_ofits
    33
    4 #include <string>
    5 #include <string.h>
    6 #include <algorithm>
    7 #include <sstream>
    8 #include <iostream>
    9 #include <fstream>
    10 #include <iomanip>
    11 #include <vector>
    12 #include <algorithm>
    13 #include <stdexcept>
    14 
    15 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
     4#include "FITS.h"
     5#include "fits.h"
    166
    177#ifndef PACKAGE_NAME
     
    3121#endif
    3222
    33 #ifndef __MARS__
    34 #ifndef gLog
    35 #define gLog std::cerr
    36 #define ___err___   ""
    37 #define ___warn___  ""
    38 #define ___all___   ""
    39 #endif
    40 #else
    41 #include "MLog.h"
    42 #include "MLogManip.h"
    43 #define ___err___   err
    44 #define ___warn___  warn
    45 #define ___all___   all
    46 #endif
    47 
    48 #include "FITS.h"
    49 #include "checksum.h"
    50 
    5123// Sloppy:  allow / <--- left
    5224//          allow all characters (see specs for what is possible)
    5325
    5426// units: m kg s rad sr K A mol cd Hz J W V N Pa C Ohm S F Wb T Hlm lx
     27
    5528
    5629class ofits : public std::ostream
     
    7245        bool changed;   // For closing the file
    7346
    74         Key(const std::string &k="") : key(k), delim(false), fitsString(""), offset(0), changed(true) { }
     47        Key() : delim(false), offset(0), changed(true) { }
     48        Key(const std::string &s) : delim(false), fitsString(s), offset(0), changed(true) { }
    7549
    7650        std::string Trim(const std::string &str)
     
    215189        std::string Compile()
    216190        {
    217 
    218             if (fitsString != "")
     191            if (!fitsString.empty())
    219192                return fitsString;
    220193
     
    318291        entry.value   = value;
    319292        entry.comment = comment;
    320         entry.offset  = 0;
    321         entry.changed = true;
    322293
    323294        if (!entry.check(fCommentTrimming))
    324295            return false;
    325296
    326         fKeys.push_back(entry);
     297        fKeys.emplace_back(entry);
    327298        return true;
    328299    }
     
    455426        fCommentTrimming = allow;
    456427    }
     428
    457429    //Etienne: required to enable 1 to 1 reconstruction of files
    458430    bool SetKeyComment(const std::string& key, const std::string& comment)
     
    461433        if (it==fKeys.end())
    462434            return false;
     435
    463436        it->comment = comment;
    464437        it->changed = true;
    465438        return true;
    466439    }
     440        /* tbretz, I removed that, because it does not comply
     441         with the FITS standard, it omits all checks... it
     442         neither checks if a row already exists, not
     443         checks the chcracter set
    467444    bool SetKeyFromFitsString(const std::string& fitsString)
    468445    {
     
    484461        fKeys.push_back(entry);
    485462        return true;
    486     }
     463}*/
     464
     465
     466    bool CopyKeys(const fits &fin, bool update=false)
     467    {
     468        if (fTable.num_rows>0)
     469        {
     470#ifdef __EXCEPTIONS
     471            throw std::runtime_error("No header keys can be copied, rows were already written to the file... ignoring CopyKeys().");
     472#else
     473            gLog << ___err___ << "ERROR - No header key can be copyied, rows were already written to the file... ignoring CopyKeys()." << std::endl;
     474            return false;
     475#endif
     476        }
     477
     478        const auto &keys = fin.GetKeys();
     479
     480        // We can assume that the keys are all valid
     481        for (auto it=keys.cbegin(); it!=keys.cend(); it++)
     482        {
     483            const std::string &key = it->first;
     484
     485            if (FITS::IsReservedKeyWord(key))
     486                continue;
     487
     488            const auto &entry = it->second;
     489
     490            // If no delimit add the row no matter if it alread exists
     491            if (entry.fitsString[9]=='=')
     492            {
     493                // if the row already exists: remove it
     494                const auto it2 = findkey(key);
     495                if (it2!=fKeys.end())
     496                {
     497                    if (!update)
     498                        continue;
     499
     500                    fKeys.erase(it2);
     501                }
     502            }
     503
     504            fKeys.emplace_back(entry.fitsString);
     505        }
     506
     507        return true;
     508    }
     509
     510
    487511    bool SetRaw(const std::string &key, const std::string &val, const std::string &comment)
    488512    {
     
    569593    }
    570594
    571     std::string CommentFromType(char type)
    572     {
    573         std::string comment;
    574 
    575         switch (type)
    576         {
    577         case 'L': comment = "[1-byte BOOL]";  break;
    578         case 'A': comment = "[1-byte CHAR]";  break;
    579         case 'B': comment = "[1-byte BOOL]";  break;
    580         case 'I': comment = "[2-byte INT]";   break;
    581         case 'J': comment = "[4-byte INT]";   break;
    582         case 'K': comment = "[8-byte INT]";   break;
    583         case 'E': comment = "[4-byte FLOAT]"; break;
    584         case 'D': comment = "[8-byte FLOAT]"; break;
    585         case 'Q': comment = "[var. Length]"; break;
    586         }
    587 
    588         return comment;
    589     }
    590 
    591     uint32_t SizeFromType(char type)
    592     {
    593         size_t size = 0;
    594 
    595         switch (type)
    596         {
    597         case 'L': size = 1; break;
    598         case 'A': size = 1; break;
    599         case 'B': size = 1; break;
    600         case 'I': size = 2; break;
    601         case 'J': size = 4; break;
    602         case 'K': size = 8; break;
    603         case 'E': size = 4; break;
    604         case 'D': size = 8; break;
    605         case 'Q': size = 16; break;
    606         }
    607 
    608         return size;
    609     }
    610595    //ETIENNE to be able to restore the file 1 to 1, I must restore the header keys myself
    611596    virtual bool AddColumn(uint32_t cnt, char typechar, const std::string &name, const std::string &unit, const std::string &comment="", bool addHeaderKeys=true)
     
    682667            const std::string nc = std::to_string(fTable.num_cols);
    683668#endif
    684             SetStr("TFORM"+nc, type.str(), "format of "+name+" "+CommentFromType(typechar));
     669            SetStr("TFORM"+nc, type.str(), "format of "+name+" "+FITS::CommentFromType(typechar));
    685670            SetStr("TTYPE"+nc, name, comment);
    686671            if (!unit.empty())
    687672                SetStr("TUNIT"+nc, unit, "unit of "+name);
    688673        }
    689         size_t size = SizeFromType(typechar);
     674
     675        const size_t size = FITS::SizeFromType(typechar);
    690676
    691677        Table::Column col;
     
    697683        col.offset = fTable.bytes_per_row;
    698684
    699         fTable.cols.push_back(col);
     685        fTable.cols.emplace_back(col);
    700686
    701687        fTable.bytes_per_row += size*cnt;
Note: See TracChangeset for help on using the changeset viewer.