diff --git a/src/pypromice/postprocess/get_bufr.py b/src/pypromice/postprocess/get_bufr.py index 49669cc0..e199b597 100644 --- a/src/pypromice/postprocess/get_bufr.py +++ b/src/pypromice/postprocess/get_bufr.py @@ -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 ---------- @@ -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, diff --git a/tests/unit/bufr_export/test_get_bufr.py b/tests/unit/bufr_export/test_get_bufr.py index 96cc9eb2..4683561f 100644 --- a/tests/unit/bufr_export/test_get_bufr.py +++ b/tests/unit/bufr_export/test_get_bufr.py @@ -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( @@ -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(