pythondatasetpython-xarraynetcdf4

Adjusting gridded data on two different grid resolutions


I have monthly bias-correction values, gridded over a domain, spaced at 0.5 degree intervals (see ds1 below). I have daily precipitation values, gridded over the same domain, spaced at 0.22 degree intervals (see ds2 below). For each grid point in ds2, I need to identify the grid cell it falls into in ds1 and then apply the correct month's bias correction value from ds1 to the daily ds2 pr value. How would I do this?

print(ds1):

    <xarray.DataArray (month: 12, lat: 160, lon: 160)>
array([[[       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        [       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        [       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        ...,
        [       nan,        nan,        nan, ..., 1.65857268,
         1.39625078, 1.29690036],
        [       nan,        nan,        nan, ..., 1.83888439,
         1.62477472, 1.51900893],
        [       nan,        nan,        nan, ..., 1.83062936,
         1.74259254, 1.7321896 ]],

       [[       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        [       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        [       nan,        nan,        nan, ...,        nan,
                nan,        nan],
...
        [       nan,        nan,        nan, ..., 2.47785328,
         2.62370308, 2.89990906],
        [       nan,        nan,        nan, ..., 2.92869831,
         2.83574652, 3.18910588],
        [       nan,        nan,        nan, ..., 3.24561771,
         3.28454319, 3.3364782 ]],

       [[       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        [       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        [       nan,        nan,        nan, ...,        nan,
                nan,        nan],
        ...,
        [       nan,        nan,        nan, ..., 1.74697667,
         1.59683893, 1.55137055],
        [       nan,        nan,        nan, ..., 1.77933588,
         1.68900752, 1.55048538],
        [       nan,        nan,        nan, ..., 1.73876797,
         1.5361837 , 1.48602644]]])
    Coordinates:
      * lon      (lon) float64 -19.75 -19.25 -18.75 -18.25 ... 58.75 59.25 59.75
      * lat      (lat) float64 -39.75 -39.25 -38.75 -38.25 ... 38.75 39.25 39.75
      * month    (month) int64 1 2 3 4 5 6 7 8 9 10 11 12

print(ds2):

 <xarray.DataArray 'pr' (time: 7200, lat: 402, lon: 388)>
array([[[           nan,            nan,            nan, ...,
                    nan,            nan,            nan],
        [1.02439264e+01, 8.53370824e+00, 9.22459352e+00, ...,
         2.01433619e+00, 2.05144843e+00,            nan],
        [1.16105385e+01, 9.16766301e+00, 9.46495759e+00, ...,
         1.99566535e+00, 2.20051880e+00,            nan],
        ...,
        [6.51055963e-02, 3.78015918e-02, 1.84728351e-02, ...,
         1.93128089e-03, 1.86512867e-03,            nan],
        [8.78525181e-02, 3.95604286e-02, 2.18918203e-02, ...,
         5.65447015e-03, 2.52667877e-03,            nan],
        [           nan,            nan,            nan, ...,
                    nan,            nan,            nan]],

       [[           nan,            nan,            nan, ...,
                    nan,            nan,            nan],
        [1.00231890e+00, 1.18213674e+00, 1.27730382e+00, ...,
         1.87700664e+01, 1.87629225e+01,            nan],
        [1.04719991e+00, 1.23321695e+00, 1.33511428e+00, ...,
         1.51802109e+01, 1.67274452e+01,            nan],
...
        [9.31256264e+00, 1.20209102e+01, 1.38895507e+01, ...,
         5.52620594e+00, 5.20073497e+00,            nan],
        [7.38018025e+00, 9.65426266e+00, 1.18221321e+01, ...,
         4.62506230e+00, 4.80847541e+00,            nan],
        [           nan,            nan,            nan, ...,
                    nan,            nan,            nan]],

       [[           nan,            nan,            nan, ...,
                    nan,            nan,            nan],
        [1.44303443e-02, 6.05955611e-02, 7.16263798e-02, ...,
         1.70073773e+01, 1.65572176e+01,            nan],
        [2.46751410e-02, 3.06789566e-02, 4.37956929e-02, ...,
         1.70963743e+01, 1.63159193e+01,            nan],
        ...,
        [3.93234263e-01, 7.24131920e-01, 1.32352682e+00, ...,
         1.11767060e+00, 9.05873321e-01,            nan],
        [8.08598244e-01, 4.34327696e-01, 8.99296539e-01, ...,
         1.16983899e+00, 9.55646188e-01,            nan],
        [           nan,            nan,            nan, ...,
                        nan,            nan,            nan]]])
    Coordinates:
      * time     (time) datetime64[ns] 2001-01-01T12:00:00 ... 2020-12-30T12:00:00
      * lon      (lon) float64 -24.75 -24.53 -24.31 -24.09 ... 59.95 60.17 60.39
      * lat      (lat) float64 -45.87 -45.65 -45.43 -45.21 ... 41.91 42.13 42.35

Solution

  • Does the following do the job?

    print(ds1)
    <xarray.DataArray (month: 12, lat: 90, lon: 360)>
    array([[[ 0.2889434 ,  0.88045499, -0.55047535, ..., -0.52085633,
             -0.11014433,  0.69861361],
           ...
             -0.10453087, -0.74248403],
            [ 0.45813065, -0.47323794, -0.28630093, ...,  0.08430415,
    ...
    Coordinates:
      * month    (month) int64 1 2 3 4 5 6 7 8 9 10 11 12
      * lat      (lat) int64 0 1 2 3 4 5 6 7 8 9 ... 80 81 82 83 84 85 86 87 88 89
      * lon      (lon) int64 0 1 2 3 4 5 6 7 8 ... 352 353 354 355 356 357 358 359
    
    print(ds2)
    <xarray.DataArray (time: 100, lat: 410, lon: 1637)>
    array([[[ 0.76161522,  1.33841391,  0.26770973, ...,  0.38682997,
             -0.17269032,  0.17136374],
            ...8 ,
             -0.6027989 ,  1.9923682 ],
            [-1.68020718,  0.96972946, -0.72490009, ...,  1.01982304,
    ...
    Coordinates:
      * time     (time) datetime64[ns] 2010-01-01 2010-01-02 ... 2010-04-10
      * lat      (lat) float64 0.0 0.22 0.44 0.66 0.88 ... 89.32 89.54 89.76 89.98
      * lon      (lon) float64 0.0 0.22 0.44 0.66 0.88 ... 359.3 359.5 359.7 359.9
    
    # interpolate ds1 to the same grid as ds2 (nearest neighbor)
    bias_correction = ds1.interp_like(ds2, method="nearest")
    # multiply ds2 by the bias correction (alternatively, perform any other operation)
    result = ds2.groupby("time.month") * bias_correction
    
    print(result)
    <xarray.DataArray (time: 100, lat: 410, lon: 1637)>
    array([[[ 0.2200637 ,  0.38672587,  0.07735296, ...,         nan,
                ...
                     nan,         nan],
            [        nan,         nan,         nan, ...,         nan,
    ...
      * time     (time) datetime64[ns] 2010-01-01 2010-01-02 ... 2010-04-10
      * lat      (lat) float64 0.0 0.22 0.44 0.66 0.88 ... 89.32 89.54 89.76 89.98
      * lon      (lon) float64 0.0 0.22 0.44 0.66 0.88 ... 359.3 359.5 359.7 359.9
        month    (time) int64 1 1 1 1 1 1 1 1 1 1 1 1 1 ... 3 3 4 4 4 4 4 4 4 4 4 4