#!/usr/bin/env python
# coding: utf-8
#
# Patrick BROCKMANN - LSCE (Climate and Environment Sciences Laboratory)
#
#
# Updated: 2019/11/13
# #### Load the ferret extension
# In[1]:
get_ipython().run_line_magic('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()
# 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[3]:
get_ipython().run_cell_magic('ferret', '', 'use levitus_climatology \n')
# In[4]:
get_ipython().run_line_magic('ferret_putdata', '--axis_pos (0,1,2,3,4,5) b')
# In[5]:
get_ipython().run_cell_magic('ferret', '', 'set text/font=arial\n\nshow data\nppl color 2, 0, 50, 100, 75\nppl color 3, 100, 50, 0, 75\nplot/thick=3/color myvar1, myvar1[x=@shf:50]\n')
# ###### Second example: 3D array (XYZ)
# Create a dummy 3D array (XY and a Z axis)
# In[6]:
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)
# Please refer to http://ferret.pmel.noaa.gov/Ferret/documentation/pyferret/data-dictionaries/
# In[7]:
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()
# In[8]:
get_ipython().run_line_magic('ferret_putdata', 'data2ferret')
# In[9]:
get_ipython().run_cell_magic('ferret', '', 'show data\nshade myvar2[k=1]\n')
# ###### Third example: 3D array (XYT)
# Create a dummy 3D array (XY and a T axis)
# In[10]:
dataXYT = np.reshape(dataXYZ, (nlons, nlats, 1, dim3))
print(dataXYT.shape)
# In[11]:
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()
# In[12]:
get_ipython().run_line_magic('ferret_putdata', 'data2ferret')
# In[13]:
get_ipython().run_cell_magic('ferret', '', 'show data\nshade myvar3[l=1]\n')