I would like to merge two netcdf files. The problem is that all the variables from the first file are contained in the second file (with different data) and the second file has some extra variables. So, I would like to get a netcdf file having all the variables of the second file with the data of the first one (when defined). Thank you all.
1 Answer
You should be able to handle this easily using nctoolkit (https://nctoolkit.readthedocs.io/en/latest/), as follows:
import nctoolkit as nc
# read in the files
ds = nc.open_data("infile1.nc")
ds2 = nc.open_data("infile2.nc")
# remove the 1st netcdf files variables from the second's
ds2.drop(ds.variables)
# merge the files
ds.append(ds2)
ds.merge()
# save the files as a netcdf file
ds.to_nc("merged.nc")
3 Comments
Ru9912
Hi, thank you Robert. I have tried your method but I have a "File "/cluster/home/rleblanc/python_venv/lib/python3.6/site-packages/nctoolkit/mergers.py", line 43, in merge raise TypeError("match supplied is not a list")" error for ds.merge(ds2). Do you know why there is that ?
Robert Wilson
My bad. I've fixed my answer
Robert Wilson
Potentially you will need to modify the match argument, as this checks the times in files are compatible