import las_reader
l = las_reader.read("GT14574.LAS")
print(type(l))

print(l._text)

"""
Explanation: LAS Reader
las_reader is a Python package to read in Log ASCII Standard (LAS) files, used in geophysical logging. 
The file format is specified by the Canadian Well Logging Society. The package only works for versions 1.2 and 2.0 so far.
Reading in data
The las_reader.read() function accepts a filename or file-like object and returns a Las 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['GCPS'][-12:])
print(l[1][-12:])

print(l['NEUT'][-12:])
print(l[-1][-12:])

"""
Explanation: You can also access curves by index (remembering that the first index is zero but the first curve is the depth). For example, the last twelve values:
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
"""


m = las_reader.las.Metadata(mnemonic="SPEED", unit='something?', value=1.5, descr='The speed of light')
m.value = 3 * 10 ** 8
m.unit = 'm/s'
print(m)

"""
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 a custom Python object which depends on the section of the LAS header in which it occurs: either a Metadata, Curve, or Parameter. All these are very similar though, they are produced by the namedlist package, which is a mutable named tuple. It sounds more complex than it is:
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 section attributes are:
End of explanation
"""


l.well._d

"""
Explanation: A simpler way of viewing this is to use the special _d attribute of each section's dictionary, but note that any changes to this dictionary do not have any effect:
End of explanation
"""


l.well['SRVC'].value = "My logging company -- not anyone's"
l.well._d

"""
Explanation: To change metadata information you have to edit the Metadata object's particular field explicitly:
End of explanation
"""


l.params

"""
Explanation: The other sections have slightly different classes for records:
End of explanation
"""


l.metadata

"""
Explanation: For convenience there's a special attribute which is an ordinary dictionary that brings together all the metadata in the file. Again, changes to this dictionary have no effect.
End of explanation
"""


print l.other

"""
Explanation: As per the standard, the ~Other section is free text. I know many companies put LAS-formatted metadata in here, but this isn't parsed yet.
End of explanation
"""


l.provenance

"""
Explanation: Finally there is some useful information in the provenance attribute that depends on how you opened the file in the first place:
End of explanation
"""


l.curves

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

neutron_log.data

"""
Explanation: Curves (more detail)
The curve definitions in the ~Curve section are stored as Curve objects (just like the Metadata and Parameter objects above). However Curve objects have the data itself as an additional .data attribute:
End of explanation
"""


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

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

l2 = las_reader.read("GT1457B.LAS")
fig = plt.figure(figsize=(18, 5))
gs = gridspec.GridSpec(1, len(l2.curves)-1, wspace=0.2)
for i, curve in enumerate(l2.curves[1:]):
    if i > 0:
        shareax = ax
    else:
        shareax = False
    ax = fig.add_subplot(gs[i], sharey=None)
    ax.plot(curve.data, l2.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)

print(l2._text)

"""
Explanation: Also, the index of the LAS file can be accessed directly by the .index attribute:
End of explanation
"""


