Skip to content

Commit 36bc385

Browse files
authored
Merge pull request #1567 from lesserwhirls/nc4iosp
Make sure to release netCDF-C resources when IOSP is in strict mode
2 parents 5f5bb97 + 33116bd commit 36bc385

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

netcdf4/src/main/java/ucar/nc2/jni/netcdf/Nc4Iosp.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,29 @@ private static boolean isLikelyNetcdf4(RandomAccessFile raf) {
139139
// Note: netCDF-4 files written outside the netCDF-C library may not pass these checks.
140140
IntByReference test_ncid = new IntByReference();
141141
int ret = nc4.nc_open(raf.getLocation(), NC_NOWRITE, test_ncid);
142-
SizeTByReference lenp = new SizeTByReference();
143-
// Does _NCProperties global attribute exist?
144-
ret = nc4.nc_inq_attlen(test_ncid.getValue(), NC_GLOBAL, "_NCCCCC", lenp);
145-
// ret = nc4.nc_inq_attlen(test_ncid.getValue(), NC_GLOBAL, CDM.NCPROPERTIES, lenp);
146-
likelyNc4 = ret == NC_NOERR;
147-
if (!likelyNc4) {
148-
// Does _IsNetcdf4 global attribute exist, and is its value not 0?
149-
// note: newer versions of netCDF-C include the _NCProperties check as part of determining
150-
// the value of this attribute, but not all, so we need both checks.
151-
lenp = new SizeTByReference();
152-
ret = nc4.nc_inq_attlen(test_ncid.getValue(), NC_GLOBAL, CDM.ISNETCDF4, lenp);
153-
if (ret == NC_NOERR && lenp.getValue().longValue() == 1) {
154-
int[] isnc4 = new int[1];
155-
ret = nc4.nc_get_att_int(test_ncid.getValue(), NC_GLOBAL, CDM.ISNETCDF4, isnc4);
156-
likelyNc4 = ret == NC_NOERR && isnc4[0] != 0;
142+
if (ret == NC_NOERR) {
143+
// netCDF-C could open the file, now perform checks to see if file is likely netCDF-4.
144+
SizeTByReference lenp = new SizeTByReference();
145+
// Does _NCProperties global attribute exist?
146+
ret = nc4.nc_inq_attlen(test_ncid.getValue(), NC_GLOBAL, CDM.NCPROPERTIES, lenp);
147+
likelyNc4 = ret == NC_NOERR;
148+
if (!likelyNc4) {
149+
// _NCProperties global attribute not found.
150+
// Does _IsNetcdf4 global attribute exist, and is its value not 0?
151+
// note: newer versions of netCDF-C include the _NCProperties check as part of determining
152+
// the value of this attribute, but not all, so we need both checks.
153+
lenp = new SizeTByReference();
154+
ret = nc4.nc_inq_attlen(test_ncid.getValue(), NC_GLOBAL, CDM.ISNETCDF4, lenp);
155+
if (ret == NC_NOERR && lenp.getValue().longValue() == 1) {
156+
int[] isnc4 = new int[1];
157+
ret = nc4.nc_get_att_int(test_ncid.getValue(), NC_GLOBAL, CDM.ISNETCDF4, isnc4);
158+
likelyNc4 = ret == NC_NOERR && isnc4[0] != 0;
159+
}
157160
}
158-
}
159-
if (!likelyNc4) {
160-
log.debug("May not be a netCDF-4 file: {}; falling back to HDF5 IOSP.", raf.getLocation());
161+
if (!likelyNc4) {
162+
log.debug("May not be a netCDF-4 file: {}; falling back to HDF5 IOSP.", raf.getLocation());
163+
}
164+
nc4.nc_close(test_ncid.getValue());
161165
}
162166
return likelyNc4;
163167
}

0 commit comments

Comments
 (0)