From df89e7126929f638a347d0f0af50113ccde638c9 Mon Sep 17 00:00:00 2001
From: Sean Arms <67096+lesserwhirls@users.noreply.github.com>
Date: Fri, 3 Jan 2025 15:45:28 -0700
Subject: [PATCH] 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 Unidata/netcdf-java#1403
---
.../ucar/nc2/internal/ncml/NcmlReader.java | 9 ++-
.../src/test/data/ncml/modifyNestedGroups.xml | 16 +++++
.../src/test/data/testModifyNestedGroups.nc4 | Bin 0 -> 8616 bytes
.../nc2/internal/ncml/TestNcmlReader.java | 56 ++++++++++++++++--
4 files changed, 73 insertions(+), 8 deletions(-)
create mode 100644 cdm/core/src/test/data/ncml/modifyNestedGroups.xml
create mode 100644 cdm/core/src/test/data/testModifyNestedGroups.nc4
diff --git a/cdm/core/src/main/java/ucar/nc2/internal/ncml/NcmlReader.java b/cdm/core/src/main/java/ucar/nc2/internal/ncml/NcmlReader.java
index df76a96854..cb55b68a26 100644
--- a/cdm/core/src/main/java/ucar/nc2/internal/ncml/NcmlReader.java
+++ b/cdm/core/src/main/java/ucar/nc2/internal/ncml/NcmlReader.java
@@ -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);
@@ -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) {
diff --git a/cdm/core/src/test/data/ncml/modifyNestedGroups.xml b/cdm/core/src/test/data/ncml/modifyNestedGroups.xml
new file mode 100644
index 0000000000..5e23c2a646
--- /dev/null
+++ b/cdm/core/src/test/data/ncml/modifyNestedGroups.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/cdm/core/src/test/data/testModifyNestedGroups.nc4 b/cdm/core/src/test/data/testModifyNestedGroups.nc4
new file mode 100644
index 0000000000000000000000000000000000000000..3685d9538b9e9c84ce007757bd3cbdd7eac09648
GIT binary patch
literal 8616
zcmeHMO-vI(6n?u~uqlh;44RLFv#*llzUBF((MNci`~y0wi!+cPwnO@eIm4PFo=92UST>!|VzzEQ~fZzWNI&41f?vU?VM
zxnsG&Y$@623twHqSKKQW4l--@kQc1)`~9C@F=DFkYZ`(F22lE6a$=Z-
zwFV(_Zlh2UA&t>YZLFNHsw1K66tWt#BB=pwC%l&CHFSMf168nyiKN#n=JmQ6BXa|{
zYZA4Yl;t1Kjda9a*UQ;X*0+i?P|ZayO<*gXos
zY=9`#hoSx)T8Plt&O_zvneZA^;?d4Gkwg}_fPdnB#JB|S!n17`->SlZh-wDer
z+OE^Jqs3u~+OCe)&ek0*qq&itUG1&y9jzUy{OzfOLx}|ChGlkH%56BqT;-CWcDgT+
z2%FZaief-9pcqgLC
+
+ 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;