% UNPACK  Part of the DODS data browser (browse.m)
%
% UNPACK  unpacks the returned arrays in the main workspace
%         and puts them into recognizable and distinct variable
%         names based on the information in the archive.m
%
%
%            Deirdre Byrne, The Island Institute, 7 April 1997
%                 dbyrne@islandinstitute.org
%

% The preceding empty line is important.
%
% $Id: unpack.m,v 1.7 1998/11/09 20:19:23 dbyrne Exp $

% $Log: unpack.m,v $
% Revision 1.7  1998/11/09 20:19:23  dbyrne
% *** empty log message ***
%
% Revision 1.15  1998/10/12 21:10:27  root
% Modified to delete any variables which are empty in every URL in the request.
%
% Revision 1.14  1998/10/12 14:50:22  root
% checking for empty arguments!
%
% Revision 1.13  1998/09/13 14:59:24  root
% Fixed 'clear all Rxx_' once again.
%
% Revision 1.12  1998/09/12 15:28:41  root
% Made modifications necessary to let display menu be called numerous times.
%
% Revision 1.11  1998/09/12 11:20:17  root
% changing display menu
%
% Revision 1.10  1998/09/12 10:38:03  root
% Testing changes to input arguments for dispmenu.m
%
% Revision 1.9  1998/09/08 21:29:59  dbyrne
% Modified to unpack URLs as well as data.
%
% Revision 1.8  1998/09/08 16:04:20  dbyrne
% browse_get_variables and browse_display_choices too long for v4. Changed
% them to browse_getvariables and browse_dispchoices
%
% Revision 1.7  1998/09/04 10:02:12  dbyrne
% fixed up error messages.
%
% Revision 1.6  1998/09/04 08:41:03  dbyrne
% Fixed bug in 'addclear' call.
%
% Revision 1.5  1998/09/04 08:27:47  dbyrne
% Preparing to fix a bug in browse_data_list.
%
% Revision 1.4  1998/09/03 20:32:41  dbyrne
% CLeaning up multivariable plot items.
%
% Revision 1.3  1998/09/03 19:02:28  dbyrne
% *** empty log message ***
%
% Revision 1.2  1998/09/01 07:15:47  dbyrne
% beginning changes for output of multivariables
%
% Revision 1.1  1998/05/17 14:10:53  dbyrne
% *** empty log message ***
%
% Revision 1.1.1.1  1997/09/22 14:13:54  tom
% Imported Matlab GUI sources to CVS
%
if exist('browse_count') == 1
  if all(size(browse_count) == [1 1]) & size(browse_sizes,2) == 3
    % the following criterion for browse_inx assumes the first 
    % argument on the selectable list is always returned!  
    % Not a good idea .....
    browse_inx = find(browse_sizes(:,3) == 1);
    browse_inx = [browse_inx(:); size(browse_sizes,1)+1];
    browse_num_urls = size(browse_inx,1)-1;
    browse_dispargs = '';
    browse_k = 1;
    for browse_j = 1:browse_num_urls
      browse_data_list = ''; 
      browse_arg1 = browse_inx(browse_j);
      browse_arg2 = browse_inx(browse_j+1)-1;
      browse_num_args = diff([browse_arg1 browse_arg2])+1;
      % do some bookkeeping and then unpack this URL
      if browse_j == 1
	browse_dispargs = browse_names(browse_arg1:browse_arg2,:);
	browse_empty = zeros(size(browse_dispargs,1), 1);
      end
      if browse_j > 1
	% increment the count
	browse_count = browse_count+1;
      end
      % unpack the URL
      browse_string = sprintf('R%i_URL = ''%s'';', browse_count, ...
	  browse_urls(browse_j,:));
      eval(browse_string)
      for browse_i = 1:browse_num_args
	browse_string = sprintf('R%i_%s', ...
	    browse_count, deblank(browse_names(browse_k,:)));
	browse_data_list = [browse_string ' ' browse_data_list];
	browse_string = sprintf('%s = %s;', browse_string, ...
	    [ 'reshape(browse_data(1:browse_sizes(browse_k,1)*', ...
	      'browse_sizes(browse_k,2)),', ...
	      'browse_sizes(browse_k,1),browse_sizes(browse_k,2))']);
	eval(browse_string)
	% clip browse_data so we don't end up using memory
	% for two full copies of the data
	browse_size = browse_sizes(browse_k,1)*browse_sizes(browse_k,2);
	browse_data = browse_data(browse_size+1:length(browse_data));
	% check for empty args
	if browse_j == 1
	  if any(browse_sizes(browse_k,1:2) == 0)
	    browse_empty(browse_i) = 1;
	  end
	else
	  for browse_l = 1:size(browse_dispargs,1)
	    if browse_empty(browse_i) > 0 & ...
		  strcmp(browse_names(browse_k,:), ...
		  browse_dispargs(browse_l,:))
	      browse_empty(browse_i) = browse_empty(browse_i)+1;
	    end
	  end
	end
	% keep track of how many args we've actually unpacked
	browse_k = browse_k + 1;
      end % end of loop through browse_i
      browse_data_list = [browse_data_list ' ' ...
	    sprintf('R%i_URL', browse_count)];
      if ~isempty(browse_data_list)
	browse('addclear', browse_data_list)
      end
    end % end of loop through browse_j
    disp('   ')
    % discard any returned arguments which are empty in every URL
    if all(sum(browse_sizes(:,1:2))) > 0
      disp(sprintf('%s%i%s','This request generated ',  ...
	  browse_num_urls, ' separate URLs,'))
      browse_string = sprintf('R%i_   ', ...
	  browse_count-browse_num_urls+1:browse_count);
      browse_string = ['which are stored in the sets:  ', ...
	    browse_string];
      disp(browse_string)
      disp('   ')
      disp('Each individual argument is stored like so:')
      disp('   ')
      if any(browse_empty == browse_num_urls)
	browse_i = find(browse_empty ~= browse_num_urls);
	browse_j = find(browse_empty == browse_num_urls);
	browse_string = '';
	for browse_l = browse_count-browse_num_urls+1:browse_count
	  for browse_m = 1:length(browse_j)
	    browse_string = [browse_string ...
		  sprintf('R%i_%s ', browse_l, ...
		  browse_dispargs(browse_j,:))];
	  end
	end
	browse_string = ['clear ' browse_string];
	eval(browse_string)
	browse_string = '';
	browse_dispargs = browse_dispargs(browse_i,:);
	for browse_j = 1:size(browse_dispargs,1)
	  browse_string = [browse_string sprintf('R%i_%s ', browse_count, ...
	      deblank(browse_dispargs(browse_j,:)))];
	end
      end
      disp(browse_string)
      disp('   ')

      % now do display
      browse_dispchoices = zeros(12,1);
      set(browse_clear_button,'enable','off')
      browse_count = browse_count - browse_num_urls+1;
      dispmenu('display', browse_dispargs, ...
	  browse_getvariables, browse_num_urls);
    else % sum(browse_sizes) == 0
      disp('This request generated no data')
      disp('   ')
    end 
  else % browse_count is empty
    disp('This request generated no URLs')
    disp('   ')
  end
else % browse_count does not exist -- this happens whenever 
  % the estimated datasize is greated than the check level,
  % so we don't want to alarm the user with a false error 
  % message here;
%  disp('This request generated no URLs')
%  disp('   ')
end
