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