% program to plot WOD01 data exported from OSD
% calls function CHL for plotting monthly median chlorophyll by depth
clear all
close('all')
load all-chl-osd %mat file of OSD data (array dat)
[mm nn]=size(dat);
data=[dat,zeros(mm,1)]; %make array data with dat and one extra column of zeros (for as many rows as there are in A) 
data(:,16)=date;  %replaces column 16 zeros with date
I=find(data==-9999); %find where data were not available (imported as -9999)
data(I)=NaN; %set -9999 to NaN
%array data contains WOD01 osd data: 
lon_wod01=data(:,1);          %longitude
lat_wod01=data(:,2);          %latitude
bdep_wod01=data(:,3);         %bottom depth (depth from water surface to sediment)
dep_wod01=data(:,4);          %sample depth
depqf_wod01=data(:,5);        %sample depth qualifer flag
temp_wod01=data(:,6);         %temperature degrees C
tempqf_wod01=data(:,7);       %temperature qualifier flag
sal_wod01=data(:,8);          %salinity psu
salqf_wod01=data(:,9);        %salinity qualifier flag
o2_wod01=data(:,10);          %oxygen ml/l
o2qf_wod01=data(:,11);        %oxygen qualifier flag
chl_wod01=data(:,12);         %chlorophyll ~$m~#g/l
chlqf_wod01=data(:,13);       %chlorophyll qualifier flag
biomass_wod01=data(:,14);     %plankton/biomass
biomassqf_wod01=data(:,15);   %plankton/biomass qualifier flag
datetime_wod01=data(:,16);    %date and time
%
% Wayne's stuff:
bindepths = [0 5 10 20:20:200]; %depths from which to pull chl for calculating monthly medians
CHL_WOD01 = extract_bin(datetime_wod01, dep_wod01, bindepths, chl_wod01); %calls extract_bin for processing chlorophyll
SAL_WOD01 = extract_bin(datetime_wod01, dep_wod01, bindepths, sal_wod01); %calls extract_bin for processing salinity
TEMP_WOD01 = extract_bin(datetime_wod01, dep_wod01, bindepths, temp_wod01); %calls extract_bin for processing temperature
% CHL_WOD01 contains:
    % (:,1) year
    % (:,2) month
    % (:,3) median chl for all measurements this month within 1st bindepth (0) range
    % ********* Note: bindepths and number of columns for these will change if you change bindepths
    % (:,4) median chl for 2nd bindepth (5)
    % (:,5) median chl for 3rd bindepth (10)
    % (:,6) median chl for 4th bindepth (20)
    % (:,7) median chl for 5th bindepth (40)... 200 by 20's
    % (:,14) median chl for last bindepth (200)
    % (:,15) standard deviation for all measurements this month within 1st bindepth range
    % (:,16) standard deviation for 2nd bindepth (5)
    % (:,17) standard deviation for 3rd bindepth (10)
    % (:,18) standard deviation for 4th bindepth (20)
    % (:,19) standard deviation for 5th bindepth (40) ... 200 by 20s
    % (:,27) standard deviation for last bindepth (200)
load 'C:\Documents and Settings\Lisa Taylor\My Documents\Data\APEX\0005\apex_date_plots'    
% apex_date_plots contains:
% P=dateAdded(:,1);
% T=dateAdded(:,2);
% S=dateAdded(:,3);
% O=dateAdded(:,4);
% BlueRef=dateAdded(:,5);
% BlueSig=dateAdded(:,6);
% LSSRef=dateAdded(:,7);
% LSSSig=dateAdded(:,8);
% theta=dateAdded(:,9);
% sigma=dateAdded(:,10);
% O2sat=dateAdded(:,11);
% O2=dateAdded(:,12);
% Chl=dateAdded(:,13);
% B_LSS=dateAdded(:,14);
% date=dateAdded(:,16);
% B_LSS_b=(LSSSig-51.8)*0.00004637;
% cp=B_LSS*63;
% cp_b=B_LSS_b*63;
% Chl_b=(BlueSig-29)*0.01079772;
% POC_b=10000*B_LSS_b;
%
CHL_APEX = extract_bin_apex_chl(date, P, bindepths, Chl_b);
SAL_APEX = extract_bin_apex_sal(date, P, bindepths, S);
TEMP_APEX = extract_bin_apex_temp(date, P, bindepths, T);
MMYY = extract_mo_apex_chl(date, P, Chl_b);
% CHL_APEX contains:
    % (:,1) year
    % (:,2) month
    % (:,3) median chl for all measurements this month within 1st bindepth (0) range
    % ********* Note: bindepths and number of columns for these will change if you change bindepths
    % (:,4) median chl for 2nd bindepth (5)
    % (:,5) median chl for 3rd bindepth (10)
    % (:,6) median chl for 4th bindepth (20)
    % (:,7) median chl for 5th bindepth (40)... 200 by 20's
    % (:,14) median chl for last bindepth (200)
    % (:,15) standard deviation for all measurements this month within 1st bindepth range
    % (:,16) standard deviation for 2nd bindepth (5)
    % (:,17) standard deviation for 3rd bindepth (10)
    % (:,18) standard deviation for 4th bindepth (20)
    % (:,19) standard deviation for 5th bindepth (40) ... 200 by 20s
    % (:,27) standard deviation for last bindepth (200)
%
% mmyy contains:
yy=MMYY(:,1);
mm=MMYY(:,2);
p_by_mo=MMYY(:,3);
chl_by_mo=MMYY(:,4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
save osd_plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% start here putting wod01 (CHL_WOD01, SAL_WOD01, TEMP_WOD01) and apex (dateAdded) data together
% need to bin the apex data by month
% then plot by month with same months from wod01
% plot chl, sal, temp
% then do interpolation (started on MODIS data with Wayne) for correlation plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots chl for depths <=15m vs date for both data sets
% subplot(2,1,1)
% I=find(dep_wod01<=15);
% plot(datetime_wod01(I),chl_wod01(I),'b.');
% datetick('x','mmmyy');
% subplot(2,1,2)
% plot(date,Chl_b,'mx');
% datetick('x','mmmyy')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots WOD01 chlorophyll by depth according to concentration
% I=find(chlqf_wod01<8); % find all chl with qualifier flags not equal to 8
% scatter(datetime_wod01(I),-dep_wod01(I),3,chl_wod01(I),'filled')
% caxis([0 4])
% colorbar
% axis([min(datetime_wod01),max(datetime_wod01),-200,max(-dep_wod01)])
% datetick('x','mmmyy')
% title({'Chlorophyll','WOD01 Data'})
% ylabel('depth [m]')
% xlabel('time [month]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots WOD01 salinity by depth according to value
% I=find(salqf_wod01<8); % find all sal with qualifier flags not equal to 8
% scatter(datetime_wod01(I),-dep_wod01(I),3,sal_wod01(I),'filled')
% colorbar
% axis([min(datetime_wod01),max(datetime_wod01),-200,max(-dep_wod01)])
% datetick('x','mmmyy')
% title({'Salinity','WOD01 Data'})
% ylabel('depth [m]')
% xlabel('time [month]')
%%%%%%%%%%%%%%%%
% plots WOD01 temperature by depth according to value
% I=find(tempqf_wod01<8); % find all temp with qualifier flags not equal to 8
% scatter(datetime_wod01(I),-dep_wod01(I),3,temp_wod01(I),'filled')
% colorbar
% axis([min(datetime_wod01),max(datetime_wod01),-200,max(-dep_wod01)])
% datetick('x','mmmyy')
% title({'Temperature','WOD01 Data'})
% ylabel('depth [m]')
% xlabel('time [month]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots median WOD01 chl, sal, temp for specified months by selected bindepths
% mo_chl_wod01=CHL_WOD01(:,2); %month
% I=find(mo_chl_wod01==4 | mo_chl_wod01==5); %filter by month
% plot(CHL_WOD01(I,3:14)', -bindepths(1:(end-1)), '.-');
% axis([-2,20,-200,-0]) % sets axis limits
% mo_sal_wod01=SAL_WOD01(:,2);
% I=find(mo_sal_wod01==8 | mo_sal_wod01==9); %filter by month
% plot(SAL_WOD01(I,3:14)', -bindepths(1:(end-1)), '.-');
% mo_temp_wod01=TEMP_WOD01(:,2);
% I=find(mo_temp_wod01==4 | mo_temp_wod01==5); %filter by month
% plot(TEMP_WOD01(I,3:14)', -bindepths(1:(end-1)), '.-');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots median APEX chl, sal, temp for specified months by selected bindepths
% mo_Chl_b=CHL_APEX(:,2); %month
% I=find(mo_Chl_b==4 | mo_Chl_b==5); %filter by month
% plot(CHL_APEX(I,3:14)', -bindepths(1:(end-1)), '.-');
% axis([-2,20,-200,-0]) % sets axis limits
% title({'Median Chlorophyll, APEX Data' 'Aprils and Mays'})
% ylabel('depth [m]')
% xlabel('Chlorophyll [mg/m^3]')
%
% mo_S=SAL_APEX(:,2); %month
% I=find(mo_S==4 | mo_S==5); %filter by month
% plot(SAL_APEX(I,3:14)', -bindepths(1:(end-1)), '.-');
% %axis([-2,20,-200,-0]) % sets axis limits
% title({'Median Salinity, APEX Data' 'Aprils and Mays'})
% ylabel('depth [m]')
% xlabel('Salinity [PSU]')
%
% mo_T=TEMP_APEX(:,2); %month
% I=find(mo_T==4 | mo_T==5); %filter by month
% plot(TEMP_APEX(I,3:14)', -bindepths(1:(end-1)), '.-');
% axis([-2,20,-200,-0]) % sets axis limits
% title({'Median Temperature, APEX Data' 'Aprils and Mays'})
% ylabel('depth [m]')
% xlabel('Temperature [ ]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots median APEX and WOD01 chlorophyll for specified months by selected bindepths
% mo_Chl_b=CHL_APEX(:,2); %month
% K=find(mo_Chl_b==6 | mo_Chl_b==7); %filter by month
% mo_chl_wod01=CHL_WOD01(:,2); %month
% H=find(mo_chl_wod01==6 | mo_chl_wod01==7); %filter by month
% subplot(2,1,1)
% plot(CHL_APEX(K,3:14)', -bindepths(1:(end-1)), 'mx');
% axis([0,10,-200,-0]) % sets axis limits
% title({'Median Chlorophyll, APEX Data' 'Junes and Julys'})
% ylabel('depth [m]')
% xlabel('Chlorophyll [mg/m^3]')
% subplot(2,1,2)
% plot(CHL_WOD01(H,3:14)', -bindepths(1:(end-1)), 'b.');
% axis([0,10,-200,-0]) % sets axis limits
% title({'Median Chlorophyll, WOD01 Data' 'Junes and Julys'})
% ylabel('depth [m]')
% xlabel('Chlorophyll [mg/m^3]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%To plot median chl for a given mm/yy:
% figure % plots median chl for row 18 (May 1991) vs all bindepths
% plot(CHL_WOD01(18,3:14)', -bindepths(1:(end-1)), 'o-') 
% to add standard deviation:
% hold on,plot(CHL_WOD01(18,3:14)'+CHL_WOD01(18,15:end)', -bindepths(1:(end-1)), 'k-') %st dev +
% hold on,plot(CHL_WOD01(18,3:14)'-CHL_WOD01(18,15:end)', -bindepths(1:(end-1)), 'k-') %st dev -
%%%%%%%%%%%%%%%%
% to see multiple months/years: (or filter by I, as above)
% plot(CHL_WOD01(1,3:14)', -bindepths(1:(end-1)), 'o-')
% hold on,plot(CHL_WOD01(6,3:14)', -bindepths(1:(end-1)), '.-') % May 1984
% hold on,plot(CHL_WOD01(13,3:14)', -bindepths(1:(end-1)), '.-') % May 1989
% hold on,plot(CHL_WOD01(15,3:14)', -bindepths(1:(end-1)), '.-') % May 1990
% hold on,plot(CHL_WOD01(17,3:14)', -bindepths(1:(end-1)), '.-')
% hold on,plot(CHL_WOD01(22,3:14)', -bindepths(1:(end-1)), '.-')
% hold on,plot(CHL_WOD01(33,3:14)', -bindepths(1:(end-1)), '.-')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots WOD01 temperature vs salinity
% I=find(salqf_wod01<8 & tempqf_wod01<8 & dep_wod01<1001); % find all sal,temp with qualifier flags not equal to 8
% %scatter(sal_wod01(I),temp_wod01(I),3,'filled') % no colorbar
% scatter(sal_wod01(I),temp_wod01(I),3,-dep_wod01(I),'filled'),colorbar
% title({'T-S' 'WOD01 Data'})
% ylabel('Temperature [C]')
% xlabel('Salinity [PSU]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots APEX temperature vs salinity
% scatter(S,T,3,-P,'filled'),colorbar
% title({'T-S' 'APEX Data'})
% ylabel('Temperature [C]')
% xlabel('Salinity [PSU]')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots WOD01 and APEX temperature vs salinity
% scatter(S,T,3,'om','filled')
% hold on
% I=find(salqf_wod01<8 & tempqf_wod01<8 & dep_wod01<1001); % find all sal with qualifier flags not equal to 8
% scatter(sal_wod01(I),temp_wod01(I),3,'ob','filled')
% title({'T-S','0 - 1000m'})
% ylabel('Temperature [C]')
% xlabel('Salinity [psu]')
% legend('APEX Data','WOD01Data')
% hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots to check if downloaded WOD01 data are reasonable
% figure
% subplot(2,2,1)
% hist(temp_wod01);
% title({'WOD01 OSD Data','Temperature'});
% subplot(2,2,2)
% hist(dep_wod01);
% title({'WOD01 OSD Data','Depth'});
% subplot(2,2,3)
% hist(chl_wod01);
% title({'WOD01 OSD Data','Chlorophyll'});
% subplot(2,2,4)
% hist(sal_wod01);
% title({'WOD01 OSD Data','Salinity'});
% figure
% subplot(2,3,1)
% scatter(temp_wod01,-dep_wod01,'.');
% xlabel('Temperature [C]')
% ylabel('Depth [ft]')
% title({'WOD01 OSD Data','Temperature vs. Depth'})
% subplot(2,3,2)
% scatter(sal_wod01,-dep_wod01,'.');
% xlabel('Salinity [C]')
% ylabel('Depth [ft]')
% title({'WOD01 OSD Data','Salinity vs. Depth'})
% subplot(2,3,3)
% scatter(chl_wod01,temp_wod01,'.');
% xlabel('Chlorophyll [mg/l]')
% ylabel('Temperature [C]')
% title({'WOD01 OSD Data','Chlorophyll vs. Temperature'})
% subplot(2,3,4)
% scatter(chl_wod01,-dep_wod01,'.');
% xlabel('Chlorophyll [mg/l]')
% ylabel('Depth [ft]')
% title({'WOD01 OSD Data','Chlorophyll vs. Depth'})
% subplot(2,3,5)
% scatter(chl_wod01,-dep_wod01,'.');
% axis([min(chl_wod01),max(chl_wod01),-1000,-800])
% xlabel('Chlorophyll [mg/l]')
% ylabel('Depth [ft]')
% title({'WOD01 OSD Data','Chlorophyll vs. Depth'})
% subplot(2,3,6)
% scatter(chl_wod01,-dep_wod01,'.');
% axis([min(chl_wod01),max(chl_wod01),-200,-0])
% xlabel('Chlorophyll [mg/l]')
% ylabel('Depth [ft]')
% title({'WOD01 OSD Data','Chlorophyll vs. Depth'})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% figure
% scatter(datetime_wod01,chl_wod01,'.');
% datetick('x','mmmyy')
% axis([min(datetime_wod01),max(datetime_wod01),min(chl_wod01),max(chl_wod01)])
% xlabel('Date')
% ylabel('Chlorophyll [mg/l]')
% title({'WOD01 OSD Data','Chlorophyll vs. Date'})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% I=find(temp_wod01>0);
% hist(temp_wod01(I));
% I=find(dep_wod01<900);
% hist(dep_wod01(I));
% I=find(800<dep_wod01<1000);
% hist(dep_wod01(I));
% scatter(temp_wod01(I),-dep_wod01(I),'.');
% axis([min(temp_wod01),max(temp_wod01),-1000,-800])
% axis([min(temp_wod01),max(temp_wod01),min(-dep_wod01),max(-dep_wod01)])
% scatter(chl_wod01,-dep_wod01,'.');
% axis([min(chl_wod01),max(chl_wod01),-1000,-800])
% axis([min(chl_wod01),max(chl_wod01),-200,-0])
% scatter(datetime_wod01(I),chl_wod01(I),'.');
% datetick('x','mmmyy')
% title({'Chlorophyll OSD Data'})
% title({'Line 1','Line 2, Line 2'})
% xlabel('Concentration [mg/m^3]')
% ylabel('Depth (ft)') %xlabel('Concentration [mg/m^3]')
% ylabel('Depth (ft)') 
