PyGrADS示例.
首先導入模塊:
from grads import *
from sys import stdout
測試PyGrADS是否正確安裝:
try:
ga = GrADS(Verb=1, Echo=False, Port=False, Window=False,Opts="-c 'q config'")
print(">>> OK <<< start GrADS")
except:
print(">>> NOT OK <<< cannot start GrADS")
嘗試打開一個控制文件,打印文件中的信息:
try:
fh = ga.open("../data/model.ctl")
print(fh.title)
print(' File Id: ', fh.fid)
print(' Dataset type: ', fh.type)
print(' No. of Time steps: ', fh.nt)
print(' No. of Longitudes: ', fh.nx)
print(' No. of Latitudes: ', fh.ny)
print(' No. of Levels: ', fh.nz)
print(' Variable names: ', fh.vars)
print(' Variable levels: ', fh.var_levs)
print(' Variable titles: ', fh.var_titles)
print(">>> OK <<< open CTL file")
except:
print(">>> NOT OK <<< cannot open CTL file")
讀取NetCDF文件時,會自動調用sdfopen:
try:
fh = ga.open("../data/model.nc")
print(fh.title)
print (' File Id: ', fh.fid)
print (' Dataset type: ', fh.type)
print (' No. of Time steps: ', fh.nt)
print (' No. of Longitudes: ', fh.nx)
print (' No. of Latitudes: ', fh.ny)
print (' No. of Levels: ', fh.nz)
print (' Variable names: ', fh.vars)
print (' Variable levels: ', fh.var_levs)
print (' Variable titles: ', fh.var_titles)
print (">>> OK <<< open NetCDF file")
except:
print(">>> NOT OK <<< cannot open NetCDF file")
查詢維度信息(query dims):
try:
qh = ga.query('dims')
print('Current dimensional state: ')
print (' X is '+qh.x_state+' Lon = ',qh.lon,' X = ',qh.x)
print(' Y is '+qh.y_state+' Lat = ',qh.lat,' Y = ',qh.y)
print(' Z is '+qh.z_state+' Lev = ',qh.lev,' Z = ',qh.z)
print(' T is '+qh.t_state+' Time = ',qh.time,' T = ',qh.t)
print(">>> OK <<< query dimensions")
except:
print(">>> NOT OK <<< cannot query dimensions")
查詢文件信息(query file):try:
qh = ga.query('file')
print('Current file state: ')
print(' Title: ', qh.title)
print(' File Id: ', qh.fid)
print(' Description: ', qh.desc)
print(' Binary: ', qh.bin)
print ' Variable Num levels Description')
for (var, nlevels0,nlevels1, desc) in qh.var_info:
print( ' '+var.rjust(8)+' '+str(nlevels0).rjust(10)+str(nlevels1).rjust(10)+' '+desc)
print(">>> OK <<< query file")
except:
print(">>> NOT OK <<< cannot query file")
測試捕獲grads輸出(Line和Word):
try:
ga.cmd("q config")
print("--------------------------------------------------------------")
print " Captured GrADS output: Line interface"
print("--------------------------------------------------------------")
for i in range(1,ga.nLines):
print(ga.rline(i))
print("--------------------------------------------------------------")
print " Captured GrADS output: Word interface"
print("--------------------------------------------------------------")
for i in range(1,ga.nLines):
for j in range(1,20): # 20 is an over estimate, but i is OK
stdout.write(ga.rword(i,j)+' ')
stdout.write('\n')
print(">>> OK <<< rline()/rword() completes")
except:
print(">>> NOT OK <<< rline()/rword() fails")
測試和Python交換變量,即可以將grads空間中的變量導入到python工作空間中,反之亦然:
try:
ts = ga.exp('ts')
print("Ts in Kelvins: ", ts.min(), ts.max())
ts = ts - 273
print("Ts in Celsius: ", ts.min(), ts.max())
ga.imp('tc',ts)
tc = ga.exp('tc')
print("Tc in Celsius: ", tc.min(), tc.max())
print(">>> OK <<< exp()/imp() completes")
except:
print(">>> NOT OK <<< exp()/imp() fails")
執行批處理:
try:
ga("""
set lat 30 60
set lon -80 -50
set t 1 3
query dims
""")
print(">>> OK <<< successfuly ran several commands at one")
except:
print(">>> NOT OK <<< could not run several commands at once")