Debian Codegraph @

cylc/flow/etc/examples/external-data-files/index.rst

raw
1 Using Data To Define Your Workflow
2 ==================================
3
4 .. admonition:: Get a copy of this example
5 :class: hint
6
7 .. code-block:: console
8
9 $ cylc get-resources examples/external-data-files
10
11 We often want to read in a dataset for use in defining our workflow.
12
13 The :ref:`Cylc tutorial <tutorial-cylc-consolidating-configuration>` is an
14 example of this where we want one ``get_observations`` task for each of a list
15 of weather stations. Each weather station has a name (e.g. "heathrow") and an
16 ID (e.g. 3772).
17
18 .. code-block:: cylc
19
20 [runtime]
21 [[get_observations_heathrow]]
22 script = get-observations
23 [[[environment]]]
24 SITE_ID = 3772
25 [[get_observations_camborne]]
26 script = get-observations
27 [[[environment]]]
28 SITE_ID = 3808
29 [[get_observations_shetland]]
30 script = get-observations
31 [[[environment]]]
32 SITE_ID = 3005
33 [[get_observations_aldergrove]]
34 script = get-observations
35 [[[environment]]]
36 SITE_ID = 3917
37
38 It can be inconvenient to write out the name and ID of each station in your
39 workflow like this, however, you may already have this information in a more
40 convenient format (i.e. a data file of some form).
41
42 With Cylc, we can use :ref:`Jinja2 <Jinja>` to read in a data file and use that data to
43 define your workflow.
44
45
46 The Approach
47 ------------
48
49 This example has three components:
50
51 1. A JSON file containing a list of weather stations along with all the data
52 associated with them.
53
54 .. literalinclude:: stations.json
55 :language: json
56 :caption: stations.json
57
58 2. A Python function that reads the JSON file.
59
60 .. code-block:: python
61 :caption: lib/python/load_data.py
62
63 import json
64
65
66 def load_json(filename):
67 with open(filename, 'r') as json_file:
68 return json.load(json_file)
69
70 We put this Python code in the workflow's ``lib/python`` directory which
71 allows us to import it from within our workflow.
72
73 3. A ``flow.cylc`` file that uses the Python function to load the
74 data file.
75
76 We can import Python functions with Jinja2 using the following syntax:
77
78 .. code-block::
79
80 {% from "load_data" import load_json %}
81
82 For more information, see :ref:`jinja2.importing_python_modules`.
83
84
85
86 The Workflow
87 ------------
88
89 The three files are arranged like so:
90
91 .. code-block:: none
92 :caption: File Structure
93
94 |-- flow.cylc
95 |-- lib
96 | `-- python
97 | `-- load_data.py
98 `-- stations.json
99
100 The ``flow.cylc`` file:
101
102 * Imports the Python function.
103 * Uses it to load the data.
104 * Then uses the data to define the workflow.
105
106 .. literalinclude:: flow.cylc
107 :language: ini
108 :caption: flow.cylc
109
110
111 Data Types
112 ----------
113
114 We can load other types of data file too. This example also includes the same
115 data in CSV format along with a Python function to load CSV data. To try it
116 out, open the ``flow.cylc`` file and replace ``stations.json`` with
117 ``stations.csv`` and ``load_json`` with ``load_csv``.
118
119 Any Python code that you import using Jinja2 will be executed using the Python
120 environment that Cylc is running in. So if you want to import Python code that
121 isn't in the standard library, you may need to get your system administrator to
122 install this dependency into the Cylc environment for you.