Changeset 10618


Ignore:
Timestamp:
May 6, 2011, 1:30:10 PM (9 years ago)
Author:
dorner
Message:
added some checks for fixed observations and time range for scheduling
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/scheduler.cc

    r10612 r10618  
    2323#include <vector>
    2424
    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 
    3425#include "LocalControl.h"
    3526#include <boost/date_time/posix_time/posix_time.hpp>
    3627#include <mysql++/mysql++.h>
     28
     29
     30namespace ba    = boost::asio;
     31namespace bs    = boost::system;
     32
     33using ba::deadline_timer;
     34using ba::ip::tcp;
     35
     36using namespace std;
     37using namespace boost::gregorian;
     38using namespace boost::posix_time;
     39
    3740
    3841// string containing database information
     
    5255// other things to do
    5356//
    54 // define what is error and warning
     57// define what to transmit as info/warn/error
    5558
    5659
     
    6265
    6366// missing:
    64 // from / to for the time range for which the scheduling shall be done
    6567//
    6668// calculate time for std obs
     
    7173// return errors
    7274// 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
    7381
    7482// =========================================================================
     
    170178    int Schedule()
    171179    {
     180        bool error = false;
     181
    172182        boost::posix_time::time_duration runtimec(0, 3, 0);
    173183        boost::posix_time::time_duration runtimep(0, 3, 0);
    174184        boost::posix_time::time_duration mintime(1, 0, 0);
    175185        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;
    176190
    177191        stringstream str;
     
    246260        int counter2=0;
    247261        int counter3=0;
     262        cout << "Obs: <obskey> <sourcename>(<sourcekey>, <fluxweight>) from <starttime> to <stoptime>" << endl;
    248263        for (vector<mysqlpp::Row>::iterator v=res.begin(); v<res.end(); v++)
    249264        {
    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
    270269            //0: obskey
    271270            //1: startime
     
    300299            if (!(*v)[0].is_null())
    301300                olist[counter].obskey=(*v)[0];
    302             //else
    303             //    cout << "no obskey" << endl;
    304301            if (!(*v)[12].is_null())
    305302                olist[counter].obsmode=(*v)[12];
    306             //else
    307             //    cout << "no obsmode" << endl;
    308303            if (!(*v)[13].is_null())
    309304                olist[counter].obstype=(*v)[13];
    310             //else
    311             //    cout << "no obstype" << endl;
    312305            if (!(*v)[14].is_null())
    313306                olist[counter].telsetup=(*v)[14];
    314             //else
    315             //    cout << "no telsetup" << endl;
    316307            if (!(*v)[6].is_null())
    317308                olist[counter].splitflag=(*v)[6];
    318             //else
    319             //{
    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             //}
    327309            if (!(*v)[7].is_null())
    328310                olist[counter].fluxweight=(*v)[7];
    329311            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
    334313            if (!(*v)[8].is_null())
    335314                olist[counter].slope=(*v)[8];
    336             //else
    337             //    cout << "no slope" << endl;
    338315            if (!(*v)[9].is_null())
    339316                olist[counter].flux=(*v)[9];
    340             //else
    341             //    cout << "no flux" << endl;
    342317            if (!(*v)[10].is_null())
    343318                olist[counter].ra=(*v)[10];
    344             //else
    345             //    cout << "no ra" << endl;
    346319            if (!(*v)[11].is_null())
    347320                olist[counter].dec=(*v)[11];
    348             //else
    349             //    cout << "no dec" << endl;
    350321
    351322            // time_duration cannot be used, as only up to 99 hours are handeled
     
    428399            counter++;
    429400        }
     401        stringstream fixedobsmsg;
     402        fixedobsmsg << obsfixedlist.size() << " fixed observations found. ";
     403        T::Message(fixedobsmsg);
    430404        cout << obsfixedlist.size() << " fixed observations found. " << endl;
     405
     406        stringstream stdobsmsg;
     407        stdobsmsg << obsstdlist.size() << " standard observations found. ";
     408        T::Message(stdobsmsg);
    431409        cout << obsstdlist.size() << " standard observations found. " << endl;
    432410
    433411        // 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
    437419        counter2=0;
     420        int skipcounter=0;
    438421        boost::posix_time::ptime finalobsfixedstart;
    439422        boost::posix_time::ptime finalobsfixedstop;
     
    446429        for (vobs=obsfixedlist.begin(); vobs!=obsfixedlist.end(); vobs++)
    447430        {
     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            }
    448441            counter3=0;
    449442            delta1=delta0;
     
    451444            finalobsfixedstart=obsfixedlist[counter2].obsfixedstart;
    452445            finalobsfixedstop=obsfixedlist[counter2].obsfixedstop;
    453             //cout << "final: " << finalobsfixedstart << " - " << finalobsfixedstop << endl;
    454446
    455447            for (vobs5=obsfixedlist.begin(); vobs5!=obsfixedlist.end(); vobs5++)
    456448            {
    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).obsfixedstop
    463                 //    && (*vobs5).obsfixedstop < obsfixedlist[counter2].obsfixedstop
    464                 //    && counter2!=counter3)
    465449                if ((*vobs5).obsfixedstart < obsfixedlist[counter2].obsfixedstop
    466450                    && obsfixedlist[counter2].obsfixedstop <= (*vobs5).obsfixedstop
     
    468452                    && counter2!=counter3)
    469453                {
    470                     //delta1=((*vobs5).obsfixedstop-obsfixedlist[counter2].obsfixedstart)/2;
    471454                    delta1=(obsfixedlist[counter2].obsfixedstop-(*vobs5).obsfixedstart)/2;
    472455                    finalobsfixedstop=obsfixedlist[counter2].obsfixedstop-delta1;
    473                     //cout << "delta1 " << delta1 << endl;
    474456                    stringstream warndelta1;
    475457                    warndelta1 << "Overlap between two fixed observations (" << obsfixedlist[counter2].obskey_fixed << " " << (*vobs5).obskey_fixed << "). The stoptime of " << obsfixedlist[counter2].obskey_fixed << " has been changed.";
    476458                    T::Warn(warndelta1);
    477459                }
    478                 //if (obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstart
    479                 //    && (*vobs5).obsfixedstart < obsfixedlist[counter2].obsfixedstop
    480                 //    && counter2!=counter3)
    481460                if ((*vobs5).obsfixedstart <= obsfixedlist[counter2].obsfixedstart
    482461                    && obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstop
     
    486465                    delta2=((*vobs5).obsfixedstop-obsfixedlist[counter2].obsfixedstart)/2;
    487466                    finalobsfixedstart=obsfixedlist[counter2].obsfixedstart+delta2;
    488                     //cout << "delta2 " << delta2 << endl;
    489467                    stringstream warndelta2;
    490468                    warndelta2 << "Overlap between two fixed observations (" << obsfixedlist[counter2].obskey_fixed << " " << (*vobs5).obskey_fixed << "). The starttime of " << obsfixedlist[counter2].obskey_fixed << " has been changed.";
     
    492470                }
    493471                counter3++;
    494                 //cout << endl;
    495472            }
    496473
    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;
    508483            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        //
    512512
    513513        // in this loop the standard observations shall be
     
    522522        }
    523523
    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
    526527        counter2=0;
    527528        struct vector<ScheduledObs>::iterator vobs3;
    528529        for (vobs3=obslist.begin(); vobs3!=obslist.end(); vobs3++)
    529530        {
    530             // add runtype and runtime calculations
    531531            runlist.resize(counter2+1);
    532532            runlist[counter2].runstart=obslist[counter2].obsstart;
     
    538538            runlist[counter2].sourcekey_run=obslist[counter2].sourcekey_obs;
    539539            counter2++;
    540             cout << (*vobs3).sourcename_obs << endl;
     540            //cout << (*vobs3).sourcename_obs << endl;
    541541        }
    542542
     
    551551        }
    552552
     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
    553556        counter3=0;
     557        int insertcount=0;
    554558        boost::posix_time::ptime finalstarttime;
    555559        boost::posix_time::ptime finalstoptime;
     
    595599                    throw;
    596600                }
     601                insertcount++;
    597602            }
    598603            counter3++;
    599604        }
     605        stringstream insertmsg;
     606        insertmsg << "Inserted " << insertcount << " runs into the DB.";
     607        T::Message(insertmsg);
    600608        //usleep(3000000);
    601609        T::Message("Scheduling done.");
     
    603611        fSessionId = -1;
    604612
    605         bool error = false;
     613        //bool error = false;
    606614        return error ? T::kSM_Error : T::kSM_Ready;
    607615    }
Note: See TracChangeset for help on using the changeset viewer.