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