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 axr.DataArray
:xr.where
takes three arguments, first is a condition, second are the values at coordinates where the condition isTrue
, third are values where the condition isFalse
.