function pres_table
%
%   pres_table
%
%   Function to create a table of the times of low water, determined from
%   the pressure record at site B, for the internal waves.

%   Soupy Alexander, 7/8/04

%This Matlab m-file was used to create portions of U.S. Geological Survey 
%Data Series 85. 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.

%Citation:  Butman, Bradford, Alexander, P. Soupy, Anderson, S.P., 
%Lightsom, F.L., Scotti, Alberto, and Beardsley, R.C., 2004, The 
%Massachusetts Bay Internal Wave Experiment, August 1998:  Data Report:  
%U.S. Geological Survey Data Series 85, 1 DVD-ROM.


presFile = 'C:\SCHTUFF\MASS_BAY\MBAY_IWAVE\REPORT\DATAFILES\5371tcp-a.nc';

presID = netcdf(presFile, 'nowrite');
presTime = singlejd(presID{'time'}(:), presID{'time2'}(:));
thePres = presID{'P_4023'}(:);
thePres(find(thePres > 1e5 | thePres < -1e5)) = NaN;
thePres(1:10) = [];
presTime(1:10) = [];

theIndex = 21;
lowPoint = [];
lowWater = [];
highPoint = [];
highWater = [];
lowPres = [];
highPres = [];

while theIndex < (length(thePres)-20)
    
   theWindow = theIndex:theIndex+20;
   
   try
       while  thePres(theIndex) ~= min(thePres(theWindow))
           [theLow, winIndex] = min(thePres(theWindow));
           theIndex = theWindow(winIndex);
           theWindow = theIndex:theIndex+20;
       end
   catch
       break
   end

   lowPoint(length(lowPoint)+1) = theIndex;
   lowWater(length(lowWater)+1) = presTime(theIndex);
   lowPres(length(lowPres)+1) = thePres(theIndex);

   theIndex = theIndex+100;
   theWindow = theIndex:theIndex+20;
   try
       while thePres(theIndex) ~= max(thePres(theWindow));
           [theHigh, winIndex] = max(thePres(theWindow));
           theIndex = theWindow(winIndex);
           theWindow = theIndex:theIndex+20;
       end
       
       highPoint(length(highPoint)+1) = theIndex;
       highWater(length(highWater)+1) = presTime(theIndex);
       highPres(length(highPres)+1) = thePres(theIndex);
   catch
       highPoint(length(highPoint)+1) = NaN;
       highWater(length(highWater)+1) = NaN;
       highPres(length(highPres)+1) = NaN;
       break
   end

   theIndex = theIndex+100;

end


theOutFile(:,1) = julian2datenum(lowWater)-datenum('30-Dec-1899');
theOutFile(:,2) = lowPres;
theOutFile(:,3) = julian2datenum(highWater)-datenum('30-Dec-1899');
theOutFile(:,4) = highPres;
xlsWrite('tide_times.xls', theOutFile)


    
    
    
