Skip to content

Commit

Permalink
Merge pull request #1254 from tdrwenski/rename-variable-used-in-agg
Browse files Browse the repository at this point in the history
Fix renamed variable reading in aggregations
  • Loading branch information
haileyajohnson authored Oct 30, 2023
2 parents 9449937 + cd16856 commit 027414a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
4 changes: 4 additions & 0 deletions cdm/core/src/main/java/ucar/nc2/internal/ncml/AggDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ protected Array read(Variable mainv, CancelTask cancelTask) throws IOException {
return null;

Variable v = findVariable(ncd, mainv);
if (v == null) {
Aggregation.logger.error("AggDataset can't find " + mainv.getFullName() + " in " + ncd.getLocation());
throw new IllegalArgumentException("Variable '" + mainv.getFullName() + "' does not exist in aggregation.");
}
if (debugRead)
System.out.printf("Agg.read %s from %s in %s%n", mainv.getNameAndDimensions(), v.getNameAndDimensions(),
getLocation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,8 @@ protected Array read(Variable mainv, CancelTask cancelTask, List<Range> section)

Variable v = findVariable(ncd, mainv);
if (v == null) {
Aggregation.logger.error("AggOuterDimension cant find " + mainv.getFullName() + " in " + ncd.getLocation()
+ "; return all zeroes!!!");
return Array.factory(mainv.getDataType(), new Section(section).getShape()); // all zeros LOOK need missing
// value
Aggregation.logger.error("AggOuterDimension can't find " + mainv.getFullName() + " in " + ncd.getLocation());
throw new IllegalArgumentException("Variable '" + mainv.getFullName() + "' does not exist in aggregation.");
}

if (Aggregation.debugRead) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ private Optional<Builder> readVariableExisting(Group.Builder groupBuilder,
.orElseThrow(() -> new IllegalStateException("Cant find variable " + nameInFile));
}
}
vb.setName(name).setDataType(dtype);
vb.setOriginalName(nameInFile).setName(name).setDataType(dtype);
if (typedefS != null) {
vb.setEnumTypeName(typedefS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ucar.nc2.internal.ncml;

import static com.google.common.truth.Truth.assertThat;

import java.io.IOException;
import java.io.StringReader;
import org.junit.Test;
import ucar.ma2.Array;
import ucar.ma2.InvalidRangeException;
import ucar.ma2.Section;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

public class TestRenameVariableInAggregation {
private static final double TOLERANCE = 1.0e-6;

@Test
public void shouldRenameVariableUsedInAggregation() throws IOException, InvalidRangeException {
final String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n" // leavit
+ "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'>\n" // leavit
+ " <variable name='temperature' orgName='T' />\n" // leavit
+ " <aggregation dimName='time' type='joinNew'>\n" // leavit
+ " <variableAgg name='T'/>\n" // leavit
+ " <netcdf location='src/test/data/ncml/nc/scalarTime0.nc'/>\n" // leavit
+ " <netcdf location='src/test/data/ncml/nc/scalarTime1.nc'/>\n" // leavit
+ " <netcdf location='src/test/data/ncml/nc/scalarTime2.nc'/>\n" // leavit
+ " </aggregation>\n" // leavit
+ "</netcdf>"; // leavit

checkNcmlDataset(ncml);
}

@Test
public void shouldRenameVariableUsedInAggregationWithScan() throws IOException, InvalidRangeException {
final String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n" // leavit
+ "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'>\n" // leavit
+ " <variable name='temperature' orgName='T' />\n" // leavit
+ " <aggregation dimName='time' type='joinNew'>\n" // leavit
+ " <variableAgg name='T'/>\n" // leavit
+ " <scan location='src/test/data/ncml/nc/' subdirs='false' regExp='scalarTime.*nc'/>\n" // leavit
+ " </aggregation>\n" // leavit
+ "</netcdf>"; // leavit

checkNcmlDataset(ncml);
}

private static void checkNcmlDataset(String ncml) throws InvalidRangeException, IOException {
try (NetcdfFile ncfile = NcmlReader.readNcml(new StringReader(ncml), null, null).build()) {
final Variable oldVariable = ncfile.findVariable("T");
assertThat((Object) oldVariable).isNull();
final Variable newVariable = ncfile.findVariable("temperature");
assertThat((Object) newVariable).isNotNull();

final Array partialArray = newVariable.read(new Section("0:2, 0:0, 0:0"));
assertThat(partialArray.getSize()).isEqualTo(3);
assertThat(partialArray.getDouble(0)).isWithin(TOLERANCE).of(0.0);
assertThat(partialArray.getDouble(1)).isWithin(TOLERANCE).of(100.0);
assertThat(partialArray.getDouble(2)).isWithin(TOLERANCE).of(200.0);

final Array array = newVariable.read();
assertThat(array.getSize()).isEqualTo(36);
assertThat(array.getDouble(0)).isWithin(TOLERANCE).of(0.0);
assertThat(array.getDouble(12)).isWithin(TOLERANCE).of(100.0);
assertThat(array.getDouble(24)).isWithin(TOLERANCE).of(200.0);
}
}
}

0 comments on commit 027414a

Please sign in to comment.