function reduce_mot(inputfile,path1)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% reduce_mot takes oversized SIMM _OUT files and discards some of
% the many columns unnecessary for analyses of walking.  Output is
% a much smaller _crit .mot file in the same directory.
%
% Inputs:
% inputfile - name of input walking file to be reduced
% path1     - directory (subject name)
%
% Jared Markowitz
% Summer 2011
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

path1 = ['~/Documents/MATLAB/data/' path1 '/processed/mot/simm_out'];
%path1 = ['~/Documents/MATLAB/data/' path1 '/processed/mot/supp'];

[labels1,x1,y1] = readColData([path1 '/' inputfile '_OUT.mot']);

A1 = cellstr(labels1);

rmcol = [];

for j = 1:length(A1)
  
  if length(findstr('torso',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('lumbar',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('neck',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('arm',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('elbow',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('pro_sup',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('wrist',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('L.',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('R.',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('HEAD.',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('C7_',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;
  if length(findstr('SACR',char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);
  end;  
  if length(findstr(char('l_ma_hip_add_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('l_ma_hip_rot_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('l_ma_hip_flex_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('l_ma_knee_flex_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('l_ma_knee_vv_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('l_ma_ankle_flex_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('l_ma_subt_angle_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end; 
  if length(findstr(char('l_ma_toe_angle_r'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_hip_add_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_hip_rot_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_hip_flex_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_knee_flex_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_knee_vv_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_ankle_flex_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_subt_angle_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;
  if length(findstr(char('r_ma_toe_angle_l'),char(A1(j)))>0)
    rmcol = cat(1,rmcol,j);    
  end;  
end;  

rmcol = unique(rmcol);

y2 = [x1 y1];

A1(rmcol) = [];
y2(:,rmcol) = [];

status = fclose('all');  

fid_in  = fopen([path1 '/' inputfile '_OUT.mot'],'r');
fid_out = fopen([path1 '/' inputfile '_crit.mot'],'w');

label_line = 0;
while label_line == 0
  tline = fgetl(fid_in);
  if length(tline) >= 11 && strcmp(tline(1:11),'datacolumns') == 1
    tline = cat(2,tline(1:12),char(num2str(length(A1))));
  end;
  if length(tline) >= 4
    label_line = strcmp(tline(1:4),'time');
  end; 
  if label_line > 0
    tline = [];
    for k = 1:length(A1)
      tline = sprintf([tline '\t' char(A1(k))]);
    end;
  end;
  fprintf(fid_out,'%s \n',tline);
end;  

format_str = [];

for j=1:length(A1)
  format_str = strcat(format_str,'   %8.6f');
end;

format_str = strcat(format_str,'\n');

fprintf(fid_out,format_str,y2');

status = fclose('all');