1#!/usr/bin/env python3
 2
 3"""Introduction widget."""
 4
 5
 6
 7from collections import OrderedDict
 8
 9from chart.common.prettyprint import Table
10from chart.common.prettyprint import show_time
11from chart.common.prettyprint import show_date
12from chart.reports.widget import Widget
13
14
15class IntroductionWidget(Widget):
16    """Display a basic introduction to the report, showing SCID, start, stop
17    times and orbits.
18    """
19
20    name = 'introduction'
21
22    thumbnail = 'widgets/intro.png'
23
24    document_options = OrderedDict([
25            ('sid', {'type': 'sid'}),
26            ('sensing-start', {'type': 'datetime'}),
27            ('sensing-stop', {'type': 'datetime'})])
28
29    def __init__(self):
30        super(IntroductionWidget, self).__init__()
31
32    def html(self, document):
33        """Render ourselves."""
34        html = document.html
35        dc = document.config
36        start = dc['sensing-start']
37        stop = dc['sensing-stop']
38
39        t = Table(title=' ', cssclass='table introduction')
40
41        # t.append(('Ref', ''))
42        t.append(({'text': 'Spacecraft id',\
43                    'style': 'font-weight:bold'},
44                    dc['sid']))
45        # test if this is a regular daily/weekly/monthly/annual report...
46        if (start.hour == 0 and
47            start.minute == 0 and
48            start.second == 0 and
49            start.microsecond == 0 and
50            stop.hour == 0 and
51            stop.minute == 0 and
52            stop.second == 0 and
53            stop.microsecond == 0):
54
55            # ... in which case write the date only ...
56
57            # number of whole days in report
58            days = (stop - start).days
59
60            # week number and day of week for the start of the report
61            _, week, doy = start.isocalendar()
62            if days == 7 and doy == 1:
63                t.append(({'text': 'Week',\
64                    'style': 'font-weight:bold'},
65                    '{week:02}'.format(
66                            # year=start.year,
67                            week=week)))
68
69            t.append(({'text': 'Start time',\
70                    'style': 'font-weight:bold'},
71                    show_date(start)))
72            t.append(({'text': 'Stop time',\
73                    'style': 'font-weight:bold'}, '{stop} ({days} days)'.format(
74                        stop=show_date(stop),
75                        days=(stop - start).days)))
76
77        else:
78            # ... otherwise write date and time of day
79            t.append(({'text': 'Start time',\
80                    'style': 'font-weight:bold'}, show_time(start)))
81            t.append(({'text': 'Stop time',\
82                    'style': 'font-weight:bold'}, show_time(stop)))
83
84        sid = dc['sid']
85        if sid.orbit is not None:
86            first_orbit = sid.orbit.find_orbit(start)
87            last_orbit = sid.orbit.find_orbit(stop)
88            t.append(({'text': 'Orbits',\
89                    'style': 'font-weight:bold'},
90                    '{first} to {last}'.format(first=first_orbit, last=last_orbit)))
91
92        t.write_html(html)
93        html.write('\n')