Search code examples
pythonnetcdf4

netcdf4-python: Reading variable storage parameters


Im trying to manually copy all dimensions, variables and attribute of a NetCDF-file to a new file. As in copy netcdf file using python this works well, except for the storage parameters like 'fill_value' or 'chunksizes'. In ncdump -sch, these parameters are shown with a leading underscore (_):

    float var1(time, lat, lon) ;
        var1:_FillValue = NaNf ;
        var1:grid_mapping = "crs" ;
        var1:unit = "m" ;
        var1:_Storage = "chunked" ;
        var1:_ChunkSizes = 1, 14, 146 ;
        var1:_DeflateLevel = 9 ;
        var1:_Shuffle = "true" ;
        var1:_Endianness = "little" ;

In createVariable I can set these parameters for the new variable, but how can I obtain the parameters like 'fill_value' or 'chunksizes' from the existing file using the netcdf4-python module? What is the syntax to read those parameters?


Solution

  • For information about chunk sizes, you can use the chunking() method on variables. Unfortunately, it looks like you can only access _FillValue if it's been set to a non-default value:

    from netCDF4 import Dataset
    import numpy as np
    
    nc = Dataset('data.nc', 'w')
    nc.createDimension('t', 10)
    var = nc.createVariable('temp', 'f', ('t',), fill_value=80)
    var[:] = np.arange(10)
    nc.close()
    
    nc_read = Dataset('data.nc')
    temp = nc_read.variables['temp']
    print(temp.chunking())
    print(temp._FillValue)
    

    So right now it looks like the easiest way to handle fill value is:

    fill = getattr(temp, '_FillValue', mydefaultvalue)
    

    Might be worth opening an issue upstream on GitHub.