Changeset 19530
- Timestamp:
- 05/29/19 11:07:47 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/scheduler.cc
r19516 r19530 64 64 Nova::SolarObjects solarobj; 65 65 Nova::ZdAzPosn position; 66 Nova::RstTime vis_sun; 67 Nova::RstTime vis_obj; 66 68 67 69 double moon_dist = -1; … … 75 77 bool valid_threshold = false; 76 78 77 bool visible = false; 79 bool visible = false; // And of all the above except valid_threshold 78 80 79 81 void calc(const Nova::EquPosn &equ, const double &jd) … … 82 84 position = Nova::GetHrzFromEqu(equ, jd); 83 85 moon_dist = Nova::GetAngularSeparation(equ, solarobj.fMoonEqu); 86 vis_sun = Nova::GetSolarRst(jd, -12); 87 vis_obj = Nova::GetObjectRst(equ, jd, zd_max); 84 88 85 89 current = FACT::PredictI(solarobj, equ); … … 459 463 const string query1 = 460 464 "SELECT\n" 461 " 462 " 463 " 464 " 465 " 466 " 467 " 468 " 465 " fSourceKey,\n" 466 " fSourceName,\n" 467 " ADIST(fDeclination, fRightAscension*15, "+to_string(dec)+", "+to_string(ra)+") AS Dist,\n" 468 " fIsToO\n" 469 "FROM Source\n" 470 "WHERE fSourceTypeKEY=1\n" 471 "HAVING Dist<"+to_string(check_dist)+"\n" 472 "ORDER BY Dist ASC"; 469 473 470 474 const mysqlpp::StoreQueryResult res1 = fConnection.query(query1).store(); … … 530 534 " ORDER BY fMagnitude ASC"; 531 535 532 Out() << query0 << endl;536 // Out() << query0 << endl; 533 537 534 538 const mysqlpp::StoreQueryResult res0 = fConnection.query(query0).store(); … … 593 597 } 594 598 595 596 bool ScheduleImp(const string &name, const double &ra, const double &dec, const CheckVisibility &check, const bool &is_pointing) 599 uint32_t AddSource(const string &name, const double &ra, const double &dec, const bool fDryRun=false) 600 { 601 const double wobble_angle = GetWobbleAngle(ra, dec); 602 603 const string query = 604 "INSERT INTO Source\n" 605 " (fSourceName, fRightAscension, fDeclination, fWobbleAngle0, fWobbleAngle1, fSourceTypeKey, fIsToO) VALUES\n" 606 " ('"+name+"', "+to_string(ra/15.)+", "+to_string(dec)+", "+to_string(wobble_angle-90)+", "+to_string(wobble_angle+90)+", 1, 1)"; 607 608 if (fDryRun) 609 { 610 Out() << query << endl; 611 return -1; 612 } 613 614 auto q = fConnection.query(query); 615 q.execute(); 616 if (!q.info().empty()) 617 Info(q.info()); 618 619 const uint32_t source_key = q.insert_id(); 620 621 Info(string(fDryRun?"[dry-run] ":"")+"The new source got the key "+to_string(source_key)); 622 623 return source_key; 624 } 625 626 bool ScheduleImp(const string &name, const ToO::DataGRB &grb, const CheckVisibility &check, const bool &is_pointing) 597 627 { 598 628 const bool fDryRun = GetCurrentState()!=ToO::State::kArmed; … … 627 657 */ 628 658 629 Source source = GetSourceKey(name, ra,dec);659 Source source = GetSourceKey(name, grb.ra, grb.dec); 630 660 631 661 Out() << Time()-stopwatch << endl; 632 662 633 if (is_pointing && source.key<0) // Not a known source 634 { 635 Warn("Observation of only known sources requested but no known source found."); 636 return false; 637 } 638 639 if (source.key>=0) 640 { 663 // This is not a real alert but a pointing information of a satellite 664 if (is_pointing) 665 { 666 // We only observe known sources in paralell with satellites... 667 if (source.key<0) 668 { 669 Warn("Observation of only known sources requested but no known source found."); 670 return false; 671 } 672 673 // ... and only if they are 'standard' sources and not ToO sources. 641 674 if (source.isToO) 642 675 { … … 645 678 } 646 679 680 // For non prioritized sources we require a threshold <3 681 // and for prioritized sources a threshold <10 647 682 const set<uint16_t> prioritized_sources = { 1, 2, 7 }; 648 683 … … 655 690 } 656 691 } 692 657 693 658 694 /* … … 842 878 843 879 if (source.key<0) 844 { 845 const double wobble_angle = GetWobbleAngle(ra, dec); 846 847 Out() << Time()-stopwatch << endl; 848 849 const string query = 850 "INSERT INTO Source\n" 851 " (fSourceName, fRightAscension, fDeclination, fWobbleAngle0, fWobbleAngle1, fSourceTypeKey, fIsToO) VALUES\n" 852 " ('"+name+"', "+to_string(ra/15.)+", "+to_string(dec)+", "+to_string(wobble_angle-90)+", "+to_string(wobble_angle+90)+", 1, 1)"; 853 854 if (!fDryRun) 855 { 856 auto q = fConnection.query(query); 857 q.execute(); 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)); 864 } 865 else 866 Out() << query << endl; 867 } 880 source.key = AddSource(name, grb.ra, grb.dec, fDryRun); 868 881 869 882 Out() << Time()-stopwatch << endl; … … 961 974 Info(out); 962 975 976 // --------------------------------------------------------- 977 978 const string queryT = 979 "INSERT INTO ToOs\n" 980 " (fTypeID, fRightAscension, fDeclination, fSourceKEY) VALUES\n" 981 " ('"+to_string(grb.type)+"', "+to_string(grb.ra/15)+", "+to_string(grb.dec)+", "+to_string(source.key)+")"; 982 983 if (!fDryRun) 984 { 985 auto q = fConnection.query(queryT); 986 q.execute(); 987 if (!q.info().empty()) 988 Info(q.info()); 989 } 990 else 991 Out() << queryT << endl; 992 993 // --------------------------------------------------------- 994 963 995 return true; 964 996 } 965 966 bool Schedule(const string &name, const double &ra, const double &dec, const CheckVisibility &check, const bool &is_pointing)997 /* 998 bool Schedule(const string &name, const ToO::DataGRB &grb, const CheckVisibility &check, const bool &is_pointing) 967 999 { 968 1000 try 969 1001 { 970 return ScheduleImp(name, ra, dec, check, is_pointing);1002 return ScheduleImp(name, grb, check, is_pointing); 971 1003 } 972 1004 catch (const exception &e) … … 977 1009 } 978 1010 } 979 1011 */ 980 1012 // ======================================================================== 981 1013 … … 994 1026 } 995 1027 1028 bool CheckMinEventSize(size_t has, const char *name, size_t size) 1029 { 1030 if (has>size) 1031 return true; 1032 1033 ostringstream msg; 1034 msg << name << " - Received event has " << has << " bytes, but expected more."; 1035 Fatal(msg); 1036 return false; 1037 } 1038 1039 void EnterIntoAlertDB(const ToO::DataGRB &grb, const CheckVisibility &check, const bool &is_pointing) 1040 { 1041 // Do not enter satellite pointing alerts (as there are too many) 1042 if (is_pointing) 1043 return; 1044 1045 // Check for general visibility of the source during the night 1046 // If required: For better performance, both (sun and object) 1047 // RstTime could be calculated only here 1048 const double sun_set = fmod(check.vis_sun.set, 1); 1049 const double sun_rise = fmod(check.vis_sun.rise, 1); 1050 1051 const double rise = fmod(check.vis_obj.rise, 1); 1052 const double trans = fmod(check.vis_obj.transit, 1); 1053 const double set = fmod(check.vis_obj.set, 1); 1054 1055 if ((rise <sun_set || rise >sun_rise) && 1056 (trans<sun_set || trans>sun_rise) && 1057 (set <sun_set || set >sun_rise) ) 1058 return; 1059 1060 Info("Source might be observable."); 1061 1062 // Make an entry in the alter databse to issue a call 1063 const string queryS = 1064 "INSERT FlareAlerts.FlareTriggers SET\n" 1065 " fTriggerInserted=Now(),\n" 1066 " fNight="+to_string(Time().NightAsInt())+",\n" 1067 " fRunID="+to_string(grb.type)+",\n" 1068 " fTriggerType=7"; 1069 1070 const bool fDryRun = GetCurrentState()!=ToO::State::kArmed; 1071 1072 if (!fDryRun) 1073 { 1074 auto q = fConnection.query(queryS); 1075 q.execute(); 1076 if (!q.info().empty()) 1077 Info(q.info()); 1078 } 1079 else 1080 Out() << queryS << endl; 1081 } 1082 996 1083 int ScheduleGCN(const EventImp &evt) 997 1084 { 998 if (!Check EventSize(evt.GetSize(), "ScheduleGCN", 2+4+3*8))1085 if (!CheckMinEventSize(evt.GetSize(), "ScheduleGCN", sizeof(ToO::DataGRB))) 999 1086 return kSM_FatalError; 1000 1087 … … 1002 1089 1003 1090 const auto it = GCN::PaketTypes.find(grb.type); 1004 1005 1091 if (it==GCN::PaketTypes.end()) 1006 1092 { … … 1009 1095 } 1010 1096 1097 1098 const string name = evt.Ptr<char>(sizeof(ToO::DataGRB)); 1099 1011 1100 const auto &paket = it->second; 1012 1101 1013 1102 ostringstream out; 1014 out << "Received: '" << paket.name << "' ID=" << grb.type << " NUM=" << grb.trigid << " RA=" << grb.ra/15. << "h DEC=" << grb.dec << "\u00b0 ERR=" << grb.err; 1015 1016 /* 1017 const std::vector<int16_t> typelist = 1018 { 1019 51, // INTEGRAL_POINTDIR 1020 1021 53, // INTEGRAL_WAKEUP 1022 54, // INTEGRAL_REFINED 1023 55, // INTEGRAL_OFFLINE 1024 1025 // 56, // INTEGRAL_WEAK 1026 // 59, // KONUS_LC 1027 1028 60, // SWIFT_BAT_GRB_ALERT 1029 61, // SWIFT_BAT_GRB_POS_ACK 1030 62, // SWIFT_BAT_GRB_POS_NACK 1031 1032 83, // SWIFT_POINTDIR 1033 1034 97, // SWIFT_BAT_QL_POS 1035 1036 100, // AGILE_GRB_WAKEUP 1037 101, // AGILE_GRB_GROUND 1038 102, // AGILE_GRB_REFINED 1039 1040 110, // FERMI_GBM_FLT_POS 1041 111, // FERMI_GBM_GND_POS 1042 112, // FERMI_GBM_LC 1043 115, // FERMI_GBM_TRANS 1044 1045 123, // FERMI_LAT_TRANS 1046 125, // FERMI_LAT_MONITOR 1047 1048 // 134, // MAXI_UNKNOWN 1049 // 135, // MAXI_KNOWN 1050 // 136, // MAXI_TEST 1051 1052 157, // AMON_ICECUBE_COINC 1053 158, // AMON_ICECUBE_HESE 1054 1055 169, // AMON_ICECUBE_EHE 1056 171, // HAWC_BURST_MONITOR 1057 173, // ICECUBE_GOLD 1058 174, // ICECUBE_BRONZE 1059 }; 1060 */ 1103 out << name << " [" << paket.name << ":" << grb.type << "]: RA=" << grb.ra/15. << "h DEC=" << grb.dec << "\u00b0 ERR=" << grb.err; 1061 1104 1062 1105 Info(out); … … 1064 1107 1065 1108 const CheckVisibility check(grb.ra, grb.dec); 1066 1067 //Info(string("Source is")+(check.visible?" ":" NOT ")+"visible.");1068 1109 1069 1110 Info(string("Sun altitude: ")+(check.valid_sun ?"OK ":"failed")+" ["+Tools::Form("%5.1f", check.solarobj.fSunHrz.alt)+"\u00b0]"); … … 1073 1114 Info(string("Zenith angle: ")+(check.valid_zd ?"OK ":"failed")+" ["+Tools::Form("%5.1f", check.position.zd)+"\u00b0]"); 1074 1115 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 } 1077 1078 if (!check.visible) 1079 { 1080 Info("Source not observable... skipped."); 1081 return fConnection.connected() ? GetCurrentState() : ToO::State::kDisconnected; 1082 } 1083 1084 const string name = paket.instrument+"#"+to_string(grb.trigid); 1085 Info("Source is visible... name set to '"+name+"'"); 1086 1087 /* 1116 //Info(string("Rel. threshold: ")+(check.valid_threshold?"OK ":"failed")+" ["+Tools::Form("%5.1f", check.threshold)+"]"); 1117 } 1118 1119 /* 1088 1120 * Swift: https://gcn.gsfc.nasa.gov/swift.html 1089 1121 relevante Paket-Typen: … … 1149 1181 const bool is_pointing = grb.type==51 || grb.type==83; 1150 1182 1151 Schedule(name, grb.ra, grb.dec, check, is_pointing); 1183 try 1184 { 1185 if (!check.visible) 1186 { 1187 Info("Source not observable... skipped."); 1188 EnterIntoAlertDB(grb, check, is_pointing); 1189 1190 return fConnection.connected() ? GetCurrentState() : ToO::State::kDisconnected; 1191 } 1192 1193 Info("Source is visible... scheduling."); 1194 1195 if (!ScheduleImp(name, grb, check, is_pointing)) 1196 EnterIntoAlertDB(grb, check, is_pointing); 1197 } 1198 catch (const exception &e) 1199 { 1200 Error(string("SQL query failed: ")+e.what()); 1201 fConnection.disconnect(); 1202 } 1152 1203 1153 1204 return fConnection.connected() ? GetCurrentState() : ToO::State::kDisconnected; 1154 //return GetCurrentState(); 1205 } 1206 1207 int AddNewSource(const EventImp &evt) 1208 { 1209 if (!CheckMinEventSize(evt.GetSize(), "AddNewSource", 2*sizeof(double))) 1210 return kSM_FatalError; 1211 1212 const double &ra = evt.Ptr<double>()[0]; 1213 const double &dec = evt.Ptr<double>()[1]; 1214 const string name = evt.Ptr<char>(sizeof(double)*2); 1215 1216 ostringstream out; 1217 out << "New Source: '" << name << "' RA=" << ra << "h DEC=" << dec << "\u00b0"; 1218 1219 Info(out); 1220 1221 try 1222 { 1223 const Source source = GetSourceKey(name, ra*15, dec); 1224 1225 if (source.key>=0) // Not a known source 1226 Warn("Source already known with key="+to_string(source.key)+"."); 1227 else 1228 /*source.key =*/ AddSource(name, ra*15, dec); 1229 1230 } 1231 catch (const exception &e) 1232 { 1233 Error(string("SQL query failed: ")+e.what()); 1234 fConnection.disconnect(); 1235 } 1236 1237 return fConnection.connected() ? GetCurrentState() : ToO::State::kDisconnected; 1155 1238 } 1156 1239 … … 1192 1275 1193 1276 if (fConnection.connected() && GetCurrentState()<=ToO::State::kDisconnected) 1194 return ToO::State::k Connected;1277 return ToO::State::kArmed; 1195 1278 1196 1279 return GetCurrentState(); … … 1210 1293 "All needed subsystems are connected to their hardware, scheduling in progress."); 1211 1294 1212 AddEvent("GCN", "S:1; I:1;D:1;D:1;D:1")1295 AddEvent("GCN", "S:1;D:1;D:1;D:1;C") 1213 1296 (bind(&StateMachineToO::ScheduleGCN, this, placeholders::_1)) 1214 ("" 1215 "|type[int16]:TypeID (see HeaderGCN.h)" 1216 "|trig[uint32]:TriggerID" 1297 ("Schedule a ToO" 1298 "|type[int16]:Pakage or type ID (see HeadersToO.h)" 1217 1299 "|ra[deg]:Right ascension" 1218 1300 "|dec[deg]:Declination" 1219 "|err[deg]:Error radius"); 1301 "|err[deg]:Error radius" 1302 "|name[string]:Tentative or known source name"); 1303 1304 AddEvent("ADD_SOURCE", "D:1;D:1;C") 1305 (bind(&StateMachineToO::AddNewSource, this, placeholders::_1)) 1306 ("Add a new source to the databse if not yet available" 1307 "|ra[h]:Right ascension" 1308 "|dec[deg]:Declination" 1309 "|name[string]:Monitored source name"); 1220 1310 1221 1311 AddEvent("START", "")
Note:
See TracChangeset
for help on using the changeset viewer.