Interpolation from pressure layers to altitude layers

Interpolate a zonal wind profile from pressure levels to a regular altitude grid.

import easyclimate as ecl
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np

Suppose the following data are available

# fmt: off
z_data = xr.DataArray(
    np.array([
        214.19354, 841.6774, 1516.871, 3055.7097, 4260.5806,
        5651.4194, 7288.032, 9288.193, 10501.097, 11946.71,
        13762.322, 16233.451, 18370.902, 20415.227, 23619.033,
        26214.322, 30731.807,
    ]),
    dims=("level"),
    coords={
        "level": np.array([
            1000.0, 925.0, 850.0, 700.0, 600.0,
            500.0, 400.0, 300.0, 250.0, 200.0,
            150.0, 100.0, 70.0, 50.0, 30.0,
            20.0, 10.0,
        ])
    },
)

uwnd_data = xr.DataArray(
    np.array([
        -2.3200073, -3.5700073, -2.5800018, 8.080002, 14.059998,
        22.119995, 33.819992, 49.339996, 57.86, 64.009995,
        62.940002, 49.809998, 31.160004, 16.59999, 10.300003,
        10.459991, 9.880005,
    ]),
    dims=("level"),
    coords={
        "level": np.array([
            1000.0, 925.0, 850.0, 700.0, 600.0,
            500.0, 400.0, 300.0, 250.0, 200.0,
            150.0, 100.0, 70.0, 50.0, 30.0,
            20.0, 10.0,
        ])
    },
)
# fmt: on

Then we need to interpolate the zonal wind data (located on the isobaric surface) to the altitude layers.

target_heights = np.linspace(0, 10000, 101)

result = ecl.interp.interp1d_vertical_pressure2altitude(
    z_data=z_data,
    variable_data=uwnd_data,
    target_heights=target_heights,
    vertical_input_dim="level",
    vertical_output_dim="altitude",
)
result
<xarray.DataArray (altitude: 101)> Size: 808B
array([-1.621444  , -1.9583984 , -2.27662079, -2.57157517, -2.83872555,
       -3.07353594, -3.27147033, -3.42799274, -3.53856716, -3.5986576 ,
       -3.60372807, -3.54924256, -3.43066509, -3.24345966, -2.98309027,
       -2.64502092, -2.22597052, -1.73105426, -1.16883252, -0.54787616,
        0.12324395,  0.83595696,  1.58169199,  2.35187819,  3.13794469,
        3.93132063,  4.72343514,  5.50571737,  6.26959645,  7.00650152,
        7.70786171,  8.36531168,  8.97677015,  9.54745024, 10.08297407,
       10.58896375, 11.07104138, 11.53482909, 11.98594898, 12.43002317,
       12.87267376, 13.31952288, 13.77619264, 14.2482397 , 14.73858718,
       15.2466844 , 15.77174317, 16.31297529, 16.86959254, 17.44080673,
       18.02582965, 18.62387309, 19.23414887, 19.85586876, 20.48824458,
       21.13048811, 21.78181115, 22.44143654, 23.10885874, 23.7838541 ,
       24.46621207, 25.15572206, 25.85217352, 26.55535589, 27.26505859,
       27.98107108, 28.70318277, 29.4311831 , 30.16486152, 30.90400746,
       31.64841035, 32.39785963, 33.15214473, 33.91105503, 34.67432735,
       35.44155111, 36.21229007, 36.98610797, 37.76256858, 38.54123564,
       39.32167292, 40.10344416, 40.88611311, 41.66924355, 42.4523992 ,
       43.23514385, 44.01704122, 44.79765509, 45.5765492 , 46.35328731,
       47.12743317, 47.89855053, 48.66620316, 49.42995455, 50.18915041,
       50.94252257, 51.68869548, 52.42629358, 53.15394131, 53.8702631 ,
       54.57388341])
Coordinates:
  * altitude  (altitude) float64 808B 0.0 100.0 200.0 ... 9.8e+03 9.9e+03 1e+04


Now we can check the interpolation results.

plt.plot(z_data[:9], uwnd_data[:9], "o", label="Original")
plt.plot(result.altitude, result.data, label="Interpolated")
plt.xlabel("Altitude [m]")
plt.ylabel("Zonal Wind [m/s]")
plt.legend()
plot interp pressure2altitude
<matplotlib.legend.Legend object at 0x730531c727e0>

Total running time of the script: (0 minutes 0.518 seconds)