import gio

ds = gio.read_odt('../../data/OdT/3d_horizon/Segment_ILXL_Single-line-header.dat')
ds

ds['twt'].plot()

"""
Explanation: Read OpendTect horizons
The best way to export horizons from OpendTect is with these options:

x/y and inline/crossline
with header (single or multi-line, it doesn't matter)
choose all the attributes you want

On the last point, if you choose multiple horizons in one file, you can only have one attribute in the file. 
IL/XL only, single-line header, multiple attributes
End of explanation
"""


ds = gio.read_odt('../../data/OdT/3d_horizon/Segment_XY-and-ILXL_Multi-line-header.dat')
ds

import matplotlib.pyplot as plt

plt.scatter(ds.coords['cdp_x'], ds.coords['cdp_y'], s=5)

"""
Explanation: IL/XL and XY, multi-line header, multiple attributes
Load everything (default)
X and Y are loaded as cdp_x and cdp_y, to be consistent with the seisnc standard in segysak.
End of explanation
"""


fname = '../../data/OdT/3d_horizon/Segment_XY-and-ILXL_Multi-line-header.dat'
names = ['Inline', 'Crossline', 'Z']  # Must match OdT DAT file.

ds = gio.read_odt(fname, names=names)
ds

"""
Explanation: Load only inline, crossline, TWT
There is only one attribute here: Z, which is the two-way time of the horizon.
Note that when loading data from OpendTect, you always get an xarray.Dataset, even if there's only a single attribute. This is because the format supports multiple grids and we didn't want you to have to guess what a given file would produce.
End of explanation
"""


fname = '../../data/OdT/3d_horizon/Segment_XY_Single-line-header.dat'

ds = gio.read_odt(fname, origin=(376, 812), step=(2, 2))
ds

ds['twt'].plot()

"""
Explanation: XY only
If you have a file with no IL/XL, gio can try to load data using only X and Y:

If there's a header you can load any number of attributes.
If there's no header, you can only one attribute (e.g. TWT) automagically...
OR, if there's no header, you can provide names to tell gio what everything is.

gio must create fake inline and crossline numbers; you can provide an origin and a step size. For example, notice above that the true inline and crossline numbers are:

inline: 376, 378, 380, etc.
crossline: 812, 814, 816, etc.

So we can pass an origin of (376, 812) and a step of (2, 2) to mimic these.
Header present
End of explanation
"""


fname = '../../data/OdT/3d_horizon/Segment_XY_No-header.dat'

ds = gio.read_odt(fname)
ds

# Raises an error:

fname = '../../data/OdT/3d_horizon/Segment_XY_No-header.dat'

ds = gio.read_odt(fname, names=['X', 'Y', 'TWT'])
ds

ds['twt'].plot()

"""
Explanation: No header, more than one attribute: raises an error
End of explanation
"""


fname = '../../data/OdT/3d_horizon/Nimitz_Salmon_XY-and-ILXL_Single-line-header.dat'

ds = gio.read_odt(fname)
ds

ds['twt'].plot.imshow()

"""
Explanation: Sparse data
Sometimes a surface only exists at a few points, e.g. a 3D seismic interpretation grid. In general, loading data like this is completely safe if you have inline and xline locations. If you only have (x, y) locations, gio will attempt to load it, but you should inspect the result carefullly.
End of explanation
"""


ds['twt'].plot()

"""
Explanation: There's some sort of artifact with the default plot style, which uses pcolormesh I think.
End of explanation
"""


fname = '../../data/OdT/3d_horizon/F3_Multi-H2-H4_ILXL_Single-line-header.dat'

ds = gio.read_odt(fname)
ds

ds['F3_Demo_2_FS6'].plot()

ds['F3_Demo_4_Truncation'].plot()

"""
Explanation: Multiple horizons in one file
You can export multiple horizons from OpendTect. These will be loaded as one xarray.Dataset as different Data variables. (The actual attribute you exported from OdT is always called Z; this information is not retained in the xarray.)
End of explanation
"""


import gio

fname = '../../data/OdT/3d_horizon/Test_Multi_XY-and-ILXL_Z-only.dat'

ds = gio.read_odt(fname, names=['Horizon', 'X', 'Y', 'Inline', 'Crossline', 'Z'])
ds

"""
Explanation: Multi-horizon, no header
Unfortunately, OdT exports (x, y) in the first two columns, meaning you can't assume that columns 3 and 4 are inline, crossline. So if there's no header, and XY as well as inline/xline, you have to give the column names:
End of explanation
"""


fname = '../../data/OdT/3d_horizon/Segment_XY_No-header_NULLs.dat'

ds = gio.read_odt(fname, names=['X', 'Y', 'TWT'])
ds

ds['twt'].plot()

"""
Explanation: Undefined values
These are exported as '1e30' by default. You can override this (not add to it, which is the default pandas behaviour) by passing one or more na_values.
End of explanation
"""


_ = gio.to_odt('out.dat', ds)

!head out.dat

"""
Explanation: Writing OdT files
You can write an OdT file using gio.to_odt():
End of explanation
"""


