Arithmetic
Arithmetic operations with a single DataArray automatically vectorize (like numpy) over all array values:
import xarray as xr
da = xr.open_dataarray("../data/air_temperature.nc")
da
<xarray.DataArray 'air' (time: 2920, lat: 25, lon: 53)>
[3869000 values with dtype=float32]
Coordinates:
* lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
* lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
* time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
Attributes:
long_name: 4xDaily Air temperature at sigma level 995
units: degK
precision: 2
GRIB_id: 11
GRIB_name: TMP
var_desc: Air temperature
dataset: NMC Reanalysis
level_desc: Surface
statistic: Individual Obs
parent_stat: Other
actual_range: [185.16 322.1 ]
da - 273.15
<xarray.DataArray 'air' (time: 2920, lat: 25, lon: 53)>
array([[[-31.949997, -30.649994, ..., -37.649994, -34.550003],
[-29.350006, -28.649994, ..., -37.850006, -33.850006],
...,
[ 22.75 , 23.049988, ..., 22.75 , 22.049988],
[ 23.140015, 23.640015, ..., 23.640015, 23.450012]],
[[-31.050003, -30.449997, ..., -39.550003, -37.350006],
[-29.550003, -29.050003, ..., -40.649994, -37.449997],
...,
[ 23.049988, 23.549988, ..., 22.350006, 21.950012],
[ 23.140015, 24.049988, ..., 23.25 , 23.450012]],
...,
[[-27.36 , -28.36 , ..., -29.160004, -28.36 ],
[-23.259995, -23.86 , ..., -30.660004, -28.86 ],
...,
[ 23.140015, 24.040009, ..., 21.940002, 21.23999 ],
[ 24.640015, 25.23999 , ..., 22.339996, 22.040009]],
[[-28.059998, -28.86 , ..., -31.660004, -31.36 ],
[-23.259995, -23.86 , ..., -32.86 , -31.460007],
...,
[ 22.940002, 23.73999 , ..., 22.540009, 22.040009],
[ 24.540009, 24.940002, ..., 23.040009, 22.540009]]], dtype=float32)
Coordinates:
* lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
* lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
* time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
da_mean = da.mean(dim='time')
da_mean
<xarray.DataArray 'air' (lat: 25, lon: 53)>
array([[260.37564, 260.1826 , 259.88593, ..., 250.81511, 251.93733, 253.43741],
[262.7337 , 262.7936 , 262.7489 , ..., 249.75496, 251.5852 , 254.35849],
[264.7681 , 264.3271 , 264.0614 , ..., 250.60707, 253.58247, 257.71475],
...,
[297.64932, 296.95294, 296.62912, ..., 296.81033, 296.28793, 295.81622],
[298.1287 , 297.93646, 297.47006, ..., 296.8591 , 296.77686, 296.44348],
[298.36594, 298.38593, 298.11386, ..., 297.33777, 297.28104, 297.30502]],
dtype=float32)
Coordinates:
* lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
* lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
da - da_mean
<xarray.DataArray 'air' (time: 2920, lat: 25, lon: 53)>
array([[[-1.917564e+01, -1.768259e+01, ..., -1.643733e+01, -1.483742e+01],
[-1.893372e+01, -1.829361e+01, ..., -1.628522e+01, -1.505850e+01],
...,
[-2.228699e+00, -1.736481e+00, ..., -8.768616e-01, -1.243500e+00],
[-2.075928e+00, -1.595917e+00, ..., -4.910278e-01, -7.050171e-01]],
[[-1.827565e+01, -1.748259e+01, ..., -1.833734e+01, -1.763742e+01],
[-1.913371e+01, -1.869362e+01, ..., -1.908521e+01, -1.865849e+01],
...,
[-1.928711e+00, -1.236481e+00, ..., -1.276855e+00, -1.343475e+00],
[-2.075928e+00, -1.185944e+00, ..., -8.810425e-01, -7.050171e-01]],
...,
[[-1.458565e+01, -1.539259e+01, ..., -7.947342e+00, -8.647415e+00],
[-1.284370e+01, -1.350362e+01, ..., -9.095215e+00, -1.006850e+01],
...,
[-1.838684e+00, -7.464600e-01, ..., -1.686859e+00, -2.053497e+00],
[-5.759277e-01, 4.058838e-03, ..., -1.791046e+00, -2.115021e+00]],
[[-1.528564e+01, -1.589259e+01, ..., -1.044734e+01, -1.164742e+01],
[-1.284370e+01, -1.350362e+01, ..., -1.129521e+01, -1.266850e+01],
...,
[-2.038696e+00, -1.046478e+00, ..., -1.086853e+00, -1.253479e+00],
[-6.759338e-01, -2.959290e-01, ..., -1.091034e+00, -1.615021e+00]]],
dtype=float32)
Coordinates:
* lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0
* lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0
* time (time) datetime64[ns] 2013-01-01 ... 2014-12-31T18:00:00
NOTE:
Notice that this required broadcasting along the time dimension. NumPy broadcasting is covered in great detail in NumPy Guide.
%load_ext watermark
%watermark --iversion -g -m -v -u -d
xarray 0.12.1
last updated: 2019-05-17
CPython 3.6.7
IPython 7.5.0
compiler : GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)
system : Darwin
release : 18.2.0
machine : x86_64
processor : i386
CPU cores : 8
interpreter: 64bit
Git hash : 83530e805423a8f36958a61783cbbd9fe388eace