00001 #pragma ident "$Id: SVExclusionList.cpp 81 2006-08-10 16:45:12Z ehagen $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdio.h>
00034
00035 #include "SVExclusionList.hpp"
00036
00037 namespace gpstk
00038 {
00039
00040 using namespace std;
00041
00042
00043 SVExclusionList::SVExclusionList( )
00044 :earliestTime(gpstk::DayTime::END_OF_TIME),
00045 latestTime(gpstk::DayTime::BEGINNING_OF_TIME)
00046 {
00047 readFailCount = 0;
00048 timeSpecString = "%F %g";
00049 }
00050
00051 SVExclusionList::SVExclusionList( const std::string filename )
00052 throw(SVExclusionList::SVExclusionFileNotFound)
00053 :earliestTime(gpstk::DayTime::END_OF_TIME),
00054 latestTime(gpstk::DayTime::BEGINNING_OF_TIME)
00055 {
00056 readFailCount = 0;
00057 timeSpecString = "%F %g";
00058 addFile( filename );
00059 }
00060
00061 void SVExclusionList::addFile( const std::string filename )
00062 throw(SVExclusionList::SVExclusionFileNotFound)
00063 {
00064 char file[100];
00065 sscanf(filename.c_str(),"%s",file);
00066 FILE* inf = fopen(file, "rt");
00067 if (inf==0)
00068 {
00069 char text[200];
00070 sprintf(text,"Exclusion file not found. Filename: %s",filename.c_str());
00071 std::string sout = text;
00072 SVExclusionFileNotFound noSVXFile( sout );
00073 GPSTK_THROW(noSVXFile);
00074 }
00075
00076 std::string temps;
00077 DayTime tempDTs;
00078 std::string tempe;
00079 DayTime tempDTe;
00080 int lineCount =0;
00081 char fileLine[200];
00082 while (fgets(fileLine, 200, inf))
00083 {
00084 lineCount++;
00085
00086 string whiteSpace = " \t\n\r";
00087 string lineIn = fileLine;
00088
00089
00090 string::size_type endIndex = lineIn.find_last_not_of(whiteSpace);
00091 lineIn = lineIn.substr( 0, endIndex+1 );
00092 string lead2Chars = lineIn.substr(0,2);
00093 if (lead2Chars.compare("TS")==0)
00094 {
00095 string::size_type q1 = lineIn.find('"');
00096 string::size_type q2 = lineIn.find('"',(q1+1));
00097 if (q1!=string::npos && q2!=string::npos)
00098 {
00099 timeSpecString = lineIn.substr((q1+1),(q2-q1-1));
00100
00101
00102 }
00103 else
00104 {
00105 readFailCount++;
00106 string failString = buildFailString(
00107 "Invalid TS specification at",
00108 lineCount,
00109 filename);
00110 readFailList.push_back( failString );
00111 }
00112 }
00113 if (lead2Chars.compare("EX")==0)
00114 {
00115 string::size_type c1 = lineIn.find(',');
00116 string::size_type c2 = lineIn.find(',',(c1+1));
00117 string::size_type c3 = lineIn.find(',',(c2+1));
00118 if (c1!=string::npos && c2!=string::npos)
00119 {
00120 std::string comment = "";
00121 int PRNID = StringUtils::asInt(lineIn.substr(2, (c1-1)) );
00122 if (PRNID<0 || PRNID>gpstk::MAX_PRN)
00123 {
00124 readFailCount++;
00125 string failString = buildFailString(
00126 "PRN ID out of range",
00127 lineCount,
00128 filename);
00129 readFailList.push_back( failString );
00130 continue;
00131 }
00132 temps = lineIn.substr((c1+1),(c2-c1-1));
00133 string::size_type nonWhiteBeg = temps.find_first_not_of(whiteSpace);
00134 string::size_type nonWhiteEnd = temps.find_last_not_of(whiteSpace);
00135
00136 if (nonWhiteEnd!=string::npos)
00137 temps = temps.substr(nonWhiteBeg,nonWhiteEnd-nonWhiteBeg+1);
00138 else
00139 temps = temps.substr(nonWhiteBeg);
00140
00141
00142 if (c3!=string::npos)
00143 {
00144 tempe = lineIn.substr(c2+1, (c3-c2-1) );
00145 comment = lineIn.substr(c3+1);
00146 nonWhiteBeg = comment.find_first_not_of(whiteSpace);
00147 comment = comment.substr(nonWhiteBeg);
00148 }
00149 else
00150 tempe = lineIn.substr(c2+1);
00151
00152 nonWhiteBeg = tempe.find_first_not_of(whiteSpace);
00153 nonWhiteEnd = tempe.find_last_not_of(whiteSpace);
00154
00155 if (nonWhiteEnd!=string::npos)
00156 tempe = tempe.substr(nonWhiteBeg,nonWhiteEnd-nonWhiteBeg+1);
00157 else
00158 tempe = tempe.substr(nonWhiteBeg);
00159 try
00160 {
00161
00162 tempDTs.setToString( temps, timeSpecString );
00163
00164 tempDTe.setToString( tempe, timeSpecString );
00165 if (tempDTs<=tempDTe)
00166 {
00167 SVExclusion svEx( tempDTs, tempDTe, PRNID, comment );
00168
00169
00170 addExclusion( svEx );
00171 }
00172 else
00173 {
00174 readFailCount++;
00175 string failString = buildFailString(
00176 "Start time after end time",
00177 lineCount,
00178 filename);
00179 readFailList.push_back( failString );
00180 }
00181 }
00182 catch (DayTime::DayTimeException& dte)
00183 {
00184 readFailCount++;
00185 string failString = buildFailString(
00186 dte.getText(),
00187 lineCount,
00188 filename);
00189 readFailList.push_back( failString );
00190 }
00191 catch (DayTime::FormatException& fe)
00192 {
00193 readFailCount++;
00194 string failString = buildFailString(
00195 fe.getText(),
00196 lineCount,
00197 filename);
00198 readFailList.push_back( failString );
00199 }
00200 catch (gpstk::StringUtils::StringException& se)
00201 {
00202 readFailCount++;
00203 string failString = buildFailString(
00204 se.getText(),
00205 lineCount,
00206 filename);
00207 readFailList.push_back( failString );
00208 }
00209 }
00210 else
00211 {
00212 readFailCount++;
00213 string failString = buildFailString(
00214 "Invalid EX line format at",
00215 lineCount,
00216 filename);
00217 readFailList.push_back( failString );
00218 }
00219 }
00220 }
00221 fclose(inf);
00222 }
00223
00224 void SVExclusionList::addExclusion( const SVExclusion svx )
00225 {
00226
00227
00228
00229
00230 pair<const int, gpstk::SVExclusion> temp( svx.getPRNID(), svx );
00231 exclusionMap.insert( temp );
00232
00233
00234 if (svx.getBeginTime() < earliestTime) earliestTime = svx.getBeginTime();
00235 if (svx.getEndTime() > latestTime) latestTime = svx.getEndTime();
00236 }
00237
00238 bool SVExclusionList::isExcluded(
00239 const int PRN,
00240 const gpstk::DayTime dt ) const
00241 {
00242 SVXListPair p = exclusionMap.equal_range( PRN );
00243 for (SVXListCI ci=p.first; ci != p.second; ++ci)
00244 {
00245 if (ci->second.isApplicable( PRN, dt )) return(true);
00246 }
00247 return(false);
00248 }
00249
00250 const SVExclusion& SVExclusionList::getApplicableExclusion(
00251 const int PRN, const gpstk::DayTime dt)
00252 const throw(SVExclusionList::NoSVExclusionFound)
00253 {
00254 SVXListPair p = exclusionMap.equal_range( PRN );
00255 for (SVXListCI ci=p.first; ci != p.second; ++ci)
00256 {
00257 if (ci->second.isApplicable( PRN, dt )) return( ci->second );
00258 }
00259
00260
00261 char textOut[80];
00262 sprintf(textOut,"No SVExclusion found for PRN %02d at %s.",
00263 PRN, dt.printf("week %F SOW %g, %02m/%02d/%02y %02H:%02M:%02S").c_str());
00264 std::string sout = textOut;
00265 NoSVExclusionFound noSVX( sout );
00266 GPSTK_THROW(noSVX);
00267 }
00268
00269 void SVExclusionList::dumpList( FILE* fp ) const
00270 {
00271 if (fp==0) return;
00272 std::string timeString = "Wk %F SOW %6.0g, %02m/%02d/%02y (DOY %03j) %02H:%02M:%02S";
00273 fprintf(fp,"List of SV Exclusion from SVExclusionList\n\n");
00274 for (int PRN=1; PRN<=gpstk::MAX_PRN; ++PRN)
00275 {
00276 fprintf(fp,"\nExclusions for PRN %02d\n",PRN);
00277 SVXListPair p = exclusionMap.equal_range( PRN );
00278 for (SVXListCI ci=p.first; ci != p.second; ++ci)
00279 {
00280 fprintf(fp," %s to %s\n",
00281 ci->second.getBeginTime().printf(timeString).c_str(),
00282 ci->second.getEndTime().printf(timeString).c_str());
00283 }
00284 }
00285 }
00286
00287 void SVExclusionList::listOfReadFailures() const
00288 {
00289 typedef list<string>::const_iterator LI;
00290 for (LI i=readFailList.begin(); i!=readFailList.end(); ++i)
00291 {
00292 cerr << *i << endl;
00293 }
00294 }
00295
00296 void SVExclusionList::listOfReadFailures( FILE* fpout ) const
00297 {
00298 if (fpout==0) return;
00299 typedef list<string>::const_iterator LI;
00300 for (LI i=readFailList.begin(); i!=readFailList.end(); ++i)
00301 {
00302 fprintf(fpout,"%s\n",(*i).c_str());
00303 }
00304 }
00305
00306 void SVExclusionList::listOfReadFailures( std::ofstream fsout ) const
00307 {
00308 if (!fsout.is_open()) return;
00309 typedef list<string>::const_iterator LI;
00310 for (LI i=readFailList.begin(); i!=readFailList.end(); ++i)
00311 {
00312 fsout << *i << endl;
00313 }
00314 }
00315
00316 std::string SVExclusionList::buildFailString(const std::string s,
00317 const int lineCount, const std::string filename )
00318 {
00319 string outString = s;
00320 outString += " at line ";
00321 outString += StringUtils::asString(lineCount);
00322 outString += " of file ";
00323 outString += filename;
00324 outString += ".";
00325 return(outString);
00326 }
00327
00328
00329 SVExclusion::SVExclusion( const gpstk::DayTime begin,
00330 const gpstk::DayTime end,
00331 const int PRNID,
00332 const std::string commentArg )
00333 {
00334 begExclude = begin;
00335 endExclude = end;
00336 PRN_IDENTIFIER = PRNID;
00337 comment = commentArg;
00338 }
00339
00340 bool SVExclusion::isApplicable( const int PRNID, const gpstk::DayTime dt ) const
00341 {
00342 if (dt>=begExclude && dt<=endExclude && PRN_IDENTIFIER==PRNID) return(true);
00343 return(false);
00344 }
00345
00346 }