
% 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';
ij=1;
names = getspmrdirs;
[r c] = size(names);
k = 0;
xy=1;
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 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)
            lwncruise(ij,:) = sscanf(dataStr, '/data_file_name=%s');     %grabs cruise name
        end
    
        if strncmp(dataStr, '/west_longitude',15)                % Grabs longitude
            lwnlong(ij) = sscanf(dataStr, '/west_longitude=%f[DEG]');
        end

        if strncmp(dataStr,'/north_latitude',15)                 % Grabs latitude
            lwnlat(ij) = sscanf(dataStr, '/north_latitude=%f[DEG]');   
        end
        
        if strncmp(dataStr, '/original_file_name',15)                % Grabs ref data
            lwnref(ij,:) = sscanf(dataStr, '/original_file_name=%s[DEG]');
            reffile=strcat(lwnref(ij,(1:length(lwnref(ij,:))-3)),'REF');
            refid=fopen(strcat(thisDir,'\',reffile));
            if refid>-1
            xt=1;
            clear LuX;
            while ~feof(refid)
                refStr=fgetl(refid);
                        if ~strncmp(refStr,'SATHDR',6)&~strncmp(refStr,'#',1)
                            LuX(:,xt)=sscanf(refStr,'%f');
                            xt=xt+1;
                        end;
            end;
            Lu412(:,xy)=mean(LuX(1,:));
            Lu443(:,xy)=mean(LuX(2,:));
            Lu490(:,xy)=mean(LuX(3,:));
            Lu510(:,xy)=mean(LuX(4,:));
            Lu555(:,xy)=mean(LuX(6,:));
            xy=xy+1;
          else
            Lu412(:,xy)=-999;
            Lu443(:,xy)=-999;
            Lu490(:,xy)=-999;
            Lu510(:,xy)=-999;
            Lu555(:,xy)=-999;
            xy=xy+1;
        end;
        
            
                
            
            
            
        end
   
        
        if strcmp(dataStr, '/end_header@')                       %Sets end of header flag
            end_head = 1;ji=1;
        end
    
        if end_head == 1 & strcmp(dataStr, '/end_header@') == 0 %Grabs lwn data
            switch ji,
            case 1
            lwn412(:,ij) = sscanf(dataStr,'%f %f %f %f %f %f');ji=ji+1;
            case 2
            lwn443(:,ij) = sscanf(dataStr,'%f %f %f %f %f %f');ji=ji+1;
            case 3
            lwn490(:,ij) = sscanf(dataStr,'%f %f %f %f %f %f');ji=ji+1;
            case 4
            lwn510(:,ij) = sscanf(dataStr,'%f %f %f %f %f %f');ji=ji+1;
            case 5
            crap519(:,ij) = sscanf(dataStr,'%f %f %f %f %f %f');ji=ji+1;
            case 6
            lwn555(:,ij) = sscanf(dataStr,'%f %f %f %f %f %f');ji=ji+1;
            end; %switch
        end
        
    end
        
    fclose(fileid);
    ij=ij+1;
    end
    clear lwnFile;
   end
   