Skip to content

Instantly share code, notes, and snippets.

@danwild
Last active September 8, 2020 12:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danwild/047e83a2e829966b2eba920911ecd36e to your computer and use it in GitHub Desktop.
Save danwild/047e83a2e829966b2eba920911ecd36e to your computer and use it in GitHub Desktop.
Crop a NetCDF file by XY, Z, and Time with CDO + python bindings
# setup
from cdo import *
cdo = Cdo()
cdo.debug = True
in_file = '/path/to/in_file.nc'
out_file = '/path/to/out_file.nc'
date1 = '2018-03-01T00:00:00'
date2 = '2018-03-01T02:00:00'
# my target is in meters
z_levels = '-1.5,-0.5'
bbox_str = '151.0,152.0,-23.8,-22.9'
# crop time dimension to range:
cdo.seldate(date1+','+date2, input = in_file, output = out_file, options ='-f nc')
# crop to target bbox
cdo.sellonlatbox(bbox_str, input = out_file, output = out_file, options ='-f nc')
# crop z to an array of target levels:
cdo.sellevel(z_levels, input = in_file, output = out_file, options ='-f nc')
# chaining multiple operations:
cdo.seldate(date1+','+date2, input = ' -sellonlatbox,'+bbox_str+' -sellevel,'+z_levels+' '+in_file, output = out_file, options = '-f nc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment