-
Notifications
You must be signed in to change notification settings - Fork 1
/
arc.pl
executable file
·1283 lines (1112 loc) · 43.5 KB
/
arc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env perl
use warnings;
use strict;
use IO::All;
use Ska::RDB qw(read_rdb);
use Ska::Run;
use Ska::Convert qw(time2date date2time);
use Ska::Web;
use Config::General qw(ParseConfig);
use Data::Dumper;
use HTML::Table;
use CGI;
use LWP::UserAgent;
use HTML::TableExtract;
use Quat;
use Carp;
use Hash::Merge;
use Time::Local;
use POSIX qw(floor);
use subs qw(dbg);
use Chandra::Time;
use Safe;
use Getopt::Long;
use Net::Netrc;
# ToDo:
# - Fix Ska::Convert to make time2date having configurable format
# via { } at end. Maintain stupid 2nd arg scalar for back-comp.
# - Improve get_obsid_event so that it does violation checks during manv'r
# - Make sure logs and all other files w/ passwd are secure
our $Task = 'arc3';
our $TaskData = "$ENV{SKA_DATA}/$Task";
our $TaskShare = "$ENV{SKA_SHARE}/$Task";
our $VERSION = '4.9.0';
require "$ENV{SKA_SHARE}/$Task/Event.pm";
require "$ENV{SKA_SHARE}/$Task/Snap.pm";
require "$ENV{SKA_SHARE}/$Task/parse_cm_file.pl";
our $FloatRE = qr/[+-]?(?:\d+[.]?\d*|[.]\d+)(?:[dDeE][+-]?\d+)?/;
our $DateRE = qr/\d\d\d\d:\d+:\d+:\d+:\d\d\.?\d*/;
our %opt = get_config_options();
# Set global current time at beginning of execution
our $CurrentTime = @ARGV ? date2time(shift @ARGV, 'unix') : time;
our $CURRENT_TIME = Chandra::Time->new($CurrentTime, {format => 'unix'});
our $conv_time = Chandra::Time->new({format => 'unix'}); # Generic time converter box
our $SCS107date;
our %load_info;
our $Debug = 0;
our @warn; # Global set of processing warnings (warn but don't die)
Event::set_CurrentTime($CurrentTime);
Snap::set_CurrentTime($CurrentTime);
Snap::set_snap_definition($opt{snap_definition});
umask 002;
# Get authorization for access to OCCweb
my $netrc = Net::Netrc->lookup('occweb');
my $occweb_user = $netrc->login;
my $occweb_passwd = $netrc->password;
{
# Substitute any ENV vars in $opt{file}. Do this in a Safe way.
interpolate_config_file_options();
# Get web data & pointers to downloaded image files from get_web_content.pl task
my %web_content = ParseConfig(-ConfigFile => "$TaskData/$opt{file}{web_content}");
my ($snap_warning_ref, %snap) = Snap::get_snap( $opt{file}{snap_archive},
[ $opt{file}{snap},
$web_content{snapshot}{content}{snapshot}{outfile},
],
);
# Info from snapshot is *req'd* for subsequent processing. Snapshot unavailability
# is almost always transient
die(@{$snap_warning_ref}) if @{$snap_warning_ref};
$SCS107date = check_for_scs107(\%snap);
my $obsid = $snap{obsid}{value};
my @event = get_iFOT_events(@{$opt{query_name}});
make_maneuver_obsid_links(\@event);
push @event, get_violation_events($obsid, \%snap, \@event);
push @event, now_event(); # Add a special event for Now
push @event, scs107_detected_event() if $SCS107date;
@event = sort { $a->tstart <=> $b->tstart } @event;
my $html = make_web_page(\%snap, \@event, \%web_content);
$html > io("$TaskData/$opt{file}{web_page}");
install_web_files($html, \%web_content);
print_iFOT_events(\@event) if $Debug;
print join("\n", @warn), "\n" if $Debug;
}
####################################################################################
sub warning {
####################################################################################
while (my $warning = shift) {
push @warn, $warning;
print STDERR "$warning\n" if $Debug;
}
}
####################################################################################
sub interpolate_config_file_options {
# Substitute any ENV vars in $opt{file}. Do this in a Safe way.
####################################################################################
my $safe = new Safe;
$safe->share('%ENV');
$safe->permit_only(qw(:base_core :base_mem :base_orig));
foreach (values %{$opt{file}}) {
if (defined $_) {
$_ = $safe->reval(qq{"$_"});
die "ERROR - problem in safe eval of file option: $@\n" if $@;
}
}
}
####################################################################################
sub get_config_options {
####################################################################################
# Read in config options and an optional test config options
my %opt = ('config' => "arc3");
GetOptions(\%opt,
'config=s');
Hash::Merge::set_behavior( 'RIGHT_PRECEDENT' );
foreach (split(':', $opt{config})) {
my $cfg_file = "$TaskShare/$_.cfg";
if (-r $cfg_file) {
my %new_opt = ParseConfig(-ConfigFile => $cfg_file);
%opt = %{ Hash::Merge::merge(\%opt, \%new_opt)};
}
}
return %opt;
}
####################################################################################
sub now_event {
####################################################################################
my $current_date = time2date($CurrentTime, 'unix');
return Event->new('Type Description' => 'Now',
'TStart (GMT)' => $current_date,
'TStop (GMT)' => $current_date,
'summary' => '--------------- NOW ---------------',
);
}
####################################################################################
sub scs107_detected_event {
####################################################################################
return Event->new('Type Description' => 'SCS-107 (Detected)',
'TStart (GMT)' => $SCS107date,
'TStop (GMT)' => $SCS107date,
'summary' => 'SCS107 detected during realtime comm',
);
}
####################################################################################
sub get_obsid_event {
####################################################################################
my $obsid = shift;
my $snap = shift;
my $event = shift;
my $warn_msg = "Unable to include PCAD violation events";
my @obsid_evt = grep { defined $_->obsid } @{$event};
my @index = grep { $obsid_evt[$_]->obsid == $obsid } (0..$#obsid_evt);
if (@index != 1) {
my $num = (@index == 0) ? 'No' : 'Multiple';
warning("$num events found matching obsid=$obsid: $warn_msg");
return;
}
# Temporarily disable pcad checking
# return $obsid_evt[$index[0]];
# Check if pcadmode is NPM, and if so look at attitude and make sure it matches
# expected for obsid. If not, try the next maneuver after current obsid.
if ($snap->{pcadmode}{value} eq 'NPNT') {
my $snap_quat = Quat->new($snap->{ra}{value}, $snap->{dec}{value}, $snap->{roll}{value});
for my $i ($index[0], $index[0]+1) {
unless (defined $obsid_evt[$i]->target_quat) {
dbg Dumper $obsid_evt[$i];
next;
}
my $target_quat = $obsid_evt[$i]->target_quat->quat;
my $dq = $target_quat->divide($snap_quat);
if (abs($dq->{ra0}) < 0.1 and abs($dq->{dec}) < 0.1 and abs($dq->{roll0}) < 1) {
return $obsid_evt[$i];
}
}
warning("No obsid event matching snapshot attitude: $warn_msg");
} else {
# Not in NPM, so all bets are off
warning("Not in NPM: $warn_msg");
}
# Failed to find obsid event corresponding to current obsid and attitude.
# So return empty handed, which in turn causes get_violation_events to do nothing.
return;
}
####################################################################################
sub make_maneuver_obsid_links {
####################################################################################
my $event = shift;
my $obsid;
my @link_types = qw(load_segment maneuver target_quat);
my %link;
local $_;
my $evt2;
for my $evt (@{$event}) {
foreach (@link_types) {
$link{$_} = $evt if $evt->type eq $_;
}
if ($evt->type =~ /\A (observation | er) \Z/x) {
$obsid = $evt->obsid;
foreach (@link_types) {
$evt->$_($link{$_}) if defined $link{$_};
}
# Check for load segment tstart/tstop in range?
# if (defined $load_segment
# and $load_segment->tstart <= $evt->tstart
# and $load_segment->tstop >= $evt->tstart);
}
if ($evt->type eq 'maneuver'
and defined ($evt2 = $link{target_quat})
and abs($evt->tstart - $evt2->tstart) < 30) {
$evt->target_quat($evt2);
$evt2->maneuver($evt);
}
if ($evt->type eq 'target_quat'
and defined ($evt2 = $link{maneuver})
and abs($evt->tstart - $evt2->tstart) < 30) {
$evt->maneuver($evt2);
$evt2->target_quat($evt);
}
}
}
####################################################################################
sub get_violation_events {
####################################################################################
my $obsid = shift;
my $snap = shift;
my $event = shift;
my $evt;
my @violation;
local $_;
my $obsid_evt = get_obsid_event($obsid, $snap, $event) or return;
my ($time_maneuver, $date_maneuver, $load_name);
eval {
$time_maneuver = $obsid_evt->maneuver->tstop;
$date_maneuver = $obsid_evt->maneuver->date_stop;
$load_name = $obsid_evt->load_segment->load_name;
};
if ($@) {
warning("Problem in get_violation_events, no PCAD violation events");
print STDERR "ERROR - $@";
return;
}
my @constraints;
push @constraints, get_constraints($load_name, ''); # PCAD constraints
my $constraint;
for $constraint (@constraints) {
my $time_constr = date2time($constraint->{date}, 'unix');
my $delta_t = $time_constr - $time_maneuver;
if (abs($delta_t) < 60) {
if ($Debug) {
print "Found matching PCAD constraint record (delta = $delta_t):\n";
print " type: ", $constraint->{violation}{type}, "\n";
print " pcad: ", $constraint->{date}, "\n";
print " manv: $date_maneuver\n";
}
my $violation_descr = $constraint->{violation}{type};
if ($constraint->{violation}{subtype}) {
$violation_descr .= ": $constraint->{violation}{subtype}";
}
push @violation, Event->new('Type Description'=> 'Violation',
'TStart (GMT)' => $constraint->{violation}{date},
'TStop (GMT)' => $constraint->{violation}{date},
'violation' => $violation_descr,
);
}
}
return @violation;
}
####################################################################################
sub get_constraints_text {
####################################################################################
my $load_name = shift;
my $prefix = shift;
local $_;
my $error;
# Get the constraint check file. Try a pre-existing local version
# first, then try approved products, and finally the backstop products.
my ($mon, $day, $yr, $rev) = ($load_name =~ /(\w\w\w)(\d\d)(\d\d)(\w)/);
my $occ_web_name = "${prefix}${load_name}.txt";
my $year = $yr + 1900 + ($yr<97 ? 100 : 0);
my $path_approved = "PRODUCTS/APPR_LOADS/$year/$mon/$load_name";
my $path_backstop = "Backstop/$load_name";
my $file = io("$TaskData/$opt{file}{pcad_constraints}/$occ_web_name");
$load_info{name} = $load_name;
if (-r "$file") {
$file > $_;
$load_info{URL} = (/% URL: (.+)/) ? $1 : 'NotFound';
} else {
my $error;
foreach my $path ($path_approved, $path_backstop) {
$load_info{URL} = "$opt{url}{mission_planning}/$path";
($_, $error) = get_url("$load_info{URL}/output/$occ_web_name",
user => $occweb_user,
passwd => $occweb_passwd,
timeout => $opt{timeout}
);
last if not defined $error;
}
if (defined $error) {
$load_info{URL} = 'NotFound';
return ('', $error);
}
# Write content to file. Assert ensures that path exists
"% URL: $load_info{URL}\n" > $file->assert;
$_ >> $file;
}
return ($_, $error);
}
####################################################################################
sub get_constraints {
####################################################################################
my $load_name = shift;
my $prefix = shift;
my @constraint;
local $_;
my $error;
# Get the constraint check file. Try the specified load revision
# (e.g. "C") then step back to "B" and "A" to try to find constraints file.
my ($mon, $day, $yr, $rev) = ($load_name =~ /(\w\w\w)(\d\d)(\d\d)(\w)/);
my $orig_load_name = $load_name;
while ($rev ge "A") {
($_, $error) = get_constraints_text($load_name, $prefix);
if (defined $error) {
$rev = chr(ord($rev) - 1);
$load_name = "${mon}${day}${yr}${rev}";
} else {
last
}
}
if (defined $error) {
warning("Could not find constraints file for any load version of "
. substr("${prefix}${orig_load_name}", 0, -1));
return;
} elsif ($load_name ne $orig_load_name) {
warning("Using constraints file ${prefix}${load_name} instead of ${prefix}${orig_load_name}");
}
# Parse the constraints
#
# Attitude Hold violation predictions
# %-----------------------------------------------------------
# Target Start Time: 2005:247:23:31:33.993
# Target Quaternion: 0.46452128 0.21824985 -0.03489846 0.85753663
# Target RA/Dec/Roll: 9.00 -24.00 58.80
# PLINE Violation: 2005:251:18:16:33.000
# TEPHIN Violation: +Inf
#
# Target Start Time: 2005:247:23:31:33.993
# Target Quaternion: 0.46452128 0.21824985 -0.03489846 0.85753663
# Target RA/Dec/Roll: 9.00 -24.00 58.80
# Attitude Violation: SPM 2005:253:09:06:33.000
# High Momentum: 2005:250:07:41:33.993
my @match;
s/.+?Attitude Hold violation predictions//s; # Chuck everything before the att. viol. predicts
my @constraint_lines = split "\n", $_;
my ($date, $quat, $att);
my $violation_RE = qr/Attitude Violation|High Momentum|PLINE Violation|TEPHIN Violation|TCYLAFT6 Violation/;
foreach (@constraint_lines) {
if (/^Target Start Time:\s*($DateRE)\s*\z/) {
$date = $1;
}
if (/^Target Quaternion:\s*($FloatRE)\s+($FloatRE)\s+($FloatRE)\s+($FloatRE)\s*\z/) {
$quat = [$1, $2, $3, $4];
}
if (/^Target RA\/Dec\/Roll:\s*($FloatRE)\s+($FloatRE)\s+($FloatRE)\s*\z/) {
$att = [$1, $2, $3];
}
if (/^($violation_RE):\s*(\S*?)\s+($DateRE|\+Inf)/) {
my $viol = { type => $1,
subtype => $2, # Sub-type of violation, e.g. SPM = Sun Position Monitor
date => $3 };
$viol->{type} .= ' (POSSIBLY UNRELIABLE)' if ($viol->{type} =~ /TEPHIN Violation/);
if ($viol->{date} eq '+Inf') { # Add 12 days to current time if constraint date = +Inf
$viol->{date} = $conv_time->date($CURRENT_TIME->unix + 86400*12);
$viol->{subtype} = 'NONE';
}
push @constraint, {date => $date,
quat => $quat,
att => $att,
violation => $viol ,
};
}
}
return @constraint;
}
####################################################################################
sub get_url {
####################################################################################
my $url = shift;
my %opt = (timeout => 60,
@_);
my $user_agent = LWP::UserAgent->new;
$user_agent->timeout($opt{timeout});
my $req = HTTP::Request->new(GET => $url);
$req->authorization_basic($opt{user}, $opt{passwd})
if (defined $opt{user} and defined $opt{passwd});
my $response = $user_agent->request($req);
if ($response->is_success) {
return ($response->content, undef);
} else {
return (undef, $response->status_line . "\n$url\n");
}
}
####################################################################################
sub get_current_load_name {
####################################################################################
my $event = shift;
local $_;
my @rev_event = sort { $b->tstart <=> $a->tstart } @{$event};
# really need to figure out load of current obsid if SCS107 has run
for (@rev_event) {
# Probably need to compare against SCS107 time? errr.
if ($_->type eq 'load_segment' and $_->tstart <= $CurrentTime) {
return uc $_->get('LOADSEG.LOAD_NAME');
}
}
return 0; # gotta do better than this
}
####################################################################################
sub make_web_page {
####################################################################################
my $snap = shift;
my $event = shift;
my $web_data = shift;
my $html = "";
my $q = CGI->new;
my @table;
my $table;
local $_;
$html .= $q->start_html(-title => $opt{web_page}{title_short},
-style => [{-code => $opt{web_page}{style} },
{-src => "timeline.css",}
],
-noScript => $opt{web_refresh}{NoScript},
-script => [{ -src => 'timeline_states.js'},
{ -src => 'timeline.js'},
{ -language => 'JavaScript',
-code => $opt{web_refresh}{JavaScript},
},
{ -language => 'JavaScript1.1',
-code => $opt{web_refresh}{JavaScript11},
},
{ -language => 'JavaScript1.2',
-code => $opt{web_refresh}{JavaScript12},
},
],
-onLoad => 'doLoad(); initTimeLineHandlers()',
);
$html .= $q->p({style => "text-align:center"},
$q->img({src => "$opt{file}{title_image}"}));
$html .= $q->p({style => 'font-size:130%; font-weight:bold; text-align:center'},
"Page content updated: ",
Event::format_date(time2date($CurrentTime, 'unix_time')),
" (", Event::calc_local_date($CurrentTime), ")"
);
$html .= make_warning_table(@warn) . $q->p if (@warn);
my $snap_table = make_snap_table($snap) . $q->p;
$html .= HTML::Table->new(-align => 'center',
-padding => 2,
-data => [[$q->img({src=>$web_data->{orbit_image}{content}{orbit}{file}}),
$snap_table]],
)->getTable;
$html .= $opt{timeline_html};
my $avail_comms_html < io("$TaskData/comms_avail.html");
$html .= $avail_comms_html;
$html .= $q->p . make_event_table($event) . $q->p;
$html .= HTML::Table->new(-align => 'center',
-padding => 2,
-data => [[make_ephin_goes_table($snap, $web_data),
make_ace_table($snap, $web_data)]]
)->getTable;
my $image_title_style = "text-align:center;$opt{web_page}{table_caption_style}";
$html .= $q->p({style => $image_title_style},
"ACE particle rates (",
$q->a({href=>$opt{url}{mta_ace}, target => "_blank"}, "MTA"),
$q->a({href=>$opt{url}{swpc_ace_rtsw}, target => "_blank"}, "SWPC"),
")",
$q->br,
$q->img({class=>"boxed", src => $web_data->{ace}{image}{five_min}{file}}),
);
$html .= $q->p({style => $image_title_style},
$q->a({href => $opt{url}{mta_goes}, target => "_blank"}, "GOES particle rates"),
$q->br,
$q->img({class=>"boxed", width => 700,
src => $web_data->{goes}{image}{five_min}{file}})
);
$html .= $q->p({style => $image_title_style},
"GOES proxy for HRC shield rates",
$q->br,
$q->img({class=>"boxed", src => "hrc_shield.png"})
);
$html .= # $q->div({style => 'width:700'},
make_solar_forecast_table($web_data);
# );
$html .= $q->p({style => $image_title_style},
$q->a({href => $opt{url}{goes_xray_flux}, target => "_blank"}, "Solar X-ray Activity"),
$q->br,
$q->img({class=>"boxed", src => 'goes_x.png'})
);
$html .= $q->p({style => $image_title_style},
"Solar Wind Data (",
$q->a({href => $opt{url}{soho_solar_wind}, target => "_blank"}, "SOHO"),
$q->a({href => $opt{url}{ace_solar_wind}, target => "_blank"}, "ACE"),
")",
$q->br,
$q->img({class=>"boxed", src => $web_data->{solar_wind}{image}{solar_wind}{file}})
);
$html .= $q->p({style => $image_title_style},
$q->a({href => $opt{url}{solar_flare_monitor}, target => "_blank"}, "Solar Flare Monitor"),
$q->br,
$q->img({class=>"boxed", src => $web_data->{solar_flare_monitor}{image}{solar_flare_monitor}{file}})
);
$html .= $q->end_html;
return $html;
}
####################################################################################
sub install_web_files {
####################################################################################
my $html = shift;
my $web_content = shift;
local $_;
# Ensure that web dir exists
eval { io($opt{file}{web_dir})->mkpath };
die "ERROR - could not create web directory $opt{file}{web_dir}: $@\n" if $@;
$html > io("$opt{file}{web_dir}/$opt{file}{web_page}");
foreach (qw(timeline_png timeline_states)){
my $in = io("$TaskData/$opt{file}{$_}");
my $out =io("$opt{file}{web_dir}/$opt{file}{$_}");
$in > $out if (not -e "$out" or $in->mtime > $out->mtime);
}
foreach (qw(title_image blue_paper blue_paper_test
timeline_js timeline_css vert_line)) {
my $in = io("$TaskShare/$opt{file}{$_}");
my $out =io("$opt{file}{web_dir}/$opt{file}{$_}");
$in > $out if (not -e "$out" or $in->mtime > $out->mtime);
}
# Go through each web site where data/images were retrieved
foreach my $web (values %{$web_content}) {
next if defined $web->{warn}; # Skip if there was a warning
foreach my $image (values %{$web->{image}}, values %{$web->{content}}) {
next if defined $image->{warn};
next unless defined $image->{outfile};
# Copy new image file if infile exists and outfile either does not exist
# or is older than infile
my $in = io($image->{outfile});
my $out = io($opt{file}{web_dir} . "/" . $image->{file});
next unless -e "$in";
if ((not -e "$out") or $in->mtime > $out->mtime) {
if ($image->{convert}) {
my ($status) = run("convert $in $image->{convert} $out");
warning($status) if $status;
} else {
$in > $out ;
}
}
}
}
}
####################################################################################
sub average {
####################################################################################
my $arr = shift;
return unless defined $arr and @{$arr};
local $_;
my $sum = 0;
$sum += $_ foreach @{$arr};
return $sum / @{$arr};
}
####################################################################################
sub make_warning_table {
####################################################################################
my @warn = @_;
my @table = map { [$_] } @warn;
my $table = new HTML::Table(-align => 'center',
-border => 2,
-spacing => 0,
-padding => 2,
-style => 'color:red',
-rules => 'none',
-data => \@table,
-width => '75%',
);
$table->setColStyle(1,"font-size:120%");
$table->setCaption("<span style=$opt{web_page}{table_caption_style}> " .
" Warnings</span>", 'TOP');
return $table->getTable;
}
####################################################################################
sub make_solar_forecast_table {
####################################################################################
my $web_data = shift;
my @table = ([ 'Geophysical Activity', $web_data->{space_weather}{content}{geophys_forecast}{content} ],
[ 'Solar Activity', $web_data->{space_weather}{content}{solar_forecast}{content} ]);
my $table = new HTML::Table(-align => 'center',
-spacing => 0,
-padding => 2,
-rules => 'none',
-border => 2,
-data => \@table,
-width => '700',
);
$table->setColHead(1);
$table->setCaption("<span style=$opt{web_page}{table_caption_style}> " .
" 3-day Solar-Geophysical Forecast</span>", 'TOP');
return $table->getTable;
}
####################################################################################
sub make_ace_table {
####################################################################################
my $snap = shift;
my $web_data = shift;
local $_;
my @table;
my %val;
my %tab_def = %{$opt{ace_table}};
my $n_row = @{$tab_def{row}}+1; # Including row and col headers
my $n_col = @{$tab_def{col}}+1;
my $start = qr/YR \s+ MO \s+ DA \s+ HHMM \s+ 38-53 .+ \s*/x;
my ($ace_date, $ace_p3);
if (defined $web_data->{ace}{content}{flux}{content}){
($ace_date, $ace_p3) = parse_mta_rad_data($start,
$web_data->{ace}{content}{flux}{content},
7);
}
return '<h2 style="color:red;text-align:center">NO RECENT ACE DATA</h2>' unless (defined $ace_p3 and @{$ace_p3});
my ($fluence_date, $orbital_fluence) = parse_mta_rad_data(qr/ACIS Fluence data...Start DOY,SOD/,
$web_data->{acis_ace}{content}{ace_fluence}{content},
9 );
$orbital_fluence = $orbital_fluence->[0];
my $orbital_fluence_limit = 2e9;
my $orbital_flux_limit = $orbital_fluence_limit / 170000;
my $two_hr_limit = '50000';
my $last_p3 = $ace_p3->[-1];
my $avg_p3 = average($ace_p3);
my $hours_to_limit = '> 30';
if ($orbital_fluence > $orbital_fluence_limit) {
$hours_to_limit = 0;
} elsif ($last_p3 > 10) {
my $hours = ($orbital_fluence_limit - $orbital_fluence) / $last_p3 / 3600;
$hours_to_limit = format_number($hours, 2) if $hours < 30;
}
$val{'Current flux'}{Value} = format_number($last_p3 , 3);
$val{'Current flux'}{'2hr limit'} = format_number($two_hr_limit, 3);
$val{'Current flux'}{'Orbital limit'} = format_number($orbital_flux_limit, 2);
$val{'Current flux'}{'Hours to<br>Orbital limit'} = $hours_to_limit;
$hours_to_limit = '> 30';
if ($orbital_fluence > $orbital_fluence_limit) {
$hours_to_limit = 0;
} elsif ($avg_p3 > 10) {
my $hours = ($orbital_fluence_limit - $orbital_fluence) / $avg_p3 / 3600;
$hours_to_limit = format_number($hours,2) if $hours < 30;
}
$val{'2hr avg flux'}{Value} = format_number($avg_p3 , 3);
$val{'2hr avg flux'}{'2hr limit'} = format_number($two_hr_limit, 3);
$val{'2hr avg flux'}{'Orbital limit'} = format_number($orbital_flux_limit, 2);
$val{'2hr avg flux'}{'Hours to<br>Orbital limit'} = $hours_to_limit;
$val{'Orbital fluence'}{Value} = format_number($orbital_fluence, 2);
$val{'Orbital fluence'}{'2hr limit'} = '---';
$val{'Orbital fluence'}{'Orbital limit'} = format_number($orbital_fluence_limit, 2);
$val{'Orbital fluence'}{'Hours to<br>Orbital limit'} = '---';
my $i = 0;
for my $row (@{$tab_def{row}}) {
my $j = 0;
$table[$i+1][0] = $tab_def{row}[$i];
for my $col (@{$tab_def{col}}) {
$table[0][$j+1] = $tab_def{col}[$j];
$table[$i+1][$j+1] = defined $val{$row}{$col} ? $val{$row}{$col} : '';
$j++;
}
$i++;
}
my $footnotes = "ACE data from $ace_date";
$footnotes .= "<br>Orbital fluence: integrated attenuated ACE flux";
$footnotes .= "<br>Grating attenuation not factored into current or 2hr flux numbers";
$footnotes .= qq{<br><a href="alert_limits.html">RADMON and SOT alert limits information</a>};
$table[$n_row][0] = $footnotes;
my $table = new HTML::Table(-align => 'center',
-rules => 'all',
-border => 2,
-spacing => 0,
-padding => 2,
-data => \@table,
);
$table->setCellHead(1,$_) foreach (1..$n_col);
$table->setCellHead($_,1) foreach (1..$n_row);
for $i (2..$n_row) {
for my $j (2..$n_col) {
$table->setCellAlign($i,$j,'RIGHT');
}
}
$table->setCellColSpan($n_row+1, 1, $n_col);
$table->setCellStyle($n_row+1, 1, $tab_def{footnote_style});
$table->setCaption("<span style=$opt{web_page}{table_caption_style}> " .
" ACE rates</span>", 'TOP');
return $table->getTable;
}
####################################################################################
sub make_ephin_goes_table {
####################################################################################
my $snap = shift;
my $web_data = shift;
local $_;
my @table;
my %val;
my %tab_def = %{$opt{ephin_goes_table}};
my ($hrc_shield_proxy, $hrc_time) = split(' ', io($opt{file}{hrc_shield})->slurp());
my ($p4gm_proxy, $p4gm_time) = split(' ', io($opt{file}{p4gm})->slurp());
my ($p41gm_proxy, $p41gm_time) = split(' ', io($opt{file}{p41gm})->slurp());
my $ephin_date = $snap->{obt}{value} . ' (' .
Event::calc_delta_date($snap->{obt}{value}) . ')';
my $hrc_delta = Event::calc_delta_date($hrc_time);
my $p4gm_delta = Event::calc_delta_date($p4gm_time);
my $p41gm_delta = Event::calc_delta_date($p41gm_time);
# Add a warning above the CXO GOES rates table if the proton data for these
# is stale. Since these times are all from the proton/get_hrc data they
# should be the same, but that implementation could change.
my $warning = '<h2 style="color:red;text-align:center">';
if (($CurrentTime - $hrc_time) > 86400){
$warning .= "HRC proxy data stale<br/>";
}
if (($CurrentTime - $p4gm_time) > 86400){
$warning .= "P4GM data stale<br/>";
}
if (($CurrentTime - $p41gm_time) > 86400){
$warning .= "P41GM data stale<br/>";
}
$warning .= "</h2>";
$val{GOES}{P4GM} = sprintf("%.2f", $p4gm_proxy);
$val{GOES}{P41GM} = sprintf("%.2f", $p41gm_proxy);
$val{GOES}{"HRC shield"} = sprintf("%.0f", $hrc_shield_proxy);
$val{CXO}{"HRC shield"} = $snap->{hrcshield}{value};
$val{CXO}{"HRC MCP"} = $snap->{hrcmcp}{value};
$val{CXO}{P4GM} = '---';
$val{CXO}{P41GM} = '---';
$val{Limit}{"HRC shield"} = 250;
$val{Limit}{"HRC MCP"} = 30;
$val{Limit}{P4GM} = 300.0;
$val{Limit}{P41GM} = 8.47;
my $n_row = @{$tab_def{row}}+1; # Including row and col headers
my $n_col = @{$tab_def{col}}+1;
my $i = 0;
for my $row (@{$tab_def{row}}) {
my $j = 0;
$table[$i+1][0] = $tab_def{row}[$i];
for my $col (@{$tab_def{col}}) {
$table[0][$j+1] = $tab_def{col}[$j];
$table[$i+1][$j+1] = defined $val{$col}{$row} ? $val{$col}{$row} : '';
$j++;
}
$i++;
}
my $footnotes = "CXO: from snapshot at $ephin_date<br />";
$footnotes .= "RadMon: DISABLED<br />" if ($snap->{radmon}{value} ne 'ENAB');
$footnotes .= "GOES proxies: scaled 15 min average of GOES-16 <br />";
$footnotes .= "Last avgs at HRC: $hrc_delta P4GM: $p4gm_delta P41GM: $p41gm_delta";
$table[$n_row][0] = $footnotes;
my $table = new HTML::Table(-align => 'center',
-rules => 'all',
-border => 2,
-spacing => 0,
-padding => 2,
-data => \@table,
);
$table->setCellHead(1,$_) foreach (1..$n_col);
$table->setCellHead($_,1) foreach (1..$n_row);
$table->setColStyle(2, "color:#$opt{color}{event_disabled}")
if ($snap->{radmon}{value} ne 'ENAB');
$table->setRowStyle(4, "color:#$opt{color}{event_disabled}");
for $i (2..$n_row) {
for my $j (2..$n_col) {
$table->setCellAlign($i,$j,'RIGHT');
}
}
$table->setCellColSpan($n_row+1, 1, $n_col);
$table->setCellStyle($n_row+1, 1, $tab_def{footnote_style});
$table->setCaption("<span style=$opt{web_page}{table_caption_style}> " .
" CXO and GOES rates</span>", 'TOP');
return $warning . $table->getTable;
}
####################################################################################
sub parse_mta_rad_data {
####################################################################################
my $start = shift;
local $_ = shift;
my @col = @_;
my $date;
my $valid_val;
my @dat;
return unless /$start\s*/gx;
while (/\G \s* (\d\d\d\d .+) \s*/gx) {
my @val = split ' ', $1;
my $all_ok = 1;
for my $i (0..$#col) {
if ($val[$col[$i]] >= 0) {
push @{$dat[$i]}, $val[$col[$i]] ;
} else {
$all_ok = 0;
}
}
# Keep track of the last set of good data to extract time/date
$valid_val = [ @val ] if $all_ok;
}
# If there was some good data, then compute date and rel date.
if ($valid_val) {
my ($year, $mon, $mday, $hhmm) = @{$valid_val};
my $min = $hhmm % 100;
my $hour = POSIX::floor($hhmm / 100);
my $time = timegm(0, $min, $hour, $mday, $mon-1, $year);
$date = Event::format_date(time2date($time, 'unix'))
. ' (' . Event::calc_delta_date($time) . ')';
}
return ($date, @dat);
}
####################################################################################
sub make_event_table {
####################################################################################
my $event = shift;
my @table = ();
# Collect the event data into a table array. Keep track of the color of each row
my @col = @{$opt{event_table}{col}};
my @row_bg_color = ();
my @row_font_color = ();
foreach my $evt (@{$event}) {
next unless exists $opt{event_table}{$evt->type};
next unless (($CurrentTime - $evt->tstart < $opt{event_table}{display_range}{hours_pre}*3600
and $evt->tstart - $CurrentTime < $opt{event_table}{display_range}{hours_post}*3600)
or $evt->type eq 'violation');
push @table, [ map { $evt->$_ } @col ];
push @row_bg_color, $opt{event_table}{$evt->type};
push @row_font_color, get_event_font_color($evt);
}
my $table = new HTML::Table(-align => 'center',
-rules => 'all',
-border => 2,
-spacing => 0,
-padding => 2,
-head=> ['Time (UT)', 'Event', 'Delta time', 'Time (Eastern)'],
-data => \@table,
);
# Set the formatting of columns
for (0..$#col) {
my $ec = $opt{event_table}{$col[$_]};
$table->setColAlign($_+1, $ec->{align}) if $ec->{align};
my $pre = $ec->{format_pre} || '';
my $post = $ec->{format_post} || '';
$table->setColFormat($_+1, $pre, $post);
}
# Set the color of each row
for (0..$#row_bg_color) {
if (defined $row_bg_color[$_]) {
my $bg_color = $opt{color}{$row_bg_color[$_]};
$table->setRowBGColor($_+2, "#$bg_color") if defined $bg_color;
}
if (defined $row_font_color[$_]) {
my $font_color = $opt{color}{$row_font_color[$_]};
$table->setRowStyle($_+2, "color:#$font_color") if defined $font_color;
}
}
$table->setRowAlign(1, 'CENTER');
my $load_link = ((defined $load_info{URL}) and (defined $load_info{name})) ?
" (Load: <a href=\"$load_info{URL}/$load_info{name}.php\" target =\"_blank\">$load_info{name}</a>)"
: " (Load link unavailable)";
$table->setCaption("<span style=$opt{web_page}{table_caption_style}>"
. "Chandra Events"
. $load_link
. "</span>", 'TOP');
return $table->getTable;
}
####################################################################################
sub make_snap_table {
####################################################################################
my $snap = shift;