Skip to content

Commit

Permalink
Merge pull request #9657 from dbaston/netcdf-band-names
Browse files Browse the repository at this point in the history
netCDF: Add BAND_NAMES creation option
  • Loading branch information
rouault authored Apr 15, 2024
2 parents d99b77d + dcfdfa9 commit cc83da7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
24 changes: 24 additions & 0 deletions autotest/gdrivers/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6489,3 +6489,27 @@ def test_gdal_subdataset_bogus(bogus):
"""Test it doesn't crash"""

gdal.GetSubdatasetInfo(bogus)


@gdaltest.enable_exceptions()
def test_band_names_creation_option(tmp_path):

fname = tmp_path / "twobands.nc"

# 1 band, 2 names
with pytest.raises(Exception, match="but 2 names provided"):
gdal.GetDriverByName("NetCDF").Create(
fname, 1, 1, 1, options={"BAND_NAMES": "t2m,prate"}
)

# 2 bands, 2 names
with gdal.GetDriverByName("NetCDF").Create(
fname, 1, 1, 2, options={"BAND_NAMES": "t2m,prate"}
):
pass

with gdal.Open(fname) as ds:
sds_names = [sds[0] for sds in ds.GetSubDatasets()]

assert gdal.GetSubdatasetInfo(sds_names[0]).GetSubdatasetComponent() == "t2m"
assert gdal.GetSubdatasetInfo(sds_names[1]).GetSubdatasetComponent() == "prate"
6 changes: 6 additions & 0 deletions doc/source/drivers/raster/netcdf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ Creation Options
installations, but NC4 and NC4C are available if NetCDF was compiled
with NetCDF-4 (and HDF5) support.

- .. co:: BAND_NAMES
:default: Band1,Band2,...
:since: 3.9.0

A comma-separated list of band names.

- .. co:: COMPRESS
:choices: NONE, DEFLATE

Expand Down
26 changes: 22 additions & 4 deletions frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9156,7 +9156,6 @@ netCDFDataset *netCDFDataset::CreateLL(const char *pszFilename, int nXSize,

return poDS;
}

// Create the dataset.
CPLString osFilenameForNCCreate(pszFilename);
#if defined(_WIN32) && !defined(NETCDF_USES_UTF8)
Expand Down Expand Up @@ -9321,12 +9320,31 @@ GDALDataset *netCDFDataset::Create(const char *pszFilename, int nXSize,
: GDAL_DEFAULT_NCDF_CONVENTIONS);
}

CPLStringList aosBandNames;
if (const char *pszBandNames = aosOptions.FetchNameValue("BAND_NAMES"))
{
aosBandNames =
CSLTokenizeString2(pszBandNames, ",", CSLT_HONOURSTRINGS);

if (aosBandNames.Count() != nBandsIn)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Attempted to create netCDF with %d bands but %d names "
"provided in BAND_NAMES.",
nBandsIn, aosBandNames.Count());
return nullptr;
}
}

// Define bands.
for (int iBand = 1; iBand <= nBandsIn; iBand++)
{
poDS->SetBand(
iBand, new netCDFRasterBand(netCDFRasterBand::CONSTRUCTOR_CREATE(),
poDS, eType, iBand, poDS->bSignedData));
const char *pszBandName =
aosBandNames.empty() ? nullptr : aosBandNames[iBand - 1];

poDS->SetBand(iBand, new netCDFRasterBand(
netCDFRasterBand::CONSTRUCTOR_CREATE(), poDS,
eType, iBand, poDS->bSignedData, pszBandName));
}

CPLDebug("GDAL_netCDF", "netCDFDataset::Create(%s, ...) done", pszFilename);
Expand Down
1 change: 1 addition & 0 deletions frmts/netcdf/netcdfdrivercore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ void netCDFDriverSetCommonMetadata(GDALDriver *poDriver)
"description='Path to a XML configuration file (or content inlined)'/>"
" <Option name='WRITE_GDAL_VERSION' type='boolean' default='YES'/>"
" <Option name='WRITE_GDAL_HISTORY' type='boolean' default='YES'/>"
" <Option name='BAND_NAMES' type='string' scope='raster' />"
"</CreationOptionList>");
poDriver->SetMetadataItem(GDAL_DMD_SUBDATASETS, "YES");

Expand Down

0 comments on commit cc83da7

Please sign in to comment.