Skip to content

Commit 69d2e61

Browse files
committed
Only apply enhancements once for CoordinateAxis variables
Fixes #1436. Not perfect, breaks encapsulation, but maintains API.
1 parent dcfc9c4 commit 69d2e61

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
2+
* Copyright (c) 1998-2025 John Caron and University Corporation for Atmospheric Research/Unidata
33
* See LICENSE for license information.
44
*/
55
package ucar.nc2.dataset;
@@ -494,9 +494,28 @@ protected Array _read(Section section) throws IOException, InvalidRangeException
494494
// do not call directly
495495
@Override
496496
public Array reallyRead(Variable client, CancelTask cancelTask) throws IOException {
497-
if (orgVar == null)
497+
if (orgVar == null) {
498+
// possible aggregation
499+
if (this.proxyReader != null && this.proxyReader instanceof ucar.nc2.ncml.Aggregation) {
500+
return this.proxyReader.reallyRead(client, cancelTask);
501+
}
498502
return getMissingDataArray(shape);
503+
}
499504

505+
if (client instanceof CoordinateAxis) {
506+
// If orgVar is a VariableDS (orgVar of a CoordinateAxis using the non-builder
507+
// API, for example), call reallyRead on it, otherwise, enhancements will get
508+
// applied twice in a second _read() call triggered by calling read() on the
509+
// VariableDS.
510+
if (orgVar instanceof VariableDS && !orgVar.hasCachedData()) {
511+
return orgVar.reallyRead(client, cancelTask);
512+
}
513+
// Some aggregations use a DatasetProxyReader for reading a coordinate axis
514+
// variable, so check to see if that's what we have and, if so, use it.
515+
if (ucar.nc2.ncml.Aggregation.instanceOfDatasetProxyReader(this.proxyReader)) {
516+
return this.proxyReader.reallyRead(client, cancelTask);
517+
}
518+
}
500519
return orgVar.read();
501520
}
502521

@@ -508,9 +527,28 @@ public Array reallyRead(Variable client, Section section, CancelTask cancelTask)
508527
if ((null == section) || section.computeSize() == getSize())
509528
return reallyRead(client, cancelTask);
510529

511-
if (orgVar == null)
512-
return getMissingDataArray(section.getShape());
530+
if (orgVar == null) {
531+
// possible aggregation
532+
if (this.proxyReader != null && this.proxyReader instanceof ucar.nc2.ncml.Aggregation) {
533+
return this.proxyReader.reallyRead(client, section, cancelTask);
534+
}
535+
return getMissingDataArray(shape);
536+
}
513537

538+
if (client instanceof CoordinateAxis) {
539+
// If orgVar is a VariableDS (orgVar of a CoordinateAxis using the non-builder
540+
// API, for example), call reallyRead on it, otherwise, enhancements will get
541+
// applied twice in a second _read() call triggered by calling read() on the
542+
// VariableDS.
543+
if (orgVar instanceof VariableDS && !orgVar.hasCachedData()) {
544+
return orgVar.reallyRead(client, section, cancelTask);
545+
}
546+
// Some aggregations use a DatasetProxyReader for reading a coordinate axis
547+
// variable, so check to see if that's what we have and, if so, use it.
548+
if (ucar.nc2.ncml.Aggregation.instanceOfDatasetProxyReader(this.proxyReader)) {
549+
return this.proxyReader.reallyRead(client, section, cancelTask);
550+
}
551+
}
514552
return orgVar.read(section);
515553
}
516554

cdm/core/src/main/java/ucar/nc2/ncml/Aggregation.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998-2020 John Caron and University Corporation for Atmospheric Research/Unidata
2+
* Copyright (c) 1998-2025 John Caron and University Corporation for Atmospheric Research/Unidata
33
* See LICENSE.txt for license information.
44
*/
55
package ucar.nc2.ncml;
@@ -777,6 +777,11 @@ protected void setDatasetAcquireProxy(DatasetProxyReader proxy, Group g) throws
777777
}
778778
}
779779

780+
public static boolean instanceOfDatasetProxyReader(Object obj) {
781+
if (obj == null)
782+
return false;
783+
return obj instanceof DatasetProxyReader;
784+
}
780785

781786
protected class DatasetProxyReader implements ProxyReader {
782787
Dataset dataset;

0 commit comments

Comments
 (0)