Skip to content

Commit

Permalink
Merge pull request #1383 from tdrwenski/fix-coverage-time-subset
Browse files Browse the repository at this point in the history
  • Loading branch information
oxelson authored Aug 29, 2024
2 parents aba4fbb + 58c8826 commit b81a9c0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public boolean isAscending() {
switch (spacing) {
case regularInterval:
case regularPoint:
return getResolution() > 0;
return getResolution() >= 0;

case irregularPoint:
return values[0] <= values[ncoords - 1];
Expand Down
103 changes: 65 additions & 38 deletions cdm/core/src/test/java/ucar/nc2/ft2/coverage/TestCoordAxisHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,88 @@

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import ucar.ma2.DataType;
import ucar.ma2.InvalidRangeException;
import ucar.ma2.Range;
import ucar.nc2.constants.AxisType;
import ucar.nc2.ft2.coverage.CoverageCoordAxis.Spacing;
import ucar.nc2.util.Optional;

@RunWith(Parameterized.class)
@RunWith(Enclosed.class)
public class TestCoordAxisHelper {

@Parameterized.Parameters(name = "{0}, {1}")
public static List<Object[]> getTestParameters() {
return Arrays.asList(
@RunWith(Parameterized.class)
public static class TestCoordAxisHelperParametrized {

new Object[] {Spacing.regularPoint, new double[] {0.0, 10.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.irregularPoint, new double[] {0.0, 5.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.regularInterval, new double[] {0.0, 10.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.contiguousInterval, new double[] {0.0, 5.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.discontiguousInterval, new double[] {0.0, 0.0, 20.0, 30.0, 40.0, 40.0, 50.0, 50.0}}
@Parameterized.Parameters(name = "{0}, {1}")
public static List<Object[]> getTestParameters() {
return Arrays.asList(

);
}
new Object[] {Spacing.regularPoint, new double[] {0.0, 10.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.irregularPoint, new double[] {0.0, 5.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.regularInterval, new double[] {0.0, 10.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.contiguousInterval, new double[] {0.0, 5.0, 20.0, 30.0, 40.0}},
new Object[] {Spacing.discontiguousInterval, new double[] {0.0, 0.0, 20.0, 30.0, 40.0, 40.0, 50.0, 50.0}}

);
}

private final Spacing spacing;
private final double[] values;

public TestCoordAxisHelperParametrized(Spacing spacing, double[] values) {
this.spacing = spacing;
this.values = values;
}

private final Spacing spacing;
private final double[] values;
@Test
public void shouldSubsetAxisByRange() throws InvalidRangeException {
final AxisType axisType = AxisType.Time;
final double resolution = values[1] - values[0];

public TestCoordAxisHelper(Spacing spacing, double[] values) {
this.spacing = spacing;
this.values = values;
final CoverageCoordAxisBuilder coverageCoordAxisBuilder = new CoverageCoordAxisBuilder("name", "unit",
"description", DataType.DOUBLE, axisType, null, CoverageCoordAxis.DependenceType.independent, null, spacing,
values.length, values[0], values[values.length - 1], resolution, values, null);
final CoverageCoordAxis1D coverageCoordAxis = new CoverageCoordAxis1D(coverageCoordAxisBuilder);
final CoordAxisHelper coordAxisHelper = new CoordAxisHelper(coverageCoordAxis);

final Range subsetRange = new Range(1, 3);
final CoverageCoordAxisBuilder subsetBuilder = coordAxisHelper.subsetByIndex(subsetRange);
assertThat(subsetBuilder).isNotNull();
assertThat(subsetBuilder.axisType).isEqualTo(axisType);
assertThat(subsetBuilder.startValue).isEqualTo(values[subsetRange.first()]);
assertThat(subsetBuilder.endValue).isEqualTo(values[subsetRange.last()]);

if (spacing == Spacing.regularPoint || spacing == Spacing.regularInterval) {
assertThat(subsetBuilder.values).isNull();
} else {
assertThat(subsetBuilder.values).isNotNull();
}
}
}

@Test
public void shouldSubsetAxisByRange() throws InvalidRangeException {
final AxisType axisType = AxisType.Time;
final double resolution = values[1] - values[0];

final CoverageCoordAxisBuilder coverageCoordAxisBuilder = new CoverageCoordAxisBuilder("name", "unit",
"description", DataType.DOUBLE, axisType, null, CoverageCoordAxis.DependenceType.independent, null, spacing,
values.length, values[0], values[values.length - 1], resolution, values, null);
final CoverageCoordAxis1D coverageCoordAxis = new CoverageCoordAxis1D(coverageCoordAxisBuilder);
final CoordAxisHelper coordAxisHelper = new CoordAxisHelper(coverageCoordAxis);

final Range subsetRange = new Range(1, 3);
final CoverageCoordAxisBuilder subsetBuilder = coordAxisHelper.subsetByIndex(subsetRange);
assertThat(subsetBuilder).isNotNull();
assertThat(subsetBuilder.axisType).isEqualTo(axisType);
assertThat(subsetBuilder.startValue).isEqualTo(values[subsetRange.first()]);
assertThat(subsetBuilder.endValue).isEqualTo(values[subsetRange.last()]);

if (spacing == Spacing.regularPoint || spacing == Spacing.regularInterval) {
assertThat(subsetBuilder.values).isNull();
} else {
assertThat(subsetBuilder.values).isNotNull();
public static class TestCoordAxisHelperNonParameterized {

@Test
public void shouldSubsetSingleValuedAxis() {
final double[] values = new double[] {42.0};
final double resolution = 0.0;

final CoverageCoordAxisBuilder coverageCoordAxisBuilder = new CoverageCoordAxisBuilder("name", "unit",
"description", DataType.DOUBLE, AxisType.Time, null, CoverageCoordAxis.DependenceType.independent, null,
Spacing.regularPoint, values.length, values[0], values[values.length - 1], resolution, values, null);
final CoverageCoordAxis1D coverageCoordAxis = new CoverageCoordAxis1D(coverageCoordAxisBuilder);
final CoordAxisHelper coordAxisHelper = new CoordAxisHelper(coverageCoordAxis);

final Optional<CoverageCoordAxisBuilder> subsetBuilder = coordAxisHelper.subset(0.0, 100.0, 1);
assertThat(subsetBuilder.isPresent()).isTrue();
assertThat(subsetBuilder.get().startValue).isEqualTo(values[0]);
assertThat(subsetBuilder.get().endValue).isEqualTo(values[0]);
}
}
}

0 comments on commit b81a9c0

Please sign in to comment.