function [data,failed] = process_GPS (data)
% process_GPS: processes GPS data
failed=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% get GPS file
pwdir=pwd;
if ((exist(data.current_path)==7)|(exist(data.current_path(1:end-1))==7))
   cd(data.current_path);
else
   data.current_path=pwd;
end;
[GPSfilename,GPSpathname]=uigetfile({'*.r17;*.dat', 'All Receiver Files (*.r17, *.dat)';
   '*.r17', 'RT17 Files (*.r17)';
   '*.dat', 'DAT Files (*.dat)';
   '*.*', 'All Files (*.*)'},'Select GPS File to Process');
cd(pwdir);
warning off;
data.GPS.GPS_file=strcat(GPSpathname,GPSfilename);      % concatinate strings for filename
warning on;
if ((isempty(data.GPS.GPS_file)==1)|(exist(data.GPS.GPS_file)==0))
   fprintf('GPS file does not exist!\n');
   return;
else
   % file name string
   j01=findstr(data.GPS.GPS_file,'.');
   filename=data.GPS.GPS_file(1:(j01(end)-1));
   data.GPS.MAT_file=strcat(filename,'.mat');
   
   data.current_path=GPSpathname;
end;

% get file type to process
[PATHSTR,NAME,EXT,VERSN]=fileparts(data.GPS.GPS_file);
if ((isempty(EXT)==0)&(strcmpi(EXT,'.dat')==1))
   answer=questdlg('Select the Data File Type:','Select File Type:','RT17','Pre-Processed','DAT','DAT');
else
   answer=questdlg('Select the Data File Type:','Select File Type:','RT17','Pre-Processed','DAT','RT17');
end;
switch answer
   case 'RT17'
      data.GPS.type='RT17';
      data.GPS.rt17_file=data.GPS.GPS_file;
   case 'Pre-Processed'
      data.GPS.type='PreRT17';
      data.GPS.rt17_file=data.GPS.GPS_file;
   case 'DAT'
      data.GPS.type='DAT';
      data.GPS.DAT_file=data.GPS.GPS_file;
   case 'Cancel'
      fprintf('Load has been canceled!\n');
      return;
   otherwise
      fprintf('Load has been canceled! (unknown option)\n');
      return;
end;

if ((strcmp(data.GPS.type,'RT17')==1)|(strcmp(data.GPS.type,'PreRT17')==1)|(strcmp(data.GPS.type,'DAT')==1))
   % Process RT17 GPS file
   [data,fail]=process_rt17(data);
   if (fail==1)
      return;
   end;
   
   % find cycle slips
   if (isempty(data.GPS.observables)==0)
      [data,fail]=find_slips(data);
      if (fail==1)
         return;
      end;
   end;
else
   return;
end;

% build data fields
data=build_GPS_fields(data);

% save file
whan=waitbar(0.10,'Saving GPS Data, Please wait...',...
   'Name','Saving',...
   'CloseRequestFcn','closereq',...
   'WindowStyle','Modal',...
   'Pointer','watch');
drawnow
clock_start=toc;
data.GPS.version=data.config.version;
junk=data.GPS;
save(data.GPS.MAT_file,'junk');
set(whan,'Pointer','arrow');
delete(whan);
drawnow;
fprintf(strcat('GPS Save Completed, Runtime: %.1f seconds (%.1f minutes)\n'),(toc-clock_start),(toc-clock_start)/60);
clear junk;
data.GPS.loaded=1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
failed=0;
return;
