function out = readusbgpsfiles(filename)

% purpose of this function is to read in expected USB GPS files
% site with the extension.  This reader is more specialized for these file
% types.  May 9 2014
% v - 2017-Nov-11 - JKH
% example filename
% filename = '20140508_24_ciGPS.txt';
% filename = '20140831_00_mnGPS.txt';

fileID = fopen(filename);

header_int = cell(20,1); %pre-allocates for speed on the guess that there won't be more than 20 lines of header

data_check = zeros(size(header_int));
i1 = 1; %initialize while loop
i1a = 1; %second (starting) index
rws_num = 10;
while sum(data_check(i1a:i1)) ~= rws_num; %assumes that if there has been 10 rows of numbers then data has started, 
%     note that this has the disadvantage of requiring at least 10 rows
    
    header_int{i1} = fgetl(fileID);
    
    
    line_i1 = textscan(header_int{i1}, '%s', 'delimiter', {',','/',':',' '});
    col_num = str2double(line_i1{1,1});
    
    data_check(i1) = ~isnan(col_num(1)); %if it's a string/nan, likely header
%     data_check(i1) = ~isempty(line_i1{1}); %if it is empty, it's likely header text
    
    i1 = i1+1;
    if i1 <= rws_num;
        i1a = i1;
    else
        i1a = i1- rws_num;
    end
end
% fileID = fclose('all'); %close file (so it can be reopened and re-read)
fclose('all');

header = textscan(header_int{i1a-1}, '%s', 'delimiter', {',','/',':', ' '}, 'MultipleDelimsAsOne', 1);
header = header{1,1}'; %creates line of data units header

header_int = header_int(1:(i1a-2),1);







%scan_format = strcat(repmat('%f',1,(length(line_i1{1,1})-2)), '%s%s'); %sets-up to read in text as floating point string, except for last column
scan_format = strcat( repmat('%s',1, length(line_i1{1,1}) ) ); %sets-up to read in text 
fileID = fopen(filename);
for i1 = 1:(i1a-1); %retrims off first rows of file that contain header without removing the data
    fgetl(fileID);
end


% 4275,14/04/09,21:20:38,0.0,0,0.0,0.0,33.3,6,915.8,986.5,0,0,13.6,180000,
% scan_format(
filedata_raw = textscan(fileID, scan_format, 'delimiter', {',','/',':', ' '}, 'MultipleDelimsAsOne', 1);
% filedata_raw_last2(:,1) = filedata_raw{end-1};
% filedata_raw_last2(:,2) = filedata_raw{end};
% filedata_raw = cell2mat(filedata_raw(:,1:(end-2)));
% filedata_raw(filedata_raw == '-999') = 'NaN'; %removes bad data values, sets as NaN
% filedata_raw(filedata_raw == -10000) = NaN;

out.filename = filename;
out.info = header_int;
out.header = header;
out.raw_data = filedata_raw;
% out.data2 = filedata_raw_last2;
% out.likelyheader = {'SerialDate', 'UTCTime', 'Lat', 'Long', 'Quality', 'Satellites', 'HDOP', 'masl_inacc', 'geoidal_WGS84_sep', 's_since_update', 'ref_stat_ID', 'checksum'};


utc_lcl = 0; %default is neither
if any(not(cellfun(@isempty, strfind(header, 'CompDate')))) && any(not(cellfun(@isempty, strfind(header, 'CompTime'))))%if computer time is avaliable (first choice)
  utc_lcl=2; %local
elseif (any(not(cellfun(@isempty, strfind(header, 'GPSUTCDate')))) && any(not(cellfun(@isempty, strfind(header, 'GPSUTCTime'))))) || (any(not(cellfun(@isempty, strfind(header, 'UTCDate')))) && any(not(cellfun(@isempty, strfind(header, 'UTCTime')))))
  utc_lcl=1; %utc
end
out.utc_lcl=utc_lcl; %output what type of time parameter



i1_ind = 1;
for i1 = 1:(length(header));
%     idx = find(strcmp([C{:}], 'a'))
    if ~isempty(filedata_raw{1, i1_ind}{1,1}) && ~isempty(str2double(filedata_raw{1, i1_ind}{1,1})) ;
        data_col_i1 =  cellfun(@str2num, filedata_raw{:,i1_ind}, 'UniformOutput', 0);
        data_col_i1 = cell2mat(data_col_i1);
        
        if length(data_col_i1) == length(filedata_raw{1, i1_ind});
            if all(cellfun(@isempty, strfind({'UTCDate','UTCTime','CompDate','CompTime','GPSUTCDate','GPSUTCTime'},header{i1})))  %if header does not match any of these strings
                out.(header{i1}) = data_col_i1;
                i1_ind = i1_ind+1;
            elseif isequal(header{i1}, 'GPSUTCDate') || isequal(header{i1}, 'UTCDate') || isequal(header{i1}, 'CompDate')
                if isequal(header{i1}, 'GPSUTCDate') || isequal(header{i1}, 'UTCDate')
                    pre_UC = 'U'; %utc time
                elseif isequal(header{i1}, 'CompDate')
                    pre_UC = 'C'; %comp time
                end
                out.([pre_UC, 'year']) = data_col_i1;
                if out.([pre_UC, 'year'])(1) < 100
                    out.year = out.([pre_UC, 'year']) + 2000;
                end
                data_col_i1 =  cellfun(@str2num, filedata_raw{:,i1_ind+1}, 'UniformOutput', 0);
                data_col_i1 = cell2mat(data_col_i1);
                out.([pre_UC, 'month']) = data_col_i1;
                data_col_i1 =  cellfun(@str2num, filedata_raw{:,i1_ind+2}, 'UniformOutput', 0);
                data_col_i1 = cell2mat(data_col_i1);
                out.([pre_UC, 'day']) = data_col_i1;
                i1_ind = i1_ind+3;
                
            elseif isequal(header{i1}, 'GPSUTCTime') || isequal(header{i1}, 'UTCTime') || isequal(header{i1}, 'CompTime')
                if isequal(header{i1}, 'GPSUTCTime') || isequal(header{i1}, 'UTCTime')
                    pre_UC = 'U'; %utc time
                elseif isequal(header{i1}, 'CompTime')
                    pre_UC = 'C'; %comp time
                end
                out.([pre_UC, 'hour']) = data_col_i1;
                data_col_i1 =  cellfun(@str2num, filedata_raw{:,i1_ind+1}, 'UniformOutput', 0);
                data_col_i1 = cell2mat(data_col_i1);
                out.([pre_UC, 'min']) = data_col_i1;
                data_col_i1 =  cellfun(@str2num, filedata_raw{:,i1_ind+2}, 'UniformOutput', 0);
                data_col_i1 = cell2mat(data_col_i1);
                out.([pre_UC, 'sec']) = data_col_i1;
                i1_ind = i1_ind+3;
            end
        else
            out.(header{i1}) = filedata_raw{:,i1_ind};
            i1_ind = i1_ind+1;
        end
    else
        out.(header{i1}) = filedata_raw{:,i1_ind};
        i1_ind = i1_ind+1;
    end
    
end




% if length(out.likelyheader) ~= size(out.data,2)+2;
%     disp('Warning, possibly unknown data in USB GPS input file')
% end
fclose('all');
