Commit ca4a0ec62047af10a6223543604abc8b839815ce

Authored by Alain Klotz
1 parent 556ee616
Exists in dev

Scheduler bugfix.

src/core/pyros_django/majordome/agent/Agent.py
... ... @@ -45,6 +45,7 @@ from datetime import datetime
45 45 from typing import Final, Sequence, Iterable, Mapping, MutableMapping, Dict, List, Tuple, Union, Any, Optional, Literal
46 46 import ast
47 47 from inspect import signature
  48 +import numpy as np
48 49  
49 50 import os
50 51 from pathlib import Path
... ... @@ -3517,9 +3518,80 @@ class Agent:
3517 3518 ephem_target = pickle.load(open(eph_file,"rb"))
3518 3519 self._oc['config'].fn.fcontext = fcontext0
3519 3520 return ephem_target
3520   -
3521 3521  
3522   -
  3522 + def update_sun_moon_ephems(self):
  3523 + """Update the files in the ephems folder.
  3524 +
  3525 + The file 'duration.pickle' contains the start, end for each night and other informations to make quota computations.
  3526 +
  3527 + Check if the file 'duration.pickle' exists in the ephem/period directory.
  3528 + If yes, returns the contents of the 'duration.pickle' file.
  3529 + If not, check each ephemeris file existance for Sun and Moon and compute them if needed.
  3530 +
  3531 + Returns:
  3532 + Dictionary with night string as key. Values are a list of integers:
  3533 +
  3534 + * total1: Cumulative sum of seconds of previous night durations at the start of the night.
  3535 + * total2: Cumulative sum of seconds of previous night durations at the end of the night.
  3536 + * total_night: Number of seconds available during this night.
  3537 + * sec1: Index of seconds for the start of the night.
  3538 + * sec2: Index of seconds for the end of the night.
  3539 +
  3540 + """
  3541 +
  3542 + # --- Get the period object
  3543 + operiod = Period.objects.exploitation_period()
  3544 +
  3545 + # --- Get the period ID
  3546 + period_id = operiod.get_id_as_str()
  3547 +
  3548 + # --- Select the context of ephemeris
  3549 + fcontext0 = self._oc['config'].fn.fcontext
  3550 + self._oc['config'].fn.fcontext = "pyros_eph"
  3551 +
  3552 + # --- Check if durations.pickle file exists
  3553 + rootdir = self._oc['config'].fn.rootdir
  3554 + filename = os.path.join(rootdir, period_id, "durations.pickle")
  3555 + self.dprint(f"Read {filename=}")
  3556 + if os.path.exists(filename):
  3557 + self._oc['config'].fn.fcontext = fcontext0
  3558 + return pickle.load(open(filename, "wb"))
  3559 +
  3560 + # --- Get the period limit dates jd1, jd2
  3561 + self.dprint(f"{dir(operiod)=}")
  3562 + d = operiod.start_date.isoformat()
  3563 + night_start = d[0:4]+d[5:7]+d[8:10]
  3564 + jd1, _ = self.config.fn.night2date(night_start)
  3565 + d = operiod.end_date.isoformat()
  3566 + night_end = d[0:4]+d[5:7]+d[8:10]
  3567 + jd2, _ = self.config.fn.night2date(night_end)
  3568 + self.dprint(f"{night_start=} {night_end=}")
  3569 + self.dprint(f"{jd1=} {jd2=}")
  3570 +
  3571 + # --- Loop over dates of the period to create ephems
  3572 + targets = ['sun', 'moon']
  3573 + jd = jd1 + 0.1
  3574 + total1 = 0
  3575 + night_info = {}
  3576 + while jd < jd2:
  3577 + night = self._oc['config'].fn.date2night(jd)
  3578 + for target in targets:
  3579 + self.dprint(f"{night=} {target=}")
  3580 + ephem = self.ephem_target2night(target, jd)
  3581 + if target == "sun":
  3582 + ks = np.where(ephem['alt'] < self._duskelev)
  3583 + total_night = len(ks[0])
  3584 + total2 = total1 + total_night
  3585 + sec1 = ks[0][0]
  3586 + sec2 = ks[0][-1]
  3587 + night_info[night] = [int(total1), int(total2), int(total_night), sec1, sec2]
  3588 + total1 = total2
  3589 + jd += 1
  3590 + night_info['total'] = [0, int(total2), int(total2), -1, -1]
  3591 + self.dprint(f"Write {filename=}")
  3592 + pickle.dump(night_info, open(filename, "wb"))
  3593 + return night_info
  3594 +
3523 3595 """
3524 3596 =================================================================
3525 3597 MAIN
... ... @@ -3541,7 +3613,6 @@ def parse_args(args):
3541 3613 parser = argparse.ArgumentParser(description='Start an agent.')
3542 3614 parser.add_argument("--computer",dest="computer",help='Launch agent with simulated computer hostname',action="store")
3543 3615 parser.add_argument("-t", action="store_true")
3544   - parser.add_argument("-d", action="store_true" )
3545 3616 args = vars(parser.parse_args(args))
3546 3617 return args
3547 3618  
... ...
src/core/pyros_django/scheduling/A_Scheduler.py
... ... @@ -639,80 +639,7 @@ class A_Scheduler(Agent):
639 639 def load_sequence(self):
640 640 sequence = ""
641 641 return sequence
642   -
643   - def update_sun_moon_ephems(self):
644   - """Update the files in the ephems folder.
645   -
646   - The file 'duration.pickle' contains the start, end for each night and other informations to make quota computations.
647   -
648   - Check if the file 'duration.pickle' exists in the ephem/period directory.
649   - If yes, returns the contents of the 'duration.pickle' file.
650   - If not, check each ephemeris file existance for Sun and Moon and compute them if needed.
651   -
652   - Returns:
653   - Dictionary with night string as key. Values are a list of integers:
654   -
655   - * total1: Cumulative sum of seconds of previous night durations at the start of the night.
656   - * total2: Cumulative sum of seconds of previous night durations at the end of the night.
657   - * total_night: Number of seconds available during this night.
658   - * sec1: Index of seconds for the start of the night.
659   - * sec2: Index of seconds for the end of the night.
660   -
661   - """
662 642  
663   - # --- Get the period object
664   - operiod = Period.objects.exploitation_period()
665   -
666   - # --- Get the period ID
667   - period_id = operiod.get_period_id_as_str()
668   -
669   - # --- Select the context of ephemeris
670   - fcontext0 = self._oc['config'].fn.fcontext
671   - self._oc['config'].fn.fcontext = "pyros_eph"
672   -
673   - # --- Check if durations.pickle file exists
674   - rootdir = self._oc['config'].fn.rootdir
675   - filename = os.path.join(rootdir, period_id, "durations.pickle")
676   - self.dprint(f"Read {filename=}")
677   - if os.path.exists(filename):
678   - self._oc['config'].fn.fcontext = fcontext0
679   - return pickle.load(open(filename, "wb"))
680   -
681   - # --- Get the period limit dates jd1, jd2
682   - self.dprint(f"{dir(operiod)=}")
683   - d = operiod.start_date.isoformat()
684   - night_start = d[0:4]+d[5:7]+d[8:10]
685   - jd1, _ = self.config.fn.night2date(night_start)
686   - d = operiod.end_date.isoformat()
687   - night_end = d[0:4]+d[5:7]+d[8:10]
688   - jd2, _ = self.config.fn.night2date(night_end)
689   - self.dprint(f"{night_start=} {night_end=}")
690   - self.dprint(f"{jd1=} {jd2=}")
691   -
692   - # --- Loop over dates of the period to create ephems
693   - targets = ['sun', 'moon']
694   - jd = jd1 + 0.1
695   - total1 = 0
696   - night_info = {}
697   - while jd < jd2:
698   - night = self._oc['config'].fn.date2night(jd)
699   - for target in targets:
700   - self.dprint(f"{night=} {target=}")
701   - ephem = self.ephem_target2night(target, jd)
702   - if target == "sun":
703   - ks = np.where(ephem['alt'] < self._duskelev)
704   - total_night = len(ks[0])
705   - total2 = total1 + total_night
706   - sec1 = ks[0][0]
707   - sec2 = ks[0][-1]
708   - night_info[night] = [int(total1), int(total2), int(total_night), sec1, sec2]
709   - total1 = total2
710   - jd += 1
711   - night_info['total'] = [0, int(total2), int(total2), -1, -1]
712   - self.dprint(f"Write {filename=}")
713   - pickle.dump(night_info, open(filename, "wb"))
714   - return night_info
715   -
716 643 def get_infos(self):
717 644 self._fn.fcontext = "pyros_seq"
718 645 rootdir = self._fn.rootdir
... ... @@ -722,7 +649,7 @@ class A_Scheduler(Agent):
722 649 self._routine_running = self.RUNNING_NOTHING
723 650 return
724 651 # retourne un str -> id de la période sous le format Pxxx
725   - period_id = operiod.get_period_id_as_str()
  652 + period_id = operiod.get_id_as_str()
726 653  
727 654 night_id = self._fn.date2night("now")
728 655 subdir = os.path.join(period_id, night_id)
... ...