Skip to content

Commit

Permalink
Make DataReader implements AutoClosable
Browse files Browse the repository at this point in the history
Make DataReader implements AutoClosable. Implement close and NetcdfDataReader. Call done() on DSS accessors. Make TemporalDataReader implements AutoClosable. Update NetcdfDataReader resources to close properly. Update NetcdfDataWriterTest and TemporalDataReaderTest to close resources.

(cherry picked from commit 7274aee)
  • Loading branch information
Nick Van committed Oct 10, 2024
1 parent 1522471 commit 86f6dcb
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,9 @@ public List<VortexDataInterval> getDataIntervals() {
.map(VortexDataInterval::of)
.toList();
}

@Override
public void close() throws Exception {
// No op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@ public List<VortexDataInterval> getDataIntervals() {
.toList();
}

@Override
public void close() throws Exception {
// No op
}

} // AscZipDataReader class
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,9 @@ public List<VortexDataInterval> getDataIntervals() {
.map(VortexDataInterval::of)
.toList();
}

@Override
public void close() throws Exception {
// No op
}
} // BilDataReader class
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,8 @@ public List<VortexDataInterval> getDataIntervals() {
.toList();
}

@Override
public void close() throws Exception {
// No op
}
} // BilZipDataReader class
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;
import java.util.Set;

public abstract class DataReader {
public abstract class DataReader implements AutoCloseable {
final PropertyChangeSupport support;

final String path;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package mil.army.usace.hec.vortex.io;

import hec.heclib.dss.DSSPathname;
import hec.heclib.dss.DssDataType;
import hec.heclib.dss.HecDSSDataAttributes;
import hec.heclib.dss.HecDssCatalog;
import hec.heclib.dss.*;
import hec.heclib.grid.GridData;
import hec.heclib.grid.GridInfo;
import hec.heclib.grid.GridUtilities;
Expand Down Expand Up @@ -43,6 +40,7 @@ private static List<DSSPathname> getCatalogPathnames(String path, String variabl
HecDssCatalog hecDssCatalog = new HecDssCatalog();
hecDssCatalog.setDSSFileName(path);
String[] dssPathnames = hecDssCatalog.getCatalog(true, variableName);
hecDssCatalog.done();

return Arrays.stream(dssPathnames).map(DSSPathname::new).toList();
}
Expand Down Expand Up @@ -73,6 +71,8 @@ private static GridData retrieveGriddedData(String dssFileName, String dssPathna
}
} catch (Exception e) {
return null;
} finally {
griddedData.done();
}

return gridData;
Expand Down Expand Up @@ -185,6 +185,8 @@ public static Set<String> getVariables(String pathToDss){
griddedRecords.add(dssPathname);
}

attributes.done();

return new HashSet<>(griddedRecords);
}

Expand Down Expand Up @@ -228,4 +230,9 @@ public List<VortexDataInterval> getDataIntervals() {
.map(VortexDataInterval::of)
.toList();
}

@Override
public void close() throws Exception {
// No op - done() is called on accessors after an opening call is made
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public List<VortexDataInterval> getDataIntervals() {
return timeBounds;
}

@Override
public void close() throws Exception {
gridDataset.close();
}

/* Grid Data Helpers */
private List<VortexData> getMultiDimensionGrids(Dimension timeDim, Dimension endDim, Dimension rtDim) {
List<VortexData> gridList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
import javax.measure.Unit;
import javax.measure.UnitConverter;
import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

abstract class NetcdfDataReader extends DataReader {
private static final Logger logger = Logger.getLogger(NetcdfDataReader.class.getName());
final NetcdfDataset ncd;

/* Factory Method */
public static NetcdfDataReader createInstance(String pathToFile, String pathToData) {
Expand All @@ -42,15 +38,14 @@ public static NetcdfDataReader createInstance(String pathToFile, String pathToDa

VariableDS variableDS = getVariableDataset(dataset, pathToData);
if (variableDS != null) {
return new VariableDsReader(variableDS, pathToData);
return new VariableDsReader(dataset, variableDS, pathToData);
}

return null;
}

NetcdfDataReader(DataReaderBuilder builder) {
super(builder);
ncd = getNetcdfDataset(path);
}

@Override
Expand All @@ -59,29 +54,6 @@ public static NetcdfDataReader createInstance(String pathToFile, String pathToDa
@Override
public abstract VortexData getDto(int idx);

@Override
public List<VortexDataInterval> getDataIntervals() {
if (!(ncd.findCoordinateAxis(AxisType.Time) instanceof CoordinateAxis1D timeAxis)) {
return Collections.emptyList();
}

String timeAxisUnits = timeAxis.getUnitsString();

int count = (int) timeAxis.getSize();
double[] startTimes = timeAxis.getBound1();
double[] endTimes = timeAxis.getBound2();

List<VortexDataInterval> timeRecords = new ArrayList<>();
for (int i = 0; i < count; i++) {
ZonedDateTime startTime = parseTime(timeAxisUnits, startTimes[i]);
ZonedDateTime endTime = parseTime(timeAxisUnits, endTimes[i]);
VortexDataInterval timeRecord = VortexDataInterval.of(startTime, endTime);
timeRecords.add(timeRecord);
}

return timeRecords;
}

@Override
public abstract int getDtoCount();

Expand Down Expand Up @@ -145,20 +117,6 @@ private static boolean isLatLon(NetcdfDataset ncd) {
return ncd.findCoordinateAxis(AxisType.Lon) != null && ncd.findCoordinateAxis(AxisType.Lat) != null;
}

private ZonedDateTime parseTime(String timeAxisUnits, double timeValue) {
String[] split = timeAxisUnits.split("since");
String chronoUnitStr = split[0].trim().toUpperCase(Locale.ROOT);
String dateTimeStr = split[1].trim();
ChronoUnit chronoUnit = ChronoUnit.valueOf(chronoUnitStr);
ZonedDateTime origin = TimeConverter.toZonedDateTime(dateTimeStr);
if (origin == null) return undefinedTime();
return origin.plus((long) timeValue, chronoUnit);
}

private ZonedDateTime undefinedTime() {
return ZonedDateTime.of(0, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));
}

private static boolean isSelectableVariable(Variable variable) {
boolean isVariableDS = variable instanceof VariableDS;
boolean isNotAxis = !(variable instanceof CoordinateAxis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,8 @@ public static String getVariable(String pathToDat) {
return getName(parseFile(pathToDat.substring(0, pathToDat.lastIndexOf(".dat"))));
}

@Override
public void close() throws Exception {
// No op
}
} // SnodasDataReader class
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,9 @@ private File createHeader(String directoryPath, String headerName) throws IOExce

return headerFile;
} // createHeader()

@Override
public void close() throws Exception {
// No op
}
} // SnodasTarDataReader class
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* data from a given data source. It supports various operations like reading data
* for a specific time range and getting grid definitions.
*/
public class TemporalDataReader {
public class TemporalDataReader implements AutoCloseable {
private final DataReader dataReader;
private final RecordIndexQuery recordIndexQuery;
private final VortexGrid baseGrid;
Expand Down Expand Up @@ -303,4 +303,9 @@ private static Comparator<VortexGrid> prioritizeLessNoDataComparator() {
return Integer.compare(o1Count, o2Count);
};
}

@Override
public void close() throws Exception {
dataReader.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.logging.Logger;

class VariableDsReader extends NetcdfDataReader {
private static final Logger logger = Logger.getLogger(VariableDsReader.class.getName());

private final NetcdfDataset ncd;
private final VariableDS variableDS;
private final Grid gridDefinition;
private final List<VortexDataInterval> timeBounds;

/* Constructor */
public VariableDsReader(VariableDS variableDS, String variableName) {
public VariableDsReader(NetcdfDataset ncd, VariableDS variableDS, String variableName) {
super(new DataReaderBuilder().path(variableDS.getDatasetLocation()).variable(variableName));
this.ncd = ncd;
this.variableDS = getVariableDS(ncd, variableName);
CoordinateSystem coordinateSystem = getCoordinateSystem(variableDS);
this.gridDefinition = getGridDefinition(ncd, coordinateSystem);
Expand Down Expand Up @@ -82,7 +82,44 @@ public int getDtoCount() {
return 1;
}

@Override
public List<VortexDataInterval> getDataIntervals() {
if (!(ncd.findCoordinateAxis(AxisType.Time) instanceof CoordinateAxis1D timeAxis)) {
return Collections.emptyList();
}

String timeAxisUnits = timeAxis.getUnitsString();

int count = (int) timeAxis.getSize();
double[] startTimes = timeAxis.getBound1();
double[] endTimes = timeAxis.getBound2();

List<VortexDataInterval> timeRecords = new ArrayList<>();
for (int i = 0; i < count; i++) {
ZonedDateTime startTime = parseTime(timeAxisUnits, startTimes[i]);
ZonedDateTime endTime = parseTime(timeAxisUnits, endTimes[i]);
VortexDataInterval timeRecord = VortexDataInterval.of(startTime, endTime);
timeRecords.add(timeRecord);
}

return timeRecords;
}

/* Helpers */
private ZonedDateTime parseTime(String timeAxisUnits, double timeValue) {
String[] split = timeAxisUnits.split("since");
String chronoUnitStr = split[0].trim().toUpperCase(Locale.ROOT);
String dateTimeStr = split[1].trim();
ChronoUnit chronoUnit = ChronoUnit.valueOf(chronoUnitStr);
ZonedDateTime origin = TimeConverter.toZonedDateTime(dateTimeStr);
if (origin == null) return undefinedTime();
return origin.plus((long) timeValue, chronoUnit);
}

private ZonedDateTime undefinedTime() {
return ZonedDateTime.of(0, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));
}

private List<VortexDataInterval> getTimeBounds() {
CoordinateAxis1D timeAxis = getTimeAxis();
if (timeAxis == null) return Collections.emptyList();
Expand Down Expand Up @@ -262,6 +299,11 @@ private static Grid getGridDefinition(NetcdfDataset ncd, CoordinateSystem coordi

return gridDefinition;
}

@Override
public void close() throws Exception {
ncd.close();
}
}


Loading

0 comments on commit 86f6dcb

Please sign in to comment.