function output = getData(inputfile,EMGleg,path1,out_list)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Reads in a group of SIMM .mot files and returns a data
% structure with the desired subset of motion capture data, force
% plate data, joint angles, joint torques, muscle lmtc's, muscle
% moment arms, subject initials, and leg with EMG electrodes.
%
% Inputs:
% inputfile - inputfile name (without _IN, _OUT, or .mot)
% EMGleg    - leg with EMG electrodes; L or R.
% path1     - data file path, assuming '~/Documents/MATLAB/data' as
%             a prefix.
% out_list  - vector of desired outputs, where 1 is info, 2 is
%             data, 3 is joints, 4 is lmtcs, and 5 is mom_arms.
%
% Outputs:
% output    - structure that contains the desired subset of the following:
% info      - subject initials and EMGleg
% data      - relevant mocap, force plate data
% joints    - joint angles and torques
% lmtcs     - muscle lmtc's
% mom_arms  - muscle moment arms
%
% Jared Markowitz
% Spring 2011 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Get subject initials and form path:

if length(find(out_list == 1)) > 0
  output.sub    = path1(1:2);                 % subject initials
  output.EMGleg = EMGleg;
end;

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

if length(find(out_list == 2)) > 0
  % Read input file and get relevant column numbers:

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

  A1 = cellstr(labels1);

  % Heel marker and force plate readings:
  if strcmp(EMGleg,'R') == 1
    offleg = 'L';
  elseif strcmp(EMGleg,'L') == 1
    offleg = 'R';
  else
    fprintf('Erroneous EMGleg');
    return;
  end;
  
  rhz  = find(strcmp(A1,'R.HEEL_pz') > 0) - 1;
  lhz = find(strcmp(A1,'L.HEEL_pz') > 0) - 1;
  fpz = find(strcmp(A1,'ground_force_vz_gcv') > 0) - 1;

  output.time     = x1;
  output.heel_z_r = y1(:,rhz);
  output.heel_z_l = y1(:,lhz);
  output.fz1      = y1(:,fpz(1)); 
  output.fz2      = y1(:,fpz(2)); 
end;

if length(find(out_list==3))+length(find(out_list==4))+length(find(out_list==5))+length(find(out_list==6)) > 0
  % Read output file and get relevant column numbers:

  [labels2,x2,y2] = readColData([path1 '/' inputfile '_crit.mot']);

  A2 = cellstr(labels2);
  EMGleg = lower(EMGleg);

end;  

if length(find(out_list==3)) > 0
  % Joint Angles, Torques:
  aar = find(strcmp(A2,['ankle_flex_r_gcv']) > 0) - 1;
  kar = find(strcmp(A2,['knee_flex_r_gcv']) > 0) - 1;
  har = find(strcmp(A2,['hip_flex_r_gcv']) > 0) - 1;
  atr = find(strcmp(A2,['ankle_flex_r_moment_gcv']) > 0) - 1;
  ktr = find(strcmp(A2,['knee_flex_r_moment_gcv']) > 0) - 1;
  htr = find(strcmp(A2,['hip_flex_r_moment_gcv']) > 0) - 1;
  aal = find(strcmp(A2,['ankle_flex_l_gcv']) > 0) - 1;
  kal = find(strcmp(A2,['knee_flex_l_gcv']) > 0) - 1;
  hal = find(strcmp(A2,['hip_flex_l_gcv']) > 0) - 1;
  atl = find(strcmp(A2,['ankle_flex_l_moment_gcv']) > 0) - 1;
  ktl = find(strcmp(A2,['knee_flex_l_moment_gcv']) > 0) - 1;
  htl = find(strcmp(A2,['hip_flex_l_moment_gcv']) > 0) - 1;

  output.time        = x2;
  output.ankle_ang_r = y2(:,aar);
  output.knee_ang_r  = y2(:,kar);
  output.hip_ang_r   = y2(:,har);
  output.ankle_mom_r = y2(:,atr);
  output.knee_mom_r  = y2(:,ktr);
  output.hip_mom_r   = y2(:,htr);
  output.ankle_ang_l = y2(:,aal);
  output.knee_ang_l  = y2(:,kal);
  output.hip_ang_l   = y2(:,hal);
  output.ankle_mom_l = y2(:,atl);
  output.knee_mom_l  = y2(:,ktl);
  output.hip_mom_l   = y2(:,htl);
end;

if length(find(out_list==4)) > 0
  % Muscle Lengths
  addl_l  = find(strcmp(A2,['add_long_' EMGleg '_len']) > 0) - 1;
  addm1_l = find(strcmp(A2,['add_mag1_' EMGleg '_len']) > 0) - 1;
  addm2_l = find(strcmp(A2,['add_mag2_' EMGleg '_len']) > 0) - 1;
  addm3_l = find(strcmp(A2,['add_mag3_' EMGleg '_len']) > 0) - 1;
  gmed1_l = find(strcmp(A2,['glut_med1_' EMGleg '_len']) > 0) - 1;
  gmed2_l = find(strcmp(A2,['glut_med2_' EMGleg '_len']) > 0) - 1;  
  gmed3_l = find(strcmp(A2,['glut_med3_' EMGleg '_len']) > 0) - 1;
  gmax1_l = find(strcmp(A2,['glut_max1_' EMGleg '_len']) > 0) - 1;
  gmax2_l = find(strcmp(A2,['glut_max2_' EMGleg '_len']) > 0) - 1;  
  gmax3_l = find(strcmp(A2,['glut_max3_' EMGleg '_len']) > 0) - 1;
  ill_l   = find(strcmp(A2,['iliacus_' EMGleg '_len']) > 0) - 1;
  pso_l   = find(strcmp(A2,['psoas_' EMGleg '_len']) > 0) - 1;
  smem_l  = find(strcmp(A2,['semimem_' EMGleg '_len']) > 0) - 1;
  sten_l  = find(strcmp(A2,['semiten_' EMGleg '_len']) > 0) - 1;
  bflh_l  = find(strcmp(A2,['bi_fem_lh_' EMGleg '_len']) > 0) - 1;
  rf_l    = find(strcmp(A2,['rectus_fem_' EMGleg '_len']) > 0) - 1;
  bfsh_l  = find(strcmp(A2,['bi_fem_sh_' EMGleg '_len']) > 0) - 1;
  lvas_l  = find(strcmp(A2,['vas_lat_' EMGleg '_len']) > 0) - 1;
  mvas_l  = find(strcmp(A2,['vas_med_' EMGleg '_len']) > 0) - 1;
  ivas_l  = find(strcmp(A2,['vas_int_' EMGleg '_len']) > 0) - 1;
  mgas_l  = find(strcmp(A2,['gas_med_' EMGleg '_len']) > 0) - 1;
  lgas_l  = find(strcmp(A2,['gas_lat_' EMGleg '_len']) > 0) - 1;
  sol_l   = find(strcmp(A2,['soleus_' EMGleg '_len']) > 0) - 1;
  ta_l    = find(strcmp(A2,['tib_ant_' EMGleg '_len']) > 0) - 1;
  
  output.time           = x2;
  output.add_long_len   = y2(:,addl_l);
  output.add_mag1_len   = y2(:,addm1_l);
  output.add_mag2_len   = y2(:,addm2_l);
  output.add_mag3_len   = y2(:,addm3_l);
  output.glut_med1_len  = y2(:,gmed1_l);
  output.glut_med2_len  = y2(:,gmed2_l);
  output.glut_med3_len  = y2(:,gmed3_l);
  output.glut_max1_len  = y2(:,gmax1_l);
  output.glut_max2_len  = y2(:,gmax2_l);
  output.glut_max3_len  = y2(:,gmax3_l);
  output.iliacus_len    = y2(:,ill_l);
  output.psoas_len      = y2(:,pso_l);
  output.semimem_len    = y2(:,smem_l);
  output.semiten_len    = y2(:,sten_l);
  output.bi_fem_lh_len  = y2(:,bflh_l);
  output.rectus_fem_len = y2(:,rf_l);
  output.bi_fem_sh_len  = y2(:,bfsh_l);
  output.vas_lat_len    = y2(:,lvas_l);
  output.vas_med_len    = y2(:,mvas_l);
  output.vas_int_len    = y2(:,ivas_l);
  output.gas_med_len    = y2(:,mgas_l);
  output.gas_lat_len    = y2(:,lgas_l);
  output.soleus_len     = y2(:,sol_l);
  output.tib_ant_len    = y2(:,ta_l);
end;  

if length(find(out_list==5)) > 0
  addl_ma_h  = find(strcmp(A2,['add_long_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  addm1_ma_h = find(strcmp(A2,['add_mag1_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  addm2_ma_h = find(strcmp(A2,['add_mag2_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  addm3_ma_h = find(strcmp(A2,['add_mag3_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  gmed1_ma_h = find(strcmp(A2,['glut_med1_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  gmed2_ma_h = find(strcmp(A2,['glut_med2_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  gmed3_ma_h = find(strcmp(A2,['glut_med3_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  gmax1_ma_h = find(strcmp(A2,['glut_max1_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  gmax2_ma_h = find(strcmp(A2,['glut_max2_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  gmax3_ma_h = find(strcmp(A2,['glut_max3_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  ill_ma_h   = find(strcmp(A2,['iliacus_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  pso_ma_h   = find(strcmp(A2,['psoas_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  smem_ma_h  = find(strcmp(A2,['semimem_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  smem_ma_k  = find(strcmp(A2,['semimem_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  sten_ma_h  = find(strcmp(A2,['semiten_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  sten_ma_k  = find(strcmp(A2,['semiten_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  bflh_ma_h  = find(strcmp(A2,['bi_fem_lh_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  bflh_ma_k  = find(strcmp(A2,['bi_fem_lh_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  rf_ma_h    = find(strcmp(A2,['rectus_fem_' EMGleg '_ma_hip_flex_' EMGleg]) > 0) - 1;
  rf_ma_k    = find(strcmp(A2,['rectus_fem_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  bfsh_ma_k  = find(strcmp(A2,['bi_fem_sh_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  lvas_ma_k  = find(strcmp(A2,['vas_lat_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  mvas_ma_k  = find(strcmp(A2,['vas_med_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  ivas_ma_k  = find(strcmp(A2,['vas_int_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;  
  mgas_ma_k  = find(strcmp(A2,['gas_med_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  mgas_ma_a  = find(strcmp(A2,['gas_med_' EMGleg '_ma_ankle_flex_' EMGleg]) > 0) - 1;
  lgas_ma_k  = find(strcmp(A2,['gas_lat_' EMGleg '_ma_knee_flex_' EMGleg]) > 0) - 1;
  lgas_ma_a  = find(strcmp(A2,['gas_lat_' EMGleg '_ma_ankle_flex_' EMGleg]) > 0) - 1;
  sol_ma_a   = find(strcmp(A2,['soleus_' EMGleg '_ma_ankle_flex_' EMGleg]) > 0) - 1;
  ta_ma_a    = find(strcmp(A2,['tib_ant_' EMGleg '_ma_ankle_flex_' EMGleg]) > 0) - 1;

  output.time            = x2;
  output.add_long_ma_h   = y2(:,addl_ma_h);
  output.add_mag1_ma_h   = y2(:,addm1_ma_h);
  output.add_mag2_ma_h   = y2(:,addm2_ma_h);
  output.add_mag3_ma_h   = y2(:,addm3_ma_h);  
  output.glut_med1_ma_h  = y2(:,gmed1_ma_h);
  output.glut_med2_ma_h  = y2(:,gmed2_ma_h);
  output.glut_med3_ma_h  = y2(:,gmed3_ma_h);
  output.glut_max1_ma_h  = y2(:,gmax1_ma_h);
  output.glut_max2_ma_h  = y2(:,gmax2_ma_h);
  output.glut_max3_ma_h  = y2(:,gmax3_ma_h);
  output.iliacus_ma_h    = y2(:,ill_ma_h);
  output.psoas_ma_h      = y2(:,pso_ma_h);
  output.semimem_ma_h    = y2(:,smem_ma_h);
  output.semimem_ma_k    = y2(:,smem_ma_k);
  output.semiten_ma_h    = y2(:,sten_ma_h);
  output.semiten_ma_k    = y2(:,sten_ma_k);
  output.bi_fem_lh_ma_h  = y2(:,bflh_ma_h);
  output.bi_fem_lh_ma_k  = y2(:,bflh_ma_k);  
  output.rectus_fem_ma_h = y2(:,rf_ma_h);
  output.rectus_fem_ma_k = y2(:,rf_ma_k);
  output.bi_fem_sh_ma_k  = y2(:,bfsh_ma_k);
  output.vas_lat_ma_k    = y2(:,lvas_ma_k);
  output.vas_med_ma_k    = y2(:,mvas_ma_k);
  output.vas_int_ma_k    = y2(:,ivas_ma_k);
  output.gas_med_ma_k    = y2(:,mgas_ma_k);
  output.gas_med_ma_a    = y2(:,mgas_ma_a);
  output.gas_lat_ma_k    = y2(:,lgas_ma_k);
  output.gas_lat_ma_a    = y2(:,lgas_ma_a);  
  output.soleus_ma_a     = y2(:,sol_ma_a);
  output.tib_ant_ma_a    = y2(:,ta_ma_a);  
end;

if length(find(out_list==6)) > 0
  % GRF, COP, GTOR, COM:
  
  copx  = find(strcmp(A2,'ground_force_px_gcv')>0)-1;
  copy  = find(strcmp(A2,'ground_force_py_gcv')>0)-1;
  copz  = find(strcmp(A2,'ground_force_pz_gcv')>0)-1;
  grfx  = find(strcmp(A2,'ground_force_vx_gcv')>0)-1;
  grfy  = find(strcmp(A2,'ground_force_vy_gcv')>0)-1;
  grfz  = find(strcmp(A2,'ground_force_vz_gcv')>0)-1;
  gtorx = find(strcmp(A2,'ground_torque_x_gcv')>0)-1;
  gtory = find(strcmp(A2,'ground_torque_y_gcv')>0)-1;
  gtorz = find(strcmp(A2,'ground_torque_z_gcv')>0)-1;  
  comx  = find(strcmp(A2,'system_mc_px')>0)-1;          
  comy  = find(strcmp(A2,'system_mc_py')>0)-1;          
  comz  = find(strcmp(A2,'system_mc_pz')>0)-1;
  
  output.time     = x2;
  output.cop_l_x  = y2(:,copx(1));
  output.cop_l_y  = y2(:,copy(1));  
  output.cop_l_z  = y2(:,copz(1));  
  output.grf_l_x  = y2(:,grfx(1));
  output.grf_l_y  = y2(:,grfy(1));  
  output.grf_l_z  = y2(:,grfz(1));  
  output.gtor_l_x = y2(:,gtorx(1));
  output.gtor_l_y = y2(:,gtory(1));  
  output.gtor_l_z = y2(:,gtorz(1));  
  output.cop_r_x  = y2(:,copx(2));
  output.cop_r_y  = y2(:,copy(2));  
  output.cop_r_z  = y2(:,copz(2));  
  output.grf_r_x  = y2(:,grfx(2));
  output.grf_r_y  = y2(:,grfy(2));  
  output.grf_r_z  = y2(:,grfz(2));  
  output.gtor_r_x = y2(:,gtorx(2));
  output.gtor_r_y = y2(:,gtory(2));  
  output.gtor_r_z = y2(:,gtorz(2));  
  output.com_x    = y2(:,comx);
  output.com_y    = y2(:,comy);  
  output.com_z    = y2(:,comz);  

end;  