Search code examples
python-iris

Using cube.interpolate to go from theta levels to rho levels


I have a cube of temperatures on theta levels and I need a cube of temperatures on rho levels. Is cube.interpolate able to do this, or do I need something else? The vertical regridding example in the Iris user guide has an example of going from hybrid height to fixed equally spaced altitude sample points but doesn't have an example where the target grid has spatially varying heights. I tried a naive approach to this but it failed.

sample_points = [p_rho_cube.coord('altitude').points]
t_rho_levs = temp.interpolate(sample_points, iris.analysis.Linear())

but this fails:

Traceback (most recent call last):
  File "cmip5_lbc_gen_um.py", line 243, in <module>
    cmip_lbc_prep_um(pp_um_file, outfile)
  File "cmip5_lbc_gen_um.py", line 197, in cmip_lbc_prep_um
    t_rho_levs = temp.interpolate(sample_points, iris.analysis.Linear())
  File "/opt/scitools/environments/default/2017_06_07/lib/python2.7/site-packages/iris/cube.py", line 3811, in interpolate
    coords, points = zip(*sample_points)
ValueError: too many values to unpack

Is there an easy way to do this, or do I have to write my own interpolation for this?


Solution

  • I believe your suggested use of cube.interpolate is suspect : the first argument should be a tuple of (coord, values) -- check out the example in the docstring.

    However I also think that is irrelevant, because you want the target coord value to vary with position, as you say...

    For that, what you probably want is "python-stratify" : https://github.com/SciTools-incubator/python-stratify
    Another fine SciTools product!
    But this one is still rather experimental, which is why it is in "scitools-incubator".

    This should be a pretty efficient calculation, and it does allow your target values to vary across additional dimensions, I believe : In the docs, it says that the 'z_target' arg can be multidimensional, matching the shape of 'z_src'.
    https://github.com/SciTools-incubator/python-stratify/blob/569a92f3bec825e9edd08f416cce1d77fc0c96df/stratify/_vinterp.pyx#L446

    Oddly, the enclosed explanatory notebook does not seem to demonstrate that though. I faintly recall that the operation was 'generalised' at some point, so possibly this was written before that and needs updating.