Project

General

Profile

Example of selgridcell using python bindings?

Added by Carlos Moffat over 6 years ago

Hi,

I'm trying to extract a particular variable from selected grid points from multiple NetCDF files. I'm new to CDO, but I believe the way to do this is in two steps, by first extracting the desired variables to individual files, and then merging them (example below):

cdo -selgridcell,640417,691450 -selname,temp  input_history_001.nc output_001.nc


and then
cod mergetime output*.nc


I have three questions:

1) Am I correct that I need to do this is two steps?

2) I tried running the above command within a python code that provides the input and output filenames to cdo, using subprocess.call (I also tried os.system()), but it's painfully slow (16 sec per file) vs running it directly from the shell (a very nice 1.7s). Any idea of why this would be?

3) I've read the python docs, but I don't quite understand how to implement the first command above using the python bindings. help(cdo.Cdo().selgridcell) is empty. Could anybody provide an example of how to do this?

Thanks,
Carlos


Replies (2)

RE: Example of selgridcell using python bindings? - Added by Carlos Moffat over 6 years ago

Erratum: the second command is meant to be cdo, not cod (It's lunchtime here).

RE: Example of selgridcell using python bindings? - Added by Karin Meier-Fleischer over 6 years ago

Hi Carlos,

1) No, you can use the operator chain in python, too. See [[https://code.mpimet.mpg.de/projects/cdo/wiki/Cdo%7Brbpy%7D#Operator-Chains]]
2+3) First, define the variables for the input and output file as usual in python.

from cdo import *

ifile = "mydata.nc" 
ofile = "outfile.nc" 

cdo = Cdo()     #-- make it easier

cdo.selgridcell('640417,691450',input="-select,name=u10,v10" + ifile, output=ofile)

-Karin

    (1-2/2)