% script quickview_processed_waves.m
%
% a way to quickly view a processed wave file.
%
% jcw Feb 25, 2011
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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, New York, 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."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Dependencies
%       fixpaper.m
%       draw.m
%       julian.m
%       gregorian.m
%       replace.m

dataDirectory = '\\IGSAGIEGGS-FILE\Stg-proc\data\FireIsland_2012\candidate_final_files\';
plotDirectory = 'I:\FireIsland\FI_12_DataDVD\PLOTS';

% define ADCP data filenames 
waveFiles={'9241whp-cal-trm.nc','9261awWvs-p-ll-trm.nc',...
            '9281whp-cal-trm.nc','9303sgdataWvs-p-trm.nc','9321awWvs-p-ll-trm.nc'};
wavenameFiles={'Site 2','Site 3','Site 4','Site 5','site 6'};

nfiles=length(waveFiles);
startTime=datenum(gregorian(julian(2012,1,27,00)));                   
stopTime=datenum(gregorian(julian(2012,04,19,00)));         
timediff=stopTime-startTime;
xlims=[(startTime-timediff*0.05) (stopTime+timediff*0.05)]; 
xt=[];
for yy=2012:2012
    for mm=1:5
%         for dd=1:2:28
            tic=datenum(yy,mm,1);
            if tic>=xlims(1) & tic<=xlims(2)
                xt=[xt; tic];
            end
%         end
    end
end
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%plot 1-D arrays first
for i=1:nfiles
    %open nc file
    eval(['nc',num2str(i),'=''',dataDirectory,waveFiles{i},''';']);
    %load data
    clear hght_18 mwh_4064 wh_4061 wp_4060 wp_peak wvdir t;
    eval(['t=datenum(gregorian(double(ncread(nc',num2str(i),',''time''))+(double(ncread(nc',num2str(i),',''time2''))/3600/24/1000)));'])
    eval(['hght_18=squeeze(ncread(nc',num2str(i),',''hght_18''));']);
    eval(['mwh_4064=squeeze(ncread(nc',num2str(i),',''mwh_4064''));']);
    eval(['wh_4061=squeeze(ncread(nc',num2str(i),',''wh_4061''));']);
    eval(['wp_4060=squeeze(ncread(nc',num2str(i),',''wp_4060''));']);
    try
    eval(['wp_peak=squeeze(ncread(nc',num2str(i),',''wp_peak''));']);
    catch
    eval(['wp_peak=squeeze(ncread(nc',num2str(i),',''dwp_4063''));']);        
    end
    try
    eval(['wvdir=squeeze(ncread(nc',num2str(i),',''wvdir''));']);
    catch
       wvdir=zeros(size(wh_4061));
       wvdir(:)=nan;
    end
    figure(1)
    set(gcf,'position',[100 255 1800 800])

    subplot(321)
    plot(t,hght_18);title('Height of the Sea Surface (m)');
    set(gca,'ylim',[max(hght_18)-2.5 max(hght_18)]);
%     set(gca,'xtick',[min(t) (min(t)+((max(t)-min(t))/4)) (min(t)+((max(t)-min(t))/2)) (max(t)-((max(t)-min(t))/4)) max(t)]);        
    set(gca,'xtick',xt);        
    set(gca,'xlim',[startTime stopTime])    
%     set(gca,'xlim',[min(t) max(t)])
    datetick('x',2,'keeplimits','keepticks');

    subplot(322)
    plot(t,mwh_4064);title('Maximum Wave Height (m)');
    set(gca,'ylim',[0 5]);
    set(gca,'xtick',xt);        
%    set(gca,'xtick',[min(t) (min(t)+((max(t)-min(t))/4)) (min(t)+((max(t)-min(t))/2)) (max(t)-((max(t)-min(t))/4)) max(t)]);
    set(gca,'xlim',[startTime stopTime])    
    datetick('x',2,'keeplimits','keepticks');

    subplot(323)
    plot(t,wh_4061);title('Significant Wave Height (m)');
    set(gca,'ylim',[0 5]);
     set(gca,'xtick',xt);        
%   set(gca,'xtick',[min(t) (min(t)+((max(t)-min(t))/4)) (min(t)+((max(t)-min(t))/2)) (max(t)-((max(t)-min(t))/4)) max(t)]);
    set(gca,'xlim',[startTime stopTime])    
    datetick('x',2,'keeplimits','keepticks');
    
    eval(['th',num2str(i),'(1,:)=t;']);
    eval(['th',num2str(i),'(2,:)=wh_4061;']);
    
    subplot(324)
    plot(t,wp_4060);title('Mean Wave Period (s)');
    set(gca,'ylim',[0 20]);
     set(gca,'xtick',xt);        
%   set(gca,'xtick',[min(t) (min(t)+((max(t)-min(t))/4)) (min(t)+((max(t)-min(t))/2)) (max(t)-((max(t)-min(t))/4)) max(t)]);        
    set(gca,'xlim',[startTime stopTime])    
    datetick('x',2,'keeplimits','keepticks');

    subplot(325)
    plot(t,wp_peak);title('Peak Wave Period (s)');
    set(gca,'ylim',[0 20])
     set(gca,'xtick',xt);        
%       set(gca,'xtick',[min(t) (min(t)+((max(t)-min(t))/4)) (min(t)+((max(t)-min(t))/2)) (max(t)-((max(t)-min(t))/4)) max(t)]);        
    set(gca,'xlim',[startTime stopTime])    
    datetick('x',2,'keeplimits','keepticks');

    subplot(326)
    plot(t,wvdir,'+');title('Peak Wave Direction (degrees North)');
    set(gca,'ylim',[0 360])
      set(gca,'xtick',xt);        
%      set(gca,'xtick',[min(t) (min(t)+((max(t)-min(t))/4)) (min(t)+((max(t)-min(t))/2)) (max(t)-((max(t)-min(t))/4)) max(t)]);        
    set(gca,'xlim',[startTime stopTime])    
    datetick('x',2,'keeplimits','keepticks');
    
    %print to file
    fixpaper
    drawnow
    eval(['print -dpng -r0 ',waveFiles{i}(1:4),'wave_qaqc.png;']);
    eval(['crop(''',waveFiles{i}(1:4),'wave_qaqc.png'');']);
end
%%
colors={'b' 'r' 'g' 'm' 'k' 'c' 'y'};
for i=1:length(waveFiles);
    figure(3);set(gcf,'position',[100 255 1800 800]);hold on;
    %         eval(['subplot(',num2str(length(waveFiles)),'1',num2str(i),');']);
    eval(['plot(th',num2str(i),'(1,:),th',num2str(i),'(2,:),''',colors{i},'.'');']);hold on;
    
end
legend(wavenameFiles);
title(['Maximum Wave Height (m)']);
set(gca,'ylim',[0 4]);
      set(gca,'xtick',xt);        
%set(gca,'xtick',[min(t) (min(t)+((max(t)-min(t))/4)) (min(t)+((max(t)-min(t))/2)) (max(t)-((max(t)-min(t))/4)) max(t)]);
set(gca,'xlim',[min(t) max(t)])
datetick('x',2,'keeplimits','keepticks');
if i==length(waveFiles)
    drawnow
    eval(['print -dpng -r0 allwaves_qaqc.png;']);
    eval(['crop(''allwaves_qaqc.png'');']);
end