To people reading the hive on the 23 of December: Hi to both of you! To the rest, I guess you can open this present after the break.
In an attempt to teach myself python and also create something useful for me and others, I made the pycdo python package. It’s a wrapper to use CDO operations from python and (hopefully) using pythonic syntax.
For those who don’t know, the Climate Data Operators is a command line tool to work with climate data. It’s super fast and efficient and super convenient, especially for relatively simple operations like computing means or regridding. The problem with CDO is that being a command line utility, is hard to program with unless you’re very comfortable with bash.
With pycdo you can construct CDO command and then execute them using python. So, say you wanted to compute the monthly climatology of Southern Hemisphere geopotential height… you would do
from pycdo import cdo
(
cdo(geopotential)
.sellonlatbox(0, 360, -90, 0)
.ymonmean()
.execute()
)
And that would execute something like this in the terminal
cdo -ymonmean -sellonlatbox,0,360,-90,0 geopotential.nc tempfile.nc
By default pycdo saves the result in a temporary file that is deleted after it’s not longer accessible. That makes it easy to work with NetCDF files as if they were just normal variables without needing to think about names for every single one of them.
A neat feature is that pycdo support caching. You can enable the cache:
from pycdo import cdo_cache
cdo_cache.set("data/cache")
And then every operation will only need to be run once, even across python sessions. pycdo will check that the input are the same and the command is the same, and will fetch the output instead of running the command again. This can save you a lot of time when iterating an analysis!
I’m still working on improving the documentation (and understanding how python documentation even works). But the package works! You can install it from pypi
pip install pycdo
It’s got no weird dependencies other than the standard libraries, so it should be compatible with analysis3. The only system dependency is cdo itself, which you can load on gadi with
module load cdo
Let me know if you find this useful. If you find bugs, you can open an issue in the repo or just send me a PM here.