
% READSPMR  - Reads all .lwn files from cruises found in Diatom
%
% This searches through all Cruise directories for gp*\spmr directories
% Then takes the seabass format files and reads all of them in to a 
% data structure
%
% Use as: x = readlwn
%         x = readlwn(ext)
%
% Inputs: ext = An extension (default = '.lwn', other option is '.ocp')
%

% Dan Cosgrove
% 11 Sep 2001

ext = '.lwn';
outfileID = 0;
[outfile outpath] = uiputfile('*.out','Select an output file',0,0);   %Select output file
if outfile == 0
    return
end

outfileID = fopen(outfile, 'w');                             % 'outfile' will be the location of the composite LWN data
fprintf(outfileID,'Cruise      Longitude     Latitude    Warning   WaveLength     Kd       Es         Kl        Lw        Lwn\n');

names = getspmrdirs;
[r c] = size(names);
k = 0;
for i = 1:r
   thisDir = deblank(names(i,:));
   lwnFile = getfname(fullfile(thisDir, ['gp*' ext]));
   [rr cc] = size(lwnFile);
   for j = 1:rr
      fullName = fullfile(thisDir, deblank(lwnFile(j,:)));
      %warning('off')
      %warning('on')
         fprintf(1, 'Reading %s\n', fullName)
         k = k + 1;

         fileid = fopen(fullName,'rt');                                 % Open LWN data for reading
        if fileid < 0
          warning(['Invalid file name: ' fullName])
            return
        end

    i = 1;
    end_head = 0;

    while ~feof(fileid)                                          % Continue to EOF marker
        warnflg = 0;
        filepos = ftell(fileid);                                 % Get the start of each line
        dataStr = fgetl(fileid);                                 % Get the line
   
        if strncmp(dataStr,'/data_file_name',15)
            cruise = sscanf(dataStr, '/data_file_name=%s');     %grabs cruise name
        end
    
        if strncmp(dataStr, '/west_longitude',15)                % Grabs longitude
            long = sscanf(dataStr, '/west_longitude=%f[DEG]');
        end

        if strncmp(dataStr,'/north_latitude',15)                 % Grabs latitude
            lat = sscanf(dataStr, '/north_latitude=%f[DEG]');   
        end
   
        if strncmp(dataStr,'! Warning',9)                        % Grabs attenuation coefficient warnings
            warn(i) = sscanf(dataStr, '! Warning Kd%fnm');
            i = i + 1;
        end
        
        if strcmp(dataStr, '/end_header@')                       %Sets end of header flag
            end_head = 1;
        end
    
        if end_head == 1 & strcmp(dataStr, '/end_header@') == 0                                         %Grabs lwn data
            data = sscanf(dataStr,'%f %f %f %f %f %f');
            for j = [1:i-1]
                if data(1) == warn(j)
                    warnflg = 1;
                end
            end
            fprintf(outfileID,'%9s   %7.4f   %9.4f   %3d  %15.4f %12.4f %9.4f %9.4f %9.4f %9.4f\n',cruise,long,lat,warnflg,data(1),...
                data(2),data(3),data(4),data(5),data(6));        
        end
    end
    clear warn;
        
    fclose(fileid);

    end
    clear lwnFile;
   end
   
end

if outfileID ~= 0
    fclose(outfileID);
end