#!/usr/bin/env python # coding: utf-8 # # 2.18 Programming for Geoscientists class test - 14:00-16:00 18th December 2014 # #Test instructions # # * This test contains **5** questions each of which should be answered. # * Write your program in a Python cell just under each question. # * You can write an explanation of your solution as comments in your code. # * In each case your solution program must fulfil all of the instructions - please check the instructions carefully and double check that your program fulfils all of the given instructions. # * Save your work regularly. # * At the end of the test you should email your IPython notebook document (i.e. this document) to [Gerard J. Gorman](http://www.imperial.ac.uk/people/g.gorman) at g.gorman@imperial.ac.uk # **Question 1** # # * Correct the 5 errors in the following program. # * In each case add a comment explaining the error and the fix. # * To verify your answer remember that $sin ^2( x ) + cos ^2( x ) = 1$. # In[ ]: from math import sin, cos x = pi /4 1_val = sin^2( x ) + cos^2( x ) print 1_VAL # **Question 2** # # * Create a python list containing 101 evenly spaced values between -1.0 and 1.0. # * Show how this could be more efficently done using NumPy. # * Print out the values using a formatted print statement. # In[ ]: # **Question 3** # # * a. Write a python function that evaluates the equation below for input variables $x$ and $t$: $$f(x, t) = e^{-(x-3t)^2} \sin \left( 3\pi \left( x-t \right) \right)$$ # * b. Generate an array of $x$ values in the range $[-3.0, 10]$. # * c. Using this array of x values, plot the function for $t=0.0$, $t=1.0$ and $t=2.0$. # In[ ]: # **Question 4** # # * Write a program to read the data file [seismic.dat](https://raw.githubusercontent.com/ggorman/Introduction-to-programming-for-geoscientists/master/notebook/data/seismic.dat) and store the magnitude of the earthquakes (*i.e.* the last column) in an numpy array. # * How many earthquakes are there above magnitude 6.0? # In[ ]: # **Question 5** The file [human_evolution.txt](https://raw.githubusercontent.com/ggorman/Introduction-to-programming-for-geoscientists/master/notebook/data/human_evolution.txt) holds information about various human species and their hight, weight, and brain volume. # # * Write a program that reads this file and stores the data in a python dictionary humans. # * The dictionary key should correspond to the specie name (e.g., “homo erectus”), and the value should be a list containing the corresponding data. For example, humans["H. habilis"] should give ["2.2 - 1.6", "1.0 – 1.5", "33 – 55", "660"]. # * Write out the humans dictionary in a formatted tabular form. # In[ ]: