Skip to content

Commit

Permalink
remove extra calls to read time axes
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyajohnson committed Nov 17, 2023
1 parent a61172a commit 8bb023e
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ protected void readValues() {
}
// make sure parent methods do not try to read from the orgVar again
this.wasRead = true;
if (orgVar instanceof CoordinateAxis1D) {
CoordinateAxis1D orgAxis = (CoordinateAxis1D) orgVar;
this.wasRead = orgAxis.wasRead;
this.coords = orgAxis.coords;
}
}

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -338,6 +343,8 @@ private CoordinateAxis1DTime(NetcdfDataset ncd, VariableDS org, Formatter errMes
List<CalendarDate> result = new ArrayList<>(ncoords);

Array data = org.read();
coords = (double[]) data.get1DJavaArray(DataType.DOUBLE);
this.wasRead = true;

int count = 0;
IndexIterator ii = data.getIndexIterator();
Expand Down
13 changes: 11 additions & 2 deletions cdm/core/src/main/java/ucar/nc2/dataset/NetcdfDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,10 @@ public ImmutableList<CoordinateAxis> getCoordinateAxes() {
return ImmutableList.copyOf(coordAxes);
}

public void setCoordinateAxes(List<CoordinateAxis> axes) {
this.coordAxes = axes;
}

/**
* Clear Coordinate System metadata, to allow them to be redone
*
Expand Down Expand Up @@ -1344,12 +1348,16 @@ public void addCoordinateTransform(CoordinateTransform ct) {
public CoordinateAxis addCoordinateAxis(VariableDS v) {
if (v == null)
return null;

final List<CoordinateAxis> coordCopy = new ArrayList<>(coordAxes);

CoordinateAxis oldVar = findCoordinateAxis(v.getFullName());
if (oldVar != null)
coordAxes.remove(oldVar);
coordCopy.remove(oldVar);

CoordinateAxis ca = (v instanceof CoordinateAxis) ? (CoordinateAxis) v : CoordinateAxis.factory(this, v);
coordAxes.add(ca);
coordCopy.add(ca);
this.coordAxes = coordCopy;

if (v.isMemberOfStructure()) {
Structure parentOrg = v.getParentStructure(); // gotta be careful to get the wrapping parent
Expand Down Expand Up @@ -1705,6 +1713,7 @@ public void replaceCoordinateAxis(Group.Builder group, CoordinateAxis.Builder ax
axis.setParentGroupBuilder(group);
}


public T setOrgFile(NetcdfFile orgFile) {
this.orgFile = orgFile;
return self();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ucar.nc2.internal.dataset.DatasetEnhancer;
import ucar.nc2.internal.ncml.NcmlReader;
import ucar.nc2.util.CancelTask;
import ucar.nc2.util.CompareNetcdf2;
import ucar.nc2.util.cache.FileCache;
import ucar.nc2.util.cache.FileCacheIF;
import ucar.nc2.util.cache.FileFactory;
Expand Down
3 changes: 3 additions & 0 deletions cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,9 @@ protected Builder<?> addLocalFieldsToBuilder(Builder<? extends Builder<?>> build
builder.setOriginalVariable(this.orgVar).setOriginalDataType(this.orgDataType).setOriginalName(this.orgName)
.setOriginalFileTypeId(this.orgFileTypeId).setEnhanceMode(this.enhanceMode).setUnits(this.enhanceProxy.units)
.setDesc(this.enhanceProxy.desc);
if (this.coordSysNames != null) {
this.coordSysNames.stream().forEach(s -> builder.addCoordinateSystemName(s));
}

return (VariableDS.Builder<?>) super.addLocalFieldsToBuilder(builder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private static CoverageCoordAxis makeCoordAxisFromDimension(Dimension dim) {

private static ucar.nc2.util.Optional<CoverageCoordAxis> makeCoordAxis(FeatureType ftype,
ucar.nc2.dataset.CoordinateAxis dtCoordAxis, DtCoverageAdapter reader) {
String name = dtCoordAxis.getFullName();
String name = dtCoordAxis.getFullName();
DataType dataType = dtCoordAxis.getDataType();
AxisType axisType = dtCoordAxis.getAxisType();
String units = dtCoordAxis.getUnitsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ucar.nc2.ft2.coverage.adapter;

import com.google.common.collect.Lists;
import thredds.client.catalog.builder.DatasetBuilder;
import ucar.nc2.Dimension;
import ucar.nc2.constants.AxisType;
import ucar.nc2.constants.FeatureType;
Expand Down Expand Up @@ -179,6 +180,7 @@ public static String describe(NetcdfDataset ds, CoordinateSystem cs, Formatter e

//////////////////////////////////////////////////////////////
// time
boolean axesChanged = false; // if any time axes are rebuilt by the factory, we need to update the netcdfDataset
CoordinateAxis rt = cs.findAxis(AxisType.RunTime);
if (rt != null) {
if (!rt.isScalar() && !(rt instanceof CoordinateAxis1D)) { // A runtime axis must be scalar or one-dimensional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import ucar.nc2.Attribute;
import ucar.nc2.Dimension;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;
import ucar.nc2.VariableSimpleIF;

import ucar.nc2.*;
import ucar.nc2.constants.CDM;
import ucar.nc2.constants.FeatureType;
import ucar.nc2.dataset.CoordinateAxis;
Expand Down Expand Up @@ -113,13 +110,11 @@ public DtCoverageDataset(NetcdfDataset ncd) throws IOException {
* @throws java.io.IOException on read error
*/
public DtCoverageDataset(NetcdfDataset ncd, Formatter parseInfo) throws IOException {
this.ncd = ncd;

Set<NetcdfDataset.Enhance> enhance = ncd.getEnhanceMode();
if (enhance == null || enhance.isEmpty()) {
enhance = NetcdfDataset.getDefaultEnhanceMode();
}
ncd = NetcdfDatasets.enhance(ncd, enhance, null);
this.ncd = NetcdfDatasets.enhance(ncd, enhance, null);

// sort by largest size first
List<CoordinateSystem> csList = new ArrayList<>(ncd.getCoordinateSystems());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ public List<CoordinateTransform> getCoordTransforms() {
return coordTransforms;
}

// make another constructor that takes a list of axes?
private CoordinatesHelper(Builder builder, NetcdfDataset ncd) {
List<CoordinateAxis> axes = new ArrayList<>();
addAxes(ncd.getRootGroup(), axes);
this.coordAxes = ImmutableList.copyOf(axes);
// if (builder.coordAxes == null || builder.coordAxes.isEmpty()) {
addAxes(ncd.getRootGroup(), axes);
// } else {
// axes = builder.coordAxes.stream().map(ct -> ct.build(ncd.getRootGroup())).filter(Objects::nonNull).collect(Collectors.toList());
// }
coordAxes = ImmutableList.copyOf(axes);

coordTransforms =
builder.coordTransforms.stream().map(ct -> ct.build(ncd)).filter(Objects::nonNull).collect(Collectors.toList());
Expand Down
4 changes: 4 additions & 0 deletions cdm/core/src/main/java/ucar/nc2/util/cache/FileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ private FileCacheable acquireCacheOnly(Object hashKey) {
return want.ncfile;
}

public void replace (Object oldHashKey, Object newHasKey){

}

// LOOK should you remove the entire CacheElement ?
private void remove(CacheElement.CacheFile want) {
want.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public void testStandaloneNoEnhanceDataset() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.SHORT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isFalse();
assertThat(scaledvar.readScalarShort()).isEqualTo(1);
}
}
Expand All @@ -72,8 +71,7 @@ public void testStandaloneEnhance() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.FLOAT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isFalse();
assertThat(scaledvar.readScalarFloat()).isEqualTo(12.0f);
}
}
Expand All @@ -91,8 +89,7 @@ public void testStandaloneEnhanceDataset() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.FLOAT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isFalse();
assertThat(scaledvar.readScalarFloat()).isEqualTo(12.0f);
}
}
Expand Down
9 changes: 3 additions & 6 deletions cdm/core/src/test/java/ucar/nc2/ncml/TestEnhance.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public void testStandaloneEnhance() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.FLOAT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isFalse();
assertThat(scaledvar.readScalarFloat()).isEqualTo(12.0f);
}
}
Expand All @@ -87,8 +86,7 @@ public void testStandaloneEnhanceDataset() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.FLOAT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isFalse();
assertThat(scaledvar.readScalarFloat()).isEqualTo(12.0f);
}
}
Expand All @@ -106,8 +104,7 @@ public void testStandaloneDoubleEnhanceDataset() throws IOException {
Variable scaledvar = ncfile.findVariable("scaledvar");
assertThat((Object) scaledvar).isNotNull();
assertThat(scaledvar.getDataType()).isEqualTo(DataType.FLOAT);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isTrue();
assertThat(scaledvar.attributes().findAttributeDouble("scale_factor", 1.0)).isEqualTo(2.0);
assertThat(scaledvar.attributes().hasAttribute("scale_factor")).isFalse();
assertThat(scaledvar.readScalarFloat()).isEqualTo(12.0f);
}
}
Expand Down

0 comments on commit 8bb023e

Please sign in to comment.