-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_batchDirectory.py
320 lines (255 loc) · 12.2 KB
/
main_batchDirectory.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
# Part of PING-Mapper software
#
# Co-Developed by Cameron S. Bodine and Dr. Daniel Buscombe
#
# Inspired by PyHum: https://github.com/dbuscombe-usgs/PyHum
#
# MIT License
#
# Copyright (c) 2022-23 Cameron S. Bodine
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
sys.path.insert(0, 'src')
from funcs_common import *
from main_readFiles import read_master_func
from main_rectify import rectify_master_func
from main_mapSubstrate import map_master_func
import time
import datetime
# Get processing script's dir so we can save it to file
scriptDir = os.getcwd()
# For the logfile
oldOutput = sys.stdout
#============================================
#######################
# Start User Parameters
#######################
# Path to data/output
inDir = r'./exampleData/' # Parent folder of sonar recordings
outDir = r'./procData' # Parent folder for export files
prefix = r''
suffix = r''
aoi = False #r"path/to/.shp"
# *** IMPORTANT ****
# Export Mode: project_mode
## 0==NEW PROJECT: Create a new project. [DEFAULT]
## If project already exists, program will exit without any project changes.
##
## 1==OVERWRITE MODE: Create new project, regardless of previous project state.
## If project exists, it will be DELETED and reprocessed.
## If project does not exist, a new project will be created.
project_mode = 1
# General Parameters
tempC = 10 #Temperature in Celsius
nchunk = 500 #Number of pings per chunk
cropRange = 0.0 #Crop imagery to specified range [in meters]; 0.0==No Cropping
exportUnknown = False #Option to export Unknown ping metadata
fixNoDat = True # Locate and flag missing pings; add NoData to exported imagery.
threadCnt = 0 #Number of compute threads to use; 0==All threads; <0==(Total threads + threadCnt); >0==Threads to use up to total threads
# Output Pixel Resolution [meters]
pix_res_son = 0.05 # 0 = Default (~0.02 m)
pix_res_map = 0.25 # 0 = Default (~0.02 m)
# Position Corrections
## Provide an x and y offset to account for position offset between
## control head (or external GPS) and transducer.
## Origin (0,0) is the location of control head (or external GPS)
## X-axis runs from bow (fore, or front) to stern (aft, or rear) with positive offset towards the bow, negative towards stern
## Y-axis runs from portside (left) to starboard (right), with negative values towards the portside, positive towards starboard
## Z-offsets can be provided with `adjDep` below.
x_offset = 0.0 # [meters]
y_offset = 0.0 # [meters]
# Sonar Intensity Corrections
egn = True
egn_stretch = 1 # 0==Min-Max; 1==% Clip
egn_stretch_factor = 0.5 # If % Clip, the percent of histogram tails to clip (1.0 == 1%)
# Sonogram Exports
tileFile = '.jpg' # Img format for plots and sonogram exports
wcp = True #Export tiles with water column present: 0==False; 1==True, side scan channels only; 2==True, all available channels.
wcr = True #Export Tiles with water column removed (and slant range corrected): 0==False; 1==True, side scan channels only; 2==True, all available channels.
# Speed corrected sonogram Exports
lbl_set = 2 # Export images for labeling: 0==False; 1==True, keep water column & shadows; 2==True, remove water column & shadows
spdCor = 1 # Speed correction: 0==No Speed Correction; 1==Stretch by GPS distance; !=1 or !=0 == Stretch factor.
maxCrop = False # True==Ping-wise crop; False==Crop tile to max range.
# Depth Detection and Shadow Removal Parameters
remShadow = 2 # 0==Leave Shadows; 1==Remove all shadows; 2==Remove only bank shadows
detectDep = 1 #0==Use Humminbird depth; 1==Auto detect depth w/ Zheng et al. 2021;
## 2==Auto detect depth w/ Thresholding
smthDep = True #Smooth depth before water column removal
adjDep = 0 #Aditional depth adjustment (in pixels) for water column removaL
pltBedPick = True #Plot bedpick on sonogram
# Rectification Parameters
rect_wcp = True #Export rectified tiles with water column present
rect_wcr = True #Export rectified tiles with water column removed/slant range corrected
son_colorMap = 'Greys_r' # Specify colorramp for rectified imagery. '_r'==reverse the ramp: https://matplotlib.org/stable/tutorials/colors/colormaps.html
# Substrate Mapping
pred_sub = 1 # Automatically predict substrates and save to npz: 0==False; 1==True, SegFormer Model
pltSubClass = True # Export plots of substrate classification and predictions
map_sub = 1 # Export substrate maps (as rasters): 0==False; 1==True. Requires substrate predictions saved to npz.
export_poly = True # Convert substrate maps to shapefile: map_sub must be > 0 or raster maps previously exported
map_predict = 0 #Export rectified tiles of the model predictions: 0==False; 1==Probabilities; 2==Logits. Requires substrate predictions saved to npz.
map_class_method = 'max' # 'max' only current option. Take argmax of substrate predictions to get final classification.
# Mosaic Exports
mosaic_nchunk = 0 # Number of chunks per mosaic: 0=All chunks. Specifying a value >0 generates multiple mosaics if number of chunks exceeds mosaic_nchunk.
mosaic = 1 #Export sonar mosaic; 0==Don't Mosaic; 1==Do Mosaic - GTiff; 2==Do Mosaic - VRT
map_mosaic = 1 #Export substrate mosaic; 0==Don't Mosaic; 1==Do Mosaic - GTiff; 2==Do Mosaic - VRT
# Miscellaneous Exports
banklines = True # Export banklines from sonar imagery
#####################
# End User Parameters
#####################
#============================================
# Normalize paths
inDir = os.path.normpath(inDir)
outDir = os.path.normpath(outDir)
# Find all DAT and SON files in all subdirectories of inDir
inFiles=[]
for root, dirs, files in os.walk(inDir):
for file in files:
if file.endswith('.DAT'):
inFiles.append(os.path.join(root, file))
inFiles = sorted(inFiles)
for i, f in enumerate(inFiles):
print(i, ":", f)
errorRecording = []
for datFile in inFiles:
try:
copied_script_name = os.path.basename(__file__).split('.')[0]+'_'+time.strftime("%Y-%m-%d_%H%M")+'.py'
script = os.path.join(scriptDir, os.path.basename(__file__))
logfilename = 'log_'+time.strftime("%Y-%m-%d_%H%M")+'.txt'
start_time = time.time()
inPath = os.path.dirname(datFile)
humFile = datFile
recName = os.path.basename(humFile).split('.')[0]
sonPath = humFile.split('.DAT')[0]
sonFiles = sorted(glob(sonPath+os.sep+'*.SON'))
recName = prefix + recName + suffix
projDir = os.path.join(outDir, recName)
params = {
'logfilename':logfilename,
'project_mode':project_mode,
'script':[script, copied_script_name],
'humFile':humFile,
'sonFiles':sonFiles,
'projDir':projDir,
'tempC':tempC,
'nchunk':nchunk,
'cropRange':cropRange,
'exportUnknown':exportUnknown,
'fixNoDat':fixNoDat,
'threadCnt':threadCnt,
'pix_res_son': pix_res_son,
'pix_res_map': pix_res_map,
'x_offset':x_offset,
'y_offset':y_offset,
'egn':egn,
'egn_stretch':egn_stretch,
'egn_stretch_factor':egn_stretch_factor,
'tileFile':tileFile,
'wcp':wcp,
'wcr':wcr,
'lbl_set':lbl_set,
'spdCor':spdCor,
'maxCrop':maxCrop,
'USE_GPU':False,
'remShadow':remShadow,
'detectDep':detectDep,
'smthDep':smthDep,
'adjDep':adjDep,
'pltBedPick':pltBedPick,
'rect_wcp':rect_wcp,
'rect_wcr':rect_wcr,
'son_colorMap':son_colorMap,
'pred_sub':pred_sub,
'map_sub':map_sub,
'export_poly':export_poly,
'map_predict':map_predict,
'pltSubClass':pltSubClass,
'map_class_method':map_class_method,
'mosaic_nchunk':mosaic_nchunk,
'mosaic':mosaic,
'map_mosaic':map_mosaic,
'banklines':banklines,
}
globals().update(params)
# =========================================================
# Determine project_mode
print(project_mode)
if project_mode == 0:
# Create new project
if not os.path.exists(projDir):
os.mkdir(projDir)
else:
projectMode_1_inval()
elif project_mode == 1:
# Overwrite existing project
if os.path.exists(projDir):
shutil.rmtree(projDir)
os.mkdir(projDir)
elif project_mode == 2:
# Update project
# Make sure project exists, exit if not.
if not os.path.exists(projDir):
projectMode_2_inval()
# =========================================================
# For logging the console output
logdir = os.path.join(projDir, 'logs')
if not os.path.exists(logdir):
os.makedirs(logdir)
logfilename = os.path.join(logdir, logfilename)
sys.stdout = Logger(logfilename)
print('\n\n', '***User Parameters***')
for k,v in params.items():
print("| {:<20s} : {:<10s} |".format(k, str(v)))
print('sonPath',sonPath)
print('\n\n\n+++++++++++++++++++++++++++++++++++++++++++')
print('+++++++++++++++++++++++++++++++++++++++++++')
print('***** Working On *****')
print(humFile)
print('Start Time: ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M'))
print('\n===========================================')
print('===========================================')
print('***** READING *****')
read_master_func(**params)
# read_master_func(sonFiles, humFile, projDir, t, nchunk, exportUnknown, wcp, wcr, detectDepth, smthDep, adjDep, pltBedPick, threadCnt)
if rect_wcp or rect_wcr or banklines:
print('\n===========================================')
print('===========================================')
print('***** RECTIFYING *****')
rectify_master_func(**params)
# rectify_master_func(sonFiles, humFile, projDir, nchunk, rect_wcp, rect_wcr, mosaic, threadCnt)
#==================================================
#==================================================
if pred_sub or map_sub or export_poly or pltSubClass:
print('\n===========================================')
print('===========================================')
print('***** MAPPING SUBSTRATE *****')
print("working on "+projDir)
map_master_func(**params)
sys.stdout.log.close()
except Exception as Argument:
unableToProcessError(logfilename)
print('\n\nCould not process:', datFile)
sys.stdout = oldOutput
gc.collect()
print("\n\nTotal Processing Time: ",datetime.timedelta(seconds = round(time.time() - start_time, ndigits=0)))
if len(errorRecording) > 0:
print('\n\nUnable to process the following:')
for d in errorRecording:
print('\n',d)