Compressing a netCDF file with the pynco package

955 views Asked by At

I'm interested in compressing a NetCDF file in python using the pynco package. From the command line, I would usually use:

nccopy -d 3 input_file.nc output_file.nc

In python, I have tried the following:

from nco import Nco
Nco.nccopy(input='input_file.nc', output='output_file.nc', options=['-d 1'])

This produces an attribute error 'Nco' has no attribute 'nccopy'. Is nccopy supported within pynco?

2

There are 2 answers

0
Charlie Zender On BEST ANSWER

nccopy is a netCDF command, not an NCO command. However, ncks can do nearly everything nccopy does, and a whole lot else. To compress as in your example, try

Nco.ncks(input='input_file.nc', output='output_file.nc', options=['-7 -L 1'])
0
ClimateUnboxed On

Just to list an alternative to pynco, you can also do this within python using the cdo package:

from cdo import *
cdo = Cdo()
cdo.copy(input='input.nc',output='output.nc',options='-f nc4 -z zip_7')

You can choose the compression level from light (zip_1) all the way up to most compressed (zip_9), I usually find that going above level 4 or 5 there is not much gain (i.e. additional size saving very minor) for a lot of pain (command takes a lot longer!). I think the -f nc4 to ensure netCDF4 output is perhaps not needed if the input is netCDF4 standard, but it doesn't hurt to put it anyway.

You can install cdo with

pip3 install cdo