| 31 |
#include <varDump/VarDumpRecord.h> |
#include <varDump/VarDumpRecord.h> |
| 32 |
#include <physics/S4/S4Event.h> |
#include <physics/S4/S4Event.h> |
| 33 |
// |
// |
| 34 |
|
#include <cTle.h> |
| 35 |
|
#include <cEci.h> |
| 36 |
|
#include <cJulian.h> |
| 37 |
|
|
| 38 |
#include <PamelaDBOperations.h> |
#include <PamelaDBOperations.h> |
| 39 |
// |
// |
| 40 |
using namespace std; |
using namespace std; |
| 41 |
using namespace pamela; |
using namespace pamela; |
| 42 |
|
|
| 43 |
|
// Some function to work with cTle stuff. |
| 44 |
|
bool compTLE(cTle* tle1, cTle *tle2); |
| 45 |
|
float getTleJulian(cTle *); |
| 46 |
|
string getTleDatetime(cTle*); |
| 47 |
|
|
| 48 |
/** |
/** |
| 49 |
* Constructor. |
* Constructor. |
| 50 |
* @param host hostname for the SQL connection. |
* @param host hostname for the SQL connection. |
| 56 |
* @param obt0 file obt0. |
* @param obt0 file obt0. |
| 57 |
* @param tsync file timesync. |
* @param tsync file timesync. |
| 58 |
* @param debug debug flag. |
* @param debug debug flag. |
| 59 |
|
* @param tlefilename ascii file with TLE 3 line elements. |
| 60 |
*/ |
*/ |
| 61 |
PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){ |
PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename){ |
| 62 |
// |
// |
| 63 |
// |
// |
| 64 |
SetConnection(host,user,password); |
SetConnection(host,user,password); |
| 72 |
SetTsync(tsync); |
SetTsync(tsync); |
| 73 |
SetObt0(obt0); |
SetObt0(obt0); |
| 74 |
// |
// |
| 75 |
|
SetTLEPath(tlefilename); |
| 76 |
|
// |
| 77 |
// |
// |
| 78 |
INSERT_RAW =!filerawname.IsNull(); |
INSERT_RAW =!filerawname.IsNull(); |
| 79 |
if(INSERT_RAW)SetRawName(filerawname); |
if(INSERT_RAW)SetRawName(filerawname); |
| 202 |
}; |
}; |
| 203 |
|
|
| 204 |
/** |
/** |
| 205 |
|
* Store path to the TLE file. |
| 206 |
|
*/ |
| 207 |
|
void PamelaDBOperations::SetTLEPath(TString str){ |
| 208 |
|
tlefilename = str; |
| 209 |
|
}; |
| 210 |
|
|
| 211 |
|
/** |
| 212 |
* Store the olderthan variable |
* Store the olderthan variable |
| 213 |
* @param olderthan |
* @param olderthan |
| 214 |
*/ |
*/ |
| 3246 |
// |
// |
| 3247 |
row = result->Next(); |
row = result->Next(); |
| 3248 |
// |
// |
| 3249 |
if ( row ){ |
while ( row ){ |
| 3250 |
// |
// |
| 3251 |
oss.str(""); |
oss.str(""); |
| 3252 |
oss << " ID= "<< row->GetField(0); |
oss << " ID= "<< row->GetField(0); |
| 3323 |
// |
// |
| 3324 |
if ( !result2 ) throw -4; |
if ( !result2 ) throw -4; |
| 3325 |
// |
// |
| 3326 |
|
row = result->Next(); |
| 3327 |
}; |
}; |
| 3328 |
}; |
}; |
| 3329 |
if ( IsDebug() ) printf(" Moved %u runs\n",moved); |
if ( IsDebug() ) printf(" Moved %u runs\n",moved); |
| 3558 |
|
|
| 3559 |
|
|
| 3560 |
|
|
| 3561 |
|
// Insert TLEs from file tlefilename in the table GL_TLE in the db |
| 3562 |
|
// opened by conn, sorting them by date from older to newer, if each |
| 3563 |
|
// TLE has not been alread inserted. |
| 3564 |
|
Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile) |
| 3565 |
|
{ |
| 3566 |
|
fstream tlefile(tlefilename, ios::in); |
| 3567 |
|
|
| 3568 |
|
if ( !tlefile ) throw -7; |
| 3569 |
|
|
| 3570 |
|
vector<cTle*> ctles; |
| 3571 |
|
vector<cTle*>::iterator iter; |
| 3572 |
|
int present = 0; |
| 3573 |
|
|
| 3574 |
|
// Get three lines from tlefile, create a cTle object and put it |
| 3575 |
|
// into ctles |
| 3576 |
|
while(1) { |
| 3577 |
|
cTle *tlef; |
| 3578 |
|
string str1, str2, str3; |
| 3579 |
|
|
| 3580 |
|
getline(tlefile, str1); |
| 3581 |
|
if(tlefile.eof()) break; |
| 3582 |
|
|
| 3583 |
|
getline(tlefile, str2); |
| 3584 |
|
if(tlefile.eof()) break; |
| 3585 |
|
|
| 3586 |
|
getline(tlefile, str3); |
| 3587 |
|
if(tlefile.eof()) break; |
| 3588 |
|
|
| 3589 |
|
// We now have three good lines for a cTle. |
| 3590 |
|
tlef = new cTle(str1, str2, str3); |
| 3591 |
|
ctles.push_back(tlef); |
| 3592 |
|
} |
| 3593 |
|
|
| 3594 |
|
tlefile.close(); |
| 3595 |
|
|
| 3596 |
|
// Sort by date |
| 3597 |
|
sort(ctles.begin(), ctles.end(), compTLE); |
| 3598 |
|
|
| 3599 |
|
// Now we insert each TLE into the db |
| 3600 |
|
for(iter = ctles.begin(); iter != ctles.end(); iter++) { |
| 3601 |
|
cTle *tle = *iter; |
| 3602 |
|
|
| 3603 |
|
// Do nothing if it's already present in the db. Just increase |
| 3604 |
|
// the counter present. |
| 3605 |
|
if (! isTlePresent(tle)) |
| 3606 |
|
{ |
| 3607 |
|
int status = insertTle(tle); |
| 3608 |
|
|
| 3609 |
|
// Insert query failed. Return 1. |
| 3610 |
|
if(status == EXIT_FAILURE) { |
| 3611 |
|
|
| 3612 |
|
if( IsDebug() ) { |
| 3613 |
|
cerr << "Error: inserting TLE:" << endl |
| 3614 |
|
<< tle->getName() << endl |
| 3615 |
|
<< tle->getLine1() << endl |
| 3616 |
|
<< tle->getLine2() << endl; |
| 3617 |
|
} |
| 3618 |
|
|
| 3619 |
|
throw -4; |
| 3620 |
|
return 1; |
| 3621 |
|
} |
| 3622 |
|
|
| 3623 |
|
} |
| 3624 |
|
else |
| 3625 |
|
present++; |
| 3626 |
|
|
| 3627 |
|
} |
| 3628 |
|
|
| 3629 |
|
int inserted = ctles.size() - present; // Number of inserted TLE. |
| 3630 |
|
if ( IsDebug() ) |
| 3631 |
|
cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl |
| 3632 |
|
<< inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl; |
| 3633 |
|
|
| 3634 |
|
ctles.clear(); |
| 3635 |
|
|
| 3636 |
|
|
| 3637 |
|
// Return 2 if no new TLE has been inserted. 0 otherwise. |
| 3638 |
|
if(! inserted ) return 2; |
| 3639 |
|
return 0; |
| 3640 |
|
} |
| 3641 |
|
|
| 3642 |
|
|
| 3643 |
|
// Insert tle in the table GL_TLE using the connection conn. |
| 3644 |
|
int PamelaDBOperations::insertTle(cTle *tle) |
| 3645 |
|
{ |
| 3646 |
|
stringstream oss; |
| 3647 |
|
TSQLResult *result = 0; |
| 3648 |
|
|
| 3649 |
|
oss.str(""); |
| 3650 |
|
oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)" |
| 3651 |
|
<< " VALUES ( '" |
| 3652 |
|
<< tle->getName() << "', '" |
| 3653 |
|
<< tle->getLine1() << "', '" |
| 3654 |
|
<< tle->getLine2() << "', '" |
| 3655 |
|
<< getTleDatetime(tle) << "')"; |
| 3656 |
|
|
| 3657 |
|
// cout << oss.str().c_str() << endl; |
| 3658 |
|
result = conn->Query(oss.str().c_str()); |
| 3659 |
|
if (result == NULL) |
| 3660 |
|
return EXIT_FAILURE; |
| 3661 |
|
|
| 3662 |
|
return EXIT_SUCCESS; |
| 3663 |
|
} |
| 3664 |
|
|
| 3665 |
|
|
| 3666 |
|
// Return whether tle is already in the db connected by conn. |
| 3667 |
|
bool PamelaDBOperations::isTlePresent(cTle *tle) |
| 3668 |
|
{ |
| 3669 |
|
stringstream oss; |
| 3670 |
|
TSQLResult *result = 0; |
| 3671 |
|
|
| 3672 |
|
oss.str(""); |
| 3673 |
|
oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '" |
| 3674 |
|
<< getTleDatetime(tle) << "'"; |
| 3675 |
|
|
| 3676 |
|
result = conn->Query(oss.str().c_str()); |
| 3677 |
|
if (result == NULL) throw -4; |
| 3678 |
|
|
| 3679 |
|
if (result->GetRowCount()) |
| 3680 |
|
return true; |
| 3681 |
|
else |
| 3682 |
|
return false; |
| 3683 |
|
} |
| 3684 |
|
|
| 3685 |
|
|
| 3686 |
|
// Return whether the first TLE is dated early than the second |
| 3687 |
|
bool compTLE (cTle *tle1, cTle *tle2) |
| 3688 |
|
{ |
| 3689 |
|
return getTleJulian(tle1) < getTleJulian(tle2); |
| 3690 |
|
} |
| 3691 |
|
|
| 3692 |
|
|
| 3693 |
|
// Return the date of the tle using the format (year-2000)*1e3 + |
| 3694 |
|
// julian day. e.g. 6365 is the 31th Dec 2006. |
| 3695 |
|
// It does *not* return a cJulian date. |
| 3696 |
|
float getTleJulian(cTle *tle) { |
| 3697 |
|
return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY); |
| 3698 |
|
} |
| 3699 |
|
|
| 3700 |
|
|
| 3701 |
|
// Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime |
| 3702 |
|
// format. |
| 3703 |
|
string getTleDatetime(cTle *tle) |
| 3704 |
|
{ |
| 3705 |
|
int year, mon, day, hh, mm, ss; |
| 3706 |
|
double dom; // day of month (is double!) |
| 3707 |
|
stringstream date; // date in datetime format |
| 3708 |
|
|
| 3709 |
|
// create a cJulian from the date in tle |
| 3710 |
|
cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY)); |
| 3711 |
|
|
| 3712 |
|
// get year, month, day of month |
| 3713 |
|
jdate.getComponent(&year, &mon, &dom); |
| 3714 |
|
|
| 3715 |
|
// build a datetime YYYY-MM-DD hh:mm:ss |
| 3716 |
|
date.str(""); |
| 3717 |
|
day = (int) floor(dom); |
| 3718 |
|
hh = (int) floor( (dom - day) * 24); |
| 3719 |
|
mm = (int) floor( ((dom - day) * 24 - hh) * 60); |
| 3720 |
|
ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60)); |
| 3721 |
|
// ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000); |
| 3722 |
|
|
| 3723 |
|
date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss; |
| 3724 |
|
|
| 3725 |
|
return date.str(); |
| 3726 |
|
} |