{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "42726620-5058-46e7-bd0c-7c0f04ab89a3",
   "metadata": {},
   "source": [
    "This tutorial deals with reading a SEG-Y data set and __creating an SU file__. Please make sure you have downloaded the test data as outlined in the file \"examples/data/README\".\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": "5bd396bf-c95c-4e1e-9de3-7411221f8c32",
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "\n",
    "import sys\n",
    "sys.path.insert(1, '../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8d4c40b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seisio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b4b1383d",
   "metadata": {},
   "outputs": [],
   "source": [
    "logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s', force=True)\n",
    "log = logging.getLogger(\"main\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02de3974-ce98-4e7d-bd84-eefcbe250dc7",
   "metadata": {},
   "source": [
    "Let's read the SEG-Y file \"Line_001.sgy\" and convert the file to Seismic Unix format, storing the data in \"Line_001.su\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "65dcd23f",
   "metadata": {},
   "outputs": [],
   "source": [
    "infile = \"data/Line_001.sgy\"   # SEGY file, IBM floats, big endian\n",
    "outfile = \"data/Line_001.su\"   # SU file, IEEE floats, little endian"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cb99334-e472-4cfd-b3af-43458439b830",
   "metadata": {},
   "source": [
    "First, we create a seisio object to read the SEG-Y file. All relevant parameters will be determined automatically (see previous tutorial for details):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d4f607cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: Input file: data/Line_001.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: original SEG-Y conforming to 1975 standard.\n",
      "INFO: Data sample format: 4-byte IBM floating-point.\n",
      "WARNING: SEG-Y fixed-trace-length flag is not set in binary header.\n",
      "WARNING: As SEG-Y major revision is 0, assuming fixed-length traces.\n",
      "INFO: Number of additional textual header records: 0.\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: 3600 bytes.\n",
      "INFO: Number of samples per data trace: 1501.\n",
      "INFO: Sampling interval: 2000 (unit as per SEG-Y standard).\n",
      "INFO: Number of data traces in file: 71284.\n",
      "INFO: Delay (on first trace): 0 (unit as per SEG-Y standard).\n"
     ]
    }
   ],
   "source": [
    "sin = seisio.input(infile)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "becaae9b-7069-41f2-930a-2004d60ec93e",
   "metadata": {},
   "source": [
    "Now we create another seisio object for the output in SU format. We would like to store the data in little-endian byte order. As we open the file in \"write\" mode, any existing file of the same name will be truncated (i.e., overwritten) and you get a corresponding warning - this happens, for instance, if you run the jupyter cell several times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d8481dd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: Output file: data/Line_001.su\n",
      "INFO: Assuming fixed-length traces for SU data.\n",
      "INFO: Output number of samples per data trace: 1501.\n",
      "INFO: Output data sample format: 4-byte IEEE floating-point.\n",
      "INFO: Output file endianess set to '<'.\n",
      "INFO: Byte offset of first trace relative to start of file: 0 bytes.\n"
     ]
    }
   ],
   "source": [
    "sout = seisio.output(outfile, ns=sin.ns, mode=\"w\", endian=\"<\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4473900-579d-49aa-b5e4-24da13845046",
   "metadata": {},
   "source": [
    "Let's convert the file in steps rather than reading the whole file at once. We would like to remap the input trace header mnemonic `fldr` to the trace header mnemonic `cdp` in the output (this would not normally make sense, we just do it to demonstrate the remapping itself). For each batch we read and convert, we also set the `scalel` and `scalco` trace headers (providing information on elevation and coordinate scalers) to zero. As SEG-Y has different trace headers compared to SU, you will see a few warnings that certain headers need to be dropped because there is no equivalent header mnemonic in the SU header definition. This could be avoided by remapping the corresponding headers to trace header mnemonics available in the SU world, or providing a non-standard SU trace header definition table (JSON file) when creating the output object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "28aaa761-708f-4fac-8665-7ead55eb3ec7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO: Reading 30000 trace(s) from disk starting at trace 0...\n",
      "INFO: Remapping mnemonic 'fldr' to 'cdp'.\n",
      "WARNING: Mnemonic 'spscal' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'tvmu' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdman' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdexp' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdun' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'dti' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'timscal' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'stypor' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sedirv' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sedirx' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sediri' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smman' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smexp' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smun' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'unassigned' dropped (not transferable without remap).\n",
      "INFO: Swapping bytes of output data.\n",
      "INFO: Writing 30000 trace(s) to disk...\n",
      "INFO: Reading 30000 trace(s) from disk starting at trace 30000...\n",
      "INFO: Remapping mnemonic 'fldr' to 'cdp'.\n",
      "WARNING: Mnemonic 'spscal' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'tvmu' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdman' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdexp' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdun' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'dti' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'timscal' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'stypor' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sedirv' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sedirx' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sediri' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smman' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smexp' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smun' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'unassigned' dropped (not transferable without remap).\n",
      "INFO: Swapping bytes of output data.\n",
      "INFO: Writing 30000 trace(s) to disk...\n",
      "INFO: Reading 11284 trace(s) from disk starting at trace 60000...\n",
      "INFO: Remapping mnemonic 'fldr' to 'cdp'.\n",
      "WARNING: Mnemonic 'spscal' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'tvmu' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdman' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdexp' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'trdun' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'dti' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'timscal' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'stypor' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sedirv' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sedirx' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'sediri' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smman' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smexp' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'smun' dropped (not transferable without remap).\n",
      "WARNING: Mnemonic 'unassigned' dropped (not transferable without remap).\n",
      "INFO: Swapping bytes of output data.\n",
      "INFO: Writing 11284 trace(s) to disk...\n"
     ]
    }
   ],
   "source": [
    "header_map = {\"fldr\": \"cdp\"}\n",
    "\n",
    "for batch in sin.batches(batch_size=30000):\n",
    "    batch[\"scalel\"] = 0   # set 'scalel' header to zero\n",
    "    batch[\"scalco\"] = 0   # set 'scalco' header to zero\n",
    "    sout.write_traces(traces=batch, remap=header_map)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88e31a5d-d9cd-4504-924a-2d3c9498f4f1",
   "metadata": {},
   "source": [
    "As we have read data in big-endian byte order, the data need to be swapped on output.\n",
    "\n",
    "You can now run the program \"surange\" on the SU data file and the output should look as follows (provided your SU installation was compiled to deal with data in native endianess and you are on a host that uses little-endian byte order):\n",
    "\n",
    "```\n",
    "$> surange < data/Line_001.su\n",
    "71284 traces:\n",
    "tracl    1 71284 (1 - 71284)\n",
    "tracr    1 71284 (1 - 71284)\n",
    "fldr     231 481 (231 - 481)\n",
    "tracf    -1 282 (-1 - 282)\n",
    "ep       32 282 (32 - 282)\n",
    "cdp      231 481 (231 - 481)\n",
    "cdpt     1 284 (1 - 284)\n",
    "trid     0 1 (0 - 1)\n",
    "nhs      1\n",
    "counit   3\n",
    "ns       1501\n",
    "dt       2000\n",
    "gain     3\n",
    "igc      1\n",
    "afilf    207\n",
    "afils    298\n",
    "hcf      207\n",
    "hcs      298\n",
    "year     1998\n",
    "$>\n",
    "```\n",
    "\n",
    "As can be seen, the `fldr` header was cloned and the `cdp` header is now present in the SU file (obviously, these numbers are no real CMP numbers, this mapping was just performed to demonstrate the feature). The `scalel` and `scalco` headers do now show up, i.e., they have been set to zero as outlined above.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d2779f7-a9c4-499e-9662-990877ad1cb5",
   "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
}
