Last active
December 11, 2015 10:29
-
-
Save taldcroft/4587441 to your computer and use it in GitHub Desktop.
IDL Python Jugalbandi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "metadata": { | |
| "name": "idl_python_jug" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<img src=\"http://www.python.org/community/logos/python-logo.png\">\n", | |
| "<img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRM802Zq_OL7je2m8ceCtWD501nYwpv_KLob11Ib6ESXeBq7rGO1w\">" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Keeping Track (Journaling)\n", | |
| "==========================\n", | |
| "**IDL**\n", | |
| " \n", | |
| " journal,'vlk.log'\t;writes commands to a file that can then be run as a batch job\n", | |
| "\n", | |
| "**Python**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%logstart" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Activating auto-logging. Current session state plus future input saved.\n", | |
| "Filename : ipython_log.py\n", | |
| "Mode : rotate\n", | |
| "Output logging : False\n", | |
| "Raw input log : False\n", | |
| "Timestamping : False\n", | |
| "State : active\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 1 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Interactive Development Environments\n", | |
| "-------------------------------------\n", | |
| "\n", | |
| "The [IDL Workbench](http://www.exelisvis.com/Support/HelpArticleDetail/ArticleId/5300.aspx) is an Interactive Development Environment based on the [Eclipse](http://www.eclipse.org) platform. In Python there are [many IDEs](http://stackoverflow.com/questions/81584/what-ide-to-use-for-python) to choose from including free (e.g. PyDev (Eclipse), Spyder, Erik IDE and IDLE) and non-free (Wingware IDE, PyCharm). \n", | |
| "\n", | |
| "The IPython Notebook is not an IDE but instead provides an interactive and reproducible way to share annotated code and figures." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Getting Help\n", | |
| "============\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL**\n", | |
| "\n", | |
| " help\t\t\t;or help,variable\n", | |
| " \n", | |
| " ?\t\t\t;or ?keyword\n", | |
| " ?help" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": true, | |
| "input": [ | |
| "x = 'string'\n", | |
| "help(help)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Help on _Helper in module site object:\n", | |
| "\n", | |
| "class _Helper(__builtin__.object)\n", | |
| " | Define the built-in 'help'.\n", | |
| " | This is a wrapper around pydoc.help (with a twist).\n", | |
| " | \n", | |
| " | Methods defined here:\n", | |
| " | \n", | |
| " | __call__(self, *args, **kwds)\n", | |
| " | \n", | |
| " | __repr__(self)\n", | |
| " | \n", | |
| " | ----------------------------------------------------------------------\n", | |
| " | Data descriptors defined here:\n", | |
| " | \n", | |
| " | __dict__\n", | |
| " | dictionary for instance variables (if defined)\n", | |
| " | \n", | |
| " | __weakref__\n", | |
| " | list of weak references to the object (if defined)\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 2 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%whos" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Variable Type Data/Info\n", | |
| "----------------------------\n", | |
| "x str string\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 3 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Python** makes it very easy to document your functions" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def add_one(x):\n", | |
| " \"\"\"\n", | |
| " Add one to the input ``x`` and print the value.\n", | |
| "\n", | |
| " :param x: input value \n", | |
| " :returns: ``x + 1``\n", | |
| " \"\"\"\n", | |
| " x = x + 1\n", | |
| " print x\n", | |
| " return x" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 13 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "help(add_one)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Help on function add_one in module __main__:\n", | |
| "\n", | |
| "add_one(x)\n", | |
| " Add one to the input ``x`` and print the value.\n", | |
| " \n", | |
| " :param x: input value \n", | |
| " :returns: ``x + 1``\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 14 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| " ;--> ?, ??, source viewer" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "add_one?" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 15 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "add_one??" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 7 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Python** documentation: Using [Sphinx](http://sphinx-doc.org/) it's trivial to make beautiful [documentation](http://astropy.readthedocs.org/en/latest/index.html) and nice [API docs](http://cxc.harvard.edu/mta/ASPECT/tool_doc/pydocs/Ska.Numpy.html#module-Ska.Numpy)." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL**\n", | |
| "\n", | |
| " doc_library,'TBD'\t;shows quick documentation\n", | |
| "\n", | |
| "XDOC of SSW can show full source. I don't have it in my path. Here's how to add it.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Paths vs Import\n", | |
| "===============\n", | |
| "\n", | |
| "In **IDL** a function or procedure is loaded into the current namespace by using `.run` or `.compile` or by referencing the function or procedure name. In all these cases the system variable `!path` is used to define a search path for finding the source file.\n", | |
| "\n", | |
| " print,!path\t\t;UNIX-like path" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "In **Python** there is a similar concept of the search path, but there is only one way to import a package or module, via the `import` statement." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import sys # collection of system-related functions and attributes\n", | |
| "sys.path # this is just a plain Python list of directories" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 16, | |
| "text": [ | |
| "['/my/new/path',\n", | |
| " '',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages/pip-1.1-py2.7.egg',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages/astropy-0.3.dev2875-py2.7-macosx-10.6-x86_64.egg',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python27.zip',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/virtualenv-1.7.1.2-py2.7.egg',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py-1.4.11-py2.7.egg',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pytest-2.3.2-py2.7.egg',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/plat-darwin',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/plat-mac',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/plat-mac/lib-scriptpackages',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/lib-tk',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/lib-old',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/lib-dynload',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyObjC',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/wx-2.8-mac-unicode',\n", | |
| " '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/extensions']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 16 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Adding paths\n", | |
| "--------------------\n", | |
| "**IDL**\n", | |
| "\n", | |
| " doc_library,'which'\n", | |
| " which,'xdoc.pro'\t\t;returns nothing\n", | |
| "\n", | |
| "**Python**\n", | |
| "\n", | |
| "In the normal Python workflow you rarely have to add paths. Here it is for reference:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "sys.path.insert(0, '/my/new/path')\n", | |
| "sys.path[:5]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 17, | |
| "text": [ | |
| "['/my/new/path',\n", | |
| " '/my/new/path',\n", | |
| " '',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',\n", | |
| " '/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages/pip-1.1-py2.7.egg']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 17 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL**\n", | |
| "\n", | |
| " !path = expand_path(':+/proj/DataCenter/ssw/gen') + ':' + !path\n", | |
| "\n", | |
| "add a directory hierarchy (\":\" is mandatory, \"+\" is optional, expand_path is necessary if \"+\" is used)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**Python** has the concept of hierarchical packages. This removes the need for the equivalent of `expand_path`. You only want to have the top-level directory of a package in `sys.path`." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import astropy\n", | |
| "import astropy.io.ascii\n", | |
| "astropy.io.ascii.__path__\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 18, | |
| "text": [ | |
| "['/Users/aldcroft/vpy/astropy/lib/python2.7/site-packages/astropy-0.3.dev2875-py2.7-macosx-10.6-x86_64.egg/astropy/io/ascii']" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 18 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL**\n", | |
| "\n", | |
| " doc_library,'xdoc'\t\t;let us see how to use it\n", | |
| " xdoc,'xdoc'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL**\n", | |
| "\n", | |
| " ;sys.path.pop(0) in IDL\n", | |
| " cc=strsplit(!path,':',/extract) & !path = strjoin(cc[where(strpos(cc,'DataCenter') lt 0)],':')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Important IDL namespace restrictions\n", | |
| "------------------------------------\n", | |
| "\n", | |
| "- Filenames on disk are case sensitive, but once compiled, they are not.\n", | |
| "- All function and subroutine names are available at all recursion levels\n", | |
| "- \"()\" can be used for both array indexing and function call arguments\n", | |
| "- Use `compile_opt IDLStrictArr` to force \"[]\" for array indexing\n", | |
| "- If function is not compiled, may be mistaken for an array\n", | |
| "- Use forward_function definition\n", | |
| "- Previously defined system-wide variables can be overwritten by other\n", | |
| " modules if they are originally not defined as read-only, and are altered \n", | |
| " with an input of the same size and type\n", | |
| "\n", | |
| "Python namespace and scoping\n", | |
| "-----------------------------\n", | |
| "\n", | |
| "- Namespaces are simple and work to make life simple (YAY!)\n", | |
| "- Variables defined at the module-level are available in the \"global\" scope\n", | |
| "- Variables defined within a function, or class are confined within that \"local\" scope.\n", | |
| "- There is no block-level scope in Python (unlike Perl or Ruby for example).\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "platform = 'hello'\n", | |
| "import sys\n", | |
| "print sys.platform, ',', platform" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "darwin , hello\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 19 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Global and Local Variables\n", | |
| "==========================\n", | |
| "\n", | |
| "**IDL**: regular variables are available downstream only if passed down through calling sequence or common blocks.\n", | |
| "\n", | |
| "**Python**: variables that are defined at the global (module-level) scope are available in functions or classes within that module. The behavior might be surprising on first encounter.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x = 2\n", | |
| "print 'Outside', x\n", | |
| "\n", | |
| "def func1():\n", | |
| " x = 10 # The local 'x' variable refers to 10\n", | |
| " print 'Inside func1()', x\n", | |
| "\n", | |
| "def func2():\n", | |
| " # 'x' is not defined within function so 'x' from the global scope is used\n", | |
| " print 'Inside func2()', x\n", | |
| "\n", | |
| "func1()\n", | |
| "func2()\n", | |
| "print 'Outside', x" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "\n", | |
| "Outside 2\n", | |
| "Inside func1() 10\n", | |
| "Inside func2() 2\n", | |
| "Outside 2\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 21 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL** has the concept of special system variables that are named beginning with `\"!\"`. This doesn't exist in **Python**." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| " help,!path\n", | |
| " help,!D,/struc\n", | |
| " \n", | |
| " help,!DPI\t;not dots per inch\n", | |
| " \n", | |
| " print,!PoA\n", | |
| "\n", | |
| " defsysv,'!PoA','PINTofALE'\t;roll your own\n", | |
| " help,!PoA\n", | |
| " \n", | |
| " !PoA=!DPI\t\t\t;strong typing\n", | |
| " help,!PoA" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "IDL Data Types\n", | |
| "===============\n", | |
| "\n", | |
| "From the [IDL Data Types](view-source:http://www.exelisvis.com/docs/IDL_Data_Types.html) documentation page:\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "<table style=\"caption-side: bottom;margin-left: 0;margin-right: auto;\" cellspacing=\"0\" class=\"IDLTable\">\n", | |
| " <caption>Data Types</caption> <col />\n", | |
| " <col /> <col /> <col /> <col />\n", | |
| " <col /> <thead> <tr>\n", | |
| " <td class=\"TableTitle\">\n", | |
| " <p>Data Type</p> </td>\n", | |
| " <td class=\"TableTitle\">\n", | |
| " <p>Description</p> </td>\n", | |
| " <td class=\"TableTitle\">\n", | |
| " <p>Type<br />Code</p> </td>\n", | |
| " <td class=\"TableTitle\">Type Name</td>\n", | |
| " <td class=\"TableTitle\">\n", | |
| " <p>Creation</p> </td>\n", | |
| " <td class=\"TableTitle\">\n", | |
| " <p>Routines</p> </td>\n", | |
| " </tr> </thead> <tbody>\n", | |
| " <tr> <td class=\"IDLTable\">Null</td>\n", | |
| " <td class=\"IDLTable\">An undefined variable. !NULL may be used for array concatenation, array indexing, and for comparison.</td>\n", | |
| " <td class=\"IDLTable\">0</td>\n", | |
| " <td class=\"IDLTable\">UNDEFINED</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>a = !NULL</pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>a = [ ]</pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>a = { }</pre><!--/sphider_noindex-->\n", | |
| " </td>\n", | |
| " <td class=\"IDLTable\"><a href=\"Constant_System_Variable.html#!NULL\">!NULL\n", | |
| " </a> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3317\"></a>Byte</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>An 8-bit unsigned integer ranging in value from 0 to 255. Pixels in images are commonly represented as byte data.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>1</p> </td>\n", | |
| " <td class=\"IDLTable\">BYTE</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>a = <span class=\"IDLnumber\">5B</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>a = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to byte type.\" href=\"BYTE.html\">BYTE</a></span>(<span class=\"IDLnumber\">5</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"BYTE.html#B_856805997_675883\">BYTE </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"BYTARR.html#B_856805997_675870\">BYTARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3318\"></a>Integer</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 16-bit signed integer ranging from <span style=\"font-family: Symbol;\">-</span>32,768 to +32,767.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>2</p> </td>\n", | |
| " <td class=\"IDLTable\">INT</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>b = <span class=\"IDLnumber\">0</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>b = <span class=\"IDLnumber\">0S</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>b = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to integer type, or type specified by TYPE keyword.\" href=\"FIX.html\">FIX</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"FIX.html#F_848155245_957011\">FIX </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"INTARR.html#I_833016429_677245\">INTARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3319\"></a>Unsigned Integer</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 16-bit unsigned integer ranging from 0 to 65535.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>12</p> </td>\n", | |
| " <td class=\"IDLTable\">UINT</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>c = <span class=\"IDLnumber\">0U</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>c = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to unsigned integer type.\" href=\"UINT.html\">UINT</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"UINT.html#U_807064173_950191\">UINT </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"UINTARR.html#U_807064173_950250\">UINTARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3320\"></a>Long</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 32-bit signed integer ranging in value from<br /><span style=\"font-family: Symbol;\">-</span>2 147 483 648 to +2 147 483 647.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>3</p> </td>\n", | |
| " <td class=\"IDLTable\">LONG</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>d = <span class=\"IDLnumber\">0L</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>d = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to longword integer type.\" href=\"LONG.html\">LONG</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"LONG.html#L_826528365_928337\">LONG </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"LONARR.html#L_826528365_916547\">LONARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3321\"></a>Unsigned Long</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 32-bit unsigned integer ranging in value from 0 to 4 294 967 296.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>13</p> </td>\n", | |
| " <td class=\"IDLTable\">ULONG</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>e = <span class=\"IDLnumber\">0UL</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>e = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to unsigned longword integer type.\" href=\"ULONG.html\">ULONG</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"ULONG.html#U_807064173_950523\">ULONG </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"ULONARR.html#U_807064173_950466\">ULONARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3322\"></a>64-bit Long</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 64-bit signed integer ranging in value from<br /><span style=\"font-family: Symbol;\">-</span>9 223 372 036 854 775 808 to +9 223 372 036 854 775 807.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>14</p> </td>\n", | |
| " <td class=\"IDLTable\">LONG64</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>f = <span class=\"IDLnumber\">0LL</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>f = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to 64-bit integer type.\" href=\"LONG64.html\">LONG64</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"LONG64.html#L_826528365_918926\">LONG64 </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"LON64ARR.html#L_826528365_983144\">LON64ARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3323\"></a>64-bit Unsigned Long</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 64-bit unsigned integer ranging in value from 0 to 18 446 744 073 709 551 615.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>15</p> </td>\n", | |
| " <td class=\"IDLTable\">ULONG64</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>g = <span class=\"IDLnumber\">0ULL</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>g = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to unsigned 64-bit integer type.\" href=\"ULONG64.html\">ULONG64</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"ULONG64.html#U_807064173_950582\">ULONG64 </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"ULON64ARR.html#U_807064173_950409\">ULON64ARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3324\"></a>Floating-point</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 32-bit, single-precision, floating-point number in the range of <span style=\"font-family: Symbol;font-size: 14pt;line-height: 16pt;\">\u00b1</span>10<sup>38</sup>, with approximately six or seven significant digits.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>4</p> </td>\n", | |
| " <td class=\"IDLTable\">FLOAT</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>h = <span class=\"IDLnumber\">0.0</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>h = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to single-precision floating-point.\" href=\"FLOAT.html\">FLOAT</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"FLOAT.html#F_848155245_676911\">FLOAT </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"FLTARR.html#F_848155245_676939\">FLTARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3325\"></a>Double-precision</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A 64-bit, double-precision, floating-point number in the range of <span style=\"font-family: Symbol;font-size: 14pt;line-height: 16pt;\">\u00b1</span>10<sup>308</sup> with approximately 14 significant digits.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>5</p> </td>\n", | |
| " <td class=\"IDLTable\">DOUBLE</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>i = <span class=\"IDLnumber\">0.0D</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>i = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to double-precision type.\" href=\"DOUBLE.html\">DOUBLE</a></span>(<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"DOUBLE.html#D_843829869_37011\">DOUBLE </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"DBLARR.html#D_843829869_1060809\">DBLARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3326\"></a>Complex</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A real-imaginary pair of single-precision, floating-point numbers. Complex numbers are useful for signal processing and frequency domain filtering.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>6</p> </td>\n", | |
| " <td class=\"IDLTable\">COMPLEX</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>j = $<br /><span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to complex type.\" href=\"COMPLEX.html\">COMPLEX</a></span>(<span class=\"IDLnumber\">1.0</span>, <span class=\"IDLnumber\">0.0</span>)</pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>j = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to complex type.\" href=\"COMPLEX.html\">COMPLEX</a></span>(<span class=\"IDLnumber\">1</span>,<span class=\"IDLnumber\">0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"COMPLEX.html#C_854643309_1052509\">COMPLEX </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"COMPLEXARR.html#C_854643309_676228\">COMPLEXARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3327\"></a>Double-precision complex</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A real-imaginary pair of double-precision, floating-point numbers.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>9</p> </td>\n", | |
| " <td class=\"IDLTable\">DCOMPLEX</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>k = $<br /><span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts argument to double-precision complex type.\" href=\"DCOMPLEX.html\">DCOMPLEX</a></span>(<span class=\"IDLnumber\">1.0</span>, <span class=\"IDLnumber\">0.0</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"DCOMPLEX.html#D_843829869_37570\">DCOMPLEX </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"DCOMPLEXARR.html#D_843829869_37687\">DCOMPLEXARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3328\"></a>String</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A sequence of characters, from 0 to 2 147 483 647 (2.1 GB) characters in length, which is interpreted as text.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>7</p> </td>\n", | |
| " <td class=\"IDLTable\">STRING</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>l = <span class=\"IDLstring\">'Hello'</span></pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre>l = $<br /><span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Converts arguments to string type.\" href=\"STRING.html\">STRING</a></span>([<span class=\"IDLnumber\">72B</span>, <span class=\"IDLnumber\">101B</span>, $</pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre><span class=\"IDLnumber\">108B</span>, <span class=\"IDLnumber\">108B</span>, <span class=\"IDLnumber\">111B</span>])</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"STRING.html#S_820040301_679053\">STRING </a>\n", | |
| " </p>\n", | |
| " <p><a class=\"MCXref_Heading_0\" href=\"STRARR.html#S_820040301_679026\">STRARR </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3329\"></a>Structure</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A compound data type that contains pairs of tags and values, where the values can be of any data type. Once a structure is created, neither the tags nor the data types of the values associated with the tags can be changed.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>8</p> </td>\n", | |
| " <td class=\"IDLTable\">Structure name or ANONYMOUS</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>s = {name,tag:<span class=\"IDLnumber\">0b</span>}</pre><!--/sphider_noindex-->\n", | |
| " <!--sphider_noindex--><pre> </pre><!--/sphider_noindex-->\n", | |
| " <p class=\"Code\" style=\"font-family: Courier;letter-spacing: -0.01em;\"></p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"CREATE_STRUCT.html#C_854643309_676445\">CREATE_STRUCT </a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3330\"></a>Pointer</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A compound data type that contains a reference to a single value, where the value can be of any data type. The data type of the value referred to can be changed.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>10</p> </td>\n", | |
| " <td class=\"IDLTable\">POINTER</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>p = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Creates a pointer.\" href=\"PTR_NEW.html\">PTR_NEW</a></span>(<span class=\"IDLnumber\">0b</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"PTR_NEW.html#P_817877613_1086484\">PTR_NEW</a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3331\"></a>Object</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A compound data type that contains a reference to an instance of an IDL object class. Object instances can contain data of any type, but the data types allowed are defined by the object class and cannot be changed after creation.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>11</p> </td>\n", | |
| " <td class=\"IDLTable\">Class name or OBJREF (for null objects)</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>o = <span class=\"IDLsysFunc\"><a class=\"link_tooltip marker_left\" title=\"Creates an object reference.\" href=\"OBJ_NEW.html\">OBJ_NEW</a></span>(<span class=\"IDLstring\">'class'</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"OBJ_NEW.html#O_828691053_889454\">OBJ_NEW</a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">\n", | |
| " <p><a name=\"kanchor3332\"></a>List</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>A compound data type that contains a reference to a collection of values. The individual values can be of any data type, and the data types and values can be changed.</p>\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p>11</p> </td>\n", | |
| " <td class=\"IDLTable\">LIST</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <!--sphider_noindex--><pre>l = <span class=\"IDLlibFunc\"><a class=\"link_tooltip marker_left\" title=\"Creates a new list variable.\" href=\"LIST.html\">LIST</a></span>(<span class=\"IDLnumber\">1</span>,<span class=\"IDLnumber\">2</span>)</pre><!--/sphider_noindex-->\n", | |
| " </td> <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"LIST.html#L_826528365_1077832\">LIST</a>\n", | |
| " </p> </td> </tr>\n", | |
| " <tr> <td class=\"IDLTable\">Hash</td>\n", | |
| " <td class=\"IDLTable\">A compound data type that contains a reference to a collection of key-value pairs. The key can be of any scalar type, and the value can be of any type. The data types and values can be changed.</td>\n", | |
| " <td class=\"IDLTable\">11</td>\n", | |
| " <td class=\"IDLTable\">HASH</td>\n", | |
| " <td class=\"IDLTable\">h = HASH('Id', 1234)</td>\n", | |
| " <td class=\"IDLTable\">\n", | |
| " <p style=\"margin-bottom: 3pt;\"><a class=\"MCXref_Heading_0\" href=\"HASH.html#L_826528365_1077832\">HASH</a>\n", | |
| " </p> </td> </tr>\n", | |
| " </tbody> </table>\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "IDL data types examples\n", | |
| "------------------------\n", | |
| "\n", | |
| " help, 2B^8-1B\n", | |
| " print,string([72b,69b,76b,76b,79b])\n", | |
| " print,201,form='(B0)'\n", | |
| " int=0 & reads,'11001001',form='(B)',int & print,int\n", | |
| " \n", | |
| " help, 2LL^63-1LL\t;\t64-bit\n", | |
| " help, 2ULL^64-1UL\t;\tunsigned 64-bit\n", | |
| " \n", | |
| " help, \"42\t\t;\toctal\n", | |
| " print,34,form='(O0)'\n", | |
| " \n", | |
| " help, '42'x\t\t;\thexadecimal\n", | |
| " print,66,form='(Z0)'\n", | |
| " \n", | |
| " help,!VALUES,/struct\t;\tNaNs" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Python data types and structures\n", | |
| "------------------------------------\n", | |
| "\n", | |
| "Python has exact analogs for all the IDL data types and structures.\n", | |
| "\n", | |
| "### Core Python vs. Numpy\n", | |
| "\n", | |
| "* It's important to understand the distinction between pure Python data structures and Numpy data structures.\n", | |
| "\n", | |
| "**Core Python**\n", | |
| "\n", | |
| "Everything is dynamically-typed and each object is an independent entity in memory\n", | |
| "\n", | |
| " w = None # None (null)\n", | |
| " x = 1 # int object\n", | |
| " y = 1.2 # float object (note \"1\" is int, \"1.0\" is float)\n", | |
| " z = 'abc' # string object\n", | |
| " a = [w, x, y, z] # list of objects (heterogenous, no guarantee of order in memory)\n", | |
| " b = {'w': w} # dict of objects\n", | |
| "\n", | |
| "**Numpy**\n", | |
| "\n", | |
| "Arrays are statically-typed and (typically) contiguous in memory.\n", | |
| "\n", | |
| " import numpy as np\n", | |
| " x = np.int64(1) # Numpy 64-bit int\n", | |
| " a = np.zeros(1000, dtype=np.int64) # 1000 zeros packed into 8000 + a few bytes\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "a = [1, 1.2, 'abc']\n", | |
| "print a\n", | |
| "a[1] = 'hello'\n", | |
| "print a" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[1, 1.2, 'abc']\n", | |
| "[1, 'hello', 'abc']\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 22 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import numpy as np\n", | |
| "b = np.arange(3, dtype=np.int64)\n", | |
| "print b\n", | |
| "b[2] = 10.1234\n", | |
| "print b" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[0 1 2]\n", | |
| "[ 0 1 10]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 23 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### type() is your friend" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print type(a)\n", | |
| "print type(b)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "<type 'list'>\n", | |
| "<type 'numpy.ndarray'>\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 24 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print a[0], type(a[0])\n", | |
| "print b[0], type(b[0])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1 <type 'int'>\n", | |
| "0 <type 'numpy.int64'>\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 25 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### Why types matter\n", | |
| "\n", | |
| "Most of the time you won't notice the difference between `int` and `np.int64`, or `float` and `np.float64`, but consider the example of serializing a data structure using JSON." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import json\n", | |
| "json.dumps(a[0])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 26, | |
| "text": [ | |
| "'1'" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 26 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "json.dumps([b[0]])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "ename": "TypeError", | |
| "evalue": "0 is not JSON serializable", | |
| "output_type": "pyerr", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[0;32m/Users/aldcroft/git/4587441/<ipython-input-28-42ac7fa52d7f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdumps\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
| "\u001b[0;32m/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.pyc\u001b[0m in \u001b[0;36mdumps\u001b[0;34m(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, **kw)\u001b[0m\n\u001b[1;32m 229\u001b[0m \u001b[0mcls\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mindent\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mseparators\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m \u001b[0;32mand\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 230\u001b[0m encoding == 'utf-8' and default is None and not kw):\n\u001b[0;32m--> 231\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_default_encoder\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 232\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcls\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 233\u001b[0m \u001b[0mcls\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mJSONEncoder\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
| "\u001b[0;32m/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.pyc\u001b[0m in \u001b[0;36mencode\u001b[0;34m(self, o)\u001b[0m\n\u001b[1;32m 199\u001b[0m \u001b[0;31m# exceptions aren't as detailed. The list call should be roughly\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 200\u001b[0m \u001b[0;31m# equivalent to the PySequence_Fast that ''.join() would do.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 201\u001b[0;31m \u001b[0mchunks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miterencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_one_shot\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 202\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchunks\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mlist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 203\u001b[0m \u001b[0mchunks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mchunks\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
| "\u001b[0;32m/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.pyc\u001b[0m in \u001b[0;36miterencode\u001b[0;34m(self, o, _one_shot)\u001b[0m\n\u001b[1;32m 262\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkey_separator\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitem_separator\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort_keys\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 263\u001b[0m self.skipkeys, _one_shot)\n\u001b[0;32m--> 264\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_iterencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 265\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 266\u001b[0m def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,\n", | |
| "\u001b[0;32m/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.pyc\u001b[0m in \u001b[0;36mdefault\u001b[0;34m(self, o)\u001b[0m\n\u001b[1;32m 176\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 177\u001b[0m \"\"\"\n\u001b[0;32m--> 178\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrepr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\" is not JSON serializable\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 179\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 180\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mencode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mo\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
| "\u001b[0;31mTypeError\u001b[0m: 0 is not JSON serializable" | |
| ] | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "\u001b[0;31mERROR\u001b[0m: TypeError: 0 is not JSON serializable [json.encoder]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 28 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### NaN and Inf available in core Python and Numpy\n", | |
| "\n", | |
| "These are interchangable (I think) since they are all Python `float`" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print float('nan'), np.nan\n", | |
| "print float('inf'), np.inf" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "nan nan\n", | |
| "inf inf\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 29 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Making arrays\n", | |
| "=============\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL**\n", | |
| "\n", | |
| "byte\n", | |
| "\n", | |
| " a = [1b] & print,a\n", | |
| " \n", | |
| "integer (array up-types to largest required register)\n", | |
| "\n", | |
| " b = [a,2] & print,b, byte(b)\n", | |
| "number of elements and size of array\n", | |
| "\n", | |
| " print,n_elements(b)\n", | |
| " print,size(b)\n", | |
| "\n", | |
| "**Python**\n", | |
| "\n", | |
| "See the [NumPy tutorial](http://www.scipy.org/Tentative_NumPy_Tutorial) for the whole story." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "b = np.array([[1, 2, 3],\n", | |
| " [4, 5, 6]], dtype=np.uint8)\n", | |
| "print b\n", | |
| "print 'array shape', b.shape\n", | |
| "print 'length', len(b)\n", | |
| "print 'size', b.size\n", | |
| "print 'bytes per element', b.itemsize" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[[1 2 3]\n", | |
| " [4 5 6]]\n", | |
| "array shape (2, 3)\n", | |
| "length 2\n", | |
| "size 6\n", | |
| "bytes per element 1\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 30 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "**IDL** *long, float, double, complex*\n", | |
| "\n", | |
| " c = replicate(b[0]+2L,3) & print,c, fix(c)\n", | |
| " d = fltarr(4)+4 & d[0]=c & print,d, long(d)\n", | |
| " e = [d,5.D] & print,e, float(e)\n", | |
| " f = complex(e,[d,6]) & print,f, double(f)\n", | |
| " \n", | |
| "For completeness, here are all the ways you can make an array:\n", | |
| "\n", | |
| "STRARR, BYTARR, INTARR, LONARR, ULONARR, FLTARR, DBLARR,\n", | |
| "COMPLEXARR, DCOMPLEXARR, LON64ARR, ULON64ARR, SINDGEN,\n", | |
| "BINDGEN, INDGEN, LINDGEN, UINDGEN, FINDGEN, DINDGEN, CINDGEN,\n", | |
| "DCINDGEN, ULINDGEN, UL64INDGEN, IDENTITY, MAKE_ARRAY, REPLICATE\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Making structures\n", | |
| "-----------------\n", | |
| "**IDL**\n", | |
| "\n", | |
| " g = {bravo:a, india:b, lima:c, foxtrot:d, delta:e, charlie:f}\n", | |
| " help,g\n", | |
| " \n", | |
| "Structure of structures\n", | |
| "\n", | |
| " h = create_struct('sierra',g)\n", | |
| " help,h,/struc\n", | |
| " help,h.sierra,/str\n", | |
| "\n", | |
| "**Python**\n", | |
| "\n", | |
| "In Pure Python you can uses `list` and `dict` to make arbitrarily complex and mutable data structures." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "a = {'bravo': 1, 'india': 2}\n", | |
| "b = [a, [3, 2, 1]]\n", | |
| "c = {'b': b, 'myfunc': lambda x: x * 2}\n", | |
| "print a\n", | |
| "print b\n", | |
| "print c" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "{'bravo': 1, 'india': 2}\n", | |
| "[{'bravo': 1, 'india': 2}, [3, 2, 1]]\n", | |
| "{'myfunc': <function <lambda> at 0x1055e88c0>, 'b': [{'bravo': 1, 'india': 2}, [3, 2, 1]]}\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 31 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "c['b'][0]['india'] = 'hello world'\n", | |
| "c" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 32, | |
| "text": [ | |
| "{'b': [{'bravo': 1, 'india': 'hello world'}, [3, 2, 1]],\n", | |
| " 'myfunc': <function __main__.<lambda>>}" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 32 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "array of structures\n", | |
| "-------------------\n", | |
| "**IDL**\n", | |
| "\n", | |
| " gg=replicate(g,5)\n", | |
| " help,gg\n", | |
| " help,gg,/str\n", | |
| " print,gg.bravo\n", | |
| " print,gg.india\n", | |
| " help,gg.delta\n", | |
| "\n", | |
| "**Python**\n", | |
| "\n", | |
| "If performance / memory is not an issue then a `list` of `dict` is a common data structure:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "data = \"\"\"smith,bob,47\n", | |
| "jones,karen,28\n", | |
| "reimann,jim,85\"\"\"\n", | |
| "persons = []\n", | |
| "for line in data.splitlines():\n", | |
| " last, first, age = line.split(',')\n", | |
| " person = {'last': last, 'first': first, 'age': int(age)}\n", | |
| " persons.append(person)\n", | |
| "\n", | |
| "print persons\n", | |
| "print 'persons[1] =', persons[1]\n", | |
| "print 'ages =', [person['age'] for person in persons]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[{'age': 47, 'last': 'smith', 'first': 'bob'}, {'age': 28, 'last': 'jones', 'first': 'karen'}, {'age': 85, 'last': 'reimann', 'first': 'jim'}]\n", | |
| "persons[1] = {'age': 28, 'last': 'jones', 'first': 'karen'}\n", | |
| "ages = [47, 28, 85]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 33 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "For higher performance and more convenient column access use [NumPy structured arrays](http://docs.scipy.org/doc/numpy/user/basics.rec.html):" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "persons = []\n", | |
| "for line in data.splitlines():\n", | |
| " last, first, age = line.split(',')\n", | |
| " person = (last, first, age)\n", | |
| " persons.append(person)\n", | |
| "\n", | |
| "# Now transform the list of lists into a numpy structured array\n", | |
| "dtype = [('last', 'S30'), # 30 character string \n", | |
| " ('first', 'S20'), # 20 character string\n", | |
| " ('age', 'i4')] # 32-bit int\n", | |
| "persons = np.array(persons, dtype=dtype)\n", | |
| "print repr(persons)\n", | |
| "print 'persons[1] =', persons[1]\n", | |
| "print 'ages =', persons['age']" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "array([('smith', 'bob', 47), ('jones', 'karen', 28), ('reimann', 'jim', 85)], \n", | |
| " dtype=[('last', '|S30'), ('first', '|S20'), ('age', '<i4')])\n", | |
| "persons[1] = ('jones', 'karen', 28)\n", | |
| "ages = [47 28 85]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 34 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "The NumPy structured array brings a lot of power:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "persons.sort(order=['age', 'last', 'first'])\n", | |
| "persons" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 35, | |
| "text": [ | |
| "array([('jones', 'karen', 28), ('smith', 'bob', 47), ('reimann', 'jim', 85)], \n", | |
| " dtype=[('last', '|S30'), ('first', '|S20'), ('age', '<i4')])" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 35 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Scripts\n", | |
| "=======\n", | |
| "\n", | |
| "**IDL**\n", | |
| "\n", | |
| "- Command line (batch mode, or \"@\") scripts\n", | |
| "- Command line multi-line scripts\n", | |
| "- Canned (.run) scripts\n", | |
| "- Procedures and functions\n", | |
| "\n", | |
| "**Python**\n", | |
| "\n", | |
| "- Just one way to write a Python script or module\n", | |
| "- Only functions, no procedures" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "### command line scripts\n", | |
| "\n", | |
| " $cat atvlk.com\n", | |
| " @atvlk.com\n", | |
| " \n", | |
| "### multiple/multi-line commands\n", | |
| "\n", | |
| " for i=0,10 do print,i\n", | |
| " \n", | |
| " for i=0,10 do print,i,format='($,i5.5)'\n", | |
| " \n", | |
| " for i=0,10 do begin & print,i,format='($,i5.5,\".\")' & wait,0.3 & endfor\n", | |
| " \n", | |
| " for i=0,10 do begin & $\n", | |
| " print,i,format='($,i5.5,\".\")' & $\n", | |
| " wait,0.3 & $\n", | |
| " endfor\n", | |
| " \n", | |
| "### canned scripts\n", | |
| "\n", | |
| " $cat runvlk.pro\n", | |
| " .run runvlk\n", | |
| " \n", | |
| "### multi line command line scripts\n", | |
| "\n", | |
| " .run\n", | |
| " for i=10,-10,-2 do begin\n", | |
| " print,i,format='($,i6.5,\".\")' & wait,0.3\n", | |
| " endfor\n", | |
| " end\n", | |
| " \n", | |
| "### procedures and functions\n", | |
| "\n", | |
| " print,'procedure'\n", | |
| " j=execute(\"print,'function'\") & print,j\n", | |
| " j=execute(\"x=sin(z)\") & print,j\n", | |
| " j=execute(\"z=f & x=sin(z)\") & print,j\n", | |
| " print,x" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Running Python\n", | |
| "--------------\n", | |
| "\n", | |
| "myscript.py:\n", | |
| "\n", | |
| " #!/usr/bin/env python\n", | |
| " print 'Welcome to my script!'\n", | |
| " x = 2\n", | |
| "\n", | |
| "**From the shell command line**\n", | |
| "\n", | |
| " $ python myscript.py\n", | |
| " $ ./myscript.py # After \"chmod +x myscript.py\"\n", | |
| "\n", | |
| "**From within IPython**\n", | |
| "\n", | |
| " %run myscript.py # runs from a clean environment\n", | |
| " %run -i myscript.py # starts with current variables defined in program scope\n", | |
| "\n", | |
| "Using `%run` any module-level variables will be available at the IPython prompt. In this example `x` would be `2`." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "IDL uses two types of program structures:\n", | |
| "-----------------------------------------\n", | |
| "\n", | |
| "- procedures, which return nothing, and\n", | |
| "\n", | |
| "- functions, which always return a value\n", | |
| "\n", | |
| "### Why two?\n", | |
| "\n", | |
| "- why not?\n", | |
| "- Fortran heritage\n", | |
| "- explicit return values vs less typing\n", | |
| "- if one dominant return value, use function, otherwise procedure is better\n", | |
| "\n", | |
| "### There are no nested subroutines. Recursive, yes. Nested, no.\n", | |
| "\n", | |
| "All compiled subroutines are available at all times everywhere (see namespace).\n", | |
| "\n", | |
| "### Examples\n", | |
| "\n", | |
| "procedures: PRINT, HELP, PLOT\n", | |
| "\n", | |
| "functions: FINDGEN(), WHERE(), EXECUTE()\n", | |
| "\n", | |
| "http://hea-www.harvard.edu/PINTofALE/doc/PoA.html\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Python has only functions\n", | |
| "-------------------------\n", | |
| "\n", | |
| "- [Python zen](http://www.python.org/dev/peps/pep-0020/): *There should be one, and preferably only one, obvious way to do it*.\n", | |
| "- If no return value is explicitly specified then `None` is implicitly returned\n", | |
| "- There is no requirement to capture function return values\n", | |
| "\n", | |
| "A function in Python is an object just like anything else. It can be inspected (e.g. dynamically find function arguments, source code), passed around to other functions, even modified during runtime." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "IDL Flow control\n", | |
| "=================\n", | |
| " \n", | |
| "do loops\n", | |
| "--------\n", | |
| "\n", | |
| "See above\n", | |
| "\n", | |
| "if then\n", | |
| "-------\n", | |
| "\n", | |
| " IF 1 THEN print,1\n", | |
| " IF 0 THEN message,'if it gets here, it is a bug!'\n", | |
| " \n", | |
| "multi-line\n", | |
| "\n", | |
| " .run\n", | |
| " x=10\n", | |
| " IF x lt 100 THEN BEGIN\n", | |
| " print,x^2\n", | |
| " ENDIF\n", | |
| " end\n", | |
| " \n", | |
| "if then else\n", | |
| "------------\n", | |
| "\n", | |
| " IF x lt 100 THEN print x^2 ELSE print sqrt(x)\n", | |
| "\n", | |
| " \n", | |
| "multi-line\n", | |
| "\n", | |
| " x=10\n", | |
| " IF x gt 100 THEN BEGIN & $\n", | |
| " print,x^2 & $\n", | |
| " ENDIF ELSE BEGIN & $\n", | |
| " print,sqrt(x) & $\n", | |
| " ENDELSE" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Python Flow control\n", | |
| "===================\n", | |
| "\n", | |
| "- Single-line flow control is legal but strongly discouraged. Don't ever use it in real code.\n", | |
| "- Python uses indentation to mark code blocks. This makes the code **clean** and **much easier to read**.\n", | |
| "- (FUD about possible code errors from indentation is just wrong in practice. It's never a problem.)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "for x in range(98, 103):\n", | |
| " if x > 100:\n", | |
| " print x ** 2\n", | |
| " else:\n", | |
| " print np.sqrt(x)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "9.89949493661\n", | |
| "9.94987437107\n", | |
| "10.0\n", | |
| "10201\n", | |
| "10404\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 36 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "IDL also has WHILE, REPEAT, CASE, SWITCH, and\n", | |
| "---------------------------------------------\n", | |
| "\n", | |
| "ways to move abruptly in and out, with BREAK, CONTINUE, GOTO\n", | |
| "\n", | |
| " @vlk_flowcontrol.com\n", | |
| "\n", | |
| "Python has if, for, while\n", | |
| "--------------------------\n", | |
| "\n", | |
| "That's all you really need! \n", | |
| "\n", | |
| "(But don't let that simplicity disappoint you, there is a rich underlayer of functional programming tools like iterators, generators that provide a lot of power in syntactically clean code)." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "x = 3\n", | |
| "while x > 0:\n", | |
| " print x\n", | |
| " x -= 1" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "3\n", | |
| "2\n", | |
| "1\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 37 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "for x in range(20):\n", | |
| " if x < 10:\n", | |
| " continue\n", | |
| " elif x > 12:\n", | |
| " break\n", | |
| " else:\n", | |
| " print x" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "10\n", | |
| "11\n", | |
| "12\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 38 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Python has exception handling\n", | |
| "-------------------------------\n", | |
| "\n", | |
| "Python has wonderful exception handling that can replace goto (in addition to handling errors gracefully)." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "class BadNestValue(ValueError):\n", | |
| " pass\n", | |
| "\n", | |
| "def deep_nest(x, y, z):\n", | |
| " if x > 1:\n", | |
| " if x > 2 or y > 2:\n", | |
| " if z > 3:\n", | |
| " raise BadNestValue('Bad values {} {} {}!'.format(x, y, z))\n", | |
| " print x, y, z\n", | |
| " \n", | |
| "try:\n", | |
| " deep_nest(5, 5, 2)\n", | |
| " deep_nest(5, 5, 5)\n", | |
| "except BadNestValue as err:\n", | |
| " print err" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "5 5 2\n", | |
| "Bad values 5 5 5!\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 39 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "try:\n", | |
| " 1 / 0\n", | |
| "except ZeroDivisionError:\n", | |
| " print 'Dividing by zero is a bad idea!'" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "Dividing by zero is a bad idea!\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 40 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "ARRAY MANIPULATION\n", | |
| "==================\n", | |
| "\n", | |
| "indexing, extracting subarrays, filtering, deforming\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| " i=indgen(20) & i2 = i^2\t;integers 0:19 and their squares\n", | |
| " print,i\n", | |
| " print,i2\n", | |
| " print,i2[i]\n", | |
| " print,i2[reverse(i)]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "i = np.arange(20)\n", | |
| "i2 = i ** 2\n", | |
| "i2[i[::-1]]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 41, | |
| "text": [ | |
| "array([361, 324, 289, 256, 225, 196, 169, 144, 121, 100, 81, 64, 49,\n", | |
| " 36, 25, 16, 9, 4, 1, 0])" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 41 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "basic indexing\n", | |
| "--------------\n", | |
| "\n", | |
| " print, i2[10]\t;11th element in the array\n", | |
| " \n", | |
| " print, i2[10:13]\t;11th, 12th, 13th, and 14th elements\n", | |
| " \n", | |
| " print, i2[0:3]\t;first four elements\n", | |
| " \n", | |
| " print, i2[16:*]\t;last four elements" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print i2[10]\n", | |
| "print i2[10:13] # prints 11th, 12th, and 13th, but NOT 14th element\n", | |
| "print i2[:3]\n", | |
| "print i2[16:]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "100\n", | |
| "[100 121 144]\n", | |
| "[0 1 4]\n", | |
| "[256 289 324 361]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 42 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Python indexing\n", | |
| "----------------\n", | |
| "\n", | |
| "Why the heck isn't the indexing **inclusive**?\n", | |
| "\n", | |
| "In the end it's just a convention that quickly becomes second nature. More often than not it's convenient.\n", | |
| "\n", | |
| "- Consider `array[i0:i1]` and `array[i1:i2]`\n", | |
| "- `i1 - i0` is the length of that slice\n", | |
| "- The elements of `array[i0:i1]` with `array[i1:i2]` are exactly `array[i0:i2]`." | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "IDL where\n", | |
| "----------\n", | |
| "\n", | |
| " print, where(i2 ge 16^2)\n", | |
| " \n", | |
| " print, i2[where(i2 ge 16^2)]\t\t;BAD CODE!!!\n", | |
| " \n", | |
| " print, i2[where(i2 gt 500,nn)]\t\t;BAD CODE!!!\n", | |
| " print, nn\n", | |
| " \n", | |
| "the most foolproof way to use WHERE\n", | |
| "\n", | |
| " idx = where(i2 gt 100, nn, complement=jdx, ncomplement=mm)\n", | |
| " help,idx,jdx,nn,mm\n", | |
| " if nn gt 0 then print,i2[idx]\n", | |
| " if mm gt 0 then print,i2[jdx]\n", | |
| "\n", | |
| "Python boolean masks and where\n", | |
| "------------------------------\n", | |
| "\n", | |
| "Most of the time you want to use boolean masks to select elements." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "ok = i2 >= 16 ** 2\n", | |
| "print ok\n", | |
| "print ok.sum() # Number of true values\n", | |
| "print i2[ok] # True values\n", | |
| "print i2[~ok] # Not true values\n", | |
| "print np.flatnonzero(ok) # indexes" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[False False False False False False False False False False False False\n", | |
| " False False False False True True True True]\n", | |
| "4\n", | |
| "[256 289 324 361]\n", | |
| "[ 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225]\n", | |
| "[16 17 18 19]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 43 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "If you really just want the index values then use `np.where`:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "np.where(i2 >= 16 ** 2)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 44, | |
| "text": [ | |
| "(array([16, 17, 18, 19]),)" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 44 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Manipulation\n", | |
| "------------\n", | |
| "\n", | |
| "**IDL**\n", | |
| "\n", | |
| " print, i2[1:*]-i2\t;smaller by one element\n", | |
| " \n", | |
| " ;another way to get difference array --\n", | |
| " print,i2-shift(i2,1)\n", | |
| "\n", | |
| "**Python**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "np.diff(i2)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 45, | |
| "text": [ | |
| "array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33,\n", | |
| " 35, 37])" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 45 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| " print, i2[[-1]]\t\t;first element\n", | |
| " \n", | |
| " print, i2[[-100,100]]\t;first and last elements\n", | |
| " print, i2[[-100,0,20,100]]\n", | |
| " \n", | |
| " print, i2[10:13]\n", | |
| " print, (i2[10:13])[1]\t;indexing of subsets\n", | |
| " print, (i2[10:13])[[0,2]]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print i2[[-1]]\n", | |
| "print i2[[-10, 10]]\n", | |
| "# Etc" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[361]\n", | |
| "[100 100]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 46 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "2D\n", | |
| "--\n", | |
| "\n", | |
| " i2d = reform(i2,4,5)\n", | |
| " help,i2d" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "i2d = i2.reshape(4, 5)\n", | |
| "print i2d" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[[ 0 1 4 9 16]\n", | |
| " [ 25 36 49 64 81]\n", | |
| " [100 121 144 169 196]\n", | |
| " [225 256 289 324 361]]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 47 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| " print, i2d[*,0]\t\t;first index rolls fastest" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print i2d[:, 0] # C ordering by default (but order in memory is configurable)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[ 0 25 100 225]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 48 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| " print,i2d\n", | |
| " print,indgen(4,5)\n", | |
| " \n", | |
| " print, i2d[0,*]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "array_equal\n", | |
| "-----------\n", | |
| "\n", | |
| " print, array_equal(i,i2)\n", | |
| " print, array_equal(i^2,i2)\n", | |
| " print, array_equal(i2d,i2)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print i == i2\n", | |
| "print i[i == i2]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[ True True False False False False False False False False False False\n", | |
| " False False False False False False False False]\n", | |
| "[0 1]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 49 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print np.all(i == i2)\n", | |
| "print np.allclose(i, i2) # For comparing float arrays" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "False\n", | |
| "False\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 50 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "array_indices\n", | |
| "-------------\n", | |
| "\n", | |
| " jj=array_indices(i2d,where(i2d eq 196))\n", | |
| " print,jj\n", | |
| " print,i2d[jj[0],jj[1]]\t;also works with higher dimensional arrays" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "jj = np.where(i2d <= 50)\n", | |
| "print jj\n", | |
| "print i2d[jj]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "(array([0, 0, 0, 0, 0, 1, 1, 1]), array([0, 1, 2, 3, 4, 0, 1, 2]))\n", | |
| "[ 0 1 4 9 16 25 36 49]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 51 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "concatenation\n", | |
| "-------------\n", | |
| "\n", | |
| " ii2 = [i,i2]\n", | |
| " ii2 = [i,i2d[*]]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print np.concatenate([i, i2])\n", | |
| "print np.concatenate([i, i2d.flatten()])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17\n", | |
| " 18 19 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225\n", | |
| " 256 289 324 361]\n", | |
| "[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17\n", | |
| " 18 19 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225\n", | |
| " 256 289 324 361]\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 52 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Parameters, Keywords\n", | |
| "====================\n", | |
| "\n", | |
| " \n", | |
| "IDL uses two methods to pass information to subroutines\n", | |
| "-------------------------------------------------------\n", | |
| "\n", | |
| "- parameters, which are ordered and don't need to be named\n", | |
| "\n", | |
| "- keywords, which are not ordered and must be named\n", | |
| "\n", | |
| "examples:\n", | |
| "---------\n", | |
| "\n", | |
| " plot,x,y and not plot,xarray=x,yarray=y\n", | |
| " plot,x,y,/ylog and not plot,x,y,xlog,ylog\n", | |
| " plot,x,y,/xlog,/ylog or plot,x,y,/ylog,/xlog\n", | |
| " plot,x,xlog=1,xstyle=5 or plot,x,/xl,xs=5\n", | |
| "\n", | |
| "http://hea-www.harvard.edu/PINTofALE/pro/fitting/mcmc_dem.pro\n", | |
| "\n", | |
| "Why two?\n", | |
| "--------\n", | |
| "\n", | |
| "- you can ensure that there be _some_ input\n", | |
| "- you can pass down keywords to subroutines without having to have them\n", | |
| " defined in the calling subroutine (_EXTRA, _REF_EXTRA, _STRICT_EXTRA)\n", | |
| "- typically, use parameters to define minimal required input, and keywords\n", | |
| " for values that can be usefully defaulted out\n", | |
| "\n", | |
| "Python has arguments and keyword arguments\n", | |
| "-------------------------------------------\n", | |
| "\n", | |
| "Pretty much the same idea. There is no special syntax for boolean arguments like `/xlog`.\n", | |
| "\n", | |
| "There is no analog of `_EXTRA, _REF_EXTRA, _STRICT_EXTRA`, that would be confusing.\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| "Error Handling and Debugging\n", | |
| "============================\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "**IDL**\n", | |
| "\n", | |
| " ;ctrl-C/.STEP/.OUT\n", | |
| " ;STOP/.CONTINUE\n", | |
| " ;MESSAGE\n", | |
| " ;BREAKPOINT\n", | |
| " ;CATCH\n", | |
| " ;PROFILER\n", | |
| "\n", | |
| "**Python**\n", | |
| "\n", | |
| "The built-in `pdb` module is a full-featured line debugger.\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| " \n", | |
| " journal\t\t\t;close the journal" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%logstop" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 53 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Parallel computing in 2 minutes\n", | |
| "================================" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "from IPython.parallel import Client\n", | |
| "c = Client()\n", | |
| "c.ids" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "\u001b[0;33mWARNING\u001b[0m: RuntimeWarning: \n", | |
| " Controller appears to be listening on localhost, but not on this machine.\n", | |
| " If this is true, you should specify Client(...,sshserver='[email protected]')\n", | |
| " or instruct your controller to listen on an external IP. [IPython.parallel.client.client]\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 54, | |
| "text": [ | |
| "[0, 1, 2, 3]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 54 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def calc_pi(n=1000):\n", | |
| " import numpy as np\n", | |
| " x = np.random.random(size=n)\n", | |
| " y = np.random.random(size=n)\n", | |
| " ok = (x ** 2 + y ** 2) < 1\n", | |
| " return ok.sum() * 4.0 / n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 55 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "calc_pi(1000000)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 56, | |
| "text": [ | |
| "3.1424599999999998" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 56 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "c[:].map_sync(calc_pi, [1000000, 1000000, 1000000])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 57, | |
| "text": [ | |
| "[3.1421679999999999, 3.1425519999999998, 3.144136]" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 57 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "GUI in 2 minutes\n", | |
| "================" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "import numpy as np\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "from matplotlib.widgets import Slider, Button, RadioButtons\n", | |
| "\n", | |
| "ax = plt.subplot(111)\n", | |
| "plt.subplots_adjust(left=0.25, bottom=0.25)\n", | |
| "t = np.arange(0.0, 1.0, 0.001)\n", | |
| "a0 = 5\n", | |
| "f0 = 3\n", | |
| "s = a0*np.sin(2*np.pi*f0*t)\n", | |
| "l, = plt.plot(t,s, lw=2, color='red')\n", | |
| "plt.axis([0, 1, -10, 10])\n", | |
| "\n", | |
| "axcolor = 'lightgoldenrodyellow'\n", | |
| "axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)\n", | |
| "axamp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)\n", | |
| "\n", | |
| "sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)\n", | |
| "samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)\n", | |
| "\n", | |
| "def update(val):\n", | |
| " amp = samp.val\n", | |
| " freq = sfreq.val\n", | |
| " l.set_ydata(amp*np.sin(2*np.pi*freq*t))\n", | |
| " plt.draw()\n", | |
| "sfreq.on_changed(update)\n", | |
| "samp.on_changed(update)\n", | |
| "\n", | |
| "resetax = plt.axes([0.8, 0.025, 0.1, 0.04])\n", | |
| "button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')\n", | |
| "def reset(event):\n", | |
| " sfreq.reset()\n", | |
| " samp.reset()\n", | |
| "button.on_clicked(reset)\n", | |
| "\n", | |
| "rax = plt.axes([0.025, 0.5, 0.15, 0.15], axisbg=axcolor)\n", | |
| "radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)\n", | |
| "def colorfunc(label):\n", | |
| " l.set_color(label)\n", | |
| " plt.draw()\n", | |
| "radio.on_clicked(colorfunc)\n", | |
| "\n", | |
| "plt.show()\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [] | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment