Skip to content

Commit 451d0d5

Browse files
Merge pull request #266 from ESMValGroup/update_episode_8
Update episode 8 after testing with esmvaltool 2.7
2 parents fc3e250 + 3d5f2ee commit 451d0d5

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

_episodes/08-diagnostics.md

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Writing your own diagnostic script"
33
teaching: 20
44
exercises: 30
5-
compatibility: ESMValTool v2.4.0
5+
compatibility: ESMValTool v2.7.0
66

77
questions:
88
- "How do I write a new diagnostic in ESMValTool?"
@@ -43,13 +43,17 @@ Let's get started!
4343

4444
## Understanding an existing Python diagnostic
4545

46-
After a development mode installation, a folder called ``ESMValTool`` is created
47-
in your working directory. This folder contains the source code of the tool. We
48-
can find the recipe ``recipe_python.yml`` and the python script
49-
``diagnostic.py`` in these directories:
46+
If you clone the ESMValTool repository, a folder called ``ESMValTool`` is
47+
created in your home/working directory, see the instructions in the lesson
48+
[Development and contribution]({{ page.root }}{% link
49+
_episodes/07-development-setup.md %}).
5050

51-
- *path_to_ESMValTool/esmvaltool/recipes/examples/recipe_python.yml*
52-
- *path_to_ESMValTool/esmvaltool/diag_scripts/examples/diagnostic.py*
51+
The folder ``ESMValTool`` contains the source code of the tool. We can find the
52+
recipe ``recipe_python.yml`` and the python script ``diagnostic.py`` in these
53+
directories:
54+
55+
- *~/ESMValTool/esmvaltool/recipes/examples/recipe_python.yml*
56+
- *~/ESMValTool/esmvaltool/diag_scripts/examples/diagnostic.py*
5357

5458
Let's have look at the code in ``diagnostic.py``.
5559
For reference, we show the diagnostic code in the dropdown box below.
@@ -189,10 +193,10 @@ There are four main sections in the script:
189193
>> that holds all the necessary
190194
>> information needed to run the diagnostic script such as the location of input
191195
>> data and various settings. We will next parse this ``cfg`` variable
192-
>> in the ``main`` function and extract information as needed
196+
>> in the ``main`` function and extract information as needed
193197
>> to do our analyses (e.g. in line 69).
194198
>> 3. The ``main`` function is called near the very end on line 107. So, it is mentioned
195-
>> twice in our code - once where it is called by the top-level Python script and
199+
>> twice in our code - once where it is called by the top-level Python script and
196200
>> second where it is defined.
197201
> {: .solution}
198202
{: .challenge}
@@ -205,10 +209,10 @@ There are four main sections in the script:
205209
>
206210
{: .callout}
207211
208-
## Preprocesor-diagnostic interface
212+
## Preprocessor-diagnostic interface
209213
210214
In the previous exercise, we have seen that the variable ``cfg`` is the input
211-
argument of the ``main`` function. The first thing passed to the diagnostic
215+
argument of the ``main`` function. The first argument passed to the diagnostic
212216
via the ``cfg`` dictionary is a path to a file called ``settings.yml``.
213217
The ESMValTool documentation page provides an overview of what is in this file, see
214218
[Diagnostic script interfaces][interface].
@@ -221,12 +225,13 @@ The ESMValTool documentation page provides an overview of what is in this file,
221225
> then run the recipe ``recipe_python.yml``:
222226
>
223227
> ~~~bash
224-
> esmvaltool run recipe_example.yml
228+
> esmvaltool run examples/recipe_python.yml
225229
> ~~~
226230
>
227231
> 1. Find one example of the file ``settings.yml`` in the ``run`` directory?
228-
> 2. Take a look at the ``input_files`` list. It contains pathes to some files
229-
> ``metadata.yml``. What information do you think is saved in those files?
232+
> 2. Open the file ``settings.yml`` and look at the ``input_files`` list.
233+
> It contains paths to some files ``metadata.yml``. What information do you
234+
> think is saved in those files?
230235
>
231236
>> ## Answer
232237
>>
@@ -252,7 +257,7 @@ ESMValTool provides many functions such as ``select_metadata`` (line 72),
252257
``sorted_metadata`` (line 76), and ``group_metadata`` (line 80). As you can see
253258
in line 8, these functions are imported from ``esmvaltool.diag_scripts.shared``
254259
that means these are shared across several diagnostics scripts. A list of
255-
available functions and their description can be found in
260+
available functions and their description can be found in
256261
[The ESMValTool Diagnostic API reference][shared].
257262
258263
@@ -266,9 +271,9 @@ available functions and their description can be found in
266271
>> There is a statement after use of ``select_metadata``, ``sorted_metadata``
267272
>> and ``group_metadata`` that starts with ``logger.info`` (lines 73, 77 and
268273
>> 83). These lines print output to the log files. In the previous exercise, we
269-
>> ran the recipe ``recipe_python.yml``. If you look at the log
270-
>> file ``path_to_recipe_output/run/map/script1/log.txt``, you can see the output
271-
>> from each of these functions, for example:
274+
>> ran the recipe ``recipe_python.yml``. If you look at the log file
275+
>> ``recipe_python_#_#/run/map/script1/log.txt`` in ``esmvaltool_output``
276+
>> directory, you can see the output from each of these functions, for example:
272277
>>
273278
>>```
274279
>>2021-03-05 13:19:38,184 [34706] INFO diagnostic,83 Example of how to group and
@@ -325,9 +330,9 @@ available functions and their description can be found in
325330
326331
## Diagnostic computation
327332
328-
After grouping and selecting data, we can read individual attributes (such as filename)
329-
of each item. Here we have grouped the input data by ``variables``
330-
so we loop over the variables (line 89-93). Following this, is a call to the
333+
After grouping and selecting data, we can read individual attributes (such as filename)
334+
of each item. Here we have grouped the input data by ``variables``
335+
so we loop over the variables (line 89-93). Following this, is a call to the
331336
function ``compute_diagnostic`` (line 94). Let's have a look at the
332337
definition of this function in line 43 where the actual analysis on the data is done.
333338
@@ -336,7 +341,7 @@ Here, ``compute_diagnostic`` uses
336341
[Iris](https://scitools-iris.readthedocs.io/en/latest/index.html) to read data
337342
from a netCDF file and performs an operation ``squeeze`` to remove any dimensions
338343
of length one. We can adapt this function to add our own analysis. As an
339-
example, here we calculate the bias using the average of the data using Iris cubes.
344+
example, here we calculate the bias using the average of the data using Iris cubes.
340345
341346
~~~python
342347
def compute_diagnostic(filename):
@@ -391,7 +396,7 @@ def compute_diagnostic(filename):
391396
392397
> ## Reading data using the netCDF4 package
393398
>
394-
> Yet another option to read the NetCDF file data is to use
399+
> Yet another option to read the NetCDF file data is to use
395400
> the [netCDF-4 Python interface][netCDF] to the netCDF C library.
396401
>
397402
>> ## Answer
@@ -443,7 +448,7 @@ there:
443448
```
444449
445450
This way, we can pass arguments such as the type of
446-
plot ``pcolormesh`` and the colormap ``cmap:Reds`` from the recipe to the
451+
plot ``pcolormesh`` and the colormap ``cmap:Reds`` from the recipe to the
447452
``quickplot`` function in the diagnostic.
448453
449454
> ## Passing arguments from the recipe to the diagnostic
@@ -477,25 +482,25 @@ plot ``pcolormesh`` and the colormap ``cmap:Reds`` from the recipe to the
477482
478483
In our example, the function ``save_data`` in line 57 is used to save the Iris
479484
cube. The saved files can be found under the ``work`` directory in a ``.nc`` format.
480-
There is also the function ``save_figure`` in line 63 to save the plots under the
481-
``plot`` directory in a ``.png`` format (or preferred format specified in your
485+
There is also the function ``save_figure`` in line 63 to save the plots under the
486+
``plot`` directory in a ``.png`` format (or preferred format specified in your
482487
configuration settings). Again, you may choose your own method
483-
of saving the output.
488+
of saving the output.
484489
485490
### Recording the provenance
486491
487492
When developing a diagnostic script, it is good practice to record
488493
provenance. To do so, we use the function ``get_provenance_record`` (line 99).
489494
Let us have a look at the definition of this function in line 21 where we
490495
describe the diagnostic data and plot. Using the dictionary ``record``, it is
491-
possible to add custom provenance to our diagnostics output.
492-
Provenance is stored in the *W3C PROV XML*
496+
possible to add custom provenance to our diagnostics output.
497+
Provenance is stored in the *[W3C PROV XML](https://www.w3.org/TR/prov-xml/)*
493498
format and also in an *SVG* file under the ``work`` and ``plot`` directory. For
494499
more information, see [recording provenance][provenance].
495500
496501
## Congratulations!
497502
498-
You now know the basic diagnostic script structure and some available tools for putting
499-
together your own diagnostics. Have a look at existing recipes and diagnostics in the
503+
You now know the basic diagnostic script structure and some available tools for putting
504+
together your own diagnostics. Have a look at existing recipes and diagnostics in the
500505
repository for more examples of functions you can use in your diagnostics!
501506
{% include links.md %}

0 commit comments

Comments
 (0)