{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Add Point and Range data\n",
    "Work with point and range attribute types, including multi-valued points and open-ended ranges."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to MI\n",
    "Get a database and table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "from GRANTA_MIScriptingToolkit import granta as mpy\n",
    "\n",
    "mi = mpy.connect('http://localhost/mi_servicelayer', autologon=True)\n",
    "db = mi.get_db(db_key='MI_Training')\n",
    "db.set_unit_system(unit_system='Metric', absolute_temperatures=False)\n",
    "tab = db.get_table('Composite Design Data')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create a new record and path\n",
    "Define a path in the table from a starting folder (in this case the top level folder) using `path_from()`.\n",
    "If the path does not exist, the required folders will be created.\n",
    "Specify `end_node` to create a new **Record** object at the end of the path with that name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(<Record long name:STK Example 12:Tue Mar 23 16:18:45 2021>,\n",
       " <Record long name:3M, S-Glass Unitape S2/SP381, [0]>)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now = datetime.now().strftime(\"%c\")\n",
    "record_name = 'STK Example 12:{}'.format(now)\n",
    "record = tab.path_from(None, tree_path=['Epoxy / Glass', '3M, S-Glass Unitape S2/SP381', '[0]'], end_node=record_name)\n",
    "record, record.parent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Edit the record's attributes\n",
    "Fetch point and range attributes for editing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "tab.bulk_fetch([record], attributes=['Test temperature',\n",
    "                                     '0° tension modulus - measured',\n",
    "                                     'Resin content',\n",
    "                                     'Fiber volume'])\n",
    "test_temperature = record.attributes['Test temperature']\n",
    "modulus = record.attributes['0° tension modulus - measured']\n",
    "resin_content = record.attributes['Resin content']\n",
    "fiber_volume = record.attributes['Fiber volume']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Point attributes\n",
    "Assign a list of numeric values to the `points` property.\n",
    "\n",
    "If multiple values are assigned, you must also assign a list of dictionaries containing the parameter values to the\n",
    "`parameters` property. (The two lists must be the same length.)\n",
    "Here, a single parameter called *Basis* is used to discriminate between the two point values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "test_temperature.points = [23]\n",
    "test_temperature.unit = '°C'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "modulus.points = [8, 7.5]\n",
    "modulus.unit = 'GPa'\n",
    "modulus.parameters = [{'Basis': 'Mean'}, {'Basis': 'A-basis'}]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Range attributes\n",
    "Access the `value` property directly and assign either a dictionary or tuple for high and low values.\n",
    "Omitting either the 'low' or 'high' value creates an open-ended range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "resin_content.value = {'low': 28, 'high': 30}\n",
    "resin_content.unit = 'wt%'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "fiber_volume.value = (None, 62.0)\n",
    "fiber_volume.unit = '%'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write your changes to MI\n",
    "First, specify the attributes on the record which you want to update on the server. Then write the changes to MI.\n",
    "The list of updated **Record** objects is returned."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "record.set_attributes([resin_content, test_temperature, fiber_volume, modulus])\n",
    "record = mi.update([record])[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Output the record's attributes\n",
    "Access the attribute values via the same properties you used to assign them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test temperature: 23.0 °C\n",
      "0° tension modulus: 7.5 GPa (A-basis), 8.0 GPa (Mean)\n"
     ]
    }
   ],
   "source": [
    "test_temperature = record.attributes['Test temperature']\n",
    "print(\"Test temperature: {0} {1}\".format(test_temperature.points[0], test_temperature.unit))\n",
    "\n",
    "modulus = record.attributes['0° tension modulus - measured']\n",
    "print('0° tension modulus: ', end=\"\")\n",
    "formatted_points = [\"{0} {1} ({2})\".format(point, modulus.unit, modulus.parameters[idx]['Basis'])\n",
    "                    for idx, point in enumerate(modulus.points)]\n",
    "print(\", \".join(formatted_points))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resin content: 28.0-30.0 wt%\n",
      "Fiber volume: < 62.0 %\n"
     ]
    }
   ],
   "source": [
    "resin_content = record.attributes['Resin content']\n",
    "print(\"Resin content: {0}-{1} {2}\".format(resin_content.value['low'], resin_content.value['high'], resin_content.unit))\n",
    "\n",
    "fiber_volume = record.attributes['Fiber volume']\n",
    "print(\"Fiber volume: < {0} {1}\".format(fiber_volume.value['high'], fiber_volume.unit))"
   ]
  }
 ],
 "metadata": {
  "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
