Skip to content
Merged
67 changes: 67 additions & 0 deletions starcheck/pcad_att_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import re
from astropy.table import Table
import Quaternion
from Quaternion import Quat

from parse_cm import read_backstop, read_or_list
from Chandra.Time import DateTime
from agasc import sphere_dist
Comment thread
jeanconn marked this conversation as resolved.
Outdated
import hopper


Expand Down Expand Up @@ -71,6 +73,71 @@ def recent_attitude_history(time, file):
return greta_time, float(q1), float(q2), float(q3), float(q4)


def get_maneuvers(backstop_file, attitude_file=None):
"""
Use the hopper state machine to make a maneuver structure from initial
conditions and the backstop file.

This duplicates some of make_pcad_attitude_check_report.

:param backstop_file: backstop file
:param attitude_file: attitude history file
:returns: list of maneuvers
"""

bs = read_backstop(backstop_file)

# Get initial state attitude and sim position from history
att_time, q1, q2, q3, q4 = recent_attitude_history(
DateTime(bs[0]['date']).secs,
attitude_file)
q = Quaternion.normalize([q1, q2, q3, q4])

initial_state = {'q1': q[0],
'q2': q[1],
'q3': q[2],
'q4': q[3]}

sc = hopper.run_cmds(backstop_file, or_list=None, ofls_characteristics_file=None,
initial_state=initial_state, starcheck=True)

mm = []
for m in sc.maneuvers:
q1 = Quaternion.normalize([m['initial']['q1'],
m['initial']['q2'],
m['initial']['q3'],
m['initial']['q4']])
q1 = Quat(q=q1)
q2 = Quaternion.normalize([m['final']['q1'],
m['final']['q2'],
m['final']['q3'],
m['final']['q4']])
q2 = Quat(q=q2)
angle = sphere_dist(q1.ra, q1.dec, q2.ra, q2.dec)

# Re-arrange the hopper maneuever structure to match the structure previously used
# from Parse_CM_File.pm
man = {'initial_obsid': m['initial']['obsid'],
'final_obsid': m['final']['obsid'],
'start_date': m['initial']['date'],
'stop_date': m['final']['date'],
'ra': q2.ra,
'dec': q2.dec,
'roll': q2.roll,
'dur': m['dur'],
'angle': angle,
'q1': m['final']['q1'],
'q2': m['final']['q2'],
'q3': m['final']['q3'],
'q4': m['final']['q4'],
'tstart': DateTime(m['initial']['date']).secs,
'tstop': DateTime(m['final']['date']).secs,
}
mm.append(man)

return mm


def make_pcad_attitude_check_report(backstop_file, or_list_file=None, attitude_file=None,
simtrans_file=None, simfocus_file=None,
ofls_characteristics_file=None, out=None,
Expand Down
46 changes: 6 additions & 40 deletions starcheck/src/lib/Ska/Starcheck/Obsid.pm
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ sub set_files {
$self->{backstop},
$self->{guide_summ},
$self->{or_file},
$self->{mm_file},
$self->{dot_file},
$self->{tlr_file}
) = @_;
Expand Down Expand Up @@ -328,22 +327,21 @@ sub find_command {
##################################################################################
sub set_maneuver {
#
# Find the right obsid for each maneuver. Note that obsids in mm_file don't
# always match those in DOT, etc
# Find the maneuver for each dot obsid.
#
##################################################################################
my $self = shift;
my %mm = @_;
my $mm = shift;
my $n = 1;
my $c;
my $found;

while ($c = find_command($self, "MP_TARGQUAT", $n++)) {
$found = 0;
foreach my $m (values %mm) {
my $manvr_obsid = $m->{manvr_dest};
foreach my $m (@{$mm}) {
my $manvr_obsid = $m->{final_obsid};

# where manvr_dest is either the final_obsid of a maneuver or the eventual destination obsid
# where manvr_dest is either the final_obsid of a maneuver or the eventual destination obsid
# of a segmented maneuver
if ( ($manvr_obsid eq $self->{dot_obsid})
&& abs($m->{q1} - $c->{Q1}) < 1e-7
Expand Down Expand Up @@ -372,39 +370,12 @@ sub set_maneuver {
sprintf(
"Uplink quaternion norm value $norm is too far from 1.0\n");
}
my @c_quat_norm = (
$c->{Q1} / $norm,
$c->{Q2} / $norm,
$c->{Q3} / $norm,
$q4_obc / $norm
);

# Get quat from MANEUVER summary file. This is correct to high precision
my $q_man = Quat->new($m->{ra}, $m->{dec}, $m->{roll});
my $q_obc = Quat->new(@c_quat_norm);
my @q_man = @{ $q_man->{q} };
my $q_diff = $q_man->divide($q_obc);

if ( abs($q_diff->{ra0} * 3600) > 1.0
|| abs($q_diff->{dec} * 3600) > 1.0
|| abs($q_diff->{roll0} * 3600) > 10.0)
{
push @{ $self->{warn} },
sprintf(
"Target uplink precision problem for MP_TARGQUAT at $c->{date}\n"
Comment thread
jeanconn marked this conversation as resolved.
. " Error is yaw, pitch, roll (arcsec) = %.2f %.2f %.2f\n"
. " Use Q1,Q2,Q3,Q4 = %.12f %.12f %.12f %.12f\n",
$q_diff->{ra0} * 3600,
$q_diff->{dec} * 3600,
$q_diff->{roll0} * 3600,
$q_man[0], $q_man[1], $q_man[2], $q_man[3]
);
}
}

}
push @{ $self->{yellow_warn} },
sprintf("Did not find match in MAN summary for MP_TARGQUAT at $c->{date}\n")
sprintf("Did not find match in maneuvers for MP_TARGQUAT at $c->{date}\n")
unless ($found);

}
Expand Down Expand Up @@ -2089,11 +2060,6 @@ sub print_report {
$self->{STARCHECK}, basename($self->{or_file}),
$self->{obsid}
) if ($self->{or_file});
$o .= sprintf(
"<A HREF=\"%s/%s.html#%s\">MANVR</A> ",
$self->{STARCHECK}, basename($self->{mm_file}),
$self->{dot_obsid}
);
$o .= sprintf(
"<A HREF=\"%s/%s.html#%s\">DOT</A> ",
$self->{STARCHECK}, basename($self->{dot_file}),
Expand Down
18 changes: 9 additions & 9 deletions starcheck/src/starcheck.pl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
get_file("$par{dir}/${sosa_dir_slash}*.backstop", 'backstop', 'required');
my $guide_summ = get_file("$par{dir}/mps/mg*.sum", 'guide summary');
my $or_file = get_file("$par{dir}/mps/or/*.or", 'OR');
my $mm_file = get_file("$par{dir}/mps/mm*.sum", 'maneuver');
my $dot_file = get_file("$par{dir}/mps/md*.dot", 'DOT', 'required');
my $mech_file =
get_file("$par{dir}/${sosa_dir_slash}output/${sosa_prefix}TEST_mechcheck.txt*",
Expand Down Expand Up @@ -254,11 +253,15 @@
print "Reading TLR file $tlr_file\n";
my @load_segments = Ska::Parse_CM_File::TLR_load_segments($tlr_file);

print "Reading MM file $mm_file\n";
my $mm = call_python(
"pcad_att_check.get_maneuvers",
[],
{
backstop_file => $backstop,
attitude_file => $attitude_file,
}
);

# Read momentum management (maneuvers + SIM move) summary file
my %mm = Ska::Parse_CM_File::MM({ file => $mm_file, ret_type => 'hash' })
if ($mm_file);

# Read maneuver management summary for handy obsid time checks
print "Reading process summary $ps_file\n";
Expand Down Expand Up @@ -416,14 +419,13 @@
$obs{$obsid}->set_obsid(\%guidesumm); # Commanded obsid
$obs{$obsid}->set_target();
$obs{$obsid}->set_star_catalog();
$obs{$obsid}->set_maneuver(%mm) if ($mm_file);
$obs{$obsid}->set_maneuver($mm);
$obs{$obsid}->set_manerr(@manerr) if (@manerr);
$obs{$obsid}->set_files(
$STARCHECK,
$backstop,
$guide_summ,
$or_file,
$mm_file,
$dot_file,
$tlr_file
);
Expand Down Expand Up @@ -941,7 +943,6 @@ sub json_obsids {
make_annotated_file('', ' ID=\s+', ', ', $backstop);
make_annotated_file($guide_summ_start, '^\s+ID:\s+', '\S\S', $guide_summ);
make_annotated_file('', '^ ID=', ', ', $or_file) if ($or_file);
make_annotated_file('', ' ID:\s+', '\S\S', $mm_file);
make_annotated_file('', 'OBSID,ID=', ',', $dot_file);
my $tlr_lines = add_obsid_to_tlr(\@bs, $tlr_file);
make_annotated_file('', 'OBSERVATION ID\s*', '\s*\(', $tlr_file, $tlr_lines);
Expand Down Expand Up @@ -1051,7 +1052,6 @@ sub make_annotated_file {
# $backstop = get_file("$par{dir}/*.backstop",'backstop', 'required');
# $guide_summ = get_file("$par{dir}/mg*.sum", 'guide summary');
# $or_file = get_file("$par{dir}/*.or", 'OR');
# $mm_file = get_file("$par{dir}/*/mm*.sum", 'maneuver');
# $dot_file = get_file("$par{dir}/*.dot", 'DOT', 'required');

my ($start_rexp, $id_pre, $id_post, $file_in, $lines) = @_;
Expand Down