1

Im trying to calculate the mean SPI value over a selected area with a specific lon/lat. I made the following area where i want to calculate the mean over.

ds_mlw1 = ds.sel(lat=slice(-16.74833, -16.75613), lon=slice(35.27023, 35.27915))

I saw that it works best to work with the cdo package. I get an error that my cdo is not recognized. Does someone know how to solve this problem.

This is my script

from cdo import *
import matplotlib.pyplot as plt
file = 'spi3_6_12_1deg_cru_ts_3_21_1949_2012.nc'
cdo = Cdo()
cdo.debug=True


meanspi  = cdo.fldmean(input=file,returnCdf=True).variables['spi3'][:] 

I get the following error

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-77-7eeb6d09369c> in <module>()
      9 import matplotlib.pyplot as plt
      10 file = 'spi3_6_12_1deg_cru_ts_3_21_1949_2012.nc'
  ---> 11 cdo = Cdo()
     12 cdo.debug=True
     13 

FileNotFoundError: [WinError 2] Het systeem kan het opgegeven bestand niet vinden

meaning (the systme can't find the given file)

ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Marijke
  • 39
  • 2

2 Answers2

2

I am not familiar with cdo, but using ipython (interactive python) I debugged this section of code:

from cdo import *
cdo=Cdo() 

And was able to get your error:

FileNotFoundError: [Errno 2] No such file or directory: 'cdo': 'cdo'

I can also see in the exception trace that it is looking for the executable 'cdo'. It appears that the python module cdo is a wrapper around a program binary cdo.

If you install the program cdo it will probably solve your problem. Information appears here? : https://code.mpimet.mpg.de/projects/cdo/wiki/Cdo#Documentation

Is this climate change related? That is very cool!

natersoz
  • 1,674
  • 2
  • 19
  • 29
  • Thanks for the link, it did not succeed yet, but i think indeed it is an error in the installation. My research is indeed about climate change, in specific drought forecast related. – Marijke Sep 04 '18 at 11:43
  • The Python `cdo` library is just an interface relaying to the binary `cdo` which must be installed on your system and available in your path. Try running `cdo` from your command line - once that works, then your problem should be solved. – Florian Metzger-Noel Apr 07 '22 at 12:26
2

I did not use the cdo package in the end, but used the slice function in xarray.

ds_mlw = ds.sel(lon=slice(33,37), lat=slice(-18,-13), time=slice("2002-01-01", "2017- 
11-01"))
spi_avg = ds_mlw.mean(dim=('lon', 'lat'))
plt.figure(figsize=(10,5))
spi_avg['spi3'].plot()
Marijke
  • 39
  • 2