function gattplot(arg1)
%    plots statistics from attitude data
%
% usage:
% attplot(arg1)
%
% where:  arg1 is a structure with the following fields (example)
%
%
%    arg1.attfile='ub309319';        %one ubprint .mat file
%    arg1.year_string='1996';        %string for year_base (4 digits)
%    arg1.title='np9611';            %title for figure
%    arg1.start_dd=[315:3:340];      %decimal days to start on
%  %optional:                         
%    arg1.days_per_page = 3;         % days of data plotted on each page
%                                    % defaults to 3
%    arg1.pages = [1:2];             %defaults to [1:3]
%                                    %  which plot pages are to be produced: 
%                                    %  1 is the primary page, with dh_mean, 
%                                    %     dh_std, p_std, r_std, and n_used; 
%                                    %  2 is secondary,  with min, max, and 
%                                    %  mean of pitch and roll.
%                                    %  3 is yet another page, this time  with
%                                    %     mrms, mrbrms, mnn, mrms, n brms, 
%                                    %     outlier, reacq, n att.
%    arg1.print_flag =   2;          % defaults to 2 (append to file)
%                                    % 0: do not print
%                                    % 1: print to default printer
%                                    % 2 print postscript files to disk 
%                                    %      (filename is from arg1.attfile:
%                                    %      eg: ademo.ps)
%    arg1.newfigwins = 0;            % defaults to 0 (recycle fig windows)
%                                    % 0: use the same figures over and over
%                                    %     figs numbered as arg1.pages
%                                    % 1: create a new figure each time
% gattplot(arg1)



%  2000/02/13 Jules - taking out backward compatibility.  Sorry, it
%  won't work in matlab4 anymore.  All you have to do to fix that is
%  take the variables from the next section (print_flag, days_per_page, pages, 
%  cruise_title, t0_array, attfile, year_string) and set them manually.
%  Use  demo/nav/attplot.m as a guide.
%
% NOTE: any changes you make to the structure (arg1) used to call this 
%     program will affect the following three programs: (as of 2/2000)
% programs/matlab/utils/gplotadcp_tool.m
% programs/codas3/adcp/demo/plotall.m
% programs/codas3/adcp/demo/nav/attplot.m
%
% 2000/04/12 - fix up the print_flag, psfile, and newfigwins options


%for historical reasons, these have defaults.
default_printflag = 0;
default_newfigwins = 0;
default_days_per_page = 3;
default_pages = [1:3];


%set values from argument
if (isstruct(arg1))

 if (~isfield(arg1,'print_flag'))
        print_flag = default_printflag;
  else
        print_flag = arg1.print_flag;
  end
  
  if (~isfield(arg1,'days_per_page'))
        days_per_page = default_days_per_page;
  else
        days_per_page = arg1.days_per_page;
  end
  
  if (~isfield(arg1,'pages'))
        pages = default_pages;
  else
        pages = arg1.pages;
  end

  
  if (~isfield(arg1,'newfigwins'))
	newfigwins = default_newfigwins;
  else
	newfigwins = arg1.newfigwins;
  end

  
  if (print_flag == 2)
	if (isfield(arg1,'psfile'))
	  psfile = sprintf('%s.ps',arg1.attfile);
	else
	  psfile = arg1.psfile;
	end
  else
	fprintf('not printing to disk or printer')
  end

 
  cruise_title = arg1.title;
  t0_array=arg1.start_dd;
  attfile=arg1.attfile;
  year_string = arg1.year_string;
else
  help gattplot
  sprintf('this must have a structure as its argument.  ')
  sprintf('see demo/nav/attplot.m for an example of usage. ')
  sprintf('look at the top of this file (gattplot) to see how to')
  sprintf('    make it matlab4 compatible')
  error('argument is not a structure')
  	
end

%%---------------end of things to edit---------------------%%

if (print_flag == 2)
  if ( ~exist(psfile,'file') )
	fprintf('creating %s\n',psfile)
  else
	fprintf('appending to %s\n',psfile)
  end
end


%load the att data and set up all the variables
load_att;

for t0 = t0_array
  t1 = t0 + days_per_page;
  time_mask = (att(i_day,:) <= t1 & att(i_day,:) >= t0);

  jall = find(time_mask);
  jgood = find(time_mask & good_mask);
  njall = length(jall);
  njgood = length(jgood);

  if (njall == 0)
	fprintf(FID_LOG,'NO DATA in this segment (dday %2.1f - %2.1f)\n',t0,t1);
  else
	fprintf(FID_LOG,...
		'  t0: %5.1f;  %d points, %d accepted; %5.2f percent rejected\n', ...
		t0, njall, njgood, 100 * (njall - njgood)/njall);
  end

  irows = irows_1;
  for ipage = pages
	eval(['irows = irows_' int2str(ipage)  ';'])

	np = length(irows);                       % number of panels;
	yp = (1 - 2 * ym - (np-1) * ysep) / np;   % panel height
	py0 = ym;


	if (newfigwins == 1)
	  figure
	else
	  figure(ipage)
	  clf
	end

	
	set(gcf,'defaulttextfontsize',12)
	set(gcf,'defaultaxesfontsize',12)
	set(gcf,'defaulttextfontweight','normal')
	set(gcf,'defaultaxesfontweight','normal')

	orient tall
    jjisok = 1;
	for ii = 1:np,
	  %if irows(ii) == i_n_used, jj = jall; else jj = jgood; end
	  
	  if (jjisok)
		if (    (irows(ii) == i_n_used)  | (irows(ii) == i_n_att) ...
			  | (irows(ii) == i_n_reacq) | (irows(ii) == i_n_outlier) ...
			  | (irows(ii) == i_n_brms)  | (irows(ii) == i_n_mrms) ...
			  | (njgood == 0)),
		  jj = jall;
		else
		  jj = jgood;
		end
		
		if (isempty(jj))
		  fprintf('NO DATA in this segment (dday %2.1f - %2.1f)\n',t0,t1)
		  jjisok = 0;
		else
		  y = att(irows(ii),jj);
		  y0 = min(y);
		  y1 = max(y);
		  dy = y1 - y0;
		  if dy == 0, dy = 1; end  % Ensure nonzero y-axis range.
		  
		  ha = axes('position', [px0 py0 xp yp] );
		  hp = plot(att(i_day, jj), y, '.');
		  %axauto = axis;
		  %axis([t0 t1 axauto(3) axauto(4)]);
		  axis([t0 t1 (y0 - 0.1*dy) ( y1 + 0.1*dy)]);
		  ylabel(labels(irows(ii),:));
		  grid
		  if (ii > 1),
			set(gca, xtl_str, []);
		  else
			xlabel([ year_string ' Decimal Days'])
		  end
		  py0 = py0 + yp + ysep;
		  
		end
	  end %end of test jj empty
	end   %end of loop through plots
	
	if (jjisok)
	  text(t1, (y1+0.1*dy), cruise_title, ...
		  'horizontalalignment', 'right', 'verticalalignment', 'bottom')
		  
	  title('Ashtech Attitude Statistics')
	  date_it('attplot.m');
	  if print_flag == 1,
		print
	  end
	  if (print_flag == 2)
		eval(['print -append ' psfile]);
		fprintf('\nappending figure %d to %s\n',ipage,psfile)
	  end
	end
	
	
  end     % of loop through pages
end       % of loop through t0_array


fclose(FID_LOG);
