GPLIB++
TimeSeriesData.cpp
Go to the documentation of this file.
1 #include "TimeSeriesData.h"
2 #include "BirrpAsciiFormat.h"
3 #include "MtuFormat.h"
4 #include "CsvFormat.h"
5 #include "LemiTsFormat.h"
6 #include "FatalException.h"
7 #include "convert.h"
8 #include "Util.h"
9 #include <iostream>
10 
11 using namespace std;
12 namespace gplib
13  {
14  TimeSeriesData::TimeSeriesData()
15  {
16  }
17 
18  TimeSeriesData::~TimeSeriesData()
19  {
20  }
21 
22  void TimeSeriesData::GetData(std::string filename)
23  {
24  string ending;
25  //we identify the type of data by the file extension
26  ending = GetFileExtension(filename);
27  //Phoenix MTU data can have different endings depending on the sampling frequency
28  if (ending == ".ts3" || ending == ".TS3" || ending == ".ts4" || ending
29  == ".TS4" || ending == ".ts5" || ending == ".TS5")
30  {
31  Data = boost::shared_ptr<TimeSeries>(new MtuFormat);
32  datatype = mtu;
33  }
34  //asc is the format used by birrp, basically just columns of numbers
35  else if (ending == ".asc")
36  {
37  Data = boost::shared_ptr<TimeSeries>(new BirrpAsciiFormat);
38  datatype = birrp;
39  }
40  //comma separated values have the ending .csv
41  else if (ending == ".csv")
42  {
43  Data = boost::shared_ptr<TimeSeries>(new CsvFormat);
44  datatype = csv;
45  }
46  //the lemi instruments produce ascii files with additional columns
47  else if (ending == ".lem")
48  {
49  Data = boost::shared_ptr<TimeSeries>(new LemiTsFormat);
50  datatype = lemi;
51  }
52  else
53  {
54  datatype = tsunknown;
55  throw FatalException(
56  "Unknown data format or file does not exist : " + filename);
57  }
58  name = filename;
59  unsigned int dotpos = name.find('.', 0);
60  if (dotpos != string::npos)
61  name.erase(dotpos);
62  Data->GetData(filename);
63  }
64 
65  TimeSeriesData& TimeSeriesData::operator=(const TimeSeriesData& source)
66  {
67  if (this != &source)
68  {
69  this->name = source.name;
70  this->datatype = source.datatype;
71  //TODO introduce virtual constructors or redesign read/write mechanism
72  this->Data = source.Data;
73  }
74  return *this;
75  }
76 
77  void TimeSeriesData::WriteAsMtu(std::string filename_base)
78  {
79  if (datatype == mtu)
80  {
81  switch (int(Data->GetSamplerate()))
82  {
83  case 2400:
84  Data->WriteData(filename_base + ".ts3");
85  break;
86  case 150:
87  Data->WriteData(filename_base + ".ts4");
88  break;
89  case 15:
90  Data->WriteData(filename_base + ".ts5");
91  break;
92  default:
93  throw FatalException(
94  "Unknown samplerate ! Cannot write file. Value is: "
95  + stringify(Data->GetSamplerate()));
96  break;
97  }
98  }
99  else
100  {
101  throw FatalException("Data conversion not implemented yet ! ");
102  }
103  }
104 
105  void TimeSeriesData::WriteAsBirrp(std::string filename_base)
106  {
107  //return static_cast<CBirrpAsciiFormat*>(Data)->WriteData(filename_base+".asc");
108  if (datatype == birrp)
109  Data->WriteData(filename_base + ".asc");
110  else
111  {
112  BirrpAsciiFormat BirrpData;
113  BirrpData = *Data;
114  BirrpData.WriteData(filename_base + ".asc");
115  }
116  }
117 
118  void TimeSeriesData::WriteAsLemi(std::string filename_base)
119  {
120  if (datatype == lemi)
121  Data->WriteData(filename_base + ".lem");
122  else
123  {
124  LemiTsFormat LemiData;
125  LemiData = *Data;
126  LemiData.WriteData(filename_base + ".lem");
127  }
128  }
129 
130  void TimeSeriesData::WriteAsCsv(std::string filename_base)
131  {
132  throw FatalException("Csv write not implemented yet ! ");
133  }
134 
135  void TimeSeriesData::WriteBack(std::string filename_base)
136  {
137  switch (datatype)
138  {
139  case lemi:
140  WriteAsLemi(filename_base);
141  break;
142  case birrp:
143  WriteAsBirrp(filename_base);
144  break;
145  case mtu:
146  WriteAsMtu(filename_base);
147  break;
148  case csv:
149  WriteAsCsv(filename_base);
150  break;
151  default:
152  throw FatalException("Cannot write back data ! Unknown datatype !");
153  break;
154  }
155  }
156  }
Read and write ascii files produced by the LEMI instruments.
Definition: LemiTsFormat.h:15
BirrpAsciiFormat reads and stores MT data in the ascii format used by the birrp processing software...
TimeSeriesData stores a pointer to the different components of magnetotelluric data and provides func...
Read and write phoenix mtu binary files.
Definition: MtuFormat.h:13
This class reads and writes data from Comma Separated Files CSV as produced by Excel etc...
Definition: CsvFormat.h:14
virtual void WriteData(const std::string filename)
Write data in birrp ascii format to a file called filename.
virtual void WriteData(const std::string filename)
The basic exception class for all errors that arise in gplib.