import os

import lasio

l = lasio.read(os.path.join("..", "tests", "examples", "6038187_v1.2.las"))
print(type(l))

print(l._text)

"""
Explanation: Reading in data
The las_reader.read() function accepts a filename, string, URL, or file-like object and returns a LASFile object:
End of explanation
"""


l.keys()

l['NEUT']

"""
Explanation: Curve data
Curves are available as numpy.ndarray objects as items in the Las object itself:
End of explanation
"""


print(l['GAMN'])
print(l[2])

print(l["COND"])
print(l[-1])

"""
Explanation: You can also access curves by index (remembering that the first index is zero but the first curve is the depth).
End of explanation
"""


print(l.data.shape)
print(l.data)

"""
Explanation: All the curves are stored as a two-dimensional ndarray under the data attribute:
End of explanation
"""


from lasio.las import HeaderItem

example = HeaderItem(mnemonic="ROP", unit="m/min", value=2.3, descr="Rate of penetration")

print(example)

print(example.mnemonic)
print(example.unit)
print(example.value)
print(example.descr)

"""
Explanation: Header information
Metadata from the LAS file's header is stored in an OrderedDictionary attribute, one for each section (version, well, curves, and params). OrderedDictionary objects are just what they sound like: very similar to a normal Python dictionary.
Each entry in the header sections is stored by mutable named tuple called a HeaderItem, which is created using the namedlist library. It's very straightforward:
End of explanation
"""


l.version

l.version['VERS'].value

l.well

"""
Explanation: In order from top to bottom as they usually are in LAS files, the header sections are:
End of explanation
"""


l.well['SRVC'].value = "My logging company -- not anyone else's"
print(l.well["SRVC"])

"""
Explanation: To change metadata information you have to edit the relevant field of the HeaderItem object:
End of explanation
"""


l.params

"""
Explanation: The other section with header items is very similar...
End of explanation
"""


l.metadata

"""
Explanation: For convenience there's a special attribute which brings together all the header metadata mnemonics and values
End of explanation
"""


print l.other

"""
Explanation: The ~Other section is left as raw text.
End of explanation
"""


l.curves

neutron_log = l.get_curve("NEUT")
print(neutron_log)

"""
Explanation: Note that commented-out lines beginning with # elsewhere in the file are not read in at all at the moment (this will be fixed in the future).
Curves (more detail)
The curve definitions in the ~Curve section are stored as Curve objects. The only difference between a Curve object and a HeaderItem is that the former have an additional data field:
End of explanation
"""


print(l["DEPT"])
print(l.index)

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
%matplotlib inline

fig = plt.figure(figsize=(18, 5))
gs = gridspec.GridSpec(1, len(l.curves)-1, wspace=0.2)
for i, curve in enumerate(l.curves[1:]):
    if i > 0:
        shareax = ax
    else:
        shareax = False
    ax = fig.add_subplot(gs[i], sharey=None)
    ax.plot(curve.data, l.index)
    ax.set_title(curve.mnemonic)
    if i > 0:
        ax.set_yticklabels([])
    ax.set_ylim(*ax.get_ylim()[::-1])
    ax.set_xticklabels(["%.0f" % xi if xi in ax.get_xlim() else "" for xi in ax.get_xticks()], rotation=30)

"""
Explanation: Notice of course that whereas l["NEUT"] returns you a numpy.ndarray directly, using the get_curve method gives you the whole Curve object, with the unit, value (usually an API curve type code), and descr attributes as well as the data.
The reference/index curve of the LAS file (e.g. the depth or time column) can be accessed directly by the .index attribute:
End of explanation
"""


