function [data,failed] = process_accel (data)
% process_accel: processes accel data
failed=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% prompt if the user wants to import a 1PPS file

answer=questdlg('Do you wish to import a 1PPS file?','1PPS Import','Yes','No','Cancel','Yes');
switch answer
   case 'Yes'
      j=[1 2];
   case 'No'
      j=[2];
   case 'Cancel'
      fprintf('Load has been canceled!\n');
      return;
   otherwise
      fprintf('Load has been canceled! (unknown option)\n');
      return;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get 1PPS input file

if (j(1)==1)
   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;
   [PPSfilename,PPSpathname]=uigetfile({'*.asc', 'ASCII Tab Delimited Files (*.asc)';
      '*.*', 'All Files (*.*)'},'Select Processed 1PPS File');
   cd(pwdir);
   warning off;
   data.accel.PPS_file=strcat(PPSpathname,PPSfilename);      % concatinate strings for filename
   warning on;
   if ((isempty(data.accel.PPS_file)==1)|(exist(data.accel.PPS_file)==0))
      fprintf('1PPS file does not exist!\n');
      return;
   else
      % file name string
      j01=findstr(data.accel.PPS_file,'.');
      filename=data.accel.PPS_file(1:(j01(end)-1));
      data.accel.PPS_LOG=strcat(filename,'.loa');
      
      data.current_path=PPSpathname;
   end;
else
   data.accel.PPS_LOG='';
   data.accel.PPS_file='';
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get ACCEL input 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;
[ACCELfilename,ACCELpathname]=uigetfile({'*.asc', 'ASCII Tab Delimited Files (*.asc)';
   '*.*', 'All Files (*.*)'},'Select Processed ACCEL File');
cd(pwdir);
warning off;
data.accel.ACCEL_file=strcat(ACCELpathname,ACCELfilename);      % concatinate strings for filename
warning on;
if ((isempty(data.accel.ACCEL_file)==1)|(exist(data.accel.ACCEL_file)==0))
   fprintf('ACCEL file does not exist!\n');
   return;
else
   % file name string
   j01=findstr(data.accel.ACCEL_file,'.');
   filename=data.accel.ACCEL_file(1:(j01(end)-1));
   data.accel.ACCEL_LOG=strcat(filename,'.loa');
   data.accel.MAT_file=strcat(filename,'.mat');
   
   data.current_path=ACCELpathname;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% process file

data.accel.GPS_sync_point=[];
data.accel.ACCEL_sync_point=[];

% process accel files
for (i=j)
   if (i==1)
      [data,fail]=load_1PPS_file(data);
   elseif (i==2)
      [data,fail]=load_accel_file(data,j(1));
   end;
   
   if (fail==1)
      return;
   end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save file

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

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