function calib_wrap(subject,save_it,wfilter)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  calib_wrap.m combines all EMG normalizations for one subject into a
%  single FMC structure.  In the case of multiple contractions of the
%  same muscle (for instance biarticular muscles) the maximum 
%  normalization value is returned.  If requested, sample "on" and
%  "off" stretches are provided for Weiner filtering. 
%
%  Input:
%  subject - subject ID string.
%  saveit  - 1 if output is to be saved, 0 otherwise. 
%  wfilter - 1 if calibration data to be saved, 0 otherwise
%
%  Output:
%  File with structure containing EMG normalization values.
%
%  Example Usage:
%  calib_wrap('AM062411',1,1)
%
%  Jared Markowitz
%  Summer 2011
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all; clc;

if exist('wfilter')==1 && wfilter==1
  wfilter1 = 1;
else
  wfilter1 = 0;
end;
% Good to go!
if strcmp(subject,'DH121412') == 1
  calibp1 = process_calib('plantar1','DH121412',[12 16],'R',wfilter1);
  calibp2 = process_calib('dorsi1','DH121412',[14],'R',wfilter1);
  calibp3 = process_calib('knee_ext1','DH121412',[2 4],'R',wfilter1); 
  calibp4 = process_calib('knee_flex1','DH121412',[1 6 8],'R',wfilter1);%12
  calibp5 = process_calib('hip_ext1','DH121412',[6 8 9 13 15],'R',wfilter1);%11 
  calibp6 = process_calib('hip_flex1','DH121412',[4 5 7 9],'R',wfilter1);
  calibp7 = process_calib('hip_add1','DH121412',[5 9],'R',wfilter1);
  calibp8 = process_calib('hip_abd','DH121412',[11 13],'R',wfilter1);  
  calib.EMGleg = 'R';
  wdat.EMGleg  = 'R'; 
elseif strcmp(subject,'MC121812') == 1
  calibp1 = process_calib('plantar1','MC121812',[11 15],'L',wfilter1);
  calibp2 = process_calib('dorsi1','MC121812',[13],'L',wfilter1);
  calibp3 = process_calib('knee_ext1','MC121812',[1 9],'L',wfilter1); 
  calibp4 = process_calib('knee_flex5','MC121812',[5 7 11],'L',wfilter1);%3
  calibp5 = process_calib('hip_ext1','MC121812',[3 4 5 6 14 16],'L',wfilter1); 
  calibp6 = process_calib('hip_flex2','MC121812',[6 8 12],'L',wfilter1);%1
  calib.EMGleg = 'L';
  wdat.EMGleg  = 'L'; 
elseif strcmp(subject,'BC011113') == 1
  calibp1 = process_calib('plantar2','BC011113',[14 16],'R',wfilter1);
  calibp2 = process_calib('dorsi1','BC011113',[12],'R',wfilter1);
  calibp3 = process_calib('knee_ext1','BC011113',[9 13],'R',wfilter1); 
  calibp4 = process_calib('knee_flex1','BC011113',[8 11 14 15],'R',wfilter1);
  calibp5 = process_calib('hip_ext2','BC011113',[5 6 7 8 11],'R',wfilter1); 
  calibp6 = process_calib('hip_flex1','BC011113',[2 4 6 9],'R',wfilter1);
  calib.EMGleg = 'R';
  wdat.EMGleg  = 'R'; 
elseif strcmp(subject,'DC011413') == 1
  calibp1 = process_calib('plantar2','DC011413',[13 15],'L',wfilter1);
  calibp2 = process_calib('dorsi1','DC011413',[11],'L',wfilter1);
  calibp3 = process_calib('knee_ext1','DC011413',[14 16],'L',wfilter1); 
  calibp4 = process_calib('knee_flex1','DC011413',[5 7 9 13],'L',wfilter1);
  calibp5 = process_calib('hip_ext1','DC011413',[5 6 8 12],'L',wfilter1);%7 
  calibp6 = process_calib('hip_flex1','DC011413',[1 2 6 16],'L',wfilter1);
  calibp7 = process_calib('hip_add1','DC011413',[1 6],'L',wfilter1);
  calib.EMGleg = 'L';
  wdat.EMGleg  = 'L'; 
elseif strcmp(subject,'JB011613') == 1
  calibp1 = process_calib('plantar1','JB011613',[12 14],'R',wfilter1);
  calibp2 = process_calib('dorsi1','JB011613',[16],'R',wfilter1);
  calibp3 = process_calib('knee_ext1','JB011613',[13 15],'R',wfilter1); 
  calibp4 = process_calib('knee_flx1','JB011613',[6 8 11 12],'R',wfilter1);
  calibp5 = process_calib('hip_ext1','JB011613',[4 5 7 11],'R',wfilter1);%6 
  calibp6 = process_calib('hip_flx3','JB011613',[2 4 9 15],'R',wfilter1);
  calibp7 = process_calib('hip_add1','JB011613',[2 4],'R',wfilter1);
  calib.EMGleg = 'R';
  wdat.EMGleg  = 'R'; 
elseif strcmp(subject,'AM062411') == 1
  calibp1 = process_calib('plantar3','AM062411',[1 3 4],'L',wfilter1);
  calibp2 = process_calib('dorsi1','AM062411',[2],'L',wfilter1);
  calibp3 = process_calib('knee_ext1','AM062411',[5 6],'L',wfilter1); 
  calibp4 = process_calib('knee_flex5','AM062411',[3 4 7 8 9],'L',wfilter1);
  calibp5 = process_calib('hip_ext3','AM062411',[7 9 11],'L',wfilter1); 
  calibp6 = process_calib('hip_flex2','AM062411',[6 12],'L',wfilter1);
  calib.EMGleg = 'L';
  wdat.EMGleg  = 'L';  
elseif strcmp(subject,'AD052611') == 1
  calibp1 = process_calib('plantar4','AD052611',[1 3 4],'L',wfilter1);
  calibp2 = process_calib('dorsi2','AD052611',[2],'L',wfilter1);
  calibp3 = process_calib('knee_ext2','AD052611',[5 6],'L',wfilter1); 
  calibp4 = process_calib('kneeflex1','AD052611',[3 4 7 8 9],'L',wfilter1);
  calibp5 = process_calib('hipext3','AD052611',[7 9 11],'L',wfilter1); 
  calibp6 = process_calib('hipflx6','AD052611',[6 12],'L',wfilter1);
  calib.EMGleg  = 'L';
  wdat.EMGleg   = 'L';
elseif strcmp(subject,'EM072611') == 1  
  calibp1 = process_calib('plantar2','EM072611',[1 2 3],'R',wfilter1);
  calibp2 = process_calib('dorsi1','EM072611',[4],'R',wfilter1);
  calibp3 = process_calib('knee_ext4','EM072611',[5 6 14],'R',wfilter1); 
  calibp4 = process_calib('knee_flx2','EM072611',[2 3 7 8 9],'R',wfilter1);
  calibp5 = process_calib('hip_ext1','EM072611',[7 8 12 13],'R',wfilter1); 
  calibp6 = process_calib('hip_flx3','EM072611',[11 14],'R',wfilter1);
  calib.EMGleg = 'R';
  wdat.EMGleg  = 'R';
elseif strcmp(subject,'DS100611') == 1  
  calibp1 = process_calib('plantar09','DS100611',[2 3 4],'R',wfilter1);
  calibp2 = process_calib('dorsi02','DS100611',[1],'R',wfilter1);
  calibp3 = process_calib('knee_ext01','DS100611',[5 6 7],'R',wfilter1); 
  calibp4 = process_calib('knee_flex02','DS100611',[3 4 8 9 11],'R',wfilter1);
  calibp5 = process_calib('hip_ext02','DS100611',[9 11 13],'R',wfilter1); 
  calibp6 = process_calib('hip_flex02','DS100611',[7 12],'R',wfilter1);
  calib.EMGleg = 'R';
  wdat.EMGleg  = 'R';  
else
  fprintf('Unknown subject.\n');
  return;
end;

calib.subject = subject;
wdat.subject  = subject; 

a1 = who('calibp*');

for i=1:length(a1)
  eval(['fnames=fieldnames(calibp' num2str(i) ');']);
  for j = 1:length(fnames)
    if isfield(calib,fnames(j))
      eval(['[calib.' char(fnames(j)) '.FMCval,ind]=max([calib.' char(fnames(j)) '.FMCval calibp' ...
             num2str(i) '.' char(fnames(j)) '.FMCval]);']);
      if ind==2
        eval(['calib.' char(fnames(j)) '.FMCind=calibp' num2str(i) '.' char(fnames(j)) '.FMCind;']);
        if wfilter1==1 
          eval(['wdat.' char(fnames(j)) '.ONsig=calibp' num2str(i) '.' char(fnames(j)) '.ONsig;']);
          eval(['wdat.' char(fnames(j)) '.OFFsig=calibp' num2str(i) '.' char(fnames(j)) '.OFFsig;']);
        end;  
      end;  
    else
      eval(['calib.' char(fnames(j)) '.FMCval=calibp' num2str(i) '.' char(fnames(j)) '.FMCval;']);  
      eval(['calib.' char(fnames(j)) '.FMCind=calibp' num2str(i) '.' char(fnames(j)) '.FMCind;']);
      if wfilter1==1 
        eval(['wdat.' char(fnames(j)) '.ONsig=calibp' num2str(i) '.' char(fnames(j)) '.ONsig;']);
        eval(['wdat.' char(fnames(j)) '.OFFsig=calibp' num2str(i) '.' char(fnames(j)) '.OFFsig;']);
      end;  
    end;
  end;  
end;

calib
if exist('wfilter')==1 && wfilter==1
  wdat
end;

if exist('save_it')==1 && save_it==1
  path_out1 = ['~/Documents/MATLAB/data/' subject '/processed/EMG'];
  if exist(path_out1) == 0;
    eval(['mkdir ' path_out1]);
  end;
  outfile = [path_out1 '/' subject '_' 'calib_new.mat']; 
  if exist('wfilter')==1 && wfilter==1
    save(outfile, 'calib','wdat');
  else
    save(outfile, 'calib');    
  end;
end;
