Skip to content

Commit

Permalink
Enable writing empty LAS file
Browse files Browse the repository at this point in the history
- Handle exceptions on empty data
- Add test case for writing an empty LAS file: test_write_empty_las()
  • Loading branch information
dcslagel committed Apr 22, 2021
1 parent 119fb31 commit 43d07c4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
31 changes: 20 additions & 11 deletions lasio/las.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,30 +379,39 @@ def update_start_stop_step(self, STRT=None, STOP=None, STEP=None, fmt="%.5f"):
# standard precision.
# If they are passed in with values, don't format them because we
# assume they are at the user's expected precision.
if STRT is None:
STRT = fmt % self.index[0]
if STOP is None:
STOP = fmt % self.index[-1]
if STEP is None:
# prevents an error being thrown in the case of only a single sample being written
if STOP != STRT:
raw_step = self.index[1] - self.index[0]
STEP = fmt % raw_step

# If the 'try' fails because self.index doesn't exist or is empty
# then use the default or parameter values for STRT, STOP, and STEP.
try:
if STRT is None:
STRT = fmt % self.index[0]
if STOP is None:
STOP = fmt % self.index[-1]
if STEP is None:
# prevents an error being thrown in the case of only a single sample being written
if STOP != STRT:
raw_step = self.index[1] - self.index[0]
STEP = fmt % raw_step
except IndexError:
pass

self.well["STRT"].value = STRT
self.well["STOP"].value = STOP
self.well["STEP"].value = STEP

def update_units(self):
# Check units
if self.curves[0].unit:
if self.curves and self.curves[0].unit:
unit = self.curves[0].unit
else:
unit = self.well["STRT"].unit
self.well["STRT"].unit = unit
self.well["STOP"].unit = unit
self.well["STEP"].unit = unit
self.curves[0].unit = unit
# Check that curves exists to avoid throwing an expection.
# to write to an non-existant object.
if self.curves:
self.curves[0].unit = unit

def write(self, file_ref, **kwargs):
"""Write LAS file to disk.
Expand Down
12 changes: 9 additions & 3 deletions lasio/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,16 @@ def write(
file_object.write("\n")
line_counter = len(lines)

# Set empty defaults for nrows and ncols
nrows, ncols = (0, 0)

# data_arr = np.column_stack([c.data for c in las.curves])
data_arr = las.data
nrows, ncols = data_arr.shape
logger.debug("Data section shape: {}".format((nrows, ncols)))
try:
data_arr = las.data
nrows, ncols = data_arr.shape
logger.debug("Data section shape: {}".format((nrows, ncols)))
except ValueError as err:
logger.debug("Data section is empty")

logger.debug("len_numeric_field = {}".format(len_numeric_field))
if len_numeric_field is None:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,40 @@ def test_wrong_stop_value():
assert las.well["STEP"].value == "-0.12500"

os.remove(testfn)


def test_write_empty_las():
las = lasio.las.LASFile()

s = StringIO()
las.write(s, version=2.0)
s.seek(0)
assert (
s.read()
== """~Version ---------------------------------------------------
VERS. 2.0 : CWLS log ASCII Standard -VERSION 2.0
WRAP. NO : One line per depth step
DLM . SPACE : Column Data Section Delimiter
~Well ------------------------------------------------------
STRT.m 0 : START DEPTH
STOP.m 0 : STOP DEPTH
STEP.m 0 : STEP
NULL. -9999.25 : NULL VALUE
COMP. : COMPANY
WELL. : WELL
FLD . : FIELD
LOC . : LOCATION
PROV. : PROVINCE
CNTY. : COUNTY
STAT. : STATE
CTRY. : COUNTRY
SRVC. : SERVICE COMPANY
DATE. : DATE
UWI . : UNIQUE WELL ID
API . : API NUMBER
~Curve Information -----------------------------------------
~Params ----------------------------------------------------
~Other -----------------------------------------------------
~ASCII -----------------------------------------------------
"""
)

0 comments on commit 43d07c4

Please sign in to comment.