Purpose: Basic familiarisation with Python and lab environment
These exercises utilise data and programs that have been pre-packaged for this course. In the following,
<path to course>
refers to the path
/home/kurs/donmor/CourseFiles/
Hello, World - edit and run
Open the Python shell/interpreter and run the simple command
print "Hello, World"
Edit a file, hello.py and run as
python hello.py
Insert the following as the first line in hello.py: #!/usr/bin/env python and then
chmod +x hello.py
./hello.py
Edit hello.py so that it looks more like a normal Python program, and run:
#!/usr/bin/env python
import sys
def main():
print 'Hello, World'
sys.exit("Exiting normally...")
#------------------------
if __name__ == '__main__':
main()
Make the previous program a little more complicated by accepting a command line argument, then execute with and without arguments
#!/usr/bin/env python
import sys
def main(argv = sys.argv):
if len(argv) > 1:
theName = argv[1]
else:
theName = "World"
print 'Hello, ' + theName
sys.exit("Exiting normally...")
#------------------------
if __name__ == '__main__':
main()
Write the following program, hellomean.py (or whatever you want to call it) and execute with various arguments
#!/usr/bin/env python
import sys
def main( arglist ):
print 'arglist: ' + str( arglist )
sum = 0.0
counter = 0
if len(arglist) > 1:
for x in arglist[1:]:
sum = sum + float(x)
counter = counter + 1
mean = sum / counter
else:
mean = 0.0
print 'mean: ' + str(mean)
#----------------------------------
if __name__ == '__main__':
main( sys.argv )
Experimenting with the Python shell and making sure matplotlib and grib-api interfaces work
Start the Python shell/interpreter by typing python at the command line (Note - Ctrl-D exits the shell)
Experiment with some simple data structures and operations, and note the pitfalls of integer division. Enter the following in the shell:
>>> mylist = [1, 2, 3, 4, 1]
>>> mysum = sum(mylist)
>>> print mysum
>>> mymean = mysum / len(mylist)
>>> print mymean
and then, notice that if at least one operand is a float, the right hand side will evaluate to a float
>>> mysum = float(mysum)
>>> mymean = mysum / len(mylist)
>>> print mymean
Besides learning a couple of things about Python and integer/float arithmetic, this and the following exercise should impress upon you that the Python shell is often a good place to experiment with small sections of code
Quick test and exposure of matplotlib and numpy
The following demonstrates how to plot a series of y-values. The x-values are implicitly assumed to be the indices of the list - in other words [0, 1, 2, 3]
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,2,3,4])
[<matplotlib.lines.Line2D object at 0x3582b50>]
>>> plt.show()
Next, let's set explicit x and y values to plot
>>> x = [1,2,3,4]
>>> y = [1,4,9,16]
>>> plt.plot(x,y)
>>> plt.show()
Then, try the following operation on x and notice the error message, implying that we can't perform a numerical group operation on a list.
>>> z = x**3
We can, however, perform such an operation on a NumPy array, and that's what we do in the following:
>>> import numpy as np
>>> x = np.array([1,2,3,4])
>>> x
>>> z = x**3
>>> z
>>> plt.plot(x,z)
>>> plt.show()
Finally, a grib-api exercise, simply making sure we can access a GFS 0.5 degree GRIB2 file and get its dimensions
>>> import gribapi
>>> FILENAME = '<path to course>/LabExerciseData/GFS0p5/GFS2012061600.gr2'
>>> f = open(FILENAME)
>>> gid = gribapi.grib_new_from_file(f)
>>> nx = gribapi.grib_get(gid,'Ni')
>>> nx
>>> ny = gribapi.grib_get(gid,'Nj')
>>> ny
>>> print nx, ny
Then, as a final exercise, put the previous program in a source file ( filename.py ) and execute it. Note that, as it stands now, you will have a couple of errors. See if you can resolve them.