2020-03-14 18:02:40 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-12-26 21:09:58 +01:00
|
|
|
'''
|
|
|
|
Iterates over a bunch of .jpg or .cr2 files and matches
|
2020-04-11 00:43:29 +02:00
|
|
|
DateTimeOriginal from Exif tags to DateTime in a csv log
|
2020-12-26 21:09:58 +01:00
|
|
|
of a GeigerMuellerCounter and writes its value to Exif/ITPC/XMP tags in µS/h
|
|
|
|
'''
|
2020-03-14 18:02:40 +01:00
|
|
|
|
|
|
|
import csv
|
|
|
|
import argparse
|
|
|
|
import pytz
|
|
|
|
import gpxpy
|
2020-03-21 17:17:42 +01:00
|
|
|
from functions import Radiation, Photo, Match, Exif, Output
|
2020-03-14 18:02:40 +01:00
|
|
|
|
|
|
|
# SIFACTOR for GQ Geiger counters
|
|
|
|
|
|
|
|
# 300 series: 0.0065 µSv/h / CPM
|
|
|
|
# 320 series: 0.0065 µSv/h / CPM
|
|
|
|
# 500 series: 0.0065 µSv/h / CPM
|
|
|
|
# 500+ series: 0.0065 µSv/h / CPM for the first tube
|
|
|
|
# 600 series: 0.0065 µSv/h / CPM
|
|
|
|
# 600+ series: 0.002637 µSv/h / CPM
|
|
|
|
|
|
|
|
# Configure argument parser for cli options
|
2020-12-26 21:09:58 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
description='''A unix-tyle tool that extracts GPS and/or radiation data
|
|
|
|
from GPX/CSV files and writes them into the Exif/ITPC/XMP tags of given
|
|
|
|
photos.'''
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-si', '--sifactor',
|
|
|
|
type=float,
|
|
|
|
default=0.0065,
|
|
|
|
help='Factor to multiply recorded CPM with.'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-tz', '--timezone',
|
|
|
|
type=str,
|
|
|
|
metavar='Timezone',
|
|
|
|
default='utc',
|
|
|
|
help='''Manually set timezone of CSV / and Photo timestamp, defaults to
|
2020-12-27 01:41:25 +01:00
|
|
|
UTC if omitted. This is useful, if the GPS-Logger saves the time in local
|
|
|
|
time (without timezone).'''
|
2020-12-26 21:09:58 +01:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--dry',
|
|
|
|
action='store_true',
|
|
|
|
help='Dry-run, do not actually write anything.'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'csv',
|
|
|
|
metavar='CSV',
|
|
|
|
type=str,
|
|
|
|
help='Geiger counter history file in CSV format.'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-g', '--gpx',
|
|
|
|
metavar='GPX',
|
|
|
|
type=str,
|
|
|
|
help='GPS track in GPX format'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'photos',
|
|
|
|
metavar='Photo',
|
|
|
|
type=str,
|
|
|
|
nargs='+',
|
|
|
|
help='One or multiple photo image files to process.'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'-o', '--outdir',
|
|
|
|
type=str,
|
|
|
|
default='.',
|
|
|
|
help='Directory to output processed photos.'
|
|
|
|
)
|
2020-03-14 18:02:40 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Create timezone datetime object
|
|
|
|
local_timezone = pytz.timezone(args.timezone)
|
|
|
|
|
2020-03-22 11:35:53 +01:00
|
|
|
# Initialize two empty lists for all radiation / gps values
|
2020-03-14 18:02:40 +01:00
|
|
|
radiation_list = []
|
|
|
|
position_list = []
|
|
|
|
|
|
|
|
# Import GeigerCounter log
|
|
|
|
with open(args.csv, "r") as f:
|
2020-12-26 21:09:58 +01:00
|
|
|
# Read csv file, filter out lines beginning with #
|
|
|
|
csv = csv.reader(
|
|
|
|
filter(lambda row: row[0] != '#', f),
|
|
|
|
delimiter=',',
|
|
|
|
skipinitialspace=True
|
|
|
|
)
|
2020-03-14 18:02:40 +01:00
|
|
|
|
2020-03-26 20:18:37 +01:00
|
|
|
# Import only relevant values, that's timestamp and CP/M
|
2020-03-14 18:02:40 +01:00
|
|
|
for _, csv_raw_time, csv_raw_cpm, _ in csv:
|
2020-12-26 21:09:58 +01:00
|
|
|
radiation = Radiation(
|
|
|
|
csv_raw_time,
|
|
|
|
csv_raw_cpm,
|
|
|
|
local_timezone,
|
|
|
|
args.sifactor
|
|
|
|
)
|
2020-03-14 18:02:40 +01:00
|
|
|
radiation_list.append(radiation)
|
|
|
|
# close CSV file
|
|
|
|
f.close()
|
|
|
|
|
2020-03-20 20:05:58 +01:00
|
|
|
# Import GPX track(s)print
|
2020-03-21 17:17:42 +01:00
|
|
|
if args.gpx:
|
2020-03-14 18:02:40 +01:00
|
|
|
gpx_file = open(args.gpx, 'r')
|
|
|
|
gpx_reader = gpxpy.parse(gpx_file)
|
2020-03-20 20:05:58 +01:00
|
|
|
for track in gpx_reader.tracks:
|
|
|
|
for segment in track.segments:
|
|
|
|
for point in segment.points:
|
|
|
|
point_aware_time = point.time.astimezone(local_timezone)
|
2020-12-26 21:09:58 +01:00
|
|
|
position = (
|
|
|
|
point_aware_time,
|
|
|
|
point.latitude,
|
|
|
|
point.longitude,
|
|
|
|
point.elevation
|
|
|
|
)
|
2020-03-20 20:05:58 +01:00
|
|
|
position_list.append(position)
|
2020-03-14 18:02:40 +01:00
|
|
|
|
|
|
|
# Inform the user about what is going to happen
|
2020-03-21 17:17:42 +01:00
|
|
|
if args.dry:
|
2020-03-14 18:02:40 +01:00
|
|
|
print('Not modifying anything. Just print what would happen without --dry')
|
|
|
|
else:
|
|
|
|
if args.outdir == ".":
|
|
|
|
print('Modifying photos in place (overwrite)')
|
|
|
|
else:
|
|
|
|
print('Modifying photos in', str(args.outdir), '(copy)')
|
|
|
|
|
2020-03-21 17:17:42 +01:00
|
|
|
# Print table header
|
|
|
|
print('{:<15} {:<25} {:<22}'.format('filename', 'date / time', 'Matched Data'))
|
|
|
|
|
2020-12-26 21:09:58 +01:00
|
|
|
# Iterate over list of photos
|
2020-03-14 18:02:40 +01:00
|
|
|
for src_photo in args.photos:
|
2020-12-26 21:09:58 +01:00
|
|
|
# Instantiate photo, copy it to destdir if needed and receive filename
|
|
|
|
# to work on
|
2020-03-14 18:02:40 +01:00
|
|
|
photo = Photo(src_photo, local_timezone, args.outdir, args.dry)
|
2020-03-14 20:42:01 +01:00
|
|
|
|
2020-03-21 17:17:42 +01:00
|
|
|
# Here the matching magic takes place
|
2020-03-15 01:57:02 +01:00
|
|
|
match = Match(photo.get_date, radiation_list, position_list)
|
2020-03-21 18:36:25 +01:00
|
|
|
|
2020-03-21 17:17:42 +01:00
|
|
|
# Formatted output:
|
2020-12-26 21:09:58 +01:00
|
|
|
data = Output(
|
|
|
|
match.radiation_value,
|
|
|
|
match.position_latitude,
|
|
|
|
match.position_longitude,
|
|
|
|
match.position_altitude
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
'{:<15} {:<25} {:<22}'.format(photo.get_photo_basename,
|
|
|
|
str(photo.get_date),
|
|
|
|
str(data))
|
|
|
|
)
|
2020-03-15 01:57:02 +01:00
|
|
|
|
2020-03-14 18:02:40 +01:00
|
|
|
# Write exif data
|
2020-12-26 21:09:58 +01:00
|
|
|
Exif(
|
|
|
|
photo.get_photo_filename,
|
|
|
|
args.dry,
|
|
|
|
match.radiation_value,
|
|
|
|
match.position_latitude,
|
|
|
|
match.position_longitude,
|
|
|
|
match.position_altitude
|
|
|
|
)
|