Skip to content

Commit

Permalink
Allow for the declaration of explicit time pools, #245.
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-brown committed Oct 22, 2024
1 parent 6d35b23 commit ab0b9d6
Show file tree
Hide file tree
Showing 12 changed files with 579 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/wres/helpers/MainUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public static boolean isSimpleOperation( String operation )
* @param operation the operation
* @return a result that may contain the operation
*/
private static Optional<Entry<WresFunction, Function<Functions.SharedResources, ExecutionResult>>> getOperation(
String operation )
private static Optional<Entry<WresFunction, Function<Functions.SharedResources, ExecutionResult>>>
getOperation( String operation )
{
Objects.requireNonNull( operation );
String finalOperation = operation.toLowerCase();
Expand Down
36 changes: 32 additions & 4 deletions wres-config/nonsrc/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,18 @@ definitions:
"$ref": "#/definitions/SpatialMask"
reference_dates:
"$ref": "#/definitions/Dates"
reference_date_pools:
time_pools:
"$ref": "#/definitions/TimePools"
reference_date_pools:
"$ref": "#/definitions/TimePoolSequence"
valid_dates:
"$ref": "#/definitions/Dates"
valid_date_pools:
"$ref": "#/definitions/TimePools"
"$ref": "#/definitions/TimePoolSequence"
lead_times:
"$ref": "#/definitions/LeadTimes"
lead_time_pools:
"$ref": "#/definitions/TimePools"
"$ref": "#/definitions/TimePoolSequence"
analysis_times:
"$ref": "#/definitions/AnalysisTimes"
time_scale:
Expand Down Expand Up @@ -875,7 +877,33 @@ definitions:
- unit

TimePools:
title: The temporal boundaries of a pool of data to evaluate.
title: Explicitly declared time pools.
description: "The declaration of an explicit, possibly irregular, sequence
of time pools. Each pool contains an interval of times whose corresponding
pairs will be pooled together when calculating statistics."
type: array
items:
"$ref": "#/definitions/TimePool"
minItems: 1
uniqueItems: true
additionalProperties: false

TimePool:
title: Explicitly declared time pool.
description: "The declaration of an explicit time pools. Each pool contains an interval of
times whose corresponding pairs will be pooled together when calculating statistics."
type: object
additionalProperties: false
properties:
reference_dates:
"$ref": "#/definitions/Dates"
valid_dates:
"$ref": "#/definitions/Dates"
lead_times:
"$ref": "#/definitions/LeadTimes"

TimePoolSequence:
title: The temporal boundaries of a pool sequence to evaluate.
description: "The declaration of a regular sequence of time pools. Each
pool contains an interval of times whose corresponding pairs will be pooled
together when calculating statistics. The regular sequence contains pools
Expand Down
171 changes: 171 additions & 0 deletions wres-config/src/wres/config/yaml/DeclarationInterpolator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -18,6 +20,8 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.google.protobuf.Timestamp;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -50,6 +54,7 @@
import wres.config.yaml.components.ThresholdSourceBuilder;
import wres.config.yaml.components.ThresholdType;
import wres.config.yaml.components.VariableBuilder;
import wres.statistics.MessageFactory;
import wres.statistics.generated.EvaluationStatus;
import wres.statistics.generated.EvaluationStatus.EvaluationStatusEvent;
import wres.statistics.generated.Geometry;
Expand All @@ -60,6 +65,7 @@
import wres.statistics.generated.Pool;
import wres.statistics.generated.SummaryStatistic;
import wres.statistics.generated.TimeScale;
import wres.statistics.generated.TimeWindow;

/**
* <p>Interpolates missing declaration from the other declaration present. The interpolation of missing declaration may
Expand Down Expand Up @@ -170,6 +176,8 @@ public static EvaluationDeclaration interpolate( EvaluationDeclaration declarati
DeclarationInterpolator.interpolateMetricParameters( adjustedBuilder );
// Interpolate output formats where none exist
DeclarationInterpolator.interpolateOutputFormatsWhenNoneDeclared( adjustedBuilder );
// Interpolate time windows
DeclarationInterpolator.interpolateTimeWindows( adjustedBuilder );

// Handle any events encountered
DeclarationInterpolator.handleEvents( events, notify, true );
Expand Down Expand Up @@ -1185,6 +1193,169 @@ private static void interpolateOutputFormatsWhenNoneDeclared( EvaluationDeclarat
}
}

/**
* Interpolates the missing components of any explicitly declared time windows.
*
* @param builder the builder to mutate
*/
private static void interpolateTimeWindows( EvaluationDeclarationBuilder builder )
{
Set<TimeWindow> timeWindows = builder.timeWindows();
Set<TimeWindow> adjustedTimeWindows = new HashSet<>();

// Set the earliest and latest lead durations
Pair<com.google.protobuf.Duration, com.google.protobuf.Duration> leadTimes =
DeclarationInterpolator.getLeadDurationInterval( builder );
com.google.protobuf.Duration earliestProtoDuration = leadTimes.getLeft();
com.google.protobuf.Duration latestProtoDuration = leadTimes.getRight();

// Set the earliest and latest valid times
Pair<Timestamp, Timestamp> validTimes = DeclarationInterpolator.getValidTimeInterval( builder );
Timestamp earliestValidProto = validTimes.getLeft();
Timestamp latestValidProto = validTimes.getRight();

// Set the earliest and latest reference times
Pair<Timestamp, Timestamp> referenceTimes = DeclarationInterpolator.getReferenceTimeInterval( builder );
Timestamp earliestReferenceProto = referenceTimes.getLeft();
Timestamp latestReferenceProto = referenceTimes.getRight();

for ( TimeWindow next : timeWindows )
{
TimeWindow.Builder nextBuilder = next.toBuilder();

if ( !next.hasEarliestLeadDuration() )
{
nextBuilder.setEarliestLeadDuration( earliestProtoDuration );
}

if ( !next.hasLatestLeadDuration() )
{
nextBuilder.setLatestLeadDuration( latestProtoDuration );
}

if ( !next.hasEarliestValidTime() )
{
nextBuilder.setEarliestValidTime( earliestValidProto );
}

if ( !next.hasLatestValidTime() )
{
nextBuilder.setLatestValidTime( latestValidProto );
}

if ( !next.hasEarliestReferenceTime() )
{
nextBuilder.setEarliestReferenceTime( earliestReferenceProto );
}

if ( !next.hasLatestReferenceTime() )
{
nextBuilder.setLatestReferenceTime( latestReferenceProto );
}

TimeWindow nextAdjusted = nextBuilder.build();
adjustedTimeWindows.add( nextAdjusted );
}

builder.timeWindows( adjustedTimeWindows );
}

/**
* @param builder the declaration builder
* @return the lead duration interval
*/

private static Pair<com.google.protobuf.Duration, com.google.protobuf.Duration>
getLeadDurationInterval( EvaluationDeclarationBuilder builder )
{
Duration earliestDuration = MessageFactory.DURATION_MIN;
Duration latestDuration = MessageFactory.DURATION_MAX;

if ( Objects.nonNull( builder.leadTimes() ) )
{
if ( Objects.nonNull( builder.leadTimes()
.minimum() ) )
{
earliestDuration = builder.leadTimes()
.minimum();
}
if ( Objects.nonNull( builder.leadTimes()
.maximum() ) )
{
latestDuration = builder.leadTimes()
.maximum();
}
}

com.google.protobuf.Duration earliestProtoDuration = MessageFactory.getDuration( earliestDuration );
com.google.protobuf.Duration latestProtoDuration = MessageFactory.getDuration( latestDuration );

return Pair.of( earliestProtoDuration, latestProtoDuration );
}

/**
* @param builder the declaration builder
* @return the valid time interval
*/

private static Pair<Timestamp, Timestamp> getValidTimeInterval( EvaluationDeclarationBuilder builder )
{
Instant earliestValid = Instant.MIN;
Instant latestValid = Instant.MAX;
if ( Objects.nonNull( builder.validDates() ) )
{
if ( Objects.nonNull( builder.validDates()
.minimum() ) )
{
earliestValid = builder.validDates()
.minimum();
}
if ( Objects.nonNull( builder.validDates()
.maximum() ) )
{
latestValid = builder.validDates()
.maximum();
}
}

Timestamp earliestValidProto = MessageFactory.getTimestamp( earliestValid );
Timestamp latestValidProto = MessageFactory.getTimestamp( latestValid );

return Pair.of( earliestValidProto, latestValidProto );
}


/**
* @param builder the declaration builder
* @return the reference time interval
*/

private static Pair<Timestamp, Timestamp> getReferenceTimeInterval( EvaluationDeclarationBuilder builder )
{
Instant earliestReference = Instant.MIN;
Instant latestReference = Instant.MAX;
if ( Objects.nonNull( builder.referenceDates() ) )
{
if ( Objects.nonNull( builder.referenceDates()
.minimum() ) )
{
earliestReference = builder.referenceDates()
.minimum();
}
if ( Objects.nonNull( builder.referenceDates()
.maximum() ) )
{
latestReference = builder.referenceDates()
.maximum();
}
}

Timestamp earliestReferenceProto = MessageFactory.getTimestamp( earliestReference );
Timestamp latestReferenceProto = MessageFactory.getTimestamp( latestReference );

return Pair.of( earliestReferenceProto, latestReferenceProto );
}

/**
* Adds the unit to each value threshold in the set that does not have the unit defined.
*
Expand Down
31 changes: 22 additions & 9 deletions wres-config/src/wres/config/yaml/DeclarationUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ public static Set<TimeWindow> getTimeWindows( EvaluationDeclaration declaration
TimePools referenceDatesPools = declaration.referenceDatePools();
TimePools validDatesPools = declaration.validDatePools();

// Has explicit pooling windows
Set<TimeWindow> timeWindows;

// Add the time windows generated from a declared sequence
if ( Objects.nonNull( leadDurationPools )
|| Objects.nonNull( referenceDatesPools )
|| Objects.nonNull( validDatesPools ) )
Expand All @@ -132,58 +134,69 @@ public static Set<TimeWindow> getTimeWindows( EvaluationDeclaration declaration
{
LOGGER.debug( "Building time windows for reference dates and valid dates and lead durations." );

return DeclarationUtilities.getReferenceDatesValidDatesAndLeadDurationTimeWindows( declaration );
timeWindows = DeclarationUtilities.getReferenceDatesValidDatesAndLeadDurationTimeWindows( declaration );
}
// Reference dates and valid dates
else if ( Objects.nonNull( referenceDatesPools ) && Objects.nonNull( validDatesPools ) )
{
LOGGER.debug( "Building time windows for reference dates and valid dates." );

return DeclarationUtilities.getReferenceDatesAndValidDatesTimeWindows( declaration );
timeWindows = DeclarationUtilities.getReferenceDatesAndValidDatesTimeWindows( declaration );
}
// Reference dates and lead durations
else if ( Objects.nonNull( referenceDatesPools ) && Objects.nonNull( leadDurationPools ) )
{
LOGGER.debug( "Building time windows for reference dates and lead durations." );

return DeclarationUtilities.getReferenceDatesAndLeadDurationTimeWindows( declaration );
timeWindows = DeclarationUtilities.getReferenceDatesAndLeadDurationTimeWindows( declaration );
}
// Valid dates and lead durations
else if ( Objects.nonNull( validDatesPools ) && Objects.nonNull( leadDurationPools ) )
{
LOGGER.debug( "Building time windows for valid dates and lead durations." );

return DeclarationUtilities.getValidDatesAndLeadDurationTimeWindows( declaration );
timeWindows = DeclarationUtilities.getValidDatesAndLeadDurationTimeWindows( declaration );
}
// Reference dates
else if ( Objects.nonNull( referenceDatesPools ) )
{
LOGGER.debug( "Building time windows for reference dates." );

return DeclarationUtilities.getReferenceDatesTimeWindows( declaration );
timeWindows = DeclarationUtilities.getReferenceDatesTimeWindows( declaration );
}
// Lead durations
else if ( Objects.nonNull( leadDurationPools ) )
{
LOGGER.debug( "Building time windows for lead durations." );

return DeclarationUtilities.getLeadDurationTimeWindows( declaration );
timeWindows = DeclarationUtilities.getLeadDurationTimeWindows( declaration );
}
// Valid dates
else
{
LOGGER.debug( "Building time windows for valid dates." );

return DeclarationUtilities.getValidDatesTimeWindows( declaration );
timeWindows = DeclarationUtilities.getValidDatesTimeWindows( declaration );
}
}
// One big pool
else
{
LOGGER.debug( "Building one big time window." );

return Collections.singleton( DeclarationUtilities.getOneBigTimeWindow( declaration ) );
timeWindows = Collections.singleton( DeclarationUtilities.getOneBigTimeWindow( declaration ) );
}

// Add the explicitly declared time windows
Set<TimeWindow> finalWindows = new HashSet<>( timeWindows );

LOGGER.debug( "Added {} explicitly declared time pools to the overall group of time pools.",
declaration.timeWindows()
.size() );

finalWindows.addAll( declaration.timeWindows() );

return Collections.unmodifiableSet( finalWindows );
}

/**
Expand Down
Loading

0 comments on commit ab0b9d6

Please sign in to comment.