From 545ecb3054a87f3503e44601df21a87b0514ee32 Mon Sep 17 00:00:00 2001
From: Alexis Koralewski <akoralewski@irap.omp.eu>
Date: Tue, 25 Apr 2023 18:06:25 +0200
Subject: [PATCH] Add ephem to pickle seq & add pickle creation when submitting seq via api

---
 CHANGELOG                                          |  7 +++++++
 src/core/pyros_django/api/views.py                 | 11 ++++++-----
 src/core/pyros_django/routine_manager/functions.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 65 insertions(+), 16 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 5960c29..b7f1531 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,10 @@
+25-04-2023 (AKo): v0.6.22.0
+	- Add ephem to pickle seq & add pickle creation when submitting seq via api
+
+06-04-2023 (AKo): v0.6.21.1
+	- Try to fix error when submitting seq simplified (no start_date)
+
+
 04-04-2023 (AKo): v0.6.21.0
 	- Improve check of ObsConfig load function and renaming functions of ObsConfig & PyrosUser 
 	- Add schema (Md file) to explain workflow when user import a sequence file
diff --git a/src/core/pyros_django/api/views.py b/src/core/pyros_django/api/views.py
index 87fa382..0285fce 100644
--- a/src/core/pyros_django/api/views.py
+++ b/src/core/pyros_django/api/views.py
@@ -23,7 +23,7 @@ from routine_manager.models import Sequence, Album, Plan
 
 
 
-from routine_manager.functions import check_sequence_file_validity_and_save
+from routine_manager.functions import check_sequence_file_validity_and_save, create_sequence_pickle
 from src.pyros_logger import log
 import yaml
 # Create your views here.
@@ -63,7 +63,7 @@ class UserViewSet(viewsets.ModelViewSet):
         if user_role in ("Unit-PI",  "Admin"):
             queryset = PyrosUser.objects.all().order_by("-created")
         else:
-            sp_of_current_user = user.get_scientific_program()
+            sp_of_current_user = user.get_scientific_programs()
             pyros_users_with_roles = []
             for sp in sp_of_current_user:
                 for sp_period in sp.SP_Periods.all():
@@ -101,7 +101,7 @@ class SequenceViewSet(viewsets.ModelViewSet):
         if user_role in ("Unit-PI",  "Admin"):
             return Sequence.objects.all().order_by("-updated")
         else:
-            sp_of_user = user.get_scientific_program()
+            sp_of_user = user.get_scientific_programs()
             return Sequence.objects.filter(scientific_program__in=sp_of_user).order_by("-updated")
             #return Sequence.objects.filter(pyros_user=user).order_by("-updated")
 
@@ -126,7 +126,7 @@ class ScientificProgramViewSet(viewsets.ModelViewSet):
         if user_role in ("Unit-PI",  "Admin"):
             return ScientificProgram.objects.all().order_by("-updated")
         else:
-            return user.get_scientific_program().order_by("-updated")
+            return user.get_scientific_programs().order_by("-updated")
 
 
 class SPPeriodViewSet(viewsets.ModelViewSet):
@@ -150,7 +150,7 @@ class SPPeriodViewSet(viewsets.ModelViewSet):
         if user_role in ("Unit-PI",  "Admin"):
             return SP_Period.objects.all().order_by("-scientific_program")
         else:
-            user_scientific_programs = user.get_scientific_program()
+            user_scientific_programs = user.get_scientific_programs()
             return SP_Period.objects.filter(scientific_program__in=user_scientific_programs).order_by("-scientific_program")
 
 
@@ -264,6 +264,7 @@ def submit_sequence_file(request):
         response = check_sequence_file_validity_and_save(yaml_content, request)
         if response.get("succeed") == True:
             seq = Sequence.objects.get(id=response.get("sequence_id"))
+            create_sequence_pickle(seq)
             log.info(
                 f"User {request.user} did action submit sequence {seq} for period {seq.period} ")
     return Response(response)
diff --git a/src/core/pyros_django/routine_manager/functions.py b/src/core/pyros_django/routine_manager/functions.py
index e413a66..74f3c22 100644
--- a/src/core/pyros_django/routine_manager/functions.py
+++ b/src/core/pyros_django/routine_manager/functions.py
@@ -12,6 +12,9 @@ from django.db import IntegrityError
 # Project imports
 from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig
 from django.http import HttpRequest
+from config.pyros.config_pyros import ConfigPyros
+# guitastro import
+import vendor.guitastro.src.guitastro as guitastro 
 #from silk.profiling.profiler import silk_profile
 
 #@silk_profile(name="check_sequence_file")
@@ -158,6 +161,11 @@ def check_sequence_file_validity_and_save(yaml_content: dict, request: HttpReque
     if Period.objects.next_period() != None and Period.objects.next_period().start_date < seq.start_date.date():
         period = Period.objects.next_period()
     seq.period = period
+    fn = guitastro.FileNames()
+    home = config.getHome()
+    guitastro_home = guitastro.Home(home)
+    fn.longitude = guitastro_home.longitude
+    seq.night_id = fn.get_night(seq.start_date.isoformat()[:19])
     try:
         seq.save()
     except IntegrityError as e:
@@ -331,6 +339,7 @@ def process_sequence(yaml_content, seq, config, is_simplified, result, user_sp):
         # pour lisibilité, simplicité et éviter redondance
         yaml_field = yaml_content["sequence"][field]
         value = yaml_field if is_simplified else yaml_field["value"]
+        
         ''' (orig)
         if is_simplified:
             value = yaml_content["sequence"][field]
@@ -414,15 +423,47 @@ def create_sequence_pickle(sequence):
         for plan in album.plans.all():
             fullseq_dict["albums"][f"{album.name}"]["plans"].append(model_to_dict(instance=plan))
     period = sequence.period
-    root_project_path = os.environ.get("PROJECT_ROOT_PATH")
-    data_path = root_project_path + "/data/"
-    if not os.path.exists(data_path + "sequences_pickle"):
-        os.mkdir(data_path +"sequences_pickle")
-    if not os.path.exists(data_path + f"sequences_pickle/P{period.id}"):
-        os.mkdir(data_path + f"sequences_pickle/P{period.id}")
-    if not os.path.exists(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}"):
-        os.mkdir(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}")
-    seq_pickle_file_name = data_path +f"./sequences_pickle/P{period.id}/{sequence.night_id}/{sequence.id}.p"
+    # Old folder & file creation
+    # root_project_path = os.environ.get("PROJECT_ROOT_PATH")
+    # data_path = root_project_path + "/data/"
+    # if not os.path.exists(data_path + "sequences_pickle"):
+    #     os.mkdir(data_path +"sequences_pickle")
+    # if not os.path.exists(data_path + f"sequences_pickle/P{period.id}"):
+    #     os.mkdir(data_path + f"sequences_pickle/P{period.id}")
+    # if not os.path.exists(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}"):
+    #     os.mkdir(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}")
+    # seq_pickle_file_name = data_path +f"./sequences_pickle/P{period.id}/{sequence.night_id}/{sequence.id}.p"
     # get guitastro ephemeris
-    #fullseq_dict["ephem"] = fn.ephem(sequence.target)
-    pickle.dump(fullseq_dict,open(seq_pickle_file_name,"wb"))
\ No newline at end of file
+    unit_name = os.environ["unit_name"]
+    config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"], unit_name)
+    pyros_config = ConfigPyros(os.environ["pyros_config_file"])
+    pyros_config.fn.fcontext = "pyros_seq"
+    home = guitastro.Home(config.getHome())
+    pyros_config.fn.longitude(home.longitude)
+    if len(str(period.id)) < 3:
+        period_id = str(period.id)
+        while len(period_id) < 3:
+            period_id = "0" + period_id
+    fn_param = {
+        "period" : f"P{period_id}",
+        "version": "1",
+        "unit": config.unit_name,
+        "date": sequence.night_id,
+        "id_seq": sequence.id
+    }
+    fname = pyros_config.fn.naming_set(fn_param)
+    pyros_config.fn.update_subdir_fname_from_filename(fname)
+    fpath_name = pyros_config.fn.join()
+    # create dirs if they don't exist
+    os.makedirs(os.path.dirname(fpath_name), exist_ok=True)
+    eph = guitastro.Ephemeris()
+    eph.set_home(config.getHome())
+    # duskelev a parametrer dans obsconfig (yml)
+    duskelev = -7
+    try:
+        # TODO remplacer les none par les fichiers pickle de ephem_sun & ephem_moon
+        fullseq_dict["ephem"] = eph.target2night(fullseq_dict["sequence"]["config_attributes"]["target"], sequence.night_id, None, None, preferance=sequence.start_expo_pref, duskelev=duskelev)
+    except:
+        # target incorrecte 
+        pass
+    pickle.dump(fullseq_dict,open(fpath_name,"wb"))
\ No newline at end of file
--
libgit2 0.21.2