c ROTATE.FOR
c
c  To rotate a grid.  Output is an xyz file containing the 
c  rotated grid values, which must be regridded.  Dvals in the input 
c  grid are ok because dvals are skipped.  The grid is rotated by 
c  pivoting it around it's center.
c
c  September 1993
c
      character*50 ifile,ofile
      character prompt*80
      dimension f(2000),id(14),pgm(2),xyf(3),xyc(2)
c      data dval/0.170412e+39/
      dval2=1.e20
c      write(6,1)
c1     format(' Enter input grid file name:'/' *'$)
c      read(5,100)ifile
c100     format(a50)
c      open(10,file=ifile,form='unformatted',status='old')
c      read(10)id,pgm,ncol,nrow,nz,x0,dx,y0,dy
      call askin
      ifile=' '
400   prompt='Enter input grid file name'
      call askc(prompt,ifile,ierr)
      if(ierr.eq.-2) stop
      call gopen(10,ifile,'old','read',ierr)
      if(ierr.ne.0) stop 'error opening file'
      call gheader('r',10,id,ncol,nrow,x0,dx,y0,dy,ierr)
      if(ierr.ne.0) stop 'error reading header'
c      if(ncol.gt.2000.or.nz.ne.1.or.dx.eq.0.e0) go to 900
      if(ncol.gt.2000.or.dx.eq.0.e0) go to 900
c        write(6,201)
c201   format(1x,'Enter output xyz file name:'/' *'$)
c      read(5,100)ofile
c      open(11,file=ofile,form='unformatted',status='new')
      ofile=' '
401   prompt='Enter output xyz file name'
      call askc(prompt,ofile,ierr)
      if(ierr.eq.-2) then
        close(10)
        go to 400
      endif
      call xopen(11,ofile,'new','write',itype,ierr)
      if(ierr.ne.0) go to 401
c202   print*,' Enter rotation angle phi, clockwise positive'
c      print*,' from north (-90 < phi < 90):'
c      print 101
c101   format(' *'$)
c      read*,phi
       phi=0.0
202    print*,' Enter rotation angle phi, clockwise positive'
       prompt='from north (-90 < phi < 90):'
       call askf4(prompt,phi,ierr)
       if(ierr.eq.-2) then
         close(11)
         go to 401
       endif
      if(phi.le.-90.0.or.phi.ge.90.0)then
	print*,' Cant handle it.'
	go to 202
      endif
      phi=phi*1.74532e-02
      s=sin(phi)
      c=cos(phi)
      x0c=x0+0.5*(ncol-1)*dx
      y0c=y0+0.5*(nrow-1)*dy
      icode=3
203   print*,'Enter pivot point code:'
      print*,'  1 pivot about grid origin xo=',x0,'yo=',y0
      print*,'  2 pivot about grid center xc=',x0c,'yc=',y0c
      print*,'  3 pivot about a specified point'
      prompt='*'
      call aski4(prompt,icode,ierr)
      if(ierr.eq.-2) go to 202
c      print 101
c      read*,icode
      if(icode.eq.1) then
        xc=x0
        yc=y0
      else if(icode.eq.2) then
        xc=x0c
        yc=y0c
      else if(icode.eq.3) then
c        print*,'enter pivot point x y:'
c        print 101
c        read*,xc,yc
        xyc(1)=0.
        xyc(2)=0.
        prompt='Enter pivot point x y'
        call aski4a(prompt,xyc,ierr)
        if(ierr.eq.-2) go to 203
        xc=xyc(1)
        yc=xyc(2)
      else
        print*,'invalid response'
        go to 203
      endif
      do 20 j=1,nrow
      y1=y0+(j-1)*dy-yc
c      call rowio(f,y,1,ncol,10,1)
      call grow('r',10,j,f,ncol,ierr)
      do 10 i=1,ncol
      if(f(i).gt.dval2)go to 10
      x1=x0+(i-1)*dx-xc
      x2=x1*c+y1*s+xc
      y2=y1*c-x1*s+yc
c      write(11)x2,y2,f(i)
      xyf(1)=x2
      xyf(2)=y2
      xyf(3)=f(i)
      call xioxyz('w',11,xyf,3,ierr)
10    continue
20    continue
      go to 998
900   write(6,901)
901   format(' Can''t handle it.')
998   close(11)
999   close(10)
      stop
      end
c      subroutine rowio(a,y,nz,ncol,ld,key)
c      dimension a(nz,ncol)
c      go to (1,2),key
c1     read(ld) y,a
c      go to 90
c2     write(ld)y,a
c90    return
c      end
