diff --git a/src/core/pyros_django/seq_submit/models.py b/src/core/pyros_django/seq_submit/models.py index d2a1cdd..98d3af2 100644 --- a/src/core/pyros_django/seq_submit/models.py +++ b/src/core/pyros_django/seq_submit/models.py @@ -29,11 +29,14 @@ from django.db.models.expressions import F from django.db.models.query import QuerySet from model_utils import Choices from django.utils import timezone +from django.db.models import Q # Project imports from user_mgmt.models import PyrosUser, ScientificProgram, Period # DeviceCommand is used by class Command sys.path.append("../../..") +from vendor.guitastro.src.guitastro import FileNames, Ima +from src.core.pyros_django.obs_config.obsconfig_class import OBSConfig ''' NOT USED - to be removed @@ -461,44 +464,115 @@ class Plan(models.Model): +# class Image(models.Model): +# plan = models.ForeignKey(Plan, on_delete=models.CASCADE, related_name="images") +# #nrtanalysis = models.ForeignKey('NrtAnalysis', models.DO_NOTHING, blank=True, null=True, related_name="images") +# name = models.CharField(max_length=45, blank=True, null=True) +# desc = models.TextField(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) +# # 12 parameters (coscrval1,sincrval1,coscrval2,sincrval2,`cd11rad`,`cd12rad`,`cd21rad`,`cd22rad`,`crpix1`,`crpix2`,`naxis1`,`naxis2`) +# sky_field = models.JSONField(blank=True, null=True) +# # File type, i.e. calibration file or image (L0, L1, L2,...) +# ftype = models.IntegerField(blank=True, null=True) +# """ +# type = models.CharField(max_length=5, blank=True, null=True) +# quality = models.CharField(max_length=45, blank=True, null=True) +# flaggps = models.CharField(max_length=45, blank=True, null=True) +# exposure = models.CharField(max_length=45, blank=True, null=True) +# tempext = models.CharField(max_length=45, blank=True, null=True) +# pressure = models.CharField(max_length=45, blank=True, null=True) +# humidext = models.CharField(max_length=45, blank=True, null=True) +# wind = models.CharField(max_length=45, blank=True, null=True) +# wind_dir = models.CharField(max_length=45, blank=True, null=True) +# dwnimg = models.CharField(max_length=45, blank=True, null=True) +# dwncata = models.CharField(max_length=45, blank=True, null=True) +# dwn = models.CharField(max_length=45, blank=True, null=True) +# """ +# # level0_fits_name = models.CharField(max_length=45, blank=True, null=True) +# # level1a_fits_name = models.CharField(max_length=45, blank=True, null=True) +# # level1b_fits_name = models.CharField(max_length=45, blank=True, null=True) +# fits_name = models.CharField(max_length=45, blank=True, null=True) + +# class Meta: +# managed = True +# db_table = 'image' + +# def __str__(self): +# return (str(self.name)) + + +## New class image : + class Image(models.Model): plan = models.ForeignKey(Plan, on_delete=models.CASCADE, related_name="images") - #nrtanalysis = models.ForeignKey('NrtAnalysis', models.DO_NOTHING, blank=True, null=True, related_name="images") name = models.CharField(max_length=45, blank=True, null=True) - desc = models.TextField(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) # 12 parameters (coscrval1,sincrval1,coscrval2,sincrval2,`cd11rad`,`cd12rad`,`cd21rad`,`cd22rad`,`crpix1`,`crpix2`,`naxis1`,`naxis2`) sky_field = models.JSONField(blank=True, null=True) - # File type, i.e. calibration file or image (L0, L1, L2,...) - ftype = models.IntegerField(blank=True, null=True) - """ - type = models.CharField(max_length=5, blank=True, null=True) - quality = models.CharField(max_length=45, blank=True, null=True) - flaggps = models.CharField(max_length=45, blank=True, null=True) - exposure = models.CharField(max_length=45, blank=True, null=True) - tempext = models.CharField(max_length=45, blank=True, null=True) - pressure = models.CharField(max_length=45, blank=True, null=True) - humidext = models.CharField(max_length=45, blank=True, null=True) - wind = models.CharField(max_length=45, blank=True, null=True) - wind_dir = models.CharField(max_length=45, blank=True, null=True) - dwnimg = models.CharField(max_length=45, blank=True, null=True) - dwncata = models.CharField(max_length=45, blank=True, null=True) - dwn = models.CharField(max_length=45, blank=True, null=True) - """ - # level0_fits_name = models.CharField(max_length=45, blank=True, null=True) - # level1a_fits_name = models.CharField(max_length=45, blank=True, null=True) - # level1b_fits_name = models.CharField(max_length=45, blank=True, null=True) - fits_name = models.CharField(max_length=45, blank=True, null=True) - - class Meta: - managed = True - db_table = 'image' - - def __str__(self): - return (str(self.name)) - + fc_name = models.CharField(max_length=45, blank=True, null=True) + abs_path = models.CharField(max_length=150, blank=True, null=True) + key1 = models.CharField(max_length=45, blank=True, null=True) + key2 = models.CharField(max_length=45, blank=True, null=True) + key3 = models.CharField(max_length=45, blank=True, null=True) + key4 = models.CharField(max_length=45, blank=True, null=True) + key5 = models.CharField(max_length=45, blank=True, null=True) + key6 = models.CharField(max_length=45, blank=True, null=True) + key7 = models.CharField(max_length=45, blank=True, null=True) + key8 = models.CharField(max_length=45, blank=True, null=True) + key9 = models.CharField(max_length=45, blank=True, null=True) + key10 = models.CharField(max_length=45, blank=True, null=True) + key11 = models.CharField(max_length=45, blank=True, null=True) + key12 = models.CharField(max_length=45, blank=True, null=True) + key13 = models.CharField(max_length=45, blank=True, null=True) + key14 = models.CharField(max_length=45, blank=True, null=True) + key15 = models.CharField(max_length=45, blank=True, null=True) + key16 = models.CharField(max_length=45, blank=True, null=True) + key17 = models.CharField(max_length=45, blank=True, null=True) + key18 = models.CharField(max_length=45, blank=True, null=True) + 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 + + def save(self, img_name:str, img_obj:Ima, *args, **kwargs): + fn_context = img_obj.fn + params = img_obj.naming_get(img_name) + self.date_from_gps = img_obj.getkwd("DATE-OBS") + self.fc_name = fn_context.naming() + self.name = img_name + 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) -- libgit2 0.21.2