Skip to content

Commit

Permalink
Make start/end time shifts granular
Browse files Browse the repository at this point in the history
In the time time-shifter utility, make start/end time shifts independent and granular. This reduces the amount of processing that is required when grids are imported with a common start/end time but independent start & end time shifts need to be made i.e., shift start by -12 hours and shift end by 12 hours.
  • Loading branch information
Tom Brauer committed Oct 17, 2024
1 parent 44aab3f commit 840b020
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ public class Shifter {

private final String pathToFile;
private final Set<String> variables;
private final Set<TimeShiftMethod> methods;
private final Duration shift;
private final Duration shiftStart;
private final Duration shiftEnd;
private final Path destination;
private final Map<String, String> options;
private final PropertyChangeSupport support;

private Shifter(Builder builder) {
this.pathToFile = builder.pathToFile;
this.variables = builder.grids;
this.methods = builder.methods;
this.shift = builder.shift;
this.shiftStart = builder.shiftStart;
this.shiftEnd = builder.shiftEnd;
this.destination = builder.destination;
this.options = builder.options;
this.support = new PropertyChangeSupport(this);
Expand All @@ -46,7 +46,9 @@ public static class Builder {
private String pathToFile;
private Set<String> grids;
private final Set<TimeShiftMethod> methods = new HashSet<>(List.of(TimeShiftMethod.START, TimeShiftMethod.END));
private Duration shift;
private Duration shift = Duration.ZERO;
private Duration shiftStart = Duration.ZERO;
private Duration shiftEnd = Duration.ZERO;
private Path destination;
private Map<String, String> options = new HashMap<>();
private final List<Handler> handlers = new ArrayList<>();
Expand All @@ -61,17 +63,40 @@ public Builder grids(final Set<String> grids) {
return this;
}

/**
* @param methods the time shift methods
* @return the builder
* @deprecated since 0.11.16, replaced by {@link #shiftStart} and {@link #shiftEnd}
*/
@Deprecated
public Builder methods(final Set<TimeShiftMethod> methods) {
this.methods.clear();
this.methods.addAll(methods);
return this;
}

/**
* @param shift the shift duration
* @return the builder
* @deprecated since 0.11.16, replaced by {@link #shiftStart} and {@link #shiftEnd}
*/
@Deprecated
public Builder shift(final Duration shift) {
logger.info(() -> "shift has been deprecated as of v0.11.16, use shiftStart and shiftEnd");
this.shift = shift;
return this;
}

public Builder shiftStart(final Duration shiftStart) {
this.shiftStart = shiftStart;
return this;
}

public Builder shiftEnd(final Duration shiftEnd) {
this.shiftEnd = shiftEnd;
return this;
}

public Builder destination(final String destination) {
this.destination = Paths.get(destination);
return this;
Expand Down Expand Up @@ -102,6 +127,22 @@ public Shifter build() {
if (methods.isEmpty())
throw new IllegalStateException("Methods must not be empty");

if (!shift.equals(Duration.ZERO) && !shiftStart.equals(Duration.ZERO)) {
logger.info(() -> "Specified shift of " + shift + " will override shiftStart of " + shiftStart);
}

if (!shift.equals(Duration.ZERO) && !shiftEnd.equals(Duration.ZERO)) {
logger.info(() -> "Specified shift of " + shift + " will override shiftEnd of " + shiftEnd);
}

if (!shift.equals(Duration.ZERO) && methods.contains(TimeShiftMethod.START)) {
shiftStart = shift;
}

if (!shift.equals(Duration.ZERO) && methods.contains(TimeShiftMethod.END)) {
shiftEnd = shift;
}

return new Shifter(this);
}
}
Expand All @@ -118,54 +159,49 @@ public void shift() {
int total = variables.size();

variables.forEach(variable -> {
DataReader reader = DataReader.builder()
try (DataReader reader = DataReader.builder()
.path(pathToFile)
.variable(variable)
.build();
.build()) {

int count = reader.getDtoCount();
int count = reader.getDtoCount();

for (int i = 0; i < count; i++) {
VortexGrid grid = (VortexGrid) reader.getDto(i);
for (int i = 0; i < count; i++) {
VortexGrid grid = (VortexGrid) reader.getDto(i);

List<VortexData> shiftedGrids = new ArrayList<>();
shiftedGrids.add(shift(grid, methods, shift));
List<VortexData> shiftedGrids = new ArrayList<>();
shiftedGrids.add(shift(grid, shiftStart, shiftEnd));

DataWriter writer = DataWriter.builder()
.data(shiftedGrids)
.destination(destination)
.options(options)
.build();
DataWriter writer = DataWriter.builder()
.data(shiftedGrids)
.destination(destination)
.options(options)
.build();

writer.write();
writer.write();
}
} catch (Exception e) {
logger.log(Level.SEVERE, e, e::getMessage);
}

int newValue = (int) (((float) processed.incrementAndGet() / total) * 100);
support.firePropertyChange("progress", null, newValue);
});
}

public static VortexGrid shift(VortexGrid dto, Set<TimeShiftMethod> methods, Duration shift) {
static VortexGrid shift(VortexGrid dto, Duration shiftStart, Duration shiftEnd) {
ZonedDateTime shiftedStart;
if (dto.startTime() != null) {
ZonedDateTime start = dto.startTime();
if (methods.contains(TimeShiftMethod.START)) {
shiftedStart = start.plus(shift);
} else {
shiftedStart = start;
}
shiftedStart = start.plus(shiftStart);
} else {
shiftedStart = null;
}

ZonedDateTime shiftedEnd;
if (dto.endTime() != null) {
ZonedDateTime end = dto.endTime();
if (methods.contains(TimeShiftMethod.END)) {
shiftedEnd = end.plus(shift);
} else {
shiftedEnd = end;
}
shiftedEnd = end.plus(shiftEnd);
} else {
shiftedEnd = null;
}
Expand Down
Loading

0 comments on commit 840b020

Please sign in to comment.