Example usage
To use malta in a project:
A simple run of constant emissons of 10 Gg of CFC-11 with zero initial conditions from 2010-2020 inclusive could be:
import numpy as np
from malta import model
start_year = 2010
end_year = 2021
species = "CFC11"
# Set up and run model
years = np.array([str(yr) for yr in range(start_year,end_year)])
emistot = np.repeat(10, len(years))
emissions = model.create_emissions(species, emistot)
sink = model.create_sink(species)
ds_out = model.run_model(years, emissions, sink)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[1], line 2
1 import numpy as np
----> 2 from malta import model
4 start_year = 2010
5 end_year = 2021
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/malta/model.py:5
3 import numpy as np
4 from malta import transport
----> 5 from malta import utils
6 import xarray as xr
7 import pandas as pd
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/malta/utils.py:2
1 # Auxillary routines for 2D model
----> 2 import xarray as xr
3 import json
4 import os
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/xarray/__init__.py:3
1 from importlib.metadata import version as _version
----> 3 from xarray import testing, tutorial
4 from xarray.backends.api import (
5 load_dataarray,
6 load_dataset,
(...)
10 save_mfdataset,
11 )
12 from xarray.backends.zarr import open_zarr
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/xarray/testing.py:11
8 import pandas as pd
10 from xarray.core import duck_array_ops, formatting, utils
---> 11 from xarray.core.coordinates import Coordinates
12 from xarray.core.dataarray import DataArray
13 from xarray.core.dataset import Dataset
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/xarray/core/coordinates.py:16
13 import pandas as pd
15 from xarray.core import formatting
---> 16 from xarray.core.alignment import Aligner
17 from xarray.core.indexes import (
18 Index,
19 Indexes,
(...)
23 create_default_index_implicit,
24 )
25 from xarray.core.merge import merge_coordinates_without_align, merge_coords
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/xarray/core/alignment.py:24
22 from xarray.core.types import T_Alignable
23 from xarray.core.utils import is_dict_like, is_full_slice
---> 24 from xarray.core.variable import Variable, as_compatible_data, calculate_dimensions
26 if TYPE_CHECKING:
27 from xarray.core.dataarray import DataArray
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/xarray/core/variable.py:48
30 from xarray.core.pycompat import (
31 integer_types,
32 is_0d_dask_array,
(...)
35 to_numpy,
36 )
37 from xarray.core.utils import (
38 OrderedSet,
39 _default,
(...)
46 maybe_coerce_to_str,
47 )
---> 48 from xarray.namedarray.core import NamedArray
50 NON_NUMPY_SUPPORTED_ARRAY_TYPES = (
51 indexing.ExplicitlyIndexed,
52 pd.Index,
53 )
54 # https://github.com/python/mypy/issues/224
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/xarray/namedarray/core.py:24
22 from xarray.core import dtypes, formatting, formatting_html
23 from xarray.namedarray._aggregations import NamedArrayAggregations
---> 24 from xarray.namedarray._typing import (
25 _arrayfunction_or_api,
26 _chunkedarray,
27 _DType,
28 _DType_co,
29 _ScalarType_co,
30 _ShapeType_co,
31 )
32 from xarray.namedarray.utils import (
33 _default,
34 is_duck_dask_array,
35 to_0d_object_array,
36 )
38 if TYPE_CHECKING:
File ~/checkouts/readthedocs.org/user_builds/malta/envs/stable/lib/python3.9/site-packages/xarray/namedarray/_typing.py:28
24 _T = TypeVar("_T")
25 _T_co = TypeVar("_T_co", covariant=True)
---> 28 _DType = TypeVar("_DType", bound=np.dtype[Any])
29 _DType_co = TypeVar("_DType_co", covariant=True, bound=np.dtype[Any])
30 # A subset of `npt.DTypeLike` that can be parametrized w.r.t. `np.generic`
TypeError: 'numpy._DTypeMeta' object is not subscriptable
It is then simple to plot the outputs using the simple plotting tools for xarray’s datasets:
ds_out.burden.plot()
Or, for example, the global mean mole fraction at the surface could be plot using the ‘weighted’ functionality:
ds_out[species][:,0,:].weighted(np.cos(np.deg2rad(ds_out.lat))).mean("lat").plot()
To change how the emissions are distributed in space, we can allocate this by, for example:
emissions = model.create_emissions(species, emistot, distribute="gdp")
which distributes the 10 Gg of emissions by the latitudinal dependence on gdp.
Initial conditions (of dimensions altitude x latitude) can be passed to the model using the ics keyword, for example using the final mole fraction from the previous run:
ics = ds_out[f"{species}_end"][-1,:,:]
ds_out = model.run_model(years, emissions, sink, ics=ics)
ds_out[species][:,0,:].weighted(np.cos(np.deg2rad(ds_out.lat))).mean("lat").plot()
By far the slowest part of transport is the convection scheme as it involves solving a matrix.
If you want to speed up the run, there is little loss of accurace for many long-lived substances (lifetime of a year or more) by turning off the convective scheme by setting ics=False:
ds_out = model.run_model(years, emissions, sink, ics=ics, convection=False)
ds_out[species][:,0,:].weighted(np.cos(np.deg2rad(ds_out.lat))).mean("lat").plot()