Assignment1.5

advertisement

GVN330 Climate Data Analysis

Assignment 1: Handling Climate data in Matlab

Calculating average monthly climate statistics.

The “climate” of a region or city is usually expressed using average monthly values. You find tables or graphs of monthly rainfall, minimum and maximum temperature, for example, in tourist information guides. More importantly, such information is used in urban and agricultural planning. Monthly averages are also the basis for saying if a recent month was warmer/colder/wetter/drier than

“average”.

The “standard climate” is usually defined to be 1961-1990. Climate change is

Assignment 1.5: Create graphs of

 average monthly maximum temperature,

 average minimum temperature, and

 average cumulative monthly rainfall over 1961-1990 using the data from Vinga_daily.csv or Saeve_daily.csv. Note that these files have SIX columns: year, month, day, precipitation, maximum temp, minimum temp

The average monthly climate for a location is often shown graphically using vertical bars for precipitation, and lines for maximum and minimum temperature 1 . Just to emphasize, a plot of average monthly climate has 12 values on the x-axis, one for each month, which is different to monthly average time-

series plot, which you will create in assignment 1.6) In this assignment you will write a script to create such a graph.

Daily weather data from two stations near Gothenburg (Vinga and Säve) are in the folder DataFiles as files Vinga_daily.csv and Saeve_daily.csv. Choose one

station to start with. Note: these files have 0 for no rainfall, 0.1 for zero-to-

0.1mm, etc.

Look at the files you are working with open a file using Excel or WordPad (note that these files using unix-style line-endings 2 , they may look strange in Notepad!)

Create a new script called exercise1_5.m. All the code to create the graph/s should be in this script – that is, the script should be non-interactive, and

1 eg. http://www.weather-and-climate.com/average-monthly-Rainfall-

Temperature-Sunshine,Goteborg,Sweden

2 for a quick explanation of this historical quirk, see http://www.leepoint.net/notes-java/io/10file/sys-indep-newline.html

running the script should create the graph/s in a matlab figure! (it does not have to export it to a graphics file).

Discussion – how will you load the data, and how will you store it in the matlab workspace? Eg. try the command:

S = importdata('Vinga_daily.csv')

How do you use this?! This is a structure 3 . You can access elements of a structure using the . ("dot") notation. Try for example:

>> S.data

>> S.textdata

>> S.colheaders

 From S, create a matrix Data containing the data from the file, with 16071 rows and six columns.

Discussion:

Now you need to create averages (for max and min temperature) and averagemonthly-totals (for rainfall) for each month.

Begin with creating the average minimum temperature for January.

 How do you extract the column of minimum temperatures from the matrix Data? Call this column-vector V

 How do you extract the column with the months from the matrix Data?

Call this column-vector M

 Create a logical matrix that has 1’s (true) where rows of M are in January, and 0’s (false) otherwise). Call this matrix M_log

 Using V and M_log, create a new matrix V_vals which contains the values of V (the data) for which M_log is true (rows are in January).

 Calculate the average (“mean”) of all the values in M_vals

Discussion: What will you do with the average you have calculated? You could store it in a variable called January_min_temp_avg. And the next in

February_min_temp_avg. But there is a better way to store them!

Now, you need to calculate the average values for the other months. Yes, you could copy-and-paste the code 12 times (and another 12 for maximum temperature), but you have learned how to use functions. Might your code look neater if you developed a function to do some of the work?

Up to now we have discussed temperature. Precipitation is more difficult, because "average January precipitation of 30mm" mean that the average of the cumulative precipitation for January is 30mm, not that 30mm falls each day!

Discussion: How might you deal with the precipitation data? You do not need to write a loop to calculate the sums of each month! You do need to know how

3 http://www.mathworks.se/help/matlab/structures.html

many days in each month of the year. Work out the solution for January first, then discuss with others what you might do about February!

Check point: When you have written the script to calculate the values, compare with other groups. Check if the values you have calculated for each month are the same (or at least check a few example values)!

Now write the code to create the graph or graphs. You may choose to make separate graphs for each variable (using the function figure), or combine them onto a common x-axis using hold on, and maybe plotyy. You can also make bar graphs (commonly used for precipitation) using the bar function if you want.

Check point: compare your graphs with those of other groups. What are the properties of what you think are the “best” graphs?

Download