Commit 5871ed5e20214ce39cfcbb0a69cb550dd9f2ec21

Authored by Alexis Koralewski
1 parent 3be8b2fc
Exists in dev

Add ephem_target2night in Agent.py

Showing 1 changed file with 41 additions and 1 deletions   Show diff stats
src/core/pyros_django/majordome/agent/Agent.py
... ... @@ -153,6 +153,12 @@ import config.old_config as config_old
153 153 #from config import *
154 154  
155 155 from majordome.models import AgentSurvey, AgentCmd, AgentLogs
  156 +from user_mgmt.models import Period
  157 +
  158 +from vendor.guitastro.src.guitastro import Ephemeris
  159 +import pickle
  160 +
  161 +
156 162 CmdException = AgentCmd.CmdException
157 163 CmdExceptionUnknown = AgentCmd.CmdExceptionUnknown
158 164 CmdExceptionUnimplemented = AgentCmd.CmdExceptionUnimplemented
... ... @@ -3476,7 +3482,41 @@ class Agent:
3476 3482 if loginfo:
3477 3483 log.info(self._oc['config'].fn.fcontexts_human())
3478 3484  
3479   -
  3485 + def ephem_target2night(self, target:str) -> dict:
  3486 + eph = Ephemeris()
  3487 + eph.set_home(self._oc['config'].getHome())
  3488 + fcontext0 = self._oc['config'].fn.fcontext
  3489 + # --- Build the path and file name of the sun ephemeris
  3490 + self._oc['config'].fn.fcontext = "pyros_eph"
  3491 + night = self._oc['config'].fn.date2night("now")
  3492 + operiod = Period.objects.exploitation_period()
  3493 + if operiod == None:
  3494 + log.info("No period valid in the database")
  3495 + self._routine_running = self.RUNNING_NOTHING
  3496 + return
  3497 + period_id = str(operiod.id)
  3498 + if len(str(operiod.id)) < 3:
  3499 + while len(period_id) < 3:
  3500 + period_id = "0" + period_id
  3501 + period_id = "P" + period_id
  3502 + fn_param = {
  3503 + "period" : f"{period_id}",
  3504 + "date": night,
  3505 + "version": "1",
  3506 + "unit": self._oc['config'].unit_name,
  3507 + "target": target
  3508 + }
  3509 + fname = self._fn.naming_set(fn_param)
  3510 + eph_file = self._fn.joinabs(fname)
  3511 + # --- Compute the sun ephemeris if needed. Save it as pickle.
  3512 + if not os.path.exists(eph_file):
  3513 + ephem_target = eph.target2night(target, night, None, None)
  3514 + os.makedirs(os.path.dirname(eph_file), exist_ok=True)
  3515 + pickle.dump(ephem_target, open(eph_file,"wb"))
  3516 + else:
  3517 + ephem_target = pickle.load(open(eph_file,"rb"))
  3518 + self._oc['config'].fn.fcontext = fcontext0
  3519 + return ephem_target
3480 3520  
3481 3521 """
3482 3522 =================================================================
... ...