-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmylastool.py
160 lines (116 loc) · 4.09 KB
/
mylastool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""My tool for working with LAS files in an azure storage container."""
import os
import sys
import azure.storage.blob
def get_container_url():
"""Get url for a container."""
return os.environ['CONTAINER_URL']
def get_container_from_url(url):
"""Create container from a url."""
return azure.storage.blob.ContainerClient.from_container_url(url)
def get_list_of_lasfiles(container):
"""Get list of LAS files in a container."""
files = []
for blob in container.list_blobs():
if blob.name.upper().endswith('.LAS'):
files.append(blob.name)
return files
def print_list_of_lasfiles(container):
"""Print pretty directory listing LAS file in container."""
files = get_list_of_lasfiles(container)
for name in files:
print(name)
def read_lasfile(container, filename):
"""Read given LAS file from container."""
if not filename.upper().endswith('.LAS'):
raise OSError("Probably not a LAS file")
blob_client = container.get_blob_client(filename)
data = blob_client.download_blob().content_as_bytes()
lines = []
for line in data.splitlines():
lines.append(line.decode("ascii", errors='ignore'))
return lines
def find_section_index(lines, prefix):
"""Find index of first line with given prefix."""
for index, line in enumerate(lines):
if line.strip().startswith(prefix):
return index
raise ValueError
def get_header_section(lines):
"""Return the lines for the header section."""
return lines[:find_section_index(lines, '~A')]
def get_data_section(lines):
"""Return the lines for the data section."""
return lines[find_section_index(lines, '~A')+1:]
def get_curve_section(lines):
"""Return the lines for the curve section."""
start_idx = find_section_index(lines, '~C')
end_idx = find_section_index(lines[start_idx+1:], '~')
return lines[start_idx+1:start_idx+1+end_idx]
def get_curve_mnemonics(lines):
"""Get a list of curve names."""
names = []
for line in get_curve_section(lines):
if line.strip().startswith("#"):
continue
curvename = line.split('.')[0].strip()
names.append(curvename)
return names
def print_header_section(lines):
"""Print the header section."""
for line in get_header_section(lines):
print(line)
def print_data_section(lines):
"""Print the data section."""
for line in get_data_section(lines):
print(line)
def print_curve_mnemonics(lines):
"""Pretty print the curve names."""
print(*get_curve_mnemonics(lines), sep=' | ')
def print_helpmessage():
"""Print help message."""
print("usage: mylastool.py <command> [file]")
print("examples:")
print(" python mylastool.py list")
print(" python mylastool.py header A/B/C.LAS")
print(" python mylastool.py data A/B/C.LAS")
print(" python mylastool.py curves A/B/C.LAS")
print("also, remember to set CONTAINER_URL")
def main(argv):
"""Parse a list of arguments and do magic."""
if len(argv) < 2:
print_helpmessage()
return 1
command = argv[1]
if command not in ('list', 'header', 'data', 'curves'):
print('error: unknown command')
print_helpmessage()
return 1
url = get_container_url()
container = get_container_from_url(url)
if command == 'list':
print_list_of_lasfiles(container)
return 0
if len(argv) < 3:
print('error: expected a filename')
print_helpmessage()
return 1
lasfile = argv[2]
lines = read_lasfile(container, lasfile)
if command == 'header':
print_header_section(lines)
return 0
if command == 'data':
print_data_section(lines)
return 0
if command == 'curves':
print_curve_mnemonics(lines)
return 0
print('Huh?')
print_helpmessage()
return 1
if __name__ == '__main__':
sys.exit(main(sys.argv))
# References:
# https://www.cwls.org/wp-content/uploads/2017/02/Las2_Update_Feb2017.pdf
# https://docs.microsoft.com/en-us/python/api/azure-storage-blob/?view=azure-python