{ "cells": [ { "cell_type": "markdown", "id": "ec11ceba-401d-4bbe-947b-c2f6bc6ba1b2", "metadata": {}, "source": [ "# Creating a New SEG-Y\n", "\n", "```{article-info}\n", ":author: Altay Sansal\n", ":date: \"{sub-ref}`today`\"\n", ":read-time: \"{sub-ref}`wordcount-minutes` min read\"\n", ":class-container: sd-p-0 sd-outline-muted sd-rounded-3 sd-font-weight-light\n", "```\n", "\n", "In this tutorial, we create a new SEG-Y file from spec.\n", "\n", "Let's start by importing some modules we will be using." ] }, { "cell_type": "code", "execution_count": null, "id": "4b98df60-71bc-4c34-a80c-3dfc1686190c", "metadata": {}, "outputs": [], "source": [ "from segy.factory import SegyFactory\n", "from segy.standards import get_segy_standard" ] }, { "cell_type": "markdown", "id": "6aedf3ba-c2de-4efc-a212-f511d87d2b96", "metadata": {}, "source": [ "We will take the default SEG-Y Revision 1 specification.\n", "\n", "The `SegyFactory` takes the spec, number of samples, and sample interval as inputs.\n", "By using its creation functions, we can make the encoded (ready to write to disk)\n", "bytes for file headers (text header and binary header)." ] }, { "cell_type": "code", "execution_count": null, "id": "3cb0479f-6183-46dc-97c4-be5815f8232e", "metadata": {}, "outputs": [], "source": [ "factory_config = {\n", " \"spec\": get_segy_standard(1.0),\n", " \"samples_per_trace\": 101,\n", " \"sample_interval\": 4_000, # in microseconds\n", "}\n", "\n", "factory = SegyFactory(**factory_config)\n", "\n", "txt = factory.create_textual_header()\n", "bin_ = factory.create_binary_header()" ] }, { "cell_type": "markdown", "id": "9eb14dc7-ed27-4f28-ba05-1604b2bb5b21", "metadata": {}, "source": [ "Let's create 15 traces and populate their values. Headers by default will be populated\n", "by sample rate and number of samples. We will set some fake headers. We will also fill\n", "in the trace samples with `trace_no` + `sample_index`. " ] }, { "cell_type": "code", "execution_count": null, "id": "eca363ad-e5bf-4f45-9b36-d6dda2d5a100", "metadata": {}, "outputs": [], "source": [ "TRACE_COUNT = 15\n", "\n", "headers = factory.create_trace_header_template(size=TRACE_COUNT)\n", "samples = factory.create_trace_sample_template(size=TRACE_COUNT)\n", "\n", "for trace_idx in range(TRACE_COUNT):\n", " headers[trace_idx][\"trace_seq_num_reel\"] = trace_idx + 1\n", " headers[trace_idx][\"cdp_x\"] = 1_000\n", " headers[trace_idx][\"cdp_y\"] = 10_000 + trace_idx * 50\n", " headers[trace_idx][\"inline\"] = 10\n", " headers[trace_idx][\"crossline\"] = 100 + trace_idx\n", "\n", " samples[trace_idx] = range(factory_config[\"samples_per_trace\"]) # sample index\n", " samples[trace_idx] += trace_idx # trace no" ] }, { "cell_type": "markdown", "id": "9716d98d-dc89-4e12-88dc-c3467b687292", "metadata": {}, "source": [ "Now we can create the encoded binary values for traces (ready to write)." ] }, { "cell_type": "code", "execution_count": null, "id": "62c68de0-13da-4d9f-8637-0e26e578613d", "metadata": {}, "outputs": [], "source": [ "traces = factory.create_traces(samples=samples, headers=headers)" ] }, { "cell_type": "markdown", "id": "3a7edc6a-3371-440b-812d-12bce6fa36e6", "metadata": {}, "source": [ "We can now compose a binary SEG-Y file from pieces.\n", "\n", "We create a new `my_segy.sgy` file and write the pieces we built." ] }, { "cell_type": "code", "execution_count": null, "id": "34d9906c-7449-4b42-8605-7174857c4093", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "with Path(\"my_segy.sgy\").open(mode=\"wb\") as fp:\n", " fp.write(txt)\n", " fp.write(bin_)\n", " fp.write(traces)" ] }, { "cell_type": "markdown", "id": "989227a9-283a-4390-afbf-cb1bfd0efb53", "metadata": {}, "source": [ "## Opening New SEG-Y\n", "\n", "Now we can open it with `SegyFile`. \n", "\n", "Note that our factory correctly populated the revision number in the header so the\n", "spec is automatically inferred!" ] }, { "cell_type": "code", "execution_count": null, "id": "3262356a-56a8-46fb-be0d-7feb5928a8fe", "metadata": {}, "outputs": [], "source": [ "from segy.file import SegyFile\n", "\n", "file = SegyFile(\"my_segy.sgy\")\n", "print(file.text_header)" ] }, { "cell_type": "code", "execution_count": null, "id": "fd20f55c-fd27-49d3-987f-09bb29f4d07d", "metadata": {}, "outputs": [], "source": [ "file.binary_header.to_dataframe()" ] }, { "cell_type": "code", "execution_count": null, "id": "6f70317d-34b7-421c-a6e0-11e048b1aca2", "metadata": {}, "outputs": [], "source": [ "file.sample[:]" ] }, { "cell_type": "code", "execution_count": null, "id": "ecc7d80e-806c-4103-bef3-b7019d17a839", "metadata": {}, "outputs": [], "source": [ "show_fields = [\n", " \"trace_seq_num_reel\",\n", " \"cdp_x\",\n", " \"cdp_y\",\n", " \"inline\",\n", " \"crossline\",\n", "]\n", "\n", "file.header[:][show_fields].to_dataframe()" ] }, { "cell_type": "code", "execution_count": null, "id": "96fafaae-894a-447a-adcb-d98ffa70a0ad", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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" } }, "nbformat": 4, "nbformat_minor": 5 }