ascii values into a file...

Stegert stegert at IFM.UNI-HAMBURG.DE
Mon Jul 3 05:21:52 EDT 2006


Dear,
it is aready said that writing to ASCII file has to be done using a script.
However, GrADS as far as I have experienced, does not support fixed
digits after the comma (or does it anyway?).
The result is, that a matrix of values is not column-uniform, but gives
e.g.:
2.45 12.68432 9.532 35.2 112.456
1.44289 4.256 26.5314 134.3455 206.1
which is not very satisfying.
So, I figured out a solution using the "q string" function (giving its
length):
the result will then be of the kind "f14.5":
      2.45         12.68432       9.532        35.2        112.456
      1.44289       4.256        26.5314      134.3455     206.1

The script-part for that is the following:
        'set strsiz 1'
* Important to have strsiz=1 at font = 0 (courier, fixed width)
        say 'Writing data to file....'
        j = ymin
* these "whiles" go through the variable data for
* i=x(0) to x(max) and j = y(0) to y(max)
        while (j <= ymax)
            'set y 'j
            fstr = ''
            i = xmin
            while (i <= xmax)
                'set x 'i
                'd var'
                dat = subwrd(result,4)
* here, undefined data is set to -1
                if (dat = _undef)
                    da0 = '    -1.000000'
                    fstr = fstr' 'da0
                else
                    'q string 'dat
                    lr = subwrd(result,4)
                    lr = lr - 1.5
* first, include space for every n in 10^(6-n) of the data magnitude
* i.e: no space for magnitudes of 10^6 but 6 spaces at 10^0
* for higher values than 10^6 this has to be enhenced
                    if (dat < 100000); fstr = fstr' ';lr = lr+1; endif
                    if (dat < 10000 ); fstr = fstr' ';lr = lr+1; endif
                    if (dat < 1000  ); fstr = fstr' ';lr = lr+1; endif
                    if (dat < 100   ); fstr = fstr' ';lr = lr+1; endif
                    if (dat < 10    ); fstr = fstr' ';lr = lr+1; endif
                    lr = lr - 10
                    fstr = fstr' 'dat
                    ls = 1
* second, include space for every decimal digit below 5
* i.e. no space for 5 digits, 4 spaces at only 1 decimal.
                    while (ls > lr)
                        fstr = fstr' '
                        ls = ls - 1
                    endwhile
                endif
                i = i + 1
            endwhile
* write string to file
            res = write('outputfile.txt',fstr,append)
            j = j - 1
        endwhile
        st1 = close('outputfile.txt')

May this can help someone.
Best Regards,
Christoph


Cottet, Loic (IFPRI) wrote:

>Hi,
>
>To output ascii values into a file, I tried to do this way:
>
>filename='praverage3'
>'set gxout print'
>'d var'
>rc=write(filename,result)
>
>But from the first step, it has not worked...
>
>
>ga-> filename='praverage3'
>Syntax Error:  Expected operand or '('
>  Error ocurred at column 1
>DEFINE error:  Invalid expression.
>ga-> filename=praverage3
>Syntax Error:  Invalid Operand
>  'praverage3' not a variable or function name
>  Error ocurred at column 1
>DEFINE error:  Invalid expression.
>ga-> filename = praverage3
>Syntax Error:  Invalid Operand
>  'praverage3' not a variable or function name
>  Error ocurred at column 1
>DEFINE error:  Invalid expression.
>ga-> set filename = praverage3
>SET error: Invalid operand
>  Operand = filename = praverage3
>
>
>I tried other ways without success...
>Can anyone help me out??
>I would really apprecaite your help,
>
>Thansk a lot,
>
>LCottet.
>
>
--
Christoph Stegert
Intitute  for Oceanography - (ZMK/ZMAW alliance member)
University of Hamburg, GER - Ecological Modelling Group
Bundesstr.53 20146 Hamburg - +49-40-42838-7486 - Rm 348



More information about the gradsusr mailing list