source: src/mlx/pirep.py@ 385:f775fd26f011

Last change on this file since 385:f775fd26f011 was 345:a62373a28d90, checked in by István Váradi <ivaradi@…>, 12 years ago

Implemented the new, more flexible way of storing the log during flight (#143)

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