c	spurion_grid.f

      subroutine cmd_grid(do_ask, do_grid, uuv, vuv, rlu, xstar, ystar)
      implicit none
      include 'arraysize.inc'
c      include 'io_files.inc'
      include 'spur_color.inc'
      include 'spur_scan.inc'
      include 'spur_layout.inc'

c...	passed variables
      logical do_ask	!! Only ask for grid size if do_ask is true
      logical do_grid	!! toggle plotting of grid on Lattice plot.
      REAL*4 uuv(3),vuv(3)	!! lattice unit vectors
      logical rlu
      real 	xstar, ystar	!! xstar=uuv(1) and  ystar=vuv(2) in A^-1
      
c...	local variables
      real xline(2), yline(2)
      integer i, iedge, ngridmax
      
      if (do_ask) then
110          WRITE (*,'(a, $)') 'Enter style of grid ', 
     &		'[c: cartesian, o:orientation vectors]: '
	     read (*,'(a1)', err=110) grid_style
cDebug	     write (*,*) 'cmd_grid: grid_style = "', grid_style, '"', 
cDebug     &	        ' "', grid_style_cart,' " ',  grid_style_orient, '"' 
	     if ((grid_style .ne. grid_style_cart) .and. 
     &		 (grid_style .ne. grid_style_orient)) goto 110
c	     if ((grid_style .ne. 'o') .and. 
c     &		 (grid_style .ne. 'c')) goto 110
120          WRITE (*,'(a, $)') 'Enter x/u and y/v grid sizes in rlu: '
	     read (*,*, err=120) grid_size1, grid_size2
	     if ((grid_size1 .lt. 0.0) .or. 
     &		 (grid_size1 .gt. 5.0)) goto 120
c130          WRITE (*,'(a, $)') 'Enter y/v grid size in rlu: '
c	     read (*,*, err=130) grid_size2
	     if ((grid_size2 .lt. 0.0) .or. 
     &		 (grid_size2 .gt. 5.0)) goto 120
      endif

c	Cartesian style      
c      if ((do_grid) .and. (grid_style .eq. 'c')) then
      if ((do_grid) .and. (grid_style .eq. grid_style_cart)) then
        call pgsls(4)          ! dotted line
        call pgsci(color_darkgrey)		!! Override color
c pg_lib.f: 	call pgwnad(xmin,xmax,ymin,ymax)
c...	vertical grid lines
c        !! xmin, xmax in A^-1 always. Convert grid_size1
	ngridmax = nint( ((xmax - xmin) / (grid_size1*xstar)) + 1.0)
	yline(1) = ymin
	yline(2) = ymax
c          if (rlu) then
cMay8	    yline(1) = yline(1) *ystar
cMay8	    yline(2) = yline(2) *ystar
c	  endif
	do i = 1, ngridmax
	  xline(1) = grid_size1*xstar*float(nint(xmin/(grid_size1*xstar))) 
     &		  + float(i)*grid_size1*xstar
          xline(2) = xline(1)
c          if (rlu) then
cMay8	    xline(1) = xline(1) *xstar
cMay8	    xline(2) = xline(2) *xstar
c	  endif
	  call pgline(2, xline, yline)
	enddo

c...	horizontal grid lines
c        !! xmin, xmax in A^-1 always. convert grid_size2
	ngridmax = nint( ((ymax - ymin) / (grid_size2*ystar)) + 1.0)
	xline(1) = xmin
	xline(2) = xmax
c          if (rlu) then
cMay8	    xline(1) = xline(1) *xstar
cMay8	    xline(2) = xline(2) *xstar
c	  endif
	do i = 1, ngridmax
	  yline(1) = grid_size2*ystar*float(nint(ymin/(grid_size2*ystar))) 
     &		  + float(i)*grid_size2*ystar
          yline(2) = yline(1)
c          if (rlu) then
cMay8	    yline(1) = yline(1) *ystar
cMay8	    yline(2) = yline(2) *ystar
c	  endif
	  call pgline(2, xline, yline)
	enddo
      endif      

c	orientation vectors style (diamonds if not cubic)
c      if ((do_grid) .and. (grid_style .eq. 'o')) then
      if ((do_grid) .and. (grid_style .eq. grid_style_orient)) then
        call pgsls(4)          ! dotted line
        call pgsci(color_darkgrey)		!! Override color
c pg_lib.f: 	call pgwnad(xmin,xmax,ymin,ymax)
c...	vertical grid lines
	iedge = (m_rlv_max/2)
	do i = -iedge,  iedge		!! overkill range
	  xline(1) = 0.0 + grid_size1*float(i)*uuv(1) 
     &		 - grid_size2*float(iedge)*vuv(1)
	  xline(2) = 0.0 + grid_size1*float(i)*uuv(1)  
     &		 + grid_size2*float(iedge)*vuv(1)
	  yline(1) = 0.0 + grid_size1*float(i)*uuv(2)  
     &		 - grid_size2*float(iedge)*vuv(2)
	  yline(2) = 0.0 + grid_size1*float(i)*uuv(2)  
     &		 + grid_size2*float(iedge)*vuv(2)
c          if (.not. rlu) then
c	    xline(1) = xline(1) *xstar
c	    xline(2) = xline(2) *xstar
c	    yline(1) = yline(1) *ystar
c	    yline(2) = yline(2) *ystar
c	  endif
	  call pgline(2, xline, yline)
cDebug	  write (*,*) 'cmd_grid: vert ', i, 
cDebug     &	  	('(', xline(j), yline(j), ') ', j=1,2)
	enddo

c...	horizontal grid lines
	do i = -iedge,  iedge		!! overkill range
	  xline(1) = 0.0 - grid_size1*float(iedge)*uuv(1)  
     &		 + grid_size2*float(i)*vuv(1)
	  xline(2) = 0.0 + grid_size1*float(iedge)*uuv(1)  
     &		 + grid_size2*float(i)*vuv(1)
	  yline(1) = 0.0 - grid_size1*float(iedge)*uuv(2)  
     &		 + grid_size2*float(i)*vuv(2)
	  yline(2) = 0.0 + grid_size1*float(iedge)*uuv(2)  
     &		 + grid_size2*float(i)*vuv(2)
c          if (rlu) then
c	S    xline(1) = xline(1) *xstar
c	    xline(2) = xline(2) *xstar
c	    yline(1) = yline(1) *ystar
c	    yline(2) = yline(2) *ystar
c	  endif
	  call pgline(2, xline, yline)
cDebug	  write (*,*) 'cmd_grid: horz ', i, 
cDebug     &	  	('(', xline(j), yline(j), ') ', j=1,2)
	enddo
      endif      

      call pgsls(1)			!! return to solid lines
      
      return
      end
