function pick_from_state(state,delta_t,output_filename)
% PICK_FROM_STATE From a `state' structure generated by guitool, do picking and produce a SPIDER file.
%
% pick_from_state(state,delta_t,output_filename)
%
% STATE is the object generated by guitool.
% DELTA_T is the picking interval
% OUTPUT_FILENAME is the name of the document file to write; if this argument is omitted, stdout will be used.

particle_no = 0;

fd = 1;

if (nargin == 3),
  [fd, message] = fopen(output_filename,'w');
  if ~isempty(message),
        
  end
end

the_time = clock;
fprintf(fd,' ; foo/bar %s AT %02d:%02d:%02d   %s\n',date,the_time(4),the_time(5),round(the_time(6)),'/dev/null'); 

fprintf(fd,' ; The files are:\n');
for i=1:length(state.files),
  fprintf(fd,' ;   %d %s\n',i,state.files(i).filename);
end

fprintf(fd,[' ; The "registers" are the file number, the group number,' ...
	    ' and then the Y and X coordinates and the orientation angle\n']);

for i=1:length(state.files),
 % fprintf(fd,' ;  Processing file "%s"\n', state.files(i).filename );
    for j=1:length(state.files(i).groups),
  %     fprintf(fd,' ;  Processing group %d.\n', state.files(i).groups(j).ID);
 
       [X,Y,A] = filament_follow(state.files(i).groups(j).XData, ...
				 state.files(i).groups(j).YData, ...
				 state.files(i).groups(j).TData, ...
				 delta_t);
       
 
       for k=1:length(X),
	 particle_no = particle_no + 1;
	 docfilewrite(fd,particle_no,[i state.files(i).groups(j).ID ...
		    X(k) Y(k) A(k)]);
       end
    end
end

function docfilewrite(fd,key,parameters)
% write using something like FORTRAN format "(I6,I1,9G12.3)"
fprintf(fd,'%5d %1d',key,length(parameters));
fprintf(fd,'%12.3f',parameters);
fprintf(fd,'\n');

