Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptation to 96TS in import NTC2 #207

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.InputStream;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -33,7 +34,7 @@ public static Double getD2ExchangeByOffsetDateTime(InputStream ntc2InputStream,
Map<Integer, Double> qtyByPositionMap = new HashMap<>();
CapacityDocument capacityDocument = DataUtil.unmarshalFromInputStream(ntc2InputStream, CapacityDocument.class);
checkTimeInterval(capacityDocument, targetDateTime);
int position = targetDateTime.atZoneSameInstant(ZoneId.of("CET")).getHour() + 1;

capacityDocument.getCapacityTimeSeries()
.stream()
.filter(ts -> IT_AREA_CODE.equalsIgnoreCase(ts.getInArea().getV()) && !ts.getPeriod().isEmpty())
Expand All @@ -46,9 +47,20 @@ public static Double getD2ExchangeByOffsetDateTime(InputStream ntc2InputStream,
final int index = interval.getPos().getV();
qtyByPositionMap.put(index, interval.getQty().getV().doubleValue());
});
return Optional.ofNullable(qtyByPositionMap.get(position))
.orElseThrow(() -> new CseDataException(
String.format("Impossible to retrieve NTC2 position %d. It does not exist", position)));
int position;
ZonedDateTime targetDateInCETZone = targetDateTime.atZoneSameInstant(ZoneId.of("CET"));
if (qtyByPositionMap.size() == 96) {
if (targetDateInCETZone.getMinute() % 15 != 0) {
throw new CseDataException("Minutes must be a multiple of 15 for 96 intervals");
}
position = 1 + (4 * targetDateInCETZone.getHour()) + (targetDateInCETZone.getMinute() / 15);
} else if (qtyByPositionMap.size() == 24) {
position = targetDateInCETZone.getHour() + 1;
} else {
throw new CseDataException(String.format("CapacityTimeSeries contains %s intervals which is different to 24 or 96", qtyByPositionMap.size()));
}

return qtyByPositionMap.get(position);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle the possibility of a null value when retrieving from the map

The method qtyByPositionMap.get(position) may return null if the position key does not exist, potentially leading to a NullPointerException.

Consider adding a null check:

- return qtyByPositionMap.get(position);
+ Double quantity = qtyByPositionMap.get(position);
+ if (quantity == null) {
+     throw new CseDataException("No quantity found for position: " + position);
+ }
+ return quantity;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return qtyByPositionMap.get(position);
Double quantity = qtyByPositionMap.get(position);
if (quantity == null) {
throw new CseDataException("No quantity found for position: " + position);
}
return quantity;

}

private static void checkTimeInterval(CapacityDocument capacityDocument, OffsetDateTime targetDateTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author Amira Kahya {@literal <amira.kahya at rte-france.com>}
* @author Joris Mancini {@literal <joris.mancini at rte-france.com>}
*/
@SpringBootTest
Expand Down Expand Up @@ -137,6 +138,26 @@ void testImportNtc2AllPresent() {
assertEquals(8192, ntc2.getExchanges().get("10YSI-ELES-----O"));
}

@Test
void testImportNtc296TSAllPresent() {
Ntc2 ntc2 = fileImporter.importNtc2(OffsetDateTime.parse("2021-06-24T00:30Z"), // 02:30 in CET zone
getClass().getResource("NTC2_20210624_2D5_AT-IT1-test-96.xml").toString(),
getClass().getResource("NTC2_20210624_2D5_CH-IT1-test-96.xml").toString(),
getClass().getResource("NTC2_20210624_2D5_FR-IT1-test-96.xml").toString(),
getClass().getResource("NTC2_20210624_2D5_SI-IT1-test-96.xml").toString());
assertNotNull(ntc2);
assertNotNull(ntc2.getExchanges());
assertEquals(4, ntc2.getExchanges().size());
assertNotNull(ntc2.getExchanges().get("10YAT-APG------L"));
assertEquals(990, ntc2.getExchanges().get("10YAT-APG------L"));
assertNotNull(ntc2.getExchanges().get("10YCH-SWISSGRIDZ"));
assertEquals(150, ntc2.getExchanges().get("10YCH-SWISSGRIDZ"));
assertNotNull(ntc2.getExchanges().get("10YFR-RTE------C"));
assertEquals(1199, ntc2.getExchanges().get("10YFR-RTE------C"));
assertNotNull(ntc2.getExchanges().get("10YSI-ELES-----O"));
assertEquals(15, ntc2.getExchanges().get("10YSI-ELES-----O"));
}

@Test
void assertThrowsWhenDataIsMissing() {
OffsetDateTime time = OffsetDateTime.parse("2021-06-23T22:00Z");
Expand Down Expand Up @@ -170,14 +191,14 @@ void assertThrowsWhenTargetDateTimeOutOfBound() {
@Test
void testImportFailsWithMissingPositions() {
OffsetDateTime time = OffsetDateTime.parse("2021-06-24T15:00Z");
String atFileUrl = getClass().getResource("NTC2_20210624_2D5_AT-IT1-test.xml").toString();
String chFileUrl = getClass().getResource("NTC2_20210624_2D5_CH-IT1-test.xml").toString();
String frFileUrl = getClass().getResource("NTC2_20210624_2D5_FR-IT1-test.xml").toString();
String siFileUrl = getClass().getResource("NTC2_20210624_2D5_SI-IT1-test.xml").toString();
String atFileUrl = getClass().getResource("NTC2_20210624_2D5_AT-IT1-test-missing-intervals.xml").toString();
String chFileUrl = getClass().getResource("NTC2_20210624_2D5_CH-IT1-test-missing-intervals.xml").toString();
String frFileUrl = getClass().getResource("NTC2_20210624_2D5_FR-IT1-test-missing-intervals.xml").toString();
String siFileUrl = getClass().getResource("NTC2_20210624_2D5_SI-IT1-test-missing-intervals.xml").toString();
Throwable e = assertThrows(CseDataException.class, () -> fileImporter.importNtc2(time, atFileUrl, chFileUrl, frFileUrl, siFileUrl));
assertEquals("Impossible to import NTC2 file for area: 10YAT-APG------L", e.getMessage());
Throwable nestedE = e.getCause();
assertEquals("Impossible to retrieve NTC2 position 18. It does not exist", nestedE.getMessage());
assertEquals("CapacityTimeSeries contains 7 intervals which is different to 24 or 96", nestedE.getMessage());
}

@Test
Expand Down
Loading
Loading