basics
次のサンプルを適当な名前の(例えば smpl_pygrads.py)というファイルにして、走らせてみると、pygradsの基本機能(pythonの中からgradsで図を描く、pythonから走らせているgradsから変数をpython に出す(exportする))という機能が使えることがわかるだろう。なお、今のところpython の変数をgradsに入れる(importする)のはエラーになってします。まあこれはなくてもそれほど問題ないでしょう。
from grads.ganum import GaNum
indir='/data/REANALYSES/JRA55/org_Monthly/anl_p125/'
ctl_fn='anl_p125_hgt.monthly.ctl' # This is ctl file for grib file.
ctl_fn_fl=indir + ctl_fn
ga=GaNum(Bin='grads')
fh=ga.open(ctl_fn_fl)
ga("d hgtprs") # This draw figure in x-window for grads.
hgt=ga.exp("hgtprs") # export variable from grads to python
from matplotlib import pyplot as plt
plt.ion()
plt.contourf(hgt.grid.lon, hgt.grid.lat, hgt) # Draw figure in x-window of python.
hgtsq=hgt*hgt
ga.imp("hgt2",hgtsq)
# This gives error "'IPC extension not available - cannot import!'"
# in climate. This problem can be related to that the grads 2.0 or
# newer does not support user defined function. To use the user defined
# function, we need to use old grads (grads 1.9X) or opengrads.
# But for now, I think that export is enough for our use.
The next sample uses two basic aspects of pygrads, i.e., running grads from python and exporting a variable from grads to python. Unfortunately, we cannot import a variable from python to grads, but I do not think this is a big problem for us.
from grads.ganum import GaNum
indir='/data/REANALYSES/JRA55/org_Monthly/anl_p125/'
ctl_fn='anl_p125_hgt.monthly.ctl' # This is ctl file for grib file.
ctl_fn_fl=indir + ctl_fn
ga=GaNum(Bin='grads')
fh=ga.open(ctl_fn_fl)
ga("d hgtprs") # This draw figure in x-window for grads.
hgt=ga.exp("hgtprs") # export variable from grads to python
from matplotlib import pyplot as plt
plt.ion()
plt.contourf(hgt.grid.lon, hgt.grid.lat, hgt) # Draw figure in x-window of python.
hgtsq=hgt*hgt
ga.imp("hgt2",hgtsq)
# This gives error "'IPC extension not available - cannot import!'"
# in climate. This problem can be related to that the grads 2.0 or
# newer does not support user defined function. To use the user defined
# function, we need to use old grads (grads 1.9X) or opengrads.
# But for now, I think that export is enough for our use.