Script for Grads

All other topics about postprocessing model data (GrADS and other software), about other numerical weather prediction software (including WRF-NMM and WRF-ARW discussion unrelated to UEMS/WRF EMS), and general meteorology talk go in this forum.
Post Reply
wrf_model
Posts: 55
Joined: Wed Feb 17, 2010 9:38 pm

Script for Grads

Post by wrf_model » Tue Feb 16, 2016 1:07 pm

Dears,

I try to create text file for 2 meter temperature with grads for all grid of GFS and for all forecast times. The problem is that i have loop for all grid points, but i have not loop for forecast times. In other words i have result for one forecast time in my text file but i want all forecast times.

Here is my script:

'open /root/gfs/ecm.ctl'

outfile = '/root/Desktop/NCEP.txt'

code=write(''outfile,'date hour LON LAT TEMP')


nf=41
t=0
while (t<=nf)
'set t 't
'q time'


timeinfo=result

year=substr(timeinfo,16,4)

month=substr(timeinfo,13,3)
if (month="JAN"); month="01"; endif
if (month="FEB"); month="02"; endif
if (month="MAR"); month="03"; endif
if (month="APR"); month="04"; endif
if (month="MAY"); month="05"; endif
if (month="JUN"); month="06"; endif
if (month="JUL"); month="07"; endif
if (month="AUG"); month="08"; endif
if (month="SEP"); month="09"; endif
if (month="OCT"); month="10"; endif
if (month="NOV"); month="11"; endif
if (month="DEC"); month="12"; endif

day=substr(timeinfo,11,2)

hour=substr(timeinfo,8,2)

dday=substr(timeinfo,38,3)
if (dday="Mon"); dday="Monday"; endif
if (dday="Tue"); dday="Tuesday"; endif
if (dday="Wed"); dday="Wednesday"; endif
if (dday="Thu"); dday="Thursday"; endif
if (dday="Fri"); dday="Friday"; endif
if (dday="Sat"); dday="Saturday"; endif
if (dday="Sun"); dday="Sunday"; endif

'q dims'

xline=sublin(result,2) ;* 2nd line
yline=sublin(result,3) ;* 3rd line
xmax=subwrd(xline,13) ;*13th word on xline
ymax=subwrd(yline,13) ;*13th word on yline

*say 'X grid-points: 'xmax
*say 'Y grid-points: 'ymax
y=1

while(y<=ymax)

x=1
while(x<=xmax)



'set x 'x
'set y 'y

'set gxout stat'
'd tmp2m-273.15'
regel = sublin(result,8)
res_t2m1=subwrd(regel,4)
res_t2m=math_nint(res_t2m1)


* Get Lat/Lon Data

'q dims'
lons=sublin(result,2)
lats=sublin(result,3)
lon=subwrd(lons,6)
lat=subwrd(lats,6)



* Save data to file
* Note the "append", so to add to the file instead of overwriting it

code=write(''outfile, day'-'month'-'year' 'hour' 'lon' 'lat' 'res_t2m'',append)

x=x+1
endwhile
y=y+1
endwhile




t=t+1
endwhile


'quit'


Thank you for your time and for your help!

cheers

meteoadriatic
Posts: 1543
Joined: Wed Aug 19, 2009 10:05 am

Re: Script for Grads

Post by meteoadriatic » Tue Feb 16, 2016 1:35 pm

Hello,

1) loop.sh:

Code: Select all

#!/bin/bash

i=1
nf=12
while [ $i -lt $nf ]
do
	grads -lbc "tmp_all_points.gs $i"
	i=$(( $i + 1 ))
done
2) tmp_all_points.gs:

Code: Select all

function main(args)
i=subwrd(args,1)
'open nmbprs_d01.grib2.ctl'
outfile='temps.txt'
code=write(''outfile,'date hour LON LAT TEMP',append)

'set t 'i

'q time'

timeinfo=result
year=substr(timeinfo,16,4)
month=substr(timeinfo,13,3)
if (month="JAN"); month="01"; endif
if (month="FEB"); month="02"; endif
if (month="MAR"); month="03"; endif
if (month="APR"); month="04"; endif
if (month="MAY"); month="05"; endif
if (month="JUN"); month="06"; endif
if (month="JUL"); month="07"; endif
if (month="AUG"); month="08"; endif
if (month="SEP"); month="09"; endif
if (month="OCT"); month="10"; endif
if (month="NOV"); month="11"; endif
if (month="DEC"); month="12"; endif

day=substr(timeinfo,11,2)
hour=substr(timeinfo,8,2)
dday=substr(timeinfo,38,3)
if (dday="Mon"); dday="Monday"; endif
if (dday="Tue"); dday="Tuesday"; endif
if (dday="Wed"); dday="Wednesday"; endif
if (dday="Thu"); dday="Thursday"; endif
if (dday="Fri"); dday="Friday"; endif
if (dday="Sat"); dday="Saturday"; endif
if (dday="Sun"); dday="Sunday"; endif

'q dims'
xline=sublin(result,2) ;* 2nd line
yline=sublin(result,3) ;* 3rd line
xmax=subwrd(xline,13) ;*13th word on xline
ymax=subwrd(yline,13) ;*13th word on yline

*say 'X grid-points: 'xmax
*say 'Y grid-points: 'ymax
y=1

while(y<=ymax)

x=1
while(x<=xmax)

'set x 'x
'set y 'y

'set gxout stat'
'd tmp2m-273.15'
regel = sublin(result,9)
res_t2m1=subwrd(regel,4)
res_t2m=math_nint(res_t2m1)


* Get Lat/Lon Data
'q dims'
lons=sublin(result,2)
lats=sublin(result,3)
lon=subwrd(lons,6)
lat=subwrd(lats,6)

* Save data to file
* Note the "append", so to add to the file instead of overwriting it
code=write(''outfile, day'-'month'-'year' 'hour' 'lon' 'lat' 'res_t2m'',append)

x=x+1
endwhile
y=y+1
endwhile

'quit'
return

Try that. Remember to replace paths/filenames, I left from my test. And don't work as root. It is dangerous ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests