Skip to content

Commit e7e02f7

Browse files
Align temperature processing in Cosmo and Icon factories to use Kelvin units, adding null-checks to prevent FactoryException for missing temperature values. Update tests to reflect changes in exception handling.
1 parent c8b20c6 commit e7e02f7

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
- Fixed tests of `TimeBasedValue` [#1469](https://github.com/ie3-institute/PowerSystemDataModel/issues/1469)
2626
- Requiring `thermalBus` field in house and storage input [#1509](https://github.com/ie3-institute/PowerSystemDataModel/issues/1509)
2727
- Fixed handling of erroneous field values in `EntitySource.enrichWithDefault()` [#1511](https://github.com/ie3-institute/PowerSystemDataModel/issues/1511)
28+
- Aligned temperature processing in Cosmo and Icon factories to kelvin and added checks to prevent NullPointerExceptions for missing values. [#1521](https://github.com/ie3-institute/PowerSystemDataModel/issues/1521)
2829

2930
### Changed
3031
- Updated CI-Pipeline to run task `Deploy` and `Staging` only for `Main` [#1403](https://github.com/ie3-institute/PowerSystemDataModel/issues/1403)

src/main/java/edu/ie3/datamodel/io/factory/timeseries/CosmoTimeBasedWeatherValueFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package edu.ie3.datamodel.io.factory.timeseries;
77

8+
import edu.ie3.datamodel.exceptions.FactoryException;
89
import edu.ie3.datamodel.models.StandardUnits;
910
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
1011
import edu.ie3.datamodel.models.value.WeatherValue;
@@ -75,7 +76,12 @@ protected TimeBasedValue<WeatherValue> buildModel(TimeBasedWeatherValueData data
7576
ComparableQuantity<Irradiance> diffuseIrradiance =
7677
data.getQuantity(DIFFUSE_IRRADIANCE, PowerSystemUnits.WATT_PER_SQUAREMETRE);
7778
ComparableQuantity<Temperature> temperature =
78-
data.getQuantity(TEMPERATURE, Units.KELVIN).to(StandardUnits.TEMPERATURE);
79+
data.getQuantityOptional(TEMPERATURE, Units.KELVIN)
80+
.map(quantity -> quantity.to(StandardUnits.TEMPERATURE))
81+
.orElseThrow(
82+
() ->
83+
new FactoryException(
84+
"The field \"" + TEMPERATURE + "\" is missing but required."));
7985
ComparableQuantity<Angle> windDirection =
8086
data.getQuantity(WIND_DIRECTION, StandardUnits.WIND_DIRECTION);
8187
ComparableQuantity<Speed> windVelocity =

src/main/java/edu/ie3/datamodel/io/factory/timeseries/IconTimeBasedWeatherValueFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package edu.ie3.datamodel.io.factory.timeseries;
77

8+
import edu.ie3.datamodel.exceptions.FactoryException;
89
import edu.ie3.datamodel.models.StandardUnits;
910
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
1011
import edu.ie3.datamodel.models.value.WeatherValue;
@@ -88,7 +89,12 @@ protected TimeBasedValue<WeatherValue> buildModel(TimeBasedWeatherValueData data
8889
ComparableQuantity<Irradiance> diffuseIrradiance =
8990
data.getQuantity(DIFFUSE_IRRADIANCE, PowerSystemUnits.WATT_PER_SQUAREMETRE);
9091
ComparableQuantity<Temperature> temperature =
91-
data.getQuantity(TEMPERATURE, Units.KELVIN).to(StandardUnits.TEMPERATURE);
92+
data.getQuantityOptional(TEMPERATURE, Units.KELVIN)
93+
.map(quantity -> quantity.to(StandardUnits.TEMPERATURE))
94+
.orElseThrow(
95+
() ->
96+
new FactoryException(
97+
"The field \"" + TEMPERATURE + "\" is missing but required."));
9298
ComparableQuantity<Angle> windDirection = getWindDirection(data);
9399
ComparableQuantity<Speed> windVelocity = getWindVelocity(data);
94100
Optional<ComparableQuantity<Temperature>> groundTemperatureLevel1 =

src/test/groovy/edu/ie3/datamodel/io/factory/timeseries/CosmoTimeBasedWeatherValueFactoryTest.groovy

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class CosmoTimeBasedWeatherValueFactoryTest extends Specification {
4040
factory.buildModel(data)
4141

4242
then:
43-
thrown(NullPointerException)
43+
def exception = thrown(FactoryException)
44+
exception.message.toLowerCase().contains("temperature")
4445
}
4546

4647
def "A PsdmTimeBasedWeatherValueFactory should be able to create time series values"() {
@@ -62,19 +63,14 @@ class CosmoTimeBasedWeatherValueFactoryTest extends Specification {
6263

6364
def data = new TimeBasedWeatherValueData(parameter, coordinate)
6465

65-
def expectedTemperature = Quantities.getQuantity(
66-
Double.parseDouble(parameter.get("temperature")),
67-
Units.KELVIN
68-
)
69-
7066
def expectedResults = new TimeBasedValue(
7167
time, new WeatherValue(coordinate,
7268
Quantities.getQuantity(286.872985839844d, StandardUnits.SOLAR_IRRADIANCE),
7369
Quantities.getQuantity(282.671997070312d, StandardUnits.SOLAR_IRRADIANCE),
74-
expectedTemperature,
70+
Quantities.getQuantity(278.019012451172d, Units.KELVIN),
7571
Quantities.getQuantity(0d, StandardUnits.WIND_DIRECTION),
7672
Quantities.getQuantity(1.66103506088257d, StandardUnits.WIND_VELOCITY),
77-
Optional.of(expectedTemperature),
73+
Optional.of(Quantities.getQuantity(278.019012451172d, Units.KELVIN)),
7874
Optional.empty()))
7975

8076
when:

src/test/groovy/edu/ie3/datamodel/io/factory/timeseries/IconTimeBasedWeatherValueFactoryTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,9 @@ class IconTimeBasedWeatherValueFactoryTest extends Specification {
200200
given:
201201
def factory = new IconTimeBasedWeatherValueFactory()
202202
def coordinate = CosmoWeatherTestData.COORDINATE_67775
203-
def time = TimeUtil.withDefaults.toZonedDateTime("2019-01-01T00:00:00Z")
204203

205204
Map<String, String> parameter = [
206-
"time" : TimeUtil.withDefaults.toString(time),
205+
"time" : "2019-01-01T00:00:00Z",
207206
"aswdifdS" : "1.0",
208207
"aswdirS" : "2.0",
209208
"t2m" : "",
@@ -218,6 +217,7 @@ class IconTimeBasedWeatherValueFactoryTest extends Specification {
218217
factory.buildModel(data)
219218

220219
then:
221-
thrown(Exception)
220+
def exception = thrown(FactoryException)
221+
exception.message.toLowerCase().contains("t2m")
222222
}
223223
}

0 commit comments

Comments
 (0)