1010import org .jdom2 .input .SAXBuilder ;
1111import org .junit .Test ;
1212import ucar .ma2 .Array ;
13+ import ucar .nc2 .Attribute ;
14+ import ucar .nc2 .Group ;
1315import ucar .nc2 .NetcdfFile ;
1416import ucar .nc2 .Variable ;
1517import 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