|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import questionary |
| 6 | +import typer |
| 7 | +from segy.schema.format import TextHeaderEncoding |
| 8 | +from segy.schema.header import HeaderField |
| 9 | +from segy.schema.segy import SegyStandard |
| 10 | +from upath import UPath |
| 11 | + |
| 12 | +from mdio.commands.segy_helpers.text_headers import _format_text_header |
| 13 | +from mdio.commands.segy_helpers.text_headers import _pager |
| 14 | +from mdio.commands.segy_helpers.text_headers import _read_text_header |
| 15 | + |
| 16 | +if TYPE_CHECKING: # pragma: no cover |
| 17 | + from segy.schema.segy import SegySpec |
| 18 | + |
| 19 | + from mdio.builder.templates.base import AbstractDatasetTemplate |
| 20 | + |
| 21 | +TEXT_ENCODING = TextHeaderEncoding.EBCDIC |
| 22 | +REVISION_MAP = { |
| 23 | + "rev 0": SegyStandard.REV0, |
| 24 | + "rev 1": SegyStandard.REV1, |
| 25 | + "rev 2": SegyStandard.REV2, |
| 26 | + "rev 2.1": SegyStandard.REV21, |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +def prompt_for_segy_standard() -> SegyStandard: |
| 31 | + """Prompt user to select a SEG-Y standard.""" |
| 32 | + choices = list(REVISION_MAP.keys()) |
| 33 | + standard_str = questionary.select("Select SEG-Y standard:", choices=choices, default="rev 1").ask() |
| 34 | + return SegyStandard(REVISION_MAP[standard_str]) |
| 35 | + |
| 36 | + |
| 37 | +def prompt_for_text_encoding() -> TextHeaderEncoding | None: |
| 38 | + """Prompt user for text header encoding (returns TextHeaderEncoding).""" |
| 39 | + choices = [member.name for member in TextHeaderEncoding] |
| 40 | + choice = questionary.select("Select text header encoding:", choices=choices, default=TEXT_ENCODING).ask() |
| 41 | + if choice is None: |
| 42 | + return None |
| 43 | + return TextHeaderEncoding(choice) |
| 44 | + |
| 45 | + |
| 46 | +def prompt_for_header_fields(field_type: str, segy_spec: SegySpec) -> list[HeaderField]: |
| 47 | + """Prompt user to customize header fields with interactive choices.""" |
| 48 | + |
| 49 | + def _get_known_fields() -> list[HeaderField]: |
| 50 | + """Get known fields for the given field type.""" |
| 51 | + if field_type.lower() == "binary": |
| 52 | + return segy_spec.binary.header.fields |
| 53 | + if field_type.lower() == "trace": |
| 54 | + return segy_spec.trace.header.fields |
| 55 | + return [] |
| 56 | + |
| 57 | + def _format_choice(hf: HeaderField) -> str: |
| 58 | + """Format a header field choice for the checkbox.""" |
| 59 | + return f"{hf.name} (byte={hf.byte}, format={hf.format})" |
| 60 | + |
| 61 | + if not questionary.confirm(f"Customize {field_type} header fields?", default=False).ask(): |
| 62 | + return [] |
| 63 | + |
| 64 | + fields = [] |
| 65 | + while True: |
| 66 | + action = questionary.select( |
| 67 | + f"Customize {field_type} header fields — choose an action:", |
| 68 | + choices=[ |
| 69 | + "Add from known fields", |
| 70 | + "Add a new field", |
| 71 | + "View current selections", |
| 72 | + "Clear selections", |
| 73 | + "Done", |
| 74 | + ], |
| 75 | + default="Add a new field", |
| 76 | + ).ask() |
| 77 | + |
| 78 | + if action == "Add from known fields": |
| 79 | + known = _get_known_fields() |
| 80 | + choices = [_format_choice(hf) for hf in known] |
| 81 | + selected = questionary.checkbox(f"Pick {field_type} header fields to add:", choices=choices).ask() |
| 82 | + lookup = {_format_choice(hf): hf for hf in known} |
| 83 | + for label in selected: |
| 84 | + header_field = lookup.get(label) |
| 85 | + if header_field is not None: |
| 86 | + fields.append(header_field) |
| 87 | + |
| 88 | + elif action == "Add a new field": |
| 89 | + name = questionary.text("Field name (e.g., inline):").ask() |
| 90 | + if not name: |
| 91 | + print("Name cannot be empty.") |
| 92 | + continue |
| 93 | + |
| 94 | + byte_str = questionary.text("Starting byte (integer):").ask() |
| 95 | + try: |
| 96 | + byte_val = int(byte_str) |
| 97 | + except (TypeError, ValueError): |
| 98 | + print("Byte must be an integer.") |
| 99 | + continue |
| 100 | + |
| 101 | + from segy.schema.format import ScalarType |
| 102 | + |
| 103 | + fmt_choices = [s.value for s in ScalarType] |
| 104 | + format_ = questionary.select("Data format:", choices=fmt_choices, default="int32").ask() |
| 105 | + if not format_: |
| 106 | + print("Format cannot be empty.") |
| 107 | + continue |
| 108 | + |
| 109 | + try: |
| 110 | + valid_field = HeaderField.model_validate({"name": name, "byte": byte_val, "format": format_}) |
| 111 | + except Exception as exc: # pydantic validation error |
| 112 | + print(f"Invalid field specification: {exc}") |
| 113 | + continue |
| 114 | + fields.append(valid_field) |
| 115 | + |
| 116 | + elif action == "View current selections": |
| 117 | + if not fields: |
| 118 | + print("No custom fields selected yet.") |
| 119 | + else: |
| 120 | + print("Currently selected fields:") |
| 121 | + for i, hf in enumerate(fields, start=1): |
| 122 | + print(f" {i}. {hf.name} (byte={hf.byte}, format={hf.format})") |
| 123 | + |
| 124 | + elif action == "Clear selections": |
| 125 | + if fields and questionary.confirm("Clear all selected fields?", default=False).ask(): |
| 126 | + fields = [] |
| 127 | + |
| 128 | + elif action == "Done": |
| 129 | + break |
| 130 | + |
| 131 | + return fields |
| 132 | + |
| 133 | + |
| 134 | +def prompt_for_mdio_template() -> AbstractDatasetTemplate: |
| 135 | + """Prompt user to select a MDIO template.""" |
| 136 | + from mdio.builder.template_registry import get_template_registry |
| 137 | + |
| 138 | + registry = get_template_registry() |
| 139 | + choices = registry.list_all_templates() |
| 140 | + template_name = questionary.select("Select MDIO template:", choices=choices).ask() |
| 141 | + |
| 142 | + if template_name is None: |
| 143 | + raise typer.Abort |
| 144 | + |
| 145 | + return registry.get(template_name) |
| 146 | + |
| 147 | + |
| 148 | +def interactive_text_header(input_path: UPath) -> TextHeaderEncoding: |
| 149 | + """Run textual header preview and return the chosen encoding.""" |
| 150 | + from segy.standards.registry import get_segy_standard |
| 151 | + |
| 152 | + text_encoding = TextHeaderEncoding.EBCDIC |
| 153 | + segy_spec_preview = get_segy_standard(SegyStandard.REV0) |
| 154 | + segy_spec_preview.text_header.encoding = text_encoding |
| 155 | + segy_spec_preview.endianness = None |
| 156 | + |
| 157 | + if questionary.confirm("Preview textual header now?", default=True).ask(): |
| 158 | + while True: |
| 159 | + main_txt = _read_text_header(input_path, segy_spec_preview) |
| 160 | + formatted_txt = _format_text_header(main_txt, segy_spec_preview.text_header.encoding) |
| 161 | + _pager(formatted_txt) |
| 162 | + |
| 163 | + did_save = False |
| 164 | + if questionary.confirm("Save displayed header(s) to a file?", default=False).ask(): |
| 165 | + default_hdr_uri = input_path.with_name(f"{input_path.stem}_text_header.txt").as_posix() |
| 166 | + out_hdr_uri = questionary.text("Filename for text header:", default=default_hdr_uri).ask() |
| 167 | + if out_hdr_uri: |
| 168 | + with UPath(out_hdr_uri).open("w") as fp: |
| 169 | + fp.write(formatted_txt) |
| 170 | + print(f"Textual header saved to '{out_hdr_uri}'.") |
| 171 | + did_save = True |
| 172 | + |
| 173 | + if did_save: |
| 174 | + break |
| 175 | + |
| 176 | + if not questionary.confirm("Switch encoding and preview again?", default=False).ask(): |
| 177 | + break |
| 178 | + |
| 179 | + new_enc = prompt_for_text_encoding() |
| 180 | + text_encoding = new_enc or text_encoding |
| 181 | + segy_spec_preview.text_header.encoding = text_encoding |
| 182 | + |
| 183 | + return text_encoding |
0 commit comments