% ttide_adcp_indiv.m  An mfile to run T_TIDE on South Carolina ADCP 
%                     velocity and 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., 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.
%
% and
%
% Sullivan, C.M., Warner, J.C., Martini, M.A., Voulgaris, G., 
% Work, P.A., Haas, K.A., and Hanes, D.H. (2006) 
% South Carolina Coastal Erosion Study Data Report for Observations
% October 2003 - April 2004., USGS Open-File Report 2005-1429.
%
% 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."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

% C. Sullivan 05/15/06, Water flow data in bins likely exposed to air has
%                       been set to the NetCDF FillValue_.  Editing code to
%                       consider the surface bin the upward-most good bin.
% C. Sullivan 01/11/05, Use data from surface, bottom, and user-selected
%                       mid-depths.  Run the analysis on each 3-month
%                       timeseries using that timeseries' start time and
%                       time step.

more off

% Define data and output directories
dataDirectory = 'X:\data\hatteras09\BBV_reviewed';
plotDirectory = 'D:\BNA\data\Carolinas\webpage\dataDVD\Hatteras2009DVD\mfiles';

% Define adcp data filenames.  
dataFiles={'8542wh-a.nc','8551wh-a.nc',...
            '8572wh-a.nc','8581wh-a.nc'};

for f = 1:length(dataFiles)
    
    nc=netcdf(fullfile(dataDirectory,dataFiles{f}), 'nowrite');
    
    %get the site, the latitude, and the depth of
    %each velocity bin
    site=dataFiles{f}(1:4);
    
    latitude=nc.latitude(:);
    binDepths=nc{'depth'}(:);
    
    %get time in julian days
    jd=nc{'time'}(:) + (nc{'time2'}(:)/3600/1000/24);
     
    %get bottom pressure and convert it to height
    %with H=P/(rho*g) where rho=1025 km/m^3 and
    %g=9.8 m/s^2
    P_4=nc{'P_1294'}(:);
    P_4(P_4>=1e35)=nan; %nan bad points
    Height=P_4/(1025*9.8);
    
    
    %%%%%%run t_tide on pressure data (bottom pressure)
    if ~isempty(Height)
        startInd = find(~isnan(Height),1,'first');
        startTime = julian2datenum(jd(startInd));
        interval = mean(diff(julian2datenum(jd)))*24;
        ttideOutFile = fullfile(plotDirectory,['ttide_pres_',dataFiles{f}(1:4),'.txt']);
        [nameu,fu,tidecon,xout]=t_tide(Height,'start time',startTime,...
                                              'interval',interval,...
                                              'latitude',latitude,...
                                              'output',ttideOutFile);
    end

    %%%%%%run t_tide on velocity data at the bottom
    ubot = [nc{'u_1205'}(:,1)];
    vbot = [nc{'v_1206'}(:,1)];
    bads=find(ubot+vbot >= 1e35);
    ubot(bads) = nan;
    vbot(bads) = nan;

    uvbot = ubot + i*vbot;

    startInd = find(~isnan(uvbot),1,'first');
    startTime = julian2datenum(jd(startInd));
    interval = mean(diff(julian2datenum(jd)))*24;
    ttideOutFile = fullfile(plotDirectory,['ttide_curr_',dataFiles{f}(1:4),'_bottom.txt']);
    [nameu,fu,tidecon,xout]=t_tide(uvbot,'start time',startTime,...
                                         'interval',interval,...
                                         'latitude',latitude,...
                                         'output',ttideOutFile);

    %%%%%%run t_tide on velocity data at the surface
    %%%%%%C. Sullivan, 05/15/06: the surface is the upward-most good bin
    questBins = nc{'u_1205'}.bins_questioned(:);
    if isempty(questBins)
        lastGoodBin = size(nc{'u_1205'}(:),2);
    else
        lastGoodBin = questBins(1)-1;
    end
    depthLastGoodBin = binDepths(lastGoodBin)
    usfc = [nc{'u_1205'}(:,lastGoodBin)];
    vsfc = [nc{'v_1206'}(:,lastGoodBin)];
    bads=find(usfc+vsfc >= 1e35);
    usfc(bads) = nan;
    vsfc(bads) = nan;

    uvsfc = usfc + i*vsfc;

    startInd = find(~isnan(uvsfc),1,'first');
    startTime = julian2datenum(jd(startInd));
    interval = mean(diff(julian2datenum(jd)))*24;
    ttideOutFile = fullfile(plotDirectory,['ttide_curr_',dataFiles{f}(1:4),'_surface.txt']);
    [nameu,fu,tidecon,xout]=t_tide(uvsfc,'start time',startTime,...
                                         'interval',interval,...
                                         'latitude',latitude,...
                                         'output',ttideOutFile);
                                   
    %%%%%%run t_tide on velocity data at user-specified mid-depths
    targetDepths=[4,6]; %mbs
    for b=1:size(targetDepths,2)
        [index_num,dist_to_hit] = value2Index(binDepths,targetDepths(b),0.5);

        u = [nc{'u_1205'}(:,index_num)];
        v = [nc{'v_1206'}(:,index_num)];
        bads=find(u+v >= 1e35);
        u(bads) = nan;
        v(bads) = nan;

        uv = u + i*v;
        
        startInd = find(~isnan(uv),1,'first');
        startTime = julian2datenum(jd(startInd));
        interval = mean(diff(julian2datenum(jd)))*24;
        ttideOutFile = fullfile(plotDirectory,['ttide_curr_',dataFiles{f}(1:4),'_',num2str(targetDepths(b)),'mbs.txt']);
        [nameu,fu,tidecon,xout]=t_tide(uv,'start time',startTime,...
                                           'interval',interval,...
                                           'latitude',latitude,...
                                           'output',ttideOutFile);
    end

    nc=close(nc);
    
   
end
   
    
    
    
    