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