{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "93391f89",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.463938Z",
     "iopub.status.busy": "2026-10-02T14:46:58.463738Z",
     "iopub.status.idle": "2026-10-02T14:46:58.468214Z",
     "shell.execute_reply": "2026-10-02T14:46:58.467396Z"
    },
    "papermill": {
     "duration": 0.009021,
     "end_time": "2026-10-02T14:46:58.468865+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.459844+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "96b191fe",
   "metadata": {
    "papermill": {
     "duration": 0.029038,
     "end_time": "2026-10-02T14:46:58.500370+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.471332+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# File Wrapping\n",
    "\n",
    "\n",
    "## Communicating with External Codes in OpenMDAO\n",
    "\n",
    "\n",
    "The [ExternalCodeComp](../features/building_blocks/components/external_code_comp.ipynb) example describes how to\n",
    "define a component that calls an external program to perform its computation,\n",
    "passing input and output values via files.\n",
    "\n",
    "The input and output files were very simple in that basic example, containing only\n",
    "the values of interest.  In the general case however, you will probably need to\n",
    "generate an input file (with a specific format of rows and fields), and you'll also need to parse a\n",
    "similarly-formatted output file to get the output values. To facilitate working\n",
    "with these more complex input and output files, OpenMDAO provides a couple of utility\n",
    "classes: `InputFileGenerator` and `FileParser`.\n",
    "\n",
    "\n",
    "## Generating the Input File\n",
    "\n",
    "You can generate an input file for an external application in a few different ways.\n",
    "One way is to write the file completely from scratch using the new values that are\n",
    "contained in the component's variables. Not much can be done to aid with this task, as\n",
    "it requires knowledge of the file format and can be completed using Python's standard\n",
    "formatted output.\n",
    "\n",
    "A second way to generate an input file is by templating. A *template* file is\n",
    "a sample input file which can be processed by a templating engine to insert\n",
    "new values in the appropriate locations. Often the template file is a valid\n",
    "input file before being processed, although other times it contains directives\n",
    "or conditional logic to guide the generation. Obviously this method works well\n",
    "for cases where only a small number of the possible variables and settings are\n",
    "being manipulated.\n",
    "\n",
    "OpenMDAO includes a basic templating capability that allows a template file to\n",
    "be read, fields to be replaced with new values, and an input file to be\n",
    "generated so that the external application can read it. Suppose you have an\n",
    "input file that contains some integer, floating point, and string inputs:\n",
    "\n",
    "```\n",
    "    INPUT\n",
    "    1 2 3\n",
    "    INPUT\n",
    "    10.1 20.2 30.3\n",
    "    A B C\n",
    "```\n",
    "\n",
    "This is a valid input file for your application, and it can also be used as a\n",
    "template file. The templating object is called `InputFileGenerator`, and it\n",
    "includes methods that can replace specific fields as measured by their row\n",
    "and field numbers.\n",
    "\n",
    "To use the InputFileGenerator, first instantiate it and give it the name of\n",
    "the template file and the name of the output file that you want to produce. (Note\n",
    "that this code must be placed in the ``compute`` method of your component *before*\n",
    "the external code is run.) The code will generally look like this:\n",
    "\n",
    "```\n",
    "from openmdao.utils.file_wrap import InputFileGenerator\n",
    "\n",
    "parser = InputFileGenerator()\n",
    "parser.set_template_file('mytemplate.txt')\n",
    "parser.set_generated_file('myinput.txt')\n",
    "\n",
    "# (Call functions to poke new values here)\n",
    "\n",
    "parser.generate()\n",
    "```\n",
    "\n",
    "\n",
    "When the template file is set, it is read into memory so that all subsequent\n",
    "replacements are done without writing the intermediate file to the disk. Once\n",
    "all replacements have been made, the `generate` method is called to create the\n",
    "input file.  If you have not provided the name of an output file, then the\n",
    "generated file data will be returned as a string.  We will use this feature in\n",
    "the following examples.\n",
    "\n",
    "\n",
    "Let's say you want to replace the second integer in the input file above\n",
    "with a 7. The code would look like this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "70220db7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.505762Z",
     "iopub.status.busy": "2026-10-02T14:46:58.505586Z",
     "iopub.status.idle": "2026-10-02T14:46:58.509091Z",
     "shell.execute_reply": "2026-10-02T14:46:58.508444Z"
    },
    "papermill": {
     "duration": 0.006867,
     "end_time": "2026-10-02T14:46:58.509539+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.502672+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "output_data = {\n",
    "        \"\": [\n",
    "            \"INPUT\",\n",
    "            \"1 2 3\",\n",
    "            \"INPUT\",\n",
    "            \"10.1 20.2 30.3\",\n",
    "            \"A B C\"\n",
    "        ],\n",
    "        \"test_transfer\": [\n",
    "            \"INPUT\",\n",
    "            \"1 7 3\",\n",
    "            \"INPUT\",\n",
    "            \"10.1 20.2 30.3\",\n",
    "            \"A B C\"\n",
    "        ],\n",
    "        \"test_transfer_2\": [\n",
    "            \"INPUT\",\n",
    "            \"1 7 3\",\n",
    "            \"INPUT\",\n",
    "            \"10.1 20.2 3.141592653589793\",\n",
    "            \"A B C\"\n",
    "        ],\n",
    "        \"test_transfer_minus2\": [\n",
    "            \"INPUT\",\n",
    "            \"99999 7 3\",\n",
    "            \"INPUT\",\n",
    "            \"10.1 20.2 3.141592653589793\",\n",
    "            \"A B C\"\n",
    "        ],\n",
    "        \"test_transfer_array\": [\n",
    "            \"INPUT\",\n",
    "            \"123 456 789\",\n",
    "            \"INPUT\",\n",
    "            \"10.1 20.2 3.141592653589793\",\n",
    "            \"A B C\"\n",
    "        ],\n",
    "        \"test_transfer_stretch\": [\n",
    "            \"INPUT\",\n",
    "            \"11 22 33 44 55 66\",\n",
    "            \"INPUT\",\n",
    "            \"10.1 20.2 3.141592653589793\",\n",
    "            \"A B C\"\n",
    "        ]\n",
    "    }\n",
    "\n",
    "prev_test = {\n",
    "        \"test_transfer\": \"\",\n",
    "        \"test_transfer_2\": \"test_transfer\",\n",
    "        \"test_transfer_minus2\": \"test_transfer_2\",\n",
    "        \"test_transfer_array\": \"test_transfer_minus2\",\n",
    "        \"test_transfer_stretch\": \"test_transfer_array\"\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "80e488fb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.514768Z",
     "iopub.status.busy": "2026-10-02T14:46:58.514580Z",
     "iopub.status.idle": "2026-10-02T14:46:58.572826Z",
     "shell.execute_reply": "2026-10-02T14:46:58.571967Z"
    },
    "papermill": {
     "duration": 0.0616,
     "end_time": "2026-10-02T14:46:58.573362+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.511762+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.file_wrap import InputFileGenerator\n",
    "\n",
    "global parser  # global so we don't need `self.` in feature doc\n",
    "def parser_setup(test_name):\n",
    "    parser = InputFileGenerator()\n",
    "\n",
    "    # the input data for each test is the output of the previous test\n",
    "    parser._data = output_data[test_name][:]\n",
    "    return parser\n",
    "\n",
    "parser = parser_setup(\"test_transfer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0899aa79",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.606169Z",
     "iopub.status.busy": "2026-10-02T14:46:58.605948Z",
     "iopub.status.idle": "2026-10-02T14:46:58.608971Z",
     "shell.execute_reply": "2026-10-02T14:46:58.608413Z"
    },
    "papermill": {
     "duration": 0.007676,
     "end_time": "2026-10-02T14:46:58.609807+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.602131+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INPUT\n",
      "1 7 3\n",
      "INPUT\n",
      "10.1 20.2 30.3\n",
      "A B C\n"
     ]
    }
   ],
   "source": [
    "parser.mark_anchor(\"INPUT\")\n",
    "parser.transfer_var(\n",
    "    7, 1, 2)\n",
    "print(parser.generate())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e690a2e4",
   "metadata": {
    "papermill": {
     "duration": 0.003176,
     "end_time": "2026-10-02T14:46:58.616251+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.613075+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The method ``mark_anchor`` is used to define an anchor, which becomes the\n",
    "starting point for the ``transfer_var`` method. Here you find the first line\n",
    "down from the anchor, then the second field on that line and replace it with\n",
    "the new value.\n",
    "\n",
    "Now, if you want to replace the third value of the floating point numbers\n",
    "after the second ``INPUT`` statement. An additional argument can be passed to the\n",
    "``mark_anchor`` method to tell it to start at the second instance of the text\n",
    "fragment ``\"INPUT\"``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3b20d0d5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.671834Z",
     "iopub.status.busy": "2026-10-02T14:46:58.671662Z",
     "iopub.status.idle": "2026-10-02T14:46:58.674511Z",
     "shell.execute_reply": "2026-10-02T14:46:58.673575Z"
    },
    "papermill": {
     "duration": 0.055567,
     "end_time": "2026-10-02T14:46:58.675154+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.619587+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "parser = parser_setup(\"test_transfer_2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0f2858ce",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.735951Z",
     "iopub.status.busy": "2026-10-02T14:46:58.735690Z",
     "iopub.status.idle": "2026-10-02T14:46:58.739058Z",
     "shell.execute_reply": "2026-10-02T14:46:58.738359Z"
    },
    "papermill": {
     "duration": 0.008823,
     "end_time": "2026-10-02T14:46:58.739627+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.730804+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INPUT\n",
      "1 7 3\n",
      "INPUT\n",
      "10.1 20.2 3.141592653589793\n",
      "A B C\n"
     ]
    }
   ],
   "source": [
    "parser.mark_anchor(\"INPUT\", 2)\n",
    "\n",
    "my_var = 3.1415926535897932\n",
    "parser.transfer_var(my_var, 1, 3)\n",
    "\n",
    "print(parser.generate())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "129c84df",
   "metadata": {
    "papermill": {
     "duration": 0.003782,
     "end_time": "2026-10-02T14:46:58.747270+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.743488+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Note that you are able to pass a floating point value to ``transfer_var`` and still\n",
    "keep 15 digits of precision. See `\"A Note on Precision\"` below for a discussion of why this is important.\n",
    "\n",
    "Note also that we used the method ``reset_anchor`` to return the anchor to the\n",
    "beginning of the file before marking our new anchor. Subsequent calls to\n",
    "``mark_anchor`` start at the previous anchor and find the next instance of the\n",
    "anchor text. It is a good practice to reset your anchor unless you are looking for\n",
    "an instance of \"B\" that follows an instance of \"A\".\n",
    "\n",
    "You can also count backwards from the bottom of the file by passing a negative\n",
    "number. Here, the second instance of ``\"INPUT\"`` from the bottom brings you\n",
    "back to the first one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bc082f45",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.822383Z",
     "iopub.status.busy": "2026-10-02T14:46:58.822205Z",
     "iopub.status.idle": "2026-10-02T14:46:58.824825Z",
     "shell.execute_reply": "2026-10-02T14:46:58.824127Z"
    },
    "papermill": {
     "duration": 0.074369,
     "end_time": "2026-10-02T14:46:58.825448+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.751079+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser = parser_setup(\"test_transfer_minus2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7b8c7c70",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:58.831330Z",
     "iopub.status.busy": "2026-10-02T14:46:58.831184Z",
     "iopub.status.idle": "2026-10-02T14:46:58.834038Z",
     "shell.execute_reply": "2026-10-02T14:46:58.833352Z"
    },
    "papermill": {
     "duration": 0.006505,
     "end_time": "2026-10-02T14:46:58.834580+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.828075+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INPUT\n",
      "99999 7 3\n",
      "INPUT\n",
      "10.1 20.2 3.141592653589793\n",
      "A B C\n"
     ]
    }
   ],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"INPUT\", -2)\n",
    "parser.transfer_var(\"99999\", 1, 1)\n",
    "\n",
    "print(parser.generate())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5338852e",
   "metadata": {
    "papermill": {
     "duration": 0.003702,
     "end_time": "2026-10-02T14:46:58.996302+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.992600+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "There is also a method for replacing an entire array of values. Try\n",
    "replacing the set of three integers as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5cff4a6f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.004207Z",
     "iopub.status.busy": "2026-10-02T14:46:59.004016Z",
     "iopub.status.idle": "2026-10-02T14:46:59.006496Z",
     "shell.execute_reply": "2026-10-02T14:46:59.005909Z"
    },
    "papermill": {
     "duration": 0.007355,
     "end_time": "2026-10-02T14:46:59.007129+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:58.999774+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "parser = parser_setup(\"test_transfer_array\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8e320bdf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.074360Z",
     "iopub.status.busy": "2026-10-02T14:46:59.074210Z",
     "iopub.status.idle": "2026-10-02T14:46:59.077265Z",
     "shell.execute_reply": "2026-10-02T14:46:59.076554Z"
    },
    "papermill": {
     "duration": 0.068228,
     "end_time": "2026-10-02T14:46:59.077690+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.009462+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INPUT\n",
      "123 456 789\n",
      "INPUT\n",
      "10.1 20.2 3.141592653589793\n",
      "A B C\n"
     ]
    }
   ],
   "source": [
    "from numpy import array\n",
    "\n",
    "array_val = array([123, 456, 789])\n",
    "\n",
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"INPUT\")\n",
    "parser.transfer_array(array_val, 1, 1, 3)\n",
    "\n",
    "print(parser.generate())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "fdacdc69",
   "metadata": {
    "papermill": {
     "duration": 0.002383,
     "end_time": "2026-10-02T14:46:59.082467+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.080084+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The method ``transfer_array`` takes four required inputs. The first is an array\n",
    "of values that will become the new values in the file. The second is the\n",
    "starting row after the anchor. The third is the starting field that will be\n",
    "replaced, and the fourth is the ending field. The new array replaces the\n",
    "block of fields spanned by the starting field and the ending field.\n",
    "\n",
    "You can also use the ``transfer_array`` method to `stretch` an existing\n",
    "array in a template to add more terms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a0da8fbb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.087958Z",
     "iopub.status.busy": "2026-10-02T14:46:59.087835Z",
     "iopub.status.idle": "2026-10-02T14:46:59.090039Z",
     "shell.execute_reply": "2026-10-02T14:46:59.089386Z"
    },
    "papermill": {
     "duration": 0.005414,
     "end_time": "2026-10-02T14:46:59.090405+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.084991+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "parser = parser_setup(\"test_transfer_stretch\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "817d6648",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.103898Z",
     "iopub.status.busy": "2026-10-02T14:46:59.103782Z",
     "iopub.status.idle": "2026-10-02T14:46:59.106451Z",
     "shell.execute_reply": "2026-10-02T14:46:59.105876Z"
    },
    "papermill": {
     "duration": 0.014087,
     "end_time": "2026-10-02T14:46:59.106889+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.092802+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INPUT\n",
      "11 22 33 44 55 66 44 55 66\n",
      "INPUT\n",
      "10.1 20.2 3.141592653589793\n",
      "A B C\n"
     ]
    }
   ],
   "source": [
    "from numpy import array\n",
    "\n",
    "array_val = array([11, 22, 33, 44, 55, 66])\n",
    "\n",
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"INPUT\")\n",
    "parser.transfer_array(array_val, 1, 1, 3, sep=' ')\n",
    "\n",
    "print(parser.generate())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f02ccd04",
   "metadata": {
    "papermill": {
     "duration": 0.003528,
     "end_time": "2026-10-02T14:46:59.112786+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.109258+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The named argument ``sep`` defines which separator to include between the\n",
    "additional terms of the array.\n",
    "\n",
    "The input file templating capability that comes with OpenMDAO is basic, but quite\n",
    "functional. If you need a more powerful templating engine, particularly one that\n",
    "allows the inclusion of logic in your template files, then you may want to consider\n",
    "one of the community-developed [templating](https://wiki.python.org/moin/Templating) engines."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "db9797e7",
   "metadata": {
    "papermill": {
     "duration": 0.0023,
     "end_time": "2026-10-02T14:46:59.117428+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.115128+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Parsing the Output File\n",
    "\n",
    "When an external code is executed, it typically outputs the results into a\n",
    "file. OpenMDAO includes a utility called `FileParser`, which contains functions\n",
    "for parsing a file, extracting the fields you specify, and converting them to the\n",
    "appropriate data type.\n",
    "\n",
    "### *Basic Extraction*\n",
    "\n",
    "Consider an application that produces the following as part of its\n",
    "text file output:\n",
    "\n",
    "```\n",
    "    LOAD CASE 1\n",
    "    STRESS 1.3334e7 3.9342e7 NaN 2.654e5\n",
    "    DISPLACEMENT 2.1 4.6 3.1 2.22234\n",
    "    LOAD CASE 2\n",
    "    STRESS 11 22 33 44 55 66\n",
    "    DISPLACEMENT 1.0 2.0 3.0 4.0 5.0\n",
    "```\n",
    "\n",
    "As part of the file wrap, you need to extract the information from this file\n",
    "that is needed by downstream components in the model. For this to\n",
    "work, the file must have some general format that would allow you to locate the\n",
    "piece of data you need relative to some constant feature in the file. In other\n",
    "words, the main capability of the FileParser is to locate and extract a set of\n",
    "characters that is some number of lines and some number of fields away from an\n",
    "`anchor` point.\n",
    "\n",
    "```\n",
    "    from openmdao.utils.file_wrap import FileParser\n",
    "\n",
    "    parser = FileParser()\n",
    "    parser.set_file('output.txt')\n",
    "```\n",
    "\n",
    "To use the FileParser object, first instantiate it and give it the name of the\n",
    "output file. (Note that this code must be placed in your component's\n",
    "``compute`` function *after* the external code has been run.\n",
    "\n",
    "Say you want to extract the first ``STRESS`` value from each load case in the file\n",
    "snippet shown above. The code would look like this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d93ed5a8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.165762Z",
     "iopub.status.busy": "2026-10-02T14:46:59.165609Z",
     "iopub.status.idle": "2026-10-02T14:46:59.169483Z",
     "shell.execute_reply": "2026-10-02T14:46:59.168786Z"
    },
    "papermill": {
     "duration": 0.007176,
     "end_time": "2026-10-02T14:46:59.169888+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.162712+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.file_wrap import FileParser\n",
    "parser = FileParser()\n",
    "\n",
    "parser._data = [\n",
    "    \"LOAD CASE 1\",\n",
    "    \"STRESS 1.3334e7 3.9342e7 NaN 2.654e5\",\n",
    "    \"DISPLACEMENT 2.1 4.6 3.1 2.22234\",\n",
    "    \"LOAD CASE 2\",\n",
    "    \"STRESS 11 22 33 44 55 66\",\n",
    "    \"DISPLACEMENT 1.0 2.0 3.0 4.0 5.0\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ac918ac6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.175415Z",
     "iopub.status.busy": "2026-10-02T14:46:59.175306Z",
     "iopub.status.idle": "2026-10-02T14:46:59.178056Z",
     "shell.execute_reply": "2026-10-02T14:46:59.177450Z"
    },
    "papermill": {
     "duration": 0.006302,
     "end_time": "2026-10-02T14:46:59.178522+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.172220+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.mark_anchor(\"LOAD CASE\")\n",
    "var = parser.transfer_var(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f9d1b3e7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.183808Z",
     "iopub.status.busy": "2026-10-02T14:46:59.183680Z",
     "iopub.status.idle": "2026-10-02T14:46:59.185737Z",
     "shell.execute_reply": "2026-10-02T14:46:59.185138Z"
    },
    "papermill": {
     "duration": 0.00528,
     "end_time": "2026-10-02T14:46:59.186126+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.180846+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(var == 1.3334e+07)\n",
    "assert(type(var) is float)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0c141c1d",
   "metadata": {
    "papermill": {
     "duration": 0.003626,
     "end_time": "2026-10-02T14:46:59.192094+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.188468+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The method ``mark_anchor`` is used to define an anchor, which becomes the\n",
    "starting point for the ``transfer_var`` method. Here, you extract the value from the\n",
    "second field in the first line down from the anchor. The parser is smart enough to\n",
    "recognize the number as floating point and to create a Python float variable.\n",
    "\n",
    "The third value of ``STRESS`` is `NaN`. Python has built-in values for `nan`\n",
    "and `inf` that are valid for float variables. The parser recognizes them when it\n",
    "encounters them in a file. This allows you to catch numerical overflows,\n",
    "underflows, etc., and take action. NumPy includes the functions ``isnan`` and\n",
    "``isinf`` to test for `nan` and `inf` respectively.  In the following example,\n",
    "we extract that `nan` value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c8f0c016",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.197537Z",
     "iopub.status.busy": "2026-10-02T14:46:59.197423Z",
     "iopub.status.idle": "2026-10-02T14:46:59.199830Z",
     "shell.execute_reply": "2026-10-02T14:46:59.199262Z"
    },
    "papermill": {
     "duration": 0.005629,
     "end_time": "2026-10-02T14:46:59.200223+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.194594+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"LOAD CASE\")\n",
    "var = parser.transfer_var(1, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "60a1064d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.221154Z",
     "iopub.status.busy": "2026-10-02T14:46:59.221030Z",
     "iopub.status.idle": "2026-10-02T14:46:59.223177Z",
     "shell.execute_reply": "2026-10-02T14:46:59.222557Z"
    },
    "papermill": {
     "duration": 0.021028,
     "end_time": "2026-10-02T14:46:59.223660+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.202632+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from numpy import isnan\n",
    "assert(isnan(var))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d38a3531",
   "metadata": {
    "papermill": {
     "duration": 0.002294,
     "end_time": "2026-10-02T14:46:59.228397+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.226103+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "When the data is not a number, it is recognized as a string. For example, we can\n",
    "extract the word ``DISPLACEMENT``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "30be9b67",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.233993Z",
     "iopub.status.busy": "2026-10-02T14:46:59.233870Z",
     "iopub.status.idle": "2026-10-02T14:46:59.236249Z",
     "shell.execute_reply": "2026-10-02T14:46:59.235728Z"
    },
    "papermill": {
     "duration": 0.006034,
     "end_time": "2026-10-02T14:46:59.237005+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.230971+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"LOAD CASE\")\n",
    "var = parser.transfer_var(2, 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3ccd2f94",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.275234Z",
     "iopub.status.busy": "2026-10-02T14:46:59.275074Z",
     "iopub.status.idle": "2026-10-02T14:46:59.277536Z",
     "shell.execute_reply": "2026-10-02T14:46:59.276846Z"
    },
    "papermill": {
     "duration": 0.038469,
     "end_time": "2026-10-02T14:46:59.277937+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.239468+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(var == \"DISPLACEMENT\")\n",
    "assert(type(var) is str)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4706b5d6",
   "metadata": {
    "papermill": {
     "duration": 0.002522,
     "end_time": "2026-10-02T14:46:59.282937+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.280415+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Now, what if you want to extract the value of stress from the second load case? An\n",
    "additional argument can be passed to the ``mark_anchor`` method telling it to\n",
    "start at the second instance of the text fragment ``\"LOAD CASE\"``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e7e5a550",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.289733Z",
     "iopub.status.busy": "2026-10-02T14:46:59.289606Z",
     "iopub.status.idle": "2026-10-02T14:46:59.292404Z",
     "shell.execute_reply": "2026-10-02T14:46:59.291706Z"
    },
    "papermill": {
     "duration": 0.007209,
     "end_time": "2026-10-02T14:46:59.292818+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.285609+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"LOAD CASE\", 2)\n",
    "var = parser.transfer_var(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "84e79ae8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.300442Z",
     "iopub.status.busy": "2026-10-02T14:46:59.300329Z",
     "iopub.status.idle": "2026-10-02T14:46:59.302596Z",
     "shell.execute_reply": "2026-10-02T14:46:59.301955Z"
    },
    "papermill": {
     "duration": 0.007085,
     "end_time": "2026-10-02T14:46:59.303182+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.296097+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(var == 11)\n",
    "assert(type(var) is int)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5f1fb4fa",
   "metadata": {
    "papermill": {
     "duration": 0.003579,
     "end_time": "2026-10-02T14:46:59.310206+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.306627+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Note also that we used the method ``reset_anchor`` to return the anchor to the\n",
    "beginning of the file before marking our new anchor. Subsequent calls to\n",
    "``mark_anchor`` start at the previous anchor and find the next instance of the\n",
    "anchor text. It is a good practice to reset your anchor unless you are looking for\n",
    "an instance of \"B\" that follows an instance of \"A\".\n",
    "\n",
    "You can also count backwards from the bottom of the file by passing a negative\n",
    "number. Here, the second instance of ``\"LOAD CASE\"`` from the bottom brings us\n",
    "back to the first one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a18d5450",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.316481Z",
     "iopub.status.busy": "2026-10-02T14:46:59.316357Z",
     "iopub.status.idle": "2026-10-02T14:46:59.319767Z",
     "shell.execute_reply": "2026-10-02T14:46:59.318979Z"
    },
    "papermill": {
     "duration": 0.006757,
     "end_time": "2026-10-02T14:46:59.320124+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.313367+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"LOAD CASE\", -2)\n",
    "var = parser.transfer_var(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "29acc9ae",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:46:59.326258Z",
     "iopub.status.busy": "2026-10-02T14:46:59.326117Z",
     "iopub.status.idle": "2026-10-02T14:47:00.016338Z",
     "shell.execute_reply": "2026-10-02T14:47:00.015710Z"
    },
    "papermill": {
     "duration": 0.694417,
     "end_time": "2026-10-02T14:47:00.017087+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:46:59.322670+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "assert_near_equal(var, 1.3334e+07)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d8093f91",
   "metadata": {
    "papermill": {
     "duration": 0.002757,
     "end_time": "2026-10-02T14:47:00.022641+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.019884+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "There is a shortcut for extracting data that is stored as ``Key Value`` or\n",
    "``\"Key Value Value ...\"``. The method ``transfer_keyvar`` finds the first occurrence\n",
    "of the *key* string after the anchor (in this case, the word ``DISPLACEMENT``), and\n",
    "extracts the specified field value. This can be useful in cases where variables are\n",
    "found on lines that are uniquely named, particularly where you don't always know how\n",
    "many lines the key will occur past the anchor location. There are two optional\n",
    "arguments to ``transfer_keyvar``. The first lets you specify the `nth` occurrence\n",
    "of the key, and the second lets you specify a number of lines to offset from\n",
    "the line where the key is found (negative numbers are allowed)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "eb9793d3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.028833Z",
     "iopub.status.busy": "2026-10-02T14:47:00.028495Z",
     "iopub.status.idle": "2026-10-02T14:47:00.031925Z",
     "shell.execute_reply": "2026-10-02T14:47:00.031261Z"
    },
    "papermill": {
     "duration": 0.00705,
     "end_time": "2026-10-02T14:47:00.032387+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.025337+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"LOAD CASE 1\")\n",
    "var = parser.transfer_keyvar(\"DISPLACEMENT\", 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ab4f23eb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.039988Z",
     "iopub.status.busy": "2026-10-02T14:47:00.039849Z",
     "iopub.status.idle": "2026-10-02T14:47:00.042265Z",
     "shell.execute_reply": "2026-10-02T14:47:00.041782Z"
    },
    "papermill": {
     "duration": 0.00792,
     "end_time": "2026-10-02T14:47:00.042805+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.034885+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(var == 2.1)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "b6524495",
   "metadata": {
    "papermill": {
     "duration": 0.00242,
     "end_time": "2026-10-02T14:47:00.047632+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.045212+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### *Array Extraction*\n",
    "\n",
    "Now consider the same application that produces the following as part of its\n",
    "text file output:\n",
    "\n",
    "```\n",
    "    LOAD CASE 1\n",
    "    STRESS 1.3334e7 3.9342e7 NaN 2.654e5\n",
    "    DISPLACEMENT 2.1 4.6 3.1 2.22234\n",
    "    LOAD CASE 2\n",
    "    STRESS 11 22 33 44 55 66\n",
    "    DISPLACEMENT 1.0 2.0 3.0 4.0 5.0\n",
    "```\n",
    "\n",
    "This time, extract all of the displacements in one read and store\n",
    "them as an array. You can do this with the ``transfer_array`` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "eaa46dcf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.130857Z",
     "iopub.status.busy": "2026-10-02T14:47:00.130588Z",
     "iopub.status.idle": "2026-10-02T14:47:00.134377Z",
     "shell.execute_reply": "2026-10-02T14:47:00.133606Z"
    },
    "papermill": {
     "duration": 0.023448,
     "end_time": "2026-10-02T14:47:00.135075+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.111627+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"LOAD CASE\")\n",
    "var = parser.transfer_array(2, 2, 2, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e5c55d83",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.203068Z",
     "iopub.status.busy": "2026-10-02T14:47:00.202784Z",
     "iopub.status.idle": "2026-10-02T14:47:00.207043Z",
     "shell.execute_reply": "2026-10-02T14:47:00.206289Z"
    },
    "papermill": {
     "duration": 0.042783,
     "end_time": "2026-10-02T14:47:00.207828+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.165045+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy\n",
    "assert_near_equal(var, numpy.array([2.1, 4.6, 3.1, 2.22234]))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "37cbaa0c",
   "metadata": {
    "papermill": {
     "duration": 0.004136,
     "end_time": "2026-10-02T14:47:00.234050+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.229914+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The ``transfer_array`` method takes four arguments: *starting row*, *starting field*,\n",
    "*ending row*, and *ending field*. The parser extracts all values from the starting\n",
    "row and field and continues until it hits the ending field in the ending row.\n",
    "These values are all placed in a 1D array. When extracting multiple lines, if\n",
    "a line break is hit, the parser continues reading from the next line until the\n",
    "last line is hit. The following extraction illustrates this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3485dec6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.242884Z",
     "iopub.status.busy": "2026-10-02T14:47:00.242624Z",
     "iopub.status.idle": "2026-10-02T14:47:00.246603Z",
     "shell.execute_reply": "2026-10-02T14:47:00.245876Z"
    },
    "papermill": {
     "duration": 0.009419,
     "end_time": "2026-10-02T14:47:00.247229+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.237810+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"LOAD CASE\")\n",
    "var = parser.transfer_array(1, 3, 2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fc669a2c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.255890Z",
     "iopub.status.busy": "2026-10-02T14:47:00.255690Z",
     "iopub.status.idle": "2026-10-02T14:47:00.258417Z",
     "shell.execute_reply": "2026-10-02T14:47:00.257859Z"
    },
    "papermill": {
     "duration": 0.007364,
     "end_time": "2026-10-02T14:47:00.258899+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.251535+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_equal_arrays\n",
    "assert_equal_arrays(var, numpy.array([\n",
    "    '39342000.0', 'nan', '265400.0',\n",
    "    'DISPLACEMENT', '2.1', '4.6', '3.1'\n",
    "]))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c8f8cc19",
   "metadata": {
    "papermill": {
     "duration": 0.083756,
     "end_time": "2026-10-02T14:47:00.365590+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.281834+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "With the inclusion of ``'DISPLACEMENT'``, this is returned as an array of strings,\n",
    "so you must be careful.\n",
    "\n",
    "There is also a method to extract a 2-dimensional array from tabulated data.\n",
    "Consider an output table that looks like this:\n",
    "\n",
    "```\n",
    "    FREQ  DELTA   A     B     C     D     E     F     G     H     I     J\n",
    "     Hz\n",
    "     50.   1.0   30.0  34.8  36.3  36.1  34.6  32.0  28.4  23.9  18.5  12.2\n",
    "     63.   1.0   36.5  41.3  42.8  42.6  41.1  38.5  34.9  30.4  25.0  18.7\n",
    "     80.   1.0   42.8  47.6  49.1  48.9  47.4  44.8  41.2  36.7  31.3  25.0\n",
    "    100.   1.0   48.4  53.1  54.7  54.5  53.0  50.4  46.8  42.3  36.9  30.6\n",
    "```\n",
    "\n",
    "We would like to extract the relevant numerical data from this table, which\n",
    "amounts to all values contained in columns labeled \"A\" through \"J\" and rows\n",
    "labeled \"50 Hz\" through \"100 Hz.\" We would like to save these values in a\n",
    "two-dimensional numpy array. This can be accomplished using the\n",
    "``transfer_2Darray`` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "faf257f4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.372196Z",
     "iopub.status.busy": "2026-10-02T14:47:00.371950Z",
     "iopub.status.idle": "2026-10-02T14:47:00.377304Z",
     "shell.execute_reply": "2026-10-02T14:47:00.376629Z"
    },
    "papermill": {
     "duration": 0.009296,
     "end_time": "2026-10-02T14:47:00.377786+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.368490+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "parser = FileParser()\n",
    "\n",
    "# A way to \"cheat\" and do this without a file.\n",
    "parser._data = []\n",
    "parser._data.append('FREQ  DELTA  -8.5  -8.5  -8.5  -8.5  -8.5  -8.5  -8.5  -8.5  -8.5  -8.5')\n",
    "parser._data.append(' Hz')\n",
    "parser._data.append(' 50.   1.0   30.0  34.8  36.3  36.1  34.6  32.0  28.4  23.9  18.5  12.2')\n",
    "parser._data.append(' 63.   1.0   36.5  41.3  42.8  42.6  41.1  38.5  34.9  30.4  25.0  18.7')\n",
    "parser._data.append(' 80.   1.0   42.8  47.6  49.1  48.9  47.4  44.8  41.2  36.7  31.3  25.0')\n",
    "parser._data.append('100.   1.0   48.4  53.1  54.7  54.5  53.0  50.4  46.8  42.3  36.9  30.6')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b182aa54",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.499532Z",
     "iopub.status.busy": "2026-10-02T14:47:00.499259Z",
     "iopub.status.idle": "2026-10-02T14:47:00.505887Z",
     "shell.execute_reply": "2026-10-02T14:47:00.505278Z"
    },
    "papermill": {
     "duration": 0.077907,
     "end_time": "2026-10-02T14:47:00.506890+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.428983+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"Hz\")\n",
    "var = parser.transfer_2Darray(1, 3, 4, 12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "88c51893",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.612386Z",
     "iopub.status.busy": "2026-10-02T14:47:00.612140Z",
     "iopub.status.idle": "2026-10-02T14:47:00.615577Z",
     "shell.execute_reply": "2026-10-02T14:47:00.614835Z"
    },
    "papermill": {
     "duration": 0.010037,
     "end_time": "2026-10-02T14:47:00.616275+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.606238+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert_equal_arrays(var, numpy.array([\n",
    "    [30.0,  34.8,  36.3,  36.1,  34.6,  32.0,  28.4,  23.9,  18.5,  12.2],\n",
    "    [36.5,  41.3,  42.8,  42.6,  41.1,  38.5,  34.9,  30.4,  25.0,  18.7],\n",
    "    [42.8,  47.6,  49.1,  48.9,  47.4,  44.8,  41.2,  36.7,  31.3,  25.0],\n",
    "    [48.4,  53.1,  54.7,  54.5,  53.0,  50.4,  46.8,  42.3,  36.9,  30.6]\n",
    "]))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "13f71f05",
   "metadata": {
    "papermill": {
     "duration": 0.087357,
     "end_time": "2026-10-02T14:47:00.707122+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.619765+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The arguments to ``transfer_2Darray`` are the *starting row*, *starting field*,\n",
    "*ending row*, and *ending field*. If the ending field is omitted, then all values\n",
    "to the end of the line are extracted. In that case, care must be taken to make\n",
    "sure that all lines have the same number of values.\n",
    "\n",
    "Note that if the delimiter is set to ``'columns'``, then the column number should be\n",
    "entered instead of the field number. Delimiters are discussed in the next section.\n",
    "\n",
    "### *Delimiters*\n",
    "\n",
    "When the parser counts fields in a line of output, it determines the field\n",
    "boundaries by comparing against a set of delimiters. These delimiters can be\n",
    "changed using the ``set_delimiters`` method. By default, the delimiters are the\n",
    "general white space characters space (``\" \"``) and tab (``\"\\t\"``). The newline characters\n",
    "(``\"\\n\"`` and ``\"\\r\"``) are always removed regardless of the delimiter status.\n",
    "\n",
    "One common case that will require a change in the default delimiter is comma\n",
    "separated values (i.e. `csv`). Here's an example of such an output file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "abff3510",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.718316Z",
     "iopub.status.busy": "2026-10-02T14:47:00.718050Z",
     "iopub.status.idle": "2026-10-02T14:47:00.722204Z",
     "shell.execute_reply": "2026-10-02T14:47:00.721788Z"
    },
    "papermill": {
     "duration": 0.011379,
     "end_time": "2026-10-02T14:47:00.723145+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.711766+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "parser = FileParser()\n",
    "\n",
    "parser._data = [\n",
    "    \"CASE 1\",\n",
    "    \"3,7,2,4,5,6\"\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9866d864",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.731646Z",
     "iopub.status.busy": "2026-10-02T14:47:00.731313Z",
     "iopub.status.idle": "2026-10-02T14:47:00.734979Z",
     "shell.execute_reply": "2026-10-02T14:47:00.734452Z"
    },
    "papermill": {
     "duration": 0.008668,
     "end_time": "2026-10-02T14:47:00.735519+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.726851+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"CASE\")\n",
    "var = parser.transfer_var(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e84e6d73",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.743891Z",
     "iopub.status.busy": "2026-10-02T14:47:00.743564Z",
     "iopub.status.idle": "2026-10-02T14:47:00.746334Z",
     "shell.execute_reply": "2026-10-02T14:47:00.745788Z"
    },
    "papermill": {
     "duration": 0.007553,
     "end_time": "2026-10-02T14:47:00.746776+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.739223+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(var == \",7,2,4,5,6\")\n",
    "assert(type(var) is str)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5affba7e",
   "metadata": {
    "papermill": {
     "duration": 0.004057,
     "end_time": "2026-10-02T14:47:00.775454+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.771397+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "What happened here is slightly confusing, but the main point is that the parser\n",
    "did not handle this as expected because commas were not in the set of\n",
    "delimiters. Now specify commas as your delimiter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c07a725d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.784077Z",
     "iopub.status.busy": "2026-10-02T14:47:00.783714Z",
     "iopub.status.idle": "2026-10-02T14:47:00.789793Z",
     "shell.execute_reply": "2026-10-02T14:47:00.789202Z"
    },
    "papermill": {
     "duration": 0.011297,
     "end_time": "2026-10-02T14:47:00.790410+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.779113+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"CASE\")\n",
    "parser.set_delimiters(\", \")\n",
    "var = parser.transfer_var(1, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "bd654688",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.848813Z",
     "iopub.status.busy": "2026-10-02T14:47:00.848412Z",
     "iopub.status.idle": "2026-10-02T14:47:00.851490Z",
     "shell.execute_reply": "2026-10-02T14:47:00.850738Z"
    },
    "papermill": {
     "duration": 0.00963,
     "end_time": "2026-10-02T14:47:00.852136+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.842506+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(var == 7)\n",
    "assert(type(var) is int)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "dfdc6df8",
   "metadata": {
    "papermill": {
     "duration": 0.031688,
     "end_time": "2026-10-02T14:47:00.887938+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.856250+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "With the correct delimiter set, you extract the second integer as expected.\n",
    "\n",
    "While the ability to set the delimiters adds flexibility for parsing many\n",
    "different types of input files, you may find cases that are too complex to\n",
    "parse (e.g., a field with separator characters inside of quotes.) In such cases\n",
    "you may need to read and extract the data manually.\n",
    "\n",
    "### *Special Case Delimiter - Columns*\n",
    "\n",
    "One special-case value of the delimiter, ``'columns'``, is useful when the\n",
    "data fields have defined column location, as is the case in certain formatted\n",
    "output from Fortran or C. When the delimiter is set to ``'columns'``, the\n",
    "behavior of some of the methods is slightly different. Consider the following\n",
    "output file:\n",
    "\n",
    "```\n",
    "    CASE 1\n",
    "    12345678901234567890\n",
    "    TTF    3.7-9.4434967\n",
    "```\n",
    "\n",
    "The second line is a comment that helps the reader identify the column\n",
    "number (particularly on a printout) and does not need to be parsed.\n",
    "\n",
    "In the third line, the first three columns contain flags that are either ``'T'``\n",
    "or ``'F'``. Columns 4-10 contain a floating point number, and columns 11\n",
    "through 20 contain another floating point number. Note that there isn't\n",
    "always a space between the two numbers in this format, particularly when the\n",
    "second number has a negative sign. We can't parse this with a regular\n",
    "separator, but we can use the special separator ``'columns'``.\n",
    "\n",
    "Let's parse this file to extract the third boolean flag and the two numbers.\n",
    "\n",
    "When the delimiters are in column mode, ``transfer_var`` takes the starting\n",
    "field and the ending field as its second and third arguments. Since we just\n",
    "want one column for the boolean flag, the starting field and ending field are\n",
    "the same. For the floating point values, we provide the appropriate column ranges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8cd9efed",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.917766Z",
     "iopub.status.busy": "2026-10-02T14:47:00.917509Z",
     "iopub.status.idle": "2026-10-02T14:47:00.922002Z",
     "shell.execute_reply": "2026-10-02T14:47:00.921267Z"
    },
    "papermill": {
     "duration": 0.029797,
     "end_time": "2026-10-02T14:47:00.922637+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.892840+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "parser = FileParser()\n",
    "\n",
    "parser._data = [\n",
    "    \"CASE 1\",\n",
    "    \"12345678901234567890\",\n",
    "    \"TTF    3.7-9.4434967\"\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "80d0a5d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:00.934488Z",
     "iopub.status.busy": "2026-10-02T14:47:00.934317Z",
     "iopub.status.idle": "2026-10-02T14:47:00.939030Z",
     "shell.execute_reply": "2026-10-02T14:47:00.938404Z"
    },
    "papermill": {
     "duration": 0.011999,
     "end_time": "2026-10-02T14:47:00.939553+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.927554+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"CASE\")\n",
    "parser.set_delimiters(\"columns\")\n",
    "\n",
    "var1 = parser.transfer_var(2, 3, 3)\n",
    "var2 = parser.transfer_var(2, 4, 10)\n",
    "var3 = parser.transfer_var(2, 11, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7d7d033f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:01.009153Z",
     "iopub.status.busy": "2026-10-02T14:47:01.008924Z",
     "iopub.status.idle": "2026-10-02T14:47:01.012378Z",
     "shell.execute_reply": "2026-10-02T14:47:01.011738Z"
    },
    "papermill": {
     "duration": 0.069379,
     "end_time": "2026-10-02T14:47:01.013045+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:00.943666+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert((var1, var2, var3) == ('F', 3.7, -9.4434967))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0fac4d8d",
   "metadata": {
    "papermill": {
     "duration": 0.069596,
     "end_time": "2026-10-02T14:47:01.119912+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:01.050316+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The ``transfer_array`` method can also be used with columns, but it is used\n",
    "differently than ``transfer_var``. Consider this output file:\n",
    "\n",
    "```\n",
    "    CASE 2\n",
    "    123456789012345678901234567890\n",
    "    NODE 11 22 33 COMMENT\n",
    "    NODE 44 55 66 STUFF\n",
    "```\n",
    "\n",
    "In this example, we want to extract the six numerical values and place them in\n",
    "an array. When the delimiter is set to columns, we can define a rectangular\n",
    "box from which all elements are parsed into an array. Note that the numbers\n",
    "inside of the box are parsed assuming standard separator characters (``\" \\t\"``).\n",
    "\n",
    "So here we call ``transfer_array`` with four arguments: *starting row*,\n",
    "*starting column*, *ending row*, and *ending column*:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "dbbb6874",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:01.155812Z",
     "iopub.status.busy": "2026-10-02T14:47:01.155587Z",
     "iopub.status.idle": "2026-10-02T14:47:01.158364Z",
     "shell.execute_reply": "2026-10-02T14:47:01.157605Z"
    },
    "papermill": {
     "duration": 0.00739,
     "end_time": "2026-10-02T14:47:01.159015+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:01.151625+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "parser._data = [\n",
    "    \"CASE 2\",\n",
    "    \"123456789012345678901234567890\",\n",
    "    \"NODE 11 22 33 COMMENT\",\n",
    "    \"NODE 44 55 66 STUFF\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "422f4dd3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:01.167479Z",
     "iopub.status.busy": "2026-10-02T14:47:01.167275Z",
     "iopub.status.idle": "2026-10-02T14:47:01.178449Z",
     "shell.execute_reply": "2026-10-02T14:47:01.177840Z"
    },
    "papermill": {
     "duration": 0.016276,
     "end_time": "2026-10-02T14:47:01.179154+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:01.162878+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "parser.reset_anchor()\n",
    "parser.mark_anchor(\"CASE 2\")\n",
    "\n",
    "parser.set_delimiters(\"columns\")\n",
    "var = parser.transfer_array(2, 6, 3, 13)\n",
    "\n",
    "parser.set_delimiters(\" \\t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4547ea11",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:47:01.187175Z",
     "iopub.status.busy": "2026-10-02T14:47:01.187000Z",
     "iopub.status.idle": "2026-10-02T14:47:01.190292Z",
     "shell.execute_reply": "2026-10-02T14:47:01.189725Z"
    },
    "papermill": {
     "duration": 0.008205,
     "end_time": "2026-10-02T14:47:01.190795+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:01.182590+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(var,\n",
    "                 numpy.array([11., 22., 33., 44., 55., 66.]))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "96eebaba",
   "metadata": {
    "papermill": {
     "duration": 0.004391,
     "end_time": "2026-10-02T14:47:01.198658+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:47:01.194267+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## A Special Case - Fortran Namelists\n",
    "\n",
    "Since legacy Fortran codes are expected to be frequent candidates for\n",
    "file wrapping, you may also consider using the [f90nml](https://f90nml.readthedocs.io/en/latest/) package for reading and writing files to wrap those codes. This package enables the creation and\n",
    "manipulation of namelist files using the common Python dictionary interface.\n",
    "\n",
    "## A Note on Precision\n",
    "\n",
    "In a file-wrapped component, all key inputs for the external code come from an intermediate file\n",
    "that must be written. When generating the input file, it is important to prevent the loss of\n",
    "precision. Consider a variable with 15 digits of precision.\n",
    "\n",
    "```\n",
    "    >>> # Python 3 compatibility\n",
    "    >>>     >>> val = 3.1415926535897932\n",
    "    >>>\n",
    "    >>> val\n",
    "    3.141592653589793...\n",
    "    >>>\n",
    "    >>> print(val)\n",
    "    3.14159265359\n",
    "    >>>\n",
    "    >>> print(\"%s\" % str(val))\n",
    "    3.14159265359\n",
    "    >>>\n",
    "    >>> print(\"%f\" % val)\n",
    "    3.141593\n",
    "    >>>\n",
    "    >>> print(\"%.16f\" % val)\n",
    "    3.141592653589793...\n",
    "```\n",
    "\n",
    "If the variable's value in the input file is created using the ``print``\n",
    "statement, only 11 digits of precision are in the generated output. The same\n",
    "is true if you convert the value to a string and use string output formatting.\n",
    "Printing the variable as a floating point number with no format string gives\n",
    "even less precision. To output the full precision of a variable, you must specify\n",
    "decimal precision using formatted output (i.e., ``\"%.16f\"``).\n",
    "\n",
    "Quibbling over the 11th--15th decimal place may sound unnecessary,\n",
    "but some applications are sensitive to changes of this magnitude. Moreover, it\n",
    "is important to consider how your component may be used during optimization. A\n",
    "gradient optimizer will often use a finite difference scheme to calculate the\n",
    "gradients for a model, and this means that some component inputs might be\n",
    "subjected to small increments and decrements. A loss of precision here can\n",
    "completely change the calculated gradient and prevent the optimizer from\n",
    "reaching a correct minimum value.\n",
    "\n",
    "The file-wrapping utilities in OpenMDAO use ``\"%.16g\"``. If you write your own\n",
    "custom input-file generator for a new component, you should use this format\n",
    "for the floating point variables.\n",
    "\n",
    "Precision is also important when parsing the output, although the file-parsing\n",
    "utilities always extract the entire number. However, some codes limit the number of\n",
    "digits of precision in their output files for human readability. In such a case,\n",
    "you should check your external application's manual to see if there is a flag for\n",
    "telling the code to output the full precision."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "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.13.14"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 3.919484,
   "end_time": "2026-10-02T14:47:01.718067+00:00",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/openmdao_book/other_useful_docs/file_wrap.ipynb",
   "output_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/_executed_book/other_useful_docs/file_wrap.ipynb",
   "parameters": {},
   "start_time": "2026-10-02T14:46:57.798583+00:00",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}