# Adding or subtracting datasets¶

Often you might want to subtract datasets from each other. This 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 methods `add`

, `subtract`

, `multiply`

and `divide`

, which from nctoolkit v0.4.0 onwards can
automatically work out the relevant method required for the dataset given.

Let’s illustrate this using a dataset of monthly sea surface temperature from 1850 to the present day. In the snippet below, we load the data into a dataset. Then we generate a second which is the annual mean temperature and subtract it from the original dataset:

```
[1]:
```

```
import nctoolkit as nc
ds1 = nc.open_data("sst.mon.mean.nc")
ds2 = nc.open_data("sst.mon.mean.nc")
ds2.tmean("year")
ds1.subtract(ds2)
```

```
nctoolkit is using Climate Data Operators version 1.9.10
```

```
Subtracting a yearly time series
```

You can see that nctoolkit has stated that it has subtracted a yearly time series.

If we wanted to subtract the monthly mean, we would do this:

```
[2]:
```

```
import nctoolkit as nc
ds1 = nc.open_data("sst.mon.mean.nc")
ds2 = nc.open_data("sst.mon.mean.nc")
ds2.tmean("month")
ds1.subtract(ds2)
```

```
Subtracting a monthly time series
```

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

```
[3]:
```

```
import nctoolkit as nc
ds1 = nc.open_data("sst.mon.mean.nc")
ds2 = nc.open_data("sst.mon.mean.nc")
ds2.tmean()
ds1.subtract(ds2)
```

```
Subtracting a single time step 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.

```
[ ]:
```

```
import nctoolkit as nc
ds1 = nc.open_data("sst.mon.mean.nc")
ds2 = nc.open_data("sst.mon.mean.nc")
ds2.drop(year = 2000)
ds2.tmean("year")
ds1.subtract(ds2)
```

## Automatic methods available¶

At present, the `add`

, `subtract`

`divide`

and `multiply`

methods will be automatically able to handle daily, monthly and yearly dataset. At present, they will not be able to handle hourly datasets or anything finer.

However, it will automatically be able to deal `add`

, `subtract`

, `divide`

or `multiply`

files that either have single time steps or have identical time steps. In this case time step comparisons are unambiguous.