Skip to content

Commit 1d3a440

Browse files
authored
Merge pull request OSGeo#11601 from rouault/GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES_fix
netCDF: fix 3.10.1RC1 regression regarding reporting of values of extra dimensions
2 parents bf234c3 + fc26671 commit 1d3a440

File tree

3 files changed

+112
-29
lines changed

3 files changed

+112
-29
lines changed
Binary file not shown.

autotest/gdrivers/netcdf.py

+90
Original file line numberDiff line numberDiff line change
@@ -6634,3 +6634,93 @@ def test_netcdf_var_with_geoloc_array_but_no_coordinates_attr():
66346634
got_md["X_DATASET"]
66356635
== 'NETCDF:"data/netcdf/var_with_geoloc_array_but_no_coordinates_attr.nc":lon'
66366636
)
6637+
6638+
6639+
###############################################################################
6640+
# Test reporting of values of an extra dimension which is unlimited
6641+
6642+
6643+
def test_netcdf_var_extra_dim_unlimited():
6644+
6645+
ds = gdal.Open("data/netcdf/extra_dim_unlimited.nc")
6646+
assert ds.GetMetadataItem("NETCDF_DIM_time_VALUES") == "{17927,17955}"
6647+
6648+
with gdal.config_option("GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES", "YES"):
6649+
ds = gdal.Open("data/netcdf/extra_dim_unlimited.nc")
6650+
assert ds.GetMetadataItem("NETCDF_DIM_time_VALUES") == "{17927,17955}"
6651+
6652+
with gdal.config_option("GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES", "NO"):
6653+
ds = gdal.Open("data/netcdf/extra_dim_unlimited.nc")
6654+
assert ds.GetMetadataItem("NETCDF_DIM_time_VALUES") is None
6655+
6656+
6657+
###############################################################################
6658+
# Test reporting of values of an extra dimension which is unlimited
6659+
6660+
6661+
@pytest.mark.require_curl()
6662+
@pytest.mark.skipif(sys.platform != "linux", reason="Incorrect platform")
6663+
def test_netcdf_var_extra_dim_unlimited_network():
6664+
6665+
import webserver
6666+
6667+
webserver_process = None
6668+
webserver_port = 0
6669+
6670+
(webserver_process, webserver_port) = webserver.launch(
6671+
handler=webserver.DispatcherHttpHandler
6672+
)
6673+
if webserver_port == 0:
6674+
pytest.skip()
6675+
6676+
filename = "data/netcdf/extra_dim_unlimited.nc"
6677+
6678+
gdal.VSICurlClearCache()
6679+
6680+
try:
6681+
filesize = gdal.VSIStatL(filename).size
6682+
handler = webserver.SequentialHandler()
6683+
handler.add("HEAD", "/test.nc", 200, {"Content-Length": "%d" % filesize})
6684+
6685+
def method(request):
6686+
# sys.stderr.write('%s\n' % str(request.headers))
6687+
6688+
if request.headers["Range"].startswith("bytes="):
6689+
rng = request.headers["Range"][len("bytes=") :]
6690+
assert len(rng.split("-")) == 2
6691+
start = int(rng.split("-")[0])
6692+
end = int(rng.split("-")[1])
6693+
6694+
request.protocol_version = "HTTP/1.1"
6695+
request.send_response(206)
6696+
request.send_header("Content-type", "application/octet-stream")
6697+
request.send_header(
6698+
"Content-Range", "bytes %d-%d/%d" % (start, end, filesize)
6699+
)
6700+
request.send_header("Content-Length", end - start + 1)
6701+
request.send_header("Connection", "close")
6702+
request.end_headers()
6703+
with open(filename, "rb") as f:
6704+
f.seek(start, 0)
6705+
request.wfile.write(f.read(end - start + 1))
6706+
6707+
handler.add("GET", "/test.nc", custom_method=method)
6708+
6709+
handler.add("HEAD", "/test.nc.aux.xml", 404)
6710+
handler.add("GET", "/", 404)
6711+
handler.add("HEAD", "/test.aux", 404)
6712+
handler.add("HEAD", "/test.AUX", 404)
6713+
handler.add("HEAD", "/test.nc.aux", 404)
6714+
handler.add("HEAD", "/test.nc.AUX", 404)
6715+
6716+
with webserver.install_http_handler(handler):
6717+
with gdal.quiet_errors():
6718+
ds = gdal.Open("/vsicurl/http://127.0.0.1:%d/test.nc" % webserver_port)
6719+
if ds is None:
6720+
pytest.skip("cannot open /vsicurl/ netCDF file")
6721+
assert ds.GetMetadataItem("NETCDF_DIM_time_VALUES") is None
6722+
6723+
finally:
6724+
webserver.server_stop(webserver_process, webserver_port)
6725+
6726+
gdal.VSICurlClearCache()

frmts/netcdf/netcdfdataset.cpp

+22-29
Original file line numberDiff line numberDiff line change
@@ -8861,40 +8861,33 @@ GDALDataset *netCDFDataset::Open(GDALOpenInfo *poOpenInfo)
88618861
// dimension in its NETCDF_DIM_xxxx band metadata item
88628862
// Addresses use case of
88638863
// https://lists.osgeo.org/pipermail/gdal-dev/2023-May/057209.html
8864+
const bool bIsLocal =
8865+
VSIIsLocal(osFilenameForNCOpen.c_str());
88648866
bool bListDimValues =
8865-
lev_count == 1 ||
8867+
bIsLocal || lev_count == 1 ||
88668868
!NCDFIsUnlimitedDim(poDS->eFormat ==
88678869
NCDF_FORMAT_NC4,
88688870
cdfid, poDS->m_anDimIds[j]);
8869-
if (!bListDimValues &&
8870-
!VSIIsLocal(osFilenameForNCOpen.c_str()))
8871+
const char *pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES =
8872+
CPLGetConfigOption(
8873+
"GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES", nullptr);
8874+
if (pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES)
88718875
{
8872-
const char *pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES =
8873-
CPLGetConfigOption(
8874-
"GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES",
8875-
nullptr);
8876-
if (!pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES)
8877-
{
8878-
if (!bREPORT_EXTRA_DIM_VALUESWarningEmitted)
8879-
{
8880-
bREPORT_EXTRA_DIM_VALUESWarningEmitted =
8881-
true;
8882-
CPLDebug(
8883-
"GDAL_netCDF",
8884-
"Listing extra dimension values is "
8885-
"skipped because this dataset is "
8886-
"hosted on a network file system, and "
8887-
"such an operation could be slow. If "
8888-
"you still want to proceed, set the "
8889-
"GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES "
8890-
"configuration option to YES");
8891-
}
8892-
}
8893-
else
8894-
{
8895-
bListDimValues = CPLTestBool(
8896-
pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES);
8897-
}
8876+
bListDimValues = CPLTestBool(
8877+
pszGDAL_NETCDF_REPORT_EXTRA_DIM_VALUES);
8878+
}
8879+
else if (!bListDimValues && !bIsLocal &&
8880+
!bREPORT_EXTRA_DIM_VALUESWarningEmitted)
8881+
{
8882+
bREPORT_EXTRA_DIM_VALUESWarningEmitted = true;
8883+
CPLDebug(
8884+
"GDAL_netCDF",
8885+
"Listing extra dimension values is skipped "
8886+
"because this dataset is hosted on a network "
8887+
"file system, and such an operation could be "
8888+
"slow. If you still want to proceed, set the "
8889+
"GDAL_NETCDF_REPORT_EXTRA_DIM_VALUES "
8890+
"configuration option to YES");
88988891
}
88998892
if (bListDimValues)
89008893
{

0 commit comments

Comments
 (0)