function [date dateb stat lat lon dat]=rd_msg(fnam)

% Program to read msg (surface) data files
% called from gen_msg_data 

% INPUTS
  % FNAM = file name to read in.

% OUTPUTS
  % dat-data(p, F_ref, F_sig, LSS_ref LSS_Sig)

% written by Emmanuel Boss, UMaine, 2005.

fid=fopen(fnam,'r')

% fid=fopen('0015.001.msg-all','r')
% read lines until the second byte is '02'-the message containing surface
% data

ii=1;
junk=fscanf(fid,'%x',[2]); %reads the first line and stores the first two strings in variable junk
while(feof(fid)==0) %checks to see if the current file position is at the end of the file, if not:
     if(junk(2)~= 2) %if the second element is not 2
        junk=fgetl(fid); %gets the next line
        junk=fscanf(fid,'%x',[2]); %reads the data in this line and stores the first two strings in variable junk (repeats line 21)
    elseif(junk(2)==2) %if the second element is 2
       fseek(fid,-5,0); %fseek(fid,offset,origin) repositions the file position indicator in fid to the offset relative to origin (back to the beginning of the line)
    %for i=1:32
        junk2(:,:)=fscanf(fid,'%s',[32]); %reads this line and stores the next 32 strings in a two-dimensional array junk2
    %end
        date1=fscanf(fid,'%s',[1]); %stores first date in variable date1
        time1=fscanf(fid,'%s',[1]); %stores first time in variable time1
        for i=1:7 %skips the next 7 strings by
           garb=fscanf(fid,'%s',[1]); %storing - overwriting - each string in variable garbage
        end
        %%%%%%%%%%%
        junk3=fscanf(fid,'%s',[1]); %stores the next string in variable junk3
        nn=length(junk3); %added 04/13/06
        %if (nn==2)
        %if(junk3(1)==01 | junk3(1)==02 | junk3(1)==03)
           %fseek(fid,-2,0); %if position data are not available (will have read first 2 characters of next line), reposition position indicator in fid to the beginning of the line
           %junk=fscanf(fid,'%x',[2]); %reads this line and stores the first two strings in variable junk (repeats line 21)
        if (nn==1) & (str2num(junk3) == 1 |str2num(junk3) == 2 |str2num(junk3) == 3) %if we have position information - junk3 equals 1, 2 or 3
           dat(ii,1)=hex2dec(junk2(5))*16^3+hex2dec(junk2(6))*16^2+hex2dec(junk2(7))*16+hex2dec(junk2(8));%pressure - gets elements 5,6,7, and 8 in array junk2, converts to decimal, and stores in array dat, 1st column for as many rows (ii) as there are records
           dat(ii,2)=hex2dec(junk2(9))*16^3+hex2dec(junk2(10))*16^2+hex2dec(junk2(11))*16+hex2dec(junk2(12));%Blue Ref - 2nd column - gets elements 9, 10, 11, and 12 ......
           dat(ii,3)=hex2dec(junk2(13))*16^3+hex2dec(junk2(14))*16^2+hex2dec(junk2(15))*16+hex2dec(junk2(16));%Blue Sig - 3rd column
           dat(ii,4)=hex2dec(junk2(17))*16^3+hex2dec(junk2(18))*16^2+hex2dec(junk2(19))*16+hex2dec(junk2(20));%LSS Ref - 4th column
           dat(ii,5)=hex2dec(junk2(21))*16^3+hex2dec(junk2(22))*16^2+hex2dec(junk2(23))*16+hex2dec(junk2(24));%Blue Sig - 5th column  
           stat(ii,:)=junk3; %stores junk3 in array stat, one column, for as many rows as there are records
           yr=str2num(date1(1,1:4)); %get 1st row, columns 1, 2, 3, and 4 of date1, change to number, and store in variable yr
           mo=str2num(date1(1,6:7)); %month
           da=str2num(date1(1,9:10)); %day
           hr=str2num(time1(1,1:2)); %hour
           min=str2num(time1(1,4:5)); %minutes
           sec=str2num(time1(1,7:8)); %seconds
           date(ii,:)=datenum(yr,mo,da,hr,min,sec); %returns serial date numbers and stores in array date - one column for as many rows as there are records
           date2=fscanf(fid,'%s',[1]); %gets next string and stores in date2
           time2=fscanf(fid,'%s',[1]); %gets next string and stores in time2
           yr=str2num(date2(1,1:4)); %see above for date1
           mo=str2num(date2(1,6:7));
           da=str2num(date2(1,9:10));
           hr=str2num(time2(1,1:2));
           min=str2num(time2(1,4:5));
           sec=str2num(time2(1,7:8));
           dateb(ii,:)=datenum(yr,mo,da,hr,min,sec);
           lat(ii,:)=fscanf(fid,'%s',[1]); %gets next string and stores in array lat, one column for as many rows as there are records
           lon(ii,:)=fscanf(fid,'%s',[1]); %gets next string and stores in array lon, one column for as many rows as there are records
           garb=fgetl(fid); %gets the next line  
           junk=fscanf(fid,'%x',[2]); %reads the next line and stores the first two strings in variable junk (repeats line 21)
           ii=ii+1; %goes back to line 22
        else
           fseek(fid,-2,0); %if position data are not available (will have read first 2 characters of next line), reposition position indicator in fid to the beginning of the line
           junk=fscanf(fid,'%x',[2]); %reads this line and stores the first two strings in variable junk (repeats line 21)
        end
     end
end
fclose(fid);