{ "cells": [ { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from ctypes import *" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Load the io_netcdf library\n", "\n", "#dll_path = r'..\\io_netcdf\\dll\\x64\\Release\\io_netcdf.dll'\n", "dll_path = r'..\\io_netcdf\\dll\\x64\\Debug\\io_netcdf.dll'\n", "libionc = CDLL(dll_path)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Prepare some C-compatible variables\n", "p = create_string_buffer(255)\n", "ival = c_int()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('ierr = ', 0)\n", "('IONC_EBADID', c_long(-2001))\n" ] } ], "source": [ "# Test 1: get constant value for a top-level io_netcdf constant\n", "p.value = b\"IONC_EBADID\"\n", "ierr = libionc.ionc_get_constant(p, byref(ival))\n", "print(\"ierr = \", ierr)\n", "print(p.value, ival)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('ierr = ', 0)\n", "('UG_INVALID_DATALOCATION', c_long(-1013))\n" ] } ], "source": [ "# Test 2: get constant value for an underlying io_ugrid constant\n", "p.value = b\"UG_INVALID_DATALOCATION\"\n", "ierr = libionc.ionc_get_constant(p, byref(ival))\n", "print(\"ierr = \", ierr)\n", "print(p.value, ival)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(c_long(-1013), 'Invalid topological data location')\n" ] } ], "source": [ "# Test 3: request an error string for the error code from above.\n", "# NOTE: pass the integer error code by reference!\n", "# Returned string comes as a pointer to a character (array)\n", "libionc.ionc_strerror.restype = c_char_p\n", "print(ival, libionc.ionc_strerror(byref(ival)))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Close the io_netcdf library to release the file lock on the DLL.\n", "kernel32 = WinDLL('kernel32') \n", "kernel32.FreeLibrary.argtypes = [wintypes.HMODULE]\n", "\n", "handle = libionc._handle # obtain the DLL handle\n", "kernel32.FreeLibrary(handle)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }