%	to open Tracerdac gsr file and plot
%	
%	rev 0 	7/7/08		new style format, from readgsr.m

%  open the input file

fname = input('gimme file =', 's');
fid = fopen(fname, 'r');

% read header

C = textscan(fid,'%s%f',6,'delimiter', ':');
line = fgetl(fid);
line = fgetl(fid);
hdr = cell2mat(C(2));
DT = hdr(3);		% sampling interval, sec

% read data

C1 = textscan(fid,'%f%s%f%*s%*s',1,'delimiter', ',');	% first is special
line = fgetl(fid);
t1 = C1{1}*DT;
gsr1 = C1{3};
datime = char(C1{2});

C = textscan(fid,'%f%*s%f','delimiter', ',');	% all the rest
fclose(fid);
t = C{1}*DT;
gsr = C{2};
t = [t1 t']';
gsr = [gsr1 gsr']';

%  make a little plot

plot(t, gsr, 'r');
grid;
ylabel('GSR, uSiemens');
xlabel('time, s');
title(sprintf('file: %s       %s', fname, datime));

% save 'em

Tsamp = input('gimme output sampling rate, sec [cr for none] = ');
if(isempty(Tsamp)) break; end
fnout = [fname '.out'];
fido = fopen(fnout, 'w');
skip = fix(Tsamp/DT);
fprintf(fido, '%f\n', gsr(1:skip:end));
fclose(fido); 
fprintf('wrote file  %s\n', fnout);

