reinit
df   = 'Insert/your/ctlfile/here'
'open 'df
var  = 'p'
levels = getlevs(df,var)

say _itmax ' '
say _ixmax ' '_ilonmin ' ' _dlon
say _iymax ' '_ilatmin ' ' _dlat
it = 1
while (it <= _itmax)
  'set t 'it
  iy = 1
  while (iy <= _iymax)
    'set y 'iy
    ix = 1
    while (ix <= _ixmax)
      'set x 'ix
      ilon = _ilonmin + ix*_dlon
      ilat = _ilatmin + iy*_dlat
*     _ilonmin and _dlon (and corresponding lats)
*     are those declared in your CTL
      'd 'var
      var2 = subwrd(result,4)
*     to skip undefined values use this:
      if (var2 < _undef) | (var2 > _undef)
        output = ilat'  'ilon'  'it'  'var2
*       to save every time step in a single file:
*       (else leave out the "it")
        outfile = 'output_'it
        res = write(outfile,output,append)
      endif
      ix = ix + 1
    endwhile
    iy = iy + 1
  endwhile
  it = it + 1
endwhile
st1 = close('output.txt')


* ----- 1) read information from descriptor file ------------
*(basic code received from k. meier-fleischer, dkrz, hamburg)
function getlevs(df,svar.1)

   file=read(df)
   stat = sublin(file,1)
   if(stat != 0)
      say 'ERROR reading the descriptor file'
      say 'Check path: 'df
   endif
   i=1
   while (i < 30)
      line = sublin(file,2)
      if (subwrd(line,1) = 'undef'); break; endif
      i=i+1
      file=read(df)
   endwhile
   _undef = subwrd(line,2)
   i=i+1
   while (i < 30)
      line = sublin(file,2)
      if (subwrd(line,1) = 'xdef'); break; endif
      i=i+1
      file=read(df)
   endwhile
   _ixmax = subwrd(line,2)
   _ilonmin = subwrd(line,4)
   _dlon = subwrd(line,5)
   i=i+1
   while (i < 30)
      line = sublin(file,2)
      if (subwrd(line,1) = 'ydef'); break; endif
      i=i+1
      file=read(df)
   endwhile
   _iymax = subwrd(line,2)
   _ilatmin = subwrd(line,4)
   _dlat = subwrd(line,5)
   i=i+1
   while (i < 30)
      line = sublin(file,2)
      if (subwrd(line,1) = 'zdef'); break; endif
      i=i+1
      file=read(df)
   endwhile
   _levmax = subwrd(line,2)
   levels = ''
   j=1
   while ( j <= subwrd(line,2) )
     levels = levels' 'subwrd(line,j+3)
     j=j+1
   endwhile
   i=i+1
   while (i < 30)
      line = sublin(file,2)
      if (subwrd(line,1) = 'tdef'); break; endif
      i=i+1
      file=read(df)
   endwhile
   _itmax = subwrd(line,2)
   yr = subwrd(line,4)
   _simyr = substr(yr,6,4)
   i=i+1
   while (i < 50)
      line = sublin(file,2)
      if (subwrd(line,1) = 'ENDVARS')
          say 'ERROR: State variable 'var' not found.'
      endif
      if (subwrd(line,1) = var); break; endif
      i=i+1
      file=read(df)
   endwhile
   j     = 4
   _tit = ''
   tit2  = subwrd(line,j)
   while (tit2 != '')
      _tit  = _tit''tit2' '
      j     = j+1
      tit2  = subwrd(line,j)
   endwhile
return(levels)

**