Changeset 10596 for trunk


Ignore:
Timestamp:
05/05/11 22:27:55 (13 years ago)
Author:
Daniela Dorner
Message:
added filling of intermediate observation lists and filling of fixed observations into db
File:
1 edited

Legend:

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

    r10530 r10596  
    22#include <boost/bind.hpp>
    33#include <boost/thread.hpp>
     4#include <boost/regex.hpp>
    45#include <boost/asio/deadline_timer.hpp>
    56
     
    3435string database;
    3536
     37// things to be done/checked/changed
     38// * do not use --database, but sth like --database-scheduler
     39//   to be independent of the configuration database
     40// * check if the following include is really needed
     41//   #include <boost/date_time/posix_time/posix_time.hpp>
     42// * string database not as global variable
     43//   pass reference to conf and the string as a member of the class
     44//   (see chat of 4.5.2011)
     45// * move definition of config parameters to AutoScheduler class
     46//   + read in from config
     47
     48// other things to do
     49//
     50// define what is error and warning
     51
     52
     53// config parameters
     54// mintime
     55// runtimec
     56// runtimep
     57// repostime
     58
     59// missing:
     60// from / to for the time range for which the scheduling shall be done
     61//
     62// calculate time for std obs
     63// calculate sun set/rise
     64//
     65// check for double std sources
     66//
     67// return errors
     68// get input from sendcommand
    3669
    3770// =========================================================================
     
    5184    {
    5285        int obskey;
     86        int obsmode;
     87        int obstype;
     88        int splitflag;
     89        int telsetup;
     90        float fluxweight;
     91        float slope;
     92        float flux;
     93        float ra;
     94        float dec;
    5395        boost::posix_time::ptime starttime;
    5496        boost::posix_time::ptime stoptime;
    5597        boost::posix_time::time_duration duration_db;
    5698        string sourcename;
     99        int sourcekey;
    57100    };
    58101
     
    63106        string sourcename_fixed;
    64107        int obsmode_fixed;
     108        int obstype_fixed;
     109        int telsetup_fixed;
     110        float ra_fixed;
     111        float dec_fixed;
    65112        boost::posix_time::ptime obsfixedstart;
    66113        boost::posix_time::ptime obsfixedstop;
    67114    };
     115
     116    // will need other types of obs
     117    // FloatingObs (duration < stop-start + splitflag no)
     118    // FloatingSplittedObs (duration < stop-start + splitflag yes)
     119    // FixedSlot, i.e. just block a time slot
    68120
    69121    struct StdObs
     
    73125        string sourcename_std;
    74126        int obsmode_std;
     127        int obstype_std;
     128        int telsetup_std;
     129        float fluxweight_std;
     130        float slope_std;
     131        float flux_std;
     132        float ra_std;
     133        float dec_std;
    75134        boost::posix_time::ptime obsstdstart;
    76135        boost::posix_time::ptime obsstdstop;
     
    82141        string sourcename_obs;
    83142        int obsmode_obs;
     143        int obstype_obs;
     144        int telsetup_obs;
    84145        boost::posix_time::ptime obsstart;
    85146        boost::posix_time::ptime obsstop;
     
    88149    struct ScheduledRun
    89150    {
    90         int runnumber;
     151        //int runnumber; // to be seen, if runnumber is needed
    91152        int runtype;
    92         int sourcekey;
    93         int obsmode;
     153        int sourcekey_run;
     154        string sourcename_run;//for convenience
     155        int obsmode_run;
     156        int obstype_run;
     157        int telsetup_run;
    94158        boost::posix_time::ptime runstart;
    95159        boost::posix_time::ptime runstop;
     
    98162    int fSessionId;
    99163
     164
     165
    100166    int Schedule()
    101167    {
     168        boost::posix_time::time_duration runtimec(0, 3, 0);
     169        boost::posix_time::time_duration runtimep(0, 3, 0);
     170        boost::posix_time::time_duration mintime(1, 0, 0);
     171        boost::posix_time::time_duration repostime(0, 5, 0);
     172
    102173        stringstream str;
    103174        str << "Scheduling started -> Preview (id=" << fSessionId << ")";
    104175        T::Message(str);
    105176
    106         //static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))?");
    107177        static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))");
    108178        // 2: user
     
    112182        // 9: db
    113183
    114         //string database="root:Ihtp4aDB@localhost:3306/Scheduling";
    115184        boost::smatch what;
    116185        if (!boost::regex_match(database, what, expr, boost::match_extra))
     
    148217            throw;
    149218        }
    150         //mysqlpp::Connection conn(db.c_str(), server.c_str(), user.c_str(), passwd.c_str(), port);
    151         //mysqlpp::Connection conn("Scheduling", "localhost", "root", "Ihtp4aDB", 3306);
    152         if (!conn.connected())
    153         {
    154             cout << "MySQL connection error: " << conn.error() << endl;
    155             throw;
    156         }
    157219
    158220        // get observation parameters from DB
    159         mysqlpp::Query query = conn.query("SELECT fObservationKEY, fStartTime, fStopTime, fDuration, fSourceName, fSourceKEY, fSplitFlag, fFluxWeight, fObservationModeKEY, fObservationTypeKEY FROM ObservationParameters LEFT JOIN Source USING(fSourceKEY) ORDER BY fObservationKEY");
    160         //maybe order by priority?
     221        // maybe order by priority?
     222        mysqlpp::Query query = conn.query("SELECT fObservationKEY, fStartTime, fStopTime, fDuration, fSourceName, fSourceKEY, fSplitFlag, fFluxWeight, fSlope, fFlux, fRightAscension, fDeclination, fObservationModeKEY, fObservationTypeKEY , fTelescopeSetupKEY FROM ObservationParameters LEFT JOIN Source USING(fSourceKEY) ORDER BY fStartTime");
    161223
    162224        mysqlpp::StoreQueryResult res = query.store();
     
    185247            cout << " T1 " << (*v)[1].c_str() << " T2: " << (*v)[2].c_str() << " (c " << counter << " " << counter2 << ")" << endl;
    186248
     249            /*
     250            cout << " 0 " << (*v)[0].c_str() << endl;
     251            cout << " 1 " << (*v)[1].c_str() << endl;
     252            cout << " 2 " << (*v)[2].c_str() << endl;
     253            cout << " 3 " << (*v)[3].c_str() << endl;
     254            cout << " 4 " << (*v)[4].c_str() << endl;
     255            cout << " 5 " << (*v)[5].c_str() << endl;
     256            cout << " 6 " << (*v)[6].c_str() << endl;
     257            cout << " 7 " << (*v)[7].c_str() << endl;
     258            cout << " 8 " << (*v)[8].c_str() << endl;
     259            cout << " 9 " << (*v)[9].c_str() << endl;
     260            cout << " 10 " << (*v)[10].c_str() << endl;
     261            cout << " 11 " << (*v)[11].c_str() << endl;
     262            cout << " 12 " << (*v)[12].c_str() << endl;
     263            cout << " 13 " << (*v)[13].c_str() << endl;
     264            cout << " 14 " << (*v)[14].c_str() << endl;
     265            */
     266            //0: obskey
     267            //1: startime
     268            //2: stoptime
     269            //3: duration
     270            //4: sourcename
     271            //5: sourcekey
     272            //6: splitflag
     273            //7: fluxweight
     274            //8: slope
     275            //9: flux
     276            //10: ra
     277            //11: dec
     278            //12: obsmode
     279            //13: obstype
     280            //14: telsetup
    187281            stringstream t1;
    188282            stringstream t2;
     
    193287
    194288            //boost::posix_time::time_duration mintime(0,conf.Get<int>("mintime"), 0);
    195             boost::posix_time::time_duration mintime(1, 0, 0);
    196289            t1 >> Time::sql >> olist[counter].starttime;
    197290            t2 >> Time::sql >> olist[counter].stoptime;
     
    199292            boost::posix_time::time_period period(olist[counter].starttime, olist[counter].stoptime);
    200293            olist[counter].sourcename=(*v)[4].c_str();
    201             olist[counter].obskey=(*v)[0];
     294            olist[counter].sourcekey=(*v)[5];
     295
     296            if (!(*v)[0].is_null())
     297                olist[counter].obskey=(*v)[0];
     298            //else
     299            //    cout << "no obskey" << endl;
     300            if (!(*v)[12].is_null())
     301                olist[counter].obsmode=(*v)[12];
     302            //else
     303            //    cout << "no obsmode" << endl;
     304            if (!(*v)[13].is_null())
     305                olist[counter].obstype=(*v)[13];
     306            //else
     307            //    cout << "no obstype" << endl;
     308            if (!(*v)[14].is_null())
     309                olist[counter].telsetup=(*v)[14];
     310            //else
     311            //    cout << "no telsetup" << endl;
     312            if (!(*v)[6].is_null())
     313                olist[counter].splitflag=(*v)[6];
     314            //else
     315            //{
     316            //    cout << "no splitflag" << endl;
     317                ////stringstream str;
     318                ////str << "no splitflag";
     319                //T::Message("no splitflag");
     320                //T::Error("---> no splitflag");
     321                //T::Warn("+++ no splitflag");
     322            //}
     323            if (!(*v)[7].is_null())
     324                olist[counter].fluxweight=(*v)[7];
     325            else
     326            {
     327                olist[counter].fluxweight=0;
     328            //    cout << "no fluxweight" << endl;
     329            }
     330            if (!(*v)[8].is_null())
     331                olist[counter].slope=(*v)[8];
     332            //else
     333            //    cout << "no slope" << endl;
     334            if (!(*v)[9].is_null())
     335                olist[counter].flux=(*v)[9];
     336            //else
     337            //    cout << "no flux" << endl;
     338            if (!(*v)[10].is_null())
     339                olist[counter].ra=(*v)[10];
     340            //else
     341            //    cout << "no ra" << endl;
     342            if (!(*v)[11].is_null())
     343                olist[counter].dec=(*v)[11];
     344            //else
     345            //    cout << "no dec" << endl;
    202346
    203347            // time_duration cannot be used, as only up to 99 hours are handeled
     
    224368            */
    225369
    226             // if start and stop time are available, it is a fixed observation
    227             if (!(olist[counter].stoptime.is_not_a_date_time() && olist[counter].starttime.is_not_a_date_time()))
     370            // always filled: obstype
     371            //
     372            // fixed observations:
     373            //   filled: starttime, stoptime
     374            //   not filled: fluxweight
     375            //   maybe filled: obsmode, telsetup, source (not filled for FixedSlotObs)
     376            //   maybe filled: duration (filled for FloatingObs and FloatingSplittedObs)
     377            //   maybe filled: splitflag (filled for FloatingSplittedObs)
     378            //
     379            // std observations:
     380            //   filled: fluxweight, telsetup, obsmore, source
     381            //   not filled: starttime, stoptime, duration
     382
     383            // fixed observations
     384            if (!(olist[counter].stoptime.is_not_a_date_time()
     385                  && olist[counter].starttime.is_not_a_date_time())
     386                && olist[counter].fluxweight==0
     387               )
    228388            {
    229389                obsfixedlist.resize(counter2+1);
     
    232392                obsfixedlist[counter2].sourcename_fixed=olist[counter].sourcename;
    233393                obsfixedlist[counter2].obskey_fixed=olist[counter].obskey;
     394                obsfixedlist[counter2].obstype_fixed=olist[counter].obstype;
     395                obsfixedlist[counter2].obsmode_fixed=olist[counter].obsmode;
     396                obsfixedlist[counter2].telsetup_fixed=olist[counter].telsetup;
     397                obsfixedlist[counter2].sourcekey_fixed=olist[counter].sourcekey;
     398                obsfixedlist[counter2].ra_fixed=olist[counter].ra;
     399                obsfixedlist[counter2].dec_fixed=olist[counter].dec;
    234400                counter2++;
    235401            }
    236             else
     402
     403            // std obs
     404            if (olist[counter].stoptime.is_not_a_date_time()
     405                  && olist[counter].starttime.is_not_a_date_time()
     406                && olist[counter].fluxweight>0
     407               )
    237408            {
    238409                obsstdlist.resize(counter3+1);
    239410                obsstdlist[counter3].sourcename_std=olist[counter].sourcename;
    240411                obsstdlist[counter3].obskey_std=olist[counter].obskey;
     412                obsstdlist[counter3].obsmode_std=olist[counter].obsmode;
     413                obsstdlist[counter3].obstype_std=olist[counter].obstype;
     414                obsstdlist[counter3].telsetup_std=olist[counter].telsetup;
     415                obsstdlist[counter3].sourcekey_std=olist[counter].sourcekey;
     416                obsstdlist[counter3].fluxweight_std=olist[counter].fluxweight;
     417                obsstdlist[counter3].flux_std=olist[counter].flux;
     418                obsstdlist[counter3].slope_std=olist[counter].slope;
     419                obsstdlist[counter3].ra_std=olist[counter].ra;
     420                obsstdlist[counter3].dec_std=olist[counter].dec;
    241421                counter3++;
    242422            }
     
    247427        cout << obsstdlist.size() << " standard observations found. " << endl;
    248428
    249         // in this loop the fixed observations shall be
    250         // checked, evaluated and added to the ScheduledObs list
     429        // loop to add the fixed observations to the ScheduledObs list
     430        // missing: checks and evaluation
     431        //  * check for sun
     432        //  * check for moon
     433        counter2=0;
     434        boost::posix_time::ptime finalobsfixedstart;
     435        boost::posix_time::ptime finalobsfixedstop;
     436        boost::posix_time::time_duration delta1;
     437        boost::posix_time::time_duration delta2;
     438        boost::posix_time::time_duration delta0(0,0,0);
    251439        struct vector<FixedObs>::iterator vobs;
     440        struct vector<FixedObs>::iterator vobs5;
    252441        cout << "Fixed Observations: " << endl;
    253442        for (vobs=obsfixedlist.begin(); vobs!=obsfixedlist.end(); vobs++)
    254443        {
    255             cout << "  " << (*vobs).sourcename_fixed <<  " " << (*vobs).obsfixedstart << flush;
    256             cout << " - " << (*vobs).obsfixedstop << endl;
     444            counter3=0;
     445            delta1=delta0;
     446            delta2=delta0;
     447            finalobsfixedstart=obsfixedlist[counter2].obsfixedstart;
     448            finalobsfixedstop=obsfixedlist[counter2].obsfixedstop;
     449            //cout << "final: " << finalobsfixedstart << " - " << finalobsfixedstop << endl;
     450
     451            for (vobs5=obsfixedlist.begin(); vobs5!=obsfixedlist.end(); vobs5++)
     452            {
     453                //cout << "startc" << obsfixedlist[counter2].obsfixedstart << "  --  " << flush;
     454                //cout << "stopc" << obsfixedlist[counter2].obsfixedstop << "  --  " << flush;
     455                //cout << "start" << (*vobs5).obsfixedstart << "  --  " << flush;
     456                //cout << "stop" << (*vobs5).obsfixedstop << "  --  " << flush;
     457
     458                //if (obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstop
     459                //    && (*vobs5).obsfixedstop < obsfixedlist[counter2].obsfixedstop
     460                //    && counter2!=counter3)
     461                if ((*vobs5).obsfixedstart < obsfixedlist[counter2].obsfixedstop
     462                    && obsfixedlist[counter2].obsfixedstop <= (*vobs5).obsfixedstop
     463                    && obsfixedlist[counter2].obsfixedstart <= (*vobs5).obsfixedstart
     464                    && counter2!=counter3)
     465                {
     466                    //delta1=((*vobs5).obsfixedstop-obsfixedlist[counter2].obsfixedstart)/2;
     467                    delta1=(obsfixedlist[counter2].obsfixedstop-(*vobs5).obsfixedstart)/2;
     468                    finalobsfixedstop=obsfixedlist[counter2].obsfixedstop-delta1;
     469                    //cout << "delta1 " << delta1 << endl;
     470                    stringstream warndelta1;
     471                    warndelta1 << "Overlap between two fixed observations (" << obsfixedlist[counter2].obskey_fixed << " " << (*vobs5).obskey_fixed << "). The stoptime of " << obsfixedlist[counter2].obskey_fixed << " has been changed.";
     472                    T::Warn(warndelta1);
     473                }
     474                //if (obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstart
     475                //    && (*vobs5).obsfixedstart < obsfixedlist[counter2].obsfixedstop
     476                //    && counter2!=counter3)
     477                if ((*vobs5).obsfixedstart <= obsfixedlist[counter2].obsfixedstart
     478                    && obsfixedlist[counter2].obsfixedstart < (*vobs5).obsfixedstop
     479                    && obsfixedlist[counter2].obsfixedstop >= (*vobs5).obsfixedstop
     480                    && counter2!=counter3)
     481                {
     482                    delta2=((*vobs5).obsfixedstop-obsfixedlist[counter2].obsfixedstart)/2;
     483                    finalobsfixedstart=obsfixedlist[counter2].obsfixedstart+delta2;
     484                    //cout << "delta2 " << delta2 << endl;
     485                    stringstream warndelta2;
     486                    warndelta2 << "Overlap between two fixed observations (" << obsfixedlist[counter2].obskey_fixed << " " << (*vobs5).obskey_fixed << "). The starttime of " << obsfixedlist[counter2].obskey_fixed << " has been changed.";
     487                    T::Warn(warndelta2);
     488                }
     489                counter3++;
     490                //cout << endl;
     491            }
     492
     493            //cout << "-> final: " << finalobsfixedstart << " - " << finalobsfixedstop << endl;
     494            obslist.resize(counter2+1);
     495            //obslist[counter2].obsstart=obsfixedlist[counter2].obsfixedstart;
     496            obslist[counter2].obsstart=finalobsfixedstart;
     497            //obslist[counter2].obsstop=obsfixedlist[counter2].obsfixedstop;
     498            obslist[counter2].obsstop=finalobsfixedstop;
     499            obslist[counter2].sourcename_obs=obsfixedlist[counter2].sourcename_fixed;
     500            obslist[counter2].obsmode_obs=obsfixedlist[counter2].obsmode_fixed;
     501            obslist[counter2].obstype_obs=obsfixedlist[counter2].obstype_fixed;
     502            obslist[counter2].telsetup_obs=obsfixedlist[counter2].telsetup_fixed;
     503            obslist[counter2].sourcekey_obs=obsfixedlist[counter2].sourcekey_fixed;
     504            counter2++;
     505            //cout << "  " << (*vobs).sourcename_fixed <<  " " << (*vobs).obsfixedstart << flush;
     506            //cout << " - " << (*vobs).obsfixedstop << endl;
    257507        }
    258508
     
    270520        // in this loop from the scheduled observations the list
    271521        // of scheduled runs shall be calculated
     522        counter2=0;
    272523        struct vector<ScheduledObs>::iterator vobs3;
    273524        for (vobs3=obslist.begin(); vobs3!=obslist.end(); vobs3++)
    274525        {
     526            // add runtype and runtime calculations
     527            runlist.resize(counter2+1);
     528            runlist[counter2].runstart=obslist[counter2].obsstart;
     529            runlist[counter2].runstop=obslist[counter2].obsstop;
     530            runlist[counter2].sourcename_run=obslist[counter2].sourcename_obs;
     531            runlist[counter2].obsmode_run=obslist[counter2].obsmode_obs;
     532            runlist[counter2].obstype_run=obslist[counter2].obstype_obs;
     533            runlist[counter2].telsetup_run=obslist[counter2].telsetup_obs;
     534            runlist[counter2].sourcekey_run=obslist[counter2].sourcekey_obs;
     535            counter2++;
    275536            cout << (*vobs3).sourcename_obs << endl;
    276537        }
    277538
     539        //delete old scheduled runs from the DB
     540        mysqlpp::Query query0 = conn.query("DELETE FROM ScheduledRun");
     541
     542        mysqlpp::SimpleResult res0 = query0.execute();
     543        if (!res0)
     544        {
     545            cout << "MySQL query failed: " << query0.error() << endl;
     546            throw;
     547        }
     548
     549        counter3=0;
     550        boost::posix_time::ptime finalstarttime;
     551        boost::posix_time::ptime finalstoptime;
     552        struct vector<ScheduledRun>::iterator vobs4;
     553        for (vobs4=runlist.begin(); vobs4!=runlist.end(); vobs4++)
     554        {
     555            for (int i=2; i<5; i++)
     556            {
     557                switch(i)
     558                {
     559                case 2:
     560                    finalstarttime=runlist[counter3].runstart+repostime+runtimec+runtimep;
     561                    finalstoptime=runlist[counter3].runstop;
     562                    break;
     563                case 3:
     564                    finalstarttime=runlist[counter3].runstart+repostime;
     565                    finalstoptime=runlist[counter3].runstart+runtimep+repostime;
     566                    break;
     567                case 4:
     568                    finalstarttime=runlist[counter3].runstart+runtimep+repostime;
     569                    finalstoptime=runlist[counter3].runstart+repostime+runtimep+runtimec;
     570                    break;
     571                }
     572                stringstream q1;
     573                //cout << (*vobs4).sourcename_run << endl;
     574                q1 << "INSERT ScheduledRun set fStartTime='" << Time::sql << finalstarttime;
     575                q1 << "', fStopTime='" << Time::sql << finalstoptime;
     576                q1 << "', fSourceKEY='" << (*vobs4).sourcekey_run;
     577                q1 << "', fRunTypeKEY='" << i;
     578                q1 << "', fTelescopeSetupKEY='" << (*vobs4).telsetup_run;
     579                q1 << "', fObservationTypeKEY='" << (*vobs4).obstype_run;
     580                q1 << "', fObservationModeKEY='" << (*vobs4).obsmode_run;
     581                q1 << "'";
     582
     583                //cout << "executing query: " << q1.str() << endl;
     584
     585                mysqlpp::Query query1 = conn.query(q1.str());
     586
     587                mysqlpp::SimpleResult res1 = query1.execute();
     588                if (!res1)
     589                {
     590                    cout << "MySQL query failed: " << query1.error() << endl;
     591                    throw;
     592                }
     593            }
     594            counter3++;
     595        }
    278596        //usleep(3000000);
    279597        T::Message("Scheduling done.");
Note: See TracChangeset for help on using the changeset viewer.