From f6375431ea30acd0f26639fd666d729c6cb8e105 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Wed, 21 Oct 2020 12:50:47 +0200
Subject: [PATCH] TMSS-379: adated task->subtask method to new station schema.
 added ToDo for TMSS-382 follow up ticket.

---
 SAS/TMSS/src/tmss/tmssapp/subtasks.py | 36 ++++++++++++---------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/SAS/TMSS/src/tmss/tmssapp/subtasks.py b/SAS/TMSS/src/tmss/tmssapp/subtasks.py
index 1a7c995a452..df48446dd3e 100644
--- a/SAS/TMSS/src/tmss/tmssapp/subtasks.py
+++ b/SAS/TMSS/src/tmss/tmssapp/subtasks.py
@@ -128,26 +128,20 @@ def create_observation_subtask_specifications_from_observation_task_blueprint(ta
     subtask_spec['stations']["antenna_set"] = task_spec["antenna_set"]
     subtask_spec['stations']["filter"] = task_spec["filter"]
 
-    if "stations" in task_spec:
-        subtask_station_list = []
-        #  task_spec["stations"] is a list
-        for station_item in task_spec["stations"]:
-            if "group" in station_item:
-                station_group_name = station_item["group"]
-                try:
-                    # retrieve stations in group from RADB virtual instrument
-                    subtask_station_list.extend(get_stations_in_group(station_group_name))
-                except Exception as e:
-                    raise SubtaskCreationException("Could not determine stations in group '%s' for task_blueprint id=%s. Error: %s" % (
-                        station_group_name, task_blueprint.id, e))
-            elif "list" in station_item:
-                subtask_station_list.extend(station_item["list"])
-            else:
-                logger.info("NO 'group' or 'list' in task spec, so nothing to be added to station list")
-        subtask_spec['stations']['station_list'] = sorted(list(set(subtask_station_list)))
-    else:
-        logger.info("NO stations defined at all")
-    # TODO: Do something with max_missing
+    # At this moment of subtask creation we known which stations we *want* from the task_spec
+    # But we do not know yet which stations are available at the moment of observing.
+    # So, we decided that we set the subtask station_list as the union of all stations in all specified groups.
+    # This way, the user can see which stations are (likely) to be used.
+    # At the moment of scheduling of this subtask, then station_list is re-evaluated, and the max_nr_missing per group is validated.
+    subtask_spec['stations']['station_list'] = []
+    if "station_groups" in task_spec:
+        for station_group in task_spec["station_groups"]:
+            subtask_spec['stations']['station_list'].extend(station_group["stations"])
+        # make list have unique items
+        subtask_spec['stations']['station_list'] = sorted(list(set(subtask_spec['stations']['station_list'])))
+
+    if not subtask_spec['stations']['station_list']:
+        raise SubtaskCreationException("Cannot create observation subtask specifications for task_blueprint id=%s. No stations are defined." % (task_blueprint.id,))
 
     if 'calibrator' not in task_blueprint.specifications_template.name.lower():
         # copy/convert the analoge/digital_pointings only for non-calibrator observations (the calibrator has its own pointing)
@@ -752,6 +746,8 @@ def schedule_observation_subtask(observation_subtask: Subtask):
     # step 4: resource assigner (if possible)
     _assign_resources(observation_subtask)
 
+    # TODO: TMSS-382: evaluate the scheduled stations and see if the requiments given in the subtask.task_bluepring.specifications_doc are met for the station_groups and max_nr_missing.
+
     # step 5: set state to SCHEDULED (resulting in the qaservice to pick this subtask up and run it)
     observation_subtask.state = SubtaskState.objects.get(value=SubtaskState.Choices.SCHEDULED.value)
     observation_subtask.save()
-- 
GitLab