function [data,failed] = load_1PPS_file (data)
% load_1PPS_file: loads 1PPS datafile
failed=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% initialize variables
data.accel.PPS=[];
data.accel.PPS_field=[];
data.accel.PPS_chan_no=[];
data.accel.sync_field=[];

% load LOG file
if (exist(data.accel.PPS_LOG)==2)
   infile=fopen(data.accel.PPS_LOG);	                  % open data file
   if (infile==-1)                           % check for file
      err_text=sprintf('could not open %s file!\n',data.accel.PPS_LOG);
      fault(err_text);
      return;
   else
      fprintf('loading LOG file: %s\n',data.accel.PPS_LOG);
   end;
   % loop to read file
   i=0;
   chan_count=0;
   orig_data_rate=[];
   decimation=[];
   eof=0;
   readline=fgetl(infile);
   while (eof==0)
      i=i+1;
      
      % extract data
      if ((size(readline,2)>18)&(strcmp(readline(1:18),'INPUT_RANGE   	CH-')==1))
         chan_count=chan_count+1;
         chan_no=str2double(readline(19:20));
         voltage=str2double(readline(22:(end-1)));
         fprintf('  Channel %.0f voltage %.1f\n',chan_no,voltage);
         data.accel.voltage{chan_no}=voltage;
         data.accel.PPS_chan_no(chan_count,:)=[chan_no  chan_count];
      elseif ((size(readline,2)>13)&(strcmp(readline(1:13),'TAPE_SRATE_CH')==1))
         orig_data_rate=str2double(readline(14:21));
         fprintf('  Original Data Rate %.0f\n',orig_data_rate);
      elseif ((size(readline,2)>10)&(strcmp(readline(1:10),'DECIMATION')==1))
         decimation=str2double(readline(11:end));
         fprintf('  Decimation %.0f\n',decimation);
      elseif ((size(readline,2)>6)&(strcmp(readline(1:6),'FORMAT')==1))
         if ((size(readline,2)>52)&(strcmp(readline(1:52),'FORMAT        	3 ASCII-TAB')==1))
            err_text=sprintf('The accel file to be imported must be tab delimited!\n');
            fault(err_text);
            return;
         end;
      else
         %fprintf('  Discarded Line: %s\n',readline);
      end;
      
      % read next line
      eof=feof(infile);
      if (eof==0)
         readline=fgetl(infile);
         if ((isempty(readline)==1)&(feof(infile)==0))
            readline=fgetl(infile);
         end;
      end;
   end;
   % close all files & figures
   fclose(infile);
else
   err_text=sprintf('1PPS "LOA" log file does not exist!\n');
   fault(err_text);
   return;
end;

% compute data rate
if ((isempty(orig_data_rate)==0)&(isempty(decimation)==0))
   data_rate=orig_data_rate/(decimation+1);
   fprintf('  Computed Data Rate %.0f\n',data_rate);
else
   fprintf('could not find a data rate!\n\n');
   return;
end;

% load accel data
fprintf('Loading 1PPS file: %s\n',data.accel.PPS_file);
clock_start=toc;
whan=waitbar(0.10,'Loading 1PPS Data, Please wait...',...
   'Name','Loading',...
   'CloseRequestFcn','closereq',...
   'WindowStyle','Modal',...
   'Pointer','watch');
drawnow
%data.accel.PPS=load_accel_mat(data.accel.PPS_file);
[PPS_mat,fail]=load_tab(data.accel.PPS_file);
set(whan,'Pointer','arrow');
delete(whan);
fprintf(strcat('1PPS Loading Completed, Runtime: %.1f seconds (%.1f minutes)\n'),(toc-clock_start),(toc-clock_start)/60);
if (fail==1)
   return;
end;

% get 1PPS field
data.accel.sync_field=data.accel.PPS_chan_no(1,1);
data.accel.PPS_field=data.accel.PPS_chan_no(end,1);
dlg_prompt={'Enter Channel for 1PPS input:','Enter Channel for Accel Syncronization:','Data Rate (samples/sec/channel):'};
dlg_title='User Channel Input Dialog';
bin=inputdlg(dlg_prompt,dlg_title,1,{num2str(data.accel.PPS_field),num2str(data.accel.sync_field),num2str(data_rate)});
if (isempty(bin)==0)
   data.accel.PPS_field=str2double(char(bin(1)));
   data.accel.sync_field=str2double(char(bin(2)));
   data_rate=str2double(char(bin(3)));
else
   fprintf('input canceled!\n');
   return;
end;
for (i=1:chan_count)
   if (data.accel.PPS_field==data.accel.PPS_chan_no(i,1))
      data.accel.PPS_field(1,2)=data.accel.PPS_chan_no(i,2);
   elseif (data.accel.sync_field==data.accel.PPS_chan_no(i,1))
      data.accel.sync_field(1,2)=data.accel.PPS_chan_no(i,2);
   end;
end;
fprintf('  1PPS Channel: %.0f (field: %.0f)\n',data.accel.PPS_field(1,1),data.accel.PPS_field(1,2));
fprintf('  Accel Sync Channel: %.0f (field: %.0f)\n',data.accel.sync_field(1,1),data.accel.sync_field(1,2));
fprintf('  Data Rate: %.0f\n\n',data_rate);

% compute time vector
data.accel.PPS(:,1)=(1/data_rate)*[1:size(PPS_mat,1)]';
data.accel.PPS(:,2:3)=[PPS_mat(:,data.accel.sync_field(1,2))  PPS_mat(:,data.accel.PPS_field(1,2))];
data.accel.PPS_field(1,2)=3;
data.accel.sync_field(1,2)=2;

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