	parameter (nx=144,ny=73,nfield=1)
	! parameter (maxwv=(nx-2)/2))
	parameter (maxwv=62)

	real u(nx,ny,nfield), v(nx,ny,nfield)
	real vort(nx,ny,nfield), div(nx,ny,nfield)
	real strm(nx,ny,nfield), vpot(nx,ny,nfield)

	open(unit=1,file='fort.1',status='unknown',
     1	   form='unformatted')
	open(unit=50,file='dump',status='old',
     1	   form='unformatted')

	read(50) u
	read(50) v

	call uv2all(u,v, .true.,vort, .true.,strm,
     1		.true.,div, .true.,vpot, nx,ny,nfield,maxwv)

	write(1) u
	write(1) v
	write(1) vort
	write(1) strm
	write(1) div
	write(1) vpot

	stop
	end

	subroutine uv2all(u,v, vortf, vort, strmf, strm,
     1		divf, div, vpotf, vpot, nx,ny,nfield,maxwv)


	parameter(iromb=0)	! triangular=0 rhombodial=1
	parameter(idrt=0)	! 4=gaussian, 0=lat-lon (with pole) 
				! 256=lat-lon (without pole)

	real u(nx,ny,nfield), v(nx,ny,nfield)
	real vort(nx,ny,nfield), div(nx,ny,nfield)
	real strm(nx,ny,nfield), vpot(nx,ny,nfield)
	logical vortf, divf, strmf, vpotf
c	real waved(mx,nfield), wavez(mx,nfield)
	real waved((maxwv+1)*((iromb+1)*maxwv+2), nfield)
	real wavez((maxwv+1)*((iromb+1)*maxwv+2), nfield)

	if (mod(nx,2).eq.1) then
		write(*,*) 'nx should be even'
		stop
	endif

c	get vorticity (wavez), and divergence (waved)

	call sptranv(iromb,maxwv,idrt,nx,ny,nfield,
     1		0,0,0,0, ! iprime, iskip, jnskip, jsskip
     2		0,0,0,0, ! kwskip, kgskip, jbeg, jend
     3		1,	 ! jcpu
     4		waved,wavez,
     5		u(1,1,1),u(1,ny,1),
     6		v(1,1,1),v(1,ny,1),
     7		-1)

c	write vorticity
	call sptran(iromb,maxwv,idrt,nx,ny,nfield,
     1		0,0,0,0, ! iprime, iskip, jnskip, jsskip
     2		0,0,0,0, ! kwskip, kgskip, jbeg, jend
     3		1,	 ! jcpu
     4		wavez, vort(1,1,1), vort(1,ny,1),1)

c	write stream function
	call vrtpsi(wavez,wavez,maxwv,iromb)
	call sptran(iromb,maxwv,idrt,nx,ny,nfield,
     1		0,0,0,0, ! iprime, iskip, jnskip, jsskip
     2		0,0,0,0, ! kwskip, kgskip, jbeg, jend
     3		1,	 ! jcpu
     4		wavez, strm(1,1,1), strm(1,ny,1),1)

c	write divergence
	call sptran(iromb,maxwv,idrt,nx,ny,nfield,
     1		0,0,0,0, ! iprime, iskip, jnskip, jsskip
     2		0,0,0,0, ! kwskip, kgskip, jbeg, jend
     3		1,	 ! jcpu
     4		waved, div(1,1,1), div(1,ny,1),1)

c	write velocity potential
	call vrtpsi(waved,waved,maxwv,iromb)
	call sptran(iromb,maxwv,idrt,nx,ny,nfield,
     1		0,0,0,0, ! iprime, iskip, jnskip, jsskip
     2		0,0,0,0, ! kwskip, kgskip, jbeg, jend
     3		1,	 ! jcpu
     4		waved, vpot(1,1,1), vpot(1,ny,1),1)

	return
	end


C  VORTICITY TO PSI (STREAMFUNCTION)                   WESLEY EBISUZAKI
C
C CONVERTS FROM VORTICITY  (SPECTRAL) TO STREAMFUNCTION
C
C
      SUBROUTINE VRTPSI(VORT,PSI,MWAVE,IROMB)
C
C
      PARAMETER (A=6.3712E6)
      REAL VORT(*), PSI(*)
C
      INTEGER I, M, NMAX, N
C
      MDIM=IROMB*(MWAVE+1)*(MWAVE+1)*2+(1-IROMB)*(MWAVE+1)*(MWAVE+2)
C
C     *** GO FROM VORTICITY TO STREAM FUNCTION ***
C
      I = 1
      DO 110 M = 0, MWAVE
          IF (IROMB.EQ.1) THEN
              NMAX = M + MWAVE
          ELSE
              NMAX = MWAVE
          ENDIF
          DO 100 N = M, NMAX
              IF (N.NE.0) THEN
                  PSI(I ) = - (A*A/(N*(N+1))) * VORT(I )
                  PSI(I+1) = - (A*A/(N*(N+1))) * VORT(I+1)
              ELSE
                  PSI(I) = 0.0
                  PSI(I+1) = 0.0
              ENDIF
              I = I + 2
100       CONTINUE
110   CONTINUE
C
      RETURN
      END
