1 |
|
---|
2 | from util import utf2unicode
|
---|
3 |
|
---|
4 | import const
|
---|
5 | import cPickle as pickle
|
---|
6 |
|
---|
7 | #------------------------------------------------------------------------------
|
---|
8 |
|
---|
9 | ## @package mlx.pirep
|
---|
10 | #
|
---|
11 | # The PIREP module.
|
---|
12 | #
|
---|
13 | # This module defines only one class, \ref PIREP. It is used to extract and
|
---|
14 | # store the information needed for a PIREP. The saved PIREPs are pickled
|
---|
15 | # instances of this class.
|
---|
16 |
|
---|
17 | #------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | class PIREP(object):
|
---|
20 | """A pilot's report of a flight."""
|
---|
21 | @staticmethod
|
---|
22 | def _formatLine(timeStr, line):
|
---|
23 | """Format the given time string and line as needed for the ACARS and
|
---|
24 | some other things."""
|
---|
25 | return "[" + timeStr + "]-[" + line + "]"
|
---|
26 |
|
---|
27 | @staticmethod
|
---|
28 | def load(path):
|
---|
29 | """Load a PIREP from the given path.
|
---|
30 |
|
---|
31 | Returns the PIREP object, or None on error."""
|
---|
32 | try:
|
---|
33 | with open(path, "rb") as f:
|
---|
34 | pirep = pickle.load(f)
|
---|
35 | if "numCrew" not in dir(pirep):
|
---|
36 | pirep.numCrew = pirep.bookedFlight.numCrew
|
---|
37 | if "numPassengers" not in dir(pirep):
|
---|
38 | pirep.numPassengers = pirep.bookedFlight.numPassengers
|
---|
39 | if "bagWeight" not in dir(pirep):
|
---|
40 | pirep.bagWeight = pirep.bookedFlight.bagWeight
|
---|
41 | if "mailWeight" not in dir(pirep):
|
---|
42 | pirep.mailWeight = pirep.bookedFlight.mailWeight
|
---|
43 | return pirep
|
---|
44 | except Exception, e:
|
---|
45 | print "Failed loading PIREP from %s: %s" % (path,
|
---|
46 | utf2unicode(str(e)))
|
---|
47 | return None
|
---|
48 |
|
---|
49 | def __init__(self, flight):
|
---|
50 | """Initialize the PIREP from the given flight."""
|
---|
51 | self.bookedFlight = flight.bookedFlight
|
---|
52 |
|
---|
53 | self.numCrew = flight.numCrew
|
---|
54 | self.numPassengers = flight.numPassengers
|
---|
55 | self.bagWeight = flight.bagWeight
|
---|
56 | self.cargoWeight = flight.cargoWeight
|
---|
57 | self.mailWeight = flight.mailWeight
|
---|
58 |
|
---|
59 | self.filedCruiseAltitude = flight.filedCruiseAltitude
|
---|
60 | self.cruiseAltitude = flight.cruiseAltitude
|
---|
61 | self.route = flight.route
|
---|
62 |
|
---|
63 | self.departureMETAR = flight.departureMETAR.upper()
|
---|
64 | self.arrivalMETAR = flight.arrivalMETAR.upper()
|
---|
65 |
|
---|
66 | self.departureRunway = flight.departureRunway.upper()
|
---|
67 | self.sid = flight.sid.upper()
|
---|
68 |
|
---|
69 | self.star = flight.star
|
---|
70 | self.transition = flight.transition
|
---|
71 | self.approachType = flight.approachType.upper()
|
---|
72 | self.arrivalRunway = flight.arrivalRunway.upper()
|
---|
73 |
|
---|
74 | self.flightType = flight.flightType
|
---|
75 | self.online = flight.online
|
---|
76 |
|
---|
77 | self.comments = flight.comments
|
---|
78 | self.flightDefects = flight.flightDefects
|
---|
79 | self.delayCodes = flight.delayCodes
|
---|
80 |
|
---|
81 | self.blockTimeStart = flight.blockTimeStart
|
---|
82 | self.flightTimeStart = flight.flightTimeStart
|
---|
83 | self.flightTimeEnd = flight.flightTimeEnd
|
---|
84 | self.blockTimeEnd = flight.blockTimeEnd
|
---|
85 | self.flownDistance = flight.flownDistance
|
---|
86 | self.fuelUsed = flight.startFuel - flight.endFuel
|
---|
87 |
|
---|
88 | logger = flight.logger
|
---|
89 | self.rating = logger.getRating()
|
---|
90 | self.logLines = logger.lines
|
---|
91 | self.faultLineIndexes = logger.faultLineIndexes
|
---|
92 |
|
---|
93 | def getACARSText(self):
|
---|
94 | """Get the ACARS text.
|
---|
95 |
|
---|
96 | This is a specially formatted version of the log without the faults."""
|
---|
97 | text = "[MAVA LOGGER X LOG]-[%s]" % (const.VERSION,)
|
---|
98 | for index in range(0, len(self.logLines)):
|
---|
99 | if index not in self.faultLineIndexes:
|
---|
100 | (timeStr, line) = self.logLines[index]
|
---|
101 | if timeStr is not None:
|
---|
102 | text += PIREP._formatLine(timeStr, line)
|
---|
103 | return text
|
---|
104 |
|
---|
105 | def getRatingText(self):
|
---|
106 | """Get the rating text.
|
---|
107 |
|
---|
108 | This is a specially formatted version of the lines containing the
|
---|
109 | faults."""
|
---|
110 | text = ""
|
---|
111 | for index in self.faultLineIndexes:
|
---|
112 | (timeStr, line) = self.logLines[index]
|
---|
113 | if timeStr is not None:
|
---|
114 | text += PIREP._formatLine(timeStr, line)
|
---|
115 | text += "\n"
|
---|
116 |
|
---|
117 | text += "\n[Flight Rating: %.1f]" % (max(0.0, self.rating),)
|
---|
118 |
|
---|
119 | return text
|
---|
120 |
|
---|
121 | def getTimeComment(self):
|
---|
122 | """Get the time comment.
|
---|
123 |
|
---|
124 | This is basically a collection of the delay codes, if any."""
|
---|
125 | if not self.delayCodes:
|
---|
126 | return "UTC"
|
---|
127 | else:
|
---|
128 | s = ""
|
---|
129 | for code in self.delayCodes:
|
---|
130 | if s: s += ", "
|
---|
131 | s += code
|
---|
132 | return s
|
---|
133 |
|
---|
134 | def getSTAR(self):
|
---|
135 | """Get the STAR and/or the transition."""
|
---|
136 | star = self.star if self.star is not None else ""
|
---|
137 | if self.transition is not None:
|
---|
138 | if star: star += ", "
|
---|
139 | star += self.transition
|
---|
140 | return star.upper()
|
---|
141 |
|
---|
142 | def save(self, path):
|
---|
143 | """Save the PIREP to the given file.
|
---|
144 |
|
---|
145 | Returns whether the saving has succeeded."""
|
---|
146 | try:
|
---|
147 | with open(path, "wb") as f:
|
---|
148 | pickle.dump(self, f)
|
---|
149 | return None
|
---|
150 | except Exception, e:
|
---|
151 | error = utf2unicode(str(e))
|
---|
152 | print u"Failed saving PIREP to %s: %s" % (path, error)
|
---|
153 | return error
|
---|