************************************************************************

function scale_zdef_zlevs(input_ctl,zscale,output_ctl)

*-----------------------------------------------------------------------
*  Reads in a GrADS ctl file "input_ctl" and writes out a new GrADS ctl
*  file "output_ctl" which is the same as the "input_ctl" except that
*  the "zdef" levels are scaled by "zscale"...
*-----------------------------------------------------------------------

do_print = 'no'

if( do_print = 'yes' )
  say
  say '"scale_zdef_zlevs"...'
  say
endif
*
*  find number of records in "input_ctl"...
*
if( do_print = 'yes' )
  say input_ctl
endif
'!wc -l 'input_ctl' > wc_file'
res = read(wc_file)
  rc = sublin(res,1)
  wc_info = sublin(res,2)
  nrecs = subwrd(wc_info,1)
  if( do_print = 'yes' )
    say 'nrecs = 'nrecs
  endif
rc = close(wc_file) ; '!rm wc_file'

if( do_print = 'yes' )
  say
  say 'search for the "zdef" record in the "input_ctl",'
  say 'reading in and writing out "input_ctl" records in the process...'
  say
endif

found = false
nrec = 1
while ( found = false & nrec <= nrecs )
  res = read(input_ctl)
  rc = sublin(res,1)
    if( rc != 0 )
      say '"read" status = 'rc
      exit
    endif
  rec = sublin(res,2)
  if( do_print = 'yes' )
    say rec
  endif
  rc = write(output_ctl,rec,append)
  rec_word1 = subwrd(rec,1)
  if( rec_word1 = zdef | rec_word1 = ZDEF )
    found = true
    zdef_rec = rec ; zdef_nrec = nrec
  else
    nrec = nrec+1
  endif
endwhile
if( found = true )
  if( do_print = 'yes' )
    say
    say '"zdef" record (nrec='nrec') is:'
    say zdef_rec
    say
  endif
  nz = subwrd(zdef_rec,2)
  if( do_print = 'yes' )
    say '"zdef" number of levels, nz = 'nz
  endif
  zdef_type = subwrd(zdef_rec,3)
*
*  "mapping method" description taken from 
*  http://iges.org/grads/gadoc/descriptorfile.html#ZDEF
*
  if( zdef_type = levels | zdef_type = LEVELS )
    if( do_print = 'yes' )
      say '"zdef" mapping method is "'zdef_type'"'
    endif
  else
    say
    say '"zdef" mapping method is "'zdef_type'"'
    say '...exiting script'
    say
    exit
  endif
else
  say
  say 'did not find the "zdef" record...'
  say '...nrec 'nrec
  say 'Record: 'rec
  say 'Record word 1: 'rec_word1
  say '...exiting script'
  say
  exit
endif
*
*  read in and scale the "zdef" levels...
*
if( do_print = 'yes' )
  say
  say '"zdef" input levels "zi", and scaled output levels "zo":'
  say
endif
nl = 1
while ( nl <= nz )
  nrec = nrec+1
  res = read(input_ctl) ; rc = sublin(res,1)
    if( rc != 0 )
      say '"read" status = 'rc
      exit
    endif
  rec = sublin(res,2)
  if( do_print = 'yes' )
    say '"input_ctl" "zdef" record: 'rec
  endif
  n=1 ; rec_word = subwrd(rec,n)
  zo_rec = ' '
  while ( rec_word != '' )
    zi = rec_word ; zo = zi*zscale
    if( do_print = 'yes' )
      say 'zi = 'zi', zo = 'zo
    endif
    zo_rec = zo_rec' 'zo
    n = n+1 ; rec_word = subwrd(rec,n)
    nl = nl+1
  endwhile
  rc = write(output_ctl,zo_rec,append)
endwhile
if( do_print = 'yes' )
  say
  say 'read in remainder of "input_ctl" and write out to "output_ctl"...'
  say
endif
nrec = nrec+1
while ( nrec <= nrecs )
  res = read(input_ctl)
  rc = sublin(res,1)
    if( rc != 0 )
      say '"read" status = 'rc
      exit
    endif
  rec = sublin(res,2)
  if( do_print = 'yes' )
    say rec
  endif
  rc = write(output_ctl,rec,append)
  nrec = nrec+1
endwhile

rc = close(input_ctl)
rc = close(output_ctl)

if( do_print = 'yes' )
  say
  say '...leaving "scale_zdef_zlevs"'
  say
endif

return

*************************************************************************