-
Notifications
You must be signed in to change notification settings - Fork 19
/
EntsoeCaseRepository.java
258 lines (231 loc) · 10.9 KB
/
EntsoeCaseRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* Copyright (c) 2016, All partners of the iTesla project (http://www.itesla-project.eu/consortium)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.cases;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import eu.itesla_project.computation.ComputationManager;
import eu.itesla_project.iidm.datasource.ReadOnlyDataSource;
import eu.itesla_project.iidm.datasource.ReadOnlyDataSourceFactory;
import eu.itesla_project.iidm.datasource.GenericReadOnlyDataSource;
import eu.itesla_project.iidm.import_.Importer;
import eu.itesla_project.iidm.import_.Importers;
import eu.itesla_project.iidm.network.Country;
import eu.itesla_project.iidm.network.Network;
import eu.itesla_project.entsoe.util.EntsoeFileName;
import eu.itesla_project.entsoe.util.EntsoeGeographicalCode;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Common ENTSOE case repository layout:
* <pre>
* CIM/SN/2013/01/15/20130115_0620_SN2_FR0.zip
* /FO/...
* UCT/SN/...
* /FO/...
* </pre>
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class EntsoeCaseRepository implements CaseRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(EntsoeCaseRepository.class);
static class EntsoeFormat {
private final Importer importer;
private final String dirName;
EntsoeFormat(Importer importer, String dirName) {
this.importer = Objects.requireNonNull(importer);
this.dirName = Objects.requireNonNull(dirName);
}
Importer getImporter() {
return importer;
}
String getDirName() {
return dirName;
}
}
private final EntsoeCaseRepositoryConfig config;
private final List<EntsoeFormat> formats;
private final ReadOnlyDataSourceFactory dataSourceFactory;
public static CaseRepository create(ComputationManager computationManager) {
return new EntsoeCaseRepository(EntsoeCaseRepositoryConfig.load(), computationManager);
}
EntsoeCaseRepository(EntsoeCaseRepositoryConfig config, List<EntsoeFormat> formats, ReadOnlyDataSourceFactory dataSourceFactory) {
this.config = Objects.requireNonNull(config);
this.formats = Objects.requireNonNull(formats);
this.dataSourceFactory = Objects.requireNonNull(dataSourceFactory);
LOGGER.info(config.toString());
}
EntsoeCaseRepository(EntsoeCaseRepositoryConfig config, ComputationManager computationManager) {
this(config,
Arrays.asList(new EntsoeFormat(Importers.getImporter("CIM1", computationManager), "CIM"),
new EntsoeFormat(Importers.getImporter("UCTE", computationManager), "UCT")), // official ENTSOE formats)
(directory, baseName) -> new GenericReadOnlyDataSource(directory, baseName));
}
public EntsoeCaseRepositoryConfig getConfig() {
return config;
}
private static class ImportContext {
private final Importer importer;
private final ReadOnlyDataSource ds;
private ImportContext(Importer importer, ReadOnlyDataSource ds) {
this.importer = importer;
this.ds = ds;
}
}
// because D1 snapshot does not exist and forecast replacement is not yet implemented
private static Collection<EntsoeGeographicalCode> forCountryHacked(Country country) {
return EntsoeGeographicalCode.forCountry(country).stream()
.filter(EntsoeGeographicalCode -> EntsoeGeographicalCode != EntsoeGeographicalCode.D1)
.collect(Collectors.toList());
}
private <R> R scanRepository(DateTime date, CaseType type, Country country, Function<List<ImportContext>, R> handler) {
Collection<EntsoeGeographicalCode> geographicalCodes = country != null ? forCountryHacked(country)
: Collections.singleton(EntsoeGeographicalCode.UX);
for (EntsoeFormat format : formats) {
Path formatDir = config.getRootDir().resolve(format.getDirName());
if (Files.exists(formatDir)) {
Path typeDir = formatDir.resolve(type.name());
if (Files.exists(typeDir)) {
Path dayDir = typeDir.resolve(String.format("%04d", date.getYear()))
.resolve(String.format("%02d", date.getMonthOfYear()))
.resolve(String.format("%02d", date.getDayOfMonth()));
if (Files.exists(dayDir)) {
List<ImportContext> importContexts = null;
for (EntsoeGeographicalCode geographicalCode : geographicalCodes) {
Collection<String> forbiddenFormats = config.getForbiddenFormatsByGeographicalCode().get(geographicalCode);
if (!forbiddenFormats.contains(format.getImporter().getFormat())) {
for (int i = 9; i >= 0; i--) {
String baseName = String.format("%04d%02d%02d_%02d%02d_" + type + "%01d_" + geographicalCode.name() + "%01d",
date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), date.getMinuteOfHour(),
date.getDayOfWeek(), i);
ReadOnlyDataSource ds = dataSourceFactory.create(dayDir, baseName);
if (importContexts == null) {
importContexts = new ArrayList<>();
}
if (format.getImporter().exists(ds)) {
importContexts.add(new ImportContext(format.getImporter(), ds));
}
}
}
}
if (importContexts != null) {
R result = handler.apply(importContexts);
if (result != null) {
return result;
}
}
}
}
}
}
return null;
}
private static DateTime toCetDate(DateTime date) {
DateTimeZone CET = DateTimeZone.forID("CET");
if (!date.getZone().equals(CET)) {
return date.toDateTime(CET);
}
return date;
}
@Override
public List<Network> load(DateTime date, CaseType type, Country country) {
Objects.requireNonNull(date);
Objects.requireNonNull(type);
List<Network> networks2 = scanRepository(toCetDate(date), type, country, importContexts -> {
List<Network> networks = null;
if (importContexts.size() > 0) {
networks = new ArrayList<>();
for (ImportContext importContext : importContexts) {
LOGGER.info("Loading {} in {} format", importContext.ds.getBaseName(), importContext.importer.getFormat());
networks.add(importContext.importer.import_(importContext.ds, null));
}
}
return networks;
});
return networks2 == null ? Collections.emptyList() : networks2;
}
@Override
public boolean isDataAvailable(DateTime date, CaseType type, Country country) {
return isNetworkDataAvailable(date, type, country);
}
private boolean isNetworkDataAvailable(DateTime date, CaseType type, Country country) {
Objects.requireNonNull(date);
Objects.requireNonNull(type);
return scanRepository(toCetDate(date), type, country, importContexts -> {
if (importContexts.size() > 0) {
for (ImportContext importContext : importContexts) {
if (importContext.importer.exists(importContext.ds)) {
return true;
}
}
return null;
}
return null;
}) != null;
}
private void browse(Path dir, Consumer<Path> handler) {
try (Stream<Path> stream = Files.list(dir)) {
stream.sorted().forEach(child -> {
if (Files.isDirectory(child)) {
browse(child, handler);
} else {
try {
if (Files.size(child) > 0) {
handler.accept(child);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Set<DateTime> dataAvailable(CaseType type, Set<Country> countries, Interval interval) {
Set<EntsoeGeographicalCode> geographicalCodes = new HashSet<>();
if (countries == null) {
geographicalCodes.add(EntsoeGeographicalCode.UX);
} else {
for (Country country : countries) {
geographicalCodes.addAll(forCountryHacked(country));
}
}
Multimap<DateTime, EntsoeGeographicalCode> dates = HashMultimap.create();
for (EntsoeFormat format : formats) {
Path formatDir = config.getRootDir().resolve(format.getDirName());
if (Files.exists(formatDir)) {
Path typeDir = formatDir.resolve(type.name());
if (Files.exists(typeDir)) {
browse(typeDir, path -> {
EntsoeFileName entsoeFileName = EntsoeFileName.parse(path.getFileName().toString());
EntsoeGeographicalCode geographicalCode = entsoeFileName.getGeographicalCode();
if (geographicalCode != null
&& !config.getForbiddenFormatsByGeographicalCode().get(geographicalCode).contains(format.getImporter().getFormat())
&& interval.contains(entsoeFileName.getDate())) {
dates.put(entsoeFileName.getDate(), geographicalCode);
}
});
}
}
}
return dates.asMap().entrySet().stream()
.filter(e -> new HashSet<>(e.getValue()).containsAll(geographicalCodes))
.map(Map.Entry::getKey)
.collect(Collectors.toCollection(TreeSet::new));
}
}