Python Xarray – Computing a New Variable on the Basis of an Existing One

netcdfnoaapythonxarray

I have netcdf file with calculated average temperature (°F). My goal is to create new data variable within the same file with cooling / heating degree days using that average. This format is totally new for me and is hard to figure out solution.
I've tried assign new data variable using following syntax but without expecting results. I guess it because is taking into calculation whole array at once.

nc = nc.assign(cdd_hdd=lambda x: x.tavg - 65 if tavg > 65 else 65 - x.tavg)

My goal:

<xarray.Dataset>
Dimensions:  (lat: 360, lon: 720, time: 259)
Coordinates:
  * lat      (lat) float32 89.75 89.25 88.75 88.25 ... -88.75 -89.25 -89.75
  * lon      (lon) float32 0.25 0.75 1.25 1.75 2.25 ... 358.2 358.8 359.2 359.8
  * time     (time) datetime64[ns] 2022-01-01 2022-01-02 ... 2022-09-16
Data variables:
    tavg     (time, lat, lon) float32 ...
    cdd_hdd     (time, lat, lon) float32 ...

Best Answer

You can add new variables to an existing xr.Dataset by adding a new "key" to the dataset, with the value being a xr.DataArray :

nc["cdd_hdd"] = xr.where(nc["tavg"] > 65, nc["tavg"] - 65, 65 - nc["tavg"])

xr.where takes three arguments, first is a condition, second are the values at coordinates where the condition is True, third are values where the condition is False.