[gradsusr] Writing a text output from Grads with header information

Muhammad Rahiz muhammad.rahiz at ouce.ox.ac.uk
Tue Mar 1 19:09:21 EST 2011


Dear Felix,

The attached script may do what you want. What it does is to draw the 
values of lon-lat from a text file, and for each lon-lat value, it 
extracts value of variable at each time step and appends to an output txt 
file.

The output file would contain the following (without headers);

timestep 	lat 	lon 	var
x.1.x 		30.0	70	2.3
...
x.120.x		30.0	70	2.1
x.1.x		35.0	69.1	1.2
...
x.120.x		35.0	69.1	1.3

If you want to extract the timesteps, grep it;

> grep "x.1.x" output.txt > timestep.1.txt
> grep "x.120.x" output.txt > timestep.120.txt

or in a shell script;

#!/bin/bash

for i in `seq 1 120`; do # where 120 = no. of timesteps
  grep "x.'$i'.x" output.txt > timestep.$i.txt
done

I've yet to test the script with multiple files according to 
your file specifications as I did not manage to download the files from 
the link you gave. Nonethless, this wouldn't be a problem as you can make 
the same "while-endwhile" loop to read in the files as I did for 
coordinates.txt. Or easier, concantenate the files.

Do tell us how it goes...

Muhammad Rahiz
Researcher & DPhil Candidate (Climate Systems & Policy)
School of Geography & the Environment
University of Oxford

On Tue, 1 Mar 2011, Felix Mutua wrote:

> Hi Muhammad,
> I'm using TRMM 3B42 Rainfall data sample on this link.. The data is Netcdf
> with each 3hrs as a different file..I.e 3*8*365 files(=timesteps)in a
> year...
> 
> I was thinking of doing a shell script to open and run the grads script for
> all the time steps producing the text output for
> some modelling application.. I'm running Opengrads in Windows and also I
> have access to a Unix server-based one..
> 
> Thanks and Regards
> 
> On Tue, Mar 1, 2011 at 6:32 PM, Muhammad Rahiz
> <muhammad.rahiz at ouce.ox.ac.uk> wrote:
>       Hi Felix,
>
>       At each lon & lat, you want to extract rainfall at every time
>       step i.e. 3 hours? Is that what you want to do? If yes, then
>       1. how many time steps are you dealing with?
>       2. what is the format of your file i.e. ctl or netcdf? If ctl,
>       then I need two samples of the filename.
>       3. are you using GrADS in windows or linux?
>
>       Off the top of my head, what you need is a double loop. If you
>       could give me the details above, then I could modify a similar
>       script that I have.
>
>       --
>       Muhammad Rahiz
>       Researcher & DPhil Candidate (Climate Systems & Policy)
>       School of Geography & the Environment
>       University of Oxford
>
>       On Tue, 1 Mar 2011, Felix Mutua wrote:
>
>       Thanks Muhammad,
>       I've looked at it before...But my challenge is that I have
>       to do this for
>       many files  (TRMM 3 hrly precipitation data) for one
>       year..Is there a way I
>       can automate or just get the formatted output in Grads?
>
>       Thanks
>
>       Regards
>
>       On Tue, Mar 1, 2011 at 5:28 PM, Muhammad Rahiz
>       <muhammad.rahiz at ouce.ox.ac.uk> wrote:
>      You need the fprintf.gs script, availableathttp://cookbooks.opengrads.org/index.php?title=Recipe-002:_Saving_GrADS_v
> ar
>      iable_data_to_a_text_file.
> 
> 
>      Muhammad Rahiz
>      Researcher & DPhil Candidate (Climate Systems & Policy)
>      School of Geography & the Environment
>      University of Oxford
> 
> 
> On Tue, 1 Mar 2011, Felix Mutua wrote:
> 
>      Hi All, Could someone kindly assist..I'm trying to export
>      a variable into an
>      text output in this format ( with header)
> 
>      Lat                            Lon               Pre
>      ...........................................................
>      1.5                           34.5             4.56
>      2.0                           34.0             3.46
>      ..                              ....               ......
>      ..                              ....               ......
> 
>      For a specified period...
> 
>      Any help greatly appreciated
> 
> 
>      Regards
> 
>      Felix 
> 
> 
> 
> 
> _______________________________________________
> gradsusr mailing list
> gradsusr at gradsusr.org
> http://gradsusr.org/mailman/listinfo/gradsusr
> 
> 
> 
> 
> 
> 
> --
> _________________________________________________________
> Felix Mutua
> School of Engineering, The University of Tokyo
> River Engineering and Environment Laboratory
> 7-3-1 Hongo Bunkyo-ku, Tokyo 113-8656, Japan
> Phone +81-80-3553-0339,E-mail: Felix.mutua at gmail.com,
> f-mutua at hydra.t.u-tokyo.ac.jp
> Skype: Felix.mutua
> 
> 
>
-------------- next part --------------
'sdfopen xxx.nc'
'q file'

* where v1 = variable name
v0 = sublin(result,7)
v1 = subwrd(v0,1)

* where d1 = no. of timesteps
d0 = sublin(result,5)
d1 = subwrd(d0,12)

* CHANGE ttot!
* where ttot = no. of lon&lat (or simply no. of rows in coordinates.txt)
ttot = 2
tx = 1
while(tx<=ttot)
fname ='coordinates.txt'
  rec = read(fname)
  IO = sublin(rec,1)
   if(IO = 1)
    say fname' > File open error'
    'quit'
   endif
  if(IO = 0)
    t1 = subwrd(rec,2)
    t2 = subwrd(rec,3)
   endif

* where timT = no. of timesteps = d1
timT = d1
tim = 1
while(tim<=timT)

'set t 'tim''
'set lat 't1''
'set lon 't2''
say ''
say 'Lat: 't1' - Lon: 't2' - Timestep : 'tim''

'd 'v1''
r0 = subwrd(result,4)

hh = 'timestep lat lon value'
res = write("output.txt",hh,append)
res = close("output.txt")

nn = 'x.'tim'.x 't1' 't2' 'r0''
res = write("output.txt",nn,append)
res = close("output.txt")

tim = tim + 1
endwhile
tx = tx +1
endwhile

say ''
'quit'
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: coordinates.txt
Url: http://gradsusr.org/pipermail/gradsusr/attachments/20110302/562b6ce9/attachment-0003.txt 


More information about the gradsusr mailing list