GPLIB++
BirrpAsciiFormat.cpp
Go to the documentation of this file.
1 #include "BirrpAsciiFormat.h"
2 #include "MtuFormat.h"
3 #include <fstream>
4 #include <iomanip>
5 #include <iostream>
6 #include <FatalException.h>
7 #include <boost/cast.hpp>
8 
9 using namespace std;
10 
11 namespace gplib
12  {
13  BirrpAsciiFormat::BirrpAsciiFormat()
14  {
15  }
16  BirrpAsciiFormat::~BirrpAsciiFormat()
17  {
18  }
19 
20  void BirrpAsciiFormat::GetData(const std::string filename)
21  {
22  ifstream infile(filename.c_str());
23  double currex, currey, currhx, currhy, currhz; // the current samples in the file
24  const double birrp_samplerate = 1.0; //arbitrary sampling rate, birrp format does not contain rate
25  // the birrp format does not store time information, so we set an arbitrary start time
26  TimeSeries::ttime basetime(boost::gregorian::date(2004, 1, 1),
27  boost::posix_time::time_duration(12, 0, 0));
28  while (infile.good())
29  {
30  infile.precision(15);
31  infile >> currex >> currey >> currhx >> currhy >> currhz;
32  if (infile.good())
33  {
34  Ex.GetData().push_back(currex);
35  Ey.GetData().push_back(currey);
36  Hx.GetData().push_back(currhx);
37  Hy.GetData().push_back(currhy);
38  Hz.GetData().push_back(currhz);
39  t.push_back(basetime);
40  basetime += boost::posix_time::seconds(
41  boost::numeric_cast<int>(birrp_samplerate)); // we assume an arbitrary sampling rate of 1 second
42  }
43  }
44  //if we didn't stop because the file ended
45  if (!infile.eof())
46  {
47  throw FatalException("Problem reading from file: " + filename);
48  }
49  Hx.SetSamplerate(birrp_samplerate); //we set the samplerate for each component, this value is arbitrary, but we don't have information
50  Hy.SetSamplerate(birrp_samplerate);
51  Hz.SetSamplerate(birrp_samplerate);
52  Ex.SetSamplerate(birrp_samplerate);
53  Ey.SetSamplerate(birrp_samplerate);
54  }
55 
56  void BirrpAsciiFormat::WriteData(const std::string filename)
57  {
58  ofstream outfile(filename.c_str());
59  TimeSeriesComponent::tdatait exit, eyit, hxit, hyit, hzit;
60  //if any of the components has a different number of points throw and error
61  const size_t exsize = Size();
62 
63  exit = Ex.GetData().begin();
64  eyit = Ey.GetData().begin();
65  hxit = Hx.GetData().begin();
66  hyit = Hy.GetData().begin();
67  hzit = Hz.GetData().begin();
68  // write a simple ascii file with 5 values in one row
69  while (exit != Ex.GetData().end())
70  {
71  outfile.precision(8);
72  outfile << setw(20) << *exit << " ";
73  outfile << setw(20) << *eyit << " ";
74  outfile << setw(20) << *hxit << " ";
75  outfile << setw(20) << *hyit << " ";
76  outfile << setw(20) << *hzit << endl;
77  exit++;
78  eyit++;
79  hzit++;
80  hyit++;
81  hxit++;
82  }
83  }
84 
85  BirrpAsciiFormat& BirrpAsciiFormat::operator=(BirrpAsciiFormat& source)
86  {
87  if (this != &source)
88  {
89  this->TimeSeries::operator=(source);
90  }
91  return *this;
92  }
93 
94  BirrpAsciiFormat& BirrpAsciiFormat::operator=(MtuFormat& source)
95  {
96 
97  this->TimeSeries::operator=(source);
98  return *this;
99  }
100 
101  BirrpAsciiFormat& BirrpAsciiFormat::operator=(TimeSeries& source)
102  {
103 
104  this->TimeSeries::operator=(source);
105  return *this;
106  }
107  }
This class is the base class for all classes dealing with MT time series.
Definition: TimeSeries.h:14
BirrpAsciiFormat reads and stores MT data in the ascii format used by the birrp processing software...
std::vector< double >::iterator tdatait
boost::posix_time::ptime ttime
We use the boost library time functionality for time types and store the time for each point in a vec...
Definition: TimeSeries.h:18
Read and write phoenix mtu binary files.
Definition: MtuFormat.h:13
The basic exception class for all errors that arise in gplib.