Skip to content

Commit

Permalink
Merge pull request #347 from NOAA-OWP/issue245
Browse files Browse the repository at this point in the history
Issue245
  • Loading branch information
james-d-brown authored Oct 24, 2024
2 parents 48a4ecb + 050433e commit 769e5c5
Show file tree
Hide file tree
Showing 12 changed files with 630 additions and 30 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
221 changes: 219 additions & 2 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 @@ -934,13 +942,15 @@ private static Outputs.GraphicFormat getGraphicsFormatOptions( Outputs.GraphicFo
Outputs.GraphicFormat.Builder newOptions = options.toBuilder();

// Reference date pools?
if ( Objects.nonNull( builder.referenceDatePools() )
if ( ( Objects.nonNull( builder.referenceDatePools() )
|| DeclarationInterpolator.hasExplicitReferenceDatePools( builder.timeWindows() ) )
&& options.getShape() == Outputs.GraphicFormat.GraphicShape.DEFAULT )
{
newOptions.setShape( Outputs.GraphicFormat.GraphicShape.ISSUED_DATE_POOLS );
}
// Valid date pools?
else if ( Objects.nonNull( builder.validDatePools() )
else if ( ( Objects.nonNull( builder.validDatePools() )
|| DeclarationInterpolator.hasExplicitValidDatePools( builder.timeWindows() ) )
&& options.getShape() == Outputs.GraphicFormat.GraphicShape.DEFAULT )
{
newOptions.setShape( Outputs.GraphicFormat.GraphicShape.VALID_DATE_POOLS );
Expand All @@ -955,6 +965,50 @@ else if ( Objects.nonNull( builder.validDatePools() )
return newOptions.build();
}

/**
* @param timeWindows the time windows
* @return whether there is more than one time window with different reference dates
*/
private static boolean hasExplicitReferenceDatePools( Set<TimeWindow> timeWindows )
{
Set<Pair<Timestamp, Timestamp>> referenceDates = new HashSet<>();
for ( TimeWindow next : timeWindows )
{
Pair<Timestamp, Timestamp> pair = Pair.of( next.getEarliestReferenceTime(), next.getLatestReferenceTime() );
referenceDates.add( pair );

// Short-circuit
if ( referenceDates.size() > 1 )
{
return true;
}
}

return false;
}

/**
* @param timeWindows the time windows
* @return whether there is more than one time window with different valid dates
*/
private static boolean hasExplicitValidDatePools( Set<TimeWindow> timeWindows )
{
Set<Pair<Timestamp, Timestamp>> validDates = new HashSet<>();
for ( TimeWindow next : timeWindows )
{
Pair<Timestamp, Timestamp> pair = Pair.of( next.getEarliestValidTime(), next.getLatestValidTime() );
validDates.add( pair );

// Short-circuit
if ( validDates.size() > 1 )
{
return true;
}
}

return false;
}

/**
* Copies the evaluation units to the value threshold units when they are not declared explicitly.
*
Expand Down Expand Up @@ -1185,6 +1239,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
Loading

0 comments on commit 769e5c5

Please sign in to comment.