1

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 1

1

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")
Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
My bad. I've fixed my answer
Potentially you will need to modify the match argument, as this checks the times in files are compatible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.