You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/scripts.rst
+26-17Lines changed: 26 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,33 +67,39 @@ Exercises 1
67
67
.. highlight:: console
68
68
69
69
70
-
1. Download the :download:`weather_observations.ipynb <../resources/code/scripts/weather_observations.ipynb>` and the weather_data file and upload them to your jupyterlab. The script plots the temperature data for Tapiola in Espoo. The data is originally from `rp5.kz <https://rp5.kz>`_ and was slightly adjusted for this lecture.
70
+
1. Download the :download:`weather_observations.ipynb <../resources/code/scripts/weather_observations.ipynb>` and the weather_data file and upload them to your Jupyterlab. The script plots the temperature data for Tapiola in Espoo. The data is originally from `rp5.kz <https://rp5.kz>`_ and was slightly adjusted for this lecture.
71
71
72
-
**Note:** If you haven't downloaded the file directly to your jupyterlab folder, it will be located in your **Downloads** folder or the folder you selected. In jupyterlab click on the 'upload file' button, navigate to the folder containing the file and select it to load it into your jupyterlab folder.
72
+
**Note:** If you haven't downloaded the file directly to your Jupyterlab folder, it will be located in your **Downloads** folder or the folder you selected. In Jupyterlab click on the 'upload file' button, navigate to the folder containing the file and select it to load it into your Jupyterlab folder.
73
73
74
-
2. Open a terminal in jupyter (File -> New -> Terminal).
74
+
2. Open a terminal in Jupyter (File → New → Terminal).
75
75
76
-
3. Convert the jupyter script to a python script by calling::
76
+
3. Convert the Jupyter script to a Python script by calling::
4. Run the script (note: you may have ``python3`` rather than ``python``)::
81
81
82
82
$ python weather_observations.py
83
83
84
-
Command line arguments with ``sys.argv``
85
-
----------------------------------------
84
+
Command line arguments with :data:`sys.argv`
85
+
--------------------------------------------
86
86
87
-
We now have a python script that is callable from the command line (e.g. for use on an HPC system).
87
+
We now have a Python script that is callable from the command line (e.g. for use on an HPC system).
88
88
However, this code is still not adjustable, as we still need to have a copy for each single
89
89
time range we want to plot, or need to modify our file whenever we want to just change parameters.
90
-
What we need is to allow arguments to be put in from the command line in order to have the same code
91
-
plot information for different time ranges without odifying the code itself. This can be achieved by
92
-
using pythons:py:mod:`sys` package, which provides access to arguments given to the python interpreter at
93
-
startup in the :py:data:`sys.argv` list. The first (i.e. ``sys.argv[0]`` entry of this array is the called script,
94
-
and any further argument (separated by space) is appended to this list. Lets see how it works:
90
+
**What we need is to allow the code to do something different based on something outside the code itself**: in this case, to
91
+
plot information for different time ranges. This can be achieved by
92
+
using Pythons:py:mod:`sys` package, which provides access to arguments given to the Python interpreter at
93
+
startup in the :py:data:`sys.argv` list. The first (i.e. ``sys.argv[0]`` entry of this array is the script that is running,
94
+
and any further argument (separated by space) is appended to this list, like such:
95
95
96
-
We modify the **weather_observations.py** script such that we allow start
96
+
.. code-block:: console
97
+
98
+
$ python my_script.py A B
99
+
$ # sys.argv[1] is 'A'
100
+
$ # sys.argv[2] is 'B'
101
+
102
+
Lets see how it works: We modify the **weather_observations.py** script such that we allow start
97
103
and end times as well as the output file to be passed in as arguments to the function:
98
104
99
105
.. code-block:: python
@@ -194,12 +200,15 @@ Exercises 2
194
200
195
201
.. challenge:: Scripts-2
196
202
197
-
1. Take the python script we have written in the preceding exercise and use
203
+
1. Take the Python script (``weather_observations.py``) we have written in the preceding exercise and use
198
204
:py:mod:`argparse` to specify the input and output files and allow the start and end dates to be set.
199
-
The start and end dates should be optional parameters with the defaults as they are in the current script.
200
205
201
-
2. Execute your script for a few different time intervals (e.g. from January 2019 to June 2020, or from Mai 2020 to October 2020).
202
-
Also use data for cairo (``https://raw.githubusercontent.com/AaltoSciComp/python-for-scicomp/master/resources/data/scripts/weather_cairo.csv``)
206
+
* Hint: try not to do it all at once, but add one or two arguments, test, then add more, and so on.
207
+
* Hint: The input and output filenames make sense as positional arguments, since they must always be given. Input is usually first, then output.
208
+
* Hint: The start and end dates should be optional parameters with the defaults as they are in the current script.
209
+
210
+
2. Execute your script for a few different time intervals (e.g. from January 2019 to June 2020, or from May 2020 to October 2020).
211
+
Also try using this data for Cairo: ``https://raw.githubusercontent.com/AaltoSciComp/python-for-scicomp/master/resources/data/scripts/weather_cairo.csv``
0 commit comments