Skip to content

Commit dd197b8

Browse files
committed
Fix NcmlReader.mergeNcml with groups
Ensure reference group is passed into readGroup when using NcmlReader.mergeNcml, and make sure the reference group information is taken into account by the readGroup code. Fixes #1403
1 parent 5e5b339 commit dd197b8

4 files changed

Lines changed: 73 additions & 8 deletions

File tree

cdm/core/src/main/java/ucar/nc2/internal/ncml/NcmlReader.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public static NetcdfDataset.Builder mergeNcml(NetcdfFile ref, @Nullable Element
288288

289289
if (ncmlElem != null) {
290290
NcmlReader reader = new NcmlReader();
291-
reader.readGroup(targetDS, null, null, ncmlElem);
291+
reader.readGroup(targetDS, null, ref.getRootGroup(), ncmlElem);
292292
}
293293

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

576576
if (parent == null) {
577-
refGroup = this.refFile == null ? null : this.refFile.getRootGroup();
577+
if (this.refFile != null) {
578+
refGroup = this.refFile.getRootGroup();
579+
} else if (refParent != null) {
580+
refGroup = refParent;
581+
}
578582
groupBuilder = builder.rootGroup;
579-
580583
} else {
581584
String name = groupElem.getAttributeValue("name");
582585
if (name == null) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2"
3+
location="nc/example.nc">
4+
<group name="outer_group">
5+
<attribute name="new_outer_attr" value="new_outer_attr_value" />
6+
<variable name="lat">
7+
<attribute name="new_lat_attr" value="new_lat_attr_value" />
8+
</variable>
9+
<group name="inner_group">
10+
<attribute name="new_inner_attr" value="new_inner_attr_value" />
11+
<variable name="lon">
12+
<attribute name="new_lon_attr" value="new_lon_attr_value" />
13+
</variable>
14+
</group>
15+
</group>
16+
</netcdf>
8.41 KB
Binary file not shown.

cdm/core/src/test/java/ucar/nc2/internal/ncml/TestNcmlReader.java

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.jdom2.input.SAXBuilder;
1111
import org.junit.Test;
1212
import ucar.ma2.Array;
13+
import ucar.nc2.Attribute;
14+
import ucar.nc2.Group;
1315
import ucar.nc2.NetcdfFile;
1416
import ucar.nc2.Variable;
1517
import ucar.nc2.dataset.NetcdfDataset;
@@ -25,8 +27,9 @@ public class TestNcmlReader {
2527
public void shouldMergeNcml() throws IOException, JDOMException {
2628
final String filename = TestDir.cdmLocalTestDataDir + "example1.nc";
2729

28-
try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null)) {
29-
final NetcdfDataset netcdfDataset = NcmlReader.mergeNcml(netcdfFile, getNcmlElement("modifyVars.xml")).build();
30+
try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null);
31+
final NetcdfDataset netcdfDataset =
32+
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("modifyVars.xml")).build();) {
3033

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

43-
try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null)) {
44-
final NetcdfDataset netcdfDataset =
45-
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("enhance/testStandardizer.ncml")).build();
46+
try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null);
47+
final NetcdfDataset netcdfDataset =
48+
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("enhance/testStandardizer.ncml")).build();) {
4649

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

57+
@Test
58+
public void mergeWithExistingGroups() throws IOException, JDOMException {
59+
final String filename = TestDir.cdmLocalTestDataDir + "testModifyNestedGroups.nc4";
60+
61+
try (NetcdfFile netcdfFile = NetcdfDatasets.openFile(filename, null);
62+
final NetcdfDataset netcdfDataset =
63+
NcmlReader.mergeNcml(netcdfFile, getNcmlElement("modifyNestedGroups.xml")).build()) {
64+
65+
final Group ncmlOuterGroup = netcdfDataset.findGroup("/outer_group");
66+
assertThat(ncmlOuterGroup).isNotNull();
67+
68+
final Attribute ncmlOuterAttribute = ncmlOuterGroup.findAttribute("new_outer_attr");
69+
assertThat(ncmlOuterAttribute).isNotNull();
70+
assertThat(ncmlOuterAttribute.getStringValue()).isNotNull();
71+
assertThat(ncmlOuterAttribute.getStringValue()).isEqualTo("new_outer_attr_value");
72+
73+
final Variable ncmlOuterVar = ncmlOuterGroup.findVariableLocal("lat");
74+
assertThat(ncmlOuterVar != null).isTrue();
75+
assertThat(ncmlOuterVar.attributes().hasAttribute("new_lat_attr")).isTrue();
76+
assertThat(ncmlOuterVar.attributes().findAttribute("new_lat_attr")).isNotNull();
77+
assertThat(ncmlOuterVar.attributes().findAttribute("new_lat_attr").getStringValue()).isNotNull();
78+
assertThat(ncmlOuterVar.attributes().findAttribute("new_lat_attr").getStringValue())
79+
.isEqualTo("new_lat_attr_value");
80+
// <attribute name="new_lat_attr" value="new_lat_attr_value" />
81+
82+
final Group ncmlInnerGroup = netcdfDataset.findGroup("/outer_group/inner_group");
83+
assertThat(ncmlInnerGroup).isNotNull();
84+
85+
final Attribute ncmlInnerAttribute = ncmlInnerGroup.findAttribute("new_inner_attr");
86+
assertThat(ncmlInnerAttribute).isNotNull();
87+
assertThat(ncmlInnerAttribute.getStringValue()).isNotNull();
88+
assertThat(ncmlInnerAttribute.getStringValue()).isEqualTo("new_inner_attr_value");
89+
90+
final Variable ncmlInnerVar = ncmlInnerGroup.findVariableLocal("lon");
91+
assertThat(ncmlInnerVar != null).isTrue();
92+
assertThat(ncmlInnerVar.attributes().hasAttribute("new_lon_attr")).isTrue();
93+
assertThat(ncmlInnerVar.attributes().findAttribute("new_lon_attr")).isNotNull();
94+
assertThat(ncmlInnerVar.attributes().findAttribute("new_lon_attr").getStringValue()).isNotNull();
95+
assertThat(ncmlInnerVar.attributes().findAttribute("new_lon_attr").getStringValue())
96+
.isEqualTo("new_lon_attr_value");
97+
}
98+
}
99+
54100
private static Element getNcmlElement(String filename) throws IOException, JDOMException {
55101
final String ncml = TestNcmlRead.topDir + filename;
56102

0 commit comments

Comments
 (0)