
      program satv
c
      include "uars.h"
      include "timegcm.h"
      include "cpwrk.h"
      parameter(mxvols=4,mxtms=50)
      dimension iasf(13),viewport(4),utlim(2),censatv(2),ipltime(nflds),
     +  gcmlat(jmx),gcmlon(imx),gcmzp(kmx),ixtime(nflds),mt(3,mxtms),
     +  gcm1(imx,kmx,jmx,nflds)
      pointer (pgcm0,gcm0(1))
      character*32 gcmvol(mxvols)
      character*2 ou
      data iasf/13*1/
      data lusat/10/, iframe/0/, lugcm/8/, iden/2/, iprhdr/0/
      data viewport/.05,.80,.13,.88/
      data re/6371./
      equivalence(ixtime,itxt)
c
      call opngks
      call gsclip(0)
      call gsasf(iasf)
      call gsfais(1)
      call getinp(utlim,norbfr,censatv,grid,erdist,ierot,ou,
     +  gcmvol,nvol,mt,ipltime,zp,itimegcm,icolor)
c
c Gcm grid:
c
      if (itimegcm.gt.0) then
        do j=1,jmx
          gcmlat(j) = rlat1 + (j-1)*dlat
        enddo
        do i=1,imx
          gcmlon(i) = rlon1 + (i-1)*dlon
        enddo
        do k=1,kmx
          gcmzp(k) = zp1 + (k-1)*dzp
        enddo
        do ip=1,nflds
          ixtime(ip) = ip
        enddo
c Assume use first plot flag for now:
        do i=1,nflds
          if (ipltime(i).gt.0) then
            ixplt = ixtime(i)
            goto 200
          endif
        enddo
 200    continue
        call gettime(gcmvol,nvol,istart,mt(1,1),gcm1,imx,kmx,jmx,
     +    nflds,iden,lugcm,iprhdr,ivol,ier)
        if (ier.ne.0) then
          write(6,"('>>> satv: error ',i3,' from gettime for time ',
     +      3i4)") ier,(mt(i,1),i=1,3)
          stop 'gettime'
        endif
        write(6,"('satv: obtained gcm for time ',3i4,' from vol ',a)") 
     +    (mt(i,1),i=1,3),gcmvol(ivol)
        ixzp = ixfind(gcmzp,kmx,zp,dzp)  
      endif
c
 100  continue
      call rdapprox(satatt,lusat,utlim,satut,satlat,satlon,satalt,
     +  mxpts,npts,iyd,norbfr,ier)
      if (ier.ne.0) then
        write(6,"('satv: error ',i3,' from rdapprox')") ier
        stop 'rdapprox'
      elseif (npts.le.0) then
        write(6,"('satv: rdapprox returned bad npts=',i4)") npts
        stop 'npts'
      endif
      aveht = 0.
      do i=1,npts
        aveht = aveht + satalt(i)
      enddo
      aveht = aveht / npts
      write(6,"('satv: npts=',i4,' utlim=',2f8.2,' aveht=',f9.3)") 
     +  npts,utlim,aveht
c
      if (ierot.le.0) then
        if (erdist.eq.0.) erdist = 1.+aveht/re
        if (erdist.lt.0.) erdist = 0.
        call mksatv(viewport,censatv,grid,erdist,ou)
        call drwpath(npts,satlat,satlon)
        if (itimegcm.gt.0) call cpgcm(gcm1,gcmlat,gcmlon,ixplt,ixzp,
     +                                icolor)
        call frame
        iframe = iframe+1
        write(6,"('satv: frame ',i4,' censatv=',2f9.3)") iframe,censatv
      else
        do ipt = 1,npts
          if (mod(ipt,ierot).eq.0) then
            censatv(1) = satlat(ipt)
            censatv(2) = satlon(ipt)
            if (erdist.eq.0.) erdist = 1.+satalt(ipt)/re
            if (erdist.lt.0.) erdist = 0.
            call arinam(iama,liama)
            call mksatv(viewport,censatv,grid,erdist,ou)
            if (itimegcm.gt.0) call cpgcm(gcm1,gcmlat,gcmlon,ixplt,ixzp,
     +                                    icolor)
            call mapgrd
            call maplot
            call drwpath(ipt,satlat,satlon)
            call frame
            iframe = iframe+1
            write(6,"('frame ',i4,' ipt=',i4,' censatv=',f7.3,
     +        f8.3,' ut=',f6.3,' alt=',f7.3)") 
     +        iframe,ipt,censatv,satut(ipt),satalt(ipt)
          endif
        enddo
      endif
      if (satut(npts).lt.utlim(2)-.001) goto 100
c
      call clsgks
      stop
      end
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
      subroutine cpgcm(gcm,gcmlat,gcmlon,ixplt,ixzp,icolor)
      include "timegcm.h"
      dimension gcm(imx,kmx,jmx,nflds),gcmlat(jmx),gcmlon(imx),
     +  plot(imx,jmx)
      data ncalls/0/
      save ncalls
c
      ncalls = ncalls+1
      if (ncalls.eq.1) then
        call cpsetup(1.e36)
        call cpsetr('XC1',gcmlon(1))
        call cpsetr('XCM',gcmlon(imx))
        call cpsetr('YC1',gcmlat(1))
        call cpsetr('YCN',gcmlat(jmx))
        call cpseti('SET',0)
        call cpseti('ILP',0)
        call cpseti('MAP',1)
        call cpsetr('ORV',1.e12)
      endif
      plot(:,:) = gcm(:,ixzp,:,ixplt)
      if (icolor.gt.0) then
        write(6,"('cpgcm calling conclr')")
        call conclr(plot,imx,imx,jmx)
      else
        call contour(plot,imx,imx,jmx)
      endif
      return
      end
