Skip to content

Commit

Permalink
Added test for missing data in get_bufr
Browse files Browse the repository at this point in the history
- Ensure get_bufr_variables raises AttributeError when station dimensions are missing
  • Loading branch information
ladsmund committed Jul 3, 2024
1 parent b3a7c4f commit 1558759
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 50 deletions.
41 changes: 20 additions & 21 deletions src/pypromice/postprocess/get_bufr.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,13 @@ def get_bufr_variables(
station_configuration: StationConfiguration,
) -> BUFRVariables:
"""
Helper function for converting our variables to the variables needed for bufr export.
Helper function for converting our variables to the variables needed for bufr export.
Raises AttributeError if station_configuration dont have the minimum dimension fields since they are required to determine barometer heights.
* height_of_gps_from_station_ground
* barometer_from_gps
Parameters
----------
Expand All @@ -544,41 +550,34 @@ def get_bufr_variables(
BUFRVariables used by bufr_utilities
"""

if station_configuration.height_of_gps_from_station_ground is None:
heightOfStationGroundAboveMeanSeaLevel = np.nan
else:
heightOfStationGroundAboveMeanSeaLevel = (
data["gps_alt_fit"]
- station_configuration.height_of_gps_from_station_ground
raise AttributeError(
"height_of_gps_from_station_ground is required for BUFR export"
)
if station_configuration.barometer_from_gps is None:
raise AttributeError("barometer_from_gps is required for BUFR export")

heightOfStationGroundAboveMeanSeaLevel = (
data["gps_alt_fit"] - station_configuration.height_of_gps_from_station_ground
)

heightOfBarometerAboveMeanSeaLevel = (
data["gps_alt_fit"] + station_configuration.barometer_from_gps
)

# TODO: Consider to use dynamic height of sensor above ground based in sonic ranger
# heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformTempRH = (
# data["z_boom_u_smooth"]+ station_configuration.temperature_from_sonic_ranger
# )
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformTempRH = (
station_configuration.temperature_from_station_ground
)
if heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformTempRH is None:
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformTempRH = np.nan

# TODO: Consider to use dynamic height of sensor above ground based in sonic ranger
# heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformWSPD = (
# data["z_boom_u_smooth"] + station_configuration.anemometer_from_sonic_ranger
# )
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformWSPD = (
station_configuration.anemometer_from_station_ground
)
if heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformWSPD is None:
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformWSPD = np.nan

if station_configuration.barometer_from_gps is None:
heightOfBarometerAboveMeanSeaLevel = np.nan
else:
heightOfBarometerAboveMeanSeaLevel = (
data["gps_alt_fit"] + station_configuration.barometer_from_gps
)

output_row = BUFRVariables(
wmo_id=station_configuration.wmo_id,
station_type=station_configuration.station_type,
Expand Down
39 changes: 10 additions & 29 deletions tests/unit/bufr_export/test_get_bufr.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ def test_bufr_variables_promice_v3(self):
heightOfBarometerAboveMeanSeaLevel=2126,
)

def test_none_values_in_config(self):
def test_fails_on_missing_dimension_values(self):
"""
Test that get_bufr_variables raises an AttributeError if the data is missing
"""
timestamp = datetime.datetime.now()
data = pd.Series(
data=dict(
Expand All @@ -220,36 +223,14 @@ def test_none_values_in_config(self):
stid="A_STID",
station_type="land",
wmo_id="4201",
barometer_from_gps=0.2,
anemometer_from_sonic_ranger=0.1,
temperature_from_sonic_ranger=1.3,
height_of_gps_from_station_ground=2.1,
)

output = get_bufr_variables(
data,
station_configuration=station_config,
export_bufr=True,
)

self.assertEqual(
BUFRVariables(
wmo_id=station_config.wmo_id,
station_type=station_config.station_type,
timestamp=timestamp,
relativeHumidity=1.0,
airTemperature=252.2, # Converted to kelvin
pressure=199300.0,
windDirection=32.0,
windSpeed=5.3,
latitude=66.0,
longitude=-46.0,
heightOfStationGroundAboveMeanSeaLevel=1091.9,
heightOfBarometerAboveMeanSeaLevel=1094.2,
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformTempRH=3.4,
heightOfSensorAboveLocalGroundOrDeckOfMarinePlatformWSPD=2.2,
),
output,
)
with self.assertRaises(AttributeError) as context:
get_bufr_variables(
data,
station_configuration=station_config,
)

@mock.patch("pypromice.postprocess.get_bufr.write_bufr_message")
def _test_bufr_variables(
Expand Down

0 comments on commit 1558759

Please sign in to comment.