function movie (args)

* This routine will animate any multi-quantity plot
* produced by another run_script.
*
* Note: if script has options, enclose them in ""
*

'numargs  'args
 numargs = result

if( numargs = 0 ) 
    say 'Usage: movie script ["opts"] [-tbeg tbeg]'
    say '                             [-tend tend]'
    say '                             [-print    ]'
    say '                             [-pause    ]'
    say '                             [-name     ]'
    say '                             [-loopdim  loopdim]'
    return
endif

if( numargs > 1 )
    opts = subwrd(args,2)
       n = 3
    while( n<=numargs )
    opts = opts' 'subwrd(args,n)
       n = n+1
    endwhile
endif

script = subwrd(args,1)
name   = script

length = strlen(opts)
if( length != 0 )
       last = substr(opts,1,1)
       if( last = '"' )
            nmax = 200
               n = 2
       while ( n<max )
       last = substr(opts,n,1)
       if( last = '"' )
           len = n-2
             n = max
       else
             n = n+1
       endif
       endwhile
       opts = substr( opts,2,len )
       endif
endif

      tbeg = 0
      tend = 0
    rotate = false
     print = false
     pause = false
   loopdim = t
       num = 0
while( num < numargs )
       num = num + 1
if( subwrd(args,num)='-tbeg'    ) ; tbeg    = subwrd(args,num+1) ; endif
if( subwrd(args,num)='-tend'    ) ; tend    = subwrd(args,num+1) ; endif
if( subwrd(args,num)='-print'   ) ; print   = true               ; endif
if( subwrd(args,num)='-pause'   ) ; pause   = true               ; endif
if( subwrd(args,num)='-rotate'  ) ; rotate  = true               ; endif
if( subwrd(args,num)='-name'    ) ; name    = subwrd(args,num+1) ; endif
if( subwrd(args,num)='-loopdim' ) ; loopdim = subwrd(args,num+1) ; endif
endwhile

if( loopdim != t )
    'run getinfo 'loopdim'min'
         tbeg = result
    'run getinfo 'loopdim'max'
         tend = result
endif

if( tbeg != 0 )
    t     = tbeg
else
    t     = 1
endif

if( tend != 0 )
    tmax  = tend
else
    tmax  = gettmax()
endif


say 'movie loop begin, 'loopdim' = 'tbeg
say 'movie loop   end, 'loopdim' = 'tmax
say 'print = 'print


frame = 1000

'run getenv "GEOSUTIL"'
             geosutil = result

'set grid on'
if( print != true ) ; 'set dbuff on' ; endif
'set csmooth on'
while (t<tmax+1)
      'set 'loopdim' 't
      frame = frame + 1
      'run 'script' 'opts
if( print != true )
      if( pause = true )
          say 'Hit -Enter- to continue ...'
          pull flag
      endif
     'swap'
else
*    'printim 'name'.'frame'.png x900 y720'
      if( rotate = true )
     'myprint -name 'name'.'frame' -rotate 90'
      else
     'myprint -name 'name'.'frame' -rotate 0'
      endif
      if( pause = true )
          say 'Hit -Enter- to continue ...'
          pull flag
      endif
     'c'
endif
      t = t+1
endwhile

function gettmax(args)
  'query file '
  tmax  = sublin(result,5)
  tmax  = subwrd(tmax,12)
return tmax
