1
|
|
|
import argparse |
2
|
|
|
import glob |
3
|
|
|
import os |
4
|
|
|
import sys |
5
|
|
|
import traceback |
6
|
|
|
|
7
|
|
|
import numpy |
8
|
|
|
import openpyxl |
9
|
|
|
|
10
|
|
|
from . import las |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class ExcelConverter(object): |
14
|
|
|
|
15
|
|
|
'''Provide ability to export LAS data into an Excel spreadsheet. |
16
|
|
|
|
17
|
|
|
Arguments: |
18
|
|
|
las (:class:`lasio.las.LASFile` object) |
19
|
|
|
|
20
|
|
|
''' |
21
|
|
|
|
22
|
|
|
def __init__(self, las): |
23
|
|
|
self.set_las(las) |
24
|
|
|
|
25
|
|
|
def set_las(self, las): |
26
|
|
|
'''Set LASFile object. |
27
|
|
|
|
28
|
|
|
Arguments: |
29
|
|
|
las (:class:`lasio.las.LASFile` object) |
30
|
|
|
|
31
|
|
|
''' |
32
|
|
|
self.las = las |
33
|
|
|
self.generate_workbook() |
34
|
|
|
return self |
35
|
|
|
|
36
|
|
|
def generate_workbook(self): |
37
|
|
|
'''Generate the Excel workbook object. |
38
|
|
|
|
39
|
|
|
Two sheets are created: |
40
|
|
|
|
41
|
|
|
* Header: contains all the header sections and metadata |
42
|
|
|
* Curves: contains the data |
43
|
|
|
|
44
|
|
|
''' |
45
|
|
|
wb = openpyxl.Workbook() |
46
|
|
|
header = wb['Sheet'] |
47
|
|
|
header.title = 'Header' |
48
|
|
|
curves = wb.create_sheet() |
49
|
|
|
curves.title = 'Curves' |
50
|
|
|
|
51
|
|
|
def write_cell(sh, i, j, value): |
52
|
|
|
c = sh.cell(row=i + 1, column=j + 1) |
53
|
|
|
c.value = value |
54
|
|
|
|
55
|
|
|
write_cell(header, 0, 0, 'Section') |
56
|
|
|
write_cell(header, 0, 1, 'Mnemonic') |
57
|
|
|
write_cell(header, 0, 2, 'Unit') |
58
|
|
|
write_cell(header, 0, 3, 'Value') |
59
|
|
|
write_cell(header, 0, 4, 'Description') |
60
|
|
|
|
61
|
|
|
sections = [ |
62
|
|
|
('~Version', self.las.version), |
63
|
|
|
('~Well', self.las.well), |
64
|
|
|
('~Parameter', self.las.params), |
65
|
|
|
('~Curves', self.las.curves), |
66
|
|
|
] |
67
|
|
|
|
68
|
|
|
n = 1 |
69
|
|
|
for sect_name, sect in sections: |
70
|
|
|
for i, item in enumerate(sect.values()): |
71
|
|
|
write_cell(header, n, 0, sect_name) |
72
|
|
|
write_cell(header, n, 1, item.mnemonic) |
73
|
|
|
write_cell(header, n, 2, item.unit) |
74
|
|
|
write_cell(header, n, 3, item.value) |
75
|
|
|
write_cell(header, n, 4, item.descr) |
76
|
|
|
n += 1 |
77
|
|
|
|
78
|
|
|
for i, curve in enumerate(self.las.curves): |
79
|
|
|
write_cell(curves, 0, i, curve.mnemonic) |
80
|
|
|
for j, value in enumerate(curve.data): |
81
|
|
|
if numpy.isnan(value): |
82
|
|
|
write_cell(curves, j + 1, i, '') |
83
|
|
|
else: |
84
|
|
|
write_cell(curves, j + 1, i, value) |
85
|
|
|
|
86
|
|
|
self.workbook = wb |
87
|
|
|
return self |
88
|
|
|
|
89
|
|
|
def write(self, xlsxfn): |
90
|
|
|
'''Write the Excel workbook to an ``.xlsx`` file. |
91
|
|
|
|
92
|
|
|
Arguments: |
93
|
|
|
xlsxfn (str): filename (will be overwritten without warning) |
94
|
|
|
|
95
|
|
|
''' |
96
|
|
|
assert xlsxfn.lower().endswith('.xlsx') |
97
|
|
|
|
98
|
|
|
self.workbook.save(xlsxfn) |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
def main(): |
102
|
|
|
args = get_parser().parse_args(sys.argv[1:]) |
103
|
|
|
lasfn = args.LAS_filename |
104
|
|
|
xlsxfn = args.XLSX_filename |
105
|
|
|
|
106
|
|
|
l = las.LASFile(lasfn) |
107
|
|
|
converter = ExcelConverter(l) |
108
|
|
|
converter.write(xlsxfn) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def get_parser(): |
112
|
|
|
parser = argparse.ArgumentParser('Convert LAS file to XLSX', |
113
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
114
|
|
|
parser.add_argument('LAS_filename') |
115
|
|
|
parser.add_argument('XLSX_filename') |
116
|
|
|
return parser |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
def main_bulk(): |
120
|
|
|
args = get_bulk_parser().parse_args(sys.argv[1:]) |
121
|
|
|
paths = [] |
122
|
|
|
if args.recursive: |
123
|
|
|
for dirpath, dirnames, filenames in os.walk(args.path): |
124
|
|
|
paths.append(dirpath) |
125
|
|
|
else: |
126
|
|
|
paths.append(args.path) |
127
|
|
|
|
128
|
|
|
for path in paths: |
129
|
|
|
for lasfn in glob.glob(os.path.join(path, args.glob)): |
130
|
|
|
xlsxfn = lasfn.lower().replace('.las', '.xlsx') |
131
|
|
|
print('Converting %s -> %s' % (lasfn, xlsxfn)) |
132
|
|
|
try: |
133
|
|
|
l = las.LASFile(lasfn, ignore_header_errors=args.ignore_header_errors) |
134
|
|
|
converter = ExcelConverter(l) |
135
|
|
|
converter.write(xlsxfn) |
136
|
|
|
except: |
137
|
|
|
print('Failed to convert file. Error message:\n' |
138
|
|
|
+ traceback.format_exc()) |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
def get_bulk_parser(): |
142
|
|
|
parser = argparse.ArgumentParser('Convert LAS files to XLSX', |
143
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
144
|
|
|
parser.add_argument('-g', '--glob', default='*.las', help='Match LAS files with this pattern') |
145
|
|
|
parser.add_argument('-r', '--recursive', action='store_true', help='Recurse through subfolders.', default=False) |
146
|
|
|
parser.add_argument('-i', '--ignore-header-errors', action='store_true', help='Ignore header section errors.', default=False) |
147
|
|
|
parser.add_argument('path') |
148
|
|
|
return parser |
149
|
|
|
|
150
|
|
|
if __name__ == '__main__': |
151
|
|
|
main() |
152
|
|
|
|