Update a small slice of a large netcdf file without overwriting the entire file.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
MCVE Code Sample
# Your code here
orig = '/tmp/orig.h5'
ii = 100000
data = xr.Dataset(
{
'x':('t',np.random.randn(ii)),
'y':('t',np.random.randn(ii))
},
coords={'t':range(ii)}
)
# function to save the large file usnig chunksizes
def save(ds,path,**kwargs):
dvars = ds.variables
chunksize = 100
var_dic = {}
for var in dvars:
var_dic[var]={
'chunksizes': (chunksize,)
}
delayed =ds.to_netcdf(path,encoding=var_dic,**kwargs)
save(data,orig)
data.close()
#open the file, using dask
data_1 = xr.open_mfdataset([orig],chunks={'t':100})
#Change variable x
data_1['x']=data_1['x']+20
data_1.close()
#update only variable x. This works!
data_1['x'].to_netcdf(orig,mode='a')
# try the same but now update only a slice of the x variable
#open the file, using dask
data_1 = xr.open_mfdataset(orig,chunks={'t':100})
#Change variable x
data_1['x']=data_1['x']+20
data_1.close()
#update only variable x. this doesnt work!
data_1['x'][{'t':slice(0,10)}].to_netcdf(orig,mode='a')
Expected Output
Problem Description
Hi,
I have a large dataset that does not fit in memory.
Lets say i only want to update a small portion of it. Is there any way to update this small portion without having to rewrite the entire file.
I was fiddling around and found a way to update one variable at a time, but i want to be able to update only a subsection of this variable
Output of xr.show_versions()
xarray: 0.12.3
pandas: 0.25.1
numpy: 1.17.1
scipy: 1.3.1
netCDF4: 1.5.1.2
pydap: None
h5netcdf: 0.7.4
h5py: 2.9.0
Nio: None
zarr: None
cftime: 1.0.3.4
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.3.0
distributed: None
matplotlib: 3.1.1
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
setuptools: 41.2.0
pip: 19.2.3
conda: 4.7.11
pytest: 4.5.0
IPython: 7.8.0
sphinx: 2.2.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the MCVE's Dataset.to_netcdf calls, especially the indexed DataArray slice written with mode='a', and inspect how open_mfdataset and the netCDF4 backend handle writes. Done means updating only the requested slice of a large netCDF variable without rewriting the entire file, with coverage for the demonstrated case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100