| 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # This is a minimal sample script showing how the individual data |
| 4 | # are accessed from the decoded report. To produce the standard text |
| 5 | # summary of a report, use the string() method of the Metar object. |
| 6 | # |
| 7 | # The parsed data are stored as attributes of a Metar object. |
| 8 | # Individual attributes are either strings. instances of one of the |
| 9 | # metar.Datatypes classes, or lists of tuples of these scalars. |
| 10 | # Here's a summary, adapted from the comments in the Metar.Metar.__init__() |
| 11 | # method: |
| 12 | # |
| 13 | # Attribute Comments [data type] |
| 14 | # ---------------- ------------------------------------------ |
| 15 | # code original METAR code [string] |
| 16 | # type METAR (routine) or SPECI (special) [string] |
| 17 | # mod AUTO (automatic) or COR (corrected) [string] |
| 18 | # station_id 4-character ICAO station code [string] |
| 19 | # time observation time [datetime] |
| 20 | # cycle observation cycle (0-23) [int] |
| 21 | # wind_dir wind direction [direction] |
| 22 | # wind_speed wind speed [speed] |
| 23 | # wind_gust wind gust speed [speed] |
| 24 | # wind_dir_from beginning of range for win dir [direction] |
| 25 | # wind_dir_to end of range for wind dir [direction] |
| 26 | # vis visibility [distance] |
| 27 | # vis_dir visibility direction [direction] |
| 28 | # max_vis visibility [distance] |
| 29 | # max_vis_dir visibility direction [direction] |
| 30 | # temp temperature (C) [temperature] |
| 31 | # dewpt dew point (C) [temperature] |
| 32 | # press barometric pressure [pressure] |
| 33 | # runway runway visibility [list of tuples...] |
| 34 | # name [string] |
| 35 | # low [distance] |
| 36 | # high [distance] |
| 37 | # weather present weather [list of tuples...] |
| 38 | # intensity [string] |
| 39 | # description [string] |
| 40 | # precipitation [string] |
| 41 | # obscuration [string] |
| 42 | # other [string] |
| 43 | # recent recent weather [list of tuples...] |
| 44 | # sky sky conditions [list of tuples...] |
| 45 | # cover [string] |
| 46 | # height [distance] |
| 47 | # cloud [string] |
| 48 | # windshear runways w/ wind shear [list of strings] |
| 49 | # |
| 50 | # press_sea_level sea-level pressure [pressure] |
| 51 | # wind_speed_peak peak wind speed in last hour [speed] |
| 52 | # wind_dir_peak direction of peak wind speed in last hour [direction] |
| 53 | # max_temp_6hr max temp in last 6 hours [temperature] |
| 54 | # min_temp_6hr min temp in last 6 hours [temperature] |
| 55 | # max_temp_24hr max temp in last 24 hours [temperature] |
| 56 | # min_temp_24hr min temp in last 24 hours [temperature] |
| 57 | # precip_1hr precipitation over the last hour [precipitation] |
| 58 | # precip_3hr precipitation over the last 3 hours [precipitation] |
| 59 | # precip_6hr precipitation over the last 6 hours [precipitation] |
| 60 | # precip_24hr precipitation over the last 24 hours [precipitation] |
| 61 | # |
| 62 | # _remarks remarks [list of strings] |
| 63 | # _unparsed_groups unparsed groups [list of strings] |
| 64 | # _unparsed_remarks unparsed remarks [list of strings] |
| 65 | |
| 66 | # |
| 67 | # The metar.Datatypes classes (temperature, pressure, precipitation, |
| 68 | # speed, direction) describe an observation and its units. They provide |
| 69 | # value() and string() methods to that return numerical and string |
| 70 | # representations of the data in any of a number of supported units. |
| 71 | # |
| 72 | # (You're going to have to study the source code for more details, |
| 73 | # like the available methods and supported unit conversions for the |
| 74 | # metar.Datatypes objects, etc..) |
| 75 | |
| 76 | # In particular, look at the Metar.string() |
| 77 | # method, and the functions it calls. |
| 78 | # |
| 79 | # Feb 4, 2005 |
| 80 | # Tom Pollard |
| 81 | # |
| 82 | from __future__ import print_function |
| 83 | |
| 84 | from metar import Metar |
| 85 | |
| 86 | # A sample METAR report |
| 87 | code = ( |
| 88 | "METAR KEWR 111851Z VRB03G19KT 2SM R04R/3000VP6000FT TSRA BR FEW015 " |
| 89 | "BKN040CB BKN065 OVC200 22/22 A2987 RMK AO2 PK WND 29028/1817 WSHFT " |
| 90 | "1812 TSB05RAB22 SLP114 FRQ LTGICCCCG TS OHD AND NW-N-E MOV NE P0013 " |
| 91 | "T02270215" |
| 92 | ) |
| 93 | |
| 94 | print("-----------------------------------------------------------------------") |
| 95 | print("METAR: ", code) |
| 96 | print("-----------------------------------------------------------------------") |
| 97 | |
| 98 | # Initialize a Metar object with the coded report |
| 99 | obs = Metar.Metar(code) |
| 100 | |
| 101 | # Print the individual data |
| 102 | |
| 103 | # The 'station_id' attribute is a string. |
| 104 | print("station: %s" % obs.station_id) |
| 105 | |
| 106 | if obs.type: |
| 107 | print("type: %s" % obs.report_type()) |
| 108 | |
| 109 | # The 'time' attribute is a datetime object |
| 110 | if obs.time: |
| 111 | print("time: %s" % obs.time.ctime()) |
| 112 | |
| 113 | # The 'temp' and 'dewpt' attributes are temperature objects |
| 114 | if obs.temp: |
| 115 | print("temperature: %s" % obs.temp.string("C")) |
| 116 | |
| 117 | if obs.dewpt: |
| 118 | print("dew point: %s" % obs.dewpt.string("C")) |
| 119 | |
| 120 | # The wind() method returns a string describing wind observations |
| 121 | # which may include speed, direction, variability and gusts. |
| 122 | if obs.wind_speed: |
| 123 | print("wind: %s" % obs.wind()) |
| 124 | |
| 125 | # The peak_wind() method returns a string describing the peak wind |
| 126 | # speed and direction. |
| 127 | if obs.wind_speed_peak: |
| 128 | print("wind: %s" % obs.peak_wind()) |
| 129 | |
| 130 | # The visibility() method summarizes the visibility observation. |
| 131 | if obs.vis: |
| 132 | print("visibility: %s" % obs.visibility()) |
| 133 | |
| 134 | # The runway_visual_range() method summarizes the runway visibility |
| 135 | # observations. |
| 136 | if obs.runway: |
| 137 | print("visual range: %s" % obs.runway_visual_range()) |
| 138 | |
| 139 | # The 'press' attribute is a pressure object. |
| 140 | if obs.press: |
| 141 | print("pressure: %s" % obs.press.string("mb")) |
| 142 | |
| 143 | # The 'precip_1hr' attribute is a precipitation object. |
| 144 | if obs.precip_1hr: |
| 145 | print("precipitation: %s" % obs.precip_1hr.string("in")) |
| 146 | |
| 147 | # The present_weather() method summarizes the weather description (rain, etc.) |
| 148 | print("weather: %s" % obs.present_weather()) |
| 149 | |
| 150 | # The sky_conditions() method summarizes the cloud-cover observations. |
| 151 | print("sky: %s" % obs.sky_conditions("\n ")) |
| 152 | |
| 153 | # The remarks() method describes the remark groups that were parsed, but |
| 154 | # are not available directly as Metar attributes. The precipitation, |
| 155 | # min/max temperature and peak wind remarks, for instance, are stored as |
| 156 | # attributes and won't be listed here. |
| 157 | if obs._remarks: |
| 158 | print("remarks:") |
| 159 | print("- " + obs.remarks("\n- ")) |
| 160 | |
| 161 | print("-----------------------------------------------------------------------\n") |