{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2.18 Programming for Geoscientists class test - 14:00-16:00 12th December 2013" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test instructions\n", "\n", "* This test contains **5** questions each of which should be answered.\n", "* Write your program in a Python cell just under each question.\n", "* You can write an explanation of your solution as comments in your code.\n", "* 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.\n", "* Save your work regularly.\n", "* 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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**1.** Correct the 5 errors in the following program." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Part a\n", "x=1\n", "print \"sin(%g)=%g\" % (x, sin(x))\n", "\n", "# Part b\n", "F = 114\n", "print 5/9*(F-32)\n", "\n", "# Part c\n", "interest = \"0.1%\"\n", "print \"Growth = %g\" % 42.0*float(interest)\n", "\n", "# Part d\n", "# Table header:\n", "# Survey Name USGS ID Contributor Year Number of Tracklines KMS Shot\n", "line = \"Beaufort Sea W-62-77-AR WesternGeco 1977 69 4,987\"\n", "print \"The year the data was collected was %d\", line[4]\n", "\n", "# Part e\n", "heights = (1.56, 1.67, 1.97, 1.43)\n", "heights.append(1.81)\n", "print heights" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2.** The equation $s=ut-\\frac{1}{2}gt^2$ can be used to describe the height of a ball thrown vertically, where $s$ is the height, $u$ is the initial velocity, $t$ is the time and $g$ is acceleration (assumed here to be $9.81ms^{-2}$). \n", "\n", "* Assuming that the initial velocity is $2ms^{-1}$, plot a graph showing how the height varies from $t=0$ to the time it hits the ground, $t=\\frac{2u}{g}$.\n", "* Use 100 data points to generate your plot.\n", "* Label both the $x$ and $y$ axis appropriately." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3.** The Gaussian function can be defined as:\n", "$$g(x) = ae^{\\frac{(x-b)^2}{2c^2}}-d.$$\n", "\n", "* Implement a Python function that evaluates this expression for a given input for x.\n", "* The parameters $a$, $b$, $c$ and $d$ should be optional arguments to the function with default values 1.0, 0, 1.0 and 0.0 respectively.\n", "* Use NumPy to create an array of 101 $x$ values uniformly spaced in the range [−1, 1].\n", "* Use the function you created in the first part to create an array of Gaussian values.\n", "* Plot the result." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4.** The file [hpip\\_yakutsk\\_borehole\\_data.txt](https://raw.githubusercontent.com/ggorman/Introduction-to-programming-for-geoscientists/master/notebook/data/hpip_yakutsk_borehole_data.txt) (provided with this IPython notebook file) contains permafrost borehole temperature data (in degrees Celsius) for January between the years 1915 and 2000. By looking at the header of the text file you can see that the 2$^{nd}$ column contains the temperatures at a depth of 0.80m, the 3$^{rd}$ column contains the temperatures at a depth of 3.20m and the 4$^{th}$ column gives the surface temperatures taken from the *World Monthly Surface Climatology dataset (ds570.0)*. A temperature value of \"-999\" indicates that the data value is missing.\n", "\n", "* Write a program that reads in this data and plots each of the temperature profiles against the year.\n", "* Any year that contains missing temperature data (*i.e.* has a value of -999) should be skipped.\n", "* Label the *x* and *y* axis clearly.\n", "* Provide a legend and locate it in the best position for the plot." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**5.** Create a Python class for *Igneous Rocks*.\n", "\n", "* The class should have the following attributes: *Rock Type*, *Grain Size*, *Usual Colour*, *Composition*, and *Notes*.\n", "* Add a class method to initialise the class.\n", "* Add a class method to print out a description of an object of this class.\n", "* Demonstrate the functionality of the class using the rock *Obsidian* where the grain size is *fine*, the usual colour is *dark*, the composition is *lava glass* and we note that it has a *glassy appearance*.\n", "* Use the class member function to print out the rock description." ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }