Project

General

Profile

CDO automation in Python using a function or txt file

Added by Froede Vrolijk almost 8 years ago

Hi all,

I have a question about process automation of cdo commands using the CDO module in Python. I need to generate a lot of maps and time series with many different settings. By this I mean:
- Year / Time period (e.g. 1985, 1990 – 2010 etc.
- Statistical value to be calculated (sum, mean, standard deviation etc.)
- Use of daily, monthly or yearly data

I want to create a function that takes several arguments, for example:
stat=’mean’
year=’1990’
timestep=’mon’

As an example, with the (function) inputs as shown below, I could generate an output that may be directly usable as a command in Python.
sel_years = '1995'
sel_timestep = 'year'

sel_cdo_input = 'sel'+''.join(sel_timestep)
sel_test = 'cdo.'+sel_cdo_input+'('+sel_years+', input=ifile, output=ofile)'

Output:
'cdo.selyear(1995, input=ifile, output=ofile)'

In this way, I can hopefully easily adapt the input to get e.g. another year or use monthly data instead of yearly data.

The object type of this output is a string. It is possible to pass this string as an input to the cdo Python module, to automatically generate an output for these specific inputs?

Alternatively, if this is not possible, could I generate a .txt file with all the specific commands, and read them successively using cdo?

Thank you.
Regards,
Froede


Replies (4)

RE: CDO automation in Python using a function or txt file - Added by Ralf Mueller almost 8 years ago

Froede Vrolijk wrote:

Hi all,
[ ... ]
Output:
'cdo.selyear(1995, input=ifile, output=ofile)'

In this way, I can hopefully easily adapt the input to get e.g. another year or use monthly data instead of yearly data.

The object type of this output is a string. It is possible to pass this string as an input to the cdo Python module, to automatically generate an output for these specific inputs?

don't get me wrong, but: have you ever programmed in python?

Alternatively, if this is not possible, could I generate a .txt file with all the specific commands, and read them successively using cdo?

cdo is not a command interpreter given in text files. you can write a shell script by just write the commands (which you would type in a terminal) into a text file and execute it with the shell-interpreter, you're using.

Please, don't be offended by my strictness - but in order to give you the best support, I have to know, what you're familiar with.

I think,you first should decide, in which environment you want to program.

cheers
ralf

RE: CDO automation in Python using a function or txt file - Added by Oliver Angelil almost 8 years ago

Hi Froede,

What you want to do is not clear.

If I am understanding you correctly, why not just use if statements in your Python code to determine whether you use cdo.selyear, or cdo.selmon for example?

Oliver

RE: CDO automation in Python using a function or txt file - Added by Froede Vrolijk almost 8 years ago

Yes I am new to Python and CDO, so this might have been a silly question, but I was just curious if this was possible. Oliver, indeed I will try the approach using if statements. Thanks.

Regards,
Froede

RE: CDO automation in Python using a function or txt file - Added by Ralf Mueller almost 8 years ago

Hi Froede!
IMO you should start with the terminal, first. The python interface is best used with some shell experience in mind.
But no matter, how you want to work, please feel free to ask here.

;-)
ralf

    (1-4/4)