%clear all
%%%% transform output from Leica GEO system

%%% comments Simultaan meting onderaan!!


%%%%%% UNDER CONSTRUCTION %%%%%%

%%% USES latlon2KML
addpath D:\madeschipper\SURVEY\Matlab  %% location latlon2KML

list=dir('*_1.txt');

if length(list)==0
    error('no .txt files found with 1.txt in the filename')
else
    disp(['processing ' num2str(length(list)) ' textfiles' ])
end

for j=1:length(list)
    filenameRAW.name=list(j).name;
    fid = fopen(filenameRAW.name);
    disp(['opening ' filenameRAW.name])
    GPSdata=[]; GPStime=[]; X_East=[]; Y_North=[]; z=[]; PosQlt=[];%% clear matrices
    for i=1:17      % skip header lines
        tline = fgetl(fid);
    end

    gi=0;
    for i=1:200000                % Arbitrary high number

        tline = fgetl(fid);
        if length(tline) >= 50
            gi=gi+1;
            %%% tline info: Stati GPSTime Date Easting Northing H-Ell SDHoriz SDHeighQ
            [ a b c d  e f g h  ] = strread(tline, '%s %s %s %s  %s %s %s %s ', 'delimiter',' ');

            %%% GPS TIME
            [ timehr timemin timesec] = strread(b{1}, '%f %f %f' ,'delimiter' ,':');%% GPS time in UTC hhmmss.decss
            GPStime(gi,1)=timehr*3600+timemin*60+timesec; %%GPS time in sec after midnight

            X_East(gi,1)=str2num(d{:,:}); %% lat
            Y_North(gi,1)=str2num(e{:,:}); %% lon

            z(gi,:)=str2num(f{:,:});
            PosQlt(gi,:)=str2num(g{:,:});
            HgtQlt(gi,:)=str2num(h{:,:});

        else
            break
        end

    end
    fclose(fid);

    %%%% STORE DATA in single Matrix

    GPSdata(:,1)=X_East;
    GPSdata(:,2)=Y_North;
    GPSdata(:,3)=z;
    GPSdata(:,4)=GPStime;   %in sec after midnight
    GPSdata(:,5)=PosQlt;  
    eval([filenameRAW.name(1:end-4) '=GPSdata;' ]); % rewrite to bluelogger unique name

    %eval(['save(''' filenameRAW.name(1:end-4) '.mat'', ''' filenameRAW.name(1:end-4) ''')'])

end
