Changeset 19516 for trunk/FACT++


Ignore:
Timestamp:
05/26/19 12:02:46 (6 years ago)
Author:
tbretz
Message:
Do not trigger reschedule if source is already observed, added TH dependent checks for known sources, take ToO flag into account, some improvements to the output
File:
1 edited

Legend:

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

    r19508 r19516  
    426426    uint16_t fKeepAliveInterval;
    427427
    428     int16_t GetSourceKey(const string &name, const double &ra, const double &dec)
     428    struct Source
     429    {
     430        int16_t key { -1 };
     431        bool isToO { false };
     432    };
     433
     434    Source GetSourceKey(const string &name, const double &ra, const double &dec)
    429435    {
    430436        const double min_dist   = 0.1;
    431437        const double check_dist = 2.0;
    432438
    433 
    434         int32_t source_key = -1;
     439        // ----------------------- Check if source with same name exists -----------------------
     440
     441        const string query2 =
     442            "SELECT fSourceKey, fIsToO FROM Source WHERE fSourceName='"+name+"'";
     443
     444        const mysqlpp::StoreQueryResult res2 = fConnection.query(query2).store();
     445
     446        if (res2.num_rows())
     447        {
     448            Source source;
     449            source.key   = res2[0]["fSourceKey"];
     450            source.isToO = res2[0]["fIsToO"];
     451
     452            Info("A source with the same name (key="+to_string(source.key)+") was found in the Source table.");
     453
     454            return source;
     455        }
     456
     457        // ----------------- Check if source with similar coordinates exists -------------------
    435458
    436459        const string query1 =
    437             "SELECT fSourceKey, fSourceName, ADIST(fDeclination, fRightAscension*15, "+to_string(dec)+", "+to_string(ra)+") AS Dist\n"
     460            "SELECT\n"
     461            "   fSourceKey,\n"
     462            "   fSourceName,\n"
     463            "   ADIST(fDeclination, fRightAscension*15, "+to_string(dec)+", "+to_string(ra)+") AS Dist,\n"
     464            "   fIsToO\n"
    438465            " FROM Source\n"
    439466            " WHERE fSourceTypeKEY=1\n"
     
    445472        if (res1.num_rows())
    446473        {
    447             Info("Found "+to_string(res1.num_rows())+" sources with a distance less than "+to_string(check_dist)+"\u00b0");
     474            Info("Found "+to_string(res1.num_rows())+" sources with a distance less than "+Tools::Form("%.2f", check_dist)+"\u00b0");
    448475
    449476            for (size_t i=0; i<res1.num_rows(); i++)
    450477            {
    451478                const mysqlpp::Row &row = res1[i];
    452                 Info(" "+string(row["fSourceName"])+" ["+string(row["fSourceKey"])+"]: D="+string(row["Dist"])+"\u00b0");
     479                Info(" "+string(row["fSourceName"])+" ["+string(row["fSourceKey"])+"]: D="+Tools::Form("%.2f", double(row["Dist"]))+"\u00b0");
    453480            }
    454481
     
    456483            if (double(row["Dist"])<min_dist)
    457484            {
    458                 Warn("Sources closer than "+to_string(check_dist)+"\u00b0 detected.");
    459                 Warn("Overwriting source key with: "+string(row["fSourceName"])+" ["+string(row["fSourceKey"])+"]: D="+string(row["Dist"])+"\u00b0");
    460                 source_key = res1[0]["fSourceKey"];
     485                Source source;
     486                source.key   = res1[0]["fSourceKey"];
     487                source.isToO = res1[0]["fIsToO"];
     488
     489                Warn("Sources closer than "+Tools::Form("%.2f", check_dist)+"\u00b0 detected.");
     490                Warn("Overwriting source key with: "+string(row["fSourceName"])+" ["+to_string(source.key)+"]: D="+Tools::Form("%.2f", double(row["Dist"]))+"\u00b0");
     491
     492                return source;
    461493            }
    462494        }
    463495
    464         //Out() << Time()-stopwatch << endl;
    465 
    466         const string query2 =
    467             "SELECT fSourceKey FROM Source WHERE fSourceName='"+name+"'";
    468 
    469         const mysqlpp::StoreQueryResult res2 = fConnection.query(query2).store();
    470 
    471         if (res2.num_rows())
    472         {
    473             source_key = res2[0]["fSourceKey"];
    474             Info("A source with the same name (key="+to_string(source_key)+") was found in the Source table.");
    475         }
    476 
    477         return source_key;
     496        return Source();
    478497    }
    479498
     
    575594
    576595
    577     bool ScheduleImp(const string &name, const double &ra, const double &dec, const bool &known_only)
     596    bool ScheduleImp(const string &name, const double &ra, const double &dec, const CheckVisibility &check, const bool &is_pointing)
    578597    {
    579598        const bool fDryRun = GetCurrentState()!=ToO::State::kArmed;
     
    608627         */
    609628
    610         int32_t source_key = GetSourceKey(name, ra, dec);
     629        Source source = GetSourceKey(name, ra, dec);
    611630
    612631        Out() << Time()-stopwatch << endl;
    613632
    614         if (known_only && source_key<0)
     633        if (is_pointing && source.key<0) // Not a known source
    615634        {
    616635            Warn("Observation of only known sources requested but no known source found.");
    617636            return false;
     637        }
     638
     639        if (source.key>=0)
     640        {
     641            if (source.isToO)
     642            {
     643                Warn("Known source with ToO flag... skipping.");
     644                return false;
     645            }
     646
     647            const set<uint16_t> prioritized_sources = { 1, 2, 7 };
     648
     649            const bool prio = prioritized_sources.find(source.key)!=prioritized_sources.end();
     650
     651            if ((check.threshold>3 && !prio) || check.threshold>10)
     652            {
     653                Warn("Relative threshold too high... skipping.");
     654                return false;
     655            }
    618656        }
    619657
     
    690728        int32_t reschedule = -1;
    691729        int32_t following  = -1;
     730        int32_t last_resch = -1;
    692731        for (size_t i=0; i<resS.num_rows(); i++)
    693732        {
     
    715754                reschedule = i;
    716755
     756            if (bool(row["Reschedule"]) && !row["Delete"])
     757                last_resch = i;
     758
    717759            if (following==-1 && bool(row["Following"]))
    718760                following = i;
     
    799841        // ====================================================================
    800842
    801         // FIXME: Check if source is not already scheduled anyways
    802 
    803         Info("Source will be scheduled.");
    804 
    805         if (source_key<0)
     843        if (source.key<0)
    806844        {
    807845            const double wobble_angle = GetWobbleAngle(ra, dec);
     
    818856                auto q = fConnection.query(query);
    819857                q.execute();
    820                 Info(q.info());
    821 
    822                 source_key = q.insert_id();
    823 
    824                 Info(string(fDryRun?"[dry-run] ":"")+"The new source got the key "+to_string(source_key));
     858                if (!q.info().empty())
     859                    Info(q.info());
     860
     861                source.key = q.insert_id();
     862
     863                Info(string(fDryRun?"[dry-run] ":"")+"The new source got the key "+to_string(source.key));
    825864            }
    826865            else
    827866                Out() << query << endl;
    828 
    829867        }
    830868
     
    833871        // ====================================================================
    834872
     873        Info("Source will be scheduled.");
     874
     875        Out() << "New entries:\n";
     876
    835877        vector<string> insert;
    836878
    837         Out() << "New entries:\n";
    838         Out() << "           | auto  | " << schedtime.GetAsStr()     << " |       < start >     | 0 | ToO  | nodrs,grb | " << setw(4) << source_key << " | 4\n";
    839 
    840         insert.emplace_back("('"+schedtime.GetAsStr()+"',0,'ToO','nodrs:true,grb:true',"+to_string(source_key)+",4)");
     879        const bool ongoing = last_resch>=0 && source.key==uint16_t(resS[last_resch]["fSourceKey"]);
     880
     881        if (ongoing)
     882            Out() << "           |       |                     |      < ongoing >    | 0 |      |           | " << setw(4) << source.key << " | 4\n";
     883        else
     884        {
     885            Out() << "           | auto  | " << schedtime.GetAsStr()     << " |       < start >     | 0 | ToO  | " << (is_pointing?"  nodrs  ":"nodrs,grb") << " | " << setw(4) << source.key << " | 4\n";
     886            insert.emplace_back("('"+schedtime.GetAsStr()+"',0,'ToO','"+(is_pointing?"nodrs:true":"nodrs:true,grb:true")+"',"+to_string(source.key)+",4)");
     887        }
    841888
    842889        // --------------------------------------------------------------------
     
    878925        // Empty interrupt to stop data taking as early as possible
    879926        // Triggers also RELOAD_SOURCES
    880         Dim::SendCommandNB("DIM_CONTROL/INTERRUPT", "prepare");
     927        if (!ongoing)
     928            Dim::SendCommandNB("DIM_CONTROL/INTERRUPT", "prepare");
    881929
    882930        // ---------------------------------------------------------
     
    897945            auto q = fConnection.query(queryI);
    898946            q.execute();
    899             Info(q.info());
     947            if (!q.info().empty())
     948                Info(q.info());
    900949        }
    901950        // ---------------------------------------------------------
     
    905954        // ---------------------------------------------------------
    906955
    907         Dim::SendCommand("DIM_CONTROL/INTERRUPT", "reschedule");
     956        if (!ongoing)
     957            Dim::SendCommand("DIM_CONTROL/INTERRUPT", "reschedule");
    908958
    909959        ostringstream out;
     
    914964    }
    915965
    916     bool Schedule(const string &name, const double &ra, const double &dec, const bool &known_only)
     966    bool Schedule(const string &name, const double &ra, const double &dec, const CheckVisibility &check, const bool &is_pointing)
    917967    {
    918968        try
    919969        {
    920             return ScheduleImp(name, ra, dec, known_only);
     970            return ScheduleImp(name, ra, dec, check, is_pointing);
    921971        }
    922972        catch (const exception &e)
     
    10181068
    10191069        Info(string("Sun altitude:   ")+(check.valid_sun      ?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.solarobj.fSunHrz.alt)+"\u00b0]");
    1020         Info(string("Moon distance:  ")+(check.valid_moon     ?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.moon_dist)+"\u00b0]");
    1021         Info(string("Zenith angle:   ")+(check.valid_zd       ?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.position.zd)+"\u00b0]");
    1022         Info(string("Current:        ")+(check.valid_current  ?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.current)+" \u00b5A]");
    1023         Info(string("Rel. threshold: ")+(check.valid_threshold?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.threshold)+"]");
     1070        if (check.valid_sun)
     1071        {
     1072            Info(string("Moon distance:  ")+(check.valid_moon     ?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.moon_dist)+"\u00b0]");
     1073            Info(string("Zenith angle:   ")+(check.valid_zd       ?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.position.zd)+"\u00b0]");
     1074            Info(string("Current:        ")+(check.valid_current  ?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.current)+" \u00b5A]");
     1075            Info(string("Rel. threshold: ")+(check.valid_threshold?"OK    ":"failed")+" ["+Tools::Form("%5.1f", check.threshold)+"]");
     1076        }
    10241077
    10251078        if (!check.visible)
     
    10941147*/
    10951148
    1096         const bool known_only = grb.type==51 || grb.type==83;
    1097 
    1098         Schedule(name, grb.ra, grb.dec, known_only);
     1149        const bool is_pointing = grb.type==51 || grb.type==83;
     1150
     1151        Schedule(name, grb.ra, grb.dec, check, is_pointing);
    10991152
    11001153        return fConnection.connected() ? GetCurrentState() : ToO::State::kDisconnected;
Note: See TracChangeset for help on using the changeset viewer.