
% READSPMR  - Reads all .spmr 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')
%

% 
% 11 Sep 2001

ext = '.spmr';
ij=1;
xj=1;
start=0;
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
       if strncmp(thisDir,'\\tornado\diatom\n1997f',23)|strncmp(thisDir,'\\tornado\diatom\n2001\gp3',26)
              break;
        end;
      fullName = fullfile(thisDir, deblank(lwnFile(j,:)));
      %warning('off')
      %warning('on')
         fprintf(1, 'Reading %s\n', fullName)
         k = k + 1;

         fileid = fopen(fullName,'rt');                                 % Open spmr 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)
            spmrcruise(xj,:) = sscanf(dataStr, '/data_file_name=%s');     %grabs cruise name
        end
    
        if strncmp(dataStr, '/west_longitude',15)                % Grabs longitude
            spmrlong(xj) = sscanf(dataStr, '/west_longitude=%f[DEG]');
        end

        if strncmp(dataStr,'/north_latitude',15)                 % Grabs latitude
            spmrlat(xj) = sscanf(dataStr, '/north_latitude=%f[DEG]');   
        end
   
        if strcmp(dataStr, '/end_header@')                       %Sets end of header flag
            end_head = 1;
        end
        
        
        if end_head==1&length(dataStr)>=4
            if str2num(dataStr(1:4))<(-4)&str2num(dataStr(1:4))>(-21)
            spmr(:,ij) = sscanf(dataStr,'%f');
            ij=ij+1;
            end;
        end
        
    end
       fclose(fileid);
       ij=ij+1;
       xj=xj+1;
    end;
 clear lwnFile;
   
end
