Changeset 15277 for trunk/Mars/mfileio


Ignore:
Timestamp:
04/08/13 12:21:14 (11 years ago)
Author:
lyard
Message:
added fits files header key support
Location:
trunk/Mars/mfileio
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mfileio/MWriteFitsFile.cc

    r14935 r15277  
    337337    return kTRUE;
    338338}
    339 
    340 
     339template<>
     340std::string MWriteFitsFile::GetFitsString(const double& value)
     341{
     342    std::ostringstream returnVal;
     343    returnVal << std::setprecision(value>1e-100 && value<1e100 ? 15 : 14) << value;
     344    std::string temp = returnVal.str();
     345    std::replace(temp.begin(), temp.end(), 'e', 'E');
     346    if (temp.find_first_of('E')==std::string::npos && temp.find_first_of('.')==std::string::npos)
     347        temp += ".";
     348    return temp;
     349}
     350template<>
     351std::string MWriteFitsFile::GetFitsString(const float& value)
     352{
     353    return GetFitsString((double)(value));
     354}
    341355// --------------------------------------------------------------------------
    342356//                                                                           
     
    355369
    356370   // remove the extension from the filename
     371   // this has been disabled for now
    357372   char fileNameNoExt[strlen(GetFileName()) + 1];
    358373   strcpy(fileNameNoExt, GetFileName());
    359    char * pos = strrchr(fileNameNoExt, '.');
    360    if (pos) *pos = 0;
    361 
     374//   char * pos = strrchr(fileNameNoExt, '.');
     375//   if (pos) *pos = 0;
     376//*fLog << inf <<"Filename no ext: " << fileNameNoExt << endl;
    362377   // loop over all FITS tables which have to be created.
    363378   map<TString, map<TString, MFitsSubTable> >::iterator i_table =
     
    373388      {
    374389          dol = dol(0, dol.Length()-5);
    375 //          *fLog << err << "New name: " << dol.Data() << endl;
    376       }
    377 //      else
    378 //          *fLog << err << dol(dol.Length()-5, dol.Length()).Data() << endl;
     390      }
     391      if (dol(dol.Length()-5, dol.Length()) == ".fits")
     392      {
     393          dol = dol(0, dol.Length()-5);
     394      }
    379395      dol += "_";
    380396      dol += i_table->first;
     
    10381054    if (!fTableHeaderWritten[tableName])
    10391055    {
     1056        for (vector<ofits::Key>::const_iterator it = fHeaderKeys.begin(); it != fHeaderKeys.end(); it++)
     1057            fFitsTables[tableName]->SetRaw(it->key, it->value, it->comment);
    10401058        fFitsTables[tableName]->WriteTableHeader(tableName.Data());
    10411059        fTableHeaderWritten[tableName] = true;
     
    11021120   // get new filename
    11031121   const TString readFileName = read->GetFullFileName();
    1104    const TString newname = MWriteRootFile::SubstituteName(fRule, readFileName)+".fits";
     1122   const TString newname = MWriteRootFile::SubstituteName(fRule, readFileName);
    11051123
    11061124   // create new files
  • trunk/Mars/mfileio/MWriteFitsFile.h

    r14935 r15277  
    138138   Int_t       PostProcess();
    139139
     140   //Header keys related stuff
     141   template<typename _T>
     142   std::string GetFitsString(const _T& value)
     143   {
     144       std::ostringstream returnVal;
     145       std::string typeName = typeid(_T).name();
     146       if (typeName == "i" || typeName == "j" || //int
     147           typeName == "s" || typeName == "t" || //short
     148           typeName == "l" || typeName == "m" || //long
     149           typeName == "x" || typeName == "y")   //long long
     150           returnVal << value;
     151
     152       if (typeName == "b")
     153       {
     154           if (value) returnVal << "T"; else returnVal << "F";
     155       }
     156       if (typeName == "Ss" || typeName == "PKc" ||
     157           (typeName.size() >= 4 && typeName[0] == 'A' && typeName[typeName.size()-1] == 'c' && typeName[typeName.size()-2] == '_'))
     158       {
     159           returnVal << value;
     160           std::string temp = returnVal.str();
     161           returnVal.str("");
     162           for (std::string::iterator c=temp.begin(); c<temp.end(); c++)
     163               if (*c=='\'')
     164                   temp.insert(c++, '\'');
     165           returnVal << "'" << temp << "'";
     166       }
     167       if (returnVal.str() == "")
     168           *fLog << warn << "WARNING: could not construct fits string from \"" << value << "\" typename: " << typeName << endl;
     169       return returnVal.str();
     170   }
     171
     172public:
     173   template<typename _T>
     174   bool SetHeaderKey(const std::string& key,
     175                     const _T& value,
     176                     const std::string& comment="")
     177   {
     178       //check if header was already written
     179       for (std::map<TString, bool>::iterator it=fTableHeaderWritten.begin(); it!= fTableHeaderWritten.end(); it++)
     180           if (it->second == true)
     181               return false;
     182       //headers were not written yet. Add one key
     183       std::vector<ofits::Key>::iterator it = fHeaderKeys.begin();
     184       //check if the value had been set beforel
     185       for (;it!=fHeaderKeys.end();it++)
     186       {
     187           if (it->key == key)
     188           {
     189               it->value = GetFitsString(value);
     190               it->comment = comment;
     191               break;
     192           }
     193       }
     194       //did we find the key ?
     195       if (it == fHeaderKeys.end())
     196       {//no ? add it !
     197           ofits::Key temp;
     198           temp.key = key;
     199           temp.value = GetFitsString(value);
     200           temp.comment = comment;
     201           fHeaderKeys.push_back(temp);
     202       }
     203       return true;
     204   }
     205private:
     206   std::vector<ofits::Key> fHeaderKeys;
     207
    140208   std::string Trim(const std::string &str);
    141209
     
    145213   std::vector<std::string> fVetoedColumns;
    146214   std::map<std::string, uint32_t> fBytesPerSamples;
     215
    147216
    148217public:
     
    186255   ClassDef(MWriteFitsFile, 0)   
    187256};
    188 
    189 
    190 #endif
     257//Specializations for float and doubles (because of the precision handling, I could not deal with it in the main function
     258template<>
     259std::string MWriteFitsFile::GetFitsString(const double& value);
     260template<>
     261std::string MWriteFitsFile::GetFitsString(const float& value);
     262#endif
Note: See TracChangeset for help on using the changeset viewer.