%% 
%
% solutions to problem set 3
% matlab bootcamp
% kyla drushka
% sept 2013
%
%


%%1.	The goal of this question is to look at near-surface temperature.  
% Although Argo floats don't come all the way up to the sea surface, they 
% get within a few meters; here, we will look at the shallowest Argo measurements.

load('argo_profiles.mat')
% a.	Plot the shallowest temperature measurement for each profile as a 
% function of time (hint: the shallowest temperatures are in the first row 
% of the temperature matrix). Use the datetick function to label the x-axis.
figure(1),clf
plot(time,T(1,:))
datetick('x');

% b.	What is the time range of these data? 
timerange=range(time);
disp(['time range is ' num2str(timerange) ' days']);


% What is the average time difference between adjacent profiles 
% (hint, use the command diff).
av_time_diff=mean(diff(time)); % in days
av_time_diff_hours=av_time_diff*24;
disp(['average time difference is ' num2str(av_time_diff_hours) ' hours'])

% c.	What is the range of the shallowest temperatures? 
Trange=range(T(:,1));
disp(['top-depth T range is ' num2str(Trange) ' degC'])

% d.	What is the average depth of the shallowest measurement 
% (i.e. average the first row of z across all profiles). 
zav=nanmean(z(:,1));
disp(['average top depth is ' num2str(zav) ' m'])



%% 2
% a.	Run through each of the 109 profiles using a for loop. 
% b.	For each profile, calculate the maximum salinity using the command max. 
% Specify two outputs for the max command: the first output is the maximum value, 
% and the second output is the index of that maximum (see help max for more 
% information). Use the index of the maximum to also calculate the depth at 
% which salinity is maximum. 

% c.	Store the maximum salinity value for each profile in a new vector called 
% Smax.
% d.	Similarly, store the depth of the salinity maximum for each profile. 
 
for ki=1:length(lon)
    [Smax(ki) maxi]=max(S(:,ki)); % Smax is the max value, maxi is the index
    zSmax(ki)=z(maxi,ki);   % zSmax is the depth for this profile at the index maxi
end

% e.	Plot the maximum salinity () against max-salinity-depth (Smax_depth), as a
% scatterplot (i.e. don't use a line, just use markers).
figure(2),clf
plot(zSmax,Smax,'.')

% f.	Use regress to fit a line to data: Smax_depth should be the independent 
% variable (i.e. "x" in the linear fit), and Smax should be the dependent 
% variable ("y" in the fit). Be sure to include a column of ones in the 
% fitting matrix to account for the fact that the line doesn't go through zero. 


xfit=[zSmax(:) ones(size(zSmax(:)))];
yfit=Smax(:);
[b bint]=regress(yfit,xfit);
zS=min(zSmax):max(zSmax);
Srecon=zS*b(1)+b(2);
Sup=zS*bint(1,1)+bint(2,1);
Sdn=zS*bint(1,2)+bint(2,2);
hold on
plot(zS,Srecon,'ro')
plot(zS,Sup,'r--')
plot(zS,Sdn,'r--')
legend('data','fit','condidence intervals')
disp('Nope, not very linear!!!')

%g.	Calculate the mean, median, maximum, and minimum of the maximum salinity depth.
zSmax_mean=nanmean(zSmax);
zSmax_median=nanmedian(zSmax);
zSmax_max=max(zSmax);
zSmax_min=min(zSmax);

disp(['stats for max salinity depth (all units in meters):'])
disp(['mean: ' num2str(zSmax_mean)])
disp(['median: ' num2str(zSmax_median)])
disp(['max: ' num2str(zSmax_max)])
disp(['min: ' num2str(zSmax_min)])


