Skip to content

Commit 3f67057

Browse files
committed
Improved output and file handling
1 parent 8dfa6e8 commit 3f67057

File tree

2 files changed

+72
-38
lines changed

2 files changed

+72
-38
lines changed

src/instrumentman/inclination/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
argument,
66
option,
77
IntRange,
8-
File,
9-
file_path
8+
File
109
)
1110

1211
from ..utils import (
@@ -63,7 +62,13 @@ def cli_measure(**kwargs: Any) -> None:
6362
@argument(
6463
"input",
6564
help="inclination measurement file to process",
66-
type=file_path()
65+
type=File("rt", encoding="utf8")
66+
)
67+
@option(
68+
"-o",
69+
"--output",
70+
help="file to save results to in CSV format",
71+
type=File("wt", encoding="utf8", lazy=True)
6772
)
6873
def cli_calc(**kwargs: Any) -> None:
6974
"""Calculate inclination from multiple measurements."""
@@ -80,12 +85,12 @@ def cli_calc(**kwargs: Any) -> None:
8085
@argument(
8186
"output",
8287
help="output file",
83-
type=file_path()
88+
type=File("wt", encoding="utf8", lazy=True)
8489
)
8590
@argument(
8691
"inputs",
8792
help="inclination measurement files",
88-
type=file_path(exists=True),
93+
type=File("rt", encoding="utf8"),
8994
nargs=-1,
9095
required=True
9196
)

src/instrumentman/inclination/app.py

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from io import TextIOWrapper
22
from time import sleep
3-
from pathlib import Path
43
from math import tan, atan, degrees
4+
from re import compile
55

66
from click_extra import echo
77
from geocompy.data import Angle, Coordinate
@@ -12,6 +12,9 @@
1212
from ..utils import echo_green
1313

1414

15+
_LINE = compile(r"^\d+(?:\.\d+)?(?:,\-?\d+\.\d+){2}$")
16+
17+
1518
def run_measure(
1619
tps: GeoCom,
1720
output: TextIOWrapper | None = None,
@@ -42,11 +45,13 @@ def run_measure(
4245
if fullangles.params is None:
4346
continue
4447

48+
az = fullangles.params[0]
4549
cross = fullangles.params[4]
4650
length = fullangles.params[5]
4751

4852
echo(
49-
f"{a % 360:d},{cross.asunit('deg') * 3600:.2f},"
53+
f"{az.asunit('deg'):.4f},"
54+
f"{cross.asunit('deg') * 3600:.2f},"
5055
f"{length.asunit('deg') * 3600:.2f}",
5156
output
5257
)
@@ -75,51 +80,75 @@ def main_measure(
7580

7681

7782
def main_merge(
78-
inputs: list[Path],
79-
output: Path
83+
inputs: list[TextIOWrapper],
84+
output: TextIOWrapper
8085
) -> None:
81-
with output.open("wt", encoding="utf8") as outfile:
82-
echo("hz_deg,cross_sec,length_sec", outfile)
83-
for item in inputs:
84-
with item.open("rt", encoding="utf8") as infile:
85-
next(infile)
86-
echo(infile.read(), outfile, False)
86+
echo("hz_deg,cross_sec,length_sec", output)
87+
for item in inputs:
88+
for line in item:
89+
if not _LINE.match(line.strip()):
90+
continue
91+
92+
echo(line, output, False)
8793

8894
echo_green(f"Merged measurements from {len(inputs)} files.")
8995

9096

9197
def main_calc(
92-
input: Path
98+
input: TextIOWrapper,
99+
output: TextIOWrapper | None = None
93100
) -> None:
94101
points: list[Coordinate] = []
95-
with input.open("rt", encoding="utf8") as file:
96-
next(file)
97-
for line in file:
98-
fields = line.strip().split(",")
99-
azimut = Angle(float(fields[0]), 'deg')
100-
cross = Angle(float(fields[1]) / 3600, 'deg')
101-
length = Angle(float(fields[2]) / 3600, 'deg')
102-
103-
coord = Coordinate(tan(cross), tan(length), 1)
104-
bearing, inclination, s = coord.to_polar()
105-
106-
points.append(
107-
Coordinate.from_polar(
108-
(bearing + azimut).normalized(),
109-
inclination,
110-
s
111-
)
102+
103+
for line in input:
104+
if not _LINE.match(line.strip()):
105+
continue
106+
107+
fields = line.strip().split(",")
108+
azimut = Angle(float(fields[0]), 'deg')
109+
cross = Angle(float(fields[1]) / 3600, 'deg')
110+
length = Angle(float(fields[2]) / 3600, 'deg')
111+
112+
coord = Coordinate(tan(cross), tan(length), 1)
113+
bearing, inclination, s = coord.to_polar()
114+
115+
points.append(
116+
Coordinate.from_polar(
117+
(bearing + azimut).normalized(),
118+
inclination,
119+
s
112120
)
121+
)
113122

114123
x, x_dev = adjust_uniform_single([p.x for p in points])
115124
y, y_dev = adjust_uniform_single([p.y for p in points])
116125

126+
inc_x = degrees(atan(x)) * 3600
127+
inc_y = degrees(atan(y)) * 3600
128+
inc_x_dev = degrees(atan(x_dev)) * 3600
129+
inc_y_dev = degrees(atan(y_dev)) * 3600
130+
117131
direction, inc, _ = Coordinate(x, y, 1).to_polar()
118132

133+
if output is None:
134+
echo(f"""Axis aligned:
135+
inclination X: {inc_x:.1f}" +/- {inc_x_dev:.1f}"
136+
inclination Y: {inc_y:.1f}" +/- {inc_y_dev:.1f}"
137+
Polar:
138+
direction: {direction.asunit('deg'):.4f}°
139+
inclination: {inc.asunit('deg') * 3600:.1f}\""""
140+
)
141+
return
142+
143+
echo(
144+
"inc_x_sec,inc_x_dev_sec,inc_y_sec,inc_y_dev_sec,dir_deg,inc_sec",
145+
output
146+
)
147+
119148
echo(
120-
f"""Direction: {direction.to_dms()}
121-
Inclination: {inc.asunit('deg') * 3600:.1f} seconds
122-
Deviation easting: {degrees(atan(x_dev)) * 3600:.1f} seconds
123-
Deviation northing: {degrees(atan(y_dev)) * 3600:.1f} seconds
124-
"""
149+
(
150+
f"{inc_x:.1f},{inc_x_dev:.1f},{inc_y:.1f},{inc_y_dev:.1f},"
151+
f"{direction.asunit('deg'):.4f},{inc.asunit('deg') * 3600:.1f}"
152+
),
153+
output
125154
)

0 commit comments

Comments
 (0)