[gdal-dev] Creating NetCDF dataset with sub datasets

Pierre Chatelier pierre at chachatelier.fr
Tue Mar 12 06:16:29 PDT 2024


Le 12/03/2024 à 13:48, Even Rouault a écrit :
>
> Le 12/03/2024 à 13:33, Pierre Chatelier a écrit :
>>>
>>>> But I can't figure out how to customize the <band name>, which is 
>>>> Band1, Band2, Band3...
>>>> I tried GDALRasterBand::SetDescription(), but without success
>>>
>>> Looking at netcdfdataset.cpp line 9625, the netCDF variable name for 
>>> a band is extracted from the NETCDF_VARNAME band metadata item of 
>>> the source band.
>>
>>
>> Sounds good, doesn't work.
>
> It must be set on the *source* band, before creating CreateCopy(). 
> I've just tested it successfully with:

For now I still have two problems :
-it means I cannot work in-place in the final netCDF dataset, I have to 
use another dataset and use CreateCopy()
-for now  band->SetMetadataItem("NETCDF_VARNAME", "foo"); will generate 
a netCDF band name of "foo1" because there is something related to 
NETCDF_DIM_EXTRA that adds an index. (netcdfdatasetp.cpp:10507)

  I am investigating if I can force (even an empty) NETCDF_DIM_EXTRA. 
(but for now it does not work)

>
> $ gdal_translate byte.tif input.vrt
> $ python -c "from osgeo import gdal; ds = gdal.Open('input.vrt', 
> gdal.GA_Update); ds.GetRasterBand(1).SetMetadataItem('NETCDF_VARNAME', 
> 'my_varname'); ds.SetMetadataItem('my_varname#long_name', 'my long 
> name')"
> $ gdal_translate input.vrt tmp.nc
> $ ncdump -h tmp.nc | grep my_varname
>     byte my_varname(y, x) ;
>         my_varname:long_name = "my long name" ;
>         my_varname:_Unsigned = "true" ;
>         my_varname:valid_range = 0s, 255s ;
>         my_varname:grid_mapping = "transverse_mercator" ;
>
> Not super intuitive admitedly. This is mostly done in a way that 
> netCDF -> netCDF CreateCopy() preserve the variable name and long_name 
> attributes
>



More information about the gdal-dev mailing list