-
Notifications
You must be signed in to change notification settings - Fork 5
/
CsvWeatherSource.java
267 lines (244 loc) · 11.5 KB
/
CsvWeatherSource.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
259
260
261
262
263
264
265
266
267
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.source.csv;
import edu.ie3.datamodel.exceptions.SourceException;
import edu.ie3.datamodel.exceptions.ValidationException;
import edu.ie3.datamodel.io.connectors.CsvFileConnector;
import edu.ie3.datamodel.io.csv.CsvIndividualTimeSeriesMetaInformation;
import edu.ie3.datamodel.io.factory.timeseries.TimeBasedWeatherValueData;
import edu.ie3.datamodel.io.factory.timeseries.TimeBasedWeatherValueFactory;
import edu.ie3.datamodel.io.naming.FileNamingStrategy;
import edu.ie3.datamodel.io.naming.timeseries.ColumnScheme;
import edu.ie3.datamodel.io.source.IdCoordinateSource;
import edu.ie3.datamodel.io.source.WeatherSource;
import edu.ie3.datamodel.models.Entity;
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
import edu.ie3.datamodel.models.value.Value;
import edu.ie3.datamodel.models.value.WeatherValue;
import edu.ie3.datamodel.utils.TimeSeriesUtils;
import edu.ie3.datamodel.utils.Try;
import edu.ie3.datamodel.utils.Try.*;
import edu.ie3.util.interval.ClosedInterval;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.locationtech.jts.geom.Point;
/** Implements a WeatherSource for CSV files by using the CsvTimeSeriesSource as a base */
public class CsvWeatherSource extends WeatherSource {
private final CsvDataSource dataSource;
/**
* Initializes a CsvWeatherSource and immediately imports weather data, which will be kept for the
* lifetime of this source
*
* @param csvSep the separator string for csv columns
* @param folderPath path to the folder holding the time series files
* @param fileNamingStrategy strategy for the file naming of time series files / data sinks
* @param idCoordinateSource a coordinate source to map ids to points
* @param weatherFactory factory to transfer field to value mapping into actual java object
* instances
*/
public CsvWeatherSource(
String csvSep,
Path folderPath,
FileNamingStrategy fileNamingStrategy,
IdCoordinateSource idCoordinateSource,
TimeBasedWeatherValueFactory weatherFactory)
throws SourceException {
super(idCoordinateSource, weatherFactory);
this.dataSource = new CsvDataSource(csvSep, folderPath, fileNamingStrategy);
coordinateToTimeSeries = getWeatherTimeSeries();
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/** Returns an empty optional for now. */
@Override
public <C extends WeatherValue> Optional<Set<String>> getSourceFields(Class<C> entityClass) {
return Optional.empty();
}
@Override
public Map<Point, IndividualTimeSeries<WeatherValue>> getWeather(
ClosedInterval<ZonedDateTime> timeInterval) {
return trimMapToInterval(coordinateToTimeSeries, timeInterval);
}
@Override
public Map<Point, IndividualTimeSeries<WeatherValue>> getWeather(
ClosedInterval<ZonedDateTime> timeInterval, Collection<Point> coordinates) {
Map<Point, IndividualTimeSeries<WeatherValue>> filteredMap =
coordinateToTimeSeries.entrySet().stream()
.filter(entry -> coordinates.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return trimMapToInterval(filteredMap, timeInterval);
}
@Override
public Optional<TimeBasedValue<WeatherValue>> getWeather(ZonedDateTime date, Point coordinate) {
IndividualTimeSeries<WeatherValue> timeSeries = coordinateToTimeSeries.get(coordinate);
if (timeSeries == null) return Optional.empty();
return timeSeries.getTimeBasedValue(date);
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/**
* Trims all time series in a map to the given time interval
*
* @param map the map to trim the time series value of
* @param timeInterval the interval to trim the data to
* @return a map with trimmed time series
*/
private Map<Point, IndividualTimeSeries<WeatherValue>> trimMapToInterval(
Map<Point, IndividualTimeSeries<WeatherValue>> map,
ClosedInterval<ZonedDateTime> timeInterval) {
// decided against parallel mode here as it likely wouldn't pay off as the expected coordinate
// count is too low
return map.entrySet().stream()
.collect(
Collectors.toMap(
Map.Entry::getKey,
entry -> TimeSeriesUtils.trimTimeSeriesToInterval(entry.getValue(), timeInterval)));
}
/**
* Merge two individual time series into a new time series with the UUID of the first parameter
*
* @param a the first time series to merge
* @param b the second time series to merge
* @return merged time series with a's UUID
*/
protected <V extends Value> IndividualTimeSeries<V> mergeTimeSeries(
IndividualTimeSeries<V> a, IndividualTimeSeries<V> b) {
SortedSet<TimeBasedValue<V>> entries = a.getEntries();
entries.addAll(b.getEntries());
return new IndividualTimeSeries<>(a.getUuid(), entries);
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
private Map<Point, IndividualTimeSeries<WeatherValue>> getWeatherTimeSeries()
throws SourceException {
/* Get only weather time series meta information */
Collection<CsvIndividualTimeSeriesMetaInformation> weatherCsvMetaInformation =
dataSource
.connector
.getCsvIndividualTimeSeriesMetaInformation(ColumnScheme.WEATHER)
.values();
return readWeatherTimeSeries(Set.copyOf(weatherCsvMetaInformation), dataSource.connector);
}
/**
* Reads weather data to time series and maps them coordinate wise
*
* @param weatherMetaInformation Data needed for reading
* @return time series mapped to the represented coordinate
*/
private Map<Point, IndividualTimeSeries<WeatherValue>> readWeatherTimeSeries(
Set<CsvIndividualTimeSeriesMetaInformation> weatherMetaInformation,
CsvFileConnector connector)
throws SourceException {
final Map<Point, IndividualTimeSeries<WeatherValue>> weatherTimeSeries = new HashMap<>();
Function<Map<String, String>, Optional<TimeBasedValue<WeatherValue>>> fieldToValueFunction =
this::buildWeatherValue;
/* Reading in weather time series */
for (CsvIndividualTimeSeriesMetaInformation data : weatherMetaInformation) {
// we need a reader for each file
try (BufferedReader reader = connector.initReader(data.getFullFilePath())) {
buildStreamWithFieldsToAttributesMap(reader)
.getOrThrow()
.map(fieldToValueFunction)
.flatMap(Optional::stream)
.collect(Collectors.groupingBy(tbv -> tbv.getValue().getCoordinate()))
.forEach(
(point, timeBasedValues) -> {
// We have to generate a random UUID as we'd risk running into duplicate key
// issues
// otherwise
IndividualTimeSeries<WeatherValue> timeSeries =
new IndividualTimeSeries<>(UUID.randomUUID(), new HashSet<>(timeBasedValues));
if (weatherTimeSeries.containsKey(point)) {
IndividualTimeSeries<WeatherValue> mergedTimeSeries =
mergeTimeSeries(weatherTimeSeries.get(point), timeSeries);
weatherTimeSeries.put(point, mergedTimeSeries);
} else {
weatherTimeSeries.put(point, timeSeries);
}
});
} catch (FileNotFoundException e) {
throw new SourceException(
"Cannot read file " + data.getFullFilePath() + ". File not found!", e);
} catch (IOException e) {
throw new SourceException("Cannot read file " + data.getFullFilePath() + ".", e);
} catch (ValidationException e) {
throw new SourceException("Validation failed for file " + data.getFullFilePath() + ".", e);
}
}
return weatherTimeSeries;
}
private Try<Stream<Map<String, String>>, SourceException> buildStreamWithFieldsToAttributesMap(
BufferedReader bufferedReader) throws ValidationException {
Class<? extends Entity> entityClass = TimeBasedValue.class;
try (BufferedReader reader = bufferedReader) {
final String[] headline = dataSource.parseCsvRow(reader.readLine(), dataSource.csvSep);
// validating read file
weatherFactory.validate(Set.of(headline), WeatherValue.class).getOrThrow();
// by default try-with-resources closes the reader directly when we leave this method (which
// is wanted to avoid a lock on the file), but this causes a closing of the stream as well.
// As we still want to consume the data at other places, we start a new stream instead of
// returning the original one
Collection<Map<String, String>> allRows =
dataSource.csvRowFieldValueMapping(reader, headline);
return Success.of(dataSource.checkExactDuplicates("Weather", allRows).parallelStream());
} catch (IOException e) {
return Failure.of(
new SourceException(
"Cannot read file to build entity '" + entityClass.getSimpleName() + "'.", e));
}
}
/**
* Builds a {@link TimeBasedValue} of type {@link WeatherValue} from given "flat " input
* information. If the single model cannot be built, an empty optional is handed back.
*
* @param fieldToValues "flat " input information as a mapping from field to value
* @return Optional time based weather value
*/
private Optional<TimeBasedValue<WeatherValue>> buildWeatherValue(
Map<String, String> fieldToValues) {
/* Try to get the coordinate from entries */
Optional<Point> maybeCoordinate = extractCoordinate(fieldToValues);
return maybeCoordinate
.map(
coordinate -> {
/* Remove coordinate entry from fields */
fieldToValues.remove(weatherFactory.getCoordinateIdFieldString());
/* Build factory data */
TimeBasedWeatherValueData factoryData =
new TimeBasedWeatherValueData(fieldToValues, coordinate);
return weatherFactory.get(factoryData).getData();
})
.orElseGet(
() -> {
log.error("Unable to find coordinate for entry '{}'.", fieldToValues);
return Optional.empty();
});
}
/**
* Extract the coordinate identifier from the field to value mapping and obtain the actual
* coordinate in collaboration with the source.
*
* @param fieldToValues "flat " input information as a mapping from field to value
* @return Optional time based weather value
*/
private Optional<Point> extractCoordinate(Map<String, String> fieldToValues) {
String coordinateString = fieldToValues.get(weatherFactory.getCoordinateIdFieldString());
if (Objects.isNull(coordinateString) || coordinateString.isEmpty()) {
log.error(
"Cannot parse weather value. Unable to find field '{}' in data: {}",
weatherFactory.getCoordinateIdFieldString(),
fieldToValues);
return Optional.empty();
}
int coordinateId = Integer.parseInt(coordinateString);
return idCoordinateSource.getCoordinate(coordinateId);
}
}