- 积分
- 42918
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2012-8-28
- 最后登录
- 1970-1-1
|
发表于 2017-7-26 19:15:24
|
显示全部楼层
set fwrite命令是用来输出二进制数据的,在这怎么用,根本就不对的。你自己多去官网看看例子吧
Output Gridded Data in ASCII Format
1. GrADS can create ascii output for any display with the 'set gxout print' option. The formatting of the output is controlled with the 'set prnopts' command. The output is printed to the command window, but it is also stored in the internal variable 'result' inside a script, where it can be easily output to a file. Below is a simple script example to write formatted ascii output to a file. Note that the choice of writing 72 numbers per line is based on the X dimension of the grid being displayed.
'open /data/grib/model/model.ctl'
'set x 1 72'
outfile = 'my_ascii_file.txt'
'set gxout print'
'set prnopts %7.3f 72 1'
'd ts'
rc = write(outfile,result)
The beginning of the file my_ascii_file.txt looks like this:
Printing Grid -- 3312 Values -- Undef = -9.99e+08
258.493 258.493 258.493 258.493 258.493 258.493 258.493 258.493 258.493 258.493 258.493 <etc.>
2. In the above example, note that the first line of the output file will contain some diagnostic information about the number of values written and the undef value. If you want to omit that line and write out just the ascii data, then use this slightly more complicated version of the script which skips over the first line of diagnostic info. Two other changes in this example are in the formatting (the output will be comma delimited for importing into a spreadsheet) and the displayed variable, which is now an expression instead of a variable name. It is not necessary to 'define' the expression before displaying it.
'open /data/grib/model/model.ctl'
'set x 1 72'
outfile='my_ascii_file.txt'
'!/bin/rm -f 'outfile
'set gxout print'
'set prnopts %g, 72 0'
'd ts-273.15'
i=1
while (1)
line = sublin(result,i)
if (line = ''); break; endif
if (i>1)
rc = write(outfile,line,append)
endif
i=i+1
endwhile
Now the beginning of the output file my_ascii_file.txt looks like this:
-14.6567,-14.6567,-14.6567,-14.6567,-14.6567,-14.6567,-14.6567,-14.6567,-14.6567,<etc.>
3. If you want to write out the grid information along with data from more than one variable, then you must save the result from the display of each variable/expression (including longitude and latitude), parse each data point individually, construct the line of ascii output, and then write it out. Here is an example the writes out longitude, latitude, surface temperature, surface pressure, and precipitation:
'open /data/grib/model/model.ctl'
'set x 1 72'
outfile='my_ascii_file.txt'
'!/bin/rm -f 'outfile
'set gxout print'
fmt='%8.3f'
numcols=72
'set prnopts 'fmt' 'numcols' 1'
'd lon'
lon_data = result
'd lat'
lat_data = result
'd ts'
v1_data = result
'd ps'
v2_data = result
'd p'
v3_data = result
i=1
while (1)
lons = sublin(lon_data,i)
lats = sublin(lat_data,i)
line1 = sublin(v1_data,i)
line2 = sublin(v2_data,i)
line3 = sublin(v3_data,i)
if (lons='' | lats='' | line1='' | line2='' | line3=''); break; endif
if (i>1)
j=1
while (j<=numcols)
str = subwrd(lons,j); lon = math_format(fmt,str)
str = subwrd(lats,j); lat = math_format(fmt,str)
str = subwrd(line1,j); v1 = math_format(fmt,str)
str = subwrd(line2,j); v2 = math_format(fmt,str)
str = subwrd(line3,j); v3 = math_format(fmt,str)
record = lon' 'lat' 'v1' 'v2' 'v3
rc = write(outfile,record,append)
j=j+1
endwhile
endif
i=i+1
endwhile
Now the head of the output file my_ascii_file.txt looks like this:
0.000 -90.000 258.493 669.911 0.000
5.000 -90.000 258.493 669.911 0.000
10.000 -90.000 258.493 669.911 0.000
15.000 -90.000 258.493 669.911 0.000
20.000 -90.000 258.493 669.911 0.000
25.000 -90.000 258.493 669.911 0.000
30.000 -90.000 258.493 669.911 0.000
35.000 -90.000 258.493 669.911 0.000
40.000 -90.000 258.493 669.911 0.000 |
|