%%BeginResource: procset LoutGraphPrependGraphic
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                    %
%  PostScript @SysPrependGraphic file for @Graph (Version 1.0)       %
%                                                                    %
%  Version 1.0 by Jeffrey H. Kingston, December 1993.                %
%  swapxandy added September 2001 by JHK.                            %
%                                                                    %
%  This file has been placed in the public domain by its author,     %
%  Jeffrey H. Kingston                                               %
%                                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

errordict begin
   /handleerror
   {
      {  /Times-Roman findfont 8 pt scalefont setfont
	 0 setgray 4 pt 4 pt moveto
	 $error /errorname get
	 dup lgraphdict exch known
	 { lgraphdict exch get }
	 { 30 string cvs } ifelse
	 show
	 (  Command: ) show
	 $error /command get 30 string cvs show
      } stopped {} if
      showpage stop
   } def
end

/lgraphdebugposy 432 def
/lgraphdebugposx 72 def

% - lgraphdebugnextline -
/lgraphdebugnextline
{
  lgraphdebugposy 72 lt
  { /lgraphdebugposx lgraphdebugposx 144 add store
    /lgraphdebugposy 432 store
  }
  {
    /lgraphdebugposy lgraphdebugposy 12 sub store
  } ifelse
  lgraphdebugposx lgraphdebugposy moveto
} def

% - lgraphdebugbeginindent -
/lgraphdebugbeginindent
{
  /lgraphdebugposx lgraphdebugposx 12 add store
} def

% - lgraphdebugendindent -
/lgraphdebugendindent
{
  /lgraphdebugposx lgraphdebugposx 12 sub store
} def

% <string> <int> lgraphdebugprint -
% must be defined outside lgraphdict since used in arbitrary places
% print <string> plus count or <int> stack entries, whichever is the smaller
/lgraphdebugprint
{
  exch
  gsave
  initgraphics
  lgraphdebugnextline
  /Times-Roman findfont 10 scalefont setfont
  0 setgray show
  lgraphdebugbeginindent
  count 1 sub 2 copy lt { pop } { exch pop } ifelse 1 sub
  0 exch 1 exch
  {
    lgraphdebugnextline
    index
    dup type (dicttype) eq
    {
      (begin dict) show
      lgraphdebugbeginindent
      {
	lgraphdebugnextline
	pop 100 string cvs show
      } forall
      lgraphdebugendindent
      lgraphdebugnextline
      (end dict) show
    }
    {
      dup type (arraytype) eq
      {
        (begin array) show
        lgraphdebugbeginindent
	{
	  lgraphdebugnextline
	  100 string cvs show
	} forall
	lgraphdebugendindent
	lgraphdebugnextline
        (end array) show
      }
      {
        100 string cvs show
      } ifelse
    } ifelse
  } for
  lgraphdebugendindent
  grestore
} def


/lgraphdict 200 dict def
lgraphdict begin

% error messages
/dictfull (dictfull error) def
/dictstackoverflow (dictstackoverflow error) def
/execstackoverflow (execstackoverflow error:  expression too complex?) def
/limitcheck (limitcheck error:  graph too complex or too large?) def
/syntaxerror (syntaxerror error:  syntax error in text of graph?) def
/typecheck (typecheck error:  syntax error in text of graph?) def
/undefined (undefined error:  unknown or misspelt symbol?) def
/rangecheck (rangecheck error:  undefined expression (e.g. divide by zero)?) def
/VMError (VMError error:  run out of memory?) def

% random number between x and y inclusive: x y dorand num
/dorand { 1 index sub 1 add rand exch mod add } def

% log to given base: base num dolog num
/dolog { ln exch ln div } def

% maximum of two numbers:  <num> <num> max <num>
/max { 2 copy gt { pop } { exch pop } ifelse } def

% add two points:  <point> <point> padd <point>
/padd { exch 3 1 roll add 3 1 roll add exch } def

% subtract first point from second:  <point> <point> psub <point>
/psub { 3 2 roll sub 3 1 roll exch sub exch } def

% distance between two points:  <point> <point> distance <length>
/distance { psub dup mul exch dup mul add sqrt } def

% point at angle and distance:  <point> <length> <angle> atangle <point>
/atangle { 2 copy cos mul 3 1 roll sin mul padd } def

% angle from one point to another:  <point> <point> angle <angle>
/angle { psub 2 copy 0 eq exch 0 eq and {pop} {exch atan} ifelse } def


% set up for line
% - linesetup <length> <dashlength>
/linesetup
{ newpath
  xcurr ycurr trpoint xprev yprev trpoint
  4 copy moveto lineto distance dashlength
} def

% set up for icon-avoiding line
% - ilinesetup <length> <dashlength>
/ilinesetup
{ newpath
  xprev yprev trpoint xcurr ycurr trpoint 4 copy
  4 copy angle symbolsize 1.5 mul exch 4 2 roll pop pop atangle
  6 2 roll 4 2 roll
  4 copy angle symbolsize 1.5 mul exch 4 2 roll pop pop atangle 
  4 copy moveto lineto distance dashlength
} def


% stroke a solid line:  <length> <dashlength> solid -
/solid
{ pop pop [] 0 setdash linewidth setlinewidth stroke
} def
  
% stroke a dashed line:   <length> <dashlength> dashed -
/dashed
{  2 copy 2 mul le 1 index 0 le or
   {  exch pop 1 pt max [ exch dup ] 0 setdash }
   {  dup [ exch 4 2 roll 2 copy div
      1 sub 2 div ceiling dup 4 1 roll
      1 add mul sub exch div ] 0 setdash
   } ifelse linewidth setlinewidth stroke
} def

% stroke a cdashed line:  <length> <dashlength> cdashed -
/cdashed
{  2 copy le 1 index 0 le or
   { exch pop 1 pt max [ exch dup ] dup 0 get 2 div setdash }
   { dup [ 4 2 roll exch 2 copy exch div
     2 div ceiling div 1 index sub
     ] exch 2 div setdash
   } ifelse linewidth setlinewidth stroke
} def

% stroke a dotted line:  <length> <dashlength> dotted -
/dotted
{  2 copy le 1 index 0 le or
   { exch pop 1 pt max [ exch 0 exch ] 0 setdash }
   { 1 index exch div ceiling div 0.99999 mul
     [ 0 3 2 roll ] 0 setdash
   } ifelse gsave 1 setlinecap linewidth setlinewidth stroke grestore newpath
} def

% stroke a noline line:  <length> <dashlength> noline -
/noline
{ pop pop
} def

% scale array elements by factor:  <array> <factor> scalearray <array>
/scalearray
{
  [ exch 3 2 roll
  { exch dup 3 1 roll mul exch } forall
  pop ]
} def

% sum array elements:  <array> sumarray <number>
/sumarray
{
  0 exch { add } forall
} def

% begin a more complex line: <length> <dashlength> linebegin -
/linebegin
{
  % (Entering linebegin) 2 debugprint
  20 dict begin
  /dashlen exch 1 pt max def
  /len exch def
  /gap dashlen def
  /halfgap dashlen 2 div def
  /dash dashlen def
  /halfdash dashlen 2 div def
  /dot 0 def
  % (Leaving linebegin) 0 debugprint
} def
% end a more complex line: <linecap> <cycle> <startpos> <stoppos> lineend -
/lineend
{
  % (Entering lineend) 3 debugprint
  /stoppos exch def
  /startpos exch def
  /cycle exch def
  /linecap exch def
  /stopposlen stoppos sumarray def
  /startposlen startpos sumarray def
  /cyclelen cycle sumarray def
  /effectivelen len startposlen add stopposlen sub def
  effectivelen 0 gt cyclelen 0 gt and
  {
    /repeats effectivelen cyclelen div ceiling def
    /factor len repeats cyclelen mul startposlen sub stopposlen add div def
    cycle factor scalearray startposlen factor mul setdash
    linecap setlinecap stroke
  } if
  end
  % (Leaving lineend) 0 debugprint
} def

% stroke a dotdashed line: <length> <dashlength> dotdashed -
/dotdashed
{
  linebegin
  1 [dash gap dot gap] [] [dash]
  lineend
} def

% stroke a dotcdashed line: <length> <dashlength> dotcdashed -
/dotcdashed
{
  linebegin
  1 [dash gap dot gap] [halfdash] [halfdash]
  lineend
} def

% stroke a dotdotdashed line: <length> <dashlength> dotdotdashed -
/dotdotdashed
{
  linebegin
  1 [dash gap dot gap dot gap] [] [dash]
  lineend
} def

% stroke a dotdotcdashed line: <length> <dashlength> dotdotcdashed -
/dotdotcdashed
{
  linebegin
  1 [dash gap dot gap dot gap] [halfdash] [halfdash]
  lineend
} def

% stroke a dotdotdotdashed line: <length> <dashlength> dotdotdotdashed -
/dotdotdotdashed
{
  linebegin
  1 [dash gap dot gap dot gap dot gap] [] [dash]
  lineend
} def

% stroke a dotdotdotcdashed line: <length> <dashlength> dotdotdotcdashed -
/dotdotdotcdashed
{
  linebegin
  1 [dash gap dot gap dot gap dot gap] [halfdash] [halfdash]
  lineend
} def


% stroke a y histogram: - yhisto -
/yhisto
{ xprev yleft trpoint yextra sub moveto
  xprev yprev trpoint lineto
  xcurr yprev trpoint lineto
  xcurr yleft trpoint yextra sub lineto 
  linewidth setlinewidth stroke
} def

% stroke an x histogram: - xhisto -
/xhisto
{ xleft yprev trpoint exch xextra sub exch moveto
  xcurr yprev trpoint lineto
  xcurr ycurr trpoint lineto
  xleft ycurr trpoint exch xextra sub exch lineto 
  linewidth setlinewidth stroke
} def

% stroke a surface y histogram: - surfaceyhisto -
/surfaceyhisto
{ firstpair
  { xprev yleft trpoint yextra sub moveto
    xprev yprev trpoint lineto
  }
  { xprev yprev trpoint moveto
  } ifelse
  xcurr yprev trpoint lineto
  lastpair
  { xcurr yleft trpoint yextra sub lineto 
  }
  { xcurr ycurr trpoint lineto
  } ifelse
  linewidth setlinewidth stroke
} def

% stroke a surface x histogram: - surfacexhisto -
/surfacexhisto
{ firstpair
  { xleft yprev trpoint exch xextra sub exch moveto
  }
  { xprev yprev trpoint moveto
  } ifelse
  xcurr yprev trpoint lineto
  xcurr ycurr trpoint lineto
  lastpair
  { xleft ycurr trpoint exch xextra sub exch lineto 
  } if
  linewidth setlinewidth stroke
} def

% % pre-texture versions
% % stroke a filled y histogram: - filledyhisto -
% /filledyhisto
% {
%   linewidth setlinewidth
%   xprev yleft trpoint exch currentlinewidth 2 div add exch yextra sub moveto
%   xprev yprev trpoint exch currentlinewidth 2 div add exch lineto
%   xcurr yprev trpoint exch currentlinewidth 2 div sub exch lineto
%   xcurr yleft trpoint exch currentlinewidth 2 div sub exch yextra sub lineto 
%   closepath fill
% } def

% % stroke a filled x histogram: - filledxhisto -
% /filledxhisto
% {
%   linewidth setlinewidth
%   xleft yprev trpoint currentlinewidth 2 div add exch xextra sub exch moveto
%   xcurr yprev trpoint currentlinewidth 2 div add lineto
%   xcurr ycurr trpoint currentlinewidth 2 div sub lineto
%   xleft ycurr trpoint currentlinewidth 2 div sub exch xextra sub exch lineto 
%   closepath fill
% } def

% stroke a filled y histogram: - filledyhisto -
/filledyhisto
{
  linewidth setlinewidth
  xprev yleft trpoint exch currentlinewidth 1.3 mul add exch yextra sub moveto
  xprev yprev trpoint exch currentlinewidth 1.3 mul add exch lineto
  xcurr yprev trpoint exch currentlinewidth 1.3 mul sub exch lineto
  xcurr yleft trpoint exch currentlinewidth 1.3 mul sub exch yextra sub lineto 
  gsave texture fill grestore stroke
} def

% stroke a filled x histogram: - filledxhisto -
/filledxhisto
{
  linewidth setlinewidth
  xleft yprev trpoint currentlinewidth 1.3 mul add exch xextra sub exch moveto
  xcurr yprev trpoint currentlinewidth 1.3 mul add lineto
  xcurr ycurr trpoint currentlinewidth 1.3 mul sub lineto
  xleft ycurr trpoint currentlinewidth 1.3 mul sub exch xextra sub exch lineto 
  gsave texture fill grestore stroke
} def


% docross: show a cross with a given symbolsize and symbollinewidth
% <x> <y> <symbolsize> <symbollinewidth> docross -
/docross
{
  setlinewidth
  /ss exch def
  newpath
  moveto
  ss neg ss neg rmoveto
  ss 2 mul ss 2 mul rlineto
  0 ss -2 mul rmoveto
  ss -2 mul ss 2 mul rlineto
  [] 0 setdash 0 setlinecap stroke
} def

% cross: show a cross
% - cross -
/cross { xcurr ycurr trpoint symbolsize symbollinewidth docross } def

% doplus: show a plus with a given symbolsize and symbollinewidth
% <x> <y> <symbolsize> <symbollinewidth> doplus -
/doplus
{
  setlinewidth
  /ss exch def
  newpath
  moveto
  ss neg 0 rmoveto
  ss 2 mul 0 rlineto
  ss neg ss neg rmoveto
  0 ss 2 mul rlineto
  [] 0 setdash 0 setlinecap stroke
} def

% plus: show a plus
% - plus -
/plus { xcurr ycurr trpoint symbolsize symbollinewidth doplus } def

% dosquare: show an open square with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> dosquare -
/dosquare
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  moveto
  ss neg ss neg rmoveto
  ss 2 mul 0 rlineto
  0 ss 2 mul rlineto
  ss -2 mul 0 rlineto
  closepath [] 0 setdash stroke
} def

% square: show an open square
% - square -
/square { xcurr ycurr trpoint symbolsize symbollinewidth dosquare } def

% dofilledsquare: show filled square with given symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilledsquare -
/dofilledsquare
{
  pop /ss exch def
  newpath
  moveto
  ss neg ss neg rmoveto
  ss 2 mul 0 rlineto
  0 ss 2 mul rlineto
  ss -2 mul 0 rlineto
  closepath fill
} def

% filledsquare: show a filled square
% - filledsquare -
/filledsquare { xcurr ycurr trpoint symbolsize symbollinewidth dofilledsquare } def

% dodiamond: show an open diamond with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> dodiamond -
/dodiamond
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  moveto
  ss neg 0 rmoveto
  ss ss neg rlineto
  ss ss rlineto
  ss neg ss rlineto
  closepath [] 0 setdash stroke
} def

% diamond: show an open diamond
% - diamond -
/diamond { xcurr ycurr trpoint symbolsize symbollinewidth dodiamond } def

% dofilleddiamond: show filled diamond with given symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilleddiamond -
/dofilleddiamond
{
  pop /ss exch def
  newpath
  moveto
  ss neg 0 rmoveto
  ss ss neg rlineto
  ss ss rlineto
  ss neg ss rlineto
  closepath fill
} def

% filleddiamond: show a filled diamond
% - filleddiamond -
/filleddiamond { xcurr ycurr trpoint symbolsize symbollinewidth dofilleddiamond } def

% docircle: show an open circle with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> docircle -
/docircle
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  ss 0 360 arc [] 0 setdash stroke
} def

% circle: show an open circle
% - circle -
/circle { xcurr ycurr trpoint symbolsize symbollinewidth docircle } def

% dofilledcircle: show filled circle with given symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilledcircle -
/dofilledcircle
{
  pop /ss exch def
  newpath
  ss 0 360 arc fill
} def

% filledcircle: show a filled circle
% - filledcircle -
/filledcircle { xcurr ycurr trpoint symbolsize symbollinewidth dofilledcircle } def

% dotriangle: show an open triangle with a given symbolsize and symbollinewidth
% NB symbolsize is reduced by half the line width to get size exactly right
% <x> <y> <symbolsize> <symbollinewidth> dotriangle -
/dotriangle
{
  dup setlinewidth
  0.5 mul sub 0 max /ss exch def
  newpath
  moveto
  0 ss 1.5 mul rmoveto
  ss neg ss -2.5 mul rlineto
  ss 2 mul 0 rlineto
  closepath [] 0 setdash stroke
} def

% triangle: show an open triangle
% - triangle -
/triangle { xcurr ycurr trpoint symbolsize symbollinewidth dotriangle } def

% dofilledtriangle: show filled triangle with symbolsize and symbollinewidth
% NB symbollinewidth is not used
% <x> <y> <symbolsize> <symbollinewidth> dofilledtriangle -
/dofilledtriangle
{
  pop /ss exch def
  newpath
  moveto
  0 ss 1.5 mul rmoveto
  ss neg ss -2.5 mul rlineto
  ss 2 mul 0 rlineto
  closepath fill
} def

% filledtriangle: show a filled triangle
% - filledtriangle -
/filledtriangle { symbolsize symbollinewidth dofilledtriangle } def


%plog: like log only with a base, and protected from failing if <= 0
% base x plog res
/plog { dup 0 le { pop pop 0 } { ln exch ln div } ifelse } def

% xtr: transform one x value logarithmically if xlog > 1
% <num> xtr <num>
/xtr
{ xlog 1 gt
  { xlog exch plog
  } if
} def

% ytr: transform one y value logarithmically if ylog > 1
% <num> ytr <num>
/ytr
{ ylog 1 gt
  { ylog exch plog
  } if
} def


% trpoint: transform (x, y) in graph space into (x', y') in print space
% x y trpoint x' y'
/trpoint
{ exch xtr xdecr { trxmax exch sub } { trxmin sub } ifelse
  trxmax trxmin sub div xwidth mul xextra add

  exch ytr ydecr { trymax exch sub } { trymin sub } ifelse
  trymax trymin sub div ywidth mul yextra add
} def


% yonly: interpolate x values 1, 2, ... into data
% [ data ] yonly [ newdata ]
/yonly
{ dup /tmp exch def
  length [ exch 1 exch 1 exch
  { dup tmp exch 1 sub get
  } for
  ]
} def

% xonly: interpolate y values 1, 2, ... into data
% [ data ] yonly [ newdata ]
/xonly
{ dup /tmp exch def
  length [ exch 1 exch 1 exch
  { dup tmp exch 1 sub get exch
  } for
  ]
} def

% xandy: no interpolation of x or y values
% [ data ] xandy [ data ]
/xandy {} def

% swapxandy: swap x and y values
% [ data ] swapxandy [ data ]
/swapxandy
{ dup /tmp exch def
  length [ exch 2 exch 2 exch
  { dup tmp exch 1 sub get exch 2 sub tmp exch get
  } for
  ]
} def


% expstringwidth: calculate width of string containing optional exponent
% <string> expstringwidth <width>
/expstringwidth
{ (^) search
  { exch pop stringwidth pop exch stringwidth pop 0.7 mul add }
  { stringwidth pop }
  ifelse
} def

% expstringshow: show string containing optional exponent
% <string> expstringshow -
/expstringshow
{ (^) search
  { exch pop show 0 0.5 ft rmoveto
    gsave currentfont 0.7 scalefont setfont show grestore
  }
  { show
  }
  ifelse
} def

% concatenate two strings: <string> <string> strconcat <string>
/strconcat
{ 2 copy length exch length add string
  dup 0 4 index putinterval
  dup 3 index length 3 index putinterval
  3 1 roll pop pop
} def

% lgen: generate one label automatically
% num lgen num string
/lgen { dup 20 string cvs } def

% loglgen: generate one logarithmic label (with exponent)
% <base> <exponent> loglgen <string>
/loglgen
{ 20 string cvs exch 20 string cvs
  (^) strconcat exch strconcat
} def


% printxtick: print one x tick
% xpos printxtick -
/printxtick
{ newpath
  yleft trpoint moveto 0 yextra neg rmoveto
  0 xticklength neg rlineto [] 0 setdash stroke
} def

% printxgrid: print one x grid line
% xpos printxgrid -
/printxgrid
{ dup newpath
  yleft trpoint moveto 0 yextra neg rmoveto
  yright trpoint lineto 0 yextra rlineto [] 0 setdash stroke
} def

% printxlabel: print one x label
% (xlabel) xpos printxlabel -
/printxlabel
{ yleft trpoint moveto 0 yextra neg rmoveto
  0 xticklength neg rmoveto 0 0.9 ft neg rmoveto
  xlog 1 gt { 0 0.3 ft neg rmoveto } if
  dup expstringwidth -2 div 0 rmoveto expstringshow
} def

% printytick: print one y tick
% ypos printytick -
/printytick
{ newpath
  xleft exch trpoint moveto xextra neg 0 rmoveto
  yticklength neg 0 rlineto [] 0 setdash stroke
} def

% printygrid: print one y grid line
% ypos printygrid -
/printygrid
{ dup newpath
  xleft exch trpoint moveto xextra neg 0 rmoveto
  xright exch trpoint lineto xextra 0 rlineto [] 0 setdash stroke
} def

% printylabel: print one y label
% (ylabel) ypos printylabel -
/printylabel
{ xleft exch trpoint moveto xextra neg 0 rmoveto
  yticklength neg 0 rmoveto -0.3 ft -0.3 ft rmoveto
  dup expstringwidth neg 0 rmoveto expstringshow
} def

% printrtick: print one r tick
% ypos printrtick -
/printrtick
{ newpath
  xright exch trpoint moveto xextra 0 rmoveto
  rticklength 0 rlineto [] 0 setdash stroke
} def

% printrlabel: print one r label
% (rlabel) ypos printrlabel -
/printrlabel
{ xright exch trpoint moveto xextra 0 rmoveto
  rticklength 0 rmoveto  0.3 ft -0.3 ft rmoveto
  expstringshow
} def

% printticks: print ticks and labels
% /tickproc /labelproc [ tickandlabeldata ] min printticks -
/printticks
{ /prev exch def
  { dup type dup dup /integertype eq exch /realtype eq or
    { pop dup /prev exch def 2 index cvx exec
    }
    { /stringtype eq
      { prev 2 index cvx exec
      }
      { pop
      } ifelse
    } ifelse
  } forall
  pop pop
} def


% printxaxistick: print one x axis tick
% xpos printxaxistick -
/printxaxistick
{ newpath
  yaxis trpoint moveto 0 xticklength -2 div rmoveto
  0 xticklength rlineto [] 0 setdash stroke
} def

% printxaxislabel: print one x axis label
% (xlabel) xpos printxaxislabel -
/printxaxislabel
{ yaxis trpoint moveto
  0 xticklength -2 div rmoveto 0 0.9 ft neg rmoveto
  xlog 1 gt { 0 0.3 ft neg rmoveto } if
  dup expstringwidth -2 div 0 rmoveto expstringshow
} def

% printyaxistick: print one y axis tick
% ypos printyaxistick -
/printyaxistick
{ newpath
  xaxis exch trpoint moveto
  yticklength -2 div 0 rmoveto
  yticklength 0 rlineto [] 0 setdash stroke
} def

% printyaxislabel: print one y axis label
% (ylabel) ypos printyaxislabel -
/printyaxislabel
{ xaxis exch trpoint moveto
  yticklength -2 div 0 rmoveto -0.3 ft -0.3 ft rmoveto
  dup expstringwidth neg 0 rmoveto expstringshow
} def


% <val> minmax -
% perform minv := min(minv, val); maxv := max(maxv, val)
% allowing for the possibility of minv, maxv, val being false (undefined)
/minmax
{ dup false eq
  { pop }
  { minv false eq
    { dup /minv exch def /maxv exch def }
    { dup minv lt
      { /minv exch def }
      { dup maxv gt
	{ /maxv exch def }
	{ pop }
	ifelse
      } ifelse
    } ifelse
  } ifelse
} def

% <ticks> ticksundef <ticks> <bool>
% returns true iff the ticks array is undefined (one false entry)
/ticksundef
{ dup length 1 eq
  { dup 0 get false eq
  }
  { false }
  ifelse
} def

% <number> integral <boolean>
% true if the number has an integral value
/integral { dup round eq } def

% ticksep ticks xory alldata minval maxval axis base ticksandlimits ticks min max base
% ticksandlimits: sort out value of x or y ticks and limits and log base
/ticksandlimits
{ /base exch def
  /minv false def
  /maxv false def

  % min and max of user-supplied minval, maxval, and axis
  minmax minmax minmax

  % min and max of data points
  { 0 get dup dup length 1 sub 3 index exch 2 exch
    { get minmax dup
    } for pop pop
  } forall
  pop dup

  % min and max of tick values
  { dup type /stringtype eq
    { pop } { minmax } ifelse
  } forall

  % fix minv and maxv if undefined (false) or equal
  minv false eq
  { /minv -1 def /maxv 1 def }
  { minv maxv eq
    { minv 0 lt
      { /minv 2 minv mul def /maxv 0 def
      }
      { minv 0 eq
	{ /minv -1 def /maxv 1 def
	}
	{ /minv 0 def /maxv 2 maxv mul def
	} ifelse
      } ifelse
    } if
  } ifelse

  % invent ticks if undefined
  ticksundef
  { pop /ticksep exch def

    % if base is reasonable and minv is positive, logarithmic ticks
    base 1 gt minv 0 gt and
    {
      % get integral log of minv and maxv
      /logminv base minv plog floor   cvi def
      /logmaxv base maxv plog ceiling cvi def

      % if minv close to base, make it 1; reset minv and maxv
      logminv 1 eq logmaxv 4 ge and { /logminv 0 def } if
      /minv base logminv exp def
      /maxv base logmaxv exp def

      % ticks := [ base**logminv, ... , base**logmaxv ]
      [ logminv 1 logmaxv
	{ dup base exch exp
	  exch base exch loglgen
	} for
      ]
    }
    { % non-logarithmic ticks
      {
        % fix tick separation if undefined (0) or too small
	/base 0 def
        /delta maxv minv sub def
        ticksep delta 30 div le
        { /ticksep 10 delta log 1 sub ceiling exp def
	  ticksep delta 2 div ge
	  { /ticksep ticksep 2 div def }
	  { ticksep delta 5 div lt
	    { /ticksep 2 ticksep mul def
	    } if
	  } ifelse
        } if

        % adjust minv and maxv to be multiples of ticksep
        /minv minv ticksep div floor   ticksep mul def
        /maxv maxv ticksep div ceiling ticksep mul def
        /delta maxv minv sub def

        % if minv or maxv near zero, move to zero and redo
        minv ticksep eq
        { /minv 0 def }
        { maxv ticksep neg eq { /maxv 0 def } { exit } ifelse
        } ifelse
      } loop

      % if minv, maxv, and ticksep are all integral, set "makeint" to true
      /makeint minv integral maxv integral ticksep integral and and def

      % ticks := [ minv, minv+ticksep, ... , maxv ]
      [ 0 1 delta ticksep div round
	{ ticksep mul minv add makeint { cvi } if lgen }
	for
      ]
    } ifelse
  }
  { exch pop
  } ifelse
  minv maxv base
} def

% xset: set up all data for x axis, including limits and ticks
% xticksep xticks 0 alldata xmin xmax xlog xextra xdecr xaxis xticklength xset -
/xset
{ /xticklength exch def
  /xaxis exch def
  /xdecr exch def
  /xextra exch def
  xaxis exch ticksandlimits
  /xlog exch def /xmax exch def /xmin exch def /xticks exch def
  /xleft xdecr { xmax } { xmin } ifelse def
  /xright xdecr { xmin } { xmax } ifelse def
  /xwidth xsize xextra 2 mul sub def
  /trxmin xmin xtr def /trxmax xmax xtr def
} def

% yset: set up all data for y axis, including limits and yticks
% yticksep yticks 0 alldata ymin ymax ylog yextra ydecr yaxis yticklength yset -
/yset
{ /yticklength exch def
  /yaxis exch def
  /ydecr exch def
  /yextra exch def
  yaxis exch ticksandlimits
  /ylog exch def /ymax exch def /ymin exch def /yticks exch def
  /yleft ydecr { ymax } { ymin } ifelse def
  /yright ydecr { ymin } { ymax } ifelse def
  /ywidth ysize yextra 2 mul sub def
  /trymin ymin ytr def /trymax ymax ytr def
} def

% rset: set up all data for y axis (again), but including limits and rticks
% rticksep rticks 0 alldata ymin ymax ylog yextra ydecr yaxis rticklength rset -
/rset
{ /rticklength exch def
  /yaxis exch def
  /ydecr exch def
  /yextra exch def
  yaxis exch ticksandlimits
  /ylog exch def /ymax exch def /ymin exch def /rticks exch def
  /yleft ydecr { ymax } { ymin } ifelse def
  /yright ydecr { ymin } { ymax } ifelse def
  /ywidth ysize yextra 2 mul sub def
  /trymin ymin ytr def /trymax ymax ytr def
} def

% norset: set up data for no rticks
% - norset -
/norset
{ /rticklength 0 def
  /rticks [] def
} def

% framestyle: print a frame around the graph
/framestyle
{ 0 0 moveto xsize 0 lineto xsize ysize lineto
  0 ysize lineto closepath stroke
  /printxtick /printxlabel xticks xleft printticks
  /printytick /printylabel yticks ymin printticks
  /printrtick /printrlabel rticks ymin printticks
} def

% gridstyle: print a frame around the graph, plus a grid
/gridstyle
{ 0 0 moveto xsize 0 lineto xsize ysize lineto
  0 ysize lineto closepath stroke
  /printxgrid /printxlabel xticks xleft printticks
  /printygrid /printylabel yticks ymin printticks
  /printrtick /printrlabel rticks ymin printticks
} def

% nonestyle: print nothing around the graph
/nonestyle
{
} def

% axesstyle: print axes for the graph (unless axis values missing)
/axesstyle
{
  xaxis false eq yaxis false eq or
  { framestyle }
  { xaxis yaxis trpoint dup 0 exch moveto xsize exch lineto
    dup 0 moveto ysize lineto stroke
    /printxaxistick /printxaxislabel xticks xleft printticks
    /printyaxistick /printyaxislabel yticks ymin printticks
  } ifelse
} def

% rundata: run all data sets
/rundata
{ alldata
  { gsave
    dup dup dup dup dup
    5 get /texture exch def
    4 get /dopaint exch def
    3 get /initrun exch def
    2 get /pairs exch def
    1 get /points exch def
    0 get /data exch def
    dopaint
    { data length 4 ge
      {
	gsave
	initrun
	newpath
	data 0 get ymin trpoint yextra sub moveto
	0 2 data length 2 sub
	{ dup 1 add
	  data exch get /ycurr exch def
	  data exch get /xcurr exch def
	  xcurr ycurr trpoint lineto
	} for
	data dup length 2 sub get ymin trpoint yextra sub lineto
	closepath texture fill
	grestore
      } if
    } if
    initrun
    data length 2 ge
    {
      /xcurr data 0 get def
      /ycurr data 1 get def
      points
      data length 4 ge
      { 2 2 data length 2 sub
	{ /xprev xcurr def
          /yprev ycurr def
	  dup dup 2 eq /firstpair exch def
	  data length 2 sub eq /lastpair exch def
	  dup 1 add
          data exch get /ycurr exch def
          data exch get /xcurr exch def
	  pairs
	  points
	} for
      } if
    } if
    grestore
  } forall
} def

end
%%EndResource
