|
1 | 1 | /* |
2 | | - * Copyright (c) 1998-2025 University Corporation for Atmospheric Research/Unidata |
| 2 | + * Copyright (c) 1998-2026 University Corporation for Atmospheric Research/Unidata |
3 | 3 | * See LICENSE for license information. |
4 | 4 | */ |
5 | 5 |
|
@@ -132,6 +132,36 @@ public static void useHdfEos(boolean val) { |
132 | 132 |
|
133 | 133 | public static void setDebugFlags(DebugFlags flags) {} |
134 | 134 |
|
| 135 | + private static boolean isLikelyNetcdf4(RandomAccessFile raf) { |
| 136 | + boolean likelyNc4 = false; |
| 137 | + // Check if likely netCDF-4 file (to prevent reading vanilla HDF5 through the netCDF-C library) |
| 138 | + // See https://github.com/Unidata/netcdf-c/discussions/3085 |
| 139 | + // Note: netCDF-4 files written outside the netCDF-C library may not pass these checks. |
| 140 | + IntByReference test_ncid = new IntByReference(); |
| 141 | + 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; |
| 157 | + } |
| 158 | + } |
| 159 | + if (!likelyNc4) { |
| 160 | + log.debug("May not be a netCDF-4 file: {}; falling back to HDF5 IOSP.", raf.getLocation()); |
| 161 | + } |
| 162 | + return likelyNc4; |
| 163 | + } |
| 164 | + |
135 | 165 | ////////////////////////////////////////////////// |
136 | 166 | // Instance Variables |
137 | 167 |
|
@@ -177,24 +207,32 @@ public void setChunker(Nc4Chunking chunker) { |
177 | 207 | @Override |
178 | 208 | public boolean isValidFile(RandomAccessFile raf) throws IOException { |
179 | 209 | int format = NCheader.checkFileType(raf); |
180 | | - boolean valid = false; |
| 210 | + boolean validCheck1 = false; |
181 | 211 | switch (format) { |
182 | 212 | case NCheader.NC_FORMAT_NETCDF4: |
183 | 213 | case NCheader.NC_FORMAT_64BIT_DATA: |
184 | | - valid = true; |
| 214 | + validCheck1 = true; |
185 | 215 | break; |
186 | 216 | default: |
187 | 217 | break;// everything else is invalid |
188 | 218 | } |
189 | | - if (valid) { |
190 | | - if (isClibraryPresent()) { |
191 | | - return true; |
| 219 | + |
| 220 | + boolean validCheck2 = false; |
| 221 | + if (!validCheck1) { |
| 222 | + log.debug("File cannot be opened by Nc4Iosp: {}", raf.getLocation()); |
| 223 | + } else if (!isClibraryPresent()) { |
| 224 | + log.debug("File appears to be valid but netCDF-C isn't installed: {}", raf.getLocation()); |
| 225 | + } else { |
| 226 | + // file appears to be valid and netCDF-c is present |
| 227 | + if (NetcdfClibrary.isStrictRead()) { |
| 228 | + // strictly limit to reading files that are very likely netcdf4 |
| 229 | + validCheck2 = isLikelyNetcdf4(raf); |
192 | 230 | } else { |
193 | | - log.debug("File is valid but the NetCDF-4 native library isn't installed: {}", raf.getLocation()); |
| 231 | + // try to read all HDF5 files through the netCDF-C library |
| 232 | + validCheck2 = true; |
194 | 233 | } |
195 | 234 | } |
196 | | - |
197 | | - return false; |
| 235 | + return validCheck2; |
198 | 236 | } |
199 | 237 |
|
200 | 238 | // 2016-06-06 note: Once netcdf-c v4.4.1 is released, we should be able to return much better information from |
|
0 commit comments