5

I would like to make a copy of netcdf file using Python.

There are very nice examples of how to read or write netcdf-file, but perhaps there is also a good way how to make the input and then output of the variables to another file.

A good-simple method would be nice, in order to get the dimensions and dimension variables to the output file with the lowest cost.

msi_gerva
  • 2,021
  • 3
  • 22
  • 28
  • I am trying to use module netCDF4. – msi_gerva Dec 18 '12 at 17:32
  • No, I mean what have you tried in attempting to solve this question? What research have you done before you posted this? – Corey Adler Dec 18 '12 at 17:34
  • I attempted finding a solution in the stackoverflow and also in the web. – msi_gerva Dec 19 '12 at 11:45
  • And what did that come up with? People here are not going to just do the work for you. You'll need to show, in your questions, precisely what code or methods you have tried in order to get it working. You need to show us that you've exhausted all of your options before coming here for help. – Corey Adler Dec 19 '12 at 14:31

5 Answers5

8

I found the answer to this question at python netcdf: making a copy of all variables and attributes but one, but I needed to change it to work with my version of python/netCDF4 (Python 2.7.6/1.0.4). If you needed to add or subtract elements, you would make the appropriate modifications.

import netCDF4 as nc

def create_file_from_source(src_file, trg_file):
    src = nc.Dataset(src_file)
    trg = nc.Dataset(trg_file, mode='w')

    # Create the dimensions of the file
    for name, dim in src.dimensions.items():
        trg.createDimension(name, len(dim) if not dim.isunlimited() else None)

    # Copy the global attributes
    trg.setncatts({a:src.getncattr(a) for a in src.ncattrs()})

    # Create the variables in the file
    for name, var in src.variables.items():
        trg.createVariable(name, var.dtype, var.dimensions)

        # Copy the variable attributes
        trg.variables[name].setncatts({a:var.getncattr(a) for a in var.ncattrs()})

        # Copy the variables values (as 'f4' eventually)
        trg.variables[name][:] = src.variables[name][:]

    # Save the file
    trg.close()

create_file_from_source('in.nc', 'out.nc')

This snippet has been tested.

Community
  • 1
  • 1
5

If you want to only use the netCDF-4 API to copy any netCDF-4 file, even those with variables that use arbitrary user-defined types, that's a difficult problem. The netCDF4 module at netcdf4-python.googlecode.com currently lacks support for compound types that have variable-length members or variable-length types of a compound base type, for example.

The nccopy utility that is available with the netCDF-4 C distribution shows it is possible to copy an arbitrary netCDF-4 file using only the C netCDF-4 API, but that's because the C API fully supports the netCDF-4 data model. If you limit your goal to copying netCDF-4 files that only use flat types supported by the googlecode module, the algorithm used in nccopy.c should work fine and should be well-suited to a more elegant implementation in Python.

A less ambitious project that would be even easier is a Python program that would copy any netCDF "classic format" file, because the classic model supported by netCDF-3 has no user-defined types or recursive types. This program would even work for netCDF-4 classic model files that also use performance features such as compression and chunking.

Russ Rew
  • 51
  • 1
  • 2
2

Since I discovered xarray, this has been my go-to tool for everything python+netCDF related

You can easily copy a netcdf file, for example:

import xarray as xr

input = xr.open_dataset('ncfile.nc')
input.to_netcdf('copy_of_ncfile.nc')
igrolvr
  • 343
  • 4
  • 13
1

If you are on Linux or macOS, this can be achived easily with nctoolkit (https://nctoolkit.readthedocs.io/en/latest/installing.html).

import nctoolkit as nc
data = nc.open_data("infile.nc")
data.to_nc("outfile.nc")
Robert Wilson
  • 3,192
  • 11
  • 19
-1

see How do I copy a file in python?: a netcdf file is not different from any other file so it should fit your needs

Community
  • 1
  • 1
furins
  • 4,979
  • 1
  • 39
  • 57
  • The OP wanted to copy from one version of the format to another, which is very different from a copy of the file. Not to mention OpenDAP ... – Eli S Jun 10 '16 at 23:07
  • 2
    @EliS the OP specifies that he needs a *simple* way to *copy* (not compress, chunk, convert) a netcdf *file* not an OpenDAP url. Everything else is just speculation. – furins Aug 04 '16 at 04:48