% ttide_adcp_indiv.m  An mfile to run T_TIDE on Fire Island ADCP 
%                     velocity and pressure data.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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."
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% B. Armstrong 6/20/14, Raw ADCP data is processed and saved to netCDF files.
%                       It is assumed that the netCDF files are stored in a 
%                       directory defined by the variable 'dataDirectory'.

% Dependencies:
%       t_tide_v1.3beta
%       gregorian.m

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

% Define data and output directories
clear
more off

% define data and output directories
dataDirectory = '\\IGSAGIEGGS-FILE\Stg-proc\data\FireIsland_2012\candidate_final_files';
plotDirectory = 'I:\FireIsland\FI_12_DataDVD\PLOTS';

% Define adcp data filenames.  
dataFiles={'9211wh-trm.nc','9241wh-trm.nc',...
            '9261aw-cal-ll-trm-single.nc','9281wh-trm.nc','9321aw-cal-ll-trm-single.nc','9303sgWvs-tide-trm.nc'};
names={'Site 1','Site 2','Site 3','Site 4','Site 6','Site 5'};

for f = 6:length(dataFiles)
    
    nc=(fullfile(dataDirectory,dataFiles{f}));
    
    %get the site, the latitude, and the depth of
    %each velocity bin
    site=names{f};
    
    latitude=double(ncread(nc,'lat'));
    binDepths=double(ncread(nc,'depth'));
    
    %get time in julian days
    time=double(ncread(nc,'time'));
    time2=double(ncread(nc,'time2'));
    jd= time + (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
    try
        P_4=double(squeeze(ncread(nc,'P_1294')));
        P_4(P_4>=1e35)=nan; %nan bad points
        Height=P_4/(1025*9.8);
    catch
        P_1=double(squeeze(ncread(nc,'P_1')));
        P_1(P_1>=1e35)=nan; %nan bad points
        Height=P_1* 1.019716
    end
    
    
    %%%%%%run t_tide on pressure data (bottom pressure)
    if ~isempty(Height)
        startInd = find(~isnan(Height),1,'first');
        startTime = datenum(gregorian(jd(startInd)));
        interval = mean(diff(datenum(gregorian(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
    u=double(squeeze(ncread(nc,'u_1205')));
    ubot = u(1,:);
    v=double(squeeze(ncread(nc,'v_1206')));    
    vbot = v(1,:);
    bads=find(ubot+vbot >= 1e35);
    ubot(bads) = nan;
    vbot(bads) = nan;

    uvbot = ubot + i*vbot;

    startInd = find(~isnan(uvbot),1,'first');
    startTime = datenum(gregorian(jd(startInd)));
    interval = mean(diff(datenum(gregorian(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 = u(21,:);
%     vsfc = v(21,:);
%     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=[1]; %mbs
    depmin=min(binDepths);
    if depmin>5
        binDepths=binDepths-(depmin+1.5);
    end
    for b=1:size(targetDepths,2)
        [index_num,dist_to_hit] = value2Index(binDepths,targetDepths(b),0.5);

        u_1 = u(index_num,:);
        v_1 = v(index_num,:);
        bads=find(u_1+v_1 >= 1e35);
        u_1(bads) = nan;
        v_1(bads) = nan;

        uv = u_1 + i*v_1;
        
        startInd = find(~isnan(uv),1,'first');
        startTime = datenum(gregorian(jd(startInd)));
        interval = mean(diff(datenum(gregorian(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

    
   
end
   
    
    
    
    