%do_rigidbodyana
%script to perform rigid body analysis of AG500 data, plus various
%book-keeping tasks.
%The script consists of 3 branches, activated by setting the flags doaddv,
%dorig, and domakecut to 1
%doaddv:    This branch should be run first, and then deactivated. It adds
%           various documentation to the position files used as input to the rigid
%           body analysis.
%dorig:     The main branch. May need running more than once to experiment with
%           different settings for the reference sensors.
%domakecut: Should be run last. It does not need running until the
%           definitive version of dorig has been carried out. The domakecut branch just
%           generates a segment file for use with the mtnew display facility


clear variables

doaddv=0; domakecut=1; dorig=0;

%If audio data has already been processed, then subjectname should match
%the first part of the file names of the mat files with the audio data,
%and outpath should match the location
subjectname='camexp_S1';

%inpath is relevant for the doaddv and dorig branches; outpath only for the
%dorig branch
inpath=['ampsfiltadj1' pathchar 'velrep' pathchar 'rawpos' pathchar];
outpath=['finaldata' pathchar 'mat'];
mkdir(outpath);		%may already exist, if audio data already present
outpath=[outpath pathchar  subjectname '_ema_head_']; %add base part of filename for ema data

%only relevant for the domakecut branch
%defines the name of a basic label file
cutfile=['finaldata' pathchar subjectname '_cut'];


%only relevant for the doaddv branch
%defines the name of a text file with general documentation of the
%experiment. The text is added to the comment variable of every position
%file that will be processed by rigidbodyana. This information is then copied to the output
%files
commentfile=[subjectname '_comment.txt'];

%only relevant for the dorig branch
%name of the reference object
refname=['palocc' pathchar 'ampsfiltds' pathchar 'beststartl' pathchar 'rawpos' pathchar '0481_refobj'];

%only relevant for the dorig branch
%specify the reference sensors to use. always include the corresponding
%virtual sensor
refsensors=str2mat('ref','v_ref','nose','v_nose','head_left','v_head_left','head_right','v_head_right');

%triallist is relevant for all branches
triallist=370:460;

%relevant for the doaddv and domakecut branches
%this refers to the log file generated by the prompt program (and further
%processed by adjlogfilev)
%If available, use it to get a list of labels for each trial into cutlabel.
logfile=['Prompts_1_ran_log_jb1_all_adj'];		%specify without the .txt extension 
[cutdata,cutlabel,agtrialnum,mttrialnum,trialnumS]=parselogfile(logfile);


%cutlabel is only used in the doaddv branch
%If cutlabel is not available from the prompt log file it must be set up
%here by hand to match the triallist (e.g read from an external text file
%with cutlabel=file2str('mylabels.txt');
cutlabel=cutlabel(triallist,:);


%addvariable2mat, addcommentfromfile and makecutfile do not automatically
%assume 4-digit trial numbers
leadingzeros=length(int2str(max(triallist)));
leadingzeros=4-leadingzeros;
leadingzeros=repmat('0',[1 leadingzeros]);
inpathz=[inpath leadingzeros];

if doaddv
    %add item_id and comment to input data
    if length(triallist)~=size(cutlabel,1)
        disp('check length of cutlabel or triallist');
        keyboard;
        return;
    end;
    addvariable2mat(inpathz,'item_id',cutlabel,triallist);
    addcommentfromfile(inpathz,commentfile,triallist);
end;


if dorig
%both the 4th and 5th input arguments should normally be set to the name of
%the reference object. see rigidbodyana help for further details.
    rigidbodyname='headrig';
    rigidbodyana(inpath,outpath,triallist,refname,refname,refsensors,rigidbodyname);
end;

if domakecut
    %makecutfile can only be done after rigidbodyana has been done
    % (or base it on input files if no changes in trial numbers are to be made)
    makecutfile([outpath leadingzeros],cutfile,triallist)
%add long version of trial labels to cut file. Only relevant if log file
%from prompt program is available
	if exist('trialnumS','var')
	valuelabel=mymatin(cutfile,'valuelabel');
    valuelabel.trial_number=trialnumS;
    addvariable2mat(cutfile,'valuelabel',valuelabel);
	end;
end;

