{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fetch Attribute Data in Bulk\n",
    "Fetch large amounts of data from multiple records with `bulk_fetch()` for optimum performance."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to MI \n",
    "Specify a database and table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "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('Metric', absolute_temperatures=True)\n",
    "tab = db.get_table('MaterialUniverse')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define a list of records to fetch\n",
    "For example, by Record GUID."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "guids = ['00001192-000e-4fff-8fff-dd92ffff0000',\n",
    "         '00001194-000e-4fff-8fff-dd92ffff0000',\n",
    "         '0000119b-000e-4fff-8fff-dd92ffff0000',\n",
    "         '00000e38-000e-4fff-8fff-dd92ffff0000',\n",
    "         '00000e41-000e-4fff-8fff-dd92ffff0000',\n",
    "         '000009cb-000e-4fff-8fff-dd92ffff0000']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get each record using its Record GUID, and combine the returned **Record** objects into a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Record long name:Titanium, alpha-beta alloy, Ti-6Al-4V, aged>,\n",
       " <Record long name:Titanium, alpha-beta alloy, Ti-6Al-4V, annealed, generic>,\n",
       " <Record long name:Titanium, alpha-beta alloy, Ti-6Al-4V, solution treated & aged>,\n",
       " <Record long name:Aluminum, 7075, wrought, T6>,\n",
       " <Record long name:Aluminum, 7075, wrought, T73>,\n",
       " <Record long name:Low alloy steel, AISI 4130, air melted, normalized>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "records = [tab.get_record_by_id(vguid=guid) for guid in guids]\n",
    "records"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fetch attribute data values \n",
    "Fetch the *Base*, *Density* and *Young's modulus* attribute values for each record, and print the results.\n",
    "Default units can be overridden before export using the `table.set_display_unit()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "tab.set_display_unit(\"Young's modulus\", \"ksi\")\n",
    "tab.bulk_fetch(records=records, attributes=[\"Base\", \"Density\", \"Young's modulus\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name                                               |   Density / kg/m^3    | Young's modulus / ksi |        Base       \n",
      "------------------------------------------------------------------------------------------------------------------------\n",
      "Titanium, alpha-beta alloy, Ti-6Al-4V, aged        |  4420.00  - 4430.00   |  16099.19 - 17259.49  |  ['Ti (Titanium)']  \n",
      "Titanium, alpha-beta alloy, Ti-6Al-4V, annealed, g |  4406.64  - 4450.93   |  15954.15 - 17259.49  |  ['Ti (Titanium)']  \n",
      "Titanium, alpha-beta alloy, Ti-6Al-4V, solution tr |  4406.64  - 4450.93   |  15954.15 - 16969.41  |  ['Ti (Titanium)']  \n",
      "Aluminum, 7075, wrought, T6                        |  2770.00  - 2830.00   |  10007.60 - 11022.87  |  ['Al (Aluminum)']  \n",
      "Aluminum, 7075, wrought, T73                       |  2781.69  - 2809.65   |  10007.60 - 10520.81  |  ['Al (Aluminum)']  \n",
      "Low alloy steel, AISI 4130, air melted, normalized |  7794.24  - 7872.58   |  29007.55 - 30495.11  |    ['Fe (Iron)']    \n"
     ]
    }
   ],
   "source": [
    "density_unit = tab.attributes['Density'].unit\n",
    "youngs_mod_unit = tab.attributes[\"Young's modulus\"].unit\n",
    "print('{:50.50} | {:^21} | {:^21} | {:^18}'.format('Name',\n",
    "                                                   'Density / ' + density_unit,\n",
    "                                                   \"Young's modulus / \" + youngs_mod_unit,\n",
    "                                                   'Base'))\n",
    "print('-'*120)\n",
    "for record in records:\n",
    "    output = '{:50.50} | {:^10.2f}-{:^10.2f} | {:^10.2f}-{:^10.2f} | {:^20}'.format(record.name,\n",
    "        record.attributes['Density'].value['low'],\n",
    "        record.attributes['Density'].value['high'],\n",
    "        record.attributes[\"Young's modulus\"].value['low'],\n",
    "        record.attributes[\"Young's modulus\"].value['high'],\n",
    "        str(record.attributes['Base'].value))\n",
    "    print(output)"
   ]
  }
 ],
 "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": 2
}
