% Use of this program is described in:
%
% Armstrong, Brandy N., Warner, John C., List, Jeff H., Martini, 
% Marinna A., Montgomery, Ellyn T.,  Traykovski, Peter, Voulgaris, George 
% 2014, Coastal Change Processes Project data report for oceanographic 
% observations near Fire Island, February through May 
% 2014 : 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 6/20/14, Raw met station data is processed and saved to netCDF files.
%                       It is assumed that the netCDF files are stored in the current  
%                       directory. NOAA tide station data is acquired using
%                       THREDDS.
%                       
% Dependencies:  
%       replace.m
%       tight_subplot.m
%       gregorian.m
%       julian.m
%       draw.m

%NOAA tide stations information
%http://dods.ndbc.noaa.gov:8080/thredds/dodsC/data/stdmet/idid#/idid#hyyyy.nc
%http://dods.ndbc.noaa.gov:8080/thredds/dodsC/data/stdmet/

plotDirectory = 'I:\FireIsland\FI_14_WEBOFR\PLOTS';
dataDirectory='\\IGSAGIEGGS-FILE\Stg-proc\data\FireIsland_2014\candidate_final_files';
clf
%%
startTime=datenum(gregorian(julian(2014,2,5,00)));
stopTime=datenum(gregorian(julian(2014,05,20,00)));
timediff=stopTime-startTime;                                %plot xlimits extend 5 percent
xlims=[(startTime-timediff*0.05) (stopTime+timediff*0.05)]; %past start time and stop time
xt=[];
for yy=2014:2014
    for mm=1:12
        tic=datenum(gregorian(julian(yy,mm,01,00)));
        if tic>=xlims(1) & tic<=xlims(2)
            xt=[xt; tic];   % place xticks on the first of every month
        end
    end
end
ylab={ {'Air Temperature','(\circC)'}...   %plot ylabels
    {'Barometric Pressure','(hPa)'},...
    {'Wind Speed','(m/s)'},...
    {'Wind Gust','(m/s)'},...
    {'Wind Direction','(degrees)'}};
%%
met_st={'9851met','9931met','10011met'};
met_files={'9851met-trm.cdf','9931met-trm.cdf','10011met-trm.cdf'};

buoyvariable={'airt' 'airp' 'windspd' 'gust' 'winddir'};
metvariable={'T_21' 'BP_915' 'WS_401' 'WG_402' 'WD_410'};
titles={'Air Temperature (deg C)' 'Barometric Pressure (hPa)' 'Wind Speed (m/s)' 'Wind Gust (m/s)' 'Wind Direction degrees'};
ylims=[ -10 40;970 1070;0 30;0 30;0 360;];
clf
set(gcf,'position',[100 25 850 1100])
h = tight_subplot(5,1,[.04 .3],[.1 .1],[.1 .1]) ;

for p=1:length(metvariable)
    h(p)=subplot(length(metvariable),1,p);hold on;
    
    for st=1:length(met_st)
        nc=(fullfile(dataDirectory,met_files{st}));
        var2plot=squeeze(ncread(nc,metvariable{p}));
        cols={'r','b','k','m','g','y'};
        
        if strmatch(metvariable{p},'WS_401')
            var2plot(var2plot>20)=nan;
        elseif  strmatch(metvariable{p},'WG_402')
            var2plot(var2plot>30)=nan;
        end
        if strmatch(metvariable{p},'WD_410')
            plot(datenum(gregorian(double(ncread(nc,'time')) + (double(ncread(nc,'time2'))/3600/1000/24))),var2plot-13.5,[cols{st} '.']);
        else
            plot(datenum(gregorian(double(ncread(nc,'time')) + (double(ncread(nc,'time2'))/3600/1000/24))),var2plot,cols{st});
        end
        title(titles{p});datetick('x',20);ylim([ylims(p,:)]);
        xlim([datenum(2012,01,01) datenum(2012,06,01)]);
    end      
    for hind=1:length(h)
        set(h(hind),'xlim',xlims,'xtick',xt)
        set(h(hind),'xticklabel',{[datestr(xt,'mmm dd yyyy')]})
        ylabhan=get(h(hind),'Ylabel');
        set(ylabhan,'String',ylab{hind})
        pos=get(ylabhan,'position');
        legend([met_st],'Location','NorthEast');        
    end
end
    set(gcf,'PaperPositionMode','Auto','PaperOrientation','portrait')
    title(h(1),{['Fire island 2014: Meteorological Buoys']})
    print(fullfile(plotDirectory,['ofr2014-xxxx-FI14_met_sites']),'-dpdf');