{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "Patrick BROCKMANN - LSCE (Climate and Environment Sciences Laboratory)
\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Updated: 2019/11/13" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load the ferret extension" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext ferretmagic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Put data from python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###### First example: 1D array" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 'data'])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "b = {}\n", "b['name']='myvar1' \n", "x=np.linspace(-np.pi*4, np.pi*4, 500)\n", "b['data']=np.sin(x)/x\n", "b.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A dataset must have been opened before putting data to ferret to get list of variables latter.\n", "\n", "https://github.com/NOAA-PMEL/PyFerret/issues/64" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%%ferret\n", "use levitus_climatology " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Message: b is now available in ferret as myvar1
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%ferret_putdata --axis_pos (0,1,2,3,4,5) b" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
     currently SET data sets:\n",
       "    1> /opt/ferret_dsets/data/levitus_climatology.cdf  (default)\n",
       " name     title                             I         J         K         L\n",
       " TEMP     TEMPERATURE                      1:360     1:180     1:20      ...\n",
       " SALT     SALINITY                         1:360     1:180     1:20      ...\n",
       " ------ Python Variables ------\n",
       " MYVAR1   myvar1                           1:500     ...       ...       ...       ...       ...\n",
       " \n",
       "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%ferret\n", "set text/font=arial\n", "\n", "show data\n", "ppl color 2, 0, 50, 100, 75\n", "ppl color 3, 100, 50, 0, 75\n", "plot/thick=3/color myvar1, myvar1[x=@shf:50]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###### Second example: 3D array (XYZ)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a dummy 3D array (XY and a Z axis)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(145, 73, 10)\n" ] } ], "source": [ "nlons, nlats, dim3 = (145, 73, 10)\n", "\n", "lats = np.linspace(-np.pi / 2, np.pi / 2, nlats)\n", "lons = np.linspace(0, 2 * np.pi, nlons)\n", "lons, lats = np.meshgrid(lons, lats, indexing='ij')\n", "\n", "wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)\n", "mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)\n", "\n", "lats = np.rad2deg(lats)\n", "lons = np.rad2deg(lons)\n", "data2D = wave + mean \n", "\n", "myaxis = np.linspace(1, 1000, dim3)\n", "dataXYZ = np.repeat(np.expand_dims(data2D,axis=-1), dim3, axis=2)\n", "\n", "print(dataXYZ.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please refer to http://ferret.pmel.noaa.gov/Ferret/documentation/pyferret/data-dictionaries/" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 'axis_names', 'axis_units', 'axis_types', 'axis_coords', 'data'])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyferret\n", "data2ferret = {}\n", "data2ferret['name']='myvar2' \n", "data2ferret['axis_names']=('lons', 'lats', 'depth')\n", "data2ferret['axis_units']=('degrees_east', 'degrees_north', 'meters')\n", "data2ferret['axis_types']=(\n", " pyferret.AXISTYPE_LONGITUDE,\n", " pyferret.AXISTYPE_LATITUDE,\n", " pyferret.AXISTYPE_LEVEL\n", " )\n", "data2ferret['axis_coords']=(lons[:,0], lats[0,:], myaxis[:])\n", "data2ferret['data']=dataXYZ\n", "data2ferret.keys()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Message: data2ferret is now available in ferret as myvar2
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%ferret_putdata data2ferret" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
     currently SET data sets:\n",
       "    1> /opt/ferret_dsets/data/levitus_climatology.cdf  (default)\n",
       " name     title                             I         J         K         L\n",
       " TEMP     TEMPERATURE                      1:360     1:180     1:20      ...\n",
       " SALT     SALINITY                         1:360     1:180     1:20      ...\n",
       " ------ Python Variables ------\n",
       " MYVAR1   myvar1                           1:500     ...       ...       ...       ...       ...\n",
       " MYVAR2   myvar2                           1:145     1:73      1:10      ...       ...       ...\n",
       " \n",
       "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%ferret\n", "show data\n", "shade myvar2[k=1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###### Third example: 3D array (XYT)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a dummy 3D array (XY and a T axis)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(145, 73, 1, 10)\n" ] } ], "source": [ "dataXYT = np.reshape(dataXYZ, (nlons, nlats, 1, dim3))\n", "print(dataXYT.shape)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['name', 'axis_names', 'axis_units', 'axis_types', 'axis_coords', 'data'])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyferret\n", "data2ferret = {}\n", "data2ferret['name']='myvar3' \n", "data2ferret['axis_names']=('lons', 'lats', '', 'time')\n", "data2ferret['axis_units']=('degrees_east', 'degrees_north', '', '')\n", "data2ferret['axis_types']=(\n", " pyferret.AXISTYPE_LONGITUDE,\n", " pyferret.AXISTYPE_LATITUDE,\n", " pyferret.AXISTYPE_NORMAL,\n", " pyferret.AXISTYPE_ABSTRACT\n", " )\n", "data2ferret['axis_coords']=(lons[:,0], lats[0,:], None, None)\n", "data2ferret['data']=dataXYT\n", "data2ferret.keys()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Message: data2ferret is now available in ferret as myvar3
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%ferret_putdata data2ferret" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
     currently SET data sets:\n",
       "    1> /opt/ferret_dsets/data/levitus_climatology.cdf  (default)\n",
       " name     title                             I         J         K         L\n",
       " TEMP     TEMPERATURE                      1:360     1:180     1:20      ...\n",
       " SALT     SALINITY                         1:360     1:180     1:20      ...\n",
       " ------ Python Variables ------\n",
       " MYVAR1   myvar1                           1:500     ...       ...       ...       ...       ...\n",
       " MYVAR2   myvar2                           1:145     1:73      1:10      ...       ...       ...\n",
       " MYVAR3   myvar3                           1:145     1:73      ...       1:10      ...       ...\n",
       " \n",
       "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%ferret\n", "show data\n", "shade myvar3[l=1]" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 1 }