1

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?

Ludwig
  • 123
  • 3

2 Answers2

1

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.

DopplerShift
  • 5,472
  • 1
  • 21
  • 20
0

Thank you, that works like a charm! I've solved the handling of an undefined _FillValue now like:

try: fillVal = variable._FillValue
except: fillVal = netCDF4.default_fillvals[str(variable.dtype.kind)+str(variable.dtype.itemsize)]

Looks a bit complicated but it seems that dtype has no output format method for the expected input for default_fillvals

Ludwig
  • 123
  • 3