Skip to content

Commit

Permalink
Improved checking for decoded values in pymetdecoder output
Browse files Browse the repository at this point in the history
´if XXX in decoded:´ => ´if decoded.get(XXX) is not None:´
  • Loading branch information
david-i-berry committed Aug 30, 2023
1 parent 0060bd3 commit fe267dd
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions synop2bufr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
output['year'] = year
output['month'] = month

if 'obs_time' in decoded:
if decoded.get('obs_time') is not None:
try:
output['day'] = decoded['obs_time']['day']['value']
except Exception:
Expand All @@ -134,7 +134,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
output['hour'] = None

# The minute will be 00 unless specified by exact observation time
if 'exact_obs_time' in decoded:
if decoded.get('exact_obs_time') is not None:
try:
output['minute'] = decoded['exact_obs_time']['minute']['value']
except Exception:
Expand All @@ -149,7 +149,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
output['minute'] = 0

# Translate wind instrument flag from the SYNOP code to the BUFR code
if 'wind_indicator' in decoded:
if decoded.get('wind_indicator') is not None:
try:
iw = decoded['wind_indicator']['value']

Expand Down Expand Up @@ -179,7 +179,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
else:
output['template'] = 307080

if 'station_id' in decoded:
if decoded.get('station_id') is not None:
try:
tsi = decoded['station_id']['value']
output['station_id'] = tsi
Expand All @@ -193,14 +193,14 @@ def parse_synop(message: str, year: int, month: int) -> dict:
# ! Removed precipitation indicator as it is redundant

# Get region of report
if 'region' in decoded:
if decoded.get('region') is not None:
try:
output['region'] = decoded['region']['value']
except Exception:
output['region'] = None

# We translate this station type flag from the SYNOP code to the BUFR code
if 'weather_indicator' in decoded:
if decoded.get('weather_indicator') is not None:
try:
ix = decoded['weather_indicator']['value']
if ix <= 3:
Expand Down Expand Up @@ -228,15 +228,15 @@ def parse_synop(message: str, year: int, month: int) -> dict:
output['lowest_cloud_base'] = None

# Visibility is already given in metres
if 'visibility' in decoded:
if decoded.get('visibility') is not None:
try:
output['visibility'] = decoded['visibility']['value']
except Exception:
output['visibility'] = None

# Cloud cover is given in oktas, which we convert to a percentage
# NOTE: By B/C10.4.4.1 this percentage is always rounded up
if 'cloud_cover' in decoded:
if decoded.get('cloud_cover') is not None:
try:
N_oktas = decoded['cloud_cover']['_code']
# If the cloud cover is 9 oktas, this means the sky was obscured
Expand Down Expand Up @@ -283,20 +283,20 @@ def parse_synop(message: str, year: int, month: int) -> dict:
output['wind_speed'] = None

# Temperatures are given in Celsius, convert to kelvin and round to 2 dp
if 'air_temperature' in decoded:
if decoded.get('air_temperature') is not None:
try:
output['air_temperature'] = round(decoded['air_temperature']['value'] + 273.15, 2) # noqa
except Exception:
output['air_temperature'] = None

if 'dewpoint_temperature' in decoded:
if decoded.get('dewpoint_temperature') is not None:
try:
output['dewpoint_temperature'] = round(decoded['dewpoint_temperature']['value'] + 273.15, 2) # noqa
except Exception:
output['dewpoint_temperature'] = None

# RH is already given in %
if 'relative_humidity' in decoded:
if decoded.get('relative_humidity') is not None:
try:
output['relative_humidity'] = decoded['relative_humidity']['value']
except Exception:
Expand Down Expand Up @@ -329,20 +329,20 @@ def parse_synop(message: str, year: int, month: int) -> dict:

# Pressure is given in hPa, which we convert to Pa. By B/C 1.3.1,
# pressure has precision in tens of Pa
if 'station_pressure' in decoded:
if decoded.get('station_pressure') is not None:
try:
output['station_pressure'] = round(decoded['station_pressure']['value'] * 100, -1) # noqa
except Exception:
output['station_pressure'] = None

# Similar to above. By B/C1.3.2, pressure has precision in tens of Pa
if 'sea_level_pressure' in decoded:
if decoded.get('sea_level_pressure') is not None:
try:
output['sea_level_pressure'] = round(decoded['sea_level_pressure']['value'] * 100, -1) # noqa
except Exception:
output['sea_level_pressure'] = None

if 'geopotential' in decoded:
if decoded.get('geopotential') is not None:
try:
output['isobaric_surface'] = round(decoded['geopotential']['surface']['value'] * 100, 1) # noqa
except Exception:
Expand All @@ -352,7 +352,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
except Exception:
output['geopotential_height'] = None

if 'pressure_tendency' in decoded:
if decoded.get('pressure_tendency') is not None:
# By B/C1.3.3, pressure has precision in tens of Pa
try:
output['3hr_pressure_change'] = round(decoded['pressure_tendency']['change']['value'] * 100, -1) # noqa
Expand All @@ -365,7 +365,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
output['pressure_tendency_characteristic'] = None

# Precipitation is given in mm, which is equal to kg/m^2 of rain
if 'precipitation_s1' in decoded:
if decoded.get('precipitation_s1') is not None:
# NOTE: When the precipitation measurement RRR has code 990, this
# represents a trace amount of rain
# (<0.01 inches), which pymetdecoder records as 0. I (RTB) agree with
Expand All @@ -382,13 +382,13 @@ def parse_synop(message: str, year: int, month: int) -> dict:

# The present and past weather SYNOP codes align with that of BUFR apart
# from missing values
if 'present_weather' in decoded:
if decoded.get('present_weather') is not None:
try:
output['present_weather'] = decoded['present_weather']['value']
except Exception:
output['present_weather'] = None

if 'past_weather' in decoded:
if decoded.get('past_weather') is not None:
try:
output['past_weather_1'] = decoded['past_weather']['past_weather_1']['value'] # noqa
except Exception:
Expand Down Expand Up @@ -417,7 +417,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:

# We translate these cloud type flags from the SYNOP codes to the
# BUFR codes
if 'cloud_types' in decoded:
if decoded.get('cloud_types') is not None:
try:
Cl = decoded['cloud_types']['low_cloud_type']['value'] + 30
except Exception:
Expand Down Expand Up @@ -598,7 +598,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
# This regional difference is as follows:
# It is either omitted, or it takes form 3EsnTgTg, where
# Tg is the ground temperature
if 'ground_state' in decoded:
if decoded.get('ground_state') is not None:
# get value
if decoded['ground_state']['state'] is not None:
try:
Expand All @@ -617,7 +617,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:

# Group 4 4E'sss - gives state of the ground with snow, and the snow
# depth (not regional like group 3 is)
if 'ground_state_snow' in decoded:
if decoded.get('ground_state_snow') is not None:
if decoded['ground_state_snow']['state'] is not None:
# We translate the snow depth flags from the SYNOP codes to the
# BUFR codes
Expand Down Expand Up @@ -648,7 +648,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
# supplementary groups for radiation measurements

# Evaporation 5EEEiE
if 'evapotranspiration' in decoded:
if decoded.get('evapotranspiration') is not None:

# Evapotranspiration is given in mm, which is equal to kg/m^2 for rain
try:
Expand All @@ -666,7 +666,7 @@ def parse_synop(message: str, year: int, month: int) -> dict:
output['evaporation_instrument'] = None

# Temperature change 54g0sndT
if 'temperature_change' in decoded:
if decoded.get('temperature_change') is not None:

if decoded['temperature_change']['change'] is not None:
try:
Expand Down Expand Up @@ -704,7 +704,7 @@ def to_bearing(direction):
if direction == 8:
return 0

if 'cloud_drift_direction' in decoded:
if decoded.get('cloud_drift_direction') is not None:
if decoded['cloud_drift_direction']['low'] is not None:
try:
low_dir = decoded['cloud_drift_direction']['low']['_code']
Expand Down Expand Up @@ -741,7 +741,7 @@ def to_bearing(direction):
output['high_cloud_drift_direction'] = None

# Direction and elevation angle of the clouds 57CDaeC
if 'cloud_elevation' in decoded:
if decoded.get('cloud_elevation') is not None:
if decoded['cloud_elevation']['genus'] is not None:
try:
output['e_cloud_genus'] = decoded['cloud_elevation']['genus']['_code'] # noqa
Expand Down Expand Up @@ -774,7 +774,7 @@ def to_bearing(direction):

# Positive 58p24p24p24 or negative 59p24p24p24 changes in surface pressure
# over 24hrs
if 'pressure_change' in decoded:
if decoded.get('pressure_change') is not None:
try:
output['24hr_pressure_change'] = round(decoded['pressure_change']['value']*100, -1) # noqa
except Exception:
Expand All @@ -794,7 +794,7 @@ def to_bearing(direction):
# In either case, B/C1.12.2 requires that all radiation measurements
# are given in J/m^2. We convert this here.

if 'radiation' in decoded:
if decoded.get('radiation') is not None:
rad_dict = decoded['radiation']
# Create a function to do the appropriate conversion depending
# on time period
Expand Down Expand Up @@ -943,7 +943,7 @@ def rad_convert(rad, time):
# Group 6 6RRRtR - this is the same group as that in section 1, but over
# a different time period tR
# (which is not a multiple of 6 hours as it is in section 1)
if 'precipitation_s3' in decoded:
if decoded.get('precipitation_s3') is not None:
# In SYNOP it is given in mm, and in BUFR it is required to be
# in kg/m^2 (1mm = 1kg/m^2 for water)
try:
Expand All @@ -959,7 +959,7 @@ def rad_convert(rad, time):

# Group 7 7R24R24R24R24 - this group is the same as group 6, but
# over a 24 hour time period
if 'precipitation_24h' in decoded:
if decoded.get('precipitation_24h') is not None:
# In SYNOP it is given in mm, and in BUFR it is required to be
# in kg/m^2 (1mm = 1kg/m^2 for water)
try:
Expand All @@ -974,7 +974,7 @@ def rad_convert(rad, time):
# Create number of s3 group 8 clouds variable, in case there is no group 8
num_s3_clouds = 0

if 'cloud_layer' in decoded:
if decoded.get('cloud_layer') is not None:

# Name the array of 8NsChshs groups
genus_array = decoded['cloud_layer']
Expand Down Expand Up @@ -1076,7 +1076,7 @@ def rad_convert(rad, time):
# Create number of s4 clouds variable, in case there are no s4 groups
num_s4_clouds = 0

if 'section4' in decoded:
if decoded.get('section4') is not None:

# Name the array of section 4 items
genus_array = decoded['section4']
Expand Down

0 comments on commit fe267dd

Please sign in to comment.