-
Notifications
You must be signed in to change notification settings - Fork 5
/
las.go
195 lines (168 loc) · 4.44 KB
/
las.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package lasgo
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
)
//LasType is the main type definition
type LasType struct {
path string
content string
}
//Las creates an instance of LasType
func Las(path string) (*LasType, error) {
bs, err := ioutil.ReadFile(path)
l := LasType{path: path}
l.content = string(bs)
return &l, err
}
// WellProps contains basic definition of a single well measurement
type WellProps struct {
unit string
description string
value string
}
//Header - returns the header in the las file
func (l *LasType) Header() ([]string, error) {
err := "file has no header content, make sure to create an instance of LasType with Las function"
if len(l.content) < 1 {
return []string{}, fmt.Errorf(err)
}
hP := pattern("~C(?:\\w*\\s*)*\n\\s*")
spl := strings.Split(hP.Split(l.content, 2)[1], "~")[0]
headers := removeComment(spl)
if len(headers) < 1 {
return []string{}, fmt.Errorf(err)
}
for i, val := range headers {
headers[i] = pattern("\\s+[.]").Split(strings.TrimSpace(val), 2)[0]
}
return headers, nil
}
// Data returns the data section in the file
func (l *LasType) Data() [][]string {
hds, err := l.Header()
if err != nil {
panic("No data in file")
}
sB := pattern("~A(?:\\w*\\s*)*\n").Split(l.content, 2)[1]
sBs := pattern("\\s+").Split(strings.TrimSpace(sB), -1)
return chunk(sBs, len(hds))
}
// DataStruct just like Data but returns output in specified struct format
func (l *LasType) DataStruct(opt *DataOptions) []interface{} {
var (
o *DataOptions
store [][]string
)
if opt != nil {
o = opt
}
hds, err := l.Header()
if err != nil {
panic("No data in file")
}
store = l.Data()
if o != nil {
ctx := context.Background()
output, err := structConvert(ctx, &store, hds, o)
if err != nil {
panic(err)
}
return output
}
return nil
}
// Version - returns the version of the las file
func (l *LasType) Version() (version string) {
version, _ = metadata(l.content)
return
}
// Wrap - returns the version of the las file
func (l *LasType) Wrap() (wrap bool) {
_, wrap = metadata(l.content)
return
}
// ColumnCount - Returns the number of columns in a .las file
func (l *LasType) ColumnCount() (count int) {
header, _ := l.Header()
// TODO: handle error
count = len(header)
return
}
// RowCount - Returns the number of rowa in a .las file
func (l *LasType) RowCount() (count int) {
count = len(l.Data())
return
}
// Column returns entry of an individual log, say gamma ray
func (l *LasType) Column(key string) []string {
header, _ := l.Header()
// TODO: handle error
data := l.Data()
var res []string
keyIndex := index(header, key)
// TODO: handle when keyIndex is -1
for _, val := range data {
res = append(res, val[keyIndex])
}
return res
}
// HeaderAndDesc return the name and description of each log entry
func (l *LasType) HeaderAndDesc() map[string]string {
// const cur = (await this.property('curve')) as object;
curve, _ := property(l.content, "curve")
res := make(map[string]string)
// TODO: handle error
for key, val := range curve {
res[key] = val.description
}
// TODO: handle when res is empty
return res
}
// CurveParams - Returns Curve Parameters
func (l *LasType) CurveParams() map[string]WellProps {
curve, _ := property(l.content, "curve")
// TODO: handle error
return curve
}
// WellParams - Returns Overrall Well Parameters
func (l *LasType) WellParams() map[string]WellProps {
well, _ := property(l.content, "well")
// TODO: handle error
return well
}
// LogParams - Returns Log Parameters
func (l *LasType) LogParams() map[string]WellProps {
param, _ := property(l.content, "param")
// TODO: handle error
return param
}
// Other returns extra information stored in ~other section
func (l *LasType) Other() string {
// TODO: make case insensitive
som := pattern("~O(?:\\w*\\s*)*\n\\s*").Split(l.content, 2)
if len(som) > 1 {
res := pattern("\n\\s*").ReplaceAllString(strings.Split(som[1], "~")[0], " ")
return strings.Join(removeComment(res), "\n")
}
return ""
}
// ToCSV creates a csv file using data and header
func (l *LasType) ToCSV(filename string) {
file, err := os.Create(fmt.Sprintf("%s.csv", filename))
if err != nil {
panic(err)
}
// close file when function call ends
defer file.Close()
header, _ := l.Header()
// TODO: handle error
file.WriteString(strings.Join(header, ",") + "\n")
for _, val := range l.Data() {
// TODO: don't include \n at the last line
file.WriteString(strings.Join(val, ",") + "\n")
}
}