Commit 6decf1a3da27befeaed479c78caf6e73fbcfccfd

Authored by Alexis Koralewski
1 parent 05ef6b7f
Exists in dev

ImgProcessor save Image in db and file

CHANGELOG
  1 +27-09-2023 (AKo): V0.6.31.0
  2 + - ImgProcessor save Image in db and file
  3 + - Update docker script (docker compose usage)
  4 +
1 5 26-09-2023 (AKo): V0.6.31.0
2 6 - Add ephem_target2night in Agent.py
3 7 - Add Image table
... ...
VERSION
1   -0.6.27.0
2 1 \ No newline at end of file
  2 +0.6.31.0
3 3 \ No newline at end of file
... ...
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
  39 +from seq_submit.models import Sequence, Image
40 40 # = Specials
41 41 import glob
42 42 import shutil
... ... @@ -63,7 +63,7 @@ class A_ImgProcessor(Agent):
63 63 # AgentCmd.CMD_STATUS_CODE.CMD_EXECUTED
64 64 _TEST_COMMANDS_LIST = [
65 65 # Format : ("self cmd_name cmd_args", timeout, "expected_result", expected_status),
66   - (True, "self do_create_test_images_2", 500, "", Agent.CMD_STATUS.CMD_EXECUTED),
  66 + (True, "self do_create_test_images_1", 500, "", Agent.CMD_STATUS.CMD_EXECUTED),
67 67 (True, "self do_stop asap", 500, "STOPPING", Agent.CMD_STATUS.CMD_EXECUTED),
68 68 ]
69 69  
... ... @@ -540,7 +540,13 @@ class A_ImgProcessor(Agent):
540 540 file_out = self._ima.join(fname)
541 541 log.info(f"{file_out=}")
542 542 self._ima.setkwd("FILTER", filter_symbol)
543   - self._ima.save(file_out)
  543 +
  544 + try:
  545 + db_image = Image()
  546 + db_image.set_attribute_and_save(img_obj=self._ima, plan_id=1)
  547 + except Exception as e:
  548 + print(e)
  549 + #self._ima.save(file_out)
544 550  
545 551 # --- Build a bias image filled by zeros
546 552 log.info("Build a test bias")
... ... @@ -642,12 +648,19 @@ class A_ImgProcessor(Agent):
642 648 fsimu = "simu_" + self._channel['symbol']
643 649 fsimu = self._ima.join(fsimu)
644 650 self._ima.extension = ext
  651 + print(" ASTROTABLE ")
645 652 if os.path.exists(fsimu):
  653 + print("FSIMU EXISTS")
646 654 att = guitastro.AstroTable()
647 655 att.read(fsimu, format="ascii.ecsv")
  656 + print("FSIMU CALL SIMULATION")
648 657 at = self._ima.simulation("ASTROTABLE", att, shutter_mode=shutter_mode, t=t, ra=ra, dec=dec)
  658 + print("FSIMU AFTER CALL SIMULATION")
649 659 else:
  660 + print("FSIMU DOESN'T EXISTS")
  661 + print("FSIMU CALL SIMULATION")
650 662 at = self._ima.simulation("GAIA", "PHOTOM", shutter_mode=shutter_mode, t=t, ra=ra, dec=dec, column_filters = {"Gmag": "<14"})
  663 + print("FSIMU AFTER CALL SIMULATION")
651 664 at.t.write(fsimu, format='ascii.ecsv', overwrite=True)
652 665 att = at
653 666 # --- Get other infos
... ... @@ -657,7 +670,9 @@ class A_ImgProcessor(Agent):
657 670  
658 671 # --- Save the image file name to be compatible with the img_L0 file context
659 672 try:
  673 + print("Associate fcontext to ima")
660 674 self._ima.fcontext = "img_L0"
  675 + print("POST Associate fcontext to ima")
661 676 except Exception as e:
662 677 raise Exception(f"_create_test_images_2: {e}")
663 678 param = {}
... ... @@ -674,7 +689,14 @@ class A_ImgProcessor(Agent):
674 689 file_out = self._ima.join(fname)
675 690 self._ima.setkwd("DATE-OBS", date_obs)
676 691 self._ima.setkwd("FILTER", filter_symbol)
677   - self._ima.save(file_out)
  692 + print("create db img")
  693 + try:
  694 + db_image = Image()
  695 + print("save db img")
  696 + db_image.save(img_obj=self._ima)
  697 + except Exception as e:
  698 + print(e)
  699 + #self._ima.save(file_out)
678 700  
679 701 # --- Build a bias image
680 702 log.info("Build a test bias")
... ...
src/core/pyros_django/seq_submit/models.py
... ... @@ -505,9 +505,35 @@ class Plan(models.Model):
505 505  
506 506 ## New class image :
507 507  
  508 +class ImageManager(models.Manager):
  509 +
  510 + def get_ids_for_field(self, fc:FileNames|str, field_name:str, field_value:str)->any:
  511 + """
  512 + Args:
  513 + fc (FileNames | str): FileName object or string of fn to be selected
  514 + field_name (str): Field name
  515 + field_value (str): Field value to match for (equal or contains)
  516 +
  517 + Returns:
  518 + any: queryset of objects matching request or None if no objects match
  519 + """
  520 + if type(fc) == FileNames:
  521 + fn_context = fc
  522 + if type(fc) == str:
  523 + obsconfig = OBSConfig(os.environ.get("PATH_TO_OBSCONF_FILE"))
  524 + obsconfig.fn.fcontext = fc
  525 + fn_context = obsconfig.fn
  526 + if field_name in fn_context.naming_keys():
  527 + index = fn_context.naming_keys().index(field_name) + 1
  528 + lookup_field = "key" + str(index)
  529 +
  530 + return Image.objects.filter(Q((lookup_field+"__contains", field_value))).values("id")
  531 + else:
  532 + return None
  533 +
508 534 class Image(models.Model):
509 535 plan = models.ForeignKey(Plan, on_delete=models.CASCADE, related_name="images")
510   - name = models.CharField(max_length=45, blank=True, null=True)
  536 + name = models.CharField(max_length=90, blank=True, null=True)
511 537 created = models.DateTimeField(blank=True, null=True, auto_now_add=True)
512 538 updated = models.DateTimeField(blank=True, null=True, auto_now=True)
513 539 date_from_gps = models.CharField(max_length=45, blank=True, null=True)
... ... @@ -536,43 +562,23 @@ class Image(models.Model):
536 562 key19 = models.CharField(max_length=45, blank=True, null=True)
537 563 key20 = models.CharField(max_length=45, blank=True, null=True)
538 564  
539   - def get_ids_for_field(fc:FileNames|str, field_name:str, field_value:str)->any:
540   - """
541   -
542   -
543   - Args:
544   - fc (FileNames | str): FileName object or string of fn to be selected
545   - field_name (str): Field name
546   - field_value (str): Field value to match for (equal or contains)
547   -
548   - Returns:
549   - any: queryset of objects matching request or None if no objects match
550   - """
551   - if type(fc) == FileNames:
552   - fn_context = fc
553   - else:
554   - obsconfig = OBSConfig(os.environ.get("PATH_TO_OBSCONF_FILE"))
555   - fn_context = obsconfig.fn.naming_set(fc)
556   - if field_name in fn_context.naming_keys():
557   - index = fn_context.naming_keys().index(field_name) + 1
558   - lookup_field = "key" + str(index)
559   -
560   - return Image.object.filter(Q((lookup_field+"_contains", field_value))).values("id")
561   - else:
562   - return None
  565 + objects = ImageManager()
563 566  
564   - def save(self, img_name:str, img_obj:Ima, *args, **kwargs):
565   - fn_context = img_obj.fn
566   - params = img_obj.naming_get(img_name)
  567 + def set_attribute_and_save(self, *args, **kwargs):
  568 + img_obj = kwargs.get("img_obj")
  569 + plan_id = kwargs.get("plan_id")
  570 + self.plan_id = plan_id
  571 + params = img_obj.naming_get(img_obj.fname)
567 572 self.date_from_gps = img_obj.getkwd("DATE-OBS")
568   - self.fc_name = fn_context.naming()
569   - self.name = img_name
  573 + self.fc_name = img_obj.naming()
  574 + self.name = os.path.basename(img_obj.fname)
570 575 self.abs_path = img_obj.path
571 576 for index, field in enumerate(params):
572 577 index += 1
573   - self._meta.fields["key" + str(index)] = params[field]
574   - img_obj.save()
575   - super().save(*args, **kwargs)
  578 + self.__dict__["key" + str(index)] = params[field]
  579 + file_out = img_obj.join(img_obj.fname)
  580 + img_obj.save(file_out)
  581 + self.save()
576 582  
577 583  
578 584  
... ...