FACT++  1.0
int main ( int  argc,
const char *  argv[] 
)

Definition at line 596 of file makeschedule.cc.

References begin, CheckStartupAndShutdown(), Configuration::DoParse(), end, Source::equ, FillSql(), Configuration::Get(), Time::GetAsStr(), Nova::GetSolarRst(), Configuration::Has(), i, Time::JD(), Source::max_current, Source::max_zd, Source::maxcurrent, Source::maxzd, Source::penalty, Source::preobs, Print(), PrintHelp(), PrintUsage(), RemoveMiniSources(), RescheduleFirstSources(), RescheduleIntermediateSources(), RescheduleLastSources(), Time::SetFromStr(), Configuration::SetPrintUsage(), SetupConfiguration(), SortByThreshold(), time, MyDouble::val, MyDouble::valid, and Configuration::Vec().

597 {
598 // gROOT->SetBatch();
599 
600  Configuration conf(argv[0]);
601  conf.SetPrintUsage(PrintUsage);
602  SetupConfiguration(conf);
603 
604  if (!conf.DoParse(argc, argv, PrintHelp))
605  return 127;
606 
607  // ------------------ Eval config ---------------------
608 
609  const int enter = conf.Has("enter-schedule-into-database") ? (conf.Get<bool>("enter-schedule-into-database") ? 1 : -1) : 0;
610  if (enter && !conf.Has("schedule-database"))
611  throw runtime_error("enter-schedule-into-database required schedule-database.");
612 
613  Time time;
614  if (conf.Has("date"))
615  time.SetFromStr(conf.Get<string>("date")+" 12:00:00");
616 
617  if (enter && floor(time.JD())<ceil(Time().JD()))
618  throw runtime_error("Only future schedules can be entered into the database.");
619 
620  Source::max_current = conf.Get<double>("max-current");
621  Source::max_zd = conf.Get<double>("max-zd");
622 
623  const double startup_offset = conf.Get<double>("startup.offset")/60/24;
624 
625  const double angle_sun_set = conf.Get<double>("data-taking.start");
626  const double angle_sun_rise = conf.Get<double>("data-taking.end");
627 
628  if (startup_offset<0 || startup_offset>120)
629  throw runtime_error("Only values [0;120] are allowed for startup.offset");
630 
631  if (angle_sun_set>-6)
632  throw runtime_error("datataking.start not allowed before sun at -6deg");
633 
634  if (angle_sun_rise>-6)
635  throw runtime_error("datataking.end not allowed after sun at -6deg");
636 
637  // -12: nautical
638  // Sun set with the same date than th provided date
639  // Sun rise on the following day
640  const RstTime sun_set = GetSolarRst(floor(time.JD())-0.5, angle_sun_set);
641  const RstTime sun_rise = GetSolarRst(floor(time.JD())+0.5, angle_sun_rise);
642 
643  const double sunset = ceil(sun_set.set*24*60) /24/60 + 1e-9;
644  const double sunrise = floor(sun_rise.rise*24*60)/24/60 + 1e-9;
645 
646  cout << "\n";
647 
648  cout << "Date: " << Time(floor(sunset)).GetAsStr() << "\n";
649  cout << "Set: " << Time(sunset).GetAsStr() << " [" << Time(sun_set.set) << "]\n";
650  cout << "Rise: " << Time(sunrise).GetAsStr() << " [" << Time(sun_rise.rise) << "]\n";
651 
652  cout << "\n";
653 
654  cout << "Global maximum current: " << Source::max_current << " uA/pix\n";
655  cout << "Global zenith distance: " << Source::max_zd << " deg\n";
656 
657  cout << "\n";
658 
659  // ------------- Get Sources from databasse ---------------------
660 
661  const vector<string> ourcenames = conf.Vec<string>("source");
662  const vector<string> sourcenames = conf.Vec<string>("source");
663  cout << "Nsources = " << sourcenames.size() << "\n";
664 
665  string query = "SELECT fSourceName, fSourceKEY, fRightAscension, fDeclination FROM Source WHERE fSourceTypeKEY=1";
666  if (sourcenames.size()>0)
667  query += " AND fSourceName IN ('" + boost::algorithm::join(sourcenames, "', '")+"')";
668 
669  const string sourcedb = conf.Get<string>("source-database");
670  const mysqlpp::StoreQueryResult res =
671  Database(sourcedb).query(query).store();
672 
673  // ------------------ Eval config ---------------------
674 
675  vector<Source> sources;
676  for (const auto &row: res)
677  {
678  const string name = string(row[0]);
679 
680  Source src(name, row[1]);
681 
682  src.equ.ra = double(row[2])*15;
683  src.equ.dec = double(row[3]);
684 
685  src.maxzd = MyDouble(conf, "setup."+name+".max-zd");
686  src.maxcurrent = MyDouble(conf, "setup."+name+".max-current");
687  src.penalty = conf.Has("setup."+name+".penalty") ?
688  conf.Get<double>("setup."+name+".penalty") : 1;
689 
690  src.preobs = conf.Vec<string>("preobs."+name);
691 
692 
693  cout << "[" << name << "]";
694 
695  if (src.maxzd.valid)
696  cout << " Zd<" << src.maxzd.val;
697  if (src.penalty!=1)
698  cout << " Penalty=" << src.penalty;
699 
700  cout << " " << boost::algorithm::join(src.preobs, "+") << endl;
701 
702  /*
703  RstTime t1 = GetObjectRst(floor(sunset)-1, src.equ);
704  RstTime t2 = GetObjectRst(floor(sunset), src.equ);
705 
706  src.rst.transit = t1.transit<floor(sunset) ? t2.transit : t1.transit;
707  src.rst.rise = t1.rise>src.rst.transit ? t2.rise : t1.rise;
708  src.rst.set = t1.set <src.rst.transit ? t2.set : t1.set;
709  */
710 
711  sources.emplace_back(src);
712  }
713  cout << endl;
714 
715  // -------------------------------------------------------------------------
716 
717  vector<Source> obs;
718 
719  const uint32_t n = nearbyint((sunrise-sunset)*24*60);
720  for (uint32_t i=0; i<n; i++)
721  {
722  const double jd = sunset + i/24./60.;
723 
724  const SolarObjects so(jd);
725 
726  vector<Source> vis;
727  for (auto& src: sources)
728  {
729  if (src.calcThreshold(so))
730  vis.emplace_back(src);
731  }
732 
733  // In case no source was found, add a sleep source
734  Source src("SLEEP");
735  vis.emplace_back(src);
736 
737  // Source has higher priority if minimum observation time not yet fullfilled
738  sort(vis.begin(), vis.end(), SortByThreshold);
739 
740  if (obs.size()>0 && obs.back().name==vis[0].name)
741  continue;
742 
743  vis[0].begin = jd;
744  obs.emplace_back(vis[0]);
745  }
746 
747  if (obs.size()==0)
748  {
749  cout << "No source found." << endl;
750  return 1;
751  }
752 
753  // -------------------------------------------------------------------------
754 
755  for (auto it=obs.begin(); it<obs.end()-1; it++)
756  it[0].end = it[1].begin;
757  obs.back().end = sunrise;
758 
759  // -------------------------------------------------------------------------
760 
761  Print(obs, startup_offset);
762  cout << endl;
763 
764  // -------------------------------------------------------------------------
765 
766  while (RescheduleFirstSources(obs));
767  while (RescheduleLastSources(obs));
768  while (RescheduleIntermediateSources(obs));
769 
770  RemoveMiniSources(obs);
772 
773  // ---------------------------------------------------------------------
774 
775  cout << endl;
776  Print(obs, startup_offset);
777  cout << endl;
778 
779  // ---------------------------------------------------------------------
780 
781  if (!enter)
782  return 0;
783 
784  const string scheduledb = conf.Get<string>("schedule-database");
785 
786  Database db(scheduledb);
787 
788  if (enter>0)
789  db.query("LOCK TABLES Schedule WRITE");
790 
791  const int rc = FillSql(db, enter, obs, startup_offset);
792 
793  if (enter>0)
794  db.query("UNLOCK TABLES");
795 
796  // ---------------------------------------------------------------------
797 
798  return rc;
799 }
bool RescheduleFirstSources(vector< Source > &obs)
void Print(const vector< Source > &obs, double startup_offset)
bool RescheduleIntermediateSources(vector< Source > &obs)
bool SortByThreshold(const Source &i, const Source &j)
void PrintHelp()
Definition: makeschedule.cc:50
int i
Definition: db_dim_client.c:21
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
static double max_zd
void PrintUsage()
Definition: makeschedule.cc:41
double begin
void CheckStartupAndShutdown(vector< Source > &obs)
double JD() const
Definition: Time.h:87
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
void RemoveMiniSources(vector< Source > &obs)
double end
Commandline parsing, resource file parsing and database access.
Definition: Configuration.h:9
void SetFromStr(const std::string &str, const char *fmt="%Y-%m-%d %H:%M:%S")
Definition: Time.cc:273
int FillSql(Database &db, int enter, const vector< Source > &obs, double startup_offset)
RstTime GetSolarRst(double jd, const LnLatPosn &obs, double hrz=LN_SOLAR_STANDART_HORIZON)
Definition: nova.h:97
static double max_current
ln_rst_time RstTime
Definition: nova.h:13
void SetupConfiguration(Configuration &conf)
Definition: makeschedule.cc:16
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240
bool RescheduleLastSources(vector< Source > &obs)

+ Here is the call graph for this function: