Aplicando numpy.polyfit ao conjunto de dados xarray
O Xarray suporta funções de computação numpy, como polyfit? Ou existe uma maneira eficiente de aplicar essas funções aos conjuntos de dados?
Exemplo: quero calcular a inclinação de uma linha ajustada a duas variáveis (temperatura e altura), para calcular uma taxa de lapso. Eu tenho um conjunto de dados (abaixo) com essas duas variáveis com dimensões de (vertical, tempo, xgrid_0, ygrid_0).
<xarray.Dataset>
Dimensions: (PressLev: 7, time: 48, xgrid_0: 685, ygrid_0: 485)
Coordinates:
gridlat_0 (ygrid_0, xgrid_0) float32 44.6896 44.6956 44.7015 44.7075 ...
gridlon_0 (ygrid_0, xgrid_0) float32 -129.906 -129.879 -129.851 ...
* ygrid_0 (ygrid_0) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
* xgrid_0 (xgrid_0) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
* time (time) datetime64[ns] 2016-08-15T01:00:00 2016-08-15T02:00:00 ...
* PressLev (PressLev) int64 0 1 2 3 4 5 6
Data variables:
Temperature (PressLev, time, ygrid_0, xgrid_0) float64 289.4 289.4 289.4 ...
Height (PressLev, time, ygrid_0, xgrid_0) float64 85.23 85.13 84.98 ...
Se eu extrair a temperatura e a altura por um determinado tempo, xgrid_0, ygrid_0; Eu posso usar a função numpy.polyfit.
ds_LR = ds.TMP_P0_L103_GST0 * 0 -9999 # Quick way to make dataarray with -9999 values but with correct dims/coords
for cts in np.arange(0,len(ds_UA.time)):
for cx in ds_UA.xgrid_0.values:
for cy in ds_UA.ygrid_0.values:
x_temp = ds_UA.Temperature[:,cts,cy,cx] # Grab the vertical profile of air temperature
y_hgt = ds_UA.Height[:,cts,cy,cx] # Grab the vertical heights of air temperature values
s = np.polyfit(y_hgt,x_temp,1) # Fit a line to the data
ds_LR[cts,cy,cx].values = s[0] # Grab the slope (first element)
Mas essa é uma abordagem lenta e ineficiente. Alguma sugestão sobre uma maneira melhor de abordar isso?