00001 #pragma ident "$Id: SatOrbitStore.cpp 2457 2010-08-18 14:20:12Z coandrei $"
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "SatOrbitStore.hpp"
00032 #include "SP3Stream.hpp"
00033 #include "SP3Data.hpp"
00034 #include "SP3Header.hpp"
00035 #include "ReferenceFrames.hpp"
00036 #include <sstream>
00037 #include <iomanip>
00038
00039 namespace gpstk
00040 {
00041
00042 using namespace std;
00043
00044
00045
00046 void SatOrbitStore::loadSP3File(const std::string& filename)
00047 throw(FileMissingException)
00048 {
00049 try
00050 {
00051
00052 SP3Stream strm(filename.c_str());
00053 if (!strm)
00054 {
00055 FileMissingException e("File "+filename+" could not be opened.");
00056 GPSTK_THROW(e);
00057 }
00058
00059 SP3Header header;
00060 strm >> header;
00061
00062
00063
00064 if (tolower(header.pvFlag) != 'v')
00065 {
00066
00067
00068 Exception e("There are no velocity data in the file : "+filename);
00069 GPSTK_THROW(e);
00070 }
00071
00072 SP3Data rec;
00073 while(strm >> rec)
00074 {
00075 bool rejectBadPosFlag = false;
00076 bool rejectBadClockFlag = false;
00077
00078
00079
00080 if( (rec.clk == 999999.999999) &&
00081 ( rejectBadClockFlag ) )
00082 {
00083 continue;
00084 }
00085
00086
00087
00088 if( ( (rec.x[0] == 0.0) ||
00089 (rec.x[1] == 0.0) ||
00090 (rec.x[2] == 0.0) ) &&
00091 ( rejectBadPosFlag ) )
00092 {
00093 continue;
00094 }
00095
00096
00097 rec.version = header.version;
00098
00099 if( pe.find(rec.sat) == pe.end())
00100 {
00101 ostringstream ss;
00102 ss << rec.sat;
00103 pe.insert(pair<SatID,PvtStore>(rec.sat,PvtStore(ss.str(),PvtStore::ITRF)));
00104 }
00105
00106 PvtStore& svEph = pe[rec.sat];
00107
00108 PvtStore::Pvt eph;
00109
00110
00111 if(svEph.isEpochExist(rec.time))
00112 {
00113 eph = svEph.getPvt(rec.time);
00114 }
00115
00116 if(tolower(rec.flag)=='p')
00117 {
00118 eph.position[0] = rec.x[0] * 1000.0;
00119 eph.position[1] = rec.x[1] * 1000.0;
00120 eph.position[2] = rec.x[2] * 1000.0;
00121 eph.dtime = rec.clk * 1e-6;
00122
00123 }
00124 else if(tolower(rec.flag)=='v')
00125 {
00126 eph.velocity[0] = rec.x[0] / 10.0;
00127 eph.velocity[1] = rec.x[1] / 10.0;
00128 eph.velocity[2] = rec.x[2] / 10.0;
00129 eph.ddtime = rec.clk * 1e-10;
00130 }
00131 else
00132 {
00133
00134 Exception e("It should never go here");
00135 GPSTK_THROW(e);
00136 }
00137
00138 svEph.addPvt(rec.time, eph);
00139
00140 }
00141
00142 }
00143 catch (gpstk::Exception& e)
00144 {
00145 GPSTK_RETHROW(e);
00146 }
00147
00148 }
00149
00150
00151 void SatOrbitStore::writeSP3File(const std::string& filename,bool sp3c)
00152 {
00153 try
00154 {
00155
00156 SP3Stream strm(filename.c_str(),std::ios::out);
00157 if (!strm)
00158 {
00159 FileMissingException e("File " +filename+ " could not be opened.");
00160 GPSTK_THROW(e);
00161 }
00162
00163
00164
00165 SP3Header header;
00166
00167 header.version = sp3c ? 'c' : 'a';
00168 header.pvFlag = 'V';
00169
00170 header.timeSystem = SP3Header::timeGPS;
00171 header.coordSystem = "ITRF";
00172
00173 header.time = DayTime(2010,1,10,23,59,0.0);
00174 header.epochInterval = 60.0;
00175 header.numberOfEpochs = 1443;
00176
00177 header.dataUsed = "ORBIT";
00178 header.orbitType = "FIT";
00179 header.agency = "IGG";
00180
00181 header.basePV = 0.0;
00182 header.baseClk = 0.0;
00183
00184 header.comments.clear();
00185 header.comments.push_back("POD Solutions of "+ orbit + " by PhDSoft");
00186 header.comments.push_back("EMail: yanweigps@hotmail.com");
00187 header.comments.push_back("QQ : 269358547");
00188 header.comments.push_back("NO PAIN, NO GAIN.");
00189
00190 for(SvEphMap::iterator it = pe.begin();
00191 it != pe.end();
00192 ++it)
00193 {
00194 header.satList[it->first] = 1;
00195 }
00196
00197 strm << header;
00198
00199
00200
00201
00202 SatID sv(05,SatID::systemLEO);
00203 SvEphMap::iterator it0 = pe.begin();
00204 if(it0 != pe.end())
00205 {
00206 sv = it0->first;
00207 }
00208 else
00209 {
00210 return;
00211 }
00212
00213
00214 PvtStore::EpochList epochList = it0->second.epochList();
00215 for(PvtStore::EpochList::iterator it = epochList.begin();
00216 it != epochList.end();
00217 ++it)
00218 {
00219 ostringstream ss;
00220 ss<<fixed;
00221 ss <<"* "
00222 <<setw(4)<<(*it).year()<<" "
00223 <<setw(2)<<(*it).month()<<" "
00224 <<setw(2)<<(*it).day()<<" "
00225 <<setw(2)<<(*it).hour()<<" "
00226 <<setw(2)<<(*it).minute()<<" "
00227 <<setw(11)<<setprecision(8)<<(*it).second()<<endl;
00228
00229 strm<<ss.str();
00230
00231 for(std::map<SatID,short>::iterator its = header.satList.begin();
00232 its != header.satList.end();
00233 ++its)
00234 {
00235 PvtStore& svEph = pe[its->first];
00236 PvtStore::Pvt eph = svEph.getPvt(*it);
00237
00238 SP3Data rec;
00239 rec.sat = its->first;
00240 rec.time = *it;
00241 rec.version = header.version;
00242 rec.sig[0]=0; rec.sig[1]=0; rec.sig[2]=0; rec.sig[3]=0;
00243
00244 SP3Data p(rec);
00245 p.flag = 'P';
00246 p.x[0] = eph.position[0]/1000.0;
00247 p.x[1] = eph.position[1]/1000.0;
00248 p.x[2] = eph.position[2]/1000.0;
00249 p.clk = eph.dtime * 1e6;
00250
00251 SP3Data v(rec);
00252 v.flag = 'V';
00253 v.x[0] = eph.velocity[0]*10.0;
00254 v.x[1] = eph.velocity[1]*10.0;
00255 v.x[2] = eph.velocity[2]*10.0;
00256 v.clk = eph.ddtime * 1e10;
00257
00258 strm << p;
00259 strm << v;
00260 }
00261
00262 }
00263
00264 strm<<"EOF"<<endl;
00265
00266
00267
00268
00269 strm.close();
00270 }
00271 catch (gpstk::Exception& e)
00272 {
00273 GPSTK_RETHROW(e);
00274 }
00275
00276 }
00277
00278
00279 void SatOrbitStore::loadGNV1BFile(const std::string& filename)
00280 {
00281 try
00282 {
00283
00284 ifstream fin(filename.c_str());
00285 if(fin.bad())
00286 {
00287 FileMissingException e("Failed to open file: "+filename);
00288 }
00289
00290 string buf;
00291
00292
00293
00294 DayTime refEpoch;
00295 double firstObsSec(0.0);
00296 double lastObsSec(0.0);
00297 string satelliteName = "GRACE ?";
00298
00299 int hdrLineCounter(0);
00300 while(getline(fin,buf))
00301 {
00302 string flag = StringUtils::strip(buf.substr(0,30));
00303 string data = StringUtils::strip(buf.substr(32));
00304
00305 if(flag == "PRODUCER AGENCY")
00306 {
00307 string agency = data;
00308 }
00309 else if(flag == "PRODUCER INSTITUTION")
00310 {
00311 string institution = data;
00312 }
00313 else if(flag == "FILE TYPE ipGNV1BF")
00314 {
00315 int fileType = StringUtils::asInt(data);
00316 if(fileType != 5)
00317 {
00318 Exception e("It's NOT a GNV1B file: " + filename);
00319 GPSTK_THROW(e);
00320 }
00321 }
00322 else if(flag == "FILE FORMAT 0=BINARY 1=ASCII")
00323 {
00324 int fileFormat = StringUtils::asInt(data);
00325 }
00326 else if(flag == "NUMBER OF HEADER RECORDS")
00327 {
00328 int numberHeaderRecord = StringUtils::asInt(data);
00329 }
00330 else if(flag == "SATELLITE NAME")
00331 {
00332 satelliteName = data;
00333 }
00334 else if(flag == "SENSOR NAME")
00335 {
00336 string sensorName = data;
00337 }
00338 else if(flag == "TIME EPOCH (GPS TIME)")
00339 {
00340
00341 short yr = StringUtils::asInt(data.substr(0,4));
00342 short mo = StringUtils::asInt(data.substr(5,2));
00343 short day = StringUtils::asInt(data.substr(8,2));
00344 short hr = StringUtils::asInt(data.substr(11,2));
00345 short min = StringUtils::asInt(data.substr(14,2));
00346 double sec = StringUtils::asDouble(data.substr(17,2));
00347
00348 refEpoch.setYMDHMS(yr,mo,day,hr,min,sec);
00349 }
00350 else if(flag == "TIME FIRST OBS(SEC PAST EPOCH)")
00351 {
00352 firstObsSec = StringUtils::asDouble(data.substr(0,16));
00353 }
00354 else if(flag == "TIME LAST OBS(SEC PAST EPOCH)")
00355 {
00356 lastObsSec = StringUtils::asDouble(data.substr(0,16));
00357 }
00358 else if(flag == "NUMBER OF DATA RECORDS")
00359 {
00360 int numberDataRecord = StringUtils::asInt(data);
00361 }
00362 else
00363 {
00364
00365 }
00366
00367
00368 if(StringUtils::strip(buf)=="END OF HEADER")
00369 {
00370 DayTime firstEpoch = refEpoch;
00371 firstEpoch += firstObsSec;
00372
00373 DayTime lastEpoch = refEpoch;
00374 lastEpoch += lastObsSec;
00375
00376 break;
00377 }
00378
00379 hdrLineCounter++;
00380 }
00381
00382
00383
00384
00385
00386 int recLineCounter(0);
00387 while(getline(fin,buf))
00388 {
00389 if(buf.length() <= 200) continue;
00390
00391 istringstream ss(buf);
00392
00393 long gps_time;
00394 char grace_id, coord_ref;
00395 double xpos,ypos,zpos;
00396 double xvel,yvel,zvel;
00397
00398 double temp;
00399
00400 ss >> gps_time >> grace_id >> coord_ref
00401 >> xpos >> ypos >> zpos >> temp >> temp >> temp
00402 >> xvel >> yvel >> zvel >> temp >> temp >> temp;
00403
00404 DayTime epoch = refEpoch;
00405 epoch += gps_time;
00406
00407
00408 PvtStore::Pvt eph( Triple(xpos,ypos,zpos),
00409 Triple(xvel,yvel,zvel),
00410 0.0,
00411 0.0);
00412
00413 int prn = (StringUtils::lowerCase(satelliteName)=="grace a") ? 5 : 6;
00414 SatID sv(prn,SatID::systemLEO);
00415
00416 if( pe.find(sv) == pe.end())
00417 {
00418 pe.insert(pair<SatID,PvtStore>(sv,PvtStore(satelliteName,PvtStore::ITRF)));
00419 }
00420
00421 PvtStore& svEph = pe[sv];
00422 svEph.addPvt(epoch,eph);
00423
00424 recLineCounter++;
00425 }
00426
00427
00428
00429
00430 fin.close();
00431 }
00432 catch (gpstk::Exception& e)
00433 {
00434 GPSTK_RETHROW(e);
00435 }
00436
00437 }
00438
00439
00440
00441 PvtStore::Pvt SatOrbitStore::getPvt(const SatID sat,
00442 const DayTime& t,
00443 bool j2k )
00444 throw(InvalidRequest)
00445 {
00446 SvEphMap::iterator svit = pe.find(sat);
00447 if(svit == pe.end())
00448 {
00449 InvalidRequest e("Data for satellite " + StringUtils::asString(sat)
00450 + " not found.");
00451 GPSTK_THROW(e);
00452 }
00453
00454 PvtStore& pvs = pe[svit->first];
00455 PvtStore::Pvt eph = pvs.getPvt(t);
00456
00457 if(j2k)
00458 {
00459 DayTime gpst(t),utc;
00460 GPST2UTC(t,utc);
00461
00462 Vector<double> ecefPosVel(6,0.0), j2kPosVel(6,0.0);
00463
00464 for(int i=0;i<3;i++)
00465 {
00466 ecefPosVel[i] = eph.position[i];
00467 ecefPosVel[i+3] = eph.velocity[i];
00468 }
00469
00470 j2kPosVel = ReferenceFrames::ECEFPosVelToJ2k(UTCTime(utc),ecefPosVel);
00471
00472 for(int i=0;i<3;i++)
00473 {
00474 eph.position[i] = j2kPosVel[i];
00475 eph.velocity[i] = j2kPosVel[i+3];
00476 }
00477
00478 }
00479
00480
00481 return eph;
00482
00483 }
00484
00485
00486
00487 PvtStore::EpochList SatOrbitStore::epochList(const SatID sat)
00488 {
00489 SvEphMap::iterator it = pe.find(sat);
00490 if(it != pe.end())
00491 {
00492 return it->second.epochList();
00493 }
00494 else
00495 {
00496 return PvtStore::EpochList();
00497 }
00498
00499 }
00500
00501
00502 void SatOrbitStore::keepOnly(const SatID sat)
00503 {
00504 for(SvEphMap::iterator it = pe.begin();
00505 it != pe.end();
00506 ++it)
00507 {
00508 if(it->first != sat) pe.erase(it);
00509 }
00510 }
00511
00512
00513 void SatOrbitStore::deleteOnly(const SatID sat)
00514 {
00515 SvEphMap::iterator it = pe.find(sat);
00516 if(it != pe.end()) pe.erase(it);
00517 }
00518
00519 void SatOrbitStore::test()
00520 {
00521
00522
00523
00524
00525 loadSP3File("graceab.sp3");
00526 writeSP3File("graceab2.sp3",true);
00527
00528 }
00529
00530 }