GVN330 Climate Data Analysis Assignment 1: Handling

advertisement
GVN330 Climate Data Analysis Assignment 1: Handling
Climate data in Matlab
By completing the tasks in Assigment1, you will learn how to work with climate
data in Matlab. These tasks are typical of those required to prepare climate data
for analysis. The tasks do not require any statistical theory.
File Archive Contents
This file archive has folders:
Reading – contains background documents.
DataFiles – climate data files for the Assignment.
The files you create should go in the directory that contains DataFiles, not be
mixed with the DataFiles themselves.
Background Reading
Begin by reading the Tutorials at Mathworks Getting Started with Matlab:
http://www.mathworks.se/help/matlab/getting-started-withmatlab.html:
Experiment with typing commands into Matlab if you do not understand!
In the Assignment notes, I use footnotes to indicate pages from the Matlab help
that might be useful to you. Of course, read as much of the help as you want!
Matlab functions are indicated by words in italics.
For Assignment 1.5/1.6/1.7, read:



Reading/Matlab_Style_Guide.pdf.
http://www.cs.utah.edu/~germain/PPS/Topics/commenting.html - this
has good advice on commenting. See especially the examples in the
section Self Documenting Code.
Optional:
http://www.datatool.com/downloads/matlab_style_guidelines.pdf
Contains detailed style recommendations.
Assignments:
Load and plotting climate data
Assignment 1.1: You have 12 files with daily rainfall data for Gothenburg (SMHI
climate station 7142). Load the data into Matlab and make a time-series plot.
In the folder DataFiles/Gothenburg_prcp_2011/ there are 12 files, one file for
each month. Each file has the format:
year, month, day, precipitation.
Note the data convention: values of -1 imply rainfall of 0, values of 0 mean less
than 0.1mm (I am not doing this just to be difficult! That is actually how the data
are supplied by SMHI!!)
Discussion points: (remember to take notes, to justify your decisions in your
report!)


will you load all the data before you make the plot, or will you plot each
month as you load it?
how will you store the data in Matlab? Why?
Checkpoint - compare with alternative suggestions from other groups.
Discussion points: How will you load the data? Will you use a script1 which calls
a matlab function2 (eg importdata) to load the data? or will you use the Import
wizard (File -> Import Data)? Why? What will you do with the strange
convention?
Visual analysis – make a simple plot using the plot function (you can find the help
for this yourself!) with one input variable (ie plot(y) plots the values on the yaxis). Is this what you expect?
Discussion point: How will you deal with this issue? Will you remove the dates
with the problem values, or make use of NaNs3? Why? You are remembering to
make notes to justify what you are doing?
In the simple plot, we get an x-axis with labels something like 0, 50, 100 … We
want to make our plot have sensible labels, like Jan, Feb, Mar… Do this using the
matlab function datenum, the two-argument form of plot(x,y), and the function
datetick.
Assignment 1.2: Exporting graphs from Matlab Figures.
By now you should have a basic time-series plot. Now you need to decide how to
format it for your report.
Discussion points: What line/symbol style will you use (see matlab help on plot
and LineSpec). Will you use color? Will you change any of the other properties,
such as LineWidth? What aspect ratio will you use (will the plot are be square or
rectangular?). What will you use for the x and y axis labels (see xlabel and ylabel
functions)? Will you have a title or a legend?
Checkpoint: When you have created your figure, compare with other groups.
what is a script?
http://www.mathworks.se/help/matlab/learn_matlab/scripts.html
2 Calling functions http://www.mathworks.se/help/matlab/matlab_prog/call-afunction.html
3 What is a NaN? http://www.mathworks.se/help/matlab/matlab_prog/infinityand-nan.html
1
But how would you get a figure from a Matlab Figure Window into a document
(presumably a Word or OpenOffice document)? The importance of this step is
usually underestimated, even by professional researchers!!
Compare the results from two methods:
o Figure->Edit->Copy Figure and then Paste into a blank Word
document.a
o Figure->SaveAs->eps and then import into a blank Word
document.
Discussion point: How do the results compare when viewed in word (note,
results here depend on the version of Word/OpenOffice you use!)?
Then save the two word documents as pdf and then open the pdf files.
Discussion point: How do the results compare viewed in acroread/mac
preview/whatever pdf viewer you use? Zoom right in…
As an alternative to using the Figure Menu, you can also use the print function
(see doc print) to export a Figure to a graphics file.
Discussion point and Check point: What are the advantages in using the print
function, compared to creating a plot using menu selections? And vice-versa?
Note: for simplicity and consistency, please make all graphics files for this
course as png files (note on file-size/resolution). You can paste them into
your report, or upload them to GUL as individual flies (and refer to them byname in your report)
Assignment 1.3: Load and plot daily rainfall data for Borås (SMHI station 7245)
Using the data in GUL folder DataFiles/Borås_prcp_2011, create a graph with the
same format that you used in Assignment1.1.
Discussion: Review how much work was involved in performing the same
analysis for a second station? Did you find yourself clicking through menu items,
dragging-and-dropping, and typing commands again (perhaps using up-arrow to
find them), or had you kept records of the commands you had type, and were
able to re-run them by pasting them from your editor into the command
window?
Check-point: Discuss with other groups, how quickly could they perform the
analysis? What methods did they use to achieve best results? Don’t forget, it is ok
to revise your work to incorporate the best ideas you see, as long as you give
credit!
Writing efficient matlab code: functions
Assignment 1.3 Create a matlab function called plot_rain_graph, which you can
use to create graphs as .png files for both stations.
You create the function in it’s own m-file (here called plot_rain_graph.m).
Remember to include comments so that if you type
>> help plot_rain_graph
then you get instructions about how to use the function! See 4
Discussion: What will be the inputs to the function? Will there be any outputs?
Now write a matlab script called plot_both_stations.m that plots graphs for both
Gothenburg and Borås, by calling the function plot_rain_graph that you just
wrote twice. The script should be non-interactive!! Your script might perform
the entire processing chain (from loading the data files through to writing the
graph to an output file) or only part of the chain (it may require that the data are
already loaded into particular variables, or require that the user creates graphics
files from the figures). If the script does not perform the entire processing chain,
the tasks the user should do should be documented as comments in the script.
Check point! Compare your plot_rain_graph and plot_both_stations.m with other
groups. Do you find better ways to write the code?
Aggregating climate data
Assignment 1.4: Create graphs of average monthly maximum temperature,
minimum temperature and rainfall for the 1961-1990.
The average monthly climate for a location is often shown graphically using
vertical bars for precipitation, and lines for maximum and minimum
temperature5. 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 timeseries 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-to0.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-endings6, they may look strange in Notepad!)
Create a new script called exercise1_4.m. All the code to create the graph should
be in this script – that is, the script should be non-interactive, and running the
script should create the graph 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:
http://www.mathworks.se/help/matlab/matlab_prog/add-help-for-yourprogram.html
5 eg. http://www.weather-and-climate.com/average-monthly-RainfallTemperature-Sunshine,Goteborg,Sweden
6 for a quick explanation of this historical quirk, see
http://www.leepoint.net/notes-java/io/10file/sys-indep-newline.html
4
S = importdata(‘Vinga_daily.csv’)
How do you use this?! Hint: this is a structure7.
Now you need to create averages (for max and min temperature) and averagemonthly-totals (for rainfall) for each month.
Discussion: How will you create averages for each month? Will you use a “for
loop”8?
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. Will you make separate graphs
for precipitation and temperature, or combine them onto a common x-axis (not
easy in Matlab!)
Check point: compare your graphs with those of other groups. What are the
properties of the “best” graphs?
Assignment 1.5: Create and plot monthly rainfall and temperature time-series.
In assignment 1.4 you calculated averages for each month ie. one average value
for January using data from the entire dataset. In this exercise you will create a
monthly time-series: that is, the monthly-average for Jan 1961, Feb 1961, Mar
1961…
Work with the same daily data file that you used for Assignment 1.4. Again, you
should write a script that loads the data, creates the averages, and then make the
figures, and the script should run without any further inputs from the user.
Begin by creating graphs of monthly-average max and min temperature
timeseries, and a graph of monthly-sum precipitation time-series
Discussion: can you load the data using the same code as you used for 1.4? How
will you create averages for each month?
Discussion: How will your graphs look? Will you use the same line/symbols and
bars from Ass1.4, or adopt a new style? Why? How will you format the x-axis
ticks and tick-labels?
Assignment 1.6: Create and plot seasonal rainfall and temperature time-series.
Next task is to create seasonal-average time-series graphs. That is, a time-series
of Winter 1960-61, Spring 1961, Summer 1961, Fall 1961, Winter 1961-62…
Discussion: Will you base your calculations on daily data, or will you use the
monthly values you calculated in Ass1.5? What are the advantages and
disadvantages of these two approaches?
http://www.mathworks.se/help/matlab/structures.html
what is a “for loop”?
http://www.mathworks.se/help/matlab/learn_matlab/scripts.html#btcd6o0-1
7
8
The values for winter will include data from two years (ie Dec from one year, and
Jan and Feb from the following year.) This may require more complex code than
you used in Ass1.4. Describe your method.
Discussion What will you use for the first value (Winter 1960-61) – you do not
have data for December 1960, only for Jan and Feb 1961? Is there a similar issue
for the last value?
Check point: Compare your code with that of another group. Can you learn
anything from the others?
Commenting, programming-style and code-refactoring.
We will being Assignment 1.7 and 1.8 together in class, on an agreed date.
Download