[401] | 1 |
|
---|
| 2 | from util import utf2unicode
|
---|
[298] | 3 |
|
---|
| 4 | import const
|
---|
| 5 | import cPickle as pickle
|
---|
[97] | 6 |
|
---|
| 7 | #------------------------------------------------------------------------------
|
---|
| 8 |
|
---|
[298] | 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.
|
---|
[97] | 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 + "]"
|
---|
[151] | 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:
|
---|
[303] | 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
|
---|
[151] | 44 | except Exception, e:
|
---|
[401] | 45 | print "Failed loading PIREP from %s: %s" % (path,
|
---|
| 46 | utf2unicode(str(e)))
|
---|
[151] | 47 | return None
|
---|
[345] | 48 |
|
---|
[262] | 49 | def __init__(self, flight):
|
---|
| 50 | """Initialize the PIREP from the given flight."""
|
---|
| 51 | self.bookedFlight = flight.bookedFlight
|
---|
[303] | 52 |
|
---|
| 53 | self.numCrew = flight.numCrew
|
---|
| 54 | self.numPassengers = flight.numPassengers
|
---|
| 55 | self.bagWeight = flight.bagWeight
|
---|
[262] | 56 | self.cargoWeight = flight.cargoWeight
|
---|
[303] | 57 | self.mailWeight = flight.mailWeight
|
---|
[345] | 58 |
|
---|
[262] | 59 | self.filedCruiseAltitude = flight.filedCruiseAltitude
|
---|
| 60 | self.cruiseAltitude = flight.cruiseAltitude
|
---|
| 61 | self.route = flight.route
|
---|
[97] | 62 |
|
---|
[262] | 63 | self.departureMETAR = flight.departureMETAR.upper()
|
---|
| 64 | self.arrivalMETAR = flight.arrivalMETAR.upper()
|
---|
[97] | 65 |
|
---|
[262] | 66 | self.departureRunway = flight.departureRunway.upper()
|
---|
| 67 | self.sid = flight.sid.upper()
|
---|
[97] | 68 |
|
---|
[262] | 69 | self.star = flight.star
|
---|
| 70 | self.transition = flight.transition
|
---|
| 71 | self.approachType = flight.approachType.upper()
|
---|
| 72 | self.arrivalRunway = flight.arrivalRunway.upper()
|
---|
[97] | 73 |
|
---|
[262] | 74 | self.flightType = flight.flightType
|
---|
| 75 | self.online = flight.online
|
---|
[97] | 76 |
|
---|
[262] | 77 | self.comments = flight.comments
|
---|
| 78 | self.flightDefects = flight.flightDefects
|
---|
| 79 | self.delayCodes = flight.delayCodes
|
---|
[345] | 80 |
|
---|
[97] | 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
|
---|
[102] | 86 | self.fuelUsed = flight.startFuel - flight.endFuel
|
---|
[97] | 87 |
|
---|
[262] | 88 | logger = flight.logger
|
---|
[97] | 89 | self.rating = logger.getRating()
|
---|
| 90 | self.logLines = logger.lines
|
---|
| 91 | self.faultLineIndexes = logger.faultLineIndexes
|
---|
[345] | 92 |
|
---|
[97] | 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)
|
---|
[345] | 115 | text += "\n"
|
---|
[97] | 116 |
|
---|
| 117 | text += "\n[Flight Rating: %.1f]" % (max(0.0, self.rating),)
|
---|
| 118 |
|
---|
| 119 | return text
|
---|
[345] | 120 |
|
---|
[97] | 121 | def getTimeComment(self):
|
---|
| 122 | """Get the time comment.
|
---|
| 123 |
|
---|
| 124 | This is basically a collection of the delay codes, if any."""
|
---|
[99] | 125 | if not self.delayCodes:
|
---|
| 126 | return "UTC"
|
---|
| 127 | else:
|
---|
| 128 | s = ""
|
---|
| 129 | for code in self.delayCodes:
|
---|
| 130 | if s: s += ", "
|
---|
[437] | 131 | s += code
|
---|
[99] | 132 | return s
|
---|
[97] | 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()
|
---|
[151] | 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)
|
---|
[393] | 149 | return None
|
---|
[151] | 150 | except Exception, e:
|
---|
[401] | 151 | error = utf2unicode(str(e))
|
---|
| 152 | print u"Failed saving PIREP to %s: %s" % (path, error)
|
---|
[393] | 153 | return error
|
---|