diff --git a/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py b/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py index 671d2c3350c69d9f1120956bb6a5c7855b870c94..58af3d10e98a2203a4617635f6588e2f3a527157 100644 --- a/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py +++ b/apps/cli/utilities/aat_wrest/aat_wrest/metadata_wrester.py @@ -388,9 +388,34 @@ class WrestWorkflowMetadata: cursor.execute(query, {"spl": spl}) data = cursor.fetchall() if data: + product_type = str(data[0][0]).lower().replace(" ", "_") + + if product_type != 'calibration': + telescope = data[0][1] + else: + # Need to query on the eb / alma_ous_id to get a telescope for calibrations + scope_query = """ + SELECT telescope + FROM execution_blocks + JOIN calibrations c_eb ON execution_blocks.execution_block_id = c_eb.execution_block_id + WHERE c_eb.science_product_locator = %(spl)s + UNION + SELECT telescope + FROM execution_blocks + JOIN calibrations c_ous ON execution_blocks.alma_ous_id = c_ous.alma_ous_id + WHERE c_ous.science_product_locator = %(spl)s + """ + cursor.execute(scope_query, {"spl": spl}) + scope_data = cursor.fetchall() + if scope_data: + telescope = scope_data[0][0] + else: + self.logger.error(f"ERROR: Failed to determine telescope from calibration's EB, got {data}") + return make_json + make_json = { - "product_type": str(data[0][0]).lower().replace(" ", "_"), - "telescope": data[0][1], + "product_type": product_type, + "telescope": telescope, "projectCode": data[0][2], }