BirrpAsciiFormat.cpp

Go to the documentation of this file.
00001 #include "BirrpAsciiFormat.h"
00002 #include "MtuFormat.h"
00003 #include <fstream>
00004 #include <iomanip>
00005 #include <iostream>
00006 #include <FatalException.h>
00007 #include <boost/cast.hpp>
00008 
00009 using namespace std;
00010 
00011 namespace gplib
00012   {
00013     BirrpAsciiFormat::BirrpAsciiFormat()
00014       {
00015       }
00016     BirrpAsciiFormat::~BirrpAsciiFormat()
00017       {
00018       }
00019 
00020     void BirrpAsciiFormat::GetData(const std::string filename)
00021       {
00022         ifstream infile(filename.c_str());
00023         double currex, currey, currhx, currhy, currhz; // the current samples in the file
00024         const double birrp_samplerate = 1.0; //arbitrary sampling rate, birrp format does not contain rate
00025         // the birrp format does not store time information, so we set an arbitrary start time
00026         TimeSeries::ttime basetime(boost::gregorian::date(2004, 1, 1),
00027             boost::posix_time::time_duration(12, 0, 0));
00028         while (infile.good())
00029           {
00030             infile.precision(15);
00031             infile >> currex >> currey >> currhx >> currhy >> currhz;
00032             if (infile.good())
00033               {
00034                 Ex.GetData().push_back(currex);
00035                 Ey.GetData().push_back(currey);
00036                 Hx.GetData().push_back(currhx);
00037                 Hy.GetData().push_back(currhy);
00038                 Hz.GetData().push_back(currhz);
00039                 t.push_back(basetime);
00040                 basetime += boost::posix_time::seconds(
00041                     boost::numeric_cast<int>(birrp_samplerate)); // we assume an arbitrary sampling rate of 1 second
00042               }
00043           }
00044         //if we didn't stop because the file ended
00045         if (!infile.eof())
00046           {
00047             throw FatalException("Problem reading from file: " + filename);
00048           }
00049         Hx.SetSamplerate(birrp_samplerate); //we set the samplerate for each component, this value is arbitrary, but we don't have information
00050         Hy.SetSamplerate(birrp_samplerate);
00051         Hz.SetSamplerate(birrp_samplerate);
00052         Ex.SetSamplerate(birrp_samplerate);
00053         Ey.SetSamplerate(birrp_samplerate);
00054       }
00055 
00056     void BirrpAsciiFormat::WriteData(const std::string filename)
00057       {
00058         ofstream outfile(filename.c_str());
00059         TimeSeriesComponent::tdatait exit, eyit, hxit, hyit, hzit;
00060         //if any of the components has a different number of points throw and error
00061         const size_t exsize = Size();
00062 
00063         exit = Ex.GetData().begin();
00064         eyit = Ey.GetData().begin();
00065         hxit = Hx.GetData().begin();
00066         hyit = Hy.GetData().begin();
00067         hzit = Hz.GetData().begin();
00068         // write a simple ascii file with 5 values in one row
00069         while (exit != Ex.GetData().end())
00070           {
00071             outfile.precision(8);
00072             outfile << setw(20) << *exit << " ";
00073             outfile << setw(20) << *eyit << " ";
00074             outfile << setw(20) << *hxit << " ";
00075             outfile << setw(20) << *hyit << " ";
00076             outfile << setw(20) << *hzit << endl;
00077             exit++;
00078             eyit++;
00079             hzit++;
00080             hyit++;
00081             hxit++;
00082           }
00083       }
00084 
00085     BirrpAsciiFormat& BirrpAsciiFormat::operator=(BirrpAsciiFormat& source)
00086       {
00087         if (this != &source)
00088           {
00089             this->TimeSeries::operator=(source);
00090           }
00091         return *this;
00092       }
00093 
00094     BirrpAsciiFormat& BirrpAsciiFormat::operator=(MtuFormat& source)
00095       {
00096 
00097         this->TimeSeries::operator=(source);
00098         return *this;
00099       }
00100 
00101     BirrpAsciiFormat& BirrpAsciiFormat::operator=(TimeSeries& source)
00102       {
00103 
00104         this->TimeSeries::operator=(source);
00105         return *this;
00106       }
00107   }

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8