% 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, B.N., Warner, J.C., Voulgaris, G., List, J.H., Thieler, E.R., 
% Martini, M.M., Montgomery, E.T., 2011, Carolinas Coastal Change Processes 
% Project Data Report for Observations near Diamond Shoals, NC from January-April 
% 2009: U.S. Geological Survey Open-File Report 2011-1156.
%
% Program written in Matlab v7.1.0 SP3
% Program ran on PC with Windows XP Professional OS.
%
% "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."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%File directory/location
filedirectory=('X:\hatteras09\candidate_final_files\');

%Filenames 
filenames={'8551p-cal.nc','8572p-cal.nc','8581p-cal.nc','8542pDecl10.8-cal.nc'};

nfiles=length(filenames);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%plot 1-D arrays first
for i=1:nfiles
    % Make sure theautoNaN values is equal to 1 for plotting
    % (browseWaveburst sets it to 0)
    global nctbx_options
    nctbx_options.theAutoNaN=1;
    nctbx_options.theAutoscale=1;
    %open nc file
    eval(['nc',num2str(i),'=netcdf(''',filedirectory,filenames{i},''');']);
    %load data
    clear hght_18 mwh_4064 wh_4061 wp_4060 wp_peak wvdir t;
    eval(['t=datenum(gregorian(nc',num2str(i),'{''time''}(:)+(nc',num2str(i),'{''time2''}(:)/3600/24/1000)));'])
    eval(['hght_18=nc',num2str(i),'{''hght_18''}(:);']);
    eval(['mwh_4064=nc',num2str(i),'{''mwh_4064''}(:);']);
    eval(['wh_4061=nc',num2str(i),'{''wh_4061''}(:);']);
    eval(['wp_4060=nc',num2str(i),'{''wp_4060''}(:);']);
    eval(['wp_peak=nc',num2str(i),'{''wp_peak''}(:);']);
    eval(['wvdir=nc',num2str(i),'{''wvdir''}(:);']);
    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',[0 20]);
    if i==3;
        set(gca,'ylim',[23 25]);
    end;
        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');

    subplot(322)
    plot(t,mwh_4064);title('Maximum Wave Height (m)');
    set(gca,'ylim',[0 5]);
    if i==3;
        set(gca,'ylim',[0 40]);
    end;
        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');

    subplot(323)
    plot(t,wh_4061);title('Significant Wave Height (m)');
    set(gca,'ylim',[0 5]);
    if i==3;
        set(gca,'ylim',[0 40]);
    end;
        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');
    
    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]);
    if i==3;
        set(gca,'ylim',[1 3]);
    end;
        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');

    subplot(325)
    plot(t,wp_peak);title('Peak Wave Period (s)');
    set(gca,'ylim',[0 20])
        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');

    subplot(326)
    plot(t,wvdir,'+');title('Peak Wave Direction (degrees North)');
    set(gca,'ylim',[0 360])
        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');
    
    %print to file
    fixpaper
    drawnow
    eval(['print -dpng -r0 ',filenames{i}(1:4),'wave_qaqc.png;']);
    eval(['crop(''',filenames{i}(1:4),'wave_qaqc.png'');']);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    eval(['vspec=nc',num2str(i),'{''vspec''}(:);']);
    eval(['sspec=nc',num2str(i),'{''sspec''}(:);']);
    eval(['pspec=nc',num2str(i),'{''pspec''}(:);']);
    eval(['frequency=nc',num2str(i),'{''frequency''}(:);']);

    
    %plot 2-D arrays next
    figure(2)
    set(gcf,'position',[100 255 1000 800])
    
    subplot(311)
    plot(frequency(1:80),vspec(:,1:80));title('vspec');
    set(gca,'ylim',[0 6000])

    subplot(312)
    %plot(frequency,sspec)
    plot(frequency(1:80),sspec(:,1:80));title('sspec');
    set(gca,'ylim',[0 6000])

    subplot(313)
%    plot(frequency(1:26),pspec(:,1:26));title('pspec');
    plot(frequency(1:80),pspec(:,1:80));title('pspec');
    xlim([0 0.7]);%xlabel('only plotting pspec frequency(1:26)');
    set(gca,'ylim',[0 6000])

    drawnow
    eval(['print -dpng -r0 ',filenames{i}(1:4),'spec_qaqc.png;']);
    eval(['crop(''',filenames{i}(1:4),'spec_qaqc.png'');']);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %plot 3-D arrays next
    %use Marinnas gui
%     eval(['browseWaveburst(''wavefile'',''',filedirectory,filenames{i},''');']);
%     go_on=input('Ready to start on the next file (type anything to proceed)?');
    ncclose
    close all
end
%%
for i=1:length(filenames);
        figure(3);set(gcf,'position',[100 255 1800 800]);hold on;
        eval(['subplot(',num2str(length(filenames)),'1',num2str(i),');']);    
    eval(['plot(th',num2str(i),'(1,:),th',num2str(i),'(2,:));']);
        title([filenames(i),' Maximum Wave Height (m)']);
    set(gca,'ylim',[0 5]);
        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');
end
    if i==length(filenames)
       drawnow
    eval(['print -dpng -r0 allwaves_qaqc.png;']);
    eval(['crop(''allwaves_qaqc.png'');']); 
    end