GPLIB++
SurfaceWaveData.cpp
Go to the documentation of this file.
1 #include "SurfaceWaveData.h"
2 #include <fstream>
3 #include <iomanip>
4 #include "FatalException.h"
5 #include <cassert>
6 #include "Util.h"
7 using namespace std;
8 
9 namespace gplib
10  {
11  SurfaceWaveData::SurfaceWaveData()
12  {
13  }
14 
15  SurfaceWaveData::~SurfaceWaveData()
16  {
17  }
18 
19  SurfaceWaveData::SurfaceWaveData(const SurfaceWaveData &Old) :
20  periods(Old.periods), phasevelocities(Old.phasevelocities)
21  {
22 
23  }
24 
26  {
27  if (this == &source)
28  return *this;
29  periods.clear();
30  phasevelocities.clear();
31  copy(source.periods.begin(), source.periods.end(), back_inserter(
32  periods));
33  copy(source.phasevelocities.begin(), source.phasevelocities.end(),
34  back_inserter(phasevelocities));
35  return *this;
36  }
37 
38  void SurfaceWaveData::ReadFile(const std::string &filename)
39  {
40  std::string ending = GetFileExtension(filename);
41  if (ending == ".asc")
42  {
43  ReadAscii(filename);
44  }
45  else
46  {
47  ReadSurf96(filename);
48  }
49  }
50  void SurfaceWaveData::ReadSurf96(const std::string &filename)
51  {
52  ifstream infile(filename.c_str());
53  double currperiod, currphase, dummy;
54  periods.clear();
55  phasevelocities.clear();
56 
57  char line[255];
58  infile.getline(line, 255);//we ignore the first line
59  while (infile.good())
60  {
61  infile >> dummy >> dummy >> currperiod >> dummy >> currphase;
62  if (infile.good())
63  {
64  periods.push_back(currperiod);
65  phasevelocities.push_back(currphase);
66  }
67  }
68  if (periods.empty() || phasevelocities.empty() || periods.size() != phasevelocities.size())
69  throw FatalException("Cannot read phase velocities from file: "
70  + filename);
71  }
72 
73  void SurfaceWaveData::ReadAscii(const std::string &filename)
74  {
75  ifstream infile(filename.c_str());
76  double currperiod, currphase;
77  periods.clear();
78  phasevelocities.clear();
79  while (infile.good())
80  {
81  infile >> currperiod >> currphase;
82  if (infile.good())
83  {
84  periods.push_back(currperiod);
85  phasevelocities.push_back(currphase);
86  }
87  }
88  if (periods.empty() || phasevelocities.empty() || periods.size() != phasevelocities.size())
89  throw FatalException("Cannot read phase velocities from file: "
90  + filename);
91  }
92 
93  void SurfaceWaveData::WriteAscii(const std::string &filename) const
94  {
95  ofstream outfile(filename.c_str());
96  assert(periods.size() == phasevelocities.size());
97  const unsigned int nelements = periods.size();
98  for (unsigned int i = 0; i < nelements; ++i)
99  {
100  outfile << setw(10) << setprecision(5) << periods.at(i);
101  outfile << setw(10) << setprecision(5) << phasevelocities.at(i);
102  outfile << endl;
103  }
104  }
105  }
void ReadSurf96(const std::string &filename)
read data as produced by the computer programs in seismology codes ascii
A class to read, write and store fundamental mode surface wave dispersion data.
SurfaceWaveData & operator=(const SurfaceWaveData &source)
virtual void ReadAscii(const std::string &filename)
Read a file in general ascii format, i.e lines with period velocity each.
virtual void WriteAscii(const std::string &filename) const
Write the data in simple ascii format.
void ReadFile(const std::string &filename)
Read data from file, depending on the extension.
The basic exception class for all errors that arise in gplib.