CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/geotag/geotag.sh
Views: 1798
1
#!/usr/bin/env bash
2
3
# Advanced geotagging tool by André Kjellstrup
4
###################################################################### VARIABLES ########################
5
6
version="v1.1"
7
mode=1 # 0=geotag, 1=jpg distance(s) , 2=CAM distance in sec
8
loglinenum=1 # last read log line number
9
loglinenummatched=0 # last read log line number used - reverting to this +1 when looking back
10
logline="start" #current line, placeholder for start
11
logtimemsprev=0
12
logended=0 #true if log ended.
13
logtarget=0 #true if we got log type we wanted
14
logmatch=1 # true if we accepted lo line as matching
15
loggotpos=0 #true if log contains pos messages (enables fallback)
16
typical_offset="notset" #contains time offset of first picture vs log.
17
jpgtot=0 #number of jpg files in folder
18
jpgtagged=0 #number of correctly tagged photos
19
jpgmiss=0 #number of missing photos
20
jpgnotinlog=0 #counting jpg's with timestamp outside log
21
camindex=0 #number of last CAM message used.
22
camtot=0 #number of CAM lines found
23
trigtot=0 #number of TRIG lines found
24
logmiss=0 #missed CAM messages
25
revjumps=0 #count how many time we have backed up
26
difflimit=10 # POS message difflimit 10 gives 0.06sec , 20 has proved useful
27
running=1
28
cmdarg=$* #save command line parameters
29
FILE=jpglist
30
ML="false"
31
logsdone=0 #counts processed logs
32
SKIPIMG=0
33
34
35
36
###########################
37
38
while [[ $# > 0 ]]
39
do
40
key="$1"
41
42
case $key in
43
-w|--write)
44
WRITE="true"
45
;;
46
-r|--readonly)
47
WRITE="false"
48
;;
49
-f|--find)
50
WRITE="false"
51
FIND="true"
52
;;
53
-p|--pos)
54
MODE="POS"
55
typical_offset="$2"
56
shift # get past argument
57
;;
58
-dl|--difflimit)
59
difflimit="$2"
60
shift # get past argument
61
;;
62
-c|--cam)
63
MODE="CAM"
64
;;
65
-t|--trig)
66
MODE="TRIG"
67
;;
68
-ns|--nosubsec)
69
NS="true"
70
;;
71
-ml|--multilog)
72
ML="true"
73
;;
74
-del|--delorg)
75
DELORG="true"
76
;;
77
-sp|--skipphoto)
78
SKIPIMG="$2"
79
shift # get past argument
80
;;
81
-sc|--skipcam)
82
SKIPCAM="$2"
83
shift # get past argument
84
;;
85
-st|--skiptrig)
86
SKIPTRIG="$2"
87
shift # get past argument
88
;;
89
-sl|--skiplog)
90
SKIPLOG="$2"
91
shift # get past argument
92
;;
93
94
-h|--help)
95
HELP="true"
96
;;
97
*)
98
# unknown option
99
;;
100
esac
101
shift # past argument or value
102
done
103
104
#echo FILE EXTENSION = "${EXTENSION}"
105
#echo SEARCH PATH = "${SEARCHPATH}"
106
#echo LIBRARY PATH = "${LIBPATH}"
107
108
if [[ "$HELP" == "true" ]] || [[ "$WRITE" == "" ]] #|| [[ "$MODE" == "" ]]
109
then
110
echo "Geotagging script by André Kjellstrup. $version"
111
echo "execute inside a folder containing .JPG and .BIN"
112
echo "example: geotag.sh -c -sp 2 -r"
113
echo ""
114
echo "-w or --write Enables photo processing"
115
echo "-r or --readonly Enables photo processing"
116
echo "-f or --readonly, finds CAM to exif spacing for +/- 10 skipped photos, and +/- 10 skipped CAM messages"
117
echo "-sp x or --skipphoto x Skips x photos counting from the start"
118
echo "-sc x or --skipcam x Skips x CAM entrys from the start"
119
echo "-sl x or --skiplog x Skips x POS entrys from the start"
120
echo "-st x or --skiptrig x Skips x TRIG entrys from the start"
121
echo "-p x or --pos x Use POS log only (if camera was independendly trigged) x=offset in seconds"
122
echo "-dl x or --difflimit x Acceptable limit in 10ms steps for POS-log vs photo differance 10=0.1s (default)"
123
echo "-c or --cam Use CAM log only (do not fall back to POS pessages)"
124
echo "-t or --trig Use TRIG log only (if camera shutter is sensed)"
125
echo "-ns or --nosubsec Do not look for subsecond EXIF tag."
126
echo "-ml or --multilog Grab and combine all .BIN files (for processing photos from more then one flight."
127
echo "-del or --delorg : Delete JPG_original (backups)."
128
echo "EXAMPLE:"
129
echo "Multiple flights dataset from S100; geotag.sh -p 0 -ns -dl 30 -w -ml -del"
130
echo "simgle CAM flight; geotag.sh -c -w -del"
131
exit 1
132
fi
133
134
##ADD DEPENDENCIES HERE
135
command -v mavlogdump.py >/dev/null 2>&1 || { echo >&2 "I require mavlogdump.py but it's not installed. Aborting."; exit 1; }
136
command -v exiftool >/dev/null 2>&1 || { echo >&2 "I require exiftool but it's not installed. Aborting."; exit 1; }
137
##command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
138
139
if [[ "$WRITE" == "true" ]]
140
then
141
echo "INFO: will backup and modify photos" | tee geotag.log
142
fi
143
144
if [[ "$WRITE" == "false" ]]
145
then
146
echo "INFO: dry run only, no photos will be modified" | tee geotag.log
147
fi
148
149
if [[ "$MODE" == "POS" ]]
150
then
151
echo "INFO: use POS records only, expected offset of $typical_offset , poslimit (time of photo vs log is $poslimit)" | tee geotag.log
152
fi
153
154
if [[ "$MODE" == "CAM" ]]
155
then
156
echo "INFO: use CAM records only" | tee geotag.log
157
fi
158
159
160
if [[ "$MODE" == "TRIG" ]]
161
then
162
echo "INFO: use TRIG records only" | tee geotag.log
163
fi
164
165
if [[ "$NS" == "true" ]]
166
then
167
echo "INFO: Do not look for subsecond-time in EXIF" | tee geotag.log
168
fi
169
170
171
if [[ "$SKIPIMG" -gt "0" ]]
172
then
173
echo "INFO: will skip first $SKIPIMG photos" | tee geotag.log
174
fi
175
176
if [[ "$SKIPCAM" -gt "0" ]]
177
then
178
echo "INFO: will skip first $SKIPCAM CAM logs" | tee geotag.log
179
fi
180
181
if [[ "$SKIPTRIG" -gt "0" ]]
182
then
183
echo "INFO: will skip first $SKIPTRIG TRIG logs" | tee geotag.log
184
fi
185
186
187
if [[ "$SKIPLOG" -gt "0" ]]
188
then
189
echo "INFO: will skip first $SKIPLOG POS logs" | tee geotag.log
190
loglinenum=$SKIPLOG
191
fi
192
193
if [ "$difflimit" != 10 ]
194
then
195
echo "INFO: Difflimit is changed to $difflimit (10=0.1s)" | tee geotag.log
196
fi
197
198
199
echo "INFO: using arguments $cmdarg " | tee geotag.log
200
201
jpglistindex=$(( 1 + $SKIPIMG)) #which file to read from jpg list
202
203
204
#========================= FUNCTION readexif ======================================================
205
# get exif data for filename in $jpgname,
206
# return: $jpgdate 20-12-2010 ,$jpgtime 14:10:20.22 ,$jpgtimems 0.22 (220ms)
207
function readexif
208
{
209
if [[ "$NS" == "true" ]]
210
then
211
jpgdatetime=$(exiftool -ee -p '$datetimeoriginal' "$jpgname" -n)
212
jpgdatetime="$jpgdatetime.50"
213
else
214
jpgdatetime=$(exiftool -ee -p '$subsecdatetimeoriginal' "$jpgname" -n)
215
fi
216
217
218
jpgdate=$(echo -n "$jpgdatetime" | head -c10)
219
jpgdate=$(echo "$jpgdate" | tr : -) #replace : with -
220
jpgtime=$(echo -n "$jpgdatetime" | tail -c-11)
221
jpgtimems=$(echo -n "$jpgtime" | tail -c-2)
222
jpgtimems=$(echo "scale=2; ($jpgtimems /100)" | bc -l)
223
#echo "DEBUG $jpgname date $jpgdate time $jpgtime "
224
##read -p "Press any key to continue... " -n1 -s
225
######browse log until we find CAM message
226
}
227
228
#========================= FUNCTION readlog ======================================================
229
# extract needed date from logline string $logline
230
# return: $logdate 20-12-2010 ,$logtime 14:10:20.22 ,$logtimems 0.22 (220ms)
231
# return: $lat $lon $alt(m AMSL)
232
#
233
###### We Prefer TRIG , then CAM, then fallback to POS
234
# in log they look alike:
235
#1970-01-01 01:01:12.55: CAM {TimeUS : 72553489, GPSTime : 0, GPSWeek : 0, Lat : 0.0, Lng : 0.0, Alt : -0.01, RelAlt : -0.01, GPSAlt : 0.0, Roll : 1.4, Pitch : 0.41, Yaw : 82.49}
236
#1970-01-01 01:01:15.80: TRIG {TimeUS : 75803571, GPSTime : 0, GPSWeek : 0, Lat : 0.0, Lng : 0.0, Alt : -0.01, RelAlt : -0.01, GPSAlt : 0.0, Roll : 1.43, Pitch : 0.43, Yaw : 82.62}
237
238
function readlog
239
{
240
logdate=$(echo "$logline"| grep -o '^\S*') #till first space
241
logtime=$(echo "$logline"| awk -F" " '{print $2}')
242
logtime=$(echo -n "$logtime" | head -c-1) #remove trailing :
243
logtimems=$(echo -n "$logtime" | tail -c-2)
244
logtimems=$(echo "scale=2; ($logtimems /100)" | bc -l)
245
246
if [[ "$logline" == *POS* ]]
247
then
248
lat=$(echo "$logline"| grep -o -P '(?<=Lat : ).*(?=, Lng)')
249
lon=$(echo "$logline"| grep -o -P '(?<=Lng : ).*(?=, Alt)')
250
alt=$(echo "$logline"| grep -o -P '(?<= Alt : ).*(?=, RelHomeAlt)')
251
fi
252
253
254
if [[ "$logline" == *CAM* ]]
255
then
256
lat=$(echo "$logline"| grep -o -P '(?<=Lat : ).*(?=, Lng)')
257
lon=$(echo "$logline"| grep -o -P '(?<=Lng : ).*(?=, Alt)')
258
alt=$(echo "$logline"| grep -o -P '(?<=Alt : ).*(?=, RelAlt)')
259
fi
260
261
262
#rol=$(echo "$logline"| grep -o -P '(?<=Roll : ).*(?=, Pitch)')
263
#pit=$(echo "$logline"| grep -o -P '(?<=Pitch : ).*(?=, Yaw)')
264
#yaw=$(echo "$logline"| grep -o -P '(?<=Yaw : ).*(?=})')
265
#echo "$logdate $logtime CAM=$cam Lat=$lat Lng=$lng Alt= $alt"
266
267
}
268
269
#========================= FUNCTION geotag ======================================================
270
# converts to WGS84 and tags photos with data found in lat,lon,alt.
271
function geotag
272
273
{
274
#jpgname="test.JPG"
275
#lat=69.6041115
276
#lon=23.2757452
277
#alt=122.8374
278
279
posWGS84=( $(echo "$lon $lat $alt" | cs2cs +proj=latlong +datum=WGS84)) # returns position as 22d16'32.683"E 69d36'14.801"N 123.837 in {posWGS84[0...2]}
280
lonWGS84ref=$(echo -n "${posWGS84[0]}" | tail -c-1)
281
lonWGS84pos=$(echo -n "${posWGS84[0]}" | head -c-1)
282
latWGS84ref=$(echo -n "${posWGS84[1]}" | tail -c-1)
283
latWGS84pos=$(echo -n "${posWGS84[1]}" | head -c-1)
284
285
#echo $lonWGS84pos
286
#echo $lonWGS84ref
287
#echo $latWGS84pos
288
#echo $latWGS84ref
289
#echo "original:"
290
#echo ${posWGS84[0]}
291
#echo ${posWGS84[1]}
292
#echo ${posWGS84[2]}
293
294
if [[ "$WRITE" == "true" ]]
295
then
296
exiftool -exif:gpsmapdatum="WGS-84" -exif:gpsaltitude="$alt" -exif:gpsaltituderef="Above Sea Level" -exif:gpslongitude="$lonWGS84pos" -exif:gpslongituderef="$lonWGS84ref" -exif:gpslatitude="$latWGS84pos" -exif:gpslatituderef="$latWGS84ref" "$jpgname" >/dev/null &
297
fi
298
let "jpgtagged++"
299
echo "ALT= "$alt" LON= "$lonWGS84pos" LATref= "$lonWGS84ref" LAT= "$latWGS84pos" LATref= "$latWGS84ref""
300
}
301
302
303
#========================= FUNCTION report ======================================================
304
function report
305
{
306
echo "Report:" | tee -a geotag.log
307
echo "Found $jpgtot photos" | tee -a geotag.log
308
echo "Found $camtot CAM log lines" | tee -a geotag.log
309
echo "Found $trigtot TRIG log lines" | tee -a geotag.log
310
echo "Time between first photo and log was ""$typical_offset""s" | tee -a geotag.log
311
312
if [[ "$WRITE" == "true" ]]
313
then
314
echo "Tagged $jpgtagged photos" | tee -a geotag.log
315
else
316
echo "Could have tagged $jpgtagged photos" | tee -a geotag.log
317
fi
318
319
if [[ "$MODE" != "POS" ]]
320
then
321
echo "Detected $jpgmiss missing photos" | tee -a geotag.log
322
echo "Detected $logmiss missing $MODE messages" | tee -a geotag.log
323
else
324
echo "(Unable to detect and report missing pictures and CAM messages in POS mode)" | tee -a geotag.log
325
fi
326
if [[ "$jpgnotinlog" -gt 0 ]]
327
then
328
echo "FAILED: to tag $jpgnotinlog jpg file(s) where no POS log matched (maybe increase difflimit?)" | tee -a geotag.log
329
fi
330
331
332
if [[ "$DELORG" == "true" ]]
333
then
334
echo "INFO: Deleting original .JPG that were cloned by EXIF" | tee -a geotag.log
335
sleep 1s
336
rm *.JPG_original
337
fi
338
339
if [[ "$WRITE" == "true" ]]
340
then
341
mv geotag.log geotagwrite.log
342
fi
343
344
}
345
346
347
#========================= FUNCTION load log to memory ======================================================
348
function loadlogdump
349
{
350
echo "INFO: Loading logdump into memory..." | tee -a geotag.log
351
OLDIFS=$IFS
352
IFS=$'\012' # \012 is a linefeed
353
logarray=( $(<logdump) )
354
IFS=$OLDIFS
355
logarraysize=${#logarray[@]}
356
echo "INFO: done, loaded $logarraysize lines." | tee -a geotag.log
357
}
358
359
#========================= FUNCTION load camdump to memory ======================================================
360
function loadcamdump
361
{
362
echo "INFO: Loading camdump into memory..." | tee -a geotag.log
363
OLDIFS=$IFS
364
IFS=$'\012' # \012 is a linefeed
365
logarray=( $(<camdump) )
366
IFS=$OLDIFS
367
logarraysize=${#logarray[@]}
368
echo "INFO: done, loaded $logarraysize lines." | tee -a geotag.log
369
}
370
371
372
##############################################################################################################
373
##############################################################################################################
374
##############################################################################################################
375
##############################################################################################################
376
##############################################################################################################
377
##############################################################################################################
378
#========================= FUNCTION CAM only loop ======================================================
379
function posloop
380
{
381
echo "INFO: Searching within POS log..." | tee -a geotag.log
382
loggotpos=0
383
384
385
###############get JPG file#########
386
while [[ "$running" == 1 ]]
387
do
388
389
if [ "$logmatch" == 1 ]
390
then #read next picture only if log line accepted
391
jpgname=$(awk 'NR=="'"$jpglistindex"'"{print;exit}' jpglist)
392
393
if [ "$jpgname" == "" ]
394
then
395
echo "END: Last photo processed" | tee -a geotag.log
396
running=0
397
report
398
exit 1
399
fi
400
readexif
401
fi
402
403
###############lookup LOG#########
404
logtarget=0 #reset, for new search
405
if [[ "$loglinereverse" -eq 1 || "$camindex" -eq "$camtot" || "$MODE" == "POS" ]]
406
then
407
POSONLY="true"
408
else
409
POSONLY="false"
410
fi
411
412
while [ "$logtarget" -eq 0 ]
413
do
414
let "loglinenum++"
415
416
#### check if we are out of log messages.
417
if [[ "$loglinenum" -gt "$logarraysize" ]]
418
then
419
echo "ERROR: Log ended at $loglinenum of $logarraysize while looking for $MODE message for filename $jpgname , maybe this photo is not a part of dataset, or in log, ignoring." | tee -a geotag.log
420
421
#read next picture only if log line accepted
422
jpgname=$(awk 'NR=="'"$jpglistindex"'"{print;exit}' jpglist)
423
let "loglinenum = loglinenummatched" # revert to last known good position in log - or to START, to mach photos in random order..
424
let "jpglistindex++" #move on to next
425
if [ "$jpgname" == "" ]
426
then
427
echo "END: Last photo processed" | tee -a geotag.log
428
running=0
429
report
430
exit 1
431
fi
432
readexif
433
fi
434
435
logline=${logarray["$loglinenum"]}
436
437
438
if [[ "$logline" == *CAM* ]]
439
then
440
logtype="CAM"
441
let "camindex++"
442
logtarget=1
443
readlog
444
# echo "DEBUG: at $loglinenum $logline"
445
fi
446
if [[ "$logline" == *POS* ]] && [[ "$POSONLY" == "true" || "$loglinereverse" -eq 1 ]]
447
then
448
loggotpos=1
449
logtype="POS"
450
logtarget=1
451
readlog
452
#echo "DEBUG: at $loglinenum $logline"
453
fi
454
455
if [[ "$SKIPCAM" -gt 0 ]] && [[ "$logtarget" -eq 1 ]]
456
then
457
let "SKIPCAM--"
458
logtarget=0
459
fi
460
461
462
463
done
464
465
#### check if we are out of suitable log messages.
466
if [[ "$loglinenum" -gt "$logarraysize" ]]
467
then
468
echo "ERROR: Log ended at $loglinenum of $logarraysize while looking for CAM message for filename $jpgname maybe there are too many photos" | tee -a geotag.log
469
running=0
470
report
471
exit 1
472
fi
473
474
#echo "DEBUG relevant logline found loglinenumber=$loglinenum logline=$logline "
475
######process log line
476
477
######## Calculate offset
478
diff=$(( ( $(date -ud "$jpgdate $jpgtime" +'%s') - $(date -ud "$logdate $logtime" +'%s') ) )) #get differance in seconds (integer)
479
difftot=$(echo "scale=2; ($diff+($jpgtimems - $logtimems))" | bc -l) # return floting point
480
######## Calculate time since last trigger command
481
differr=$(( ( $(date -ud "$logdate $logtime" +'%s') - $(date -ud "$logdateprev $logtimeprev" +'%s') ) )) #get differance in seconds (integer)
482
difftoterr=$(echo "scale=2; ($differr+($logtimems - $logtimemsprev))" | bc -l) # return floting point
483
difftotoff=$(echo "scale=2; ($difftot - $typical_offset)" | bc -l)
484
difftoterroff=$(echo "scale=2; ($differr - $typical_offset)" | bc -l) # return floting point
485
if [ "$typical_offset" == "notset" ]
486
then
487
typical_offset=$difftot
488
echo "INFO: Expected time offset between camera and log is ""$typical_offset""s (positive = camera is ahead)" | tee -a geotag.log
489
fi
490
491
492
493
###############compare offset to previous offset to detect skipped photos
494
495
diffeee=$(echo "(($difftot - $typical_offset)*100)" | bc -l | cut -f1 -d"." ) #keep integer
496
if [[ "$diffeee" -gt "$difflimit" ]] || [[ "$diffeee" -lt -"$difflimit" ]]
497
498
then
499
if [[ "$MODE" == "POS" ]] #Do fail analysis ONLY in CAM mode
500
then ##Speed up POS mode
501
logmatch=0
502
if [[ "$diffeee" -gt 50 ]] # are we more than 1 sec behind ?
503
then
504
let "loglinenum=loglinenum+25"
505
#echo "DEBUG: > 1 sec behind, jumping ahead"
506
fi
507
if [[ "$diffeee" -lt -25 ]] # are we more than 0.5 sec ahead ?
508
then
509
let "loglinenum=loglinenum-30"
510
let "revjumps++"
511
#echo "DEBUG: > 0.5 sec ahead, jumping back"
512
513
if [[ "$revjumps" -gt 30 ]] ## we have backed up too many times, the image must be invalid for this log !
514
then
515
echo "ERROR: Failed to find log time for image $jpgname, it's not in log or does not belong to this dataset.( maybe try higher difflimit ?)" | tee -a geotag.log
516
let "jpgnotinlog++"
517
let "jpglistindex++" #move on to next
518
let "revjumps=0"
519
logmatch=1 #trigger lookup of next picture
520
521
fi
522
fi
523
524
##do CAM mode analysis
525
526
fi
527
#logmatch=1 ###############################################################ACCEPT ANYTHING
528
#let "jpglistindex=jpglistindex+1" ###############################################################ACCEPT ANYTHING
529
530
else ## PHOTO AND LOG MATCHING
531
532
percent=$(echo "scale=2; (($jpglistindex/$jpgtot)*100)" | bc -l | cut -f1 -d".")
533
echo "MATCHED: ""$percent""% Done, time diff.: ""$difftotoff""s between $jpgname $jpgdate $jpgtime & $logtype log $loglinenum $logdate $logtime" | tee -a geotag.log
534
let "revjumps=0"
535
geotag
536
logmatch=1
537
#logmatcgfailinrow=0
538
let "jpglistindex++"
539
let "loglinenummatched = loglinenum + 1" # if we need to go back for POS message, go here
540
#loglinereverse=0
541
fi #save first image camera vs log offset
542
543
logdateprev=$logdate #for calculation of picture spacing
544
logtimeprev=$logtime #for calculation of picture spacing
545
logtimemsprev=$logtimems #for calculation of picture spacing
546
logtarget=0 #reset so we are looking for next logline next time
547
548
done #running
549
}
550
551
##### END POS loop ###########################################################################################
552
##############################################################################################################
553
##############################################################################################################
554
##############################################################################################################
555
##############################################################################################################
556
##############################################################################################################
557
558
##############################################################################################################
559
##############################################################################################################
560
##############################################################################################################
561
##############################################################################################################
562
##############################################################################################################
563
##############################################################################################################
564
#========================= FUNCTION CAM only loop ======================================================
565
# In case of missing photo: try next log entry
566
# in case of missing log entry: do not tag photo
567
function camloop
568
{
569
echo "INFO: Searching for CAM only..." | tee -a geotag.log
570
loggotpos=0
571
572
573
###############get JPG file#########
574
while [[ "$running" == 1 ]] ; do
575
if [ "$logmatch" == 1 ]
576
then #read next picture only if log line accepted
577
jpgname=$(awk 'NR=="'"$jpglistindex"'"{print;exit}' jpglist)
578
if [ "$jpgname" == "" ] ; then
579
echo "END: Last photo processed" | tee -a geotag.log
580
running=0
581
report
582
exit 1
583
fi
584
readexif
585
fi
586
587
###############lookup LOG#########
588
logtarget=0 #reset, for new search
589
590
while [ "$logtarget" -eq 0 ] ; do
591
let "loglinenum++"
592
logline=${logarray["$loglinenum"]}
593
594
if [[ "$logline" == *"$MODE"* ]] ; then
595
logtype="$MODE"
596
logtarget=1
597
readlog
598
#echo "DEBUG: at $loglinenum $logline"
599
fi
600
601
#skip CAM lines if needded
602
if [[ "$MODE" == "CAM" && "$SKIPCAM" -gt 0 ]] && [[ "$logtarget" -eq 1 ]] ; then
603
let "SKIPCAM--"
604
echo "INFO: Skipped a CAM log line." | tee -a geotag.log
605
logtarget=0
606
fi
607
608
#skip TRIG lines if needded
609
if [[ "$MODE" == "TRIG" && "$SKIPTRIG" -gt 0 ]] && [[ "$logtarget" -eq 1 ]] ; then
610
let "SKIPTRIG--"
611
logtarget=0
612
fi
613
614
done
615
616
#### check if we are out of suitable log messages.
617
if [[ "$loglinenum" -gt "$logarraysize" ]] ; then
618
echo "ERROR: Log ended at $loglinenum of $logarraysize while looking for $MODE message for filename $jpgname maybe there are too many photos" | tee -a geotag.log
619
running=0
620
report
621
exit 1
622
fi
623
624
######## Calculate offset
625
diff=$(( ( $(date -ud "$jpgdate $jpgtime" +'%s') - $(date -ud "$logdate $logtime" +'%s') ) )) #get differance in seconds (integer)
626
difftot=$(echo "scale=2; ($diff+($jpgtimems - $logtimems))" | bc -l) # return floting point
627
######## Calculate time since last trigger command
628
differr=$(( ( $(date -ud "$logdate $logtime" +'%s') - $(date -ud "$logdateprev $logtimeprev" +'%s') ) )) #get differance in seconds (integer)
629
difftoterr=$(echo "scale=2; ($differr+($logtimems - $logtimemsprev))" | bc -l) # return floting point
630
difftotoff=$(echo "scale=2; ($difftot - $typical_offset)" | bc -l)
631
difftoterroff=$(echo "scale=2; ($differr - $typical_offset)" | bc -l) # return floting point
632
if [ "$typical_offset" == "notset" ] ; then
633
typical_offset=$difftot
634
echo "INFO: Expected time offset between camera and log is ""$typical_offset""s (positive = camera is ahead)" | tee -a geotag.log
635
fi
636
637
difflimit=100 # CAM difflimit 1sec variation between command and exif
638
639
640
###############compare offset to previous offset to detect skipped photos
641
diffeee=$(echo "(($difftot - $typical_offset)*100)" | bc -l | cut -f1 -d"." ) #keep integer
642
if [[ "$diffeee" -gt "$difflimit" ]] || [[ "$diffeee" -lt -"$difflimit" ]]
643
then ##TOO MUCH DIFFERENCE
644
logmatch=0
645
if [[ "$diffeee" -lt -100 ]] ; then # are we more than 1 sec ahead ?
646
#let "loglinenum=loglinenum-10"
647
echo "WARNING: Found extra photo for which there is no $MODE event. I am at logline $loglinenum (Details below)"
648
echo "WARNING: Big Time diff.: ""$difftotoff""s between $jpgname date $jpgtime and $logtype logline $loglinenum $logdate $logtime Time since last $MODE event was ""$difftoterr""s" | tee -a geotag.log
649
logmatch=1 #make the log-fetcher get next picture
650
let "loglinenum=loglinenummatched" #make log-fetcher keep the log line for next picture
651
let "jpglistindex++" #move on to next picture
652
let "logmiss++" #count missing log items
653
654
fi
655
656
657
else ## PHOTO AND LOG MATCHING
658
659
if [[ $logmatch -eq 0 ]] && [[ "$logmatcgfailinrow" -lt 5 ]] ; then
660
let "jpgmiss++"
661
echo "ERROR: MISSING PHOTO #$jpgmiss detected, matched current photo with next logged $logtype logline" | tee -a geotag.log
662
fi
663
if [[ $logmatch -eq 0 ]] && [[ "$logmatcgfailinrow" -gt 4 ]] ; then
664
let "logmiss++"
665
echo "ERROR: MISSING $MODE log entry detected, matched current photo with logged $logtype logline" | tee -a geotag.log
666
fi
667
668
percent=$(echo "scale=2; (($jpglistindex/$jpgtot)*100)" | bc -l | cut -f1 -d".")
669
echo "MATCHED: ""$percent""% Done, time diff.: ""$difftotoff""s between $jpgname $jpgdate $jpgtime & $logtype log $loglinenum $logdate $logtime Time since last $MODE command ""$difftoterr""s" | tee -a geotag.log
670
geotag
671
logmatch=1
672
logmatcgfailinrow=0
673
let "jpglistindex++"
674
let "loglinenummatched = loglinenum + 1" # if we need to go back for POS message, go here
675
loglinereverse=0
676
fi #save first image camera vs log offset
677
678
logdateprev=$logdate #for calculation of picture spacing
679
logtimeprev=$logtime #for calculation of picture spacing
680
logtimemsprev=$logtimems #for calculation of picture spacing
681
logtarget=0 #reset so we are looking for next logline next time
682
683
done #running
684
}
685
686
##### END CAM only loop ######################################################################################
687
##############################################################################################################
688
##############################################################################################################
689
##############################################################################################################
690
##############################################################################################################
691
##############################################################################################################
692
693
694
###################################################################### List JPG's
695
find . -maxdepth 1 -name "*.[Jj][Pp][Gg]" | sort >jpglist
696
jpgtot=$(wc -l < jpglist)
697
echo "INFO: Found $jpgtot photos" | tee -a geotag.log
698
699
700
701
###################################################################### look for logdump, create if needed
702
if [ -f ./logdump ]; then
703
echo "INFO: found logdump"
704
else
705
binname=$(find . -maxdepth 1 -name "*.[Bb][Ii][Nn]")
706
if [[ "$binname" == "" ]]; then
707
echo "ERROR: .BIN log not found, make sure directory contain a .BIN or .bin log or logdump file." | tee -a geotag.log
708
exit 1
709
fi
710
for binname in $(find . -maxdepth 1 -name "*.[Bb][Ii][Nn]"); do
711
if [[ "$ML" == "true" ]]; then
712
echo "INFO: adding $binname to logdump..." | tee -a geotag.log
713
mavlogdump.py --types POS,CAM,TRIG --format standard "$binname" >> logdump
714
else
715
if [[ "$logsdone" < 1 ]]; then
716
echo "INFO: dumping $binname to logdump..." | tee -a geotag.log
717
mavlogdump.py --types POS,CAM,TRIG --format standard "$binname" > logdump
718
fi
719
fi
720
let "logsdone ++"
721
done
722
fi
723
echo "INFO: done" | tee -a geotag.log
724
725
###################################################################### DUMP camdump
726
if [ ! -f ./camdump ] && [[ "$MODE" != "POS" ]]; then
727
echo "INFO: no camdump found, exporting loglines from .bin to camdump..." | tee -a geotag.log
728
mavlogdump.py --types CAM,TRIG --format standard "$binname" > camdump
729
echo "INFO: done" | tee -a geotag.log
730
else
731
echo "INFO: camdump found" | tee -a geotag.log
732
fi
733
734
735
###################################################################### DUMP exiflog
736
#if [ ! -f ./exifdump ]; then
737
# echo "INFO: no exifdump found, exporting exif names and timestamps exifdump..." | tee -a geotag.log
738
# #mavlogdump.py --types POS,CAM,TRIG --format standard "$binname" > logdump
739
# echo "INFO: done" | tee -a geotag.log
740
#else
741
# echo "INFO: exifdump found" | tee -a geotag.log
742
#fi
743
744
745
###################################################################### Count CAM events
746
camtot=$(grep CAM logdump | wc -l)
747
echo "INFO: Found $camtot CAM log lines (camera shutter sensed)" | tee -a geotag.log
748
749
###################################################################### Count TRIG events
750
trigtot=$(grep TRIG logdump | wc -l)
751
echo "INFO: Found $trigtot TRIG log lines (commands to shoot)" | tee -a geotag.log
752
753
754
755
###################################################################### LOAD exifs to memory
756
#echo "INFO: Loading Photo data into memory..." | tee -a geotag.log
757
#OLDIFS=$IFS
758
#IFS=$'\012' # \012 is a linefeed
759
#logarray=( $(<exifdump) )
760
#IFS=$OLDIFS
761
#logarraysize=${#exifarray[@]}
762
#echo "INFO: done, loaded $exifarraysize lines." | tee -a geotag.log
763
764
765
766
###################################################################### Are we searching or processing ?
767
#if [ "$FIND" == "true" ]
768
#then
769
# loadcamdump
770
# camloop
771
#else
772
loadlogdump
773
if [[ "$MODE" == "POS" ]]; then
774
posloop
775
else
776
camloop
777
fi
778
#fi
779
780
781