Course:GEOB503/Assign1

From UBC Wiki

Assignment 1: Analyzing data from the Water Survey of Canada, Part 1

This assignment is intended to get you up to speed by starting with some of the more advanced commands for loading data, and a couple of small programming challenges. If you have any questions about the assignment, or any insights that you would like to share, please add them in the relevant sections, below. If you have a question, please begin your paragraph with QUESTION:...any answers should be placed immediately below following ANSWER:


This is the assignment as is stands on 8 September 2010 (abbreviated from the assignment m file):

Loading data

  • load the data in the csv format typically provided by WSC
  • define a new set of variables with meaningful names for the numeric data in the data file. You should also save the character string identifying the station.
  • construct a single matrix with just the Day, Year and Flow parameters and save this as a txt file called 'My_Flow_Data.csv'. Now ERASE ALL YOU DATA using the 'clear all' command and try opening your file using the 'open' and 'load' commands.

Here is some sample code to load data, reassign variables, etc. It can be copied to an .m file and run line-by-line to see what happens

clear all;  %removes all data from the workspace
fid = fopen('Fishtrap-daily-flows.csv'); 
%opens the file so that it may be scanned
C = textscan(fid, '%s %f %f %f %f %s', 'delimiter', ',', 'HeaderLines', 3, 'EmptyValue', NaN);
%this reads the file with the associated File Identification number (fid)
%and formats the information that is given...%s = string, %f = floating
%point number, %u = integer...many other formats are possible too
fclose(fid);
%this closes the file we had opened and scanned
Q = C{5};    %assign the discharge values to the variable Flow
jday = C{4};     %assign the Julian date to the variable Day
yr = C{3};    %assign the year to variable year
Codes = C{6};   %preserve the data codes associated with the daily data 
StnID = C{1}{1};    %preserve the station ID
clear C;        %discard the rest of the data and the import cell array
Flow_data = horzcat(jday,yr,Q);
csvwrite('FlowData.csv', Flow_data);

Basic plotting

  • plot all of the flow data in one long sequence
  • plot the flow data against day of the year to look at the annual distribution

Here is some sample code for plotting things in Matlab

%plot all flow data
figure(1)
plot(Q);
figure(2)
plot(jday,Q);
%plot the first 365 datapoints
figure(3)
plot(jday(1:365),Q(1:365));

FOR loop, IF statement and BAR plots

  • plot the flow data for the third year with a complete data record. To do this we will have to (a) identify all the years for which we have data and (b) calculate how many daily discharge values we have for each year (it is also useful to plot this data in a bar chart so that we get a visual impression of the data distribution...use help to find the commands that produce bar graphs, and save the code for generating the statistics on data and the bar graph to this m-file... USEFUL COMMANDS: FOR LOOP, IF LOOP, BAR, SUM

(comments/questions go here)

COMMENT: don't for get about leap years! Also, some of you might not know that it is fair game in MATLAB to reference the same variable twice in a single equation (e.g. j = j +1, which would incrementally increase the value of j)


More plotting

  • plot the flow data for 1915 using a red line and for 1985 using a dashed blue line
  • write a bit of code that prompts you to tell the program what year to plot, so that you can look at any year of data. Even better, try to create a loop that allows you to select whether you want to continue looking at data or not. You should incorporate this into your m file...USEFUL COMMANDS: MENU, MSGBOX

More sample code

%determine how many years of data there are
datayears(1) = yr(1);
num_years = 1;
for i = 2: numel(yr)
   if yr(i) > yr(i-1)
     num_years = num_years + 1;  %increase the total number of data years
       datayears(num_years) = yr(i);
   end
end
%determine how many data points exist for each year
for i=1:numel(datayears)
  N(i) = sum(yr == datayears(i));
end
%display this data graphically using a bar chart 
figure(4)
bar(datayears, N)
%plot the data for 1915 and 1972 using red dashed and blue solid lines 
figure(5)
plot(jday(yr==1915), Q(yr==1915), 'r--')
hold
plot(jday(yr==1975), Q(yr==1975), 'b-')
%plot the data for a user-selected year
figure(6)
flag = 1
while flag == 1
   clf;
   %entry = inputdlg('enter the year for which you wish to view the annual hydrograph');
   %target = str2num(entry{1});
   ind = menu('select year for which you wish to view data', num2cell(datayears));
   target = datayears(ind);
       if sum(datayears == target)==1
           plot(jday(yr==target), Q(yr==target), 'r.');
           axis([1 365 0 ceil(max(Q))]);  %set the axis to 1 year, max Q
           figure(6)
       else
           h = msgbox('No data for that year')
       end
   flag = menu('Do you want to view data from another year?', 'yes', 'no')
end