Course:GEOB503/Week 2

From UBC Wiki

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

This assignment is intended to introduce flow control (for, while, if) and the use of function m files in MATLAB. 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 14 September 2010 (abbreviated from the assignment m file):

Program control

Controlling the program: there are various structures that controll the flow of a program. You should know what the following commands do, and be able to identify some examples of what you might use them for

FOR loop

WHILE loop

IF statement


comments/questions go here


Functions

Functions: it is often useful to turn a script (such as the commands for importing data using TEXTSCAN) into a function that can be called by another script. Turn your data import script for the WSC data into a function, then use it in a script to import data from Fraser River (data is on the website). Call the function WSC_daily, and have it export Flow, Day, and Year once you tell it the WSC data file to use.


Here is a working function for importing data from WSC

function [Flow, Day, Year] = WSC_daily(datafile)
    %This function imports data from standard Julian Date - Data export files
    %for daily data from the WSC, in csv format
    %   Flow is an output variable containing the daily flows
    %   Day is an output variable containing the Julian date for each flow
    %   Year is an output variable containing the year for each flow 
    
    fid = fopen(datafile);
        C = textscan(fid, '%s %f %f %f %f %s', 'delimiter', ',', 'HeaderLines', 3, 'EmptyValue', NaN);
    fclose(fid);
    Param = C{2};   %pull out the parameter variable
    Var1 = C{5};    %assign the discharge values to the variable Flow
    Flow = Var1(Param==1);  %use only the flow data (Param =1), not the level data (Param = 2)
    
    Var2 = C{4};     %assign the Julian date to the variable Day
    Day = Var2(Param==1);
    
    Var3 = C{3};    %assign the year to variable year
    Year = Var3(Param==1);
end

PART 1

Using only the data from complete years of record, construct a cumulative departure plot using the annual flow time series and the mean annual flow for the period of record (in m3, not m3/s)


The first step is to generate a list of unique years. The following code does this

%check complete years of record
unqYear=unique(year);
for i=unqYear(1):max(unqYear)
   totDays(i)=length(year(year==i));
end

There is a problem with the loop, though, because totDays ends up being 2008 elements long, with no data before 1912. Better to start the loop off with min(unqYear), and offset the index for totDays to 1+i-min(unqYear)

A nice bit of code to clean up a workspace

clearvars -except Flow         % so that flow can be used in part 4

PART 2

Using only data from complete years, construct a cumulative departure period for 5-day periods (i.e., change the temporal resolution form 365 days to 5 days), and compare these results to the annual sequence


comments/questions



A tool identifying leap years may be useful:

http://www.advancedmcode.org/isleap.html


-1913 starts at row 307 day_vol -I am attempting to bin my daily volumes into 5-day sums so that way i can work it into departures

j=1;
for i = 307:5:(length(day_vol)-1)    %length minus 1 because the last cell is NaN
    
    pentaday_vol(j,1) = sum(day_vol(i):day_vol(i+4)); %#ok<SAGROW>     %THIS IS CAUSING A REALLY INTENSE MEMORY CRASH!
    
    j=j+1;
end


Any suggestions on how to make this more efficient so that the whole loop will run would be great!



A potential alternative to using a 'for' loop to calculate 5-day averages is to use the reshape() function. Reshape your day_vol vector into a 5 by length(day_vol)/5 matrix. Run mean() on the newly created matrix and you should be able to create a vector of 5-day averages.


PART 3

See if you can figure out a way of visualizating/detecting within-year patterns that are superimposed on the decadal scale change


comments/questions