
%REMINDER: notice that pause is used on occasion, which means you must
%click in matlab to continue

% section 2.a 

%read arrays of yearly lake level, utah temp, and utah precip
% data has been preprocessed for 1895-2006 period

%lake level data with year,number of values, lake level (ft)
load gsl_yr.txt;
time = gsl_yr(:,1);
%convert to meters
lev = gsl_yr(:,3)*.3048;

% figure 2.1
figure(1);
bar(time,lev,'g');
axis([1895 2010 1277 1284])


% annual mean utah temperature (C)
load  ut_temp_yr.txt;
temp = ut_temp_yr(:,2);

%figure 2.2
figure(2);
bar(time,temp,'c');
axis([1895 2010 7 11])

% annual total utah precip (cm)
load  ut_ppt_yr.txt;
ppt = ut_ppt_yr(:,2);

%figure 2.3
figure(3);
bar(time,ppt,'r');
axis([1895 2010 15 50])

pause

% section 2.b

% data ranges
% with stat tool package
range_lev = range(lev)
range_temp = range(temp)
range_ppt = range(ppt)

% without stat tool package
range_lev = max(lev) - min(lev)
range_temp = max(temp) - min(temp)
range_ppt = max(ppt) - min(ppt)

%figure 2.4
figure(4)
x = 1277.5:1:1283.5;
hist(lev,x);

%figure 2.4
figure(5)
x = 1277.25:.5:1283.75;
hist(lev,x);

pause

%figure 2.5 and 2.6 created using dfittool interactively

% section 2.c

%median values
med_lev = median(lev)
med_temp = median(temp)
med_ppt = median(ppt)

%prctile in stattool package
perc_lev = prctile(lev,[10,25,50,75,90])
perc_temp = prctile(temp,[10,25,50,75,90])
perc_ppt = prctile(ppt,[10,25,50,75,90])

%boxplot in stattool package
%figure 2.7
figure(6)
boxplot(lev,'notch','on')

figure(7)
boxplot(temp,'notch','on')

figure(8)
boxplot(ppt,'notch','on')
pause



%load all variables into an array to streamline processing
array(:,1) = lev;
array(:,2) = temp;
array(:,3) = ppt;

iq_var = iqr(array)

xbar = mean(array)

xbar_trim = trimmean(array,10)

ma = mad(array)

% unbiased estimate of population standard deviation and variance
var0 = var(array,0)
std0 = std(array,0)

% sample standard deviation and variance
var1 = var(array,1)
std1 = std(array,1)


skew = skewness(array)

%section 2d

% compute departures from mean
% need to use some matlab matrix algebra for the array
ny = length(lev);

%sample means
xbar_array = ones(ny,1)*xbar;
%sample standard deviations
sx_array = ones(ny,1)*std(array,1);

array_a = array - xbar_array;

% figure 2.8
% lake level
figure(9);
bar(time,array_a(:,1),'g');


% annual mean utah temperature (C)

%figure 2.9
figure(10);
bar(time,array_a(:,2),'c');


% annual total utah precip (cm)

%figure 2.10
figure(11);
bar(time,array_a(:,3),'r');

pause

% define climate normal
cnorm = mean(array(77:106,:));
cnorm_array = ones(ny,1)*cnorm;

array_cna = array - cnorm_array;

% figure 2.11
% lake level
figure(12);
bar(time,array_cna(:,1),'g');


% annual mean utah temperature (C)

%figure 2.11
figure(13);
bar(time,array_cna(:,2),'c');


% annual total utah precip (cm)

%figure 2.11
figure(14);
bar(time,array_cna(:,3),'r');

pause

% figure 2.17
figure(15)
bar(time,array_cna(:,3),'r');
hold on
% compute median smoothed values with 3 passes of a 3 point smoother
ppt_ms_1 = median_smooth(array_cna(:,3),ny,3);
ppt_ms_2 = median_smooth(ppt_ms_1,ny,3);
ppt_ms = median_smooth(ppt_ms_2,ny,3);
plot(time,ppt_ms,'g');



