From 9f322e6bdb35a5dd5ef8ecf4d2c9887b979467f4 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sat, 20 Aug 2022 15:25:10 -0400 Subject: [PATCH] Optionally use MAUDE to get star data for find_attitude --- .../blueprints/find_attitude/find_attitude.py | 78 ++++++++++++++++--- .../templates/find_attitude/index.html | 31 ++++++-- 2 files changed, 93 insertions(+), 16 deletions(-) diff --git a/kadi_apps/blueprints/find_attitude/find_attitude.py b/kadi_apps/blueprints/find_attitude/find_attitude.py index 81b2caa..1ceb119 100644 --- a/kadi_apps/blueprints/find_attitude/find_attitude.py +++ b/kadi_apps/blueprints/find_attitude/find_attitude.py @@ -1,4 +1,5 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst +from io import StringIO import os from flask import Blueprint, request @@ -21,6 +22,41 @@ ) +def get_stars_from_maude(date=None): + from maude import get_msids + import astropy.units as u + from cxotime import CxoTime + from astropy.table import Table + import numpy as np + + msids = [] + msids.extend([f"aoacyan{ii}" for ii in range(8)]) + msids.extend([f"aoaczan{ii}" for ii in range(8)]) + msids.extend([f"aoacmag{ii}" for ii in range(8)]) + if date is not None: + start = CxoTime(date) + stop = start + 10 * u.s + kwargs = {'start': start, 'stop': stop} + else: + kwargs = {} + dat = get_msids(msids, **kwargs) + results = dat["data"] + out = {} + for result in results: + msid = result["msid"] + value = result["values"][-1] + out[msid] = value + tbl = Table() + + tbl['slot'] = np.arange(8) + tbl['YAG'] = [out[f"AOACYAN{ii}"] for ii in range(8)] + tbl['ZAG'] = [out[f"AOACZAN{ii}"] for ii in range(8)] + tbl['MAG_ACA'] = [out[f"AOACMAG{ii}"] for ii in range(8)] + tbl.meta['date_solution'] = CxoTime(date).date + + return tbl + + @blueprint.route("/", methods=['GET', 'POST']) def index(): if request.method == 'POST': @@ -44,16 +80,34 @@ def index(): def find_solutions_and_get_context(): stars_text = request.form.get('stars_text', '') - context = {'stars_text': stars_text} + context = {} + if stars_text.strip() == '': + # Get date for solution, defaulting to NOW for any blank input + date_solution = request.form.get('date_solution', '').strip() or None + stars = get_stars_from_maude(date_solution) + + # Get a formatted version of the stars table that is used for finding + # the solutions. This gets put back into the web page output. + out = StringIO() + stars_context = stars.copy() + cols_new = ['yag', 'zag', 'mag'] + stars_context.rename_columns(['YAG', 'ZAG', 'MAG_ACA'], cols_new) + for name in cols_new: + stars_context[name].format = '.2f' + stars_context.write(out, format='ascii.fixed_width', delimiter=' ') + context['stars_text'] = out.getvalue() + context['date_solution'] = stars.meta['date_solution'] + else: + context['stars_text'] = stars_text - # First parse the star text input - try: - stars = get_stars_from_text(stars_text) - except Exception as err: - context['error_message'] = ("{}\n" - "Does it look like one of the examples?" - .format(err)) - return context + # First parse the star text input + try: + stars = get_stars_from_text(stars_text) + except Exception as err: + context['error_message'] = ("{}\n" + "Does it look like one of the examples?" + .format(err)) + return context # Try to find solutions tolerance = float(request.form.get('distance_tolerance', '2.5')) @@ -72,7 +126,11 @@ def find_solutions_and_get_context(): context['solutions'] = [] for solution in solutions: - summary_lines = solution['summary'].pformat(max_width=-1, max_lines=-1) + tbl = solution['summary'] + tbl['YAG'].format = '.2f' + tbl['ZAG'].format = '.2f' + tbl['MAG_ACA'].format = '.2f' + summary_lines = tbl.pformat(max_width=-1, max_lines=-1) sol = {'att_fit': solution['att_fit'], 'summary': os.linesep.join(summary_lines)} context['solutions'].append(sol) diff --git a/kadi_apps/blueprints/find_attitude/templates/find_attitude/index.html b/kadi_apps/blueprints/find_attitude/templates/find_attitude/index.html index d86558a..0ad14c6 100644 --- a/kadi_apps/blueprints/find_attitude/templates/find_attitude/index.html +++ b/kadi_apps/blueprints/find_attitude/templates/find_attitude/index.html @@ -15,12 +15,22 @@

Find Chandra attitude from stars {{subtitle}}

-
-

Enter ACA star data below using either GRETA display A_ACA_ALL or as a simple text - table.
- Click Submit and wait for up to a minute for the attitude solution results. -

-
+
+

Choose one of the following options: +

    +
  • Enter ACA star data below using either GRETA display A_ACA_ALL + or as a simple text table.
  • +
  • Leave the Star data field blank and use MAUDE to fetch ACA star data.
  • +
      +
    • Enter a date to get the solution at a particular time.
    • +
    • Leave the date blank to get the solution at the current time.
    • +
    +
+
+ Click Submit and wait for up to a minute for the attitude solution + results. +

+
@@ -34,6 +44,15 @@

Find Chandra attitude from stars {{subtitle}}

 
+ + +
+
+   +
+
+ +