--- YodaProfiler/src/GLTables.cpp 2006/11/09 12:02:54 1.17 +++ YodaProfiler/src/GLTables.cpp 2007/01/23 17:04:11 1.21 @@ -161,9 +161,6 @@ TYPE = 0; } -GL_TLE::GL_TLE(){ -} - // **************************************************** void GL_RUN::SetEV_FROM(UInt_t evfrom){ @@ -1309,9 +1306,8 @@ // stringstream oss; oss.str(""); - oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='" + oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='" << rname.str().c_str() << "';"; - // if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); pResult = dbc->Query(oss.str().c_str()); Row = pResult->Next(); // @@ -1332,13 +1328,11 @@ }; if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi(); }; - // if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data()); // if ( !Row ){ oss.str(""); - oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " + oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " << dworbit << " order by FROM_ORBIT desc limit 1;"; - // if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); pResult = dbc->Query(oss.str().c_str()); Row = pResult->Next(); if ( !Row ){ @@ -1347,9 +1341,7 @@ }; }; // - // - TDatime ti = TDatime(Row->GetField(0)); - TTimeStamp tu = TTimeStamp((UInt_t)ti.GetYear(),(UInt_t)ti.GetMonth(),(UInt_t)ti.GetDay(),(UInt_t)ti.GetHour(),(UInt_t)ti.GetMinute(),(UInt_t)ti.GetSecond(),0,true,0); + TTimeStamp tu = TTimeStamp((UInt_t)atoi(Row->GetField(0)),(UInt_t)atoi(Row->GetField(1)),(UInt_t)atoi(Row->GetField(2)),(UInt_t)atoi(Row->GetField(3)),(UInt_t)atoi(Row->GetField(4)),(UInt_t)atoi(Row->GetField(5)),0,true,0); T0 = (UInt_t)tu.GetSec(); // // look for the correct timesync entry @@ -1391,43 +1383,76 @@ */ UInt_t GL_TIMESYNC::ResursTime(UInt_t OBT){ // - return(((UInt_t)((this->DBobt(OBT)-this->DBobt(OBT0))/1000)+TIMESYNC)); + return(((UInt_t)((Int_t)(this->DBobt(OBT)-this->DBobt(OBT0))/1000)+TIMESYNC)); // }; -ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){ +/** + * Return the correct packet number if we went back to zero + */ +Long64_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){ // - if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((ULong64_t)(obt+numeric_limits::max())); + if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ){ + return((Long64_t)pkt_num+16777215LL); + }; // - if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ - if ( (obt-numeric_limits::max()) < 0 ){ - return((ULong64_t)(numeric_limits::max()-obt)); - } else { - return((ULong64_t)(obt-numeric_limits::max())); - }; + if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){ + return((Long64_t)pkt_num-16777215LL); }; // - return((ULong64_t)obt); + return((Long64_t)pkt_num); // }; -UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){ +/** + * Return the correct On Board Time if we went back to zero + */ +Long64_t GL_TIMESYNC::DBobt(UInt_t obt){ // - if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ) return((pkt_num+16777215)); + if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((Long64_t)(obt+numeric_limits::max())); // - if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){ - if ( (pkt_num-16777215) < 0 ){ - return((16777215-pkt_num)); - } else { - return((pkt_num-16777215)); - }; + if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ + return((Long64_t)obt-(Long64_t)numeric_limits::max()); }; // - return(pkt_num); - // + return((Long64_t)obt); }; + + +// ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){ +// // +// if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((ULong64_t)(obt+numeric_limits::max())); +// // +// if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ +// if ( (obt-numeric_limits::max()) < 0 ){ +// return((ULong64_t)(numeric_limits::max()-obt)); +// } else { +// return((ULong64_t)(obt-numeric_limits::max())); +// }; +// }; +// // +// return((ULong64_t)obt); +// // +// }; + +// UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){ +// // +// if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ) return((pkt_num+16777215)); +// // +// if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){ +// if ( (pkt_num-16777215) < 0 ){ +// return((16777215-pkt_num)); +// } else { +// return((pkt_num-16777215)); +// }; +// }; +// // +// return(pkt_num); +// // +// }; + /* * * Convert the time in the DB from UInt_t to a string @@ -1545,84 +1570,111 @@ } -// **************************************************** -/** - * Function to query the GL_TLE table of the DB. - * - * time is the unix time for which a good tle is requested. - * - * Return the pointer to a cTle object that has the closest and - * previous date compared with time. - * - * If errors occurs it returns NULL. - */ -cTle* GL_TLE::Query_GL_TLE(UInt_t time, TSQLServer *dbc){ +// +// Build a query and call DoQuery. +// +// date it's an SQL datetime date and dbc is the connection to be +// used. It will query for the tle with the nearest but previous date +// and the immediatly next one. +// +Int_t GL_TLE::Query(TString date, TSQLServer *dbc){ stringstream myquery; myquery.str(""); - myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE " - << "WHERE FROM_TIME < FROM_UNIXTIME(" << time << ") ORDER BY FROM_TIME DESC LIMIT 1;"; - + myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME <= '" << date.Data() + << "' ORDER BY FROM_TIME DESC LIMIT 1) " + << "UNION " + << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME > '" << date.Data() + << "' ORDER BY FROM_TIME ASC LIMIT 1)"; - return Query_GL_TLE_go(myquery.str(), dbc); + return DoQuery(myquery.str().c_str(), dbc); } -// **************************************************** -/** - * Function to query the GL_TLE table of the DB. - * - * date is a datetime format YYYY-MM-DD hh:mm:ss for which a good tle - * is requested. - * - * Return the pointer to a cTle object that has the closest and - * previous date compared with time. - * - * If errors occurs it returns NULL. - */ -cTle* GL_TLE::Query_GL_TLE(TString date, TSQLServer *dbc){ +// +// Build a query and call DoQuery. +// +// time is the UTC date in unix time (UTC) and dbc is the connection +// to be used. It will query for the tle with the nearest but +// previous date and the immediatly next one. +// +// Returns the value returned by DoQuery(). +// +Int_t GL_TLE::Query(UInt_t time, TSQLServer *dbc){ stringstream myquery; myquery.str(""); - myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE " - << "WHERE FROM_TIME < '" << date.Data() << "' ORDER BY FROM_TIME DESC LIMIT 1;"; - + myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME <= FROM_UNIXTIME('" << time + << "') ORDER BY FROM_TIME DESC LIMIT 1) " + << "UNION " + << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME > FROM_UNIXTIME('" << time + << "') ORDER BY FROM_TIME ASC LIMIT 1)"; - return Query_GL_TLE_go(myquery.str(), dbc); + return DoQuery(myquery.str().c_str(), dbc); } -// **************************************************** -/** - * Private function used by Query_GL_TLE methods. - * - * myquery is the query string. - * - * Return the pointer to a cTle object that has the closest and - * previous date compared with time. - * - * If errors occurs it returns NULL. - */ -cTle* GL_TLE::Query_GL_TLE_go(TString myquery, TSQLServer *dbc){ - cTle *tle; - string tle1, tle2, tle3; - - // MySQL variables +// +// Do the query myquery on the connectio dbc. Initialize tle, +// tleFromTime and tleToTime. +// +// We should have two rows (unless the table is old). From the last +// one we only take tleToTime. +// +// Returns 0 for success, 1 for failure. +// +Int_t GL_TLE::DoQuery(TString myquery, TSQLServer *dbc){ TSQLResult *result; TSQLRow *row; + // Set the right time_zone (otherwise horrible things will occur! :) + dbc->Query("SET time_zone = '+0:00'"); + + // Do the query result = dbc->Query(myquery.Data()); - if(! result->GetRowCount() ) return NULL; + if(! result->GetRowCount() ) { + cerr << "GL_TLE: query failed: " << myquery.Data() << endl; + return 1; + } + + // Get results + row = result->Next(); // first tle row + tle = GiveTle(row); + + tleFromTime = strtol(row->GetField(4), NULL, 10); + + row = result->Next(); // second tle row + if(row) + tleToTime = strtol(row->GetField(4), NULL, 10); + else { + cerr << "GL_TLE: Warning: using last avaible TLE. Please update GL_TLE table!\n"; + tleToTime = UINT_MAX; + } - row = result->Next(); - tle1 = row->GetField(0); - tle2 = row->GetField(1); - tle3 = row->GetField(2); + delete row; + delete result; - tle = new cTle(tle1, tle2, tle3); + return 0; +} - delete result; - delete row; - return tle; +// +// Build a cTle object from the GL_TLE row. +// +cTle* GL_TLE::GiveTle(TSQLRow *row) { + cTle *thistle = NULL; + string tle1, tle2, tle3; + + // Build cTle object + tle1 = row->GetField(1); + tle2 = row->GetField(2); + tle3 = row->GetField(3); + + thistle = new cTle(tle1, tle2, tle3); + + return thistle; }