Simple arithmetic & comparisons

Basic arithmetic and logical comparisons can be carried out using the standard Python operators: +, -, *, /, >, <, >=, <=, == and !=.

Arithmetic with constants and datasets

Illustrated using monthly sea surface temperature from 1850 to the present. The first time step, in Celsius:

python
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.subset(timestep=0)
ds.plot()
ds.plot() — interactive output Open interactive
Global sea surface temperature in Celsius, first time step

Converting to Kelvin is as simple as adding a constant:

python
ds + 273.15
ds.plot()
ds.plot() — interactive output Open interactive
Global sea surface temperature in Kelvin, first time step

The same works between two datasets. Here, working out how much warmer the 2010s were than the 1910s:

python
ds_2010s = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds_2010s.subset(years=range(2010, 2020))
ds_2010s.tmean()

ds_1910s = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds_1910s.subset(years=range(1910, 1920))
ds_1910s.tmean()

ds_2010s - ds_1910s
ds_2010s.plot()
ds_2010s.plot() — interactive output Open interactive
How much warmer the 2010s were than the 1910s

Subtracting datasets of different resolution

NCToolkit can figure out what it's subtracting from what, even when the two datasets have different temporal resolution. For example, to work out how much warmer or colder each month is than its long-term average, calculate the mean monthly climatology first:

python
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")

climatology = ds.copy()
climatology.tmean("month")   # mean monthly climatology, 12 time steps

ds - climatology              # NCToolkit reports it's subtracting a monthly time series
ds.spatial_mean()
ds.plot()
ds.plot() — interactive output Open interactive
Global mean monthly SST anomaly relative to the monthly climatology, 1850 to present

NCToolkit tells you which alignment it inferred. Subtracting an annual climatology instead gives a different message, since NCToolkit recognizes it as annual rather than monthly data:

python
climatology = ds.copy()
climatology.tmean("year")   # annual mean instead of monthly

ds - climatology              # NCToolkit reports it's subtracting an annual time series

The datasets must otherwise be consistent: subtracting an annual mean with a missing year, for example, from a monthly series that includes it, will raise an error rather than guess.

Operator reference

SuccinctVerbose
+add
-subtract
/divide
*multiply

So the following are equivalent:

python
ds + 273.15
ds.add(273.15)
Right-hand side must be monthly or annual

At present, +, -, / and * can only automatically handle monthly or annual data on the right-hand side of the operator. Support for subtracting a daily climatology from a multi-year daily series is planned.

Comparisons

Logical comparisons follow the same pattern, with the dataset on the left and a constant or compatible dataset on the right: >, <, ==, >=, <=, !=.

python
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.subset(timestep=0)
ds > 10                 # where temperature exceeds 10C
ds.plot()
ds.plot() — interactive output Open interactive
Where sea surface temperature exceeds 10C

The same works between two datasets — where the 2010s were warmer than the 1910s:

python
ds_2010s = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds_2010s.subset(years=range(2010, 2020))
ds_2010s.tmean()

ds_1910s = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds_1910s.subset(years=range(1910, 1920))
ds_1910s.tmean()

ds_2010s > ds_1910s      # where the 2010s were warmer than the 1910s
ds_2010s.plot()
ds_2010s.plot() — interactive output Open interactive
Where the 2010s were warmer than the 1910s

Combined with spatial_mean, this is an easy way to calculate the fraction of the ocean meeting a condition — for the warming example above, over 95% of the world's oceans were warmer in the 2010s than the 1910s.