* To generate ascii data from grads to use in applications like IGOR PRO
*
* Prince XAVIER (17 Nov 2004, LMD, Paris)
*
function printascii(args)

if(args='')
say ''
say 'No arguments found.'
say ''
say 'Syntax is '
say '----------------------------------'
say 'printascii filename variable [z t]'
say '----------------------------------'
say ''
say 'z(or Z) and t(or T) may be switched on in case of 3 or 4 dimension data.'
say 'If not switched on, no information on level and time will be printed.'
say ''
say 'Examples:'
say 'ga-> printascii rain.ascii rain'
say 'ga-> printascii rain.ascii rain z'
say 'ga-> printascii rain.ascii rain t'
say 'ga-> printascii rain.ascii rain z t'
say ''
say ''
return
endif

_fname = subwrd(args,1)
_var = subwrd(args,2)
arg3 = subwrd(args,3)
arg4 = subwrd(args,4)

say ''

zflag = 0
tflag = 0
if(arg3=z|arg3=Z)
say 'Level information requested.'
zflag = 1
endif
if(arg3=t|arg3=T)
say 'Time information requested.'
tflag = 1
endif

if(arg4=z|arg4=Z)
say 'Level information requested.'
zflag = 1
endif
if(arg4=t|arg4=T)
say 'Time information requested.'
tflag = 1
endif


gxstat()
*say _xs' '_xe' '_ys' '_ye' '_zs' '_ze' '_ts' '_te
'set x ' _xs' '_xe
'set y ' _ys' '_ye
gxstat()
if((_xe-_xs)=0)
_lonint = 1
else
_lonint = (_lone-_lons)/(_xe-_xs)
endif
if((_ye-_ys)=0)
_latint = 1
else
_latint = (_late-_lats)/(_ye-_ys)
endif
*say _lonint' '_latint

_tint = 1
_zint = 1
_yint = 1
_xint = 1

_tt = 1
while(_tt<=_te)
'set t '_tt

 _zz = 1
 while(_zz<=_ze)
 'set z '_zz

   _yy = 1
   while(_yy<=_ye)
   'set y '_yy

    _xx = 1
    while(_xx<=_xe)
    'set x ' _xx
    'd '_var
    val = subwrd(result,4)
*    say val
     'query dims'
      dum = sublin(result,2)
      alon = subwrd(dum,6)
      dum = sublin(result,3)
      alat = subwrd(dum,6)
      dum = sublin(result,4)
      alev = subwrd(dum,6)
      dum = sublin(result,5)
      atim = subwrd(dum,6)



       if (zflag = 1 & tflag = 1)
        rc = write(_fname, atim' 'alev' 'alon' 'alat' 'val)
*        say atim' 'alev' 'alon' 'alat' 'val
       endif
       if (zflag = 0 & tflag = 1)
        rc = write(_fname, atim' 'alon' 'alat' 'val)
*        say atim' 'alon' 'alat' 'val
       endif
       if (zflag = 1 & tflag = 0)
        rc = write(_fname, alev' 'alon' 'alat' 'val)
*        say alev' 'alon' 'alat' 'val
        endif
       if (zflag = 0 & tflag = 0)
        rc = write(_fname, alon' 'alat' 'val)
*        say alon' 'alat' 'val
       endif

    _xx = _xx + _xint
    endwhile

   _yy = _yy + _yint
   endwhile

 _zz = _zz + _zint
 endwhile

_tt = _tt + _tint
endwhile

say 'ASCII output written to '_fname'.'
say ' '
rc = close(_fname)



return


function gxstat()
'q dims'
dinf = result
lx = sublin(dinf,2)
ly = sublin(dinf,3)
lz = sublin(dinf,4)
lt = sublin(dinf,5)
if ( subwrd(lx,7) = 'to')
  _lons = subwrd(lx,6)
  _lone = subwrd(lx,8)
  _xs = subwrd(lx,11)
  _xe = subwrd(lx,13)
  _xs = math_int(_xs)
  _xe = math_int(_xe)
else
  _lons = subwrd(lx,6)
  _lone = subwrd(lx,6)
  _xs = subwrd(lx,9)
  _xe = subwrd(lx,9)
  _xs = math_int(_xs)
  _xe = math_int(_xe)
endif
if ( subwrd(ly,7) = 'to')
  _lats = subwrd(ly,6)
  _late = subwrd(ly,8)
  _ys = subwrd(ly,11)
  _ye = subwrd(ly,13)
  _ys = math_int(_ys)
  _ye = math_int(_ye)
else
  _lats = subwrd(ly,6)
  _late = subwrd(ly,6)
  _ys = subwrd(ly,9)
  _ye = subwrd(ly,9)
  _ys = math_int(_ys)
  _ye = math_int(_ye)
endif
if ( subwrd(lz,7) = 'to')
  _levs = subwrd(lz,6)
  _leve = subwrd(lz,8)
  _zs = subwrd(lz,11)
  _ze = subwrd(lz,13)
else
  _levs = subwrd(lz,6)
  _leve = subwrd(lz,6)
  _zs = subwrd(lz,9)
  _ze = subwrd(lz,9)
endif
if ( subwrd(lt,7) = 'to')
  _tims = subwrd(lt,6)
  _time = subwrd(lt,8)
  _ts = subwrd(lt,11)
  _te = subwrd(lt,13)
else
  _tims = subwrd(lt,6)
  _time = subwrd(lt,6)
  _ts = subwrd(lt,9)
  _te = subwrd(lt,9)
endif
return
