{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6596d3b9",
   "metadata": {},
   "source": [
    "(how-to:data:share:archive:profile)=\n",
    "\n",
    "# How to inspect an archive\n",
    "\n",
    "```{note}\n",
    "This tutorial can be downloaded and run as a Jupyter Notebook: {nb-download}`archive_profile.ipynb` {octicon}`download`, together with the archive {download}`include/process.aiida`.\n",
    "```\n",
    "\n",
    "The AiiDA archive is a file format for long term storage of data from a particular profile.\n",
    "\n",
    "See {ref}`how-to:share:archives` for information on how to create and migrate an archive.\n",
    "Once you have an archive at the latest version, you can inspect its contents in the same way you would with a standard AiiDA profile.\n",
    "\n",
    "We first create a profile instance from the archive path:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "537ae41a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Profile<'da9945b1a88440069a4db1c16a12caa3' ('process.aiida')>\n"
     ]
    }
   ],
   "source": [
    "from aiida import manage, orm, profile_context\n",
    "from aiida.storage.sqlite_zip.backend import SqliteZipBackend\n",
    "\n",
    "archive_profile = SqliteZipBackend.create_profile('include/process.aiida')\n",
    "print(archive_profile)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44817e35",
   "metadata": {},
   "source": [
    "The {py:func}`~aiida.manage.configuration.profile_context` function works similarly to the {py:func}`~aiida.manage.configuration.load_profile` function,\n",
    "but is used within a context manager, that insures that the storage is properly closed when the context is exited.\n",
    "With this, we can load our archive as a profile:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2452bbe8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mYou are currently using a post release development version of AiiDA: 2.0.0.post0\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mBe aware that this is not recommended for production and is not officially supported.\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mDatabases used with this version may not be compatible with future releases of AiiDA\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mas you might not be able to automatically migrate your data.\n",
      "\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Profile<'da9945b1a88440069a4db1c16a12caa3' ('process.aiida')>\n"
     ]
    }
   ],
   "source": [
    "with profile_context(archive_profile):\n",
    "    print(manage.get_manager().get_profile())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13187057",
   "metadata": {},
   "source": [
    "To directly access the storage backend, and view information about it, we can use:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "23932d98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mYou are currently using a post release development version of AiiDA: 2.0.0.post0\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mBe aware that this is not recommended for production and is not officially supported.\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mDatabases used with this version may not be compatible with future releases of AiiDA\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mas you might not be able to automatically migrate your data.\n",
      "\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SqliteZip storage (read-only) [open] @ include/process.aiida\n",
      "{\n",
      "  \"metadata\": {\n",
      "    \"export_version\": \"main_0001\",\n",
      "    \"aiida_version\": \"2.0.0a1\",\n",
      "    \"key_format\": \"sha256\",\n",
      "    \"compression\": 6,\n",
      "    \"ctime\": \"2022-03-10T11:56:56.759722\",\n",
      "    \"creation_parameters\": {\n",
      "      \"entities_starting_set\": {\n",
      "        \"node\": [\n",
      "          \"cff1e914-5a34-4930-9429-9dcc6d38feb1\"\n",
      "        ]\n",
      "      },\n",
      "      \"include_authinfos\": false,\n",
      "      \"include_comments\": true,\n",
      "      \"include_logs\": true,\n",
      "      \"graph_traversal_rules\": {\n",
      "        \"input_calc_forward\": false,\n",
      "        \"input_calc_backward\": true,\n",
      "        \"create_forward\": true,\n",
      "        \"create_backward\": true,\n",
      "        \"return_forward\": true,\n",
      "        \"return_backward\": false,\n",
      "        \"input_work_forward\": false,\n",
      "        \"input_work_backward\": true,\n",
      "        \"call_calc_forward\": true,\n",
      "        \"call_calc_backward\": true,\n",
      "        \"call_work_forward\": true,\n",
      "        \"call_work_backward\": true\n",
      "      }\n",
      "    }\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "with profile_context(archive_profile):\n",
    "    storage = manage.get_manager().get_profile_storage()\n",
    "    print(storage)\n",
    "    print(json.dumps(storage.get_info(), indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8843de80",
   "metadata": {},
   "source": [
    "This is directly equivalent to the command-line call:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6acde218",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\b\u001b[22mmetadata:\r\n",
      "  export_version: main_0001\r\n",
      "  aiida_version: 2.0.0a1\r\n",
      "  key_format: sha256\r\n",
      "  compression: 6\r\n",
      "  ctime: '2022-03-10T11:56:56.759722'\r\n",
      "  creation_parameters:\r\n",
      "    entities_starting_set:\r\n",
      "      node:\r\n",
      "      - cff1e914-5a34-4930-9429-9dcc6d38feb1\r\n",
      "    include_authinfos: false\r\n",
      "    include_comments: true\r\n",
      "    include_logs: true\r\n",
      "    graph_traversal_rules:\r\n",
      "      input_calc_forward: false\r\n",
      "      input_calc_backward: true\r\n",
      "      create_forward: true\r\n",
      "      create_backward: true\r\n",
      "      return_forward: true\r\n",
      "      return_backward: false\r\n",
      "      input_work_forward: false\r\n",
      "      input_work_backward: true\r\n",
      "      call_calc_forward: true\r\n",
      "      call_calc_backward: true\r\n",
      "      call_work_forward: true\r\n",
      "      call_work_backward: true\r\n",
      "\u001b[0m\r\n"
     ]
    }
   ],
   "source": [
    "!verdi archive info include/process.aiida"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d000607",
   "metadata": {},
   "source": [
    "Note, that once the context manager is exited, the storage is closed, and will except on further calls."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f563484c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SqliteZip storage (read-only) [closed] @ include/process.aiida\n"
     ]
    }
   ],
   "source": [
    "print(storage)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ffdc711",
   "metadata": {},
   "source": [
    "As per a standard profile, we can now use the {py:class}`~aiida.orm.QueryBuilder`, to [find and query for data](how-to:query):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ddaf82e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mYou are currently using a post release development version of AiiDA: 2.0.0.post0\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mBe aware that this is not recommended for production and is not officially supported.\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mDatabases used with this version may not be compatible with future releases of AiiDA\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mas you might not be able to automatically migrate your data.\n",
      "\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "uuid: cff1e914-5a34-4930-9429-9dcc6d38feb1 (pk: 61)\n"
     ]
    }
   ],
   "source": [
    "with profile_context(archive_profile):\n",
    "    process = orm.QueryBuilder().append(orm.ProcessNode).first(flat=True)\n",
    "    print(process)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d0ccbf7",
   "metadata": {},
   "source": [
    "and also use {py:class}`~aiida.tools.visualization.graph.Graph`, to [visualize data provenance](how-to:data:visualise-provenance):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e8dc105b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mYou are currently using a post release development version of AiiDA: 2.0.0.post0\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mBe aware that this is not recommended for production and is not officially supported.\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mDatabases used with this version may not be compatible with future releases of AiiDA\u001b[0m\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[93m\u001b[1mWarning\u001b[0m: \u001b[22mas you might not be able to automatically migrate your data.\n",
      "\u001b[0m\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"576pt\" height=\"139pt\"\n",
       " viewBox=\"0.00 0.00 576.00 138.81\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(.7272 .7272) rotate(0) translate(4 186.8701)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-186.8701 788.0387,-186.8701 788.0387,4 -4,4\"/>\n",
       "<!-- N58 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>N58</title>\n",
       "<ellipse fill=\"#8cd499\" stroke=\"#000000\" stroke-width=\"0\" cx=\"97.5807\" cy=\"-130.8701\" rx=\"42.7926\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"97.5807\" y=\"-127.1701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">Dict (58)</text>\n",
       "</g>\n",
       "<!-- N61 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>N61</title>\n",
       "<polygon fill=\"#de707f\" fill-opacity=\"0.466667\" stroke=\"#000000\" stroke-width=\"0\" points=\"459.1615,-116.8701 314.1615,-116.8701 314.1615,-80.8701 459.1615,-80.8701 459.1615,-116.8701\"/>\n",
       "<text text-anchor=\"middle\" x=\"386.6615\" y=\"-95.1701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">CalcFunctionNode (61)</text>\n",
       "</g>\n",
       "<!-- N58&#45;&gt;N61 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>N58&#45;&gt;N61</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M139.3068,-126.2512C182.1256,-121.5113 249.9031,-114.0086 303.6907,-108.0546\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"304.3514,-111.5029 313.9056,-106.9238 303.5812,-104.5454 304.3514,-111.5029\"/>\n",
       "<text text-anchor=\"middle\" x=\"254.6615\" y=\"-136.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">INPUT_CALC</text>\n",
       "<text text-anchor=\"middle\" x=\"254.6615\" y=\"-121.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">parameters</text>\n",
       "</g>\n",
       "<!-- N56 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>N56</title>\n",
       "<ellipse fill=\"#8cd499\" stroke=\"#000000\" stroke-width=\"0\" cx=\"700.6001\" cy=\"-161.8701\" rx=\"42.7926\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"700.6001\" y=\"-158.1701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">Dict (56)</text>\n",
       "</g>\n",
       "<!-- N61&#45;&gt;N56 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>N61&#45;&gt;N56</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M428.8339,-117.0151C443.8511,-122.8572 461.0384,-128.8522 477.1615,-132.8701 534.6797,-147.2034 602.396,-154.7292 648.0822,-158.4845\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"647.8112,-161.9739 658.056,-159.2731 648.3631,-154.9957 647.8112,-161.9739\"/>\n",
       "<text text-anchor=\"middle\" x=\"538.1615\" y=\"-171.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">CREATE</text>\n",
       "<text text-anchor=\"middle\" x=\"538.1615\" y=\"-156.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">group_data</text>\n",
       "</g>\n",
       "<!-- N57 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>N57</title>\n",
       "<ellipse fill=\"#8cd499\" stroke=\"#000000\" stroke-width=\"0\" cx=\"700.6001\" cy=\"-98.8701\" rx=\"83.3772\" ry=\"26.7407\"/>\n",
       "<text text-anchor=\"middle\" x=\"700.6001\" y=\"-102.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">StructureData (57)</text>\n",
       "<text text-anchor=\"middle\" x=\"700.6001\" y=\"-87.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">Fe2Se2</text>\n",
       "</g>\n",
       "<!-- N61&#45;&gt;N57 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>N61&#45;&gt;N57</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M459.4744,-98.8701C503.3499,-98.8701 559.6798,-98.8701 606.8595,-98.8701\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"607.0887,-102.3702 617.0887,-98.8701 607.0886,-95.3702 607.0887,-102.3702\"/>\n",
       "<text text-anchor=\"middle\" x=\"538.1615\" y=\"-117.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">CREATE</text>\n",
       "<text text-anchor=\"middle\" x=\"538.1615\" y=\"-102.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">reduced_structure_0</text>\n",
       "</g>\n",
       "<!-- N60 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>N60</title>\n",
       "<ellipse fill=\"#8cd499\" stroke=\"#000000\" stroke-width=\"0\" cx=\"700.6001\" cy=\"-26.8701\" rx=\"83.3772\" ry=\"26.7407\"/>\n",
       "<text text-anchor=\"middle\" x=\"700.6001\" y=\"-30.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">StructureData (60)</text>\n",
       "<text text-anchor=\"middle\" x=\"700.6001\" y=\"-15.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">Fe2Se2</text>\n",
       "</g>\n",
       "<!-- N61&#45;&gt;N60 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>N61&#45;&gt;N60</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M429.8399,-80.7299C444.6452,-75.027 461.4447,-69.117 477.1615,-64.8701 521.1149,-52.993 570.9827,-44.0893 612.4389,-37.8976\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"613.127,-41.3343 622.5138,-36.4212 612.112,-34.4082 613.127,-41.3343\"/>\n",
       "<text text-anchor=\"middle\" x=\"538.1615\" y=\"-83.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">CREATE</text>\n",
       "<text text-anchor=\"middle\" x=\"538.1615\" y=\"-68.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">rotated3D_structure_0</text>\n",
       "</g>\n",
       "<!-- N41585 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>N41585</title>\n",
       "<ellipse fill=\"#8cd499\" stroke=\"#000000\" stroke-width=\"0\" cx=\"97.5807\" cy=\"-67.8701\" rx=\"97.6615\" ry=\"26.7407\"/>\n",
       "<text text-anchor=\"middle\" x=\"97.5807\" y=\"-71.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">StructureData (41585)</text>\n",
       "<text text-anchor=\"middle\" x=\"97.5807\" y=\"-56.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">Fe2Se2</text>\n",
       "</g>\n",
       "<!-- N41585&#45;&gt;N61 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>N41585&#45;&gt;N61</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M194.8454,-70.1069C227.1866,-71.6883 263.3219,-74.3622 296.1615,-78.8701 298.6722,-79.2147 301.2166,-79.5927 303.781,-79.9985\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"303.2972,-83.4664 313.7421,-81.6907 304.4696,-76.5653 303.2972,-83.4664\"/>\n",
       "<text text-anchor=\"middle\" x=\"254.6615\" y=\"-97.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">INPUT_CALC</text>\n",
       "<text text-anchor=\"middle\" x=\"254.6615\" y=\"-82.6701\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">structure</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7f239acc20d0>"
      ]
     },
     "execution_count": 7,
     "metadata": {
      "filenames": {
       "image/svg+xml": "/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-tmp/checkouts/latest/docs/source/_build/jupyter_execute/howto/archive_profile_13_4.svg"
      }
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from aiida.tools.visualization import Graph\n",
    "\n",
    "with profile_context(archive_profile):\n",
    "    process = orm.QueryBuilder().append(orm.ProcessNode).first(flat=True)\n",
    "    graph = Graph(graph_attr={\"size\": \"8!,8!\", \"rankdir\": \"LR\"})\n",
    "    graph.add_incoming(process, annotate_links=\"both\")\n",
    "    graph.add_outgoing(process, annotate_links=\"both\")\n",
    "\n",
    "graph.graphviz"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "text_representation": {
    "extension": ".md",
    "format_name": "myst",
    "format_version": 0.13,
    "jupytext_version": "1.11.4"
   }
  },
  "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.8.6"
  },
  "source_map": [
   12,
   29,
   35,
   41,
   44,
   48,
   54,
   58,
   60,
   64,
   66,
   70,
   74,
   78
  ]
 },
 "nbformat": 4,
 "nbformat_minor": 5
}