function [plot_switches,failed] = accel_plots (data,plot_type)
% accel_plots: calls plot routine for accel time, freq, or amp plots
failed=1;
plot_switches=[];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if (data.accel.loaded==0)
   err_text=sprintf('Please load Accel data!\n');
   fault(err_text);
   return;
end;

% get the ACCEL file directory
j01=findstr(data.accel.ACCEL_file,'\');
data.current_path=data.accel.ACCEL_file(1:j01(end));
if (exist(data.current_path)~=7)
   err_text=sprintf('The directory for the ACCEL data does not exist!\n');
   fault(err_text);
   return;
end;

% file name string
j01=findstr(data.accel.ACCEL_file,'.');
filename01=data.accel.ACCEL_file(1:(j01(end)-1));
j02=findstr(filename01,'\');
filename02=filename01((j02(end)+1):end);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% switches

if ((plot_type==1)|(plot_type==3))
   if (plot_type==1)
      data.temp.plot_type=1;
   elseif (plot_type==3)
      data.temp.plot_type=3;
   end;
   % call dialog to get elements to plot
   fig=accel_select(data);
   uiwait(fig);
   if ~ishandle(fig)
      return;
   else
      dbhandles = guidata(fig);
      answer = get(dbhandles.figure1,'UserData');
      delete(fig);
      
      if (isempty(answer)==1)
         return;
      else
         data=answer;
      end;
   end;
elseif (plot_type==2)
   % call dialog to get elements to plot
   fig=psd_select(data);
   uiwait(fig);
   if ~ishandle(fig)
      return;
   else
      dbhandles = guidata(fig);
      answer = get(dbhandles.figure1,'UserData');
      delete(fig);
      
      if (isempty(answer)==1)
         return;
      else
         data=answer;
      end;
   end;
else
   err_text=sprintf('Unknown accel plot type!\n');
   fault(err_text);
   return;
end;

if (plot_type==1)
   plot_by_location=data.config.accel.time.plot_by_location;
   data.config.single_axes=data.config.accel.time.single_axes;
   plot_mag=data.config.accel.time.plot_mag;
   x_lim=[];
   y_lim=data.config.accel.time.y_limits;
   mag_sort=0;
elseif (plot_type==2)
   plot_by_location=data.config.accel.freq.plot_by_location;
   data.config.single_axes=data.config.accel.freq.single_axes;
   plot_mag=data.config.accel.freq.plot_mag;
   x_lim=data.config.accel.freq.x_limits;
   y_lim=data.config.accel.freq.y_limits;
   mag_sort=0;
elseif (plot_type==3)
   plot_by_location=data.config.accel.amp.plot_by_location;
   data.config.single_axes=data.config.accel.amp.single_axes;
   plot_mag=data.config.accel.amp.plot_mag;
   x_lim=data.config.accel.amp.x_limits;
   y_lim=[0 100 nan];
   mag_sort=data.config.accel.amp.mag_sort;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% add wait bar
whan=waitbar(0.10,'Generating Accel Plots, Please wait...',...
   'Name','Plotting',...
   'CloseRequestFcn','closereq',...
   'WindowStyle','Normal',...
   'Pointer','watch');
drawnow;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% find accel fields

% standard plot settings
data.config.disp_position=0;
data.config.single_label='';

% load EC1 (if applicable)
if ((plot_type==2)&(data.config.accel.freq.show_EC1==1))
   EC1file='EC1spec.txt';
   if (exist(EC1file)==2)
      %EC1=load(EC1file);
      
      %data=dlmread(outputfile,'\t');
      argumentlist{1}=EC1file;
      argumentlist{2}='';
      argumentlist{3}=[-1];
      argumentlist{4}='delimiter';
      argumentlist{5}=sprintf('\t');
      argumentlist{6}='whitespace';
      argumentlist{7}=char([8 13 32]);
      argumentlist{8}='headerlines';
      argumentlist{9}=[0];
      argumentlist{10}='headercolumns';
      argumentlist{11}=[0];
      EC1=dataread('file',argumentlist{:});
      
      if (size(EC1)~=[5 2])
         err_text='EC1 file is improperly sized!';
         fault(err_text);
         return;
      end;
   else
      err_text='EC1 file does not exist!';
      fault(err_text);
      return;
   end;
end;

% find plot fields
completed_locations=[];
still_running=1;
i=0;
n=1;
while (still_running==1)
   i=i+1;
   
   % get loaded fields
   column=[];
   column_set=0;
   current_location='';
   label_name=[];
   current_axis='';
   axes_found=0;
   for (j=1:size(data.config.field_names,1))
      data.config.field_ranges(j,8)=0;
      axis_label='';
      location_label='';
      if (plot_by_location==0)
         if (n<=size(data.config.accel_axis_names,1))
            if (isempty(findstr(data.config.field_names{j},data.config.accel_axis_names{n}))==0)
               % get axis label
               axis_label=data.config.accel_axis_names{n};
               
               % get location label
               for (m=1:size(data.config.accel_axis_locations,1))
                  if (isempty(findstr(data.config.field_names{j},data.config.accel_axis_locations{m}))==0)
                     location_label=data.config.accel_axis_location_label{m};
                     label_name{end+1}=data.config.accel_axis_location_label{m};
                  end;
               end;
               
               % assign to plot array
               if ((isempty(axis_label)==0)&(isempty(location_label)==0))
                  axes_found=axes_found+1;
                  data.config.field_ranges(j,8)=axes_found;
                  column(data.config.field_ranges(j,8))=j;
               end;
            end;
            
            if (j==size(data.config.field_names,1))
               current_axis=data.config.accel_axis_name_label{n};
               column_set=1;
               n=n+1;
            end;
         end;
      elseif (plot_by_location==1)
         for (k=1:size(data.config.accel_axis_names,1))
            if ((isempty(findstr(data.config.field_names{j},data.config.accel_axis_names{k}))==0)&(column_set==0))
               % get axis label
               axis_label=data.config.accel_axis_names{k};
               label_name{end+1}=data.config.accel_axis_name_label{k};
               
               % get location label
               for (m=1:size(data.config.accel_axis_locations,1))
                  if (isempty(findstr(data.config.field_names{j},data.config.accel_axis_locations{m}))==0)
                     location_label=data.config.accel_axis_location_label{m};
                  end;
               end;
               
               % set the current location name (if it's not yet set)
               if (isempty(current_location)==1)
                  if (max(strcmp(completed_locations,location_label))==0)
                     current_location=location_label;
                     completed_locations{end+1}=location_label;
                  end;
               end;
               
               % assign to plot array
               if ((isempty(axis_label)==0)&(isempty(location_label)==0)&(strcmp(current_location,location_label)==1))
                  axes_found=axes_found+1;
                  data.config.field_ranges(j,8)=k;
                  column(data.config.field_ranges(j,8))=j;
               end;
               
               % check to see if all fields have been found
               if (axes_found==size(data.config.accel_axis_names,1))
                  column_set=1;
               end;
            end;
         end;
      end;
   end;
   
   if ((isempty(column)==0)&(column_set==1))
      % build label string
      if (plot_by_location==0)
         label01=current_axis;
      elseif (plot_by_location==1)
         label01=current_location;
      end;
      if (plot_type==1)
         filename=sprintf('Accel %s Time Plot (Accel file - %s)',label01,filename02);
         titlename=sprintf('Accel %s Time Plot (Accel file - %s)',label01,dir_clip(data.accel.ACCEL_file,50));
         data.config.single_label=sprintf('%s Accel (G''s)',label01);
      elseif (plot_type==2)
         if (data.config.accel.freq.backtracking(1)==0)
            backtracking=data.config.accel.freq.backtracking(1);
         else
            backtracking=data.config.accel.freq.backtracking(2);
         end;
         if (data.config.accel.freq.windowing==1)
            windowing='none';
         elseif (data.config.accel.freq.windowing==2)
            windowing='Blackman';
         elseif (data.config.accel.freq.windowing==3)
            windowing='Hamming';
         elseif (data.config.accel.freq.windowing==4)
            windowing='Hanning';
         end;
         if (data.config.accel.freq.max_hold==0)
            max_hold='off';
         elseif (data.config.accel.freq.max_hold==1)
            max_hold='on';
         end;
         filename=sprintf('Accel %s Frequency Plot (Accel file - %s)',label01,filename02);
         titlename=sprintf('Accel %s Frequency Plot (Accel file - %s) FFT step %.0f; back tracking %.0f; windowing %s; max hold %s',...
            label01,dir_clip(data.accel.ACCEL_file,50),data.config.accel.freq.n_fft,backtracking,windowing,max_hold);
         data.config.single_label=sprintf('%s PSD (g^2/Hz)',label01);
      elseif (plot_type==3)
         filename=sprintf('Accel %s Amplitude Plot (Accel file - %s)',label01,filename02);
         titlename=sprintf('Accel %s Amplitude Plot (Accel file - %s)',label01,dir_clip(data.accel.ACCEL_file,50));
         data.config.single_label=sprintf('Sorted %s Amplitude Measurements (%%)',label01);
      end;
      
      % compute magnitude
      if (((plot_mag==1)|(mag_sort==1))&(plot_by_location==1))
         accel_mag=sqrt(data.accel.accel(:,data.config.field_location(column(1),2)).^2+...
            data.accel.accel(:,data.config.field_location(column(2),2)).^2+...
            data.accel.accel(:,data.config.field_location(column(3),2)).^2);
      else
         plot_mag=0;
      end;
      
      if (plot_type==1)
         for (q=1:size(column,2))
            data.config.field_ranges(column(q),1:3)=y_lim;
         end;
         
         if (plot_mag==1)
            q=q+1;
            data.temp.accel_mag=accel_mag;
            data.config.field_names{end+1}='3-D Magnitude (G''s)';
            label_name{end+1}=data.config.field_names{end};
            data.config.field_ranges(end+1,:)=[0 NaN NaN 0 0 0 1 4];
            data.config.field_location(end+1,:)=[8 1 0];
            data.config.field_labels{end+1}=[];
            column(q)=size(data.config.field_ranges,1);
            data.config.field_ranges(column(q),2:3)=[sqrt(3*y_lim(2)^2)  y_lim(3)];
         end;
      end;
      
      % run FFT on data to generate the PSD data
      if (plot_type==2)
         for (q=1:size(column,2))
            % get accel data
            if (data.config.field_location(column(q),1)==1)
               ydata=data.accel.accel(:,data.config.field_location(column(q),2));
            end;
            
            % run PSD FFT
            if (data.config.accel.freq.backtracking(1)==0)
               backtracking=data.config.accel.freq.backtracking(1);
            else
               backtracking=data.config.accel.freq.backtracking(2);
            end;
            [p,f,grms]=psdfft(ydata,data.config.accel.freq.n_fft,...
               data.config.accel.freq.sample_rate,data.config.accel.freq.windowing,...
               backtracking,data.config.accel.freq.max_hold);
            
            % assign to plot variables
            data.temp.PSD.array{q}=[f p];
            data.config.field_names{column(q)}=sprintf('%s grms: %.1f',label_name{q},grms);
            data.config.field_location(column(q),:)=[10 0 0];
            data.config.field_ranges(column(q),7)=inf;
            data.config.field_ranges(column(q),1:2)=y_lim;
            data.config.field_ranges(column(q),4)=0;
            
            clear ydata p f grms;
         end;
         
         if (plot_mag==1)
            q=q+1;
            
            % run PSD FFT
            ydata=accel_mag;
            if (data.config.accel.freq.backtracking(1)==0)
               backtracking=data.config.accel.freq.backtracking(1);
            else
               backtracking=data.config.accel.freq.backtracking(2);
            end;
            [p,f,grms]=psdfft(ydata,data.config.accel.freq.n_fft,...
               data.config.accel.freq.sample_rate,data.config.accel.freq.windowing,...
               backtracking,data.config.accel.freq.max_hold);
            
            % assign to plot variables
            data.temp.PSD.array{q}=[f p];
            data.config.field_names{end+1}=sprintf('3-D Mag grms: %.1f',grms);
            label_name{q}=char(data.config.field_names{end});
            data.config.field_ranges(end+1,:)=[0 NaN NaN 0 0 0 1 4];
            data.config.field_location(end+1,:)=[10 0 0];
            data.config.field_labels{end+1}=[];
            column(q)=size(data.config.field_ranges,1);
            data.config.field_ranges(column(q),7)=inf;
            data.config.field_ranges(column(q),1:2)=y_lim;
            data.config.field_ranges(column(q),4)=0;
            
            clear ydata p f grms;
         end;
         
         if (data.config.accel.freq.show_EC1==1)
            prev_ranges=data.config.field_ranges(column(q),:);
            q=q+1;
            data.temp.PSD.array{q}=EC1;
            data.config.field_names{end+1}='EC1 Spec';
            data.config.field_location(end+1,:)=[10 0 0];
            data.config.field_ranges(end+1,:)=prev_ranges;
            data.config.field_labels{end+1}=[];
            column(q)=size(data.config.field_ranges,1);
         end;
         
         data.temp.PSD.x_limits=x_lim;
         data.temp.PSD.x_label='Frequency (Hz)';
      end;
      
      % generate the amplitude plot data
      if (plot_type==3)
         for (q=1:size(column,2))
            if (mag_sort==0)
               xdata=sort(abs(data.accel.accel(:,data.config.field_location(column(q),2))));
            elseif (mag_sort==1)
               sdata=sortrows(abs([accel_mag  data.accel.accel(:,data.config.field_location(column(q),2))]),1);
               xdata=sdata(:,2);
            end;
            xdata=flipud(xdata);
            ydata=[1:size(xdata,1)]';
            ydata=100.*(ydata./ydata(end));
            
            % assign to plot variables
            data.temp.AMP.array{q}=[xdata  ydata];
            data.config.field_names{column(q)}=sprintf('%s',label_name{q});
            data.config.field_location(column(q),:)=[11 0 0];
            data.config.field_ranges(column(q),7)=inf;
            data.config.field_ranges(column(q),1:2)=y_lim(1:2);
            data.config.field_ranges(column(q),4)=0;
            
            clear xdata ydata;
         end;
         
         if (plot_mag==1)
            q=q+1;
            
            xdata=sort(abs(accel_mag));
            xdata=flipud(xdata);
            ydata=[1:size(xdata,1)]';
            ydata=100.*(ydata./ydata(end));
            
            % assign to plot variables
            data.temp.AMP.array{q}=[xdata  ydata];
            data.config.field_names{end+1}='3-D Magnitude (G''s)';
            label_name{q}=char(data.config.field_names{end});
            data.config.field_ranges(end+1,:)=[0 NaN NaN 0 0 0 1 4];
            data.config.field_location(end+1,:)=[11 0 0];
            data.config.field_labels{end+1}=[];
            column(q)=size(data.config.field_ranges,1);
            data.config.field_ranges(column(q),7)=inf;
            data.config.field_ranges(column(q),1:2)=y_lim(1:2);
            data.config.field_ranges(column(q),4)=0;
            
            clear xdata ydata;
         end;
         
         data.temp.AMP.x_limits=x_lim;
         data.temp.AMP.x_label='Acceleration (G''s)';
      end;
      
      %  generate analysis plot
      [data,fig]=analysis_plot(data,column,titlename,filename,0);
      if (data.config.auto_close==0)
         uiwait(fig);
      end;
      data.temp=[];
      rmfield(data,'temp');
      
      % update waitbar
      if (plot_by_location==0)
         waitbar(i/3,whan);
      elseif (plot_by_location==1)
         waitbar(i/2,whan);
      end;
      drawnow;
   else
      still_running=0;
   end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% remove wait bar
set(whan,'Pointer','arrow');
delete(whan);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
plot_switches=data.config.accel;
failed=0;
return;
