Commit 51f431017086d4db717f07fcd6486f1dcc9895e4

Authored by Alexis Koralewski
1 parent ca4a0ec6
Exists in dev

Update A_ImgProcessor, manage orphan image & Add link of image file into DB

Showing 1 changed file with 43 additions and 6 deletions   Show diff stats
src/core/pyros_django/img_process/A_ImgProcessor.py
... ... @@ -36,7 +36,7 @@ for short_path in short_paths:
36 36 sys.path.insert(0, path)
37 37  
38 38 from src.core.pyros_django.majordome.agent.Agent import Agent, build_agent, log, parse_args
39   -from seq_submit.models import Sequence, Image
  39 +from seq_submit.models import Sequence, Album, Plan, Image
40 40 # = Specials
41 41 import glob
42 42 import shutil
... ... @@ -412,6 +412,25 @@ class A_ImgProcessor(Agent):
412 412 # - Load file in memory
413 413 log.info(f"Image to process {fitsfile}")
414 414 self._ima.load(fitsfile)
  415 + # Count number of Image entries in db, if >0 we can hope we can retrieve the Image entry.
  416 + is_orphan = False
  417 + if Image.objects.all().count() > 0:
  418 + try:
  419 + image_db = Image.objects.get(name=self._ima.fname)
  420 + except Image.DoesNotExist:
  421 + # Can't find the image in db
  422 + is_orphan = True
  423 + else:
  424 + is_orphan = True
  425 + if is_orphan:
  426 + log.info("Image not in DB. Moved in orphan directory.")
  427 + # Orphan Image, this image will be moved in orphans folder and will be deleted later
  428 + orphan_path = os.path.join(self._ima.rootdir, "..", "orphan")
  429 + os.makedirs(orphan_path, exist_ok=True)
  430 + shutil.move(fitsfile, orphan_path)
  431 + log.info("\n" + "="*70 + f"\n=== End process an image for L0->L1\n" + "="*70 + "\n")
  432 + return None
  433 +
415 434  
416 435 # - Get datedict from DATE-OBS of img
417 436 date_obs = self._ima.getkwd("DATE-OBS")
... ... @@ -455,8 +474,10 @@ class A_ImgProcessor(Agent):
455 474 self._ima.fcontext = "img_L1a"
456 475 param_img['ftype'] = 'L1A'
457 476 fname = self._ima.naming_set(param_img)
  477 +
458 478 fname = self._ima.join(fname)
459   - self._ima.save(fname)
  479 + image_db.set_attribute_and_save(self._ima, plan_id=image_db.plan.id)
  480 + #self._ima.save(fname)
460 481  
461 482 if param_img['ftype'] == "L1A" or param_img['ftype'] == "L1B":
462 483  
... ... @@ -469,7 +490,8 @@ class A_ImgProcessor(Agent):
469 490 param_img['ftype'] = 'L1B'
470 491 fname = self._ima.naming_set(param_img)
471 492 fname = self._ima.join(fname)
472   - self._ima.save(fname)
  493 + image_db.set_attribute_and_save(self._ima, plan_id=image_db.plan.id)
  494 + #self._ima.save(fname)
473 495  
474 496 # - TODO Resampling (img_L1c)
475 497  
... ... @@ -520,7 +542,20 @@ class A_ImgProcessor(Agent):
520 542 filter_symbol = "C"
521 543 kid = 110
522 544 id_sequence = int("999" + f"{kid:07d}")
523   -
  545 + try:
  546 + # 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
  547 + seq = Sequence.objects.get(id=id_sequence)
  548 + seq.delete()
  549 + except Sequence.DoesNotExist:
  550 + pass
  551 + try:
  552 + sequence_db = Sequence.objects.create(id=id_sequence)
  553 + album_db = Album.objects.create(sequence=sequence_db)
  554 + plan_db = Plan.objects.create(album=album_db)
  555 + except Exception as e:
  556 + raise Exception(f"_create_test_images_1_BD: {e}")
  557 + # Note : Plan.objects.create() # revient à faire Plan() puis plan.save()
  558 + # Lien entre les tables séquence, album et plan : id_sequence <- id_album <- id_plan
524 559 # --- Rename the image file name when copying to be compatible with the img_L0 file context
525 560 try:
526 561 self._ima.fcontext = "img_L0"
... ... @@ -534,7 +569,7 @@ class A_ImgProcessor(Agent):
534 569 param['version'] = 1
535 570 param['channel'] = self._channel['symbol']
536 571 param['id_seq'] = id_sequence
537   - param['plane'] = 1
  572 + param['plane'] = plan_db.id
538 573 param['frame'] = 1
539 574 fname = self._ima.naming_set(param)
540 575 file_out = self._ima.join(fname)
... ... @@ -543,7 +578,9 @@ class A_ImgProcessor(Agent):
543 578  
544 579 try:
545 580 db_image = Image()
546   - db_image.set_attribute_and_save(img_obj=self._ima, plan_id=1)
  581 +
  582 + db_image.set_attribute_and_save(img_obj=self._ima, plan_id=plan_db.id)
  583 + # db_image has an id assigned
547 584 except Exception as e:
548 585 print(e)
549 586 #self._ima.save(file_out)
... ...