Exercise #1 - Familiarisation with the System
Objectives
Familiarisation with local course computing environment
Make sure things work for everybody, and resolve problems early
Light exposure to Python and objects in Python
Familiarisation with local environment
Review the course logistics page and make sure you can log in to the vdmmss-dev system with the userid and password provided to you in class.
Many of the exercises are easier to coordinate if you have multiple windows to switch between. Make sure that once you log in to vdmmss-dev
ssh -X vdmmss-dev -l kurs1 (or 2, 3, ...)
OR
ssh -X kurs1@vdmmss-dev
you can open multiple xterms as follows - from your vdmmss-dev login window, issue the following commend, once for each xterm that you want
$ xterm&
If you want larger windows, you can specify a font to use - for example
$ xterm -font 9x15& (other fonts include 6x13, 7x14, 8x16, etc. a full list is available with the xlsfonts Unix command)
Try to open 3 or 4 windows
Hello, World
Using an editor of your choice, edit the following file, hello.py, replacing "World" with your name.
print 'Hello, World!!'
This is also available to you, already, at the bottom of this page and in
/home/kurs/donmor/Files/Exercise01/
If you use the version I created, you will need to copy it into your own directory.
Then, make sure you can run the file from the command line as follows
$ python hello.py
Hello, World!!
Then, you should make sure you understand the difference between the Python shell (or interpreter), and IPython by running the program as follows
Python interpreter (shell)
$ python
Python 2.7.11 |Anaconda 2.1.0 (64-bit)| (default, Dec 6 2015, 18:08:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> execfile('hello.py')
Hello, World!!
>>>
To exit the Python interpreter, enter quit() or CTRL-D
ipython
$ ipython
Python 2.7.11 |Anaconda 2.1.0 (64-bit)| (default, Dec 6 2015, 18:08:32)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.1 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: %run hello.py
Hello, World!!
In [2]:
And, again, you can exit with quit() or CTRL-D
matplotlib and pylab
Invoke the python shell and issue commands like the following to insure that all works, and to gain familiarity with an object-oriented usage of the environment
$ python
>>> import pylab as pl
>>> import numpy as np
>>> x = np.arange(0.0, 1.0, 0.01)
>>> fig = pl.figure()
>>> ax1 = fig.add_subplot(111)
>>> ax1.plot(x, np.sin( 2*np.pi*x ))
[<matplotlib.lines.Line2D object at 0x7f9aaaa7a910>]
>>> ax1.set_ylim( (-1,1) )
(-1, 1)
>>> ax1.set_title('A sine wave')
<matplotlib.text.Text object at 0x7f9aaaac09d0>
>>> pl.show()
Testing of gribapi
We just want to make sure that you have access to the GRIB Tools, data files, and can access the data through the gribapi Python module.
The test file, forecast geopotential heights from an Alaska operational WRF run, is located at
/home/kurs/donmor/Files/Exercise01/alaska.gr2
(also available to download at bottom of this page)
First, make sure you can access the metadata with grib_ls and grib_dump tools available on the system
$ grib_ls
/home/kurs/donmor/Files/Exercise01/alaska.gr2 | less
$ grib_dump /home/kurs/donmor/Files/Exercise01/alaska.gr2 | less
Then, start the Python shell and try to interact with the data file as follows:
>>> import gribapi
>>> gribFH = open('/home/kurs/donmor/Files/Exercise01/alaska.gr2', 'r')
>>> gribapi.grib_count_in_file(gribFH)
9
>>> gid = gribapi.grib_new_from_file(gribFH)
>>> gribapi.grib_get(gid, 'shortName')
'gh'
>>> gribapi.grib_get(gid, 'level')
300
>>> gribapi.grib_get(gid, 'typeOfLevel')
'isobaricInhPa'
Testing of graphics production
Copy the file
/home/kurs/donmor/Files/Exercise01/gfsplot.py
into your own directory. This file, and the GFS data file that it needs, is also available at the bottom of this page.
Note - you may have to change the variable GRIB_FILE in this program so that it points to a correct path.
Then, run the Python program as follows
python gfsplot.py
and ensure that after a few seconds you see a graphic which looks like the following
CherryPy
Make sure the CherryPy web server works for you:
First, create Python program hellocherry.py , editing SERVER_PORT for YOUR specific port number. If your userid is kurs1, you should use port 50001, if it is kurs2, you should use port 50002, and so on.
This file is available at
/home/kurs/donmor/Files/Exercise01/hellocherry.py
as well as at the bottom of this page
import cherrypy
SERVER_PORT = 50000
class HelloWorld(object):
def index(self):
return "Hello Cherry Pie!"
index.exposed = True
cherrypy.config.update({'server.socket_host': '0.0.0.0',
'server.socket_port': SERVER_PORT,
})
cherrypy.quickstart(HelloWorld())
Run hellocherry.py to start the web server,
$ python hellocherry.py
[09/Aug/2014:18:06:27] ENGINE Listening for SIGHUP.
[09/Aug/2014:18:06:27] ENGINE Listening for SIGTERM.
[09/Aug/2014:18:06:27] ENGINE Listening for SIGUSR1.
[09/Aug/2014:18:06:27] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[09/Aug/2014:18:06:27] ENGINE Started monitor thread 'Autoreloader'.
[09/Aug/2014:18:06:27] ENGINE Started monitor thread '_TimeoutMonitor'.
[09/Aug/2014:18:06:27] ENGINE Serving on http://0.0.0.0:50000
[09/Aug/2014:18:06:27] ENGINE Bus STARTED
then go to a web browser on your local machine and try to connect to
http://vdmmss-dev:5000n
where "n" is your particular port. Verify that you can see the message Hello Cherry Pie! on the web page.
Finally, from another window on vdmmss-dev, verify that you can use curl to connect with the web server, as follows (using your own port number instead of the 50000 shown below):
$ curl localhost:50000
Hello Cherry Pie!$