1. Basics

This is a basic introduction to using pyarts

It will take you through the following concepts:

  1. Creating a workspace

  2. Setting common variables on the workspace

  3. Loading data from files

  4. Running methods using the workspace

  5. Create a custom Agenda

1. Create a workspace

This example shows how to create a Workspace, the common entry point for all pyarts simulations.

 1# The first step is to import the pyarts package. This will make all
 2# pyarts modules available to you.
 3import pyarts
 4
 5# The Workspace class is the common entry point for all pyarts simulations.
 6# It is used to interface with the ARTS C++ library and to manage the
 7# simulation state.
 8ws = pyarts.Workspace()
 9
10# That's it. You have created a Workspace. You can now use it to run
11# simulations. For example, you can run the ARTS Hello World example:
12print("Hello, World!\n", ws)

2. Setting workspace variables

This example shows how to set a workspace variable. The way workspace variables are set depends on the constructors or __init__ methods available for the respective ARTS classes. We hope that most of our constructors make pythonic sense

 1# Import the module
 2import pyarts
 3import numpy as np  # For some of the examples
 4
 5
 6# Create a workspace
 7ws = pyarts.Workspace()
 8
 9"""
10Indices can be set from the built-in python int.
11
12It is necessary to create an index object first, since the workspace
13variable must know the type.
14"""
15ws.ind = pyarts.arts.Index(1)
16print("Should contain 1:           ", ws.ind)
17
18"""
19Once an index exist on the workspace, you can set it directly from int.
20"""
21ws.ind = 2
22print("Should contain 2:           ", ws.ind)
23
24
25"""
26What follows are some examples of how to set other types of workspace
27variables.  The general rule is that you can set a workspace variable
28from any python object that has a corresponding ARTS type.  For
29example, you can set a string from a python string, a matrix from a
30numpy array, etc.
31"""
32
33# Numeric
34ws.num = pyarts.arts.Numeric(9.81)  # from builtin float
35print("Should contain 9.81:        ", ws.num)
36
37ws.num = 10   # from builtin int
38print("Should contain 10:          ", ws.num)
39
40ws.num = np.float64(9.81)  # from numpy float
41print("Should contain 9.81, again: ", ws.num)
42
43# Numeric
44ws.str = pyarts.arts.String("hello")  # from builtin str
45print("Should contain 'hello':     ", ws.str)
46
47ws.str = b"ARTS"  # from bytes
48print("Should contain 'ARTS':      ", ws.str)
49
50
51# Vector
52ws.vec = pyarts.arts.Vector([1, 2, 3])  # from builtin list
53print("Should contain 1 2 3:       ", ws.vec)
54
55ws.vec = np.array([4, 5, 6])  # from numpy array
56print("Should contain 4 5 6:       ", ws.vec)
57
58
59# Matrix
60ws.mat = pyarts.arts.Matrix([[1, 2], [3, 4]])  # from builtin list
61print("Should contain\n 1 2\n3 4:\n", ws.mat)
62
63ws.mat = np.array([[5, 6], [7, 8]])  # from numpy array
64print("Should contain\n 5 6\n7 8:\n", ws.mat)
65
66"""
67Many more types are supported.  Please look at the documentation of the
68respective Workspace Group for more information on how to initialize them.
69"""
70
71# TESTING
72# AS THIS FILE IS RUN TO TEST ARTS, WE NEED TO CHECK THAT
73# THE CONTENT OF THE VARIABLES ARE GOOD.
74assert np.isclose(ws.ind, 2)
75assert np.isclose(ws.num, 9.81)
76assert np.allclose(ws.vec, [4, 5, 6])
77assert np.allclose(ws.mat, [[5, 6], [7, 8]])
78# END TESTING

3. Loading data from file

This example shows how to load data from a file into a workspace variable.

 1# Import the module
 2import pyarts
 3
 4
 5# Create a workspace
 6ws = pyarts.Workspace()
 7
 8
 9"""
10Before we load data into a workspace variable, we need to know about
11one of the tricks that ARTS uses to make the file IO more flexible.
12
13The trick is that ARTS does not just look for files in the folder
14that you specify, but also in a number of other folders.  This is
15useful since a lot of radiative transfer data is the same regardless
16of the scenario that you are simulating.  For example, the absorption
17lines of H2O are the same regardless of whether you are simulating
18a clear sky or a cloudy sky.
19
20The folders that ARTS looks in are called search paths.  You can
21print the search paths that ARTS uses by doing:
22"""
23print("ARTS search paths:", pyarts.arts.globals.parameters.datapath)
24
25
26"""
27The results of this command will depend on your environment variables
28at the time of first import of pyarts during the scripting.  The default
29search paths can be changed by setting the ARTS_DATA_PATH environment
30variable.  For example, if you want to add a folder called "my_data"
31to the search paths, you can do:
32
33export ARTS_DATA_PATH=/my_data
34
35before starting python.  If you want to add multiple folders, you can
36separate them with a colon:
37export ARTS_DATA_PATH=/my_data:/my_other_data
38
39Check the documentation for your shell if you want to make this change
40permanent.
41
42It is always possible to add additional search paths from within python:
43"""
44pyarts.arts.globals.parameters.datapath.append("/my_data")
45print("ARTS search paths:", pyarts.arts.globals.parameters.datapath)
46
47
48"""
49The following examples assumes that you have a copy of the arts-cat-data
50and arts-xml-data repositories in the search paths.  If you do not have
51these, you can download them from: https://www.radiativetransfer.org/tools/
52
53It demonstrates different ways that you can load data from a file into
54the workspace.  There is no "best" way to do this, it depends on the
55context, and what you find most readable.
56"""
57
58# Call the WorkspaceVariable member method "readxml" to load data from file
59ws.absorption_bands.readxml("lines/O2-66.xml")
60
61# TESTING
62# AS THIS FILE IS RUN TO TEST ARTS, WE NEED TO CHECK THAT
63# THE CONTENT OF THE VARIABLES ARE GOOD.
64assert len(ws.absorption_bands) > 0
65assert "/my_data" in pyarts.arts.globals.parameters.datapath
66# END TESTING

4. Running a workspace method

This example demonstrates how to run a workspace method. We have shown this in previous examples, but here we will explain some details.

 1# Import the module
 2import pyarts
 3
 4
 5# Create a workspace
 6ws = pyarts.Workspace()
 7
 8
 9"""
10All workspace methods can take pure python instance or workspace variables
11"""
12example_index = pyarts.arts.Index(1)
13ws.example_index = example_index
14print(example_index)
15print(ws.example_index)
16print(type(ws.example_index), type(example_index))
17
18"""
19You can call workspace methods with named or positional arguments or any
20combination thereof that python accepts
21"""
22# 1)  Name some arguments
23ws.spectral_radiance_space_agendaSet(
24    option="UniformCosmicBackground"
25)
26# 2)  Name some arguments, use positional for the others
27ws.spectral_radiance_space_agendaSet(
28    ws.spectral_radiance_space_agenda,
29    option="UniformCosmicBackground",
30)
31# 3)  Name all arguments
32ws.spectral_radiance_space_agendaSet(
33    spectral_radiance_space_agenda=ws.spectral_radiance_space_agenda,
34    option="UniformCosmicBackground",
35)
36# 4)  Use all positional arguments
37ws.spectral_radiance_space_agendaSet(
38    ws.spectral_radiance_space_agenda, "UniformCosmicBackground"
39)

5. Create an agenda

This will explain how to create an agenda and set it on the workspace.

 1# Import the module
 2import pyarts
 3
 4
 5# Create a workspace
 6ws = pyarts.Workspace()
 7
 8
 9"""
10Agendas are a collection of functions that define the simulation
11of some core component.  For example, the iy_space_agenda defines
12how the radiative transfer equation receives radiation from a
13background of space --- commonly just the cosmic background radiation.
14
15These Agendas are defined from a set of common inputs and outputs.
16
17Agendas must consist of methods that combined covers the inputs and
18outputs of the agenda itself.  While it is possible to create an
19agenda manually, it is much more preferable to use the workspace
20methods that exist to create the agenda for you.
21
22Before showing you how to creating your own iy_space_agenda,
23this method call can do it for you, and it will probably do
24it faster and safer than any manual approach:
25"""
26ws.spectral_radiance_space_agendaSet(
27    option="UniformCosmicBackground"
28)
29
30"""
31That said, we provide interpreted ways to create agendas manually.
32The cosmic background radiation agenda of the method call above
33can be created by the following, decorated code:
34"""
35
36
37@pyarts.workspace.arts_agenda
38def cosmic_background(ws):
39    ws.spectral_radianceUniformCosmicBackground(
40        spectral_radiance=ws.spectral_radiance,
41        frequency_grid=ws.frequency_grid,
42    )
43    ws.spectral_radiance_jacobianEmpty()
44    ws.Ignore(ws.ray_path_point)
45
46
47ws.spectral_radiance_space_agenda = cosmic_background
48print(cosmic_background)
49print(ws.spectral_radiance_space_agenda)
50
51
52"""
53The local namespace of your python instance will now contain
54the variable "cosmic_background".  This is of type Agenda but is unchecked.
55The assignment to "ws.iy_space_agenda" will cause the agenda to
56be created, set, and checked on the current workspace.  This checking might
57fail if, for instance, a required input or output is missing.
58
59It is possible to create a checked agenda that lives on the both the python
60and on the pyarts workspace by specifying which workspace it should be set
61on as a named argument to the agenda decorator:
62"""
63
64
65@pyarts.workspace.arts_agenda(ws=ws)
66def spectral_radiance_space_agenda(ws):
67    ws.spectral_radianceUniformCosmicBackground()
68    ws.spectral_radiance_jacobianEmpty()
69    ws.Ignore(ws.ray_path_point)
70
71
72print(spectral_radiance_space_agenda)
73print(ws.spectral_radiance_space_agenda)
74
75"""
76Lastly, an advanced feature to skip typing ignores is to tell the property to
77deal with this for you by fixing the agenda.  Note that this might give you bad
78results if the agenda is ever changed in a future update of ARTS as the method
79will simply append all new input.  Still, it is convenient:
80"""
81
82@pyarts.workspace.arts_agenda(ws=ws, fix=True)
83def spectral_radiance_space_agenda(ws):
84    ws.spectral_radianceUniformCosmicBackground()
85    ws.spectral_radiance_jacobianEmpty()
86
87print(spectral_radiance_space_agenda)