function [data,dec_hr,mjd,t_sample] = read_compact_2(filename)

% READ_COMPACT_2
%   Parses a teqc-generated compact2 format file and outputs
%   a matlab-friendly structure containing arrays sorted by 
%   satellite number. Note: Ignores slip indicators (I) from 
%   iod file.
%
%   Each data.??? array has 60 coloumns. 
%
%   Version: 1.0 07/11/2011
%   Contact: berglund@unavco.org
%
%   Usage:[data,dec_hr,mjd,t_sample] = read_compact_2('filename')
%   Input:    filename    - name of file to be parsed
%   Outputs:  data.[gps,glo,sbs,gal] - a structure with values [epoch,PRN]
%             dec_hr      - decimal hours from the start of the file
%             mjd         - time of first epoch (modified julian date)
%             t_sample    - sample rate of the compact file

% open the file
fid=fopen(filename);
%fid=fopen('IRID1940.ion');
% get header - assumes three header lines
header = cell(3,1);
for i=1:3
    header{i}=fgetl(fid);
end
% parse mjd and sample rate
t_sample = textscan(header{2},'%*s%f');
mjd = textscan(header{3},'%*s%f');
tic
% start parseing the data file
i=1; %intialize
while ~feof(fid) % loop until the end of file is reached
    % get two lines 
    line1 = fgetl(fid);
    if str2double(line1(1:2))==0 % catch rows with no data
        data.gps(i,1:60)=NaN; % initialize the row for 60 sats - optimistic
        data.glo(i,1:60)=NaN; 
        data.gal(i,1:60)=NaN;
        data.sbs(i,1:60)=NaN;
        i=i+1;
        break
    end
    line2 = fgetl(fid); % get the data line from the file
    % check to see if sats changed
    switch(str2double(line1(1:2))==-1) 
        case 0 % satnum changed; read in satnum
            satnum = []; % clear satnum
            temp = textscan(line1,'%s'); % read in sat info with text
            temp2 = textscan(line1,'%f','whitespace','GRSE'); % exclude text
            satnum = temp{1}(2:end); % discard first value
            satnum2 = temp2{1}(2:end); %discard first value
            data1 = textscan(line2,'%f','whitespace','I');
            data.gps(i,1:60)=NaN; % initialize the row for 60 sats - optimistic
            data.glo(i,1:60)=NaN; 
            data.gal(i,1:60)=NaN;
            data.sbs(i,1:60)=NaN;
            for isat = 1:length(data1{1});
                sat_type = satnum{isat};
                sat_type = sat_type(isletter(sat_type));
                switch sat_type % determine with GNSS system is being read
                    case 'G'
                        data.gps(i,satnum2(isat)) = data1{1}(isat);
                    case 'R'
                        data.glo(i,satnum2(isat)) = data1{1}(isat);
                    case 'E'
                        data.gal(i,satnum2(isat)) = data1{1}(isat);
                    case 'S'
                        data.sbs(i,satnum2(isat)) = data1{1}(isat);
                end
            end
            i=i+1;
        case 1 % satnum did not change
            data1 = textscan(line2,'%f','whitespace','I'); 
            data.gps(i,1:60)=NaN; % initialize the row for 60 sats
            data.glo(i,1:60)=NaN;
            data.gal(i,1:60)=NaN;
            data.sbs(i,1:60)=NaN;
            for isat = 1:length(data1{1});
                sat_type = satnum{isat};
                sat_type = sat_type(isletter(sat_type));
                switch sat_type
                    case 'G'
                        data.gps(i,satnum2(isat)) = data1{1}(isat);
                    case 'R'
                        data.glo(i,satnum2(isat)) = data1{1}(isat);
                    case 'E'
                        data.gal(i,satnum2(isat)) = data1{1}(isat);
                    case 'S'
                        data.sbs(i,satnum2(isat)) = data1{1}(isat);
                end
            end
            i=i+1;
    end
end
% close the data file
fclose(fid);
% create a time vector
dec_hr = (1:length(data.gps))*t_sample{1}/60/60; % should output decimal hours
% end % end the primary function
toc
