Skip to content

Commit 3141844

Browse files
committed
Prevent circular imports and strip '?' from non-standard AGS 3 groups and headers
1 parent 1777457 commit 3141844

File tree

6 files changed

+248
-203
lines changed

6 files changed

+248
-203
lines changed

examples/hk_kaitak_ags3/hk_kaitak_ags3_to_brgi_geodb.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ def _(mo):
227227

228228
@app.cell
229229
def _(CRS, zip, zipfile):
230+
from bedrock_ge.gi.ags3 import ags3_to_brgi_db_mapping
230231
from bedrock_ge.gi.ags_parser import ags_to_brgi_db_mapping
232+
from bedrock_ge.gi.mapping_models import BedrockGIMapping
233+
from bedrock_ge.gi.mapper import map_to_brgi_db
231234

232235
projected_crs = CRS("EPSG:2326")
233236
vertrical_crs = CRS("EPSG:5738")
@@ -240,18 +243,23 @@ def _(CRS, zip, zipfile):
240243
print(f"\n🖥️ Processing {file_name} ...")
241244
with zip_ref.open(file_name) as ags3_file:
242245
# Convert content of a single AGS 3 file to a Dictionary of pandas dataframes (a database)
243-
ags3_to_brgi_db_mapping = ags_to_brgi_db_mapping(
246+
ags3_mapping = ags_to_brgi_db_mapping(
244247
ags3_file, projected_crs, vertrical_crs
245248
)
249+
brgi_db_obj = map_to_brgi_db(ags3_mapping)
246250

247251
# with zipfile.ZipFile(zip) as zip_ref:
248-
# file_name = "58358/GE201304.18A.ags"
252+
# file_name = "21659/9508008.AGS"
249253
# print(f"\n🖥️ Processing {file_name} ...")
250254
# with zip_ref.open(file_name) as ags3_file:
251255
# # Convert content of a single AGS 3 file to a Dictionary of pandas dataframes (a database)
252-
# ags3_to_brgi_db_mapping = ags_to_brgi_db_mapping(
253-
# ags3_file, projected_crs, vertrical_crs
256+
# ags3_mapping_obj = ags3_to_brgi_db_mapping(
257+
# ags3_file, projected_crs, vertrical_crs, "utf-8"
254258
# )
259+
260+
# brgi_db_obj = map_to_brgi_db(ags3_mapping_obj)
261+
262+
# brgi_db_obj
255263
return
256264

257265

src/bedrock_ge/gi/ags3.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77
from pyproj import CRS
88

99
from bedrock_ge.gi.ags_schemas import Ags3HOLE, Ags3SAMP, check_ags_proj_group
10-
from bedrock_ge.gi.brgi_db_mapping import (
11-
BedrockGIDatabaseMapping,
10+
from bedrock_ge.gi.io_utils import coerce_string, open_text_data_source
11+
from bedrock_ge.gi.mapping_models import (
12+
BedrockGIMapping,
1213
InSituTestTableMapping,
1314
LabTestTableMapping,
1415
LocationTableMapping,
1516
OtherTable,
1617
ProjectTableMapping,
1718
SampleTableMapping,
1819
)
19-
from bedrock_ge.gi.io_utils import coerce_string, open_text_data_source
2020

2121

2222
def ags3_to_dfs(
2323
source: str | Path | IO[str] | IO[bytes] | bytes, encoding: str
2424
) -> dict[str, pd.DataFrame]:
2525
"""Converts AGS 3 data to a dictionary of pandas DataFrames.
2626
27+
Also strips '?' from non-standard AGS 3 group and header names, in order to
28+
make the rest of the code more generic.
29+
2730
Args:
2831
source (str | Path | IO[str] | IO[bytes] | bytes): The AGS 3 file (str or Path)
2932
or a file-like object that represents the AGS 3 file.
@@ -52,14 +55,14 @@ def ags3_to_dfs(
5255
if group:
5356
ags3_dfs[group] = pd.DataFrame(group_data, columns=headers)
5457

55-
group = line.strip(' ,"*')
58+
group = line.strip(' ,"*?')
5659
group_data = []
5760

5861
# In AGS 3 header names are prefixed with "*
5962
elif line.startswith('"*'):
6063
line_type = "headers"
6164
new_headers = line.split('","')
62-
new_headers = [h.strip(' ,"*') for h in new_headers]
65+
new_headers = [h.strip(' ,"*?') for h in new_headers]
6366

6467
# Some groups have so many headers that they span multiple lines.
6568
# Therefore we need to check whether the new headers are
@@ -131,7 +134,7 @@ def ags3_to_brgi_db_mapping(
131134
projected_crs: CRS,
132135
vertical_crs: CRS,
133136
encoding: str,
134-
) -> BedrockGIDatabaseMapping:
137+
) -> BedrockGIMapping:
135138
"""Map AGS 3 data to the Bedrock GI data model.
136139
137140
Args:
@@ -180,7 +183,6 @@ def ags3_to_brgi_db_mapping(
180183
)
181184
del ags3_dfs["SAMP"]
182185
else:
183-
print("Your AGS 3 data doesn't contain a SAMP group, i.e. samples.")
184186
ags3_sample = None
185187

186188
ags3_lab_tests = []
@@ -190,7 +192,7 @@ def ags3_to_brgi_db_mapping(
190192
for group, df in ags3_dfs.items():
191193
# Non-standard group names contain the "?" prefix.
192194
# => checking that "SAMP_TOP" / "HOLE_ID" is in the columns is too restrictive.
193-
if any("SAMP_TOP" in col for col in df.columns):
195+
if "SAMP_TOP" in df.columns:
194196
df = _add_sample_source_id(df)
195197
ags3_lab_tests.append(
196198
LabTestTableMapping(
@@ -200,7 +202,7 @@ def ags3_to_brgi_db_mapping(
200202
sample_id_column="sample_source_id",
201203
)
202204
)
203-
elif any("HOLE_ID" in col for col in df.columns):
205+
elif "HOLE_ID" in df.columns:
204206
top_depth, base_depth = _get_depth_columns(group, list(df.columns))
205207
ags3_insitu_tests.append(
206208
InSituTestTableMapping(
@@ -214,24 +216,26 @@ def ags3_to_brgi_db_mapping(
214216
else:
215217
ags3_other_tables.append(OtherTable(table_name=group, data=df))
216218

217-
ags3_brgi_db_mapping = BedrockGIDatabaseMapping(
219+
brgi_db_mapping = BedrockGIMapping(
218220
Project=ags3_project,
219221
Location=ags3_location,
220222
InSitu=ags3_insitu_tests,
221223
Sample=ags3_sample,
222224
Lab=ags3_lab_tests,
223225
Other=ags3_other_tables,
224226
)
225-
return ags3_brgi_db_mapping
227+
return brgi_db_mapping
226228

227229

228230
def _add_sample_source_id(df: pd.DataFrame) -> pd.DataFrame:
229231
df["sample_source_id"] = (
230232
df["SAMP_REF"].astype(str)
231-
+ "_"
233+
+ "-"
232234
+ df["SAMP_TYPE"].astype(str)
233-
+ "_"
235+
+ "-"
234236
+ df["SAMP_TOP"].astype(str)
237+
+ "-"
238+
+ df["HOLE_ID"].astype(str)
235239
)
236240
return df
237241

src/bedrock_ge/gi/ags_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
from pyproj import CRS
88

99
from bedrock_ge.gi.ags3 import ags3_to_brgi_db_mapping
10-
from bedrock_ge.gi.brgi_db_mapping import BedrockGIDatabaseMapping
1110
from bedrock_ge.gi.io_utils import detect_encoding, open_text_data_source
11+
from bedrock_ge.gi.mapping_models import BedrockGIMapping
1212

1313

1414
def ags_to_brgi_db_mapping(
1515
source: str | Path | IO[str] | IO[bytes] | bytes,
1616
projected_crs: CRS,
1717
vertical_crs: CRS = CRS(3855),
1818
encoding: str | None = None,
19-
) -> BedrockGIDatabaseMapping:
19+
) -> BedrockGIMapping:
2020
"""Map AGS 3 or AGS 4 data to the Bedrock GI data model.
2121
2222
Args:

src/bedrock_ge/gi/brgi_db_mapping.py

Lines changed: 0 additions & 184 deletions
This file was deleted.

0 commit comments

Comments
 (0)