function [ctdData1, ctdData2, ctdData3] = mbay_iwave_ctd;
%
%   Function to extract the ctd data from the ascii files into matlab.
%
%Soupy Alexander, 6/17/2003

%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.

filePath = 'C:\SCHTUFF\MASS_BAY\MBAY_IWAVE\REPORT\DATAFILES';
fileStart = 'amaine';
theScans = 1:67;

filePath2 = 'C:\SCHTUFF\MASS_BAY\MBAY_IWAVE\REPORT\DATAFILES';
fileStart2 = 'ca01';
theScans2 = 1:14;

filePath3 = 'C:\SCHTUFF\MASS_BAY\MBAY_IWAVE\REPORT\DATAFILES';
fileStart3 = 'ca02';
theScans3 = 1:14;

for indexFile = 1:length(theScans)
    theNumber = num2str(theScans(indexFile), '%.0f');
    if length(theNumber) == 1
        theNumber = ['0' theNumber];
    end
    fileName = [fileStart theNumber];
    fileList(indexFile) = {fileName};
    
    headerFile = fullfile(filePath, [fileName '.hdr']); 
    
    headText = textread(headerFile,'%s');
    latInd=find(strcmp(headText,'Lat:'));
    decLat = str2num(headText{latInd+1}) + str2num(headText{latInd+2})/60;
    lonInd=find(strcmp(headText,'Long:'));
    decLon = str2num(headText{lonInd+1}) + str2num(headText{lonInd+2})/60;

    dateInd = find(strcmp(headText, 'Date:'));
    timeInd = find(strcmp(headText, 'Time:'));
    theTime = headText{timeInd+1};
    theDate = datenum([headText{dateInd+1} '-' headText{dateInd+2} '-' ...
            headText{dateInd+3} ' ' theTime(1:2) ':' theTime(3:4)]);
    theJul = datenum2julian(theDate);
    
    dataFile = fullfile(filePath, [fileName '.asc']);
    theData = load(dataFile);
    ctdFile(:,4) = theData(:,5);    %Depth (m)
    ctdFile(:,1) = theJul;  %Julian date
    
    if indexFile == 31
        decLat = 42+14.55/60;
        decLon = (70+33.8/60);
    elseif indexFile == 32;
        decLat = 42.2470;
        decLon = 70.5509;
    elseif indexFile == 2;
        decLon = (70 + 29.348/60);
    elseif indexFile == 19;
        decLon = -70.4644;
        decLat = 42.2942;
    end
    
    if ~isempty(decLat)
        ctdFile(:,2) = decLat;  %Lat
    else
        ctdFile(:,2) = 0;
    end
    if ~isempty(decLon)
        ctdFile(:,3) = decLon; %Lon
    else
        ctdFile(:,3) = 0;
    end
        amaineLat(indexFile) = ctdFile(1,2);   
    amaineLon(indexFile) = ctdFile(1,3);
    ctdFile(:,5) = theData(:,2);    %Sigma theta (kg/m^3)
    ctdFile(:,6) = theData(:,3);    %Salinity (PSU)
    ctdFile(:,7) = theData(:,4);    %Pressure (db)
    ctdFile(:,8) = theData(:,6);    %Temperature (deg. C)
    ctdFile(:,9) = -4*log(theData(:,7)./100);   %Attenuation (m^-1)
    eval(['ctdData1.' fileName ' = ctdFile;']); 
    clear ctdFile
end


for indexFile = 1:length(theScans2)
    theNumber = num2str(theScans2(indexFile), '%.0f');
    if length(theNumber) == 1
        theNumber = ['0' theNumber];
    end
    fileName = [fileStart2 theNumber];
    fileList(indexFile) = {fileName};
    headerFile = fullfile(filePath3, [fileName '.hdr']); 
    
    headText = textread(headerFile,'%s');
    latInd=find(strcmp(headText,'Latitude:'));
    decLat = str2num(headText{latInd+1}) + str2num(headText{latInd+2})/60;
    lonInd=find(strcmp(headText,'Longitude:'));
    decLon = -1*abs(str2num(headText{lonInd+1}) + str2num(headText{lonInd+2})/60);
    dateInd = find(strcmp(headText, 'System'));
    theDate = datenum([headText{dateInd+5} '-' headText{dateInd+4} '-' ...
            headText{dateInd+6} ' ' headText{dateInd+7}]);
    theJul = datenum2julian(theDate);
    
    dataFile = fullfile(filePath2, [fileName '.asc']);
    theData = load(dataFile);
    ctdFile(:,4) = theData(:,10);    %Depth (m)
    ctdFile(:,1) = theJul;  %Julian date
    if ~isempty(decLat)
        ctdFile(:,2) = decLat;  %Lat
    else
        ctdFile(:,2) = 0;
    end
    if ~isempty(decLon)
        ctdFile(:,3) = decLon; %Lon
    else
        ctdFile(:,3) = 0;
    end
    ctdFile(:,5) = theData(:,9);    %Sigma theta (kg/m^3)
    ctdFile(:,6) = theData(:,7);    %Salinity (PSU)
    ctdFile(:,7) = theData(:,2);    %Pressure (db)
    ctdFile(:,8) = theData(:,6);    %Temperature (deg. C)
    ctdFile(:,9) = NaN; %-4*log(theData(:,7)./100);   %Attenuation (m^-1)
    eval(['ctdData2.' fileName ' = ctdFile;']); 
    clear ctdFile
end

for indexFile = 1:length(theScans3)
    theNumber = num2str(theScans3(indexFile), '%.0f');
    if length(theNumber) == 1
        theNumber = ['0' theNumber];
    end
    fileName = [fileStart3 theNumber];
    fileList(indexFile) = {fileName};
    headerFile = fullfile(filePath3, [fileName '.hdr']); 
    
    headText = textread(headerFile,'%s');
    latInd=find(strcmp(headText,'Latitude:'));
    decLat = str2num(headText{latInd+1}) + str2num(headText{latInd+2})/60;
    lonInd=find(strcmp(headText,'Longitude:'));
    decLon = -1*abs(str2num(headText{lonInd+1}) + str2num(headText{lonInd+2})/60);
    dateInd = find(strcmp(headText, 'System'));
    theDate = datenum([headText{dateInd+5} '-' headText{dateInd+4} '-' ...
            headText{dateInd+6} ' ' headText{dateInd+7}]);
    theJul = datenum2julian(theDate);
    
    dataFile = fullfile(filePath2, [fileName '.asc']);
    theData = load(dataFile);
    ctdFile(:,4) = theData(:,10);    %Depth (m)
    ctdFile(:,1) = theJul;  %Julian date
    if ~isempty(decLat)
        ctdFile(:,2) = decLat;  %Lat
    else
        ctdFile(:,2) = 0;
    end
    if ~isempty(decLon)
        ctdFile(:,3) = decLon; %Lon
    else
        ctdFile(:,3) = 0;
    end
    ctdFile(:,5) = theData(:,9);    %Sigma theta (kg/m^3)
    ctdFile(:,6) = theData(:,7);    %Salinity (PSU)
    ctdFile(:,7) = theData(:,2);    %Pressure (db)
    ctdFile(:,8) = theData(:,6);    %Temperature (deg. C)
    ctdFile(:,9) = NaN; %-4*log(theData(:,7)./100);   %Attenuation (m^-1)
    eval(['ctdData3.' fileName ' = ctdFile;']); 
    clear ctdFile
end
