% Assumes feature_script has been run on all the images to be
% processed, so that the
% /elib/data/images/blobs/1000/1001/1001_bd.mat files
% exist, as do /elib/data/images/blobs/1000/1001/1001_
% blobworld.jpeg, support.map, blob1.jpeg, blob1ring.pbm, etc.
% 
% Loops through the CDLIST file to write the following files in
% /elib/data/images/blobs/1000/1001/:
% 1001_data_small.txt
% 1001_data_whole_small.txt
%
% Creates the directory /elib/data/images/blobs/1000/1001/blobpairs
%
%
% Creates the files
% /elib/data/images/blobs/data_small.txt
% /elib/data/images/blobs/data_whole_small.txt
% /elib/data/blobs/data_full.txt
% /elib/data/blobs/data_whole_full.txt
% to contain data for all the images

params

load necessary218_full_U.mat

fidlist=fopen(CDLIST,'r');
fid_all_full=fopen([DATAPATH 'data35000_full.txt'],'w');
fid_all_small=fopen([DATAPATH 'data35000_small.txt'],'w');
fid_all_whole_full=fopen([DATAPATH 'data35000_whole_full.txt'],'w');
fid_all_whole_small=fopen([DATAPATH 'data35000_whole_small.txt'],'w');

n=0;

while 1
   if feof(fidlist), break, end
   cdname=fgetl(fidlist);    % this is, e.g., 1000
   if isempty(cdname), break, end
   fprintf(1,'%s\n',cdname);

   for i=0:99
      %%if n>4,break,end
      n=n+1;
      imgname=int2str(str2num(cdname)+i);
      dirpath=[DATAPATH cdname '/' imgname '/'];
      imgpath=[DATAPATH cdname '/' imgname '/' imgname];
   
      if ~exist(dirpath,'dir')
	 unix(['mkdir ' dirpath]);
      end
      unix(['chmod 775 ' dirpath]);
      unix(['chmod 664 ' dirpath '*']);

      if ~exist([dirpath 'blobpairs'],'dir')
	 unix(['mkdir ' dirpath 'blobpairs']);
	 unix(['chmod 777 ' dirpath 'blobpairs']);
      else
	 unix(['chmod 777 ' dirpath 'blobpairs']);
	 %%unix(['""rm ' dirpath 'blobpairs/*']);
      end
      
      eval(['load ' imgpath '_bd.mat']);
      
      fid_img_small=fopen([imgpath '_data218_small.txt'],'w');
      fid_img_whole_small=fopen([imgpath '_data218_whole_small.txt'],'w');
   
      fprintf(fid_all_full,'%s\n',imgname);
      fprintf(fid_all_small,'%s\n',imgname);      
      fprintf(fid_all_whole_full,'%s\n',imgname);
      fprintf(fid_all_whole_small,'%s\n',imgname);

      % write the color histogram for the whole image to all files
      % (for WICH querying and background querying)
      % 1. write sparse histogram
      ch=whole_color_hist;
      num_bins=sum(ch>.001);  % see check_color_dist for details
      fprintf(fid_all_whole_full, '%i ', num_bins);
      fprintf(fid_all_whole_small, '%i ', num_bins);
      fprintf(fid_img_whole_small, '%i ', num_bins);
      fprintf(fid_all_full,  '%i ', num_bins);
      fprintf(fid_all_small, '%i ', num_bins);
      fprintf(fid_img_small, '%i ', num_bins);
      count=0;
      for bin=1:length(ch)     % size of histogram
	 if ch(bin)>.001
	    count=count+1;
	    if count/4~=floor(count/4)
	       string=sprintf('\t%i %.3f', bin, ch(bin));
	    else
	       string=sprintf('\n\t%i %.3f', bin, ch(bin));
	    end
	    fprintf(fid_all_whole_full, '%s', string);
	    fprintf(fid_all_whole_small, '%s', string);
	    fprintf(fid_img_whole_small, '%s', string);
	    fprintf(fid_all_full,  '%s', string);
	    fprintf(fid_all_small, '%s', string);
	    fprintf(fid_img_small, '%s', string);
	 end
      end
      if count~=num_bins
	 disp('count~=num_bins')
	 keyboard
      end
      fprintf(fid_all_whole_full, '\n');
      fprintf(fid_all_whole_small, '\n');
      fprintf(fid_img_whole_small, '\n');
      fprintf(fid_all_full, '\n');
      fprintf(fid_all_small, '\n');
      fprintf(fid_img_small, '\n');
      
      % write entire (transformed) color histogram (for full)
      %%%x = ch(1:end-1)';
      %%%fprintf(fid_all_full, '%.4f\t', Up*x);
      x = Up*ch(1:end-1)';
      fprintf(fid_all_full, '%.4f\t', x(1:50));
      fprintf(fid_all_full, '\n');
      
      % 2. write entire (transformed) color histogram to whole-image files
      %%%x = ch(1:end-1)';
      %%%fprintf(fid_all_whole_full, '%.4f\t', Up*x);
      fprintf(fid_all_whole_full, '%.4f\t', x(1:50));
      fprintf(fid_all_whole_full, '\n');
      
      % write number of blobs in image
      fprintf(fid_all_full,'%i\n',length(bd));      
      fprintf(fid_all_small,'%i\n',length(bd));
      fprintf(fid_img_small,'%i\n',length(bd));

      for m=1:length(bd)
	 blob=bd(m);
      
	 % write size (pixels), texture and location values
	 pixels=blob.pixels/(Xdim*Ydim);
	 true_texture=[blob.con blob.aniso];
	 %% for 35,000 run, was true_texture=[blob.con blob.con*blob.aniso];
	 %% (double multiplying!)
	 x=blob.x/Xdim;
	 y=blob.y/Ydim;
	 fprintf(fid_all_full, '%.4f\n%.4f %.4f\n%.4f %.4f\n', ...
	     pixels, true_texture(1), true_texture(2), x, y);
	 fprintf(fid_all_small, '%.4f\n%.4f %.4f\n%.4f %.4f\n', ...
	     pixels, true_texture(1), true_texture(2), x, y);
	 fprintf(fid_img_small, '%.4f\n%.4f %.4f\n%.4f %.4f\n', ...
	     pixels, true_texture(1), true_texture(2), x, y);
	 
	 % write sparse histogram
	 ch=blob.color_hist;
	 num_bins=sum(ch>.002);  % see check_color_dist for details
	 fprintf(fid_all_full, '%i ', num_bins);
	 fprintf(fid_all_small, '%i ', num_bins);
	 fprintf(fid_img_small, '%i ', num_bins);
	 count=0;
	 for bin=1:length(ch)     % size of histogram
	    if ch(bin)>.002
	       count=count+1;
	       if count/4~=floor(count/4)
		  string=sprintf('\t%i %.3f', bin, ch(bin));
	       else
		  string=sprintf('\n\t%i %.3f', bin, ch(bin));
	       end
	       fprintf(fid_all_full, '%s', string);
	       fprintf(fid_all_small, '%s', string);
	       fprintf(fid_img_small, '%s', string);
	    end
	 end
	 if count~=num_bins
	    disp('count~=num_bins')
	    keyboard
	 end
	 fprintf(fid_all_full, '\n');
	 fprintf(fid_all_small, '\n');
	 fprintf(fid_img_small, '\n');
      
	 % write shape values (just top 10 coefficients; 20 are saved)
	 shape=blob.shape_oriented(:);
	 for sh=1:10
	    fprintf(fid_all_full, '%.4f ', shape((sh-1)*4+1:sh*4));
	    fprintf(fid_all_small, '%.4f ', shape((sh-1)*4+1:sh*4));
	    fprintf(fid_img_small, '%.4f ', shape((sh-1)*4+1:sh*4));
	    fprintf(fid_all_full, '\n');
	    fprintf(fid_all_small, '\n');
	    fprintf(fid_img_small, '\n');
	 end
      
	 % (for full) write texture, location, shape bins
	 fprintf(fid_all_full,'%i %i %i %i\n', ...
	     blob.texbin, blob.locbin, blob.ABbin, blob.Bbin);
      
	 % write entire (transformed) color histogram (for full)
	 %%%x = ch(1:end-1)';
	 %%%fprintf(fid_all_full, '%.4f\t', Up*x);
	 x = Up*ch(1:end-1)';
	 fprintf(fid_all_full, '%.4f\t', x(1:50));
	 fprintf(fid_all_full, '\n');
	 
	 fprintf(fid_all_full, '\n');
	 fprintf(fid_all_small, '\n');
	 fprintf(fid_img_small, '\n');
      end
      fclose(fid_img_small);
      unix(['chmod 664 ' imgpath '_data218_small.txt']);
      
      % write the texture histograms for the whole image to fid_whole
      fprintf(fid_all_whole_full, '%.4f ', whole_con_hist);
      fprintf(fid_all_whole_full, '\n');
      fprintf(fid_all_whole_full, '%.4f ', whole_aniso_hist);
      fprintf(fid_all_whole_full, '\n');
      
      fprintf(fid_all_whole_small, '%.4f ', whole_con_hist);
      fprintf(fid_all_whole_small, '\n');
      fprintf(fid_all_whole_small, '%.4f ', whole_aniso_hist);
      fprintf(fid_all_whole_small, '\n');
      
      fprintf(fid_img_whole_small, '%.4f ', whole_con_hist);
      fprintf(fid_img_whole_small, '\n');
      fprintf(fid_img_whole_small, '%.4f ', whole_aniso_hist);
      fprintf(fid_img_whole_small, '\n');
      fclose(fid_img_whole_small);
      unix(['chmod 664 ' imgpath '_data218_whole_small.txt']);
      
      fprintf(fid_all_full, '\n\n');
      fprintf(fid_all_small, '\n\n');
      fprintf(fid_all_whole_full, '\n\n');
      fprintf(fid_all_whole_small, '\n\n');
   end
end

fclose(fid_all_full);
fclose(fid_all_small);
fclose(fid_all_whole_full);
fclose(fid_all_whole_small);
fclose(fidlist);
disp([int2str(n) ' images.'])
