2

I can see the default chunking setting in netCDF4 library, but I have no idea how to change the chunk size.

from netCDF4 import Dataset    
volcgrp = Dataset('datasets/volcano.nc', 'r')
data = volcgrp.variables['abso4']
print data.shape
print data.chunking()
>(8, 96, 192)
>[1, 96, 192]

Is there anyone who can help with the setting?

American curl
  • 1,259
  • 2
  • 18
  • 21

2 Answers2

3

You can use xarray to read the netcdf file and set chunks, e.g.

import xarray as xr

ds = xr.open_dataset('/datasets/volcano.nc', chunks={'time': 10})
N1B4
  • 3,377
  • 1
  • 21
  • 24
3

It is a little unclear what you are trying to do. data.chunking() tells you the chunksize of the variable as stored in the file. If you would like to change this, you need to re-write the file on disk, setting the chunksize for each variable. With the netCDF4 library, you can do this with the chunksizes keword argument to netCDF4.CreateVariable(). Documentation found here:

http://unidata.github.io/netcdf4-python/#netCDF4.Dataset.createVariable

Tor
  • 658
  • 6
  • 19
  • there is a small but important typo here, the correct keyword is chunksizes and not chunksize. – pan May 18 '20 at 06:10
  • This should be the accepted answer I think. The chunks in the question refer to the chunks inside the netcdf-file, which is different from the chunks used by `xarray` as mentioned in the answer by @N1B4. In `xarray` you could see the "internal" chunks like this `ds["abso4"].encoding["chunksizes"]`. – Bert Coerver Aug 17 '22 at 09:15