Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally use MAUDE to get star data for find_attitude #14

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 68 additions & 10 deletions kadi_apps/blueprints/find_attitude/find_attitude.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(results[0]['times'][-1]).date

return tbl


@blueprint.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
Expand All @@ -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'))
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ <h1 align="center"> Find Chandra attitude from stars {{subtitle}}</h1>
</form>

<div class="row">
<div class="large-12 columns">
<p>Enter ACA star data below using either GRETA display A_ACA_ALL or as a simple text
table.<br>
Click Submit and wait for up to a minute for the attitude solution results.
</p>
</div>
<div class="large-12 columns">
<p>Choose one of the following options:
<ul>
<li> Enter ACA star data below using either GRETA display A_ACA_ALL
or as a simple text table.</li>
<li> Leave the Star data field blank and use MAUDE to fetch ACA star data.</li>
<ul>
<li> Enter a date to get the solution at a particular time. </li>
<li> Leave the date blank to get the solution at the current time.</li>
</ul>
</ul>
<br>
Click Submit and wait for up to a minute for the attitude solution
results.
</p>
</div>
</div>

<form action="/find_attitude/" method="post">
Expand All @@ -34,6 +44,15 @@ <h1 align="center"> Find Chandra attitude from stars {{subtitle}}</h1>
&nbsp;
<div class="row">
<div class="col">
<label for="date_solution">Solution date (if no Star data are supplied) </label>
<input type="text" class="form-control" id="date_solution" name="date_solution" value="{{date_solution}}">
</div>
</div>
&nbsp;
<div class="row">
<div class="col">
<label for="stars_text">Star data (leave blank to get star data from MAUDE) </label>

<textarea
class="form-control"
style="font-family:Consolas,Monaco,Lucida Console,Liberation
Expand Down