-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbor2diggs.py
331 lines (289 loc) · 11.3 KB
/
bor2diggs.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
import xml.etree.ElementTree as ET
from datetime import date, datetime
from xml.dom import minidom
import bor
import click
__version__ = "0.1.0.dev"
VERSION = __version__
# Register necessary namespaces
namespaces = {
"": "http://diggsml.org/schemas/2.6",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xlink": "http://www.w3.org/1999/xlink",
"gml": "http://www.opengis.net/gml/3.2",
"g3.3": "http://www.opengis.net/gml/3.3/ce",
"glr": "http://www.opengis.net/gml/3.3/lr",
"glrov": "http://www.opengis.net/gml/3.3/lrov",
"diggs_geo": "http://diggsml.org/schemas/2.6/geotechnical",
"witsml": "http://www.witsml.org/schemas/131",
"diggs": "http://diggsml.org/schemas/2.6",
}
def convert_to_diggs(file_path):
bf = bor.read(file_path)
for prefix, uri in namespaces.items():
ET.register_namespace(prefix, uri)
root = ET.Element(
"Diggs",
{
"xmlns": "http://diggsml.org/schemas/2.6",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xlink": "http://www.w3.org/1999/xlink",
"xmlns:gml": "http://www.opengis.net/gml/3.2",
"xmlns:g3.3": "http://www.opengis.net/gml/3.3/ce",
"xmlns:glr": "http://www.opengis.net/gml/3.3/lr",
"xmlns:glrov": "http://www.opengis.net/gml/3.3/lrov",
"xmlns:diggs_geo": "http://diggsml.org/schemas/2.6/geotechnical",
"xmlns:witsml": "http://www.witsml.org/schemas/131",
"xmlns:diggs": "http://diggsml.org/schemas/2.6",
"xsi:schemaLocation": "http://diggsml.org/schemas/2.6 https://diggsml.org/schema-dev/Diggs.xsd",
"gml:id": "Github_Exported",
},
)
# Add document information
doc_info_element = ET.SubElement(
ET.SubElement(root, "documentInformation"),
"DocumentInformation",
{"gml:id": f"di_{bf.description['filename']}"},
)
ET.SubElement(doc_info_element, "creationDate").text = bf.description["creation"]
# Add source software information
software_application_element = ET.SubElement(
ET.SubElement(root, "sourceSoftware"),
"SoftwareApplication",
{"gml:id": "bor2diggs"},
)
ET.SubElement(software_application_element, "gml:name").text = "bor2diggs"
ET.SubElement(software_application_element, "version").text = VERSION
# Add project information
project_element = ET.SubElement(
ET.SubElement(root, "project"),
"Project",
{"gml:id": f"pr_{bf.description['project_ref']}"},
)
ET.SubElement(project_element, "gml:name").text = bf.description["project_ref"]
# # Add sampling feature (borehole)
borehole = ET.SubElement(
ET.SubElement(root, "samplingFeature"),
"Borehole",
{"gml:id": f"id_{bf.description['borehole_ref']}"},
)
ET.SubElement(borehole, "gml:name").text = bf.description["borehole_ref"]
ET.SubElement(borehole, "investigationTarget").text = "Natural Ground"
ET.SubElement(
borehole, "projectRef", {"xlink:href": f"pr_{bf.description['project_ref']}"}
)
# Convert coordinate strings to floats and format them
try:
latitude = float(bf.description["position"]["latitude"]["value"])
longitude = float(bf.description["position"]["longitude"]["value"])
altitude = float(bf.description["position"]["altitude"]["value"])
# Format coordinates to 6 decimal places
coord_string = f"{latitude:.6f} {longitude:.6f} {altitude:.2f}"
pos_string = f"{coord_string} {latitude:.6f} {longitude:.6f} {(altitude - bf.data.DEPTH.iat[-1]):.2f}"
except ValueError:
# If conversion fails, use a default or log an error
print("Warning: Invalid coordinate data in header. Using default values.")
coord_string = "0.000000 0.000000 0.00"
pos_string = "{coord_string} {coord_string}"
# add reference point
ET.SubElement(
ET.SubElement(
ET.SubElement(borehole, "referencePoint"),
"PointLocation",
{"gml:id": f"pl_bh_{bf.description['borehole_ref']}"},
),
"gml:pos",
{
"srsDimension": "3",
"uomLabels": "dega dega m",
"axisLabels": "latitude longitude height",
},
).text = coord_string
# add center line
ET.SubElement(
ET.SubElement(
ET.SubElement(borehole, "centerLine"),
"LinearExtent",
{"gml:id": f"cl_bh_{bf.description['borehole_ref']}"},
),
"gml:posList",
{
"srsDimension": "3",
"uomLabels": "dega dega m",
"axisLabels": "latitude longitude height",
},
).text = pos_string
# add linear referencing
lsrs = ET.SubElement(
ET.SubElement(borehole, "linearReferencing"),
"LinearSpatialReferenceSystem",
{"gml:id": f"lr_bh_{bf.description['borehole_ref']}"},
)
ET.SubElement(
lsrs, "gml:identifier", {"codeSpace": "DIGGS"}
).text = f"urn:x-diggs:def:fi:DIGGSINC:lr_bh_{bf.description['borehole_ref']}"
ET.SubElement(
lsrs,
"glr:linearElement",
{"xlink:href": f"#cl_bh_{bf.description['borehole_ref']}"},
)
lrm_method = ET.SubElement(
ET.SubElement(lsrs, "glr:lrm"),
"glr:LinearReferencingMethod",
{"gml:id": f"lrm_bh_{bf.description['borehole_ref']}"},
)
ET.SubElement(lrm_method, "glr:name").text = "chainage"
ET.SubElement(lrm_method, "glr:type").text = "absolute"
ET.SubElement(lrm_method, "glr:units").text = "m"
# add construction method
construction_method = ET.SubElement(borehole, "constructionMethod")
method_element = ET.SubElement(
construction_method,
"BoreholeConstructionMethod",
{"gml:id": f"cm_bh_{bf.description['borehole_ref']}"},
)
ET.SubElement(method_element, "gml:name").text = bor.codes.DRILLING_METHOD[
bf.description["drilling"]["method"]
]
# Add location element
location = ET.SubElement(method_element, "location")
linear_extent = ET.SubElement(
location,
"LinearExtent",
{"gml:id": f"le_cm_bh_{bf.description['borehole_ref']}"},
)
ET.SubElement(
linear_extent,
"gml:posList",
{"srsName": f"#lr_bh_{bf.description['borehole_ref']}", "srsDimension": "1"},
).text = f"{bf.data.DEPTH.iat[0]:g} {bf.data.DEPTH.iat[-1]:g}"
# Add DrillRig
if bf.description["drilling"].get("machine_ref"):
ET.SubElement(
ET.SubElement(method_element, "constructionEquipment"),
"DrillRig",
{"gml:id": bf.description["drilling"]["machine_ref"]},
)
# Add CuttingTool
if bf.description["drilling"].get("tool"):
cuttingtool_el = ET.SubElement(
ET.SubElement(method_element, "cuttingToolInfo"), "CuttingTool"
)
ET.SubElement(
cuttingtool_el,
"gml:name",
).text = bor.codes.DRILLING_TOOL[bf.description["drilling"]["tool"]]
if bf.description["drilling"].get("tool_diameter"):
ET.SubElement(
cuttingtool_el,
"toolOuterDiameter",
{"uom": bf.description["drilling"]["tool_diameter"]["@unit"]},
).text = bf.description["drilling"]["tool_diameter"]["value"]
# add measurement
measurement = ET.SubElement(root, "measurement")
mwd = ET.SubElement(
measurement,
"MeasurementWhileDrilling",
{"gml:id": f"mwd_{bf.description['filename']}"},
)
ET.SubElement(mwd, "gml:name").text = f"MWD_{bf.description['filename']}"
ET.SubElement(mwd, "investigationTarget").text = "Natural Ground"
ET.SubElement(
mwd, "projectRef", {"xlink:href": f"#pr_{bf.description['project_ref']}"}
)
ET.SubElement(
mwd,
"samplingFeatureRef",
{"xlink:href": f"#bh_{bf.description['borehole_ref']}"},
)
# add mwd result
outcome = ET.SubElement(mwd, "outcome")
result = ET.SubElement(
outcome, "MWDResult", {"gml:id": f"mwdr_{bf.description['filename']}"}
)
# add time domain
time_domain = ET.SubElement(result, "timeDomain")
time_position_list = ET.SubElement(
time_domain,
"TimePositionList",
{"gml:id": f"tpl_{bf.description['filename']}", "unit": "second"},
)
positions = ET.SubElement(time_position_list, "timePositionList")
# Join all timestamps with a space and wrap every 5 timestamps
timestamps = list(f"{t:g}" for t in bf.data.index.array)
wrapped_timestamps = (
[" "]
+ [" ".join(timestamps[i : i + 5]) for i in range(0, len(timestamps), 5)]
+ [""]
)
positions.text = "\n ".join(wrapped_timestamps)
# add result set
results = ET.SubElement(result, "results")
result_set = ET.SubElement(results, "ResultSet")
parameters = ET.SubElement(result_set, "parameters")
property_params = ET.SubElement(
parameters, "PropertyParameters", {"gml:id": "params1"}
)
properties = ET.SubElement(property_params, "properties")
property_classes = {
"time": "measured_time",
"DEPTH": "measured_depth",
"AS": "penetration_rate",
"RV": "vibration_acceleration",
"EVR": "event_new_rod",
"TP": "hydraulic_crowd_pressure",
"TPAF": "crowd_downward_thrust",
"TQ": "hydraulic_torque_pressure",
"TQAT": "torque",
"HP": "holdback_pressure",
"SP": "hammering_pressure",
"IP": "fluid_injection_pressure",
"IF": "fluid_injection_volume_rate",
"OF": "fluid_return_volume_rate",
"RSP": "rotation_shaft",
"GEAR": "gear_number",
}
index = 1
for name, info in bf.metadata.items():
unit = info.get("unit", "-")
property_element = ET.SubElement(
properties, "Property", {"gml:id": f"prop{index}", "index": str(index)}
)
ET.SubElement(property_element, "propertyName").text = name
ET.SubElement(property_element, "typeData").text = "double"
property_class = property_classes.get(name, "missing")
ET.SubElement(
property_element,
"propertyClass",
{"codeSpace": "http://diggsml.org/def/codes/DIGGS/0.1/mwd_properties.xml"},
).text = property_class
if unit != "-":
ET.SubElement(property_element, "uom").text = unit
ET.SubElement(
property_element, "nullValue", {"reason": "missing"}
).text = "9999"
index += 1
# add data values
data_values = ET.SubElement(
result_set, "dataValues", {"cs": ",", "ts": " ", "decimal": "."}
)
data_values.text = bf.to_csv(header=False, index=False).strip()
# Convert the XML to a pretty-printed string
xml_diggs = minidom.parseString(ET.tostring(root)).toprettyxml(indent=" ")
return xml_diggs
@click.command()
@click.argument("bor_input", type=click.File("rb"))
@click.option(
"-o", "--output", type=click.Path(writable=True, dir_okay=False), required=False
)
def main(bor_input, output):
"""Convert BOR file to a DIGGS."""
diggs_content = convert_to_diggs(bor_input)
if not output:
# sys.stdout.buffer
print(diggs_content)
else:
output.write(diggs_content)
if __name__ == "__main__":
main()