PyFerret can also be used as a Python module. First we show how to use the pyferret module as any other Python module. Then, in Option 2 and Option 3, we show an easier way of doing this, using the pyferret script to start Python and PyFerret, but stay in (or return to) Python to work as a Python programmer.
Using the pyferret module in Python - Option 1
As mentioned in the previous section, some environment variables for Ferret need to defined using the ferret_paths script. The Pyferret version of the ferret_paths script now (as of PyFerret version 1.1.0) defines the PYTHONPATH and LD_LIBRARY_DIR environment variables needed by PyFerret. As before, this only needs to be done once in your terminal and may be automatically done in your terminal start-up scripts. For C-shell users, the commands would look something like:
% source /path/to/PyFerret/ferret_paths.csh
For Bourne-shell users, the commands would instead look something like:
$ . /path/to/PyFerret/ferret_paths.sh
Then in Python, after importing the pyferret module, you just need to call either the pyferret.init or pyferret.start function to configure and start the Ferret engine in PyFerret.
% python2.6
Python 2.6.6 (r266:84292, May 22 2015, 08:34:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyferret
>>> pyferret.start(quiet=True)
True
>>> (errval, errmsg) = pyferret.run('use coads_climatology')
>>> sstdict = pyferret.getdata('sst', False)
>>> sstdict.keys()
['axis_coords', 'axis_types', 'data_unit', 'axis_units', 'title', 'axis_names', 'missing_value', 'data']
>>> sstdata = sstdict['data']
>>> type(sstdata)
<type 'numpy.ndarray'>
>>> sstdata.shape
(180, 90, 1, 12)
>>> import numpy
>>> mask = numpy.abs(sstdata - sstdict['missing_value']) < 1.0E-5
>>> sstdata[mask] = -10.0
>>> sstdict['name'] = 'SSTCopy'
>>> sstdict['title'] = 'SST w/ miss vals set to -10'
>>> pyferret.putdata(sstdict)
>>> (errval, errmsg) = pyferret.run()
yes? show data
currently SET data sets:
1> /home/users/ksmith/Datasets/data/coads_climatology.cdf (default)
name title I J K L
SST SEA SURFACE TEMPERATURE 1:180 1:90 ... 1:12
AIRT AIR TEMPERATURE 1:180 1:90 ... 1:12
SPEH SPECIFIC HUMIDITY 1:180 1:90 ... 1:12
WSPD WIND SPEED 1:180 1:90 ... 1:12
UWND ZONAL WIND 1:180 1:90 ... 1:12
VWND MERIDIONAL WIND 1:180 1:90 ... 1:12
SLP SEA LEVEL PRESSURE 1:180 1:90 ... 1:12
------ Python Variables ------
SSTCOPY SST w/ miss vals set to -10 1:180 1:90 ... 1:12
yes? list sstcopy[X=125W:115W,Y=65S:55S,T=17-MAR]
VARIABLE : SST w/ miss vals set to -10 (Deg C)
FILENAME : coads_climatology.cdf
FILEPATH : /home/users/ksmith/Datasets/data/
SUBSET : 6 by 6 points (LONGITUDE-LATITUDE)
TIME : 17-MAR 02:58
125W 123W 121W 119W 117W 115W
108 109 110 111 112 113
55S / 18: 5.60 5.79 -10.00 5.60 -10.00 -10.00
57S / 17: -10.00 -10.00 -10.00 -10.00 5.00 5.00
59S / 16: -10.00 -10.00 -10.00 -10.00 -10.00 -10.00
61S / 15: -10.00 -10.00 -10.00 -10.00 -10.00 -10.00
63S / 14: 1.83 -10.00 -10.00 -10.00 -10.00 -10.00
65S / 13: 0.75 1.65 2.47 1.65 2.30 2.15
yes? exit /topython
>>>
Note:
- The Python help function provides lots of useful information on the pyferret module, using help(pyferret), and for pyferret functions; for example, help(pyferret.run).
- Calling pyferret.run without any arguments take you into Ferret's command-line interface. Using the Ferret command EXIT /TOPYTHON returns back to the Python prompt.
Using the pyferret module in Python - Option 2
Running pyferret -python will start Python and the pyferret module appropriately, but remain at the Python prompt instead of going into Ferret's command-line interface. Although you still need the environment variables set using ferret_paths, you do not have to worry about importing the pyferret module and starting the Ferret engine.
% pyferret -python -quiet
>>> pyferret.run('use coads_climatology')
(3, '')
>>>
Using the pyferret module in Python - Option 3
Or you can just run pyferret to start out in Ferret's command-line interface, then use the Ferret command EXIT /TOPYTHON to return to the Python prompt when desired. Again, you will still need the Ferret environment variables set using ferret_paths, but you do not have to worry about importing the pyferret module and starting the Ferret engine.
% pyferret -quiet
yes? use coads_climatology
yes? exit /topy
>>>