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")