Anwenden von numpy.polyfit auf xarray Dataset

Unterstützt Xarray Numpy-Berechnungsfunktionen wie Polyfit? Oder gibt es eine effiziente Möglichkeit, solche Funktionen auf Datensätze anzuwenden?

Beispiel: Ich möchte die Steigung einer Linie berechnen, die an zwei Variablen (Temperatur und Höhe) angepasst ist, um eine Ablaufrate zu berechnen. Ich habe einen Datensatz (unten) mit diesen beiden Variablen mit den Dimensionen (vertikal, zeitlich, 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 ...

Wenn ich die Temperatur und Höhe für eine bestimmte Zeit extrahiere, xgrid_0, ygrid_0; Ich kann die Funktion numpy.polyfit verwenden.

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)

Aber dies ist ein langsamer und ineffizienter Ansatz. Irgendwelche Vorschläge, wie man dies besser angehen kann?

Antworten auf die Frage(0)

Ihre Antwort auf die Frage