Patrick BROCKMANN - LSCE (Climate and Environment Sciences Laboratory)
<img align="left" width="40%" src="http://www.lsce.ipsl.fr/Css/img/banniere_LSCE_75.png" >


Updated: 2018/12/10

Load the ferret extension


In [1]:
%load_ext ferretmagic

Put data from python

First example: 1D array

In [2]:
import numpy as np
b = {}
b['name']='myvar1'                         
x=np.linspace(-np.pi*4, np.pi*4, 500)
b['data']=np.sin(x)/x
b.keys()


Out[2]:
['data', 'name']

A dataset must have been opened before putting data to ferret to get list of variables latter.

https://github.com/NOAA-PMEL/PyFerret/issues/64


In [6]:
%%ferret
use levitus_climatology

In [7]:
%ferret_putdata --axis_pos (0,1,2,3,4,5) b


Message: b is now available in ferret as myvar1

In [8]:
%%ferret
set text/font=arial

show data
ppl color 2, 0, 50, 100, 75
ppl color 3, 100, 50, 0, 75
plot/thick=3/color myvar1, myvar1[x=@shf:50]


     currently SET data sets:
    1> /opt/ferret_dsets/data/levitus_climatology.cdf  (default)
 name     title                             I         J         K         L
 TEMP     TEMPERATURE                      1:360     1:180     1:20      ...
 SALT     SALINITY                         1:360     1:180     1:20      ...
 ------ Python Variables ------
 MYVAR1   myvar1                           1:500     ...       ...       ...       ...       ...
 
Second example: 3D array (XYZ)

Create a dummy 3D array (XY and a Z axis)


In [9]:
nlons, nlats, dim3 = (145, 73, 10)

lats = np.linspace(-np.pi / 2, np.pi / 2, nlats)
lons = np.linspace(0, 2 * np.pi, nlons)
lons, lats = np.meshgrid(lons, lats, indexing='ij')

wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)

lats = np.rad2deg(lats)
lons = np.rad2deg(lons)
data2D = wave + mean 

myaxis = np.linspace(1, 1000, dim3)
dataXYZ = np.repeat(np.expand_dims(data2D,axis=-1), dim3, axis=2)

print dataXYZ.shape


(145, 73, 10)

In [10]:
import pyferret
data2ferret = {}
data2ferret['name']='myvar2'   
data2ferret['axis_names']=('lons', 'lats', 'depth')
data2ferret['axis_units']=('degrees_east', 'degrees_north', 'meters')
data2ferret['axis_types']=(
                            pyferret.AXISTYPE_LONGITUDE,
                            pyferret.AXISTYPE_LATITUDE,
                            pyferret.AXISTYPE_LEVEL
                        )
data2ferret['axis_coords']=(lons[:,0], lats[0,:], myaxis[:])
data2ferret['data']=dataXYZ
data2ferret.keys()


Out[10]:
['name', 'axis_names', 'axis_types', 'axis_units', 'axis_coords', 'data']

In [11]:
%ferret_putdata data2ferret


Message: data2ferret is now available in ferret as myvar2

In [12]:
%%ferret
show data
shade myvar2[k=1]


     currently SET data sets:
    1> /opt/ferret_dsets/data/levitus_climatology.cdf  (default)
 name     title                             I         J         K         L
 TEMP     TEMPERATURE                      1:360     1:180     1:20      ...
 SALT     SALINITY                         1:360     1:180     1:20      ...
 ------ Python Variables ------
 MYVAR1   myvar1                           1:500     ...       ...       ...       ...       ...
 MYVAR2   myvar2                           1:145     1:73      1:10      ...       ...       ...
 
Third example: 3D array (XYT)

Create a dummy 3D array (XY and a T axis)


In [13]:
dataXYT = np.reshape(dataXYZ, (nlons, nlats, 1, dim3))
print dataXYT.shape


(145, 73, 1, 10)

In [14]:
import pyferret
data2ferret = {}
data2ferret['name']='myvar3'   
data2ferret['axis_names']=('lons', 'lats', '', 'time')
data2ferret['axis_units']=('degrees_east', 'degrees_north', '', '')
data2ferret['axis_types']=(
                            pyferret.AXISTYPE_LONGITUDE,
                            pyferret.AXISTYPE_LATITUDE,
                            pyferret.AXISTYPE_NORMAL,
                            pyferret.AXISTYPE_ABSTRACT
                        )
data2ferret['axis_coords']=(lons[:,0], lats[0,:], None, None)
data2ferret['data']=dataXYT
data2ferret.keys()


Out[14]:
['name', 'axis_names', 'axis_types', 'axis_units', 'axis_coords', 'data']

In [15]:
%ferret_putdata data2ferret


Message: data2ferret is now available in ferret as myvar3

In [16]:
%%ferret
show data
shade myvar3[l=1]


     currently SET data sets:
    1> /opt/ferret_dsets/data/levitus_climatology.cdf  (default)
 name     title                             I         J         K         L
 TEMP     TEMPERATURE                      1:360     1:180     1:20      ...
 SALT     SALINITY                         1:360     1:180     1:20      ...
 ------ Python Variables ------
 MYVAR1   myvar1                           1:500     ...       ...       ...       ...       ...
 MYVAR2   myvar2                           1:145     1:73      1:10      ...       ...       ...
 MYVAR3   myvar3                           1:145     1:73      ...       1:10      ...       ...
 

In [ ]:


In [ ]: