Simple arithmetic and comparisons

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

Basic arithmetic using constants and datasets

Often you might want to subtract datasets from each other, or add or subtract a dataset by a constant. The former is potentially made complicated as datasets can take different forms. For example, you might want to subtract a dataset which contains annual means from a dataset that contains monthly values. In this case you want to subtract the annual mean from the relevant month in each year. To deal with this problem, nctoolkit offers the ability to use standard Python operations +, -, * and / to carry out these operations, and in most use-cases it will be able to carry out an appropriate calculation.

Let’s illustrate this using a dataset of monthly sea surface temperature from 1850 to the present day. We will start by looking at the first time step:

[1]:
import nctoolkit as nc
ds = nc.open_data("sst.mon.mean.nc")
ds.subset(time = 0)
ds.plot()
nctoolkit is using Climate Data Operators version 2.1.0
[1]:

This is temperature in Celsius. However, we might want this in Kelvin. To get this we can simply add 273.15 to the dataset:

[2]:
ds+273.15
ds.plot()
[2]:

That was easy. And the same is true for adding or subtracting datasets from each other. Let’s calculate the temperature in 2010s and see how much warmer it was than in the 1910s:

[3]:
ds_2010s = nc.open_data("sst.mon.mean.nc")
ds_2010s.subset(year = range(2010, 2020))
ds_2010s.tmean()
ds_1910s = nc.open_data("sst.mon.mean.nc")
ds_1910s.subset(year = range(1910, 1920))
ds_1910s.tmean()
ds_2010s-ds_1910s
ds_2010s.plot()
[3]:

We can see that overall, the world’s oceans where much warmer in the 2010s than a century before.

Similarly, if you want to subtract a dataset with only one time step, things will work as expected:

Now, let’s think of something slightly more complicated. If I wanted to work out how much warmer or colder each month was than average, how would I do that? You can just use - as above. But in this case nctoolkit we can use the fact that nctoolkit can figure out what it is subtracting from what. Let’s start by calculating the mean monthly temperature for all years in our data:

[4]:
ds_ave = nc.open_data("sst.mon.mean.nc")
ds_ave.tmean("month")

Now, let’s now subtract this from our original dataset which has monthly temperature from 1850 to the present day. Once, we have done that we can then calculate a spatial mean to get some idea of long-term trends.

[5]:
ds_ave = nc.open_data("sst.mon.mean.nc")
ds_ave.tmean("month")
ds = nc.open_data("sst.mon.mean.nc")
ds-ds_ave
ds.spatial_mean()
ds.plot()
Subtracting a monthly time series
[5]:

You will notice that nctoolkit has told you it is subtracting a monthly time series. If we were to subtract a time series that only has annual data, we would get a different message:

[6]:
ds_ave = nc.open_data("sst.mon.mean.nc")
ds_ave.tmean("year")
ds = nc.open_data("sst.mon.mean.nc")
ds-ds_ave
ds.run()
Subtracting a yearly time series

Note: these methods require consistency between the datasets. For example, in the code below we are subtracting the annual means from the monthly values, but we have removed the year 2000. So running this code will throw an error.

At present, the +, - / and * methods will only be able to handle monthly or yearly datasets on the right-hand side of the operator. That is, it will be able to automatically handle monthly and annual mean data on the right hand-side. An upcoming nctoolkit release will add the ability to automatically handle the ability to handle daily data, so that you can subtract a daily climatology from a dataset with multiple years of daily data.

Importantly, nctoolkit will automatically handle datasets on the right-hand side of the +, -, / or * operators if they have one time step or the same number of time steps as the dataset on the left-hand side. In this case the operation is unambiguous.

nctoolkit also offers verbose methods for these methods, with the following names:

Succinct

Verbose

+

add

-

subtract

/

divide

*

multiply

So, the following would be equivalent:

[7]:
ds+273.15
ds.add(273.15)

Comparisons

As with simple arithematic, logical comparisons can be done in the standard way in nctoolkit, with the dataset being on the left side of the operator. The right hand side of the operator can be a constant or another dataset of the same structure. The following are available: >, <, ==, >=, <=, !=. We can illustrate this using the temperature dataset used above. If we wanted to calculate where temperature is higher than 10 C, we could do the following:

[8]:
ds = nc.open_data("sst.mon.mean.nc")
ds.subset(time = 0)
ds>10
ds.plot()
[8]:

In a similar way, we could work out which parts of the world’s oceancs were warmer in the 2010s than the 1910s:

[9]:
ds_2010s = nc.open_data("sst.mon.mean.nc")
ds_2010s.subset(year = range(2010, 2020))
ds_2010s.tmean()
ds_1910s = nc.open_data("sst.mon.mean.nc")
ds_1910s.subset(year = range(1910, 1920))
ds_1910s.tmean()
ds_2010s>ds_1910s
ds_2010s.plot()
[9]:

We can see that the the vast majority was warmer. In fact, we could take it a step further and calculate what fraction was warmer:

[10]:
ds_2010s.spatial_mean()
ds_2010s.to_dataframe()
[10]:
time_bnds sst
time bnds lon lat
2019-12-01 0 0.0 0.0 2019-12-01 0.965985
1 0.0 0.0 2019-12-01 0.965985

We see that it was over 95%.