From 6decf1a3da27befeaed479c78caf6e73fbcfccfd Mon Sep 17 00:00:00 2001 From: Alexis Koralewski Date: Wed, 27 Sep 2023 09:00:35 +0200 Subject: [PATCH] ImgProcessor save Image in db and file --- CHANGELOG | 4 ++++ VERSION | 2 +- src/core/pyros_django/img_process/A_ImgProcessor.py | 30 ++++++++++++++++++++++++++---- src/core/pyros_django/seq_submit/models.py | 72 +++++++++++++++++++++++++++++++++++++++--------------------------------- 4 files changed, 70 insertions(+), 38 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 32f6ed2..3c1da22 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +27-09-2023 (AKo): V0.6.31.0 + - ImgProcessor save Image in db and file + - Update docker script (docker compose usage) + 26-09-2023 (AKo): V0.6.31.0 - Add ephem_target2night in Agent.py - Add Image table diff --git a/VERSION b/VERSION index 9670684..3ca5458 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.6.27.0 \ No newline at end of file +0.6.31.0 \ No newline at end of file diff --git a/src/core/pyros_django/img_process/A_ImgProcessor.py b/src/core/pyros_django/img_process/A_ImgProcessor.py index aeaab30..42dad06 100755 --- a/src/core/pyros_django/img_process/A_ImgProcessor.py +++ b/src/core/pyros_django/img_process/A_ImgProcessor.py @@ -36,7 +36,7 @@ for short_path in short_paths: sys.path.insert(0, path) from src.core.pyros_django.majordome.agent.Agent import Agent, build_agent, log, parse_args -from seq_submit.models import Sequence +from seq_submit.models import Sequence, Image # = Specials import glob import shutil @@ -63,7 +63,7 @@ class A_ImgProcessor(Agent): # AgentCmd.CMD_STATUS_CODE.CMD_EXECUTED _TEST_COMMANDS_LIST = [ # Format : ("self cmd_name cmd_args", timeout, "expected_result", expected_status), - (True, "self do_create_test_images_2", 500, "", Agent.CMD_STATUS.CMD_EXECUTED), + (True, "self do_create_test_images_1", 500, "", Agent.CMD_STATUS.CMD_EXECUTED), (True, "self do_stop asap", 500, "STOPPING", Agent.CMD_STATUS.CMD_EXECUTED), ] @@ -540,7 +540,13 @@ class A_ImgProcessor(Agent): file_out = self._ima.join(fname) log.info(f"{file_out=}") self._ima.setkwd("FILTER", filter_symbol) - self._ima.save(file_out) + + try: + db_image = Image() + db_image.set_attribute_and_save(img_obj=self._ima, plan_id=1) + except Exception as e: + print(e) + #self._ima.save(file_out) # --- Build a bias image filled by zeros log.info("Build a test bias") @@ -642,12 +648,19 @@ class A_ImgProcessor(Agent): fsimu = "simu_" + self._channel['symbol'] fsimu = self._ima.join(fsimu) self._ima.extension = ext + print(" ASTROTABLE ") if os.path.exists(fsimu): + print("FSIMU EXISTS") att = guitastro.AstroTable() att.read(fsimu, format="ascii.ecsv") + print("FSIMU CALL SIMULATION") at = self._ima.simulation("ASTROTABLE", att, shutter_mode=shutter_mode, t=t, ra=ra, dec=dec) + print("FSIMU AFTER CALL SIMULATION") else: + print("FSIMU DOESN'T EXISTS") + print("FSIMU CALL SIMULATION") at = self._ima.simulation("GAIA", "PHOTOM", shutter_mode=shutter_mode, t=t, ra=ra, dec=dec, column_filters = {"Gmag": "<14"}) + print("FSIMU AFTER CALL SIMULATION") at.t.write(fsimu, format='ascii.ecsv', overwrite=True) att = at # --- Get other infos @@ -657,7 +670,9 @@ class A_ImgProcessor(Agent): # --- Save the image file name to be compatible with the img_L0 file context try: + print("Associate fcontext to ima") self._ima.fcontext = "img_L0" + print("POST Associate fcontext to ima") except Exception as e: raise Exception(f"_create_test_images_2: {e}") param = {} @@ -674,7 +689,14 @@ class A_ImgProcessor(Agent): file_out = self._ima.join(fname) self._ima.setkwd("DATE-OBS", date_obs) self._ima.setkwd("FILTER", filter_symbol) - self._ima.save(file_out) + print("create db img") + try: + db_image = Image() + print("save db img") + db_image.save(img_obj=self._ima) + except Exception as e: + print(e) + #self._ima.save(file_out) # --- Build a bias image log.info("Build a test bias") diff --git a/src/core/pyros_django/seq_submit/models.py b/src/core/pyros_django/seq_submit/models.py index 98d3af2..2398afd 100644 --- a/src/core/pyros_django/seq_submit/models.py +++ b/src/core/pyros_django/seq_submit/models.py @@ -505,9 +505,35 @@ class Plan(models.Model): ## New class image : +class ImageManager(models.Manager): + + def get_ids_for_field(self, fc:FileNames|str, field_name:str, field_value:str)->any: + """ + Args: + fc (FileNames | str): FileName object or string of fn to be selected + field_name (str): Field name + field_value (str): Field value to match for (equal or contains) + + Returns: + any: queryset of objects matching request or None if no objects match + """ + if type(fc) == FileNames: + fn_context = fc + if type(fc) == str: + obsconfig = OBSConfig(os.environ.get("PATH_TO_OBSCONF_FILE")) + obsconfig.fn.fcontext = fc + fn_context = obsconfig.fn + if field_name in fn_context.naming_keys(): + index = fn_context.naming_keys().index(field_name) + 1 + lookup_field = "key" + str(index) + + return Image.objects.filter(Q((lookup_field+"__contains", field_value))).values("id") + else: + return None + class Image(models.Model): plan = models.ForeignKey(Plan, on_delete=models.CASCADE, related_name="images") - name = models.CharField(max_length=45, blank=True, null=True) + name = models.CharField(max_length=90, blank=True, null=True) created = models.DateTimeField(blank=True, null=True, auto_now_add=True) updated = models.DateTimeField(blank=True, null=True, auto_now=True) date_from_gps = models.CharField(max_length=45, blank=True, null=True) @@ -536,43 +562,23 @@ class Image(models.Model): key19 = models.CharField(max_length=45, blank=True, null=True) key20 = models.CharField(max_length=45, blank=True, null=True) - def get_ids_for_field(fc:FileNames|str, field_name:str, field_value:str)->any: - """ - - - Args: - fc (FileNames | str): FileName object or string of fn to be selected - field_name (str): Field name - field_value (str): Field value to match for (equal or contains) - - Returns: - any: queryset of objects matching request or None if no objects match - """ - if type(fc) == FileNames: - fn_context = fc - else: - obsconfig = OBSConfig(os.environ.get("PATH_TO_OBSCONF_FILE")) - fn_context = obsconfig.fn.naming_set(fc) - if field_name in fn_context.naming_keys(): - index = fn_context.naming_keys().index(field_name) + 1 - lookup_field = "key" + str(index) - - return Image.object.filter(Q((lookup_field+"_contains", field_value))).values("id") - else: - return None + objects = ImageManager() - def save(self, img_name:str, img_obj:Ima, *args, **kwargs): - fn_context = img_obj.fn - params = img_obj.naming_get(img_name) + def set_attribute_and_save(self, *args, **kwargs): + img_obj = kwargs.get("img_obj") + plan_id = kwargs.get("plan_id") + self.plan_id = plan_id + params = img_obj.naming_get(img_obj.fname) self.date_from_gps = img_obj.getkwd("DATE-OBS") - self.fc_name = fn_context.naming() - self.name = img_name + self.fc_name = img_obj.naming() + self.name = os.path.basename(img_obj.fname) self.abs_path = img_obj.path for index, field in enumerate(params): index += 1 - self._meta.fields["key" + str(index)] = params[field] - img_obj.save() - super().save(*args, **kwargs) + self.__dict__["key" + str(index)] = params[field] + file_out = img_obj.join(img_obj.fname) + img_obj.save(file_out) + self.save() -- libgit2 0.21.2