Commit e060d7af8e2eab9814818291f37d7991a2e8f14b
1 parent
3825738b
Exists in
dev
Add image table
Showing
1 changed file
with
104 additions
and
30 deletions
Show diff stats
src/core/pyros_django/seq_submit/models.py
... | ... | @@ -29,11 +29,14 @@ from django.db.models.expressions import F |
29 | 29 | from django.db.models.query import QuerySet |
30 | 30 | from model_utils import Choices |
31 | 31 | from django.utils import timezone |
32 | +from django.db.models import Q | |
32 | 33 | |
33 | 34 | # Project imports |
34 | 35 | from user_mgmt.models import PyrosUser, ScientificProgram, Period |
35 | 36 | # DeviceCommand is used by class Command |
36 | 37 | sys.path.append("../../..") |
38 | +from vendor.guitastro.src.guitastro import FileNames, Ima | |
39 | +from src.core.pyros_django.obs_config.obsconfig_class import OBSConfig | |
37 | 40 | |
38 | 41 | ''' |
39 | 42 | NOT USED - to be removed |
... | ... | @@ -461,44 +464,115 @@ class Plan(models.Model): |
461 | 464 | |
462 | 465 | |
463 | 466 | |
467 | +# class Image(models.Model): | |
468 | +# plan = models.ForeignKey(Plan, on_delete=models.CASCADE, related_name="images") | |
469 | +# #nrtanalysis = models.ForeignKey('NrtAnalysis', models.DO_NOTHING, blank=True, null=True, related_name="images") | |
470 | +# name = models.CharField(max_length=45, blank=True, null=True) | |
471 | +# desc = models.TextField(blank=True, null=True) | |
472 | +# created = models.DateTimeField(blank=True, null=True, auto_now_add=True) | |
473 | +# updated = models.DateTimeField(blank=True, null=True, auto_now=True) | |
474 | +# date_from_gps = models.CharField(max_length=45, blank=True, null=True) | |
475 | +# # 12 parameters (coscrval1,sincrval1,coscrval2,sincrval2,`cd11rad`,`cd12rad`,`cd21rad`,`cd22rad`,`crpix1`,`crpix2`,`naxis1`,`naxis2`) | |
476 | +# sky_field = models.JSONField(blank=True, null=True) | |
477 | +# # File type, i.e. calibration file or image (L0, L1, L2,...) | |
478 | +# ftype = models.IntegerField(blank=True, null=True) | |
479 | +# """ | |
480 | +# type = models.CharField(max_length=5, blank=True, null=True) | |
481 | +# quality = models.CharField(max_length=45, blank=True, null=True) | |
482 | +# flaggps = models.CharField(max_length=45, blank=True, null=True) | |
483 | +# exposure = models.CharField(max_length=45, blank=True, null=True) | |
484 | +# tempext = models.CharField(max_length=45, blank=True, null=True) | |
485 | +# pressure = models.CharField(max_length=45, blank=True, null=True) | |
486 | +# humidext = models.CharField(max_length=45, blank=True, null=True) | |
487 | +# wind = models.CharField(max_length=45, blank=True, null=True) | |
488 | +# wind_dir = models.CharField(max_length=45, blank=True, null=True) | |
489 | +# dwnimg = models.CharField(max_length=45, blank=True, null=True) | |
490 | +# dwncata = models.CharField(max_length=45, blank=True, null=True) | |
491 | +# dwn = models.CharField(max_length=45, blank=True, null=True) | |
492 | +# """ | |
493 | +# # level0_fits_name = models.CharField(max_length=45, blank=True, null=True) | |
494 | +# # level1a_fits_name = models.CharField(max_length=45, blank=True, null=True) | |
495 | +# # level1b_fits_name = models.CharField(max_length=45, blank=True, null=True) | |
496 | +# fits_name = models.CharField(max_length=45, blank=True, null=True) | |
497 | + | |
498 | +# class Meta: | |
499 | +# managed = True | |
500 | +# db_table = 'image' | |
501 | + | |
502 | +# def __str__(self): | |
503 | +# return (str(self.name)) | |
504 | + | |
505 | + | |
506 | +## New class image : | |
507 | + | |
464 | 508 | class Image(models.Model): |
465 | 509 | plan = models.ForeignKey(Plan, on_delete=models.CASCADE, related_name="images") |
466 | - #nrtanalysis = models.ForeignKey('NrtAnalysis', models.DO_NOTHING, blank=True, null=True, related_name="images") | |
467 | 510 | name = models.CharField(max_length=45, blank=True, null=True) |
468 | - desc = models.TextField(blank=True, null=True) | |
469 | 511 | created = models.DateTimeField(blank=True, null=True, auto_now_add=True) |
470 | 512 | updated = models.DateTimeField(blank=True, null=True, auto_now=True) |
471 | 513 | date_from_gps = models.CharField(max_length=45, blank=True, null=True) |
472 | 514 | # 12 parameters (coscrval1,sincrval1,coscrval2,sincrval2,`cd11rad`,`cd12rad`,`cd21rad`,`cd22rad`,`crpix1`,`crpix2`,`naxis1`,`naxis2`) |
473 | 515 | sky_field = models.JSONField(blank=True, null=True) |
474 | - # File type, i.e. calibration file or image (L0, L1, L2,...) | |
475 | - ftype = models.IntegerField(blank=True, null=True) | |
476 | - """ | |
477 | - type = models.CharField(max_length=5, blank=True, null=True) | |
478 | - quality = models.CharField(max_length=45, blank=True, null=True) | |
479 | - flaggps = models.CharField(max_length=45, blank=True, null=True) | |
480 | - exposure = models.CharField(max_length=45, blank=True, null=True) | |
481 | - tempext = models.CharField(max_length=45, blank=True, null=True) | |
482 | - pressure = models.CharField(max_length=45, blank=True, null=True) | |
483 | - humidext = models.CharField(max_length=45, blank=True, null=True) | |
484 | - wind = models.CharField(max_length=45, blank=True, null=True) | |
485 | - wind_dir = models.CharField(max_length=45, blank=True, null=True) | |
486 | - dwnimg = models.CharField(max_length=45, blank=True, null=True) | |
487 | - dwncata = models.CharField(max_length=45, blank=True, null=True) | |
488 | - dwn = models.CharField(max_length=45, blank=True, null=True) | |
489 | - """ | |
490 | - # level0_fits_name = models.CharField(max_length=45, blank=True, null=True) | |
491 | - # level1a_fits_name = models.CharField(max_length=45, blank=True, null=True) | |
492 | - # level1b_fits_name = models.CharField(max_length=45, blank=True, null=True) | |
493 | - fits_name = models.CharField(max_length=45, blank=True, null=True) | |
494 | - | |
495 | - class Meta: | |
496 | - managed = True | |
497 | - db_table = 'image' | |
498 | - | |
499 | - def __str__(self): | |
500 | - return (str(self.name)) | |
501 | - | |
516 | + fc_name = models.CharField(max_length=45, blank=True, null=True) | |
517 | + abs_path = models.CharField(max_length=150, blank=True, null=True) | |
518 | + key1 = models.CharField(max_length=45, blank=True, null=True) | |
519 | + key2 = models.CharField(max_length=45, blank=True, null=True) | |
520 | + key3 = models.CharField(max_length=45, blank=True, null=True) | |
521 | + key4 = models.CharField(max_length=45, blank=True, null=True) | |
522 | + key5 = models.CharField(max_length=45, blank=True, null=True) | |
523 | + key6 = models.CharField(max_length=45, blank=True, null=True) | |
524 | + key7 = models.CharField(max_length=45, blank=True, null=True) | |
525 | + key8 = models.CharField(max_length=45, blank=True, null=True) | |
526 | + key9 = models.CharField(max_length=45, blank=True, null=True) | |
527 | + key10 = models.CharField(max_length=45, blank=True, null=True) | |
528 | + key11 = models.CharField(max_length=45, blank=True, null=True) | |
529 | + key12 = models.CharField(max_length=45, blank=True, null=True) | |
530 | + key13 = models.CharField(max_length=45, blank=True, null=True) | |
531 | + key14 = models.CharField(max_length=45, blank=True, null=True) | |
532 | + key15 = models.CharField(max_length=45, blank=True, null=True) | |
533 | + key16 = models.CharField(max_length=45, blank=True, null=True) | |
534 | + key17 = models.CharField(max_length=45, blank=True, null=True) | |
535 | + key18 = models.CharField(max_length=45, blank=True, null=True) | |
536 | + key19 = models.CharField(max_length=45, blank=True, null=True) | |
537 | + key20 = models.CharField(max_length=45, blank=True, null=True) | |
538 | + | |
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 | |
563 | + | |
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 | + self.date_from_gps = img_obj.getkwd("DATE-OBS") | |
568 | + self.fc_name = fn_context.naming() | |
569 | + self.name = img_name | |
570 | + self.abs_path = img_obj.path | |
571 | + for index, field in enumerate(params): | |
572 | + index += 1 | |
573 | + self._meta.fields["key" + str(index)] = params[field] | |
574 | + img_obj.save() | |
575 | + super().save(*args, **kwargs) | |
502 | 576 | |
503 | 577 | |
504 | 578 | ... | ... |