-
Notifications
You must be signed in to change notification settings - Fork 2
/
CBlueApp.py
244 lines (211 loc) · 9.71 KB
/
CBlueApp.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
"""
cBLUE (comprehensive Bathymetric Lidar Uncertainty Estimator)
Copyright (C) 2019
Oregon State University (OSU)
Center for Coastal and Ocean Mapping/Joint Hydrographic Center, University of New Hampshire (CCOM/JHC, UNH)
NOAA Remote Sensing Division (NOAA RSD)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Contact:
Christopher Parrish, PhD
School of Construction and Civil Engineering
101 Kearney Hall
Oregon State University
Corvallis, OR 97331
(541) 737-5688
Last Edited By:
Keana Kief (OSU)
January 10th, 2024
"""
# -*- coding: utf-8 -*-
import logging
import sys
# Customize logging
import utils
import os
import json
import laspy
from Subaerial import SensorModel, Jacobian
from Merge import Merge
from Sbet import Sbet
from Tpu import Tpu
from Sensor import Sensor
from UserInput import UserInput
import argparse
from datetime import datetime
import subprocess
#Create a logging file named CBlue.log stored in the current working directory
utils.CustomLogger(filename="CBlue.log")
WIND_OPTIONS = [
"Calm-light air (0-2 kts)",
"Light Breeze (3-6 kts)",
"Gentle Breeze (7-10 kts)",
"Moderate Breeze (11-15 kts)",
"Fresh Breeze (16-20 kts)"
]
TURBIDITY_OPTIONS = [
"Clear (0.06-0.10 m^-1)",
"Clear-Moderate (0.11-0.17 m^-1)",
"Moderate (0.18-0.25 m^-1)",
"Moderate-High (0.26-0.32 m^-1)",
"High (0.33-0.36 m^-1)"
]
TPU_METRIC_OPTIONS = ["1-\u03c3", "95% confidence"]
def CBlueApp(controller_configuration):
"""Run CBLUE main process. Trajectory Processing will be followed by TPU Processing without interruption"""
with open("cBLUE_ASCII_splash.txt", "r") as f:
message = f.read()
print(message)
sbet_dir_value = controller_configuration["directories"]["sbet"]
selected_sensor_value = controller_configuration["sensor_model"]
sbet = Sbet(sbet_dir_value, selected_sensor_value)
sbet.set_data()
las_dir_value = controller_configuration["directories"]["las"]
sensor_model = SensorModel(selected_sensor_value)
settings_object = UserInput(controller_configuration)
# Create a sensor object initialized to the user's selected sensor
sensor_object = Sensor(selected_sensor_value)
# Initialize the tpu object
tpu = Tpu(settings_object, sensor_object)
las_files = [
os.path.join(las_dir_value, l)
for l in os.listdir(las_dir_value)
if l.endswith(".las") | l.endswith(".laz")
]
num_las = len(las_files)
# GENERATE JACOBIAN FOR SENSOR MODEL OBSERVATION EQUATIONS
jacobian = Jacobian(sensor_model)
# CREATE OBJECT THAT PROVIDES FUNCTIONALITY TO MERGE LAS AND TRAJECTORY DATA
merge = Merge(sensor_object)
logging.cblue(f"processing {num_las} las file(s) ({settings_object.cpu_process_info[0]})...")
logging.cblue(f"multiprocessing = {settings_object.multiprocess}")
def sbet_las_tiles_generator():
"""This generator is the 2nd argument for the run_tpu_multiprocessing method,
to avoid passing entire sbet or list of tiled sbets to the calc_tpu() method"""
for las_file in las_files:
sbet_tile = os.path.split(las_file)[-1]
logging.cblue(f"({sbet_tile}) generating SBET tile...")
inFile = laspy.read(las_file)
west = inFile.header.x_min
east = inFile.header.x_max
north = inFile.header.y_max
south = inFile.header.y_min
yield sbet.get_tile_data(north, south, east, west), las_file, jacobian, merge
if settings_object.multiprocess == "True":
p = tpu.run_tpu_multiprocess(num_las, sbet_las_tiles_generator())
p.close()
p.join()
elif settings_object.multiprocess == "False":
tpu.run_tpu_singleprocess(num_las, sbet_las_tiles_generator())
else:
logging.cblue(f"multiprocessing set to {settings_object.multiprocess} (Must be True or False)")
print("Done!")
def updateConfig(config_dict):
"""Updates the cblue_configuration.json with the current run's settings."""
new_config_dict = config_dict.copy()
del new_config_dict["wind_ind"]
del new_config_dict["wind_selection"]
del new_config_dict["wind_vals"]
del new_config_dict["kd_ind"]
del new_config_dict["kd_selection"]
del new_config_dict["kd_vals"]
del new_config_dict["vdatum_region"]
del new_config_dict["mcu"]
del new_config_dict["csv_option"]
with open("cblue_configuration.json", "w") as update_config:
json.dump(new_config_dict, update_config, indent=4)
if just_save_config:
sys.exit()
if __name__ == "__main__":
# Command Line Interface
def get_help_text(options_list):
"""Generate command line interface help text for arguments that are designated using a list index value"""
help_text = f"Choose an integer: \n"
for n, option in enumerate(options_list):
option = option.replace("%", "%%") # Escape percent sign to avoid argparse error
help_text += f"{n} = {option}, \n"
help_text = help_text.strip(", \n")
help_text += f'\n\n'
return help_text
# ADD ARGUMENTS
parser = argparse.ArgumentParser(description="Run CBlueApp through the command line interface.", formatter_class=argparse.RawTextHelpFormatter)
# Data Directories
parser.add_argument("in_sbet_dir", help="Trajectory directory file path.\n\n")
parser.add_argument("in_las_dir", help="LAS directory file path.\n\n")
parser.add_argument("output_dir", help="Output directory file path.\n\n")
# Environmental Parameters
# # Water Surface
wind_values = [[1], [2, 3], [4, 5], [6, 7], [8, 9, 10]]
wind_help_text = get_help_text(WIND_OPTIONS)
parser.add_argument("wind", type=int, choices=[0, 1, 2, 3, 4], help=wind_help_text, metavar="wind_speed")
# # Turbidity
turbidity_values = [[6, 11], [11, 18], [18, 26], [26, 33], [33, 37]]
turbidity_help_text = get_help_text(TURBIDITY_OPTIONS)
parser.add_argument("turbidity", type=int, choices=[0, 1, 2, 3, 4], help=turbidity_help_text, metavar="turbidity")
# VDatum Region
parser.add_argument("mcu", default=0.0, help=f"Input MCU value for the VDatum region. Enter a float value.\nSee .\\lookup_tables\\V_Datum_MCU_Values.txt for MCU values for different VDatum regions.\n\n")
parser.add_argument("-vdatum_region", default=f"Used MCU value given in the command line interface.",
help=f"Adds the name of the VDatum region to the metadata log.\nUser must provide the region name after -vdatum_region flag.\n\n")
# Sensor Model
with open("lidar_sensors.json", "r") as sensors_json:
sensor_json_content = json.load(sensors_json)
sensor_options = list(sensor_json_content.keys())
sensor_help_text = get_help_text(sensor_options)
parser.add_argument("sensor", type=int, choices=list(range(len(sensor_options))), help=sensor_help_text, metavar="sensor")
# TPU Metric
tpu_help_text = get_help_text(TPU_METRIC_OPTIONS)
parser.add_argument("tpu_metric", type=int, choices=[0, 1], help=tpu_help_text, metavar="tpu_metric")
# Output Options
parser.add_argument("--csv", action="store_true", help="Add the --csv flag to generate CSV output files.")
parser.add_argument("--just_save_config", action="store_true", help="Do not run process. Save config file only.")
# Water Surface Ellipsoid Height
parser.add_argument("water_height", help="Nominal water surface ellipsoid height in meters. Enter a float value.")
# RUN GUI IF NOT ARGUMENTS GIVEN
if not len(sys.argv) > 1:
subprocess.run(["python", "CBlueAppGui.py"])
sys.exit()
# PARSE ARGUMENTS
args = parser.parse_args()
in_sbet_dir = args.in_sbet_dir
in_las_dir = args.in_las_dir
output_dir = args.output_dir
wind_index = int(args.wind)
turbidity_index = int(args.turbidity)
mcu = args.mcu
vdatum_region = args.vdatum_region
sensor_index = int(args.sensor)
tpu_metric_index = int(args.tpu_metric)
csv = args.csv
just_save_config = args.just_save_config
water_height = float(args.water_height)
# UPDATE CONFIG
with open("cblue_configuration.json", "r") as config:
config_dict = json.load(config)
config_dict["directories"] = {}
config_dict["directories"]["sbet"] = in_sbet_dir
config_dict["directories"]["las"] = in_las_dir
config_dict["directories"]["tpu"] = output_dir
config_dict["wind_ind"] = wind_index
config_dict["wind_selection"] = WIND_OPTIONS[wind_index]
config_dict["wind_vals"] = wind_values[wind_index]
config_dict["kd_ind"] = turbidity_index
config_dict["kd_selection"] = TURBIDITY_OPTIONS[turbidity_index]
config_dict["kd_vals"] = (turbidity_values[turbidity_index][0], turbidity_values[turbidity_index][1])
config_dict["vdatum_region"] = vdatum_region
config_dict["mcu"] = mcu
config_dict["sensor_model"] = sensor_options[sensor_index]
config_dict["error_type"] = TPU_METRIC_OPTIONS[tpu_metric_index]
config_dict["csv_option"] = csv
config_dict["water_surface_ellipsoid_height"] = water_height
updateConfig(config_dict)
CBlueApp(config_dict)