Commit 545ecb3054a87f3503e44601df21a87b0514ee32

Authored by Alexis Koralewski
1 parent cbf8131a
Exists in dev

Add ephem to pickle seq & add pickle creation when submitting seq via api

CHANGELOG
  1 +25-04-2023 (AKo): v0.6.22.0
  2 + - Add ephem to pickle seq & add pickle creation when submitting seq via api
  3 +
  4 +06-04-2023 (AKo): v0.6.21.1
  5 + - Try to fix error when submitting seq simplified (no start_date)
  6 +
  7 +
1 8 04-04-2023 (AKo): v0.6.21.0
2 9 - Improve check of ObsConfig load function and renaming functions of ObsConfig & PyrosUser
3 10 - Add schema (Md file) to explain workflow when user import a sequence file
... ...
src/core/pyros_django/api/views.py
... ... @@ -23,7 +23,7 @@ from routine_manager.models import Sequence, Album, Plan
23 23  
24 24  
25 25  
26   -from routine_manager.functions import check_sequence_file_validity_and_save
  26 +from routine_manager.functions import check_sequence_file_validity_and_save, create_sequence_pickle
27 27 from src.pyros_logger import log
28 28 import yaml
29 29 # Create your views here.
... ... @@ -63,7 +63,7 @@ class UserViewSet(viewsets.ModelViewSet):
63 63 if user_role in ("Unit-PI", "Admin"):
64 64 queryset = PyrosUser.objects.all().order_by("-created")
65 65 else:
66   - sp_of_current_user = user.get_scientific_program()
  66 + sp_of_current_user = user.get_scientific_programs()
67 67 pyros_users_with_roles = []
68 68 for sp in sp_of_current_user:
69 69 for sp_period in sp.SP_Periods.all():
... ... @@ -101,7 +101,7 @@ class SequenceViewSet(viewsets.ModelViewSet):
101 101 if user_role in ("Unit-PI", "Admin"):
102 102 return Sequence.objects.all().order_by("-updated")
103 103 else:
104   - sp_of_user = user.get_scientific_program()
  104 + sp_of_user = user.get_scientific_programs()
105 105 return Sequence.objects.filter(scientific_program__in=sp_of_user).order_by("-updated")
106 106 #return Sequence.objects.filter(pyros_user=user).order_by("-updated")
107 107  
... ... @@ -126,7 +126,7 @@ class ScientificProgramViewSet(viewsets.ModelViewSet):
126 126 if user_role in ("Unit-PI", "Admin"):
127 127 return ScientificProgram.objects.all().order_by("-updated")
128 128 else:
129   - return user.get_scientific_program().order_by("-updated")
  129 + return user.get_scientific_programs().order_by("-updated")
130 130  
131 131  
132 132 class SPPeriodViewSet(viewsets.ModelViewSet):
... ... @@ -150,7 +150,7 @@ class SPPeriodViewSet(viewsets.ModelViewSet):
150 150 if user_role in ("Unit-PI", "Admin"):
151 151 return SP_Period.objects.all().order_by("-scientific_program")
152 152 else:
153   - user_scientific_programs = user.get_scientific_program()
  153 + user_scientific_programs = user.get_scientific_programs()
154 154 return SP_Period.objects.filter(scientific_program__in=user_scientific_programs).order_by("-scientific_program")
155 155  
156 156  
... ... @@ -264,6 +264,7 @@ def submit_sequence_file(request):
264 264 response = check_sequence_file_validity_and_save(yaml_content, request)
265 265 if response.get("succeed") == True:
266 266 seq = Sequence.objects.get(id=response.get("sequence_id"))
  267 + create_sequence_pickle(seq)
267 268 log.info(
268 269 f"User {request.user} did action submit sequence {seq} for period {seq.period} ")
269 270 return Response(response)
... ...
src/core/pyros_django/routine_manager/functions.py
... ... @@ -12,6 +12,9 @@ from django.db import IntegrityError
12 12 # Project imports
13 13 from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig
14 14 from django.http import HttpRequest
  15 +from config.pyros.config_pyros import ConfigPyros
  16 +# guitastro import
  17 +import vendor.guitastro.src.guitastro as guitastro
15 18 #from silk.profiling.profiler import silk_profile
16 19  
17 20 #@silk_profile(name="check_sequence_file")
... ... @@ -158,6 +161,11 @@ def check_sequence_file_validity_and_save(yaml_content: dict, request: HttpReque
158 161 if Period.objects.next_period() != None and Period.objects.next_period().start_date < seq.start_date.date():
159 162 period = Period.objects.next_period()
160 163 seq.period = period
  164 + fn = guitastro.FileNames()
  165 + home = config.getHome()
  166 + guitastro_home = guitastro.Home(home)
  167 + fn.longitude = guitastro_home.longitude
  168 + seq.night_id = fn.get_night(seq.start_date.isoformat()[:19])
161 169 try:
162 170 seq.save()
163 171 except IntegrityError as e:
... ... @@ -331,6 +339,7 @@ def process_sequence(yaml_content, seq, config, is_simplified, result, user_sp):
331 339 # pour lisibilitรฉ, simplicitรฉ et รฉviter redondance
332 340 yaml_field = yaml_content["sequence"][field]
333 341 value = yaml_field if is_simplified else yaml_field["value"]
  342 +
334 343 ''' (orig)
335 344 if is_simplified:
336 345 value = yaml_content["sequence"][field]
... ... @@ -414,15 +423,47 @@ def create_sequence_pickle(sequence):
414 423 for plan in album.plans.all():
415 424 fullseq_dict["albums"][f"{album.name}"]["plans"].append(model_to_dict(instance=plan))
416 425 period = sequence.period
417   - root_project_path = os.environ.get("PROJECT_ROOT_PATH")
418   - data_path = root_project_path + "/data/"
419   - if not os.path.exists(data_path + "sequences_pickle"):
420   - os.mkdir(data_path +"sequences_pickle")
421   - if not os.path.exists(data_path + f"sequences_pickle/P{period.id}"):
422   - os.mkdir(data_path + f"sequences_pickle/P{period.id}")
423   - if not os.path.exists(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}"):
424   - os.mkdir(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}")
425   - seq_pickle_file_name = data_path +f"./sequences_pickle/P{period.id}/{sequence.night_id}/{sequence.id}.p"
  426 + # Old folder & file creation
  427 + # root_project_path = os.environ.get("PROJECT_ROOT_PATH")
  428 + # data_path = root_project_path + "/data/"
  429 + # if not os.path.exists(data_path + "sequences_pickle"):
  430 + # os.mkdir(data_path +"sequences_pickle")
  431 + # if not os.path.exists(data_path + f"sequences_pickle/P{period.id}"):
  432 + # os.mkdir(data_path + f"sequences_pickle/P{period.id}")
  433 + # if not os.path.exists(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}"):
  434 + # os.mkdir(data_path +f"sequences_pickle/P{period.id}/{sequence.night_id}")
  435 + # seq_pickle_file_name = data_path +f"./sequences_pickle/P{period.id}/{sequence.night_id}/{sequence.id}.p"
426 436 # get guitastro ephemeris
427   - #fullseq_dict["ephem"] = fn.ephem(sequence.target)
428   - pickle.dump(fullseq_dict,open(seq_pickle_file_name,"wb"))
429 437 \ No newline at end of file
  438 + unit_name = os.environ["unit_name"]
  439 + config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"], unit_name)
  440 + pyros_config = ConfigPyros(os.environ["pyros_config_file"])
  441 + pyros_config.fn.fcontext = "pyros_seq"
  442 + home = guitastro.Home(config.getHome())
  443 + pyros_config.fn.longitude(home.longitude)
  444 + if len(str(period.id)) < 3:
  445 + period_id = str(period.id)
  446 + while len(period_id) < 3:
  447 + period_id = "0" + period_id
  448 + fn_param = {
  449 + "period" : f"P{period_id}",
  450 + "version": "1",
  451 + "unit": config.unit_name,
  452 + "date": sequence.night_id,
  453 + "id_seq": sequence.id
  454 + }
  455 + fname = pyros_config.fn.naming_set(fn_param)
  456 + pyros_config.fn.update_subdir_fname_from_filename(fname)
  457 + fpath_name = pyros_config.fn.join()
  458 + # create dirs if they don't exist
  459 + os.makedirs(os.path.dirname(fpath_name), exist_ok=True)
  460 + eph = guitastro.Ephemeris()
  461 + eph.set_home(config.getHome())
  462 + # duskelev a parametrer dans obsconfig (yml)
  463 + duskelev = -7
  464 + try:
  465 + # TODO remplacer les none par les fichiers pickle de ephem_sun & ephem_moon
  466 + fullseq_dict["ephem"] = eph.target2night(fullseq_dict["sequence"]["config_attributes"]["target"], sequence.night_id, None, None, preferance=sequence.start_expo_pref, duskelev=duskelev)
  467 + except:
  468 + # target incorrecte
  469 + pass
  470 + pickle.dump(fullseq_dict,open(fpath_name,"wb"))
430 471 \ No newline at end of file
... ...