|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from datetime import UTC |
| 6 | +from datetime import datetime |
5 | 7 | from typing import TYPE_CHECKING |
6 | 8 |
|
7 | 9 | from mdio.api.io import _normalize_path |
| 10 | +from mdio.api.io import open_mdio |
8 | 11 | from mdio.api.io import to_mdio |
9 | 12 | from mdio.builder.template_registry import TemplateRegistry |
10 | 13 | from mdio.builder.xarray_builder import to_xarray_dataset |
@@ -84,3 +87,81 @@ def create_empty( # noqa PLR0913 |
84 | 87 | meta_ds = dataset[drop_vars_delayed + ["trace_mask"]] |
85 | 88 | to_mdio(meta_ds, output_path=output_path, mode="r+", compute=True) |
86 | 89 |
|
| 90 | + |
| 91 | +def create_empty_like( # noqa PLR0913 |
| 92 | + input_path: UPath | Path | str, |
| 93 | + output_path: UPath | Path | str, |
| 94 | + keep_coordinates: bool = False, |
| 95 | + overwrite: bool = False, |
| 96 | +) -> xr_Dataset: |
| 97 | + """A function that creates an empty MDIO v1 file with the same structure as an existing one. |
| 98 | +
|
| 99 | + Args: |
| 100 | + input_path: The path of the input MDIO file. |
| 101 | + output_path: The path of the output MDIO file. |
| 102 | + If None, the output will not be written to disk. |
| 103 | + keep_coordinates: Whether to keep the coordinates in the output file. |
| 104 | + overwrite: Whether to overwrite the output file if it exists. |
| 105 | +
|
| 106 | + Returns: |
| 107 | + The output MDIO dataset. |
| 108 | +
|
| 109 | + Raises: |
| 110 | + FileExistsError: If the output location already exists and overwrite is False. |
| 111 | + """ |
| 112 | + input_path = _normalize_path(input_path) |
| 113 | + output_path = _normalize_path(output_path) if output_path is not None else None |
| 114 | + |
| 115 | + if not overwrite and output_path is not None and output_path.exists(): |
| 116 | + err = f"Output location '{output_path.as_posix()}' exists. Set `overwrite=True` if intended." |
| 117 | + raise FileExistsError(err) |
| 118 | + |
| 119 | + ds = open_mdio(input_path) |
| 120 | + |
| 121 | + # Create a copy with the same structure but no data or, |
| 122 | + # optionally, coordinates |
| 123 | + ds_output = ds.copy(data=None).reset_coords(drop=not keep_coordinates) |
| 124 | + |
| 125 | + # Dataset |
| 126 | + # Keep the name (which is the same as the used template name) and the original API version |
| 127 | + # ds_output.attrs["name"] |
| 128 | + # ds_output.attrs["apiVersion"] |
| 129 | + ds_output.attrs["createdOn"] = datetime.now(UTC) |
| 130 | + |
| 131 | + # Coordinates |
| 132 | + if not keep_coordinates: |
| 133 | + for coord_name in ds_output.coords: |
| 134 | + ds_output[coord_name].attrs["unitsV1"] = None |
| 135 | + |
| 136 | + # MDIO attributes |
| 137 | + attr = ds_output.attrs["attributes"] |
| 138 | + if attr is not None: |
| 139 | + attr.pop("gridOverrides", None) # Empty dataset should not have gridOverrides |
| 140 | + # Keep the original values for the following attributes |
| 141 | + # attr["defaultVariableName"] |
| 142 | + # attr["surveyType"] |
| 143 | + # attr["gatherType"] |
| 144 | + |
| 145 | + # "All traces should be marked as dead in empty dataset" |
| 146 | + if "trace_mask" in ds_output.variables: |
| 147 | + ds_output["trace_mask"][:] = False |
| 148 | + |
| 149 | + # Data variable |
| 150 | + var_name = attr["defaultVariableName"] |
| 151 | + var = ds_output[var_name] |
| 152 | + var.attrs["statsV1"] = None |
| 153 | + if not keep_coordinates: |
| 154 | + var.attrs["unitsV1"] = None |
| 155 | + |
| 156 | + # SEG-Y file header |
| 157 | + if "segy_file_header" in ds_output.variables: |
| 158 | + segy_file_header = ds_output["segy_file_header"] |
| 159 | + if segy_file_header is not None: |
| 160 | + segy_file_header.attrs["textHeader"] = None |
| 161 | + segy_file_header.attrs["binaryHeader"] = None |
| 162 | + segy_file_header.attrs["rawBinaryHeader"] = None |
| 163 | + |
| 164 | + if output_path is not None: |
| 165 | + to_mdio(ds_output, output_path=output_path, mode="w", compute=True) |
| 166 | + |
| 167 | + return ds_output |
0 commit comments