[gdal-dev] How to change variable names in netCDF?

Etienne Tourigny etourigny.dev at gmail.com
Thu Apr 19 14:45:25 EDT 2012


Asuka,

I do not understand what is your intent - creating files from scratch,
copying from other format, using gdalwarp/gdal_translate?

If you are using the CreateCopy mechanism (like in gdal_translate),
then you can set the variable name in the Band-level metadata
NETCDF_VARNAME.  This is what is done by the driver when it opens a
netcdf file.

See netcdfdataset.cpp line 4477 and 4507:

        /* Get var name from NETCDF_VARNAME */
        tmpMetadata = poSrcBand->GetMetadataItem("NETCDF_VARNAME");
       	if( tmpMetadata != NULL) {
            if( nBands > 1 ) sprintf(szBandName,"%s%d",tmpMetadata,iBand);
            else strcpy( szBandName, tmpMetadata );
            // poSrcBand->SetMetadataItem("NETCDF_VARNAME","");
        }
        else
            szBandName[0]='\0';
[...]
        poBand = new netCDFRasterBand( poDS, eDT, iBand,
                                       bSignedData,
                                       szBandName, szLongName );

However the SetMetadata() function does not work for netcdfdataset,
due to differences between gdal metadata vs. netcdf metadata.

You can always call the netCDFRasterBand constructor with appropriate
arguments, but there is no api for netcdfdataset.

However, if you are creating a netcdf file manually you could always
modify Create() to accept a new creation option like
BAND_NAME=<new_name> but I don't see how you could deal with multiple
bands - maybe separate them like this BAND_NAME=var1,var2,var3

As I said - might be easier to use other tools after creating the file
with gdal.

Etienne

On Thu, Apr 19, 2012 at 11:09 AM,  <blaa_katt at yahoo.co.jp> wrote:
> Hello,
>
> Thank you for the reply, Etienne.
>
> If it is impossible with the gdal 1.9.0, I would like to implement some
> modifications.
> We found netCDF constructor in netcdfdataset.cpp.
> I think it is possible to modify the names by setting pszBandName as shown
> follows:
> ---------------------------------------------------
>
>     if ( !pszBandName || EQUAL(pszBandName,"")  )
>         sprintf( szTemp, "Band%d", nBand );
>     else
>         strcpy( szTemp, pszBandName );
>             CPLDebug( "GDAL_netCDF", "nc_def_var(%d,%s,%d)",
>                       cdfid, szTemp, nc_datatype );
>             status = nc_def_var( cdfid, szTemp, nc_datatype,
>                          NCDF_NBDIM, anBandDims, &nZId );
>
>
> But the course of the modification is unclear for me.
> Can anyone give me any advice which way is better,  (e.g. parsing of names
> through string options, making reserved metadata with a name, modification
> of Python APIs, etc)?
>
> Kind regards,
>
> Asuka
>
>
>
> Etienne Tourigny <etourigny.dev at gmail.com> wrote:
>
> If you use gdal-1.9 the driver will use the variable name from the
> original netcdf file (if any).
>
> for example, if you use the following command the original filename
> will be preserved.
> gdal_translate -of netcdf in.nc out.nc
>
> However, if copying from other formats (such as gtiff), or creating a
> new netcdf file, there is no way to change the variable name with
> gdal.
>
> You can use other tools such as:
> NCO -> nco.sourceforge.net/
> cdo ->https://code.zmaw.de/projects/cdo
>
> Etienne
>
> On Thu, Apr 19, 2012 at 5:15 AM,  <blaa_katt at yahoo.co.jp> wrote:
>> Hello everybody,
>>
>> I have a question about netCDF creation.
>> I am using GDAL 1.9.0 in python.
>>
>> Creation Issues in http://www.gdal.org/frmt_netcdf.html mentions
>> “each variable array is names Band1, Band2,… ”.
>>
>> I would like to change the names.
>> Can anyone give me ideas?
>>
>> Thank you for your time.
>>
>> Kind regards,
>>
>> Asuka
>>
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list