Project

General

Profile

Calculating wind speed from uwnd and vwnd via python

Added by Beate K about 11 years ago

Hello all,

I'm new to cdo{py} and I'm no programming crack, so I'm still having problems with the syntax. I've installed the the newest cdo version 1.6.0, but I've written all the code based on version 1.4.7.

I need to calculate the wind speed from the horizontal and vertical wind vectors. In cdo I use the following code to do so:
cdo chname,uwnd,ws -sqrt -add -sqr -selname,uwnd ifile1 -sqr -selname,vwnd ifile2 ofile

I tried the following as python syntax, but it doesn't work, I'm getting an error "Too many streams! Operator needs 1 input and 1 output stream":
cdo.chname(input="uwnd,ws -sqrt -add -sqr -selname,uwnd " + ifile1 + " -sqr -selname,vwnd " + ifile2, output=ofile)

Due to the fact that the following syntax works fine, I think the two input files are causing the problem?
cdo.chname("uwnd,ws", input=ifile1, output=ofile)

Has anybody an idea how the correct syntax looks like?

Thanks in advance and kind regards,
Beate


Replies (6)

RE: Calculating wind speed from uwnd and vwnd via python - Added by Ralf Mueller about 11 years ago

Hi Beate!
For the CDO call

cdo chname,uwnd,ws -sqrt -add -sqr -selname,uwnd ifile1 -sqr -selname,vwnd ifile2 ofile
the cdo.py call is
cdo.chname("uwnd,ws",input="-sqrt -add -sqr -selname,uwnd " + ifile1 + " -sqr -selname,vwnd " + ifile2, output=ofile)

Parameters for operator must be given as plain arguments to the python (operator) method

For debugging, you might use

cdo.debug = True
In a script, I often use the environment for setting things. This avoids comandline parsing - for simple scripts or scripts with only a few switches, this might be sufficient: https://github.com/Try2Code/iconPlot/blob/master/xsec.py#L22

hth
ralf

RE: Calculating wind speed from uwnd and vwnd via python - Added by Ralf Mueller about 11 years ago

if u and v are in the same files; you can also use the expr operator

cdo expr,'ws=sqrt(uwnd*uwnd+vwnd*vwnd)' ifile ofile

of in python
cdo.expr('ws=sqrt(uwnd*uwnd+vwnd*vwnd)',input=ifile,output=ofile)

RE: Calculating wind speed from uwnd and vwnd via python - Added by Beate K about 11 years ago

Hi Ralf,

thanks again, now it works! I've already seen the code for debugging in an example, but realized now the value of activating it...

Best regards,
Beate

RE: Calculating wind speed from uwnd and vwnd via python - Added by Oliver Angelil over 6 years ago

Hi Ralf,

I'm getting an error using the python syntax:
syntax error near unexpected token `('

Oliver

RE: Calculating wind speed from uwnd and vwnd via python - Added by Oliver Angelil over 6 years ago

I think it should be: cdo.expr("'ws=sqrt(uwnd*uwnd+vwnd*vwnd)'",input=ifile,output=ofile)

as shown here: https://github.com/Try2Code/cdo-bindings/blob/ccb2a56f92526ecb80981baeb39658cb2c263f2f/python/test/test_cdo.py#L106

Oliver

RE: Calculating wind speed from uwnd and vwnd via python - Added by Karin Meier-Fleischer over 6 years ago

Hi Oliver,

you're right. The equation must be set in double quotes, too.

-Karin

    (1-6/6)