Changeset 10618 for trunk/FACT++/src
- Timestamp:
- 05/06/11 13:30:10 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/scheduler.cc
r10612 r10618 23 23 #include <vector> 24 24 25 namespace ba = boost::asio;26 namespace bs = boost::system;27 28 using ba::deadline_timer;29 using ba::ip::tcp;30 31 using namespace std;32 33 34 25 #include "LocalControl.h" 35 26 #include <boost/date_time/posix_time/posix_time.hpp> 36 27 #include <mysql++/mysql++.h> 28 29 30 namespace ba = boost::asio; 31 namespace bs = boost::system; 32 33 using ba::deadline_timer; 34 using ba::ip::tcp; 35 36 using namespace std; 37 using namespace boost::gregorian; 38 using namespace boost::posix_time; 39 37 40 38 41 // string containing database information … … 52 55 // other things to do 53 56 // 54 // define what is error and warning57 // define what to transmit as info/warn/error 55 58 56 59 … … 62 65 63 66 // missing: 64 // from / to for the time range for which the scheduling shall be done65 67 // 66 68 // calculate time for std obs … … 71 73 // return errors 72 74 // get input from sendcommand 75 76 // in which cases should the scheduler go in error state? 77 // when db is unavailable 78 // does one also need a 'set scheduler to ready' function then? 79 // do we want any error state at all? 80 73 81 74 82 // ========================================================================= … … 170 178 int Schedule() 171 179 { 180 bool error = false; 181 172 182 boost::posix_time::time_duration runtimec(0, 3, 0); 173 183 boost::posix_time::time_duration runtimep(0, 3, 0); 174 184 boost::posix_time::time_duration mintime(1, 0, 0); 175 185 boost::posix_time::time_duration repostime(0, 5, 0); 186 //boost::posix_time::ptime startsched=microsec_clock::local_time(); 187 boost::posix_time::ptime startsched(microsec_clock::local_time()); 188 boost::posix_time::ptime stopsched=startsched+years(1); 189 cout << "Scheduling for the period from " << startsched << " to " << stopsched << endl; 176 190 177 191 stringstream str; … … 246 260 int counter2=0; 247 261 int counter3=0; 262 cout << "Obs: <obskey> <sourcename>(<sourcekey>, <fluxweight>) from <starttime> to <stoptime>" << endl; 248 263 for (vector<mysqlpp::Row>::iterator v=res.begin(); v<res.end(); v++) 249 264 { 250 cout << "Obskey: " << (*v)[0].c_str() << " source: " << (*v)[4].c_str() << "(" << (*v)[5].c_str() << ")" << flush; 251 cout << " T1 " << (*v)[1].c_str() << " T2: " << (*v)[2].c_str() << " (c " << counter << " " << counter2 << ")" << endl; 252 253 /* 254 cout << " 0 " << (*v)[0].c_str() << endl; 255 cout << " 1 " << (*v)[1].c_str() << endl; 256 cout << " 2 " << (*v)[2].c_str() << endl; 257 cout << " 3 " << (*v)[3].c_str() << endl; 258 cout << " 4 " << (*v)[4].c_str() << endl; 259 cout << " 5 " << (*v)[5].c_str() << endl; 260 cout << " 6 " << (*v)[6].c_str() << endl; 261 cout << " 7 " << (*v)[7].c_str() << endl; 262 cout << " 8 " << (*v)[8].c_str() << endl; 263 cout << " 9 " << (*v)[9].c_str() << endl; 264 cout << " 10 " << (*v)[10].c_str() << endl; 265 cout << " 11 " << (*v)[11].c_str() << endl; 266 cout << " 12 " << (*v)[12].c_str() << endl; 267 cout << " 13 " << (*v)[13].c_str() << endl; 268 cout << " 14 " << (*v)[14].c_str() << endl; 269 */ 265 cout << " Obs: " << (*v)[0].c_str() << " " << (*v)[4].c_str() << "(" << (*v)[5].c_str() << flush; 266 cout << ", " << (*v)[7].c_str() << ")" << flush; 267 cout << " from " << (*v)[1].c_str() << " to " << (*v)[2].c_str() << endl; 268 270 269 //0: obskey 271 270 //1: startime … … 300 299 if (!(*v)[0].is_null()) 301 300 olist[counter].obskey=(*v)[0]; 302 //else303 // cout << "no obskey" << endl;304 301 if (!(*v)[12].is_null()) 305 302 olist[counter].obsmode=(*v)[12]; 306 //else307 // cout << "no obsmode" << endl;308 303 if (!(*v)[13].is_null()) 309 304 olist[counter].obstype=(*v)[13]; 310 //else311 // cout << "no obstype" << endl;312 305 if (!(*v)[14].is_null()) 313 306 olist[counter].telsetup=(*v)[14]; 314 //else315 // cout << "no telsetup" << endl;316 307 if (!(*v)[6].is_null()) 317 308 olist[counter].splitflag=(*v)[6]; 318 //else319 //{320 // cout << "no splitflag" << endl;321 ////stringstream str;322 ////str << "no splitflag";323 //T::Message("no splitflag");324 //T::Error("---> no splitflag");325 //T::Warn("+++ no splitflag");326 //}327 309 if (!(*v)[7].is_null()) 328 310 olist[counter].fluxweight=(*v)[7]; 329 311 else 330 { 331 olist[counter].fluxweight=0; 332 // cout << "no fluxweight" << endl; 333 } 312 olist[counter].fluxweight=0;//set fluxweight to 0 for check below 334 313 if (!(*v)[8].is_null()) 335 314 olist[counter].slope=(*v)[8]; 336 //else337 // cout << "no slope" << endl;338 315 if (!(*v)[9].is_null()) 339 316 olist[counter].flux=(*v)[9]; 340 //else341 // cout << "no flux" << endl;342 317 if (!(*v)[10].is_null()) 343 318 olist[counter].ra=(*v)[10]; 344 //else345 // cout << "no ra" << endl;346 319 if (!(*v)[11].is_null()) 347 320 olist[counter].dec=(*v)[11]; 348 //else349 // cout << "no dec" << endl;350 321 351 322 // time_duration cannot be used, as only up to 99 hours are handeled … … 428 399 counter++; 429 400 } 401 stringstream fixedobsmsg; 402 fixedobsmsg << obsfixedlist.size() << " fixed observations found. "; 403 T::Message(fixedobsmsg); 430 404 cout << obsfixedlist.size() << " fixed observations found. " << endl; 405 406 stringstream stdobsmsg; 407 stdobsmsg << obsstdlist.size() << " standard observations found. "; 408 T::Message(stdobsmsg); 431 409 cout << obsstdlist.size() << " standard observations found. " << endl; 432 410 433 411 // loop to add the fixed observations to the ScheduledObs list 434 // missing: checks and evaluation 435 // * check for sun 436 // * check for moon 412 // performed checks: 413 // * overlap of fixed observations: the overlap is split half-half 414 // * check for scheduling time range: only take into account fixed obs within the range 415 // missing checks and evaluation 416 // * check for mintime (pb with overlap checks) 417 // * check for sun 418 // * check for moon 437 419 counter2=0; 420 int skipcounter=0; 438 421 boost::posix_time::ptime finalobsfixedstart; 439 422 boost::posix_time::ptime finalobsfixedstop; … … 446 429 for (vobs=obsfixedlist.begin(); vobs!=obsfixedlist.end(); vobs++) 447 430 { 431 if (obsfixedlist[counter2].obsfixedstart < startsched 432 || obsfixedlist[counter2].obsfixedstop > stopsched) 433 { 434 stringstream skipfixedobsmsg; 435 skipfixedobsmsg << "Skip 1 fixed observation (obskey " << obsfixedlist[counter2].obskey_fixed << ") as it is out of scheduling time range."; 436 T::Message(skipfixedobsmsg); 437 counter2++; 438 skipcounter++; 439 continue; 440 } 448 441 counter3=0; 449 442 delta1=delta0; … … 451 444 finalobsfixedstart=obsfixedlist[counter2].obsfixedstart; 452 445 finalobsfixedstop=obsfixedlist[counter2].obsfixedstop; 453 //cout << "final: " << finalobsfixedstart << " - " << finalobsfixedstop << endl;454 446 455 447 for (vobs5=obsfixedlist.begin(); vobs5!=obsfixedlist.end(); vobs5++) 456 448 { 457 //cout << "startc" << obsfixedlist[counter2].obsfixedstart << " -- " << flush;458 //cout << "stopc" << obsfixedlist[counter2].obsfixedstop << " -- " << flush;459 //cout << "start" << (*vobs5).obsfixedstart << " -- " << flush;460 //cout << "stop" << (*vobs5).obsfixedstop << " -- " << flush;461 462 //if (obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstop463 // && (*vobs5).obsfixedstop < obsfixedlist[counter2].obsfixedstop464 // && counter2!=counter3)465 449 if ((*vobs5).obsfixedstart < obsfixedlist[counter2].obsfixedstop 466 450 && obsfixedlist[counter2].obsfixedstop <= (*vobs5).obsfixedstop … … 468 452 && counter2!=counter3) 469 453 { 470 //delta1=((*vobs5).obsfixedstop-obsfixedlist[counter2].obsfixedstart)/2;471 454 delta1=(obsfixedlist[counter2].obsfixedstop-(*vobs5).obsfixedstart)/2; 472 455 finalobsfixedstop=obsfixedlist[counter2].obsfixedstop-delta1; 473 //cout << "delta1 " << delta1 << endl;474 456 stringstream warndelta1; 475 457 warndelta1 << "Overlap between two fixed observations (" << obsfixedlist[counter2].obskey_fixed << " " << (*vobs5).obskey_fixed << "). The stoptime of " << obsfixedlist[counter2].obskey_fixed << " has been changed."; 476 458 T::Warn(warndelta1); 477 459 } 478 //if (obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstart479 // && (*vobs5).obsfixedstart < obsfixedlist[counter2].obsfixedstop480 // && counter2!=counter3)481 460 if ((*vobs5).obsfixedstart <= obsfixedlist[counter2].obsfixedstart 482 461 && obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstop … … 486 465 delta2=((*vobs5).obsfixedstop-obsfixedlist[counter2].obsfixedstart)/2; 487 466 finalobsfixedstart=obsfixedlist[counter2].obsfixedstart+delta2; 488 //cout << "delta2 " << delta2 << endl;489 467 stringstream warndelta2; 490 468 warndelta2 << "Overlap between two fixed observations (" << obsfixedlist[counter2].obskey_fixed << " " << (*vobs5).obskey_fixed << "). The starttime of " << obsfixedlist[counter2].obskey_fixed << " has been changed."; … … 492 470 } 493 471 counter3++; 494 //cout << endl;495 472 } 496 473 497 //cout << "-> final: " << finalobsfixedstart << " - " << finalobsfixedstop << endl; 498 obslist.resize(counter2+1); 499 //obslist[counter2].obsstart=obsfixedlist[counter2].obsfixedstart; 500 obslist[counter2].obsstart=finalobsfixedstart; 501 //obslist[counter2].obsstop=obsfixedlist[counter2].obsfixedstop; 502 obslist[counter2].obsstop=finalobsfixedstop; 503 obslist[counter2].sourcename_obs=obsfixedlist[counter2].sourcename_fixed; 504 obslist[counter2].obsmode_obs=obsfixedlist[counter2].obsmode_fixed; 505 obslist[counter2].obstype_obs=obsfixedlist[counter2].obstype_fixed; 506 obslist[counter2].telsetup_obs=obsfixedlist[counter2].telsetup_fixed; 507 obslist[counter2].sourcekey_obs=obsfixedlist[counter2].sourcekey_fixed; 474 int num=counter2-skipcounter; 475 obslist.resize(num+1); 476 obslist[num].obsstart=finalobsfixedstart; 477 obslist[num].obsstop=finalobsfixedstop; 478 obslist[num].sourcename_obs=obsfixedlist[counter2].sourcename_fixed; 479 obslist[num].obsmode_obs=obsfixedlist[counter2].obsmode_fixed; 480 obslist[num].obstype_obs=obsfixedlist[counter2].obstype_fixed; 481 obslist[num].telsetup_obs=obsfixedlist[counter2].telsetup_fixed; 482 obslist[num].sourcekey_obs=obsfixedlist[counter2].sourcekey_fixed; 508 483 counter2++; 509 //cout << " " << (*vobs).sourcename_fixed << " " << (*vobs).obsfixedstart << flush; 510 //cout << " - " << (*vobs).obsfixedstop << endl; 511 } 484 cout << " " << (*vobs).sourcename_fixed << " " << (*vobs).obsfixedstart << flush; 485 cout << " - " << (*vobs).obsfixedstop << endl; 486 } 487 stringstream obsmsg; 488 obsmsg << "Added " << obslist.size() << " fixed observations to ScheduledObs. "; 489 T::Message(obsmsg); 490 cout << "Added " << obslist.size() << " fixed observations to ScheduledObs. " << endl; 491 492 for (int i=0; i<(int)obsstdlist.size(); i++) 493 { 494 for (int j=0; j<(int)obsstdlist.size(); j++) 495 { 496 if (obsstdlist[i].sourcekey_std == obsstdlist[j].sourcekey_std && i!=j) 497 { 498 cout << "One double sourcekey in std observations: " << obsstdlist[j].sourcekey_std << endl; 499 stringstream errdoublestd; 500 errdoublestd << "One double sourcekey in std observations: " << obsstdlist[j].sourcekey_std << " (" << obsstdlist[j].sourcename_std << ")."; 501 T::Error(errdoublestd); 502 T::Message("Scheduling stopped."); 503 return error ? T::kSM_Error : T::kSM_Ready; 504 } 505 } 506 } 507 508 // loop over nights 509 // calculate sunset and sunrise 510 // check if there is already scheduled obs in that night 511 // 512 512 513 513 // in this loop the standard observations shall be … … 522 522 } 523 523 524 // in this loop from the scheduled observations the list 525 // of scheduled runs shall be calculated 524 // in this loop the ScheduledRuns are filled 525 // (only data runs -> no runtype yet) 526 // might be merged with next loop 526 527 counter2=0; 527 528 struct vector<ScheduledObs>::iterator vobs3; 528 529 for (vobs3=obslist.begin(); vobs3!=obslist.end(); vobs3++) 529 530 { 530 // add runtype and runtime calculations531 531 runlist.resize(counter2+1); 532 532 runlist[counter2].runstart=obslist[counter2].obsstart; … … 538 538 runlist[counter2].sourcekey_run=obslist[counter2].sourcekey_obs; 539 539 counter2++; 540 cout << (*vobs3).sourcename_obs << endl;540 //cout << (*vobs3).sourcename_obs << endl; 541 541 } 542 542 … … 551 551 } 552 552 553 // in this loop the ScheduledRuns are inserted to the DB 554 // before the runtimes are adapted according to 555 // duration of P-Run, C-Run and repositioning 553 556 counter3=0; 557 int insertcount=0; 554 558 boost::posix_time::ptime finalstarttime; 555 559 boost::posix_time::ptime finalstoptime; … … 595 599 throw; 596 600 } 601 insertcount++; 597 602 } 598 603 counter3++; 599 604 } 605 stringstream insertmsg; 606 insertmsg << "Inserted " << insertcount << " runs into the DB."; 607 T::Message(insertmsg); 600 608 //usleep(3000000); 601 609 T::Message("Scheduling done."); … … 603 611 fSessionId = -1; 604 612 605 bool error = false;613 //bool error = false; 606 614 return error ? T::kSM_Error : T::kSM_Ready; 607 615 }
Note:
See TracChangeset
for help on using the changeset viewer.