[401] | 1 |
|
---|
| 2 | from util import utf2unicode
|
---|
[829] | 3 | from flight import Flight
|
---|
[298] | 4 |
|
---|
| 5 | import const
|
---|
| 6 | import cPickle as pickle
|
---|
[829] | 7 | import calendar
|
---|
[743] | 8 | import datetime
|
---|
| 9 | import time
|
---|
[97] | 10 |
|
---|
| 11 | #------------------------------------------------------------------------------
|
---|
| 12 |
|
---|
[298] | 13 | ## @package mlx.pirep
|
---|
| 14 | #
|
---|
| 15 | # The PIREP module.
|
---|
| 16 | #
|
---|
| 17 | # This module defines only one class, \ref PIREP. It is used to extract and
|
---|
| 18 | # store the information needed for a PIREP. The saved PIREPs are pickled
|
---|
| 19 | # instances of this class.
|
---|
[97] | 20 |
|
---|
| 21 | #------------------------------------------------------------------------------
|
---|
| 22 |
|
---|
| 23 | class PIREP(object):
|
---|
| 24 | """A pilot's report of a flight."""
|
---|
[743] | 25 | _flightTypes = { const.FLIGHTTYPE_SCHEDULED : "SCHEDULED",
|
---|
| 26 | const.FLIGHTTYPE_OLDTIMER : "OT",
|
---|
| 27 | const.FLIGHTTYPE_VIP : "VIP",
|
---|
| 28 | const.FLIGHTTYPE_CHARTER : "CHARTER" }
|
---|
| 29 |
|
---|
[97] | 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 + "]"
|
---|
[151] | 35 |
|
---|
| 36 | @staticmethod
|
---|
[743] | 37 | def formatTimestampForRPC(t):
|
---|
| 38 | """Format the given timestamp for RPC."""
|
---|
| 39 | return time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t))
|
---|
| 40 |
|
---|
| 41 | @staticmethod
|
---|
[829] | 42 | def parseTimestampFromRPC(s):
|
---|
| 43 | """Format the given timestamp for RPC."""
|
---|
| 44 | dt = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
|
---|
| 45 | return calendar.timegm(dt.utctimetuple())
|
---|
| 46 |
|
---|
| 47 | @staticmethod
|
---|
| 48 | def decodeFlightTypeText(s):
|
---|
| 49 | """Decode the given flight type text."""
|
---|
| 50 | for (flighType, text) in PIREP._flightTypes.iteritems():
|
---|
| 51 | if s==text:
|
---|
| 52 | return flighType
|
---|
| 53 | return const.FLIGHTYPE_SCHEDULED
|
---|
| 54 |
|
---|
| 55 | @staticmethod
|
---|
| 56 | def parseLogFromRPC(log):
|
---|
| 57 | """Parse the given log coming from the RPC."""
|
---|
| 58 | index = 0
|
---|
| 59 | entries = []
|
---|
| 60 |
|
---|
| 61 | inTimeStr = False
|
---|
| 62 | inEntry = False
|
---|
| 63 |
|
---|
| 64 | timestr = ""
|
---|
| 65 | entry = ""
|
---|
| 66 |
|
---|
| 67 | while index<len(log):
|
---|
| 68 | c = log[index]
|
---|
| 69 | index += 1
|
---|
| 70 |
|
---|
| 71 | if c==']':
|
---|
| 72 | if inEntry:
|
---|
| 73 | entries.append((timestr, entry))
|
---|
| 74 | timestr = ""
|
---|
| 75 | entry = ""
|
---|
| 76 |
|
---|
| 77 | inTimeStr = False
|
---|
| 78 | inEntry = False
|
---|
| 79 | elif not inTimeStr and not inEntry:
|
---|
| 80 | if c=='[':
|
---|
| 81 | if timestr:
|
---|
| 82 | inEntry = True
|
---|
| 83 | else:
|
---|
| 84 | inTimeStr = True
|
---|
| 85 | elif inTimeStr:
|
---|
| 86 | timestr += c
|
---|
| 87 | elif inEntry:
|
---|
| 88 | entry += c
|
---|
| 89 |
|
---|
| 90 | return entries
|
---|
| 91 |
|
---|
| 92 | @staticmethod
|
---|
[151] | 93 | def load(path):
|
---|
| 94 | """Load a PIREP from the given path.
|
---|
| 95 |
|
---|
| 96 | Returns the PIREP object, or None on error."""
|
---|
| 97 | try:
|
---|
| 98 | with open(path, "rb") as f:
|
---|
[303] | 99 | pirep = pickle.load(f)
|
---|
| 100 | if "numCrew" not in dir(pirep):
|
---|
| 101 | pirep.numCrew = pirep.bookedFlight.numCrew
|
---|
| 102 | if "numPassengers" not in dir(pirep):
|
---|
| 103 | pirep.numPassengers = pirep.bookedFlight.numPassengers
|
---|
| 104 | if "bagWeight" not in dir(pirep):
|
---|
| 105 | pirep.bagWeight = pirep.bookedFlight.bagWeight
|
---|
| 106 | if "mailWeight" not in dir(pirep):
|
---|
| 107 | pirep.mailWeight = pirep.bookedFlight.mailWeight
|
---|
| 108 | return pirep
|
---|
[151] | 109 | except Exception, e:
|
---|
[401] | 110 | print "Failed loading PIREP from %s: %s" % (path,
|
---|
| 111 | utf2unicode(str(e)))
|
---|
[151] | 112 | return None
|
---|
[345] | 113 |
|
---|
[262] | 114 | def __init__(self, flight):
|
---|
| 115 | """Initialize the PIREP from the given flight."""
|
---|
[829] | 116 | if flight is None:
|
---|
| 117 | return
|
---|
| 118 |
|
---|
[262] | 119 | self.bookedFlight = flight.bookedFlight
|
---|
[303] | 120 |
|
---|
| 121 | self.numCrew = flight.numCrew
|
---|
| 122 | self.numPassengers = flight.numPassengers
|
---|
| 123 | self.bagWeight = flight.bagWeight
|
---|
[262] | 124 | self.cargoWeight = flight.cargoWeight
|
---|
[303] | 125 | self.mailWeight = flight.mailWeight
|
---|
[345] | 126 |
|
---|
[262] | 127 | self.filedCruiseAltitude = flight.filedCruiseAltitude
|
---|
| 128 | self.cruiseAltitude = flight.cruiseAltitude
|
---|
| 129 | self.route = flight.route
|
---|
[97] | 130 |
|
---|
[262] | 131 | self.departureMETAR = flight.departureMETAR.upper()
|
---|
| 132 | self.arrivalMETAR = flight.arrivalMETAR.upper()
|
---|
[97] | 133 |
|
---|
[262] | 134 | self.departureRunway = flight.departureRunway.upper()
|
---|
| 135 | self.sid = flight.sid.upper()
|
---|
[97] | 136 |
|
---|
[262] | 137 | self.star = flight.star
|
---|
| 138 | self.transition = flight.transition
|
---|
| 139 | self.approachType = flight.approachType.upper()
|
---|
| 140 | self.arrivalRunway = flight.arrivalRunway.upper()
|
---|
[97] | 141 |
|
---|
[262] | 142 | self.flightType = flight.flightType
|
---|
| 143 | self.online = flight.online
|
---|
[97] | 144 |
|
---|
[262] | 145 | self.comments = flight.comments
|
---|
| 146 | self.flightDefects = flight.flightDefects
|
---|
| 147 | self.delayCodes = flight.delayCodes
|
---|
[345] | 148 |
|
---|
[97] | 149 | self.blockTimeStart = flight.blockTimeStart
|
---|
| 150 | self.flightTimeStart = flight.flightTimeStart
|
---|
| 151 | self.flightTimeEnd = flight.flightTimeEnd
|
---|
| 152 | self.blockTimeEnd = flight.blockTimeEnd
|
---|
| 153 | self.flownDistance = flight.flownDistance
|
---|
[102] | 154 | self.fuelUsed = flight.startFuel - flight.endFuel
|
---|
[97] | 155 |
|
---|
[262] | 156 | logger = flight.logger
|
---|
[97] | 157 | self.rating = logger.getRating()
|
---|
| 158 | self.logLines = logger.lines
|
---|
| 159 | self.faultLineIndexes = logger.faultLineIndexes
|
---|
[345] | 160 |
|
---|
[829] | 161 | def setupFromPIREPData(self, pirepData, bookedFlight):
|
---|
| 162 |
|
---|
| 163 | self.bookedFlight = bookedFlight
|
---|
| 164 |
|
---|
| 165 | self.numCrew = int(pirepData["numCrew"])
|
---|
| 166 | self.numPassengers = int(pirepData["numPassengers"])
|
---|
| 167 | self.bagWeight = int(pirepData["bagWeight"])
|
---|
| 168 | self.cargoWeight = int(pirepData["cargoWeight"])
|
---|
| 169 | self.mailWeight = int(pirepData["mailWeight"])
|
---|
| 170 |
|
---|
| 171 | self.filedCruiseAltitude = int(pirepData["filedCruiseLevel"][2:])*100
|
---|
[854] | 172 | cruiseLevel = pirepData["cruiseLevel"].strip()
|
---|
| 173 | if cruiseLevel:
|
---|
| 174 | if cruiseLevel.startswith("FL"):
|
---|
| 175 | cruiseLevel = cruiseLevel[2:]
|
---|
[829] | 176 | if cruiseLevel:
|
---|
| 177 | self.cruiseAltitude = int(cruiseLevel[2:])*100
|
---|
| 178 | else:
|
---|
| 179 | self.cruiseAltitude = self.filedCruiseAltitude
|
---|
| 180 | self.route = pirepData["route"]
|
---|
| 181 |
|
---|
| 182 | self.departureMETAR = pirepData["departureMETAR"]
|
---|
| 183 | self.arrivalMETAR = pirepData["arrivalMETAR"]
|
---|
| 184 |
|
---|
| 185 | self.departureRunway = pirepData["departureRunway"]
|
---|
| 186 | self.sid = pirepData["sid"]
|
---|
| 187 |
|
---|
| 188 | star = pirepData["star"].split(",")
|
---|
| 189 | self.star = star[0]
|
---|
| 190 | self.star.strip()
|
---|
| 191 |
|
---|
| 192 | if len(star)>1:
|
---|
| 193 | self.transition = star[1]
|
---|
| 194 | self.transition.strip()
|
---|
| 195 | else:
|
---|
| 196 | self.transition = ""
|
---|
| 197 | self.approachType = pirepData["approachType"]
|
---|
| 198 | self.arrivalRunway = pirepData["arrivalRunway"]
|
---|
| 199 |
|
---|
| 200 | self.flightType = PIREP.decodeFlightTypeText(pirepData["flightType"])
|
---|
| 201 | self.online = int(pirepData["online"])!=0
|
---|
| 202 |
|
---|
| 203 | self.comments = pirepData["comments"]
|
---|
| 204 | self.flightDefects = pirepData["flightDefects"]
|
---|
| 205 | self.delayCodes = pirepData["timeComment"]
|
---|
| 206 | if self.delayCodes=="UTC":
|
---|
[838] | 207 | self.delayCodes = []
|
---|
| 208 | else:
|
---|
| 209 | self.delayCodes = self.delayCodes.split(", ")
|
---|
[829] | 210 |
|
---|
| 211 | flightDate = pirepData["flightDate"] + " "
|
---|
| 212 |
|
---|
| 213 | self.blockTimeStart = \
|
---|
| 214 | PIREP.parseTimestampFromRPC(flightDate + pirepData["blockTimeStart"])
|
---|
| 215 | self.flightTimeStart = \
|
---|
| 216 | PIREP.parseTimestampFromRPC(flightDate + pirepData["flightTimeStart"])
|
---|
| 217 | self.flightTimeEnd = \
|
---|
| 218 | PIREP.parseTimestampFromRPC(flightDate + pirepData["flightTimeEnd"])
|
---|
| 219 | self.blockTimeEnd = \
|
---|
| 220 | PIREP.parseTimestampFromRPC(flightDate + pirepData["blockTimeEnd"])
|
---|
| 221 | self.flownDistance = float(pirepData["flownDistance"])
|
---|
| 222 | self.fuelUsed = float(pirepData["fuelUsed"])
|
---|
| 223 |
|
---|
| 224 | # logger = flight.logger
|
---|
| 225 | self.rating = float(pirepData["rating"])
|
---|
| 226 |
|
---|
| 227 | log = pirepData["log"]
|
---|
| 228 |
|
---|
| 229 | self.logLines = PIREP.parseLogFromRPC(log)[1:]
|
---|
| 230 | if self.logLines and \
|
---|
| 231 | (self.logLines[0][0]=="LOGGER NG LOG" or
|
---|
| 232 | self.logLines[0][0]=="MAVA LOGGER X"):
|
---|
| 233 | self.logLines = self.logLines[1:]
|
---|
| 234 | numLogLines = len(self.logLines)
|
---|
| 235 |
|
---|
| 236 | lastFaultLineIndex = 0
|
---|
| 237 | self.faultLineIndexes = []
|
---|
| 238 | for ratingText in pirepData["ratingText"].splitlines()[:-1]:
|
---|
| 239 | faultLines = PIREP.parseLogFromRPC(ratingText)
|
---|
| 240 | for (timeStr, entry) in faultLines:
|
---|
| 241 | for i in range(lastFaultLineIndex, numLogLines-1):
|
---|
| 242 | if timeStr>=self.logLines[i][0] and \
|
---|
| 243 | timeStr<self.logLines[i+1][0]:
|
---|
| 244 | self.logLines = self.logLines[:i+1] + \
|
---|
| 245 | [(timeStr, entry)] + self.logLines[i+1:]
|
---|
| 246 | self.faultLineIndexes.append(i+1)
|
---|
| 247 | lastFaultLineIndex = i+1
|
---|
| 248 | numLogLines += 1
|
---|
| 249 | break
|
---|
| 250 |
|
---|
[743] | 251 | @property
|
---|
[778] | 252 | def flightDateText(self):
|
---|
| 253 | """Get the text version of the booked flight's departure time."""
|
---|
| 254 | return self.bookedFlight.departureTime.strftime("%Y-%m-%d")
|
---|
| 255 |
|
---|
| 256 | @property
|
---|
[743] | 257 | def flightTypeText(self):
|
---|
| 258 | """Get the text representation of the flight type."""
|
---|
| 259 | return PIREP._flightTypes[self.flightType]
|
---|
| 260 |
|
---|
| 261 | @property
|
---|
| 262 | def blockTimeStartText(self):
|
---|
| 263 | """Get the beginning of the block time in string format."""
|
---|
| 264 | return PIREP.formatTimestampForRPC(self.blockTimeStart)
|
---|
| 265 |
|
---|
| 266 | @property
|
---|
| 267 | def flightTimeStartText(self):
|
---|
| 268 | """Get the beginning of the flight time in string format."""
|
---|
| 269 | return PIREP.formatTimestampForRPC(self.flightTimeStart)
|
---|
| 270 |
|
---|
| 271 | @property
|
---|
| 272 | def flightTimeEndText(self):
|
---|
| 273 | """Get the end of the flight time in string format."""
|
---|
| 274 | return PIREP.formatTimestampForRPC(self.flightTimeEnd)
|
---|
| 275 |
|
---|
| 276 | @property
|
---|
| 277 | def blockTimeEndText(self):
|
---|
| 278 | """Get the end of the block time in string format."""
|
---|
| 279 | return PIREP.formatTimestampForRPC(self.blockTimeEnd)
|
---|
| 280 |
|
---|
[97] | 281 | def getACARSText(self):
|
---|
| 282 | """Get the ACARS text.
|
---|
| 283 |
|
---|
| 284 | This is a specially formatted version of the log without the faults."""
|
---|
| 285 | text = "[MAVA LOGGER X LOG]-[%s]" % (const.VERSION,)
|
---|
| 286 | for index in range(0, len(self.logLines)):
|
---|
| 287 | if index not in self.faultLineIndexes:
|
---|
| 288 | (timeStr, line) = self.logLines[index]
|
---|
| 289 | if timeStr is not None:
|
---|
| 290 | text += PIREP._formatLine(timeStr, line)
|
---|
| 291 | return text
|
---|
| 292 |
|
---|
| 293 | def getRatingText(self):
|
---|
| 294 | """Get the rating text.
|
---|
| 295 |
|
---|
| 296 | This is a specially formatted version of the lines containing the
|
---|
| 297 | faults."""
|
---|
| 298 | text = ""
|
---|
| 299 | for index in self.faultLineIndexes:
|
---|
| 300 | (timeStr, line) = self.logLines[index]
|
---|
| 301 | if timeStr is not None:
|
---|
| 302 | text += PIREP._formatLine(timeStr, line)
|
---|
[345] | 303 | text += "\n"
|
---|
[97] | 304 |
|
---|
| 305 | text += "\n[Flight Rating: %.1f]" % (max(0.0, self.rating),)
|
---|
| 306 |
|
---|
| 307 | return text
|
---|
[345] | 308 |
|
---|
[97] | 309 | def getTimeComment(self):
|
---|
| 310 | """Get the time comment.
|
---|
| 311 |
|
---|
| 312 | This is basically a collection of the delay codes, if any."""
|
---|
[99] | 313 | if not self.delayCodes:
|
---|
| 314 | return "UTC"
|
---|
| 315 | else:
|
---|
| 316 | s = ""
|
---|
| 317 | for code in self.delayCodes:
|
---|
| 318 | if s: s += ", "
|
---|
[437] | 319 | s += code
|
---|
[99] | 320 | return s
|
---|
[97] | 321 |
|
---|
| 322 | def getSTAR(self):
|
---|
| 323 | """Get the STAR and/or the transition."""
|
---|
| 324 | star = self.star if self.star is not None else ""
|
---|
| 325 | if self.transition is not None:
|
---|
| 326 | if star: star += ", "
|
---|
| 327 | star += self.transition
|
---|
| 328 | return star.upper()
|
---|
[151] | 329 |
|
---|
| 330 | def save(self, path):
|
---|
| 331 | """Save the PIREP to the given file.
|
---|
| 332 |
|
---|
| 333 | Returns whether the saving has succeeded."""
|
---|
| 334 | try:
|
---|
| 335 | with open(path, "wb") as f:
|
---|
| 336 | pickle.dump(self, f)
|
---|
[393] | 337 | return None
|
---|
[151] | 338 | except Exception, e:
|
---|
[401] | 339 | error = utf2unicode(str(e))
|
---|
| 340 | print u"Failed saving PIREP to %s: %s" % (path, error)
|
---|
[393] | 341 | return error
|
---|
[743] | 342 |
|
---|
| 343 | def _serialize(self):
|
---|
| 344 | """Serialize the PIREP for JSON-RPC."""
|
---|
| 345 | attrs = {}
|
---|
| 346 | attrs["log"] = self.getACARSText()
|
---|
[778] | 347 | attrs["flightDate"] = self.flightDateText
|
---|
| 348 | attrs["callsign"] = self.bookedFlight.callsign
|
---|
| 349 | attrs["departureICAO"] = self.bookedFlight.departureICAO
|
---|
| 350 | attrs["arrivalICAO"] = self.bookedFlight.arrivalICAO
|
---|
[743] | 351 | attrs["numPassengers"] = self.numPassengers
|
---|
| 352 | attrs["numCrew"] = self.numCrew
|
---|
| 353 | attrs["cargoWeight"] = self.cargoWeight
|
---|
| 354 | attrs["bagWeight"] = self.bagWeight
|
---|
| 355 | attrs["mailWeight"] = self.mailWeight
|
---|
| 356 | attrs["flightType"] = self.flightTypeText
|
---|
| 357 | attrs["online"] = 1 if self.online else 0
|
---|
| 358 | attrs["blockTimeStart"] = self.blockTimeStartText
|
---|
| 359 | attrs["blockTimeEnd"] = self.blockTimeEndText
|
---|
| 360 | attrs["flightTimeStart"] = self.flightTimeStartText
|
---|
| 361 | attrs["flightTimeEnd"] = self.flightTimeEndText
|
---|
| 362 | attrs["timeComment"] = self.getTimeComment()
|
---|
| 363 | attrs["fuelUsed"] = self.fuelUsed
|
---|
| 364 | attrs["departureRunway"] = self.departureRunway
|
---|
| 365 | attrs["arrivalRunway"] = self.arrivalRunway
|
---|
| 366 | attrs["departureMETAR"] = self.departureMETAR
|
---|
| 367 | attrs["arrivalMETAR"] = self.arrivalMETAR
|
---|
| 368 | attrs["filedCruiseLevel"] = self.filedCruiseAltitude / 100.0
|
---|
| 369 | attrs["cruiseLevel"] = self.cruiseAltitude / 100.0
|
---|
| 370 | attrs["sid"] = self.sid
|
---|
| 371 | attrs["route"] = self.route
|
---|
| 372 | attrs["star"] = self.star
|
---|
| 373 | attrs["approachType"] = self.approachType
|
---|
| 374 | attrs["comments"] = self.comments
|
---|
| 375 | attrs["flightDefects"] = self.flightDefects
|
---|
| 376 | attrs["ratingText"] = self.getRatingText()
|
---|
| 377 | attrs["rating"] = max(0.0, self.rating)
|
---|
[839] | 378 | attrs["flownDistance"] = "%.2f" % (self.flownDistance,)
|
---|
[743] | 379 | # FIXME: it should be stored in the PIREP when it is sent later
|
---|
[778] | 380 | attrs["performDate"] = datetime.date.today().strftime("%Y-%m-%d")
|
---|
[743] | 381 |
|
---|
| 382 | return ([], attrs)
|
---|