% abs_ave_ts.m - plots of Mean amplitude for Acoustic Backscatter Systems
% on site 1 and 2 flobees
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use of this program is described in:
%
% Armstrong, Brandy N., Warner, John C., List, Jeff H., Martini, 
% Marinna A., Montgomery, Ellyn T., Voulgaris, George 
% 2014, Coastal Change Processes Project data report for oceanographic 
% observations near Fire Island, January to April 
% 2012 : U.S. Geological Survey Open-File Report 2014-xxxx, available 
% online at http://pubs.usgs.gov/of/2014/xxxx/.
%
% Program written in Matlab v8.0.0.755
% Program ran on PC with Windows 7 64bit
%
%
% "Although this program has been used by the USGS, no warranty, 
% expressed or implied, is made by the USGS or the United States 
% Government as to the accuracy and functioning of the program 
% and related program material nor shall the fact of distribution 
% constitute any such warranty, and no responsibility is assumed 
% by the USGS in connection therewith."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% B. Armstrong 06/20/14, Raw ABS data is processed and saved to netCDF files.
%                       It is assumed that the netCDF files are stored in a 
%                       directory defined by the variable 'dataDirectory'.

%
% Dependencies:  
%       draw.m
%       replace.m
%       julian.m
%       gregorian.m
%       gmean.m
%       dolandscape.m
%       tight_subplot.m
%%
clear
more off

% define data and output directories
dataDirectory = 'I:\FireIsland\FI_12_DataDVD\mfiles';
% dataDirectory = '\\IGSAGIEGGS-FILE\Stg-proc\data\FireIsland_2012\candidate_final_files';
plotDirectory = 'I:\FireIsland\FI_12_DataDVD\PLOTS';

% define ABS data filenames 
dataFiles={'9234abss1s.cdf','9234abss2s.cdf','9234abss3s.cdf'...
            '9204abss1s.cdf','9204abss2s.cdf','9204abss3s.cdf'};
        transducer={'1mHz','2_5mHz','4mHz','1mHz','2_5mHz','4mHz'};
names={'Site 2','Site 1'};

%% set x-limits, ticks; x-limits extend 5% beyond startTime and stopTime
startTime=julian(2012,1,27,00);                   
stopTime=julian(2012,04,19,00);         
timediff=stopTime-startTime;
xlims=[(startTime) (stopTime)]; 
xt=[];
for yy=2012:2012
    for mm=1:5
        for dd=1:15:28
            tic=julian(yy,mm,dd,0);
            if tic>=xlims(1) & tic<=xlims(2)
                xt=[xt; tic]
            end
        end
    end
end

%% Site 2 plot

for f=1:3
    nc=(fullfile(dataDirectory,dataFiles{f}));
    time=double(ncread(nc,'time'));
    time2=double(ncread(nc,'time2'));
    jd{f}= time + (time2/3600/1000/24);
    delta_t=gmean(diff(jd{f}));
    depth{f}=ncread(nc,'depth');
    site=names{1};
    ncobj=squeeze(ncread(nc,'abs_trans_mean'));
    theFillValue=ncreadatt(nc,'abs_trans_mean','_FillValue');
    data=ncobj;
    bads=find(data>=theFillValue);
    data(bads)=nan;
    eval(['mean_',transducer{f},'=data;'])
end
set(gcf,'position',[100 25 1100 850])
ha = tight_subplot(3,1,[.1 .3],[.1 .1],[.1 .1]) ;
dolandscape

pcolor(ha(1),datenum(gregorian(jd{1})),(depth{1}),(log(mean_1mHz)));
axes(ha(1));shading flat;ylabel('Distance from ABS (cm)','FontSize',12);set(gca,'Ydir','reverse');caxis([-9 1]);
c1=colorbar;ylabel(c1,'ABS Units','FontSize',12);
title('Mean Amplitude at 1.035 Mhz','FontSize',12);set(ha(1),'FontSize',12);
set(ha(1),'xtick',datenum(gregorian(xt)));datetick('x',2,'keeplimits','keepticks');   

pcolor(ha(2),datenum(gregorian(jd{2})),(depth{2}),(log(mean_2_5mHz)));
axes(ha(2));shading flat;ylabel('Distance from ABS (cm)','FontSize',12);set(ha(2),'Ydir','reverse');caxis([-9 1]);
c2=colorbar;ylabel(c2,'ABS Units','FontSize',12);
title('Mean Amplitude at 2.535 Mhz','FontSize',12);set(ha(2),'FontSize',12);
set(ha(2),'xtick',datenum(gregorian(xt)));datetick('x',2,'keeplimits','keepticks');

pcolor(ha(3),datenum(gregorian(jd{3})),(depth{3}),(log(mean_4mHz)));
axes(ha(3));shading flat;ylabel('Distance from ABS (cm)','FontSize',12);set(gca,'Ydir','reverse');caxis([-9 1]);
c3=colorbar;ylabel(c3,'ABS Units','FontSize',12);
title('Mean Amplitude at 3.693 Mhz','FontSize',12);set(ha(3),'FontSize',12);
set(ha(3),'xtick',datenum(gregorian(xt)));datetick('x',2,'keeplimits','keepticks');

print(fullfile(plotDirectory,['FI12_site2_abs']),'-dpdf')
print(fullfile(plotDirectory,['FI12_site2_abs']),'-dpng')
%% Site 1 plot

for f=4:6
    nc=(fullfile(dataDirectory,dataFiles{f}));
    time=double(ncread(nc,'time'));
    time2=double(ncread(nc,'time2'));
    jd{f}= time + (time2/3600/1000/24);
    delta_t=gmean(diff(jd{f}));
    depth{f}=ncread(nc,'depth');
    site=names{2}; 
    ncobj=squeeze(ncread(nc,'abs_trans_mean'));
    theFillValue=ncreadatt(nc,'abs_trans_mean','_FillValue');
    data=ncobj;
    bads=find(data>=theFillValue);
    data(bads)=nan;
    eval(['mean2_',transducer{f},'=data;'])
end

set(gcf,'position',[100 25 1100 850])
ha = tight_subplot(3,1,[.1 .3],[.1 .1],[.1 .1]) ;
dolandscape

pcolor(ha(1),datenum(gregorian(jd{4})),(depth{4}),(log(mean2_1mHz)));
axes(ha(1));shading flat;ylabel('Distance from ABS (cm)','FontSize',12);set(gca,'Ydir','reverse');caxis([-9 1]);
c1=colorbar;ylabel(c1,'ABS Units','FontSize',12);
title('Mean Amplitude at 1.035 Mhz','FontSize',12);set(ha(1),'FontSize',12);
set(ha(1),'xtick',datenum(gregorian(xt)));datetick('x',2,'keeplimits','keepticks');   

pcolor(ha(2),datenum(gregorian(jd{5})),(depth{5}),(log(mean2_2_5mHz)));
axes(ha(2));shading flat;ylabel('Distance from ABS (cm)','FontSize',12);set(ha(2),'Ydir','reverse');caxis([-9 1]);
c2=colorbar;ylabel(c2,'ABS Units','FontSize',12);
title('Mean Amplitude at 2.535 Mhz','FontSize',12);set(ha(2),'FontSize',12);
set(ha(2),'xtick',datenum(gregorian(xt)));datetick('x',2,'keeplimits','keepticks');

pcolor(ha(3),datenum(gregorian(jd{6})),(depth{6}),(log(mean2_4mHz)));
axes(ha(3));shading flat;ylabel('Distance from ABS (cm)','FontSize',12);set(gca,'Ydir','reverse');caxis([-9 1]);
c3=colorbar;ylabel(c3,'ABS Units','FontSize',12);
title('Mean Amplitude at 3.693 Mhz','FontSize',12);set(ha(3),'FontSize',12);
set(ha(3),'xtick',datenum(gregorian(xt)));datetick('x',2,'keeplimits','keepticks');

print(fullfile(plotDirectory,['FI12_site1_abs']),'-dpdf')
print(fullfile(plotDirectory,['FI12_site1_abs']),'-dpng')