Skip to content

Commit

Permalink
netCDF CreateCopy(): fix taking into account NETCDF_DIM_EXTRA when so…
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Oct 14, 2024
1 parent d3dc839 commit c4b96d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
24 changes: 23 additions & 1 deletion autotest/gdrivers/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6542,7 +6542,7 @@ def test_netcdf_create_metadata_with_equal_sign(tmp_path):
# Test force opening a HDF55 file with netCDF driver


def test_netcdf_force_opening_hdf5_file(tmp_vsimem):
def test_netcdf_force_opening_hdf5_file():

ds = gdal.OpenEx("data/hdf5/groups.h5", allowed_drivers=["netCDF"])
assert ds.GetDriver().GetDescription() == "netCDF"
Expand All @@ -6559,3 +6559,25 @@ def test_netcdf_force_opening_no_match():

drv = gdal.IdentifyDriverEx("data/byte.tif", allowed_drivers=["netCDF"])
assert drv is None


###############################################################################


def test_netcdf_extra_dim_no_georef(tmp_path):

fname = tmp_path / "out.nc"

src_ds = gdal.Translate("", "../gcore/data/stefan_full_rgba.tif", format="MEM")
size_z = 4
src_ds.SetMetadataItem("NETCDF_DIM_EXTRA", "{Z}")
src_ds.SetMetadataItem("NETCDF_DIM_Z_DEF", f"{{{size_z},4}}")
src_ds.SetMetadataItem("NETCDF_DIM_Z_VALUES", "{0,1,2,3}")
src_ds.SetMetadataItem("Z#axis", "Z")

# Create netCDF file
gdal.GetDriverByName("netCDF").CreateCopy(fname, src_ds)

ds = gdal.Open(fname)
assert ds.RasterCount == 4
assert ds.ReadRaster() == src_ds.ReadRaster()
11 changes: 6 additions & 5 deletions frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9604,11 +9604,6 @@ netCDFDataset::CreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
pScaledProgress =
GDALCreateScaledProgress(0.1, 0.25, pfnProgress, pProgressData);
poDS->AddProjectionVars(false, GDALScaledProgress, pScaledProgress);
// Save X,Y dim positions.
panDimIds[nDim - 1] = poDS->nXDimID;
panBandDimPos[0] = nDim - 1;
panDimIds[nDim - 2] = poDS->nYDimID;
panBandDimPos[1] = nDim - 2;
GDALDestroyScaledProgress(pScaledProgress);
}
else
Expand All @@ -9622,6 +9617,12 @@ netCDFDataset::CreateCopy(const char *pszFilename, GDALDataset *poSrcDS,
}
}

// Save X,Y dim positions.
panDimIds[nDim - 1] = poDS->nXDimID;
panBandDimPos[0] = nDim - 1;
panDimIds[nDim - 2] = poDS->nYDimID;
panBandDimPos[1] = nDim - 2;

// Write extra dim values - after projection for optimization.
if (nDim > 2)
{
Expand Down

0 comments on commit c4b96d0

Please sign in to comment.