% Autotagger.m-	Writes a V3D script to automatically tag CDA files according to description and notes fields from Workstation.

% After this thing is thoroughly debugged, compile it into an exe so it can be called by V3D.

cd(folder);	
delete AUTOTAG.v3s;		% Delete old file to not append new commands
fid = fopen('AUTOTAG.v3s','wt');	% Initialize the text file to use as a V3D script
% Reset pipeline parameter
fprintf(fid,'Set_Pipeline_Parameter_To_Folder_Path\n/PARAMETER_NAME=FOLDER\n/PARAMETER_VALUE=\n! /PARAMETER_VALUE_SEARCH_FOR=\n! /PARAMETER_VALUE_REPLACE_WITH=\n! /PARAMETER_VALUE_APPEND=\n;\n');

%% STATIC TRIALS
filenames = ls('Static*.Trial*.enf');
for i = 1:size(filenames,1),
	%% Find DESCRIPTION field in ENF file
	X = textscan(fopen(filenames(i,:)),'%s');
	temp = char(X{1});	% Convert this cell into a text string
	for j = 1:size(temp,1),
		if isequal(temp(j,1:12),'DESCRIPTION='),
			index = j;
		end
	end
	temp = temp(index,:);	%fclose all;
	%% Add tag commands to script text file from DESCRIPTION field
	if isequal(temp(13:15),'EMG'),		% EMG = Static trial lying down to get resting EMG values
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Static%s.c3d\n/TAGS=EMG_resting;\n',filenames(i,7:8));
	elseif isequal(temp(13:16),'Good'),	% Good = The best standing up static trial with arms crossed and wand visible
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Static%s.c3d\n/TAGS=Standing;\n',filenames(i,7:8));
	end
end

%% GAIT TRIALS
filenames = ls('Gait*.Trial*.enf');
for i = 1:size(filenames,1),
	%% Find DESCRIPTION and NOTES fields in ENF file
	X = textscan(fopen(filenames(i,:)),'%s');
	temp = char(X{1});	% Convert this cell into a text string
	for j = 1:size(temp,1),
		if isequal(temp(j,1:12),'DESCRIPTION='),
			index1 = j;
		end
		if isequal(temp(j,1:6),'NOTES='),
			index2 = j;
		end
	end
	desc = temp(index1,:);	notes = temp(index2,:);	%fclose all;
	%% Add tag commands to script text file from DESCRIPTION field
	% N =  No obstacles		V = Visible obstacles		H = Hidden obstacles
	if isequal(desc(14),'S'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Gait%s.c3d\n/TAGS=G_sp-S;\n',filenames(i,5:6));
	elseif isequal(desc(14),'P'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Gait%s.c3d\n/TAGS=G_sp-P;\n',filenames(i,5:6));
	elseif isequal(desc(14),'F'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Gait%s.c3d\n/TAGS=G_sp-F;\n',filenames(i,5:6));
	end
	% S =  Slow speed gait	P = Preferred speed gait	F = Fast speed gait 
	if isequal(desc(13),'N'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Gait%s.c3d\n/TAGS=G_obs-N;\n',filenames(i,5:6));
	elseif isequal(desc(13),'V'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Gait%s.c3d\n/TAGS=G_obs-V;\n',filenames(i,5:6));
	elseif isequal(desc(13),'H'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Gait%s.c3d\n/TAGS=G_obs-H;\n',filenames(i,5:6));
	end
	if isequal(desc(13:19),'MaxJump'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Gait%s.c3d\n/TAGS=MaxJump;\n',filenames(i,5:6));
	end
	%% Add tag commands to script text file for any falls from the NOTES section
	if sum(ismember(notes,'fall'))==4 || sum(ismember(notes,'Fall'))==4 || sum(ismember(notes,'FALL'))==4,
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=G_FALL;\n',filenames(i,9:10));
	end
end

%% STEPPING TRIALS
filenames = ls('Stepping*.Trial*.enf');
for i = 1:size(filenames,1),
	%% Find DESCRIPTION and NOTES fields in ENF file
	X = textscan(fopen(filenames(i,:)),'%s');
	temp = char(X{1});	% Convert this cell into a text string
	for j = 1:size(temp,1),
		if isequal(temp(j,1:12),'DESCRIPTION='),
			index1 = j;
		end
		if isequal(temp(j,1:6),'NOTES='),
			index2 = j;
		end
	end
	desc = temp(index1,:);	notes = temp(index2,:);	%fclose all;
	%% Add tag commands to script text file from DESCRIPTION field
	% R = Right step	L = Left Step	
	if max(ismember(temp(15:16),'R')),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_R;\n',filenames(i,9:10));
	elseif max(ismember(temp(15:16),'L')),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_L;\n',filenames(i,9:10));
	end
	% 20% = Step length of 20% height	30% = Step lenght of 30% height, etc.	Max = Maximum step length
	if isequal(desc(13:14),'20'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_20%%;\n',filenames(i,9:10));
	elseif isequal(desc(13:14),'30'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_30%%;\n',filenames(i,9:10));
	elseif isequal(desc(13:14),'40'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_40%%;\n',filenames(i,9:10));
	elseif isequal(desc(13:14),'50'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_50%%;\n',filenames(i,9:10));
	elseif isequal(desc(13:14),'60'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_60%%;\n',filenames(i,9:10));
	elseif isequal(desc(13:14),'70'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_70%%;\n',filenames(i,9:10));
	elseif isequal(desc(13:14),'80'),
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_80%%;\n',filenames(i,9:10));
	end
	%% Add tag commands to script text file for max step length and errors from NOTES field
	if sum(ismember(notes,'Max'))==3 || sum(ismember(notes,'MAX'))==3,
		fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_MAX;\n',filenames(i,9:10));
	end
	if ~(sum(ismember(notes,'Error'))==5 || sum(ismember(notes,'ERROR'))==5 || ...
		 sum(ismember(notes,'bad'))==3   || sum(ismember(notes,'Bad'))==3   || sum(ismember(notes,'BAD'))==3 || ...
		 sum(ismember(notes,'short'))==5 || sum(ismember(notes,'Short'))==5 || sum(ismember(notes,'SHORT'))==5),
			fprintf(fid,'\nAssign_Tags_To_Files\n/MOTION_FILE_NAMES=::FOLDER&Stepping%s.c3d\n/TAGS=S_OK;\n',filenames(i,9:10));
		% Could have labeled this "S_ERROR", but I will be including good rather than excluding bad, so therefore flipped around.
	end
end

%% Close access to the text file & copy it to the script folder
fclose(fid);	copyfile('AUTOTAG.v3s','C:\Program Files\Visual3D v4\Scripts','f');