% pressure_ts.m  An mfile to plot the timeseries of North Carolina pressure
%                data.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use of this program is described in:
%
%  Armstrong, B.N., Warner, J.C., Voulgaris, G., List, J.H., Thieler, E.R., 
%  Martini, M.M., McNinch, J., Book, J.W., Haas, K., 2012, Carolinas Coastal 
%  Change Processes Project Data Report for nearshore observations at Cape 
%  Hatteras, North Carolina, February, 2010: 
%  U.S. Geological Survey Open-File Report 2012-xxxx.
%
% Program written in Matlab 2012b v8.0.0 
% Program ran on PC with Windows 7 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."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% B.Armstrong 9/7/2012
% Dependencies:
%       the netcdf toolbox
%       julian.m
%       dolandscape.m
%       fixpaper.m

clear
more off

addpath('D:\BNA\Matlab\m_cmg\branches\barmstrong\BNA\t_tide_v1.3beta','-end');

% Define data and output directories
dataDirectory = '\\IGSAGIEGGS-FILE\Stg-proc\data\HatterasNearshore10\BBV_reviewed_new';
plotDirectory = 'D:\BNA\data\Carolinas\webpage\dataDVD\Hatteras2010DVD\PLOTS';

% Define adcp data filenames.
plot_location=2;%1=north,2=south,3=offshore
if plot_location==1
    %wave file names
    waveFiles={'8771aqwp-cal.cdf','8781aqdwp-cal.nc','8791aqwp-cal.cdf',...
        '8801adwp-cal.cdf','8641adwp-cal.cdf','8651aqwp-cal.cdf'};
    
    %names of stations
    names={'N1','N2','N3','N4','N5','N6'};
    
    %location
    location='North Sites';
elseif plot_location==2   
    %wave file names
    waveFiles={'8751aqwp-cal.cdf','8711adwp-cal.cdf','8661aqwp-cal.cdf',...
        '8671Aaqwp-cal.cdf','8671Baqwp-cal.cdf'};
    
    %names of stations
    names={'N8','N9','N11','N12A','N12B'};
    
    %location
    location='South Sites';
else %plot location ==3
    %wave file names
    waveFiles={'8721adwp-cal.nc','8731adwp-cal.cdf','8741Aadwp-cal.cdf','8741Badwp-cal.cdf'};
    
    %names of stations
    names={'O1 (South side)','O2 (North Side)','O3 (North Side)',''};
    
    %location
    location='Offshore Sites';
end
filt='no';
nFiles=length(waveFiles);
%% define parameters for plots
if plot_location==3
    startTime=julian(2010,2,5,00);
    stopTime=julian(2010,3,24,00);
else
    startTime=julian(2010,2,5,00);
    stopTime=julian(2010,02,23,00);
end
cols=get(0,'defaultaxescolororder');     %plot color order
ylab={ {'Significant Wave','Height (m)'},...   %plot ylabels
    {'Water Level (m)'},...
    {'Peak Wave','Period (s)'},...
    {'Peak Wave','direction (deg)'} };
timediff=stopTime-startTime;                                %plot xlimits extend 5 percent
xlims=[(startTime-timediff*0.05) (stopTime+timediff*0.05)]; %past start time and stop time

timediff=stopTime-startTime;
xlims=[(startTime-timediff*0.05) (stopTime+timediff*0.05)];
xt=[];
for yy=2010:2010
    for mm=2:2
        for dd=1:2:28
            tic=julian(yy,mm,dd,00);
            if tic>=xlims(1) & tic<=xlims(2)
                xt=[xt; tic];
            end
        end
    end
end
% larger than 'maxGap' hours are not interpolated over
delta_t = 900/(3600*24); %for 15 min. data
jd_int = [startTime:delta_t:stopTime]';
maxGap = 1; %hr

% Get the color scheme to distinguish between data sites
col=get(0,'defaultaxescolororder');

%% The following loop loads the data and creates plots
for f=1:length(names)
    ncobj=[];
    ncwaves=(fullfile(dataDirectory,waveFiles{f}));
    time=double(ncread(ncwaves,'time'));
    time2=double(ncread(ncwaves,'time2'));
    jd_waves = time + (time2/3600/1000/24);
    waveDelta_t=gmean(diff(jd_waves));
    %wave height
    hsig=squeeze(ncread(ncwaves,'wh_4061'));
    theFillValue=ncreadatt(ncwaves,'wh_4061','_FillValue');
    hsig(hsig >= theFillValue)=nan;
    hsig(hsig <= 0)=nan;
    hsig_int(:,f) = smart_interp([jd_waves], [hsig],...
        jd_int, 3);
    %Water Level
    hght_18=squeeze(ncread(ncwaves,'hght_18'));
    theFillValue=ncreadatt(ncwaves,'hght_18','_FillValue');
    hght_18(hght_18 >= theFillValue)=nan;
    hght_18(hght_18 <= 0)=nan;
    hght_18_int(:,f) = smart_interp([jd_waves], [hght_18],...
        jd_int, 3);
    %wave period
    wp_peak=squeeze(ncread(ncwaves,'wp_peak'));
    theFillValue=ncreadatt(ncwaves,'wp_peak','_FillValue');
    wp_peak(wp_peak >= theFillValue)=nan;
    wp_peak(wp_peak <= 0)=nan;
    wp_peak_int(:,f) = smart_interp([jd_waves], [wp_peak],...
        jd_int, 3);
    %wave direction
    wvdir=squeeze(ncread(ncwaves,'wvdir'));
    theFillValue=ncreadatt(ncwaves,'wp_peak','_FillValue');
    wvdir(wvdir >= theFillValue)=nan;
    wvdir(wvdir <= 0)=nan;
    wvdir_int(:,f) = smart_interp([jd_waves], [wvdir],...
        jd_int, 3);
%      hsig_int(:,f) = interp1([jd_waves], [hsig],...
%         jd_int);
end
time_dn=datenum(gregorian(jd_int));
%%
if plot_location==3
corder=get(gca,'ColorOrder');
corder2=zeros(11,3);
corder2(1,:)=corder(1,:);
corder2(2,:)=corder(2,:);
corder2(3,:)=corder(3,:);
corder2(4,:)=corder(3,:);
end
clf
set(gcf,'position',[100 100 900 1000])
subplot(4,1,1)
if plot_location==3
set(gca,'NextPlot','replacechildren');
set(gca,'ColorOrder',corder2);
end
plot(time_dn,hsig_int,'LineWidth',1.2);
legend(names)%,'FontSize',7);
ylabel(ylab{1});
if plot_location==3
set(gca,'YTickLabel',[0 1 2 3 4]);
end
title(['Wave Observations ',location]);
datetick('x',2,'keeplimits','keepticks');

subplot(4,1,2)
if plot_location==3
set(gca,'NextPlot','replacechildren');
set(gca,'ColorOrder',corder2);
end
plot(time_dn,hght_18_int,'LineWidth',1.2);
% legend(names,'FontSize',7);
if plot_location==3
set(gca,'YTickLabel',[7 8 9 10 11 12]);
end
ylabel(ylab{2});
datetick('x',2,'keeplimits','keepticks');

subplot(4,1,3)
if plot_location==3
set(gca,'NextPlot','replacechildren');
set(gca,'ColorOrder',corder2);
end
plot(time_dn,wp_peak_int,'LineWidth',1.2);
% legend(names,'FontSize',7);
if plot_location==3
set(gca,'YTickLabel',[0 5 10 15 20]);
end
ylabel(ylab{3});
datetick('x',2,'keeplimits','keepticks');

subplot(4,1,4)
if plot_location==3
set(gca,'NextPlot','replacechildren');
set(gca,'ColorOrder',corder2);
end
plot(time_dn,wvdir_int,'LineWidth',1.2);
% legend(names,'FontSize',7);
if plot_location==3
set(gca,'YTickLabel',[0 100 200 300]);
end
ylabel(ylab{4});
datetick('x',2,'keeplimits','keepticks');


set(gcf,'PaperPositionMode','Auto')
print(fullfile(plotDirectory,[location,'_waves']),'-dpdf');

        