diff --git a/src/core/pyros_django/img_process/A_ImgProcessor.py b/src/core/pyros_django/img_process/A_ImgProcessor.py index 42dad06..0039b36 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, Image +from seq_submit.models import Sequence, Album, Plan, Image # = Specials import glob import shutil @@ -412,6 +412,25 @@ class A_ImgProcessor(Agent): # - Load file in memory log.info(f"Image to process {fitsfile}") self._ima.load(fitsfile) + # Count number of Image entries in db, if >0 we can hope we can retrieve the Image entry. + is_orphan = False + if Image.objects.all().count() > 0: + try: + image_db = Image.objects.get(name=self._ima.fname) + except Image.DoesNotExist: + # Can't find the image in db + is_orphan = True + else: + is_orphan = True + if is_orphan: + log.info("Image not in DB. Moved in orphan directory.") + # Orphan Image, this image will be moved in orphans folder and will be deleted later + orphan_path = os.path.join(self._ima.rootdir, "..", "orphan") + os.makedirs(orphan_path, exist_ok=True) + shutil.move(fitsfile, orphan_path) + log.info("\n" + "="*70 + f"\n=== End process an image for L0->L1\n" + "="*70 + "\n") + return None + # - Get datedict from DATE-OBS of img date_obs = self._ima.getkwd("DATE-OBS") @@ -455,8 +474,10 @@ class A_ImgProcessor(Agent): self._ima.fcontext = "img_L1a" param_img['ftype'] = 'L1A' fname = self._ima.naming_set(param_img) + fname = self._ima.join(fname) - self._ima.save(fname) + image_db.set_attribute_and_save(self._ima, plan_id=image_db.plan.id) + #self._ima.save(fname) if param_img['ftype'] == "L1A" or param_img['ftype'] == "L1B": @@ -469,7 +490,8 @@ class A_ImgProcessor(Agent): param_img['ftype'] = 'L1B' fname = self._ima.naming_set(param_img) fname = self._ima.join(fname) - self._ima.save(fname) + image_db.set_attribute_and_save(self._ima, plan_id=image_db.plan.id) + #self._ima.save(fname) # - TODO Resampling (img_L1c) @@ -520,7 +542,20 @@ class A_ImgProcessor(Agent): filter_symbol = "C" kid = 110 id_sequence = int("999" + f"{kid:07d}") - + try: + # Retrouver la séquence dans la bd puis la supprimer (on supprime à la fois la séquence et les objets à laquelle elle est liée), si elle n'existe pas, on ne fait rien + seq = Sequence.objects.get(id=id_sequence) + seq.delete() + except Sequence.DoesNotExist: + pass + try: + sequence_db = Sequence.objects.create(id=id_sequence) + album_db = Album.objects.create(sequence=sequence_db) + plan_db = Plan.objects.create(album=album_db) + except Exception as e: + raise Exception(f"_create_test_images_1_BD: {e}") + # Note : Plan.objects.create() # revient à faire Plan() puis plan.save() + # Lien entre les tables séquence, album et plan : id_sequence <- id_album <- id_plan # --- Rename the image file name when copying to be compatible with the img_L0 file context try: self._ima.fcontext = "img_L0" @@ -534,7 +569,7 @@ class A_ImgProcessor(Agent): param['version'] = 1 param['channel'] = self._channel['symbol'] param['id_seq'] = id_sequence - param['plane'] = 1 + param['plane'] = plan_db.id param['frame'] = 1 fname = self._ima.naming_set(param) file_out = self._ima.join(fname) @@ -543,7 +578,9 @@ class A_ImgProcessor(Agent): try: db_image = Image() - db_image.set_attribute_and_save(img_obj=self._ima, plan_id=1) + + db_image.set_attribute_and_save(img_obj=self._ima, plan_id=plan_db.id) + # db_image has an id assigned except Exception as e: print(e) #self._ima.save(file_out) -- libgit2 0.21.2