Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NcmlReader.mergeNcml with groups #1404

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static NetcdfDataset.Builder mergeNcml(NetcdfFile ref, @Nullable Element

if (ncmlElem != null) {
NcmlReader reader = new NcmlReader();
reader.readGroup(targetDS, null, null, ncmlElem);
reader.readGroup(targetDS, null, ref.getRootGroup(), ncmlElem);
}

setEnhanceMode(targetDS, ncmlElem, null);
Expand Down Expand Up @@ -574,9 +574,12 @@ private Group.Builder readGroup(NetcdfDataset.Builder builder, @Nullable Group.B
Group refGroup = null;

if (parent == null) {
refGroup = this.refFile == null ? null : this.refFile.getRootGroup();
if (this.refFile != null) {
refGroup = this.refFile.getRootGroup();
} else if (refParent != null) {
refGroup = refParent;
}
groupBuilder = builder.rootGroup;

} else {
String name = groupElem.getAttributeValue("name");
if (name == null) {
Expand Down
16 changes: 16 additions & 0 deletions cdm/core/src/test/data/ncml/modifyNestedGroups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2"
location="nc/example.nc">
<group name="outer_group">
<attribute name="new_outer_attr" value="new_outer_attr_value" />
<variable name="lat">
<attribute name="new_lat_attr" value="new_lat_attr_value" />
</variable>
<group name="inner_group">
<attribute name="new_inner_attr" value="new_inner_attr_value" />
<variable name="lon">
<attribute name="new_lon_attr" value="new_lon_attr_value" />
</variable>
</group>
</group>
</netcdf>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.jdom2.input.SAXBuilder;
import org.junit.Test;
import ucar.ma2.Array;
import ucar.nc2.Attribute;
import ucar.nc2.Group;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.nc2.dataset.NetcdfDataset;
Expand All @@ -25,8 +27,9 @@ public class TestNcmlReader {
public void shouldMergeNcml() throws IOException, JDOMException {
final String filename = TestDir.cdmLocalTestDataDir + "example1.nc";

try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null)) {
final NetcdfDataset netcdfDataset = NcmlReader.mergeNcml(netcdfFile, getNcmlElement("modifyVars.xml")).build();
try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null);
final NetcdfDataset netcdfDataset =
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("modifyVars.xml")).build();) {

final Variable ncmlVariable = netcdfDataset.findVariable("deltaLat");
assertThat((Object) ncmlVariable).isInstanceOf(VariableDS.class);
Expand All @@ -40,9 +43,9 @@ public void shouldMergeNcml() throws IOException, JDOMException {
public void shouldMergeNcmlWithEnhancements() throws IOException, JDOMException {
final String filename = TestDir.cdmLocalTestDataDir + "example1.nc";

try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null)) {
final NetcdfDataset netcdfDataset =
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("enhance/testStandardizer.ncml")).build();
try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null);
final NetcdfDataset netcdfDataset =
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("enhance/testStandardizer.ncml")).build();) {

final Variable ncmlVariable = netcdfDataset.findVariable("doublevar");
assertThat((Object) ncmlVariable).isNotNull();
Expand All @@ -51,6 +54,49 @@ public void shouldMergeNcmlWithEnhancements() throws IOException, JDOMException
}
}

@Test
public void mergeWithExistingGroups() throws IOException, JDOMException {
final String filename = TestDir.cdmLocalTestDataDir + "testModifyNestedGroups.nc4";

try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null);
final NetcdfDataset netcdfDataset =
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("modifyNestedGroups.xml")).build()) {

final Group ncmlOuterGroup = netcdfDataset.findGroup("/outer_group");
assertThat(ncmlOuterGroup).isNotNull();

final Attribute ncmlOuterAttribute = ncmlOuterGroup.findAttribute("new_outer_attr");
assertThat(ncmlOuterAttribute).isNotNull();
assertThat(ncmlOuterAttribute.getStringValue()).isNotNull();
assertThat(ncmlOuterAttribute.getStringValue()).isEqualTo("new_outer_attr_value");

final Variable ncmlOuterVar = ncmlOuterGroup.findVariableLocal("lat");
assertThat(ncmlOuterVar != null).isTrue();
assertThat(ncmlOuterVar.attributes().hasAttribute("new_lat_attr")).isTrue();
assertThat(ncmlOuterVar.attributes().findAttribute("new_lat_attr")).isNotNull();
assertThat(ncmlOuterVar.attributes().findAttribute("new_lat_attr").getStringValue()).isNotNull();
assertThat(ncmlOuterVar.attributes().findAttribute("new_lat_attr").getStringValue())
.isEqualTo("new_lat_attr_value");
// <attribute name="new_lat_attr" value="new_lat_attr_value" />

final Group ncmlInnerGroup = netcdfDataset.findGroup("/outer_group/inner_group");
assertThat(ncmlInnerGroup).isNotNull();

final Attribute ncmlInnerAttribute = ncmlInnerGroup.findAttribute("new_inner_attr");
assertThat(ncmlInnerAttribute).isNotNull();
assertThat(ncmlInnerAttribute.getStringValue()).isNotNull();
assertThat(ncmlInnerAttribute.getStringValue()).isEqualTo("new_inner_attr_value");

final Variable ncmlInnerVar = ncmlInnerGroup.findVariableLocal("lon");
assertThat(ncmlInnerVar != null).isTrue();
assertThat(ncmlInnerVar.attributes().hasAttribute("new_lon_attr")).isTrue();
assertThat(ncmlInnerVar.attributes().findAttribute("new_lon_attr")).isNotNull();
assertThat(ncmlInnerVar.attributes().findAttribute("new_lon_attr").getStringValue()).isNotNull();
assertThat(ncmlInnerVar.attributes().findAttribute("new_lon_attr").getStringValue())
.isEqualTo("new_lon_attr_value");
}
}

private static Element getNcmlElement(String filename) throws IOException, JDOMException {
final String ncml = TestNcmlRead.topDir + filename;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public boolean accept(File pathname) {
// NcMLReader does not change variable to type int, so fails.
if (name.contains("aggSynthetic.xml"))
return false;
// Simulates a TDS datasetScan using NcML and does not fully define a dataset.
if (name.contains("modifyNestedGroups.xml"))
return false;
// Bug in old reader
if (name.contains("testStandaloneNoEnhance.ncml"))
return false;
Expand Down
Loading