unable to invoke cdo operaotrs (methods) in cdo.py
Added by Laura Brown over 7 years ago
Hello,
I'm working on a project where I am modelling the effect of climate change on diurnal spring temperatures. I'd like to use several CDO operators in a python script. I have use the "pip install cdo" in the WinPython command window and the installation was successful. But my script fails when it tries to execute a CDO operator. At first I thought it might be that the "gec,#" and "lec,#" operators were not included. So I tried to get a help file using the example provided on the Ruby, Python page. Again I got the same error message so I tried copying the example provided on the Ruby, Python page:
from cdo import Cdo #note when I tried from cdo import * it didn't work and I found replacing * with CDO given on this page avoided the error "https://www.mi.uni-hamburg.de/en/arbeitsgruppen/strahlung-und-fernerkundung/intern/howtos/cdo.html" cdo=Cdo() help(cdo.sinfo) This is an example of the error messages I'm getting: "C:\WinPython64\WinPython-64bit-3.4.4.6Qt5\python-3.4.4.amd64\lib\site-packages\cdo.py", line 257, in __getattr__ raise AttributeError("Unknown method '" + method_name +"'!") AttributeError: Unknown method 'sinfo'!
This is my script:
#************************** # Access standard libraries #************************** from cdo import Cdo import os #*************************************** # Set user-defined environment variables #*************************************** # Set the input/output directories wrkDir = 'C:/Netcfd/BCSD' Minifile = wrkDir + '/tasmin' Minofile = wrkDir + '/tasmin/Sap' Maxifile = wrkDir + '/tasmax' Maxofile = wrkDir + '/tasmax/Sap' #*********************** # Process input datasets #*********************** inList = os.listdir(Minifile) # List all the files in the 'tasmin' directory print(inList) for fileName in inList: # Step through each file ifile = fileName baseName, extension = os.path.splitext(ifile) if extension == '.nc': #create ofile named according to input file ofile = Minofile + ifile + '_sap.nc' #Call cdo cdo=Cdo() # when tasmin is less than or equal to -1 a value of one is written to # the output file everythiong else is given a 0 using cdo lec,-1 cdo.lec,-1(input = ifile, out=ofile, options = '-a, -f nc4c') inList = os.listdir(Maxifile) # List all the files in the 'tasmax' directory for fileName in inList: # Step through each input file ifile = fileName baseName, extension = os.path.splitext(ifile) if extension == '.nc': #create ofile named according to input file ofile = Maxofile + fileName + '_sap.nc' #Call cdo cdo=Cdo() # when tasmax is greater than or equal to 2.2 a value of one is written # to the output file everythiong else is given a 0 using cdo gec,2.2 cdo.gec,2.2(input = ifile, output= ofile, options = '-a, -f nc4c') print ("All done!!")
This is the console output:
runfile('C:/Netcfd/Saptemp1.py', wdir='C:/Netcfd') ['Sap', 'tasmin_26_20.nc', 'tasmin_26_50.nc', 'tasmin_26_80.nc', 'tasmin_26_90.nc', 'tasmin_45_20.nc', 'tasmin_45_50.nc', 'tasmin_45_80.nc', 'tasmin_45_90.nc', 'tasmin_85_20.nc', 'tasmin_85_50.nc', 'tasmin_85_80.nc', 'tasmin_85_90.nc'] Traceback (most recent call last): File "<ipython-input-29-44facb0e92fa>", line 1, in <module> runfile('C:/Netcfd/Saptemp1.py', wdir='C:/Netcfd') File "C:\WinPython64\WinPython-64bit-3.4.4.6Qt5\python-3.4.4.amd64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile execfile(filename, namespace) File "C:\WinPython64\WinPython-64bit-3.4.4.6Qt5\python-3.4.4.amd64\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Netcfd/Saptemp1.py", line 59, in <module> cdo.lec,-1(input = ifile, out=ofile, options = '-a, -f nc4c') File "C:\WinPython64\WinPython-64bit-3.4.4.6Qt5\python-3.4.4.amd64\lib\site-packages\cdo.py", line 257, in __getattr__ raise AttributeError("Unknown method '" + method_name +"'!") AttributeError: Unknown method 'lec'!
Can anyone help me to get this running?
If I can get this going I want to continue with another script calling on the mul operator, then use the splityear operator, and then the consecsum operator.
Thank you for your time,
Laura
Replies (3)
RE: unable to invoke cdo operaotrs (methods) in cdo.py - Added by Ralf Mueller over 7 years ago
Hi Laura!
cdo.py needs a runabel CDO binary. Since you're using plain windows, I guess there is none on your machine (the win32 version is outdated and you dont seem to use cygwin)
I recommend to install cygwin (http://cygwin.com/) and use its shell for what you want to do. There is a pre-build cdo binary for cygwin (https://code.zmaw.de/attachments/download/12761/cdo-1.7.2-cygwin64.zip) which needs some extra packages. but these can be installed with cygwin's own package manager)
hth
ralf
RE: unable to invoke cdo operaotrs (methods) in cdo.py - Added by Laura Brown over 7 years ago
Thank you Ralf.
I do use cygwin and ended up using its shell to run the operations I wanted. I was just hoping to do everything in Python.
Allow me to thank you and your colleagues for CDO and its operators. Its really saved me a lot of time and is so easy to use.
Laura
RE: unable to invoke cdo operaotrs (methods) in cdo.py - Added by Ralf Mueller over 7 years ago
Just to get thing clear for me: you're now using cygwin with cdo and cdo.py without problems??
cheers
ralf