function [data,failed] = load_accel_file (data,mode)
% load_accel_file: loads accel datafile
failed=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% initialize variables
data.accel.accel=[];
data.accel.voltage=[];
data.accel.labels=[];
data.accel.convert=[];
data.accel.chan_no=[];

% load LOG file
if (exist(data.accel.ACCEL_LOG)==2)
   infile=fopen(data.accel.ACCEL_LOG);	                  % open data file
   if (infile==-1)                           % check for file
      err_text=sprintf('could not open %s file!\n',data.accel.ACCEL_LOG);
      fault(err_text);
      return;
   else
      fprintf('loading LOG file: %s\n',data.accel.ACCEL_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.chan_no(chan_count,:)=[chan_no  chan_count];
      elseif ((size(readline,2)>16)&(strcmp(readline(1:16),'CHANNEL       	0')==1))
         j01=findstr(readline,'"');
         chan_no=str2double(readline(16:17));
         if ((size(j01,2)>=2)&(j01(2)-j01(1)==6)&(strcmp(readline(j01(1):(j01(1)+3)),'"ch-')==1))
            % regular label, do nothing
            default_label{chan_no}='';
         elseif ((size(j01,2)>=2)&(j01(2)-j01(1)>=2))
            label=readline(j01(1)+1:j01(2)-1);
            units='';
            if (strcmp(readline(j01(2):(j01(2)+2)),'", ')==1)
               new_line=readline(j01(2)+3:end);
               j02=findstr(new_line,',');
               if (isempty(j02)==1)
                  units=new_line;
               else
                  units=new_line(1:j02(1)-1);
               end;
            end;
            if ((isempty(label)==0)&(isempty(units)==0))
               default_label{chan_no}=sprintf('%s (%s)',label,units);
            end;
         else
            default_label{chan_no}='';
         end;
         if (isempty(default_label{chan_no})==0)
            fprintf('  Channel %.0f default label: %s\n',chan_no,default_label{chan_no});
         end;
      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);
   fprintf('  Channels Loaded: %.0f\n',chan_count);
else
   err_text=sprintf('Accel "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 ACCEL file: %s\n',data.accel.ACCEL_file);
clock_start=toc;
whan=waitbar(0.10,'Loading ACCEL Data, Please wait...',...
   'Name','Loading',...
   'CloseRequestFcn','closereq',...
   'WindowStyle','Modal',...
   'Pointer','watch');
drawnow
%data.accel.accel=load_accel_mat(data.accel.ACCEL_file);
[data.accel.accel,fail]=load_tab(data.accel.ACCEL_file);
set(whan,'Pointer','arrow');
delete(whan);
fprintf(strcat('ACCEL Loading Completed, Runtime: %.1f seconds (%.1f minutes)\n'),(toc-clock_start),(toc-clock_start)/60);
if (fail==1)
   return;
end;

% get 1PPS field & data rate
data.accel.PPS_field(1,3)=inf;
if (mode==1)
   for (i=1:chan_count)
      if (data.accel.PPS_field(1,1)==data.accel.chan_no(i,1))
         data.accel.PPS_field(1,3)=data.accel.chan_no(i,2);
      end;
   end;
   fprintf('  1PPS Channel: %.0f (field: %.0f)\n',data.accel.PPS_field(1,1),data.accel.PPS_field(1,3));
end;

% get data rate
dlg_prompt={'Data Rate (samples/sec/channel):'};
dlg_title='User Channel Input Dialog';
bin=inputdlg(dlg_prompt,dlg_title,1,{num2str(data_rate)});
if (isempty(bin)==0)
   data_rate=str2double(char(bin));
else
   fprintf('input canceled!\n');
   return;
end;
fprintf('  Data Rate: %.0f\n\n',data_rate);

% build dialog titles & default labels
j=0;
for (i=[data.accel.chan_no(:,1)]')
   if (isempty(data.accel.voltage{i})==0)
      j=j+1;
      data.accel.voltage{j}=data.accel.voltage{i};
      
      dlg_label_prompt{j}=sprintf('channel %.0f (voltage range: %.1f)',i,data.accel.voltage{i});
      dlg_conv_prompt{j}=sprintf('channel %.0f conversion factor (1G = X volts (voltage range: %.1f))',i,data.accel.voltage{j});
      if (isempty(default_label{i})==0)
         dlg_label_default{j}=default_label{i};
      else
         dlg_label_default{j}=sprintf('channel %.0f',i);
      end;
      dlg_conv_default{j}='1';
      dlg_label_field{j}=i;
      dlg_conv_field{j}=i;
   end;
end;

% get labels
dlg_label_title='User Channel Input Dialog';
bin=inputdlg(dlg_label_prompt,dlg_label_title,1,dlg_label_default);
if (isempty(bin)==0)
   for (i=1:size(bin,1))
      data.accel.labels{i}=char(bin(i));
      fprintf('  Channel %.0f Label: %s\n',dlg_label_field{i},data.accel.labels{i});
   end;
else
   fprintf('input canceled!\n');
   return;
end;
fprintf('\n');

% get conversions
dlg_conv_title='User Channel Input Dialog';
bin=inputdlg(dlg_conv_prompt,dlg_conv_title,1,dlg_conv_default);
if (isempty(bin)==0)
   for (i=1:size(bin,1))
      data.accel.convert{i}=str2double(char(bin(i)));
      fprintf('  Channel %.0f conversion: %.3f\n',dlg_conv_field{i},data.accel.convert{i});
   end;
else
   fprintf('input canceled!\n');
   return;
end;
fprintf('\n');

% data & time conversions
clock_start=toc;
whan=waitbar(0.10,'ACCEL Computations, Please wait...',...
   'Name','Computations',...
   'CloseRequestFcn','closereq',...
   'WindowStyle','Modal',...
   'Pointer','watch');
drawnow

% conversions to G-forces
j=0;
for (i=1:size(data.accel.accel,2))
   j=j+1;
   data.accel.accel(:,i)=data.accel.accel(:,i)/data.accel.convert{j};
end;

% compute time vector
data.accel.accel(:,2:(size(data.accel.accel,2)+1))=data.accel.accel;
data.accel.accel(:,1)=(1/data_rate)*[1:size(data.accel.accel,1)]';

set(whan,'Pointer','arrow');
delete(whan);
fprintf(strcat('ACCEL Computations Completed, Runtime: %.1f seconds (%.1f minutes)\n'),(toc-clock_start),(toc-clock_start)/60);

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