lasio: Read/Write Well Data from LAS Files
The `lasio` library (version 0.32) provides robust tools for reading and writing well data from Log ASCII Standard (LAS) files, supporting both LAS 1.2 and 2.0 specifications. It is actively maintained with regular updates for bug fixes and feature enhancements, typically releasing new minor versions a few times a year.
Common errors
-
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position ...: invalid start byte
cause The LAS file uses an encoding other than the default 'utf-8', such as 'latin-1' or 'cp1252', which cannot be decoded by 'utf-8'.fixSpecify the correct encoding when reading the file, e.g., `l = lasio.read('your_file.las', encoding='latin-1')`. -
AttributeError: 'LASFile' object has no attribute 'well_name' (or similar for other direct well attributes)
cause Well information attributes (like WELL, COMP, DATE) are not directly accessible as properties of the `LASFile` object. They are stored within the `l.well` object.fixAccess well attributes through the `l.well` object and its `Item` objects, e.g., `well_name = l.well.WELL.value` or `company = l.well['COMP'].value`. -
ValueError: No curve mnemonic found for curve with index ...
cause The LAS file contains data columns that do not have corresponding mnemonic definitions in the `~CURVE` section, indicating a malformed file.fixInspect the `~CURVE` section of the LAS file to ensure all data columns are properly defined with a unique mnemonic. If the file is uneditable, you might need to manually clean the data or skip the problematic section.
Warnings
- gotcha LAS files frequently use non-standard encodings (e.g., 'latin-1', 'cp1252') that can cause `UnicodeDecodeError` if not specified. The default encoding for `lasio.read` is 'utf-8'.
- breaking Version 0.20.0 introduced a significant refactor in how curve definitions and curve data are handled, using more explicit `CurveItem` objects. Direct access patterns used in older versions might break.
- gotcha When working with `lasio` data, remember to account for the `NULL` value defined in the LAS file header. `lasio` converts this value to `numpy.nan` by default.
Install
-
pip install lasio
Imports
- lasio.read
import lasio l = lasio.read('my_file.las') - lasio.LASFile
import lasio l = lasio.LASFile()
Quickstart
import lasio
import io
# Simulate a LAS file content for demonstration
las_content = """~VERSION
VERS. 2.00: LAS Version
WRAP. NO: No line wrapping
~WELL
STRT.M 10.00: START DEPTH
STOP.M 20.00: STOP DEPTH
STEP.M 0.50: STEP
NULL. -999.25: NULL VALUE
WELL. EXAMPLE WELL: WELL
COMP. EXAMPLE CO: COMPANY
~CURVE
DEPT.M : Depth
GR.API : Gamma Ray
NEUT.V/V : Neutron
~A DEPT GR NEUT
10.000 100.0 0.300
10.500 105.0 0.310
11.000 110.0 0.320
11.500 112.0 0.315
12.000 115.0 0.325
"""
# Read the LAS file from a string (or from a path: lasio.read('path/to/file.las'))
las_file_obj = io.StringIO(las_content)
l = lasio.read(las_file_obj)
# Access well information
print(f"Well Name: {l.well.WELL.value}")
print(f"Company: {l.well.COMP.value}")
# Access curve data
print(f"Gamma Ray Curve (first 3 values): {l['GR'][:3].tolist()}")
print(f"Depth Curve (last 2 values): {l['DEPT'][-2:].tolist()}")
# Convert to Pandas DataFrame
df = l.df()
print("\nDataFrame head:")
print(df.head())