{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8cb71ee-5811-4189-b675-d013ffe31365",
   "metadata": {
    "lines_to_next_cell": 0
   },
   "source": [
    "This tutorial deals with __reading a SEG-Y revision 2__ file. Please make sure you have run tutorial #4 to create the required SEG-Y file.\n",
    "\n",
    "Let's start by importing some packages (including the seisio package itself) and setting up the logging config."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6ddae925-e467-4a99-8917-3db6ef66c9c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import sys\n",
    "sys.path.insert(1, '../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8887b5b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seisio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ef33d394",
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s', force=True)\n",
    "log = logging.getLogger(\"main\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68f98349-8d42-432b-a88b-ab5608d9a7cf",
   "metadata": {},
   "source": [
    "Let's create the seisio object. Under ideal circumstances, the module can determine all relevant parameters automatically to read the file correctly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c06a7da3-4583-4170-a763-a9e5d9811cd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: Input file: data/SEGYrev2.sgy\n",
      "INFO: SEG-Y textual file header encoding looks to be 'EBCDIC' (best guess).\n",
      "INFO: Input file endianess looks to be '>' (best guess).\n",
      "INFO: SEG-Y revision (according to binary header): 2.0\n",
      "INFO: Data sample format: 4-byte IEEE floating-point.\n",
      "INFO: SEG-Y fixed-trace-length flag is set in binary header.\n",
      "INFO: SEG-Y ext. textual record 1 encoding looks to be 'EBCDIC' (best guess).\n",
      "INFO: SEG-Y ext. textual record 2 encoding looks to be 'EBCDIC' (best guess).\n",
      "INFO: Number of additional textual header records: 2.\n",
      "INFO: Number of trailer stanza records: 0.\n",
      "INFO: SEG-Y trace header extension 1 is not present.\n",
      "INFO: Number of user-defined trace headers: 0\n",
      "INFO: Byte offset of first trace relative to start of file: 10000 bytes.\n",
      "INFO: Number of samples per data trace: 80001.\n",
      "INFO: Sampling interval: 0.1 (unit as per SEG-Y standard).\n",
      "INFO: Number of data traces in file: 10.\n",
      "INFO: Delay (on first trace): 0 (unit as per SEG-Y standard).\n"
     ]
    }
   ],
   "source": [
    "file = \"data/SEGYrev2.sgy\"\n",
    "\n",
    "sio = seisio.input(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9a999cad",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: nt=10, ns=80001, dt=1.000000e-07\n"
     ]
    }
   ],
   "source": [
    "nt = sio.ntraces\n",
    "ns = sio.nsamples\n",
    "dt = sio.vsi*1e-6\n",
    "t = np.arange(0, (ns-1)*dt+dt/2, dt)\n",
    "log.info(\"nt=%d, ns=%d, dt=%e\", nt, ns, dt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dfeaddb-65b3-478c-a45d-34d39b11a119",
   "metadata": {},
   "source": [
    "Let's get and log the binary file header:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "83b3d982-53d7-49db-8916-224e69298a28",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: SEG-Y binary file header:\n",
      "+----------+-----------------+----------------------------------------------------------+\n",
      "|          |           value | description                                              |\n",
      "|----------+-----------------+----------------------------------------------------------|\n",
      "| job      |     1           | Job identification number                                |\n",
      "| line     |    42           | Line number                                              |\n",
      "| nt       |    10           | Number of traces per ensemble                            |\n",
      "| format   |     5           | Data sample format code                                  |\n",
      "| ens      | 80001           | Extended number of samples per data trace                |\n",
      "| edt      |     0.1         | Extended sample interval (IEEE)                          |\n",
      "| iconst   |     1.69091e+07 | The integer constant 16909060_10 (01020304_16)           |\n",
      "| segymaj  |     2           | SEG-Y major format revision number                       |\n",
      "| fixed    |     1           | Fixed length trace flag                                  |\n",
      "| ntxthead |     2           | Number of 3200-byte extended textual file header records |\n",
      "| ntfile   |    10           | Number of traces in file                                 |\n",
      "| byteoff  | 10000           | Byte offset of first trace relative to start of file     |\n",
      "+----------+-----------------+----------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "binhead = sio.log_binhead()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5aa764d0-b6bf-4306-9522-7bf5843cf74f",
   "metadata": {},
   "source": [
    "Let's get and log the primary textual file header:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "61c46581-6d3e-428d-9156-7bd71f2ac8e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: SEG-Y textual file header:\n",
      "INFO: -------- BEGIN --------\n",
      "C01 This is an exemplary textual file header.                                   \n",
      "C02                                                                             \n",
      "C03                                                                             \n",
      "C04                                                                             \n",
      "C05                                                                             \n",
      "C06                                                                             \n",
      "C07                                                                             \n",
      "C08                                                                             \n",
      "C09                                                                             \n",
      "C10                                                                             \n",
      "C11                                                                             \n",
      "C12                                                                             \n",
      "C13                                                                             \n",
      "C14                                                                             \n",
      "C15                                                                             \n",
      "C16                                                                             \n",
      "C17                                                                             \n",
      "C18                                                                             \n",
      "C19                                                                             \n",
      "C20                                                                             \n",
      "C21                                                                             \n",
      "C22                                                                             \n",
      "C23                                                                             \n",
      "C24                                                                             \n",
      "C25                                                                             \n",
      "C26                                                                             \n",
      "C27                                                                             \n",
      "C28                                                                             \n",
      "C29                                                                             \n",
      "C30                                                                             \n",
      "C31                                                                             \n",
      "C32                                                                             \n",
      "C33                                                                             \n",
      "C34                                                                             \n",
      "C35                                                                             \n",
      "C36                                                                             \n",
      "C37                                                                             \n",
      "C38                                                                             \n",
      "C39 SEG-Y_REV2.0                                                                \n",
      "C40 END TEXTUAL HEADER                                                          \n",
      "INFO: --------- END ---------\n"
     ]
    }
   ],
   "source": [
    "txthead = sio.log_txthead()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c88db0c4-83fa-4870-b70a-df7f2f481763",
   "metadata": {},
   "source": [
    "Let's get the additional textual header records:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6fac0313-0229-4555-b995-74811c5017a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: (( OGP: P1/11 Data Geographic Extent ))                                         ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             ...                                                                             \n"
     ]
    }
   ],
   "source": [
    "records = sio.records\n",
    "log.info(\"%s\", records[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "50f1c1d3-f987-44aa-895b-99e161714af7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: (( Company XYZ: User-defined textual header ))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  \n"
     ]
    }
   ],
   "source": [
    "log.info(\"%s\", records[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8616a27f-557c-4ff4-88a6-0fd6fbbed484",
   "metadata": {},
   "source": [
    "Now read the data set and plot it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "882fe40b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: Reading entire file (10 traces) from disk...\n",
      "INFO: Reading all traces took 0.001 seconds.\n"
     ]
    }
   ],
   "source": [
    "dataset = sio.read_all_traces()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "73cd145e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 35744 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(1, 1)\n",
    "for i in range(nt):\n",
    "    ax.plot(i+dataset[\"data\"][i, :], t, 'k-')\n",
    "ax.set_xlabel(\"Trace number\")\n",
    "ax.set_ylabel(\"Time in s\")\n",
    "ax.set_xlim(-0.5, 9.99)\n",
    "ax.set_ylim(t[-1], t[0])\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94b506ab-8a14-45c3-a569-0f2623310fb4",
   "metadata": {},
   "source": [
    "Note: We provide another Python package called __seisplot__ for displaying seismic data in a convenient way."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60788da1-9a14-4f91-b5a5-d74479f146a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "executable": "/usr/bin/env python3",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
