% this m file used to create parameter vs. date (at 900m) through time and parameter vs. date
    % by distinct depths (the "vertical plots".
% file loads float0005, which contains profile, date, lat, long, P, theta,
  % S, sigma, B_LSS_b_corr, Chl_b_corr, O2sat, O2
% file also loads MLD.txt for adding the mixed layer depth line.
% no mat files are saved.
    
% written by E. Boss
% UMaine
% 10/19/06    

clear all
close('all')

% get profile data
load float0005 % float0005 contains:
profileNum=float0005(:,1); % profile number
date=float0005(:,2); % date
lat=float0005(:,3); % latitue (degrees)
lon=float0005(:,4); % longitude (degrees)
P=float0005(:,5); % depth (dB)
theta=float0005(:,6); % potential temperature (degrees C)
S=float0005(:,7); % salinity (psu)
sigma=float0005(:,8); % sigma
B_LSS_b_corr=float0005(:,9); % bb (calibrated)
Chl_b_corr=float0005(:,10); % chlorophyll (calibrated) 
O2sat=float0005(:,11); % oxygen (% saturation) 
O2=float0005(:,12); % oxygen (um/kg) 
POC_b=float0005(:,13);
C_phy=float0005(:,14);
profile=float0005(:,15); % (in days from start)

% get surface data
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\ARGOS\float0005Surface'
% float0005Surface contains:
  % float0005SurfaceChl: [date, Chl_b_corr]

% get first reading of the day
I=find(diff(float0005SurfaceChl(:,1))>4);
time_surf=[float0005SurfaceChl(1,1);float0005SurfaceChl(I+1,1)];
chl_surf=[float0005SurfaceChl(1,2);float0005SurfaceChl(I+1,2)];

% get modis data
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\MODIS\modis_chl'
% modis_chl contains:
  % modis_chl_ts (2116x2601) - chl data only from 06/13/04 - 01/01/07 THIS IS A 51X51 GRID
  % modisChl [date, chl] (2116x2602)
  % distgrid (2116x2601) grid of distance around float
modis_date=modisChl(:,1);

%%% get mean and median modis values within specified distances of float by way of Emmanuel's method
[uu vv]=size(modis_chl_ts); % 2116x2601
% 
% for j=1:25
%     j
%     dd=0.5+(j-1);
%     I=find(distgrid(1,:)<dd);
%     for i=1:uu
%         dist_A(i,j)=nanmean(modis_chl_ts(i,I),2);
%         dist_B(i,j)=nanmedian(modis_chl_ts(i,I),2);
%     end
% end

%%%7.5km of float
I7km_modis=find(distgrid(1,:)<7.5); 
for i=1:uu
   dist_7km_A_modis(i)=nanmean(modis_chl_ts(i,I7km_modis),2);
   dist_7km_B_modis(i)=nanmedian(modis_chl_ts(i,I7km_modis),2);
end

dist_7km_mean_modis=dist_7km_A_modis';
dist_7km_median_modis=dist_7km_B_modis';

% remove zero's from plots
I7k_modis=find(dist_7km_mean_modis>0); % size:
I7kk_modis=find(dist_7km_median_modis>0); % size:

%%%%%%%%%%%%%%%%% correlation
float_sat_median_chl=interp1(modis_date(I7kk_modis), dist_7km_median_modis(I7kk_modis), time_surf, 'linear');
I=finite(float_sat_median_chl);
A1=corrcoef(float_sat_median_chl(I), chl_surf(I)); % .8531
size(float_sat_median_chl(I)); % 208

% get bb data to put on same figure w/chl
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\0005\APEX\apexModis_bb.mat'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% plot
h=figure('Units','inches','Resize','off','position',[0 0 7.25 6],'paperposition',[0.5 3 7.25 6]);
figure(h); % upper left chlorophyll
subplot('Position',[0.11 0.72 0.31 0.15]);
plot(time_surf, chl_surf, 'bo', 'MarkerSize',3);
hold on;
plot(modis_date(I7kk_modis),dist_7km_median_modis(I7kk_modis),'k*');
% plot([732910 732910],[-0.002 4],'k','LineWidth',2)
% plot([732985 732985],[-0.002 4],'k','LineWidth',2)
hold off;
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',8);
set(gca,'LineWidth',0.75);
% set(gca,'XTick',[732115 732480 732850 733215]);
% datetick(gca,'x','mmmyy','keepticks');
set(gca,'XTick',[]);
axis([min(time_surf-1) max(time_surf) 0 3.5]);
ylabel('Chl [m^{-3}]');
% title({'Median MODIS and Float Surface Chlorophyll','Satellite Data Within 7.5km of Float'});

figure(h); % upper right bb
subplot('Position',[0.55 0.72 0.305 0.152]);
plot(floatDate, floatBb, 'bo', 'MarkerSize',3);
hold on;
plot(time_MODIS,b_bp,'k*');
% plot([732910 732910],[-0.002 0.015],'k','LineWidth',2)
% plot([732985 732985],[-0.002 0.015],'k','LineWidth',2)
hold off
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',8);
set(gca,'LineWidth',0.75);
% set(gca,'XTick',[732115 732480 732850 733215]);
% datetick(gca,'x','mmmyy','keepticks');
set(gca,'XTick',[]);
axis([min(floatDate-1) max(floatDate) -0.001 0.015]);
ylabel('b_{bp} [m^{-3}]');
legend('Float 0005','MODIS');
legend('boxoff');
% title({'Median MODIS and Float Bb','Satellite Data Within 7.5km of Float'});

MLD=load('MLD.txt');
I=find(P>2);

% figure(h); % sigma
% % subplot('Position',[0.1 0.35 0.40 0.35]);
% scatter(tt(I),-P(I),5,sigma(I),'filled');
% caxis([27 28]);
% % scatter(tt,-P,5,Chl_b_corr,'filled');
% hold on 
% plot(unique(tt),-MLD,'k','LineWidth',1)
% hold off
% colorbar
% xlim([min(tt),max(tt)]);
% ylim([-400,max(-P)]);
% set(gca,'FontName','Times New Roman');
% set(gca,'FontSize',10);
% set(gca,'LineWidth',0.75);
% set(gca,'XTick',[732115 732480 732850 733215]);
% % datetick(gca,'x','mmmyy','keepticks');
% set(gca,'XTick',[]);
% title('Log(chlorophyll) [mg/m^3]');
% ylabel('depth [db]');

% figure(h); % theta
% % subplot('Position',[0.1 0.35 0.40 0.35]);
% scatter(tt(I),-P(I),5,theta(I),'filled');
% % caxis([27 28]);
% % scatter(tt,-P,5,Chl_b_corr,'filled');
% hold on 
% plot(unique(tt),-MLD,'k','LineWidth',1)
% hold off
% colorbar
% xlim([min(tt),max(tt)]);
% ylim([-400,max(-P)]);
% set(gca,'FontName','Times New Roman');
% set(gca,'FontSize',10);
% set(gca,'LineWidth',0.75);
% set(gca,'XTick',[732115 732480 732850 733215]);
% % datetick(gca,'x','mmmyy','keepticks');
% set(gca,'XTick',[]);
% title('Log(chlorophyll) [mg/m^3]');
% ylabel('depth [db]');

figure(h); % middle left chlorophyll
subplot('Position',[0.1 0.33 0.377 0.35]);
scatter(date(I),-P(I),5,log10(Chl_b_corr(I)),'filled');
% scatter(tt,-P,5,Chl_b_corr,'filled');
hold on 
plot(unique(tt),-MLD,'k','LineWidth',1)
hold off
colorbar
xlim([min(tt),max(tt)]);
ylim([-400,max(-P)]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',8);
set(gca,'LineWidth',0.75);
set(gca,'XTick',[732115 732480 732850 733215]);
% datetick(gca,'x','mmmyy','keepticks');
set(gca,'XTick',[]);
title('Log_{10}\{Chl\}');
ylabel('Depth [db]');

figure(h); % middle right bb
subplot('Position',[0.55 0.33 0.362 0.36]);
% scatter(tt,-P,5,B_LSS_b_corr,'filled');
scatter(tt(I),-P(I),5,log10(B_LSS_b_corr(I)),'filled');
hold on 
plot(unique(tt),-MLD,'k','LineWidth',1)
hold off
colorbar
% caxis([-4 -1.5]);
xlim([min(tt),max(tt)]);
ylim([-400,max(-P)]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',8);
set(gca,'LineWidth',0.75);
set(gca,'XTick',[732115 732480 732850 733215]);
% datetick(gca,'x','mmmyy','keepticks');
set(gca,'XTick',[]);
set(gca,'YTick',[]);
title('Log_{10}\{b_{bp}\}');
% ylabel('depth [db]');

I6=find(P>899 & P<1050);

% % get average chl concentration per month
% [yy,month,dd] = datevec(date(I6)); % converts date string to date vector
% mmyy = [yy month Chl_b_corr(I6)]; % finds each year (once) in dataset and stores in array uyear
% uyear = unique(yy); % finds each year (once) in dataset and stores in array uyear
% CHL_APEX = []; % initializes array CHL_APEX
% for ky = 1:length(uyear) % for each year in dataset
%     umonth = unique(month(yy==uyear(ky))); % finds each month (once) in each year
%     for km = 1:length(umonth) % for each month in each year in each dataset
%         midx = yy==uyear(ky) & month==umonth(km); % where year AND month exist (create logical array)
%         Chl_b_midx = Chl_b_corr(midx); % create array chl_b_midx of chl data at midx = true
%         N=length(find(midx==1)); % get number of float points in the month
%         thisrow = [uyear(ky) umonth(km) nan(1,2)]; % make array thisrow of yr, mo, NaNs for each chl
%         thisrow(3) = nanmedian(Chl_b_midx); % add median chl concentration for month to thisrow column 3
%         thisrow(4) = nanmean(Chl_b_midx);  % add mean chl concentration for month
%         thisrow(5) = N; % add number of points to column 5
%         thisrow (6) = std(Chl_b_midx);
%         CHL_APEX = [CHL_APEX; thisrow]; % append new row to big array CHL_APEX
%     end
% end
% 
% A=datenum(CHL_APEX(:,1),CHL_APEX(:,2),15);
% 
% % get average bb concentration per month
% [yy,month,dd] = datevec(date(I6)); % converts date string to date vector
% mmyy = [yy month B_LSS_b_corr(I6)]; % finds each year (once) in dataset and stores in array uyear
% uyear = unique(yy); % finds each year (once) in dataset and stores in array uyear
% BB_APEX = []; % initializes array BB_APEX
% for ky = 1:length(uyear) % for each year in dataset
%     umonth = unique(month(yy==uyear(ky))); % finds each month (once) in each year
%     for km = 1:length(umonth) % for each month in each year in each dataset
%         midx = yy==uyear(ky) & month==umonth(km); % where year AND month exist (create logical array)
%         bb_midx = B_LSS_b_corr(midx); % create array chl_b_midx of chl data at midx = true
%         N=length(find(midx==1)); % get number of float points in the month
%         thisrow = [uyear(ky) umonth(km) nan(1,2)]; % make array thisrow of yr, mo, NaNs for each bb
%         thisrow(3) = nanmedian(bb_midx); % add median bb concentration for month to thisrow column 3
%         thisrow(4) = nanmean(bb_midx);  % add mean bb concentration for month
%         thisrow(5) = N; % add number of points to column 5
%         thisrow (6) = std(bb_midx);
%         BB_APEX = [BB_APEX; thisrow]; % append new row to big array BB_APEX
%     end
% end
% 
% B=datenum(BB_APEX(:,1),BB_APEX(:,2),15);

figure(h); % bottom left chlorophyll
subplot('Position',[0.115 0.14 0.305 0.15]);
plot(tt(I6),Chl_b_corr(I6),'k.');
axis([min(tt),max(tt),min(Chl_b_corr),0.05]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',8);
set(gca,'LineWidth',0.75);
set(gca,'XTick',[732115 732480 732850 733215]);
datetick(gca,'x','mmmyy','keepticks');
ylabel('Chl [m^{-3}]');
% title('Chlorophyll');

figure(h); % bottom right bb
subplot('Position',[0.55 0.14 0.3 0.152]);
semilogy(tt(I6),B_LSS_b_corr(I6),'k.');
axis([min(tt),max(tt),0.0001,0.01]);
set(gca,'FontName','Times New Roman');
set(gca,'FontSize',8);
set(gca,'LineWidth',0.75);
set(gca,'XTick',[732115 732480 732850 733215]);
set(gca,'YTick',[0.0001 0.001 0.01]);
datetick(gca,'x','mmmyy','keepticks');
ylabel('Log_{10}\{b_{bp}\}');
% title('Side Scatter');
% legend('900-1050');

