Correlate two datasets with different dimensions

Viewed 20

I am trying to calculate the spearman's rank correlation co-efficient between two datasets. However, the one dataset is a gridded dataset (with latitude and longitude coordinates) and the other is simply a time series (please see below).

print(spi_djf_cor)

<xarray.DataArray 'pre' (time: 41, lat: 162, lon: 162)>
array([[[        nan,         nan,         nan, ...,         nan,
                 nan,         nan],
       ...,
        [        nan,         nan,         nan, ..., -0.19086839,
         -0.31531059, -0.32325101],
        [        nan,         nan,         nan, ..., -0.30278464,
         -0.37735563, -0.43342958],
        [        nan,         nan,         nan, ..., -0.5057808 ,
         -0.49130144, -0.69925514]]])
Coordinates:
  * time     (time) datetime64[ns] 1980-12-01 1981-12-01 ... 2020-12-01
  * lon      (lon) float64 -20.25 -19.75 -19.25 -18.75 ... 59.25 59.75 60.25
  * lat      (lat) float64 -40.25 -39.75 -39.25 -38.75 ... 39.25 39.75 40.25

print(nino_djf)

<xarray.DataArray 'ANOM' (time: 41)>
array([-0.31666667, -0.08      ,  2.16333333, -0.63      , -1.07333333,
       -0.59333333,  1.13      ,  0.70666667, -1.8       ,  0.03      ,
        0.38666667,  1.77      ,  0.13333333,  0.10666667,  1.        ,
       -0.89      , -0.51333333,  2.23333333, -1.56666667, -1.68666667,
       -0.76      , -0.2       ,  0.87      ,  0.31      ,  0.58666667,
       -0.83333333,  0.65666667, -1.64333333, -0.84666667,  1.50333333,
       -1.41666667, -0.86666667, -0.43333333, -0.42666667,  0.54666667,
        2.49666667, -0.33666667, -0.91333333,  0.75      ,  0.49666667,
       -1.05      ])
Coordinates:
  * time     (time) datetime64[ns] 1980-12-01 1981-12-01 ... 2020-12-01

I would like to correlate the time series at each grid point in spi_djf_cor with the time series in nino_djf. How do I do this? I don't think using the below code is giving the correct output as I don't think it is correlating at each grid point...

xs.spearman_r(spi_djf_cor, nino_djf, dim='time')
1 Answers

You can use xr.broadcast to broadcast the arrays before calling spearman_r:

xs.spearman_r(*xr.broadcast(spi_djf_cor, nino_djf), dim='time')
Related