import os, sys; sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

import fnmatch

import numpy
import pytest
import math

from lasio import las, read, exceptions


test_dir = os.path.dirname(__file__)

egfn = lambda fn: os.path.join(os.path.dirname(__file__), "examples", fn)
stegfn = lambda vers, fn: os.path.join(
    os.path.dirname(__file__), "examples", vers, fn)


def test_autodepthindex():
    m = read(egfn("autodepthindex_M.las"))
    f = read(egfn("autodepthindex_F.las"))
    ft = read(egfn("autodepthindex_FT.las"))
    err = read(egfn("autodepthindex_M_FT.las"))


def test_autodepthindex_inconsistent():
    err = read(egfn("autodepthindex_M_FT.las"))
    with pytest.raises(exceptions.LASUnknownUnitError):
        print(err.depth_m)


def test_autodepthindex_m():
    l = read(egfn("autodepthindex_M.las"))
    assert (l.depth_ft[-1] * 0.3048 == l.index[-1])


def test_autodepthindex_f():
    l = read(egfn("autodepthindex_F.las"))
    assert (l.depth_m[-1] / 0.3048 == l.index[-1])


def test_autodepthindex_ft():
    l = read(egfn("autodepthindex_FT.las"))
    assert (l.depth_m[-1] / 0.3048 == l.index[-1])

def test_autodepthindex_feet():
    l = read(egfn("autodepthindex_FEET.las"))
    assert (l.depth_m[-1] / 0.3048 == l.index[-1])

def test_df_indexing():
    l = read(egfn("6038187_v1.2.las"))
    metres = 9.05
    spacing = l.well["STEP"].value
    calc_index = math.floor((metres / spacing) - (l.well["STRT"].value / spacing))
    calc_index = int(calc_index)
    assert l["GAMN"][calc_index] == l.df()["GAMN"][metres]


# TODO: make above test in reverse-ordered LAS (e.g. STRT > STOP)

def test_df_reverse():
    l = read(egfn("sample_rev.las"))
    metres = 1667
    spacing = l.well["STEP"].value
    calc_index = math.floor((metres // spacing) - (l.well["STRT"].value // spacing))
    calc_index = int(calc_index)
    assert l["DT"][calc_index] == l.df()["DT"][metres]

def test_df_curve_names():
    l = read(egfn("sample_rev.las"))
    assert l.keys()[1:] == list(l.df().columns.values)

def test_non_standard_section():
    l = read(egfn("non-standard-header-section.las"))
    assert "SPECIAL INFORMATION" in l.sections.keys()

def test_non_standard_sections():
    l = read(egfn("non-standard-header-sections.las"))
    assert "SPECIAL INFORMATION" in l.sections.keys()
    assert "extra special information" in l.sections.keys()
@@ -87,16 +87,43 @@
  fnmatch
+%0Afrom pprint import pformat
 %0A%0Aimport
@@ -156,16 +156,29 @@
 t math%0A%0A
+import lasio%0A
 from las
@@ -2397,28 +2397,158 @@
 mation%22 in l.sections.keys()
+%0A%0Adef test_repr():%0A    h = lasio.HeaderItem('MN', unit='m', value=20, descr='test testing')%0A    assert h.__repr__() == pformat(h)%0A

add repr test