MTStationList.cpp
Go to the documentation of this file.00001 #include "MTStationList.h"
00002 #include "StationParser.h"
00003 #include "FatalException.h"
00004 #include "convert.h"
00005 #include "stringcompare.h"
00006 #include "NumUtil.h"
00007 #include <fstream>
00008 #include <iostream>
00009 #include <iomanip>
00010 #include <utility>
00011 #include <boost/bind.hpp>
00012 using namespace std;
00013
00014 namespace gplib
00015 {
00016 MTStationList::MTStationList()
00017 {
00018 }
00019 MTStationList::~MTStationList()
00020 {
00021 }
00022
00023 void MTStationList::GetData(const std::string filename)
00024 {
00025 ifstream infile(filename.c_str());
00026
00027 if (infile)
00028 {
00029 StationParser parser;
00030 parser.ParseFile(infile);
00031 for (unsigned int i = 0; i < parser.Stationnames.size(); ++i)
00032 {
00033 cout << "Working on file " << parser.Stationnames.at(i) << endl;
00034 try
00035 {
00036 MTStation CurrentStation;
00037 CurrentStation.GetData(parser.Stationnames.at(i));
00038 if (parser.HasLatLong.at(i))
00039 {
00040 CurrentStation.SetLongitude(parser.Longitudes.at(i));
00041 CurrentStation.SetLatitude(parser.Latitudes.at(i));
00042 }
00043 StationData.push_back(CurrentStation);
00044 } catch (FatalException &e)
00045 {
00046 cerr << e.what() << " Skipping file !" << endl;
00047 }
00048 }
00049 }
00050 else
00051 {
00052 throw FatalException("File not found: " + filename);
00053 }
00054 if (!StationData.empty())
00055 {
00056 FindCommon();
00057 }
00058 }
00059
00060 MTStation& MTStationList::at(int loc)
00061 {
00062 return (StationData.at(loc));
00063 }
00064
00065 void MTStationList::WriteList(const std::string filename)
00066 {
00067 ofstream outfile(filename.c_str());
00068 for (vector<MTStation>::iterator CurrentStation = StationData.begin(); CurrentStation
00069 != StationData.end(); CurrentStation++)
00070 {
00071 outfile << CurrentStation->GetName();
00072 outfile << setfill(' ') << setw(15) << resetiosflags(ios::fixed);
00073 outfile << CurrentStation->GetLongitude() << " ";
00074 outfile << setfill(' ') << setw(15) << resetiosflags(ios::fixed);
00075 outfile << CurrentStation->GetLatitude();
00076 outfile << endl;
00077 }
00078 }
00079
00080 void MTStationList::WriteAllData()
00081 {
00082 for (std::vector<MTStation>::iterator CurrentStation =
00083 StationData.begin(); CurrentStation != StationData.end(); CurrentStation++)
00084 CurrentStation->WriteBack();
00085 }
00086
00087 void MTStationList::FindCommon(void)
00088 {
00089 trealdata masterfrequencies(StationData.begin()->GetFrequencies());
00090 const double tolerance = 0.1;
00091 double bestfit;
00092 bool foundfrequency, iscommon;
00093 unsigned int k;
00094 commonfrequencies.clear();
00095 for (unsigned int i = 0; i < masterfrequencies.size(); ++i)
00096 {
00097 iscommon = true;
00098 for (unsigned int j = 0; j < StationData.size(); ++j)
00099 {
00100 foundfrequency = false;
00101 k = 0;
00102 while ((foundfrequency == false) && (k
00103 < StationData.at(j).GetFrequencies().size()))
00104 {
00105 if (fcmp(masterfrequencies.at(i),
00106 StationData.at(j).GetFrequencies().at(k), tolerance)
00107 == 0)
00108 foundfrequency = true;
00109 ++k;
00110 }
00111 iscommon = (iscommon && foundfrequency);
00112 }
00113 if (iscommon)
00114 commonfrequencies.push_back(masterfrequencies.at(i));
00115 }
00116 tindexvector indices(commonfrequencies.size(), -1);
00117 cfindices.assign(StationData.size(), indices);
00118 for (unsigned int i = 0; i < commonfrequencies.size(); ++i)
00119 {
00120 for (unsigned int j = 0; j < StationData.size(); ++j)
00121 {
00122 bestfit = 1e32;
00123 for (k = 0; k < StationData.at(j).GetFrequencies().size(); ++k)
00124 {
00125 if ((fcmp(commonfrequencies.at(i),
00126 StationData.at(j).GetFrequencies().at(k), tolerance)
00127 == 0) && (abs(commonfrequencies.at(i) - StationData.at(
00128 j).GetFrequencies().at(k)) < bestfit))
00129 {
00130 cfindices.at(j).at(i) = k;
00131 bestfit = commonfrequencies.at(i)
00132 - StationData.at(j).GetFrequencies().at(k);
00133 }
00134 }
00135 }
00136 }
00137 }
00138
00139
00140
00141
00142 class HasSameName: public std::binary_function<MTStation, MTStation, bool>
00143 {
00144 public:
00145 bool inline operator()(MTStation a, MTStation b)
00146 {
00147
00148 return !ciStringCompare(a.GetName(), b.GetName())
00149 && !ciStringCompare(b.GetName(), a.GetName());
00150 }
00151 };
00152
00153 tStatSyncPair FindCorrespondingSites(tStationList &MasterList,
00154 tStationList &SlaveList)
00155 {
00156 std::vector<std::pair<MTStation*, MTStation*> > CorrespondingSites;
00157 for (size_t i = 0; i < MasterList.size(); ++i)
00158 {
00159 tStationList::iterator slaveit = find_if(SlaveList.begin(),
00160 SlaveList.end(), boost::bind(HasSameName(), MasterList.at(i),
00161 _1));
00162 if (slaveit != SlaveList.end())
00163 CorrespondingSites.push_back(std::make_pair(&MasterList.at(i),
00164 &*slaveit));
00165 else
00166 std::cerr << "Not found: " << MasterList.at(i).GetName()
00167 << std::endl;
00168 }
00169 return CorrespondingSites;
00170 }
00171 }