sgf-parser 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sgf_parser/__init__.py +1 -0
- sgf_parser/datetime_parser.py +44 -0
- sgf_parser/models/__init__.py +12 -0
- sgf_parser/models/method.py +447 -0
- sgf_parser/models/method_cpt.py +278 -0
- sgf_parser/models/method_dp.py +102 -0
- sgf_parser/models/method_dt.py +50 -0
- sgf_parser/models/method_rp.py +75 -0
- sgf_parser/models/method_srs.py +198 -0
- sgf_parser/models/method_svt.py +57 -0
- sgf_parser/models/method_tot.py +195 -0
- sgf_parser/models/method_type.py +16 -0
- sgf_parser/models/method_wst.py +67 -0
- sgf_parser/models/types.py +149 -0
- sgf_parser/parser.py +138 -0
- sgf_parser-0.0.1.dist-info/LICENSE +21 -0
- sgf_parser-0.0.1.dist-info/METADATA +110 -0
- sgf_parser-0.0.1.dist-info/RECORD +19 -0
- sgf_parser-0.0.1.dist-info/WHEEL +4 -0
sgf_parser/parser.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import TextIO, Any
|
|
3
|
+
|
|
4
|
+
from sgf_parser.models.method import Method, MethodData
|
|
5
|
+
from sgf_parser import models
|
|
6
|
+
from sgf_parser.models import ParseState
|
|
7
|
+
|
|
8
|
+
# Fields are generally separated by "," and contain a single "="
|
|
9
|
+
# separating the key from the value. However, some fields have values
|
|
10
|
+
# containing ",", with no quoting. To handle this, we require the key
|
|
11
|
+
# to contain only a-z and A-Z. In addition, the Geotech AB extension,
|
|
12
|
+
# have date fields (key "%") with no "=" separating the key from the
|
|
13
|
+
# value...
|
|
14
|
+
_RE_FIELD_SEP = re.compile(r",(?=[a-zA-Z%])")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Parser:
|
|
18
|
+
"""
|
|
19
|
+
A class to parse an SGF file
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
method_code_class_mapping = {
|
|
23
|
+
"7": models.MethodCPT,
|
|
24
|
+
"07": models.MethodCPT,
|
|
25
|
+
"101": models.MethodWST,
|
|
26
|
+
"102": models.MethodWST,
|
|
27
|
+
"107A": models.MethodCPT,
|
|
28
|
+
"107B": models.MethodCPT,
|
|
29
|
+
"8": models.MethodDP,
|
|
30
|
+
"108A": models.MethodDP,
|
|
31
|
+
"108B": models.MethodDP,
|
|
32
|
+
"108C": models.MethodDP,
|
|
33
|
+
"108D": models.MethodDP,
|
|
34
|
+
"108E": models.MethodDP,
|
|
35
|
+
"23": models.MethodRP,
|
|
36
|
+
"24": models.MethodTOT,
|
|
37
|
+
"12": models.MethodSRS,
|
|
38
|
+
"41": models.MethodSRS,
|
|
39
|
+
"42": models.MethodSRS,
|
|
40
|
+
"73": models.MethodSRS,
|
|
41
|
+
"13": models.MethodSVT,
|
|
42
|
+
"35": models.MethodDT,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
def parse(self, file: TextIO) -> list[Method]:
|
|
46
|
+
"""
|
|
47
|
+
Parse the SGF file
|
|
48
|
+
|
|
49
|
+
The file parameter must be an opened file in text mode (with correct character encoding), pointing at the start
|
|
50
|
+
of the file to parse. The file pointer may not point at the end of the file when this method returns.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
blocks = {
|
|
54
|
+
"£": ParseState.METHOD,
|
|
55
|
+
"$": ParseState.HEADER,
|
|
56
|
+
"#": ParseState.DATA,
|
|
57
|
+
"€": ParseState.METHOD,
|
|
58
|
+
"#$": ParseState.QUIT,
|
|
59
|
+
}
|
|
60
|
+
methods: list[Method] = []
|
|
61
|
+
method: Method | None = None
|
|
62
|
+
header: dict[str, Any] = {}
|
|
63
|
+
|
|
64
|
+
state = None
|
|
65
|
+
for row in file:
|
|
66
|
+
row = row.rstrip()
|
|
67
|
+
if not row:
|
|
68
|
+
continue
|
|
69
|
+
|
|
70
|
+
# Possible state changes
|
|
71
|
+
if row in blocks:
|
|
72
|
+
_new_state = blocks[row]
|
|
73
|
+
_old_state = state
|
|
74
|
+
if _new_state == ParseState.DATA and state in (ParseState.HEADER, ParseState.METHOD):
|
|
75
|
+
# Starting a new data block, so store the current collected header in a new method
|
|
76
|
+
method = self.parse_header(header)
|
|
77
|
+
header = {}
|
|
78
|
+
elif _new_state in (ParseState.HEADER, ParseState.METHOD) and _old_state == ParseState.DATA:
|
|
79
|
+
# Finished populating current method, since new method is starting
|
|
80
|
+
# Store the current method, and empty the current method
|
|
81
|
+
if method:
|
|
82
|
+
methods.append(method)
|
|
83
|
+
else:
|
|
84
|
+
raise Exception("Method is None, that is unexpected")
|
|
85
|
+
method = None
|
|
86
|
+
state = _new_state
|
|
87
|
+
continue
|
|
88
|
+
|
|
89
|
+
match state:
|
|
90
|
+
case ParseState.HEADER | ParseState.METHOD:
|
|
91
|
+
header |= self._convert_str_to_dict(row)
|
|
92
|
+
case ParseState.DATA:
|
|
93
|
+
if not method:
|
|
94
|
+
raise ValueError("No method to add data to")
|
|
95
|
+
method.method_data.append(self.parse_data(method, row))
|
|
96
|
+
case ParseState.QUIT:
|
|
97
|
+
break
|
|
98
|
+
case None:
|
|
99
|
+
raise ValueError("First block is not a main block")
|
|
100
|
+
|
|
101
|
+
if method:
|
|
102
|
+
method.post_processing()
|
|
103
|
+
methods.append(method)
|
|
104
|
+
|
|
105
|
+
return methods
|
|
106
|
+
|
|
107
|
+
@staticmethod
|
|
108
|
+
def _convert_str_to_dict(line: str) -> dict[str, Any]:
|
|
109
|
+
"""
|
|
110
|
+
Convert row to dict. If repeated keys, then append values with ", " (comma and a space) as a separator
|
|
111
|
+
"""
|
|
112
|
+
line = line.rstrip(",")
|
|
113
|
+
result: dict[str, Any] = {}
|
|
114
|
+
for k, v in (i.split("=", 1) if "=" in i else [i[0], i[1:]] for i in re.split(_RE_FIELD_SEP, line) if i):
|
|
115
|
+
if not v:
|
|
116
|
+
continue
|
|
117
|
+
if k in result:
|
|
118
|
+
result[k] += f", {v}"
|
|
119
|
+
else:
|
|
120
|
+
result[k] = v
|
|
121
|
+
return result
|
|
122
|
+
|
|
123
|
+
def parse_header(self, header: dict[str, Any]) -> Method:
|
|
124
|
+
"""
|
|
125
|
+
When finished collecting the header dict, then create the Method object
|
|
126
|
+
"""
|
|
127
|
+
if "HM" not in header:
|
|
128
|
+
raise ValueError("Header does not contain a HM field")
|
|
129
|
+
|
|
130
|
+
if header["HM"] not in self.method_code_class_mapping:
|
|
131
|
+
raise ValueError(f"Unsupported value in the HM field {header['HM']!r}")
|
|
132
|
+
|
|
133
|
+
return self.method_code_class_mapping[header["HM"]].model_validate(header)
|
|
134
|
+
|
|
135
|
+
def parse_data(self, method: Method, row: str) -> MethodData:
|
|
136
|
+
row_dict = self._convert_str_to_dict(row)
|
|
137
|
+
method_data = method.method_data_type.model_validate(row_dict)
|
|
138
|
+
return method_data
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Norwegian Geotechnical Institute (NGI)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sgf-parser
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Parser for the Swedish Geotechnical Society / Svenska Geotekniska Föreningen (SGF) data format
|
|
5
|
+
Home-page: https://github.com/norwegian-geotechnical-institute/sgf-parser
|
|
6
|
+
License: MIT
|
|
7
|
+
Author: Jostein Leira
|
|
8
|
+
Author-email: jostein@leira.net
|
|
9
|
+
Maintainer: Jostein Leira
|
|
10
|
+
Maintainer-email: jostein@leira.net
|
|
11
|
+
Requires-Python: >=3.11,<4
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: GIS
|
|
19
|
+
Requires-Dist: pydantic (>=2.8.2,<3.0.0)
|
|
20
|
+
Requires-Dist: python-dateutil (>=2.9.0.post0,<3.0.0)
|
|
21
|
+
Requires-Dist: types-python-dateutil (>=2.9,<3.0)
|
|
22
|
+
Project-URL: Repository, https://github.com/norwegian-geotechnical-institute/sgf-parser
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# sgf-parser
|
|
26
|
+
|
|
27
|
+
Parser for data from geotechnical field investigations in the data format
|
|
28
|
+
specified in Report 3:2012E from the Swedish Geotechnical Society /
|
|
29
|
+
[Svenska Geotekniska Föreningen](http://sgf.net/) (SGF).
|
|
30
|
+
|
|
31
|
+
This package is inspired by the [libsgfdata](https://github.com/emerald-geomodelling/libsgfdata)
|
|
32
|
+
package by [EMerald Geomodelling](https://www.emerald-geomodelling.com/).
|
|
33
|
+
|
|
34
|
+
Latest releases see [CHANGES.md](CHANGES.md)
|
|
35
|
+
|
|
36
|
+
# Installation (end user)
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install sgf-parser
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Basic usage
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from sgf_parser import Parser
|
|
46
|
+
|
|
47
|
+
with open("tests/data/cpt-test-3.cpt", "rt") as file:
|
|
48
|
+
# The test file only contains one method
|
|
49
|
+
[method] = Parser().parse(file)
|
|
50
|
+
|
|
51
|
+
print(repr(method))
|
|
52
|
+
# <MethodCPT CPT method_data=[<MethodCPTData 1.000>, <MethodCPTData 1.020>, <MethodCPTData 1.040>,
|
|
53
|
+
# <MethodCPTData 1.060>, <MethodCPTData 1.080>, <MethodCPTData 1.100>, <MethodCPTData 1.120>, ...,
|
|
54
|
+
# <MethodCPTData 24.940>, <MethodCPTData 24.960>, <MethodCPTData 24.980>]>
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
method.model_dump(exclude={'method_data'}, exclude_defaults=True)
|
|
58
|
+
# {'location_name': 'Test', 'project_number': '1234', 'method_type_string': '107A',
|
|
59
|
+
# 'conducted_at': datetime.datetime(2019, 9, 5, 11, 5), 'predrilling_depth': Decimal('1.00'),
|
|
60
|
+
# 'cone_reference': '5349', 'cone_area_ratio': Decimal('0.844'), 'sleeve_area_ratio': Decimal('0'),
|
|
61
|
+
# 'application_class_depth': <ApplicationClass.ONE: 1>, 'application_class_resistance': <ApplicationClass.ONE: 1>,
|
|
62
|
+
# 'application_class_friction': <ApplicationClass.ONE: 1>, 'application_class_pressure': <ApplicationClass.ONE: 1>,
|
|
63
|
+
# 'depth_top': Decimal('1.000'), 'depth_base': Decimal('24.980'), 'stopcode': 90}
|
|
64
|
+
|
|
65
|
+
method.stopcode
|
|
66
|
+
# 90
|
|
67
|
+
|
|
68
|
+
len(method.method_data)
|
|
69
|
+
# 1200
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
# Getting Started developing
|
|
74
|
+
|
|
75
|
+
1. Software dependencies
|
|
76
|
+
|
|
77
|
+
- Python 3.11 or higher
|
|
78
|
+
- Poetry
|
|
79
|
+
- ruff code formatter
|
|
80
|
+
|
|
81
|
+
2. Clone this repository
|
|
82
|
+
|
|
83
|
+
3. Install
|
|
84
|
+
|
|
85
|
+
`poetry install`
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# Build and Test
|
|
89
|
+
|
|
90
|
+
Run in the project root folder:
|
|
91
|
+
|
|
92
|
+
poetry shell pytest
|
|
93
|
+
|
|
94
|
+
Build the package wheel:
|
|
95
|
+
|
|
96
|
+
poetry build
|
|
97
|
+
|
|
98
|
+
# Publish
|
|
99
|
+
|
|
100
|
+
# TODOs
|
|
101
|
+
|
|
102
|
+
- [ ] Implement everything
|
|
103
|
+
- [ ] Do the rest
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# Contribute
|
|
107
|
+
|
|
108
|
+
Please start by adding an issue before submitting any pull requests.
|
|
109
|
+
|
|
110
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
sgf_parser/__init__.py,sha256=Opan3zhgZpba6cvScgPd7W-IsHj3IADqLaqvLrh6zfE,37
|
|
2
|
+
sgf_parser/datetime_parser.py,sha256=VQyixN_gkhSVpoHQOA0tDHChIL89HTtv4XOfKIzUtRQ,1162
|
|
3
|
+
sgf_parser/models/__init__.py,sha256=FU4nHAHdOh7FRaY1Zjemrld5e57y3bvKsIOg-HEw5hc,686
|
|
4
|
+
sgf_parser/models/method.py,sha256=CRWTT1pIYZkjvvS3k2udDnuRt0xiV0IcMwcR3XXtFHk,16997
|
|
5
|
+
sgf_parser/models/method_cpt.py,sha256=GJu23cP7Bu28scmOpjZ7qlLAZZfMtV6HLAT88fdNZFY,10895
|
|
6
|
+
sgf_parser/models/method_dp.py,sha256=L_ryjNcz7I9laxXMXW-bILOsY8Zn-9qHAbB558t1wlc,3104
|
|
7
|
+
sgf_parser/models/method_dt.py,sha256=6OMgIXiB26PTj4tTj4QgYe98MQUqowNvsTa4vIqljjs,1229
|
|
8
|
+
sgf_parser/models/method_rp.py,sha256=YDlkZYtBLjTgaC6qUZHco0Bdvxry_SpTJQLPhgg9Acg,2170
|
|
9
|
+
sgf_parser/models/method_srs.py,sha256=a5l9tTGx_2OBLmOaXGNG3IDj_SsgoM_aTcADNWiTfLw,6484
|
|
10
|
+
sgf_parser/models/method_svt.py,sha256=BywWy8XIn5QSk_JBrkhfwwmZ9vcEXDepof7K3VFqm6w,2048
|
|
11
|
+
sgf_parser/models/method_tot.py,sha256=xz-5RXdkjIFuQpY7-6ZeG0KcFWEVm99G-gNR6aqhmVo,6198
|
|
12
|
+
sgf_parser/models/method_type.py,sha256=FJRzNQW_h1i1ICnbsD7GZfDM-LubByKs2ogK-bO2HCM,201
|
|
13
|
+
sgf_parser/models/method_wst.py,sha256=7W64YrFAmeSXjYkNpAsoFS5Uw9gma0lybBZ38VYmRhA,2041
|
|
14
|
+
sgf_parser/models/types.py,sha256=pY1sCkBJ7JAKAbSmWovQtXFeqbNFl_s_FblNW7nKCds,2842
|
|
15
|
+
sgf_parser/parser.py,sha256=RUu3TLevzZyrRqDLm4ZFRnmH09dZIffJyd4Z7_WKa24,5021
|
|
16
|
+
sgf_parser-0.0.1.dist-info/LICENSE,sha256=0ooAuz7cztjSOr3NY_j81tJfBrl1ID8ZcSUMSVc76jk,1095
|
|
17
|
+
sgf_parser-0.0.1.dist-info/METADATA,sha256=_Qq3CkGm0AOKHZ3mRKXB0WmdMdswniYrd1bEj5etCXU,3172
|
|
18
|
+
sgf_parser-0.0.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
19
|
+
sgf_parser-0.0.1.dist-info/RECORD,,
|