Ignore:
Timestamp:
12/03/12 14:40:26 (12 years ago)
Author:
lyard
Message:
changed behavior. Now if service format changes, a new file is opened
File:
1 edited

Legend:

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

    r14238 r14715  
    125125    {//if we arrived here, this means that the columns descriptions could not be parsed
    126126        ostringstream str;
    127         str << "Expected " << dataFormat.size() << " descriptions of columns, got " << desc.size()-1 << " for service: ";
     127        str << "Expected " << dataFormat.size() << " descriptions of columns, got " << (int)(desc.size())-1 << " for service: ";
    128128        if (desc.size() > 0)
    129129            str << desc[0].name;
     
    208208        {
    209209            Close();
    210             return false;
     210            //if the file already exist, then the column names must have changed
     211            //let's move the file and try to open it again.
     212            string fileNameWithoutFits = fFileName.substr(0, fileName.size()-4);
     213            int counter = 0;
     214            while (counter < 100)
     215            {
     216                ostringstream newFileName;
     217                newFileName << fileNameWithoutFits << counter << ".fits";
     218                ifstream testStream(newFileName.str().c_str());
     219                if (!testStream)
     220                {
     221                    if (rename(fFileName.c_str(), newFileName.str().c_str()))
     222                        return false;
     223                    break;
     224                }
     225                counter++;
     226            }
     227            if (counter == 100)
     228                return false;
     229            //now we open it again.
     230            fFile = new FitsFile(*fMess);
     231            if (file == NULL)
     232            {
     233                if (!fFile->OpenFile(fileName, true))
     234                    return false;
     235                fNumOpenFitsFiles = fitsCounter;
     236                (*fNumOpenFitsFiles)++;
     237            }
     238            else
     239            {
     240                if (!fFile->SetFile(file))
     241                    return false;
     242            }
     243            //YES, we must also redo that thing here...
     244            //concatenate the standard and data columns
     245            //do it the inneficient way first: its easier and faster to code.
     246            for (unsigned int i=0;i<fStandardColDesc.size();i++)
     247            {
     248                fFile->AddColumn(fStandardColDesc[i].name, fStandardFormats[i],
     249                                 fStandardColDesc[i].unit);
     250            }
     251
     252            for (unsigned int i=0; i<fDataColDesc.size(); i++)
     253            {
     254                string name = fDataColDesc[i].name;
     255                if (name.empty())
     256                {
     257                    ostringstream stt;
     258                    stt << "Data" << i;
     259                    name = stt.str();
     260                }
     261        //cout << endl << "#####adding column: " << name << " " << fDataFormats[i] << " " << fDataColDesc[i].unit << endl << endl;
     262                fFile->AddColumn(name, fDataFormats[i], fDataColDesc[i].unit);
     263            }
     264            if (!fFile->OpenNewTable(tableName, 100))
     265            {
     266                Close();
     267                return false;
     268            }
    211269        }
    212270
    213271        fCopyBuffer.resize(fFile->GetDataSize());
    214272//write header comments
     273
    215274        ostringstream str;
    216275        for (unsigned int i=0;i<fStandardColDesc.size();i++)
     
    240299            fFile->WriteKeyNT(str.str(), fDataColDesc[i].comment, "");
    241300        }
     301
    242302        fFile->WriteKeyNT("COMMENT", fTableDesc, "");
    243303
     
    250310            fEndMjD = Time().Mjd();
    251311        }
     312
    252313        return fFile->GetNumRows()==0 ? WriteHeaderKeys() : true;
    253314    }
    254315    catch (const CCfits::FitsException &e)
    255316    {
     317        cout << "Exception !" << endl;
    256318        fMess->Error("Opening or creating table '"+tableName+"' in '"+fileName+"': "+e.message());
    257319
     
    323385        shift += fStandardNumBytes[i];
    324386    }
    325 
    326387    try
    327388    {
     
    339400    // This is not necessary, is it?
    340401    // fFile->fTable->makeThisCurrent();
    341 
    342402    if (!fFile->AddRow())
    343403    {
     
    346406        return false;
    347407    }
    348 
    349408    if (!fFile->WriteData(fCopyBuffer))
    350409    {
     
    352411        return false;
    353412    }
    354 
    355413    const double tm = *reinterpret_cast<double*>(fStandardPointers[0]);
    356414
     
    385443    if (!fFile)
    386444        return;
    387 
    388445    if (fFile->IsOpen() && fFile->IsOwner())
    389446    {
     
    395452                          "Time when last event received");
    396453    }
    397 
    398454    if (fFile->IsOwner())
    399455    {
     
    401457            (*fNumOpenFitsFiles)--;
    402458    }
    403 
    404459    const string name = fFile->GetName();
    405 
    406460    delete fFile;
    407461    fFile = NULL;
    408 
    409462    fMess->Info("Closed: "+name);
    410 
    411463//    fMess = NULL;
    412464}
Note: See TracChangeset for help on using the changeset viewer.