Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update_curve and replace_curve_item methods to LASFile (fixes #478) #479

Merged
merged 5 commits into from
Aug 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions lasio/las.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,17 @@ def __setitem__(self, key, value):
key, value.mnemonic
)
)
self.append_curve_item(value)
if key in self.curves.keys():
ix = self.curves.keys().index(key)
self.replace_curve_item(ix, value)
else:
self.append_curve_item(value)
else:
# Assume value is an ndarray
self.append_curve(key, value)
if key in self.curves.keys():
self.update_curve(mnemonic=key, data=value)
else:
self.append_curve(key, value)

def keys(self):
"""Return curve mnemonics."""
Expand Down Expand Up @@ -999,6 +1006,18 @@ def insert_curve_item(self, ix, curve_item):
assert isinstance(curve_item, CurveItem)
self.curves.insert(ix, curve_item)


def replace_curve_item(self, ix, curve_item):
"""Replace a CurveItem.

Args:
ix (int): position to insert CurveItem i.e. 0 for start
curve_item (lasio.CurveItem)

"""
self.delete_curve(ix=ix)
self.insert_curve_item(ix, curve_item)

def add_curve(self, *args, **kwargs):
"""Deprecated. Use append_curve() or insert_curve() instead."""
return self.append_curve(*args, **kwargs)
Expand Down Expand Up @@ -1049,6 +1068,32 @@ def delete_curve(self, mnemonic=None, ix=None):
ix = self.curves.keys().index(mnemonic)
self.curves.pop(ix)

def update_curve(self, mnemonic=None, ix=None, data=False, unit=False, descr=False, value=False):
"""Update a curve.

Keyword Arguments:
ix (int): index of curve in LASFile.curves.
mnemonic (str): mnemonic of curve.
data (ndarray): new data array (False if no update desired)
unit (str): new value for unit (False if no update desired)
descr (str): new description (False if no update desired)
value (str/int/float etc): new value (False if no update desired)

The index takes precedence over the mnemonic.

"""
if ix is None:
ix = self.curves.keys().index(mnemonic)
curve = self.curves[ix]
if data is not False:
curve.data = data
if unit is not False:
curve.unit = unit
if descr is not False:
curve.descr = descr
if value is not False:
curve.value = value

@property
def json(self):
"""Return object contents as a JSON string."""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import lasio
import lasio.examples
from lasio import read, las

import logging
Expand Down Expand Up @@ -89,3 +90,17 @@ def test_data_attr():
logger.debug("las.data = {}".format(las.data))
# the .all() method assumes these are numpy ndarrays; that should be the case.
assert (las.data == np.asarray([[1, 4, 7], [2, 5, 8], [3, 6, 9]])).all()

def test_update_curve():
las = lasio.examples.open("sample.las")
las["NPHI"] = las["NPHI"] * 100
assert "NPHI" in las.keys()
assert not "NPHI:1" in las.keys()
assert not "NPHI:2" in las.keys()

def test_replace_curve():
las = lasio.examples.open("sample.las")
las["NPHI"] = lasio.CurveItem("NPHI", "%", "Porosity", data=(las["NPHI"] * 100))
assert las.keys() == ["DEPT", "DT", "RHOB", "NPHI", "SFLU", "SFLA", "ILM", "ILD"]
assert (las["NPHI"] == [45, 45, 45]).all()