1#!/usr/bin/env python3
 2
 3"""Read from GEOEVENTS table and generate EVENTS."""
 4
 5import logging
 6
 7from chart.alg import init_algorithm
 8from chart.db import ts
 9from chart.backend.jobs import JobStatus
10from chart.events.event import Event
11
12logger = logging.getLogger()
13
14# prefix for event class names to mark them as geoevents
15# EVT_PREFIX = "GEOEVENT_"
16EVENT_CLASS = 'GEO-EVENTS'
17
18TABLE_GEOEVENTS = "GEO_EVENTS"
19
20
21def raise_geoevents(sid, start, stop, eventraiser):
22    """Raise GEO_EVENTS events."""
23
24    logger.info('Scanning for GEO Events Instances entries for {sid} from {start} to {stop}'.format(
25            sid=sid, start=start, stop=stop))
26
27    for (sensing_start,
28         name,
29         desc,
30         abs_orbit,
31         time_from_anx,
32         deg_from_anx,
33         params) in ts.select(sid=sid,
34                              table=TABLE_GEOEVENTS,
35                              fields=("SENSING_TIME",
36                                       "NAME",
37                                       "DESCRIPTION",
38                                       "ABSOLUTE_ORBIT_NUMBER",
39                                       "TIME_FROM_ANX",
40                                       "DEG_FROM_ANX",
41                                       "PARAMETERS"),
42                              sensing_start=start,
43                              sensing_stop=stop):
44
45        instance_properties={
46                "Name": name,
47                "Description": desc,
48                "Absolute_Orbit": abs_orbit,
49                "Time_From_ANX": time_from_anx,
50                "Deg_From_ANX": deg_from_anx,
51                "Parameters": params}
52                
53        eventraiser.add(Event(event_classname=EVENT_CLASS,
54                              sid=sid,
55                              start_time=sensing_start,
56                              instance_properties=instance_properties))
57
58
59def dispatch(wo, resultfile, eventraiser):
60    """We are being run by the dispatcher tool."""
61    logging.info("Creating geoevents events")
62    for job in wo.read_jobs():
63        logging.info(
64            "Begin event from {start} to {stop}".format(
65                start=job["sensing_start"], stop=job["sensing_stop"]
66            )
67        )
68
69        raise_geoevents(job.sid, job.sensing_start, job.sensing_stop, eventraiser)
70
71        resultfile.add_job(job, JobStatus.COMPLETED)
72
73    eventraiser.log_report()
74    logging.info("All done")
75
76
77def main():
78    """Command line entry point.
79    This program can either be called via `dispatcher` or standalone."""
80
81    dispatch(*init_algorithm())
82
83
84if __name__ == "__main__":
85    main()