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

  6. Convert an existing ARTS controlfile

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:
12ws.Print("Hello, World!", 0)

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.example_index = pyarts.arts.Index(1)
16print("Should contain 1:           ", ws.example_index)
17
18"""
19Once an index exist on the workspace, you can set it directly from int.
20"""
21ws.example_index = 2
22print("Should contain 2:           ", ws.example_index)
23
24"""
25There are several predefined indices in ARTS.  For example, the ybatch_index.
26You do not have to create an object for these as the type is already known.
27"""
28ws.ybatch_index = 3
29print("Should contain 3:           ", ws.ybatch_index)
30
31
32"""
33Lastly, there exist a workspace method to set indices.  To-date, this is
34the only way to set the value of indices inside an Agenda.
35"""
36ws.IndexSet(ws.ybatch_index, 4)
37print("Should contain 4:           ", ws.ybatch_index)
38
39
40"""
41What follows are some examples of how to set other types of workspace
42variables.  The general rule is that you can set a workspace variable
43from any python object that has a corresponding ARTS type.  For
44example, you can set a string from a python string, a matrix from a
45numpy array, etc.
46
47All of the variables below are initialization-time known workspace variables,
48so there is no need to create the corresponding pyarts class instance first.
49"""
50
51# Numeric
52ws.g0 = 9.81  # from builtin float
53print("Should contain 9.81:        ", ws.g0)
54
55ws.g0 = 10   # from builtin int
56print("Should contain 10:          ", ws.g0)
57
58ws.g0 = np.float64(9.81)  # from numpy float
59print("Should contain 9.81, again: ", ws.g0)
60
61
62# String
63ws.iy_unit = "dB"  # from builtin str
64print("Should contain dB:          ", ws.iy_unit)
65
66ws.iy_unit = b'avc'  # from builtin bytes
67print("Should contain avc:         ", ws.iy_unit)
68
69
70# Vector
71ws.f_grid = [1, 2, 3]  # from builtin list
72print("Should contain 1 2 3:       ", ws.f_grid)
73
74ws.f_grid = np.array([4, 5, 6])  # from numpy array
75print("Should contain 4 5 6:       ", ws.f_grid)
76
77
78# Matrix
79ws.iy = [[1, 2], [3, 4]]  # from builtin list
80print("Should contain\n 1 2\n3 4:\n", ws.iy)
81
82ws.iy = np.array([[5, 6], [7, 8]])  # from numpy array
83print("Should contain\n 5 6\n7 8:\n", ws.iy)
84
85"""
86Many more types are supported.  Please look at the documentation of the
87respective Workspace Group for more information on how to initialize them.
88"""
89
90# TESTING
91# AS THIS FILE IS RUN TO TEST ARTS, WE NEED TO CHECK THAT
92# THE CONTENT OF THE VARIABLES ARE GOOD.
93assert np.isclose(ws.example_index.value.value, 2)
94assert np.isclose(ws.ybatch_index.value.value, 4)
95assert np.isclose(ws.g0.value.value, 9.81)
96assert ws.iy_unit.value == "avc"
97assert np.allclose(ws.f_grid.value, [4, 5, 6])
98assert np.allclose(ws.iy.value, [[5, 6], [7, 8]])
99# 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# TESTING
48# AS THIS FILE IS RUN TO TEST ARTS, WE NEED TO CHECK THAT
49# THE CONTENT OF THE VARIABLES ARE GOOD.
50assert pyarts.arts.globals.parameters.datapath.index("/my_data")
51# END TESTING
52
53"""
54The following examples assume that you have a copy of the arts-cat-data
55and arts-xml-data repositories in the search paths.
56
57The easiest way to get the correct catalogs for your current ARTS version is
58by calling the `pyarts.cat.download.retrieve` function.
59The function will not only download the catalogs, but also set the search path
60to the download location (default is `~/.cache/arts/`).
61
62There are different ways to load data from a file into the workspace.
63There is no "best" way to do this, it depends on the context, and what you find
64most readable.
65"""
66
67# Download ARTS catalogs matching the current pyarts version
68pyarts.cat.download.retrieve()
69
70# Call the WorkspaceVariable member method "readxml" to load data from file
71ws.abs_lines.readxml("lines/O2-66.xml")

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
14ws.Print(example_index, 0)
15ws.Print(ws.example_index, 0)
16print(type(ws.example_index), type(example_index))
17
18"""
19You can call workspace methods with named or unnamed arguments or any combination
20thereof that python accepts
21"""
22ws.iy_space_agendaSet(ws.iy_space_agenda, "CosmicBackground") # Unnamed
23ws.iy_space_agendaSet(ws.iy_space_agenda, option="CosmicBackground") # Mixed
24ws.iy_space_agendaSet(option="CosmicBackground", iy_space_agenda=ws.iy_space_agenda) # Named
25
26
27"""
28All arguments that the method marks as output can be omitted.  The
29following are equivalent:
30"""
31ws.iy_space_agendaSet(ws.iy_space_agenda, option="CosmicBackground")
32ws.iy_space_agendaSet(option="CosmicBackground") # Omitted iy_space_agenda
33
34"""
35All arguments that are marked as input must be provided.  An alternative
36way to provide input arguments is to set the corresponding workspace
37variable before calling the method.  The following are equivalent:
38"""
39ws.z_surfaceConstantAltitude(lat_grid=[0, 1, 2], lon_grid=[3, 4, 5])
40print("A 3-by-3 matrix of zeroes:\n", ws.z_surface)
41ws.lat_grid = [0, 1, 2]
42ws.lon_grid = [3, 4, 5]
43ws.z_surfaceConstantAltitude()
44print("A 3-by-3 matrix of zeroes:\n", ws.z_surface)
45
46"""
47Some methods take input arguments that do not have a corresponding
48workspace variable.  These arguments may have a default value, or they
49may be required.  The following is equivalent to the last call of z_surfaceConstantAltitude:
50"""
51ws.z_surfaceConstantAltitude(altitude=0)
52print("A 3-by-3 matrix of zeroes:\n", ws.z_surface)
53
54# TESTING
55# AS THIS FILE IS RUN TO TEST ARTS, WE NEED TO CHECK THAT
56# THE CONTENT OF THE VARIABLES ARE GOOD.
57assert (ws.z_surface.value == 0).all()
58# END TESTING

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.iy_space_agendaSet(option="CosmicBackground")
 27
 28"""
 29That said, we provide interpreted ways to create agendas manually.
 30The cosmic background radiation agenda of the method call above
 31can be created by the following, decorated code:
 32"""
 33@pyarts.workspace.arts_agenda
 34def cosmic_background(ws):
 35    ws.MatrixCBR(output=ws.iy, f=ws.f_grid)
 36    ws.Ignore(ws.rtp_pos)
 37    ws.Ignore(ws.rtp_los)
 38ws.iy_space_agenda = cosmic_background
 39print("cosmic_background:", type(cosmic_background))
 40print("ws.iy_space_agenda:", type(ws.iy_space_agenda.value))
 41
 42"""
 43The local namespace of your python instance will now contain
 44the variable "cosmic_background".  This is of type DelayedAgenda.
 45The assignment to "ws.iy_space_agenda" will cause the agenda to
 46be created and set on the workspace.  DelayedAgenda:s are useful
 47when you want to set up your agendas independently of the workspace.
 48
 49It is possible to create an actual Agenda object by naming the
 50workspace input as a named-argument to the decorator:
 51"""
 52@pyarts.workspace.arts_agenda(ws=ws)
 53def cosmic_background_instant(ws):
 54    ws.MatrixCBR(output=ws.iy, f=ws.f_grid)
 55    ws.Ignore(ws.rtp_pos)
 56    ws.Ignore(ws.rtp_los)
 57ws.iy_space_agenda = cosmic_background_instant
 58print("cosmic_background_instant:", type(cosmic_background_instant))
 59
 60"""
 61The "cosmic_background_instant" variable is now an Agenda instance and
 62can be used only with the named workspace instance.
 63
 64You can also just set the agenda to the workspace directly by providing
 65the "set_agenda=True" argument to the decorator:
 66"""
 67@pyarts.workspace.arts_agenda(ws=ws, set_agenda=True)
 68def cosmic_background_set(ws):
 69    ws.MatrixCBR(output=ws.iy, f=ws.f_grid)
 70    ws.Ignore(ws.rtp_pos)
 71    ws.Ignore(ws.rtp_los)
 72ws.iy_space_agenda = cosmic_background_set
 73print("ws.cosmic_background_set:", type(ws.cosmic_background_set.value))
 74
 75"""
 76Now the type of the "cosmic_background_set" variable is actually a
 77WorkspaceVariable, it lives already on the workspace and it has
 78defined "ws.cosmic_background_set".  Note that this is not very
 79useful as Agenda:s must be named properly to work in Arts.  Their
 80names themselves carry a lot of meaning for the internal controlflow.
 81
 82
 83In fact, it is better to simply name the decorated function
 84"iy_space_agenda" as this will cause the agenda to be set on
 85the workspace automatically:
 86"""
 87@pyarts.workspace.arts_agenda(ws=ws, set_agenda=True)
 88def iy_space_agenda(ws):
 89    ws.MatrixCBR(output=ws.iy, f=ws.f_grid)
 90    ws.Ignore(ws.rtp_pos)
 91    ws.Ignore(ws.rtp_los)
 92
 93"""
 94Finally, it is not possible by default to create an agenda that
 95calls pure python functions.  This is because the agenda must
 96know its inputs and outputs.  If you really want an agenda that
 97can call pure python functions, you can do it by providing the
 98"allow_callbacks=True" argument to the decorator:
 99"""
100@pyarts.workspace.arts_agenda(ws=ws, set_agenda=True, allow_callbacks=True)
101def iy_space_agenda(ws):
102    print("Running iy_space_agenda")
103    ws.MatrixCBR(output=ws.iy, f=ws.f_grid)
104    ws.Ignore(ws.rtp_pos)
105    ws.Ignore(ws.rtp_los)
106
107"""
108Note that this is not recommended, as it is not possible to isolate inputs
109from outputs when callbacks are involved.  This means that the agenda
110above may leak any changes it does to the workspace to the outside world.
111
112The whole point of the Agenda system is to control the inputs and outputs
113of the simulation, so it is not recommended to make use of this feature
114as it is too powerful in scope.
115"""

6. Convert controlfile

To take a legacy ARTS controlfile (.arts file) as a starting point, you can use the arts_convert.py script to convert it to a Python script. The script is included with the conda package and should be available in your path. For example:

arts_convert.py mycontrolfile.arts

This will create a Python script called mycontrolfile.py which you can then edit and run.

Warning

Before converting your controlfile, make sure it is compatible with the current version of ARTS. Otherwise, the conversion may fail.

ARTS Controlfile:

Arts2 {
    VectorNLinSpace( f_grid, 5, 320e9, 322e9 )
}

Python conversion:

1import numpy as np
2import pyarts
3from pyarts.workspace import Workspace, arts_agenda
4ws = Workspace(verbosity=0)
5ws.VectorNLinSpace(ws.f_grid, 5, 320000000000.0, 322000000000.0)