SurfaceWaveData.cpp
Go to the documentation of this file.00001 #include "SurfaceWaveData.h"
00002 #include <fstream>
00003 #include <iomanip>
00004 #include "FatalException.h"
00005 #include <cassert>
00006 #include "Util.h"
00007 using namespace std;
00008
00009 namespace gplib
00010 {
00011 SurfaceWaveData::SurfaceWaveData()
00012 {
00013 }
00014
00015 SurfaceWaveData::~SurfaceWaveData()
00016 {
00017 }
00018
00019 SurfaceWaveData::SurfaceWaveData(const SurfaceWaveData &Old) :
00020 periods(Old.periods), phasevelocities(Old.phasevelocities)
00021 {
00022
00023 }
00024
00025 SurfaceWaveData& SurfaceWaveData::operator=(const SurfaceWaveData& source)
00026 {
00027 if (this == &source)
00028 return *this;
00029 periods.clear();
00030 phasevelocities.clear();
00031 copy(source.periods.begin(), source.periods.end(), back_inserter(
00032 periods));
00033 copy(source.phasevelocities.begin(), source.phasevelocities.end(),
00034 back_inserter(phasevelocities));
00035 return *this;
00036 }
00037
00038 void SurfaceWaveData::ReadFile(const std::string &filename)
00039 {
00040 std::string ending = GetFileExtension(filename);
00041 if (ending == ".asc")
00042 {
00043 ReadAscii(filename);
00044 }
00045 else
00046 {
00047 ReadSurf96(filename);
00048 }
00049 }
00050 void SurfaceWaveData::ReadSurf96(const std::string &filename)
00051 {
00052 ifstream infile(filename.c_str());
00053 double currperiod, currphase, dummy;
00054 periods.clear();
00055 phasevelocities.clear();
00056
00057 char line[255];
00058 infile.getline(line, 255);
00059 while (infile.good())
00060 {
00061 infile >> dummy >> dummy >> currperiod >> dummy >> currphase;
00062 if (infile.good())
00063 {
00064 periods.push_back(currperiod);
00065 phasevelocities.push_back(currphase);
00066 }
00067 }
00068 if (periods.empty() || phasevelocities.empty() || periods.size() != phasevelocities.size())
00069 throw FatalException("Cannot read phase velocities from file: "
00070 + filename);
00071 }
00072
00073 void SurfaceWaveData::ReadAscii(const std::string &filename)
00074 {
00075 ifstream infile(filename.c_str());
00076 double currperiod, currphase;
00077 periods.clear();
00078 phasevelocities.clear();
00079 while (infile.good())
00080 {
00081 infile >> currperiod >> currphase;
00082 if (infile.good())
00083 {
00084 periods.push_back(currperiod);
00085 phasevelocities.push_back(currphase);
00086 }
00087 }
00088 if (periods.empty() || phasevelocities.empty() || periods.size() != phasevelocities.size())
00089 throw FatalException("Cannot read phase velocities from file: "
00090 + filename);
00091 }
00092
00093 void SurfaceWaveData::WriteAscii(const std::string &filename) const
00094 {
00095 ofstream outfile(filename.c_str());
00096 assert(periods.size() == phasevelocities.size());
00097 const unsigned int nelements = periods.size();
00098 for (unsigned int i = 0; i < nelements; ++i)
00099 {
00100 outfile << setw(10) << setprecision(5) << periods.at(i);
00101 outfile << setw(10) << setprecision(5) << phasevelocities.at(i);
00102 outfile << endl;
00103 }
00104 }
00105 }