2020-03-01 17:17:37 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-03-09 23:22:34 +01:00
|
|
|
# This file is now obsolete and is going to disappear.
|
|
|
|
|
2020-03-01 17:17:37 +01:00
|
|
|
"""Iterates over a bunch of .jpg or .cr2 files and matches
|
|
|
|
DateTimeOriginal from Exif tag to DateTime in a csv log
|
2020-03-05 13:36:01 +01:00
|
|
|
of a GeigerMuellerCounter and writes its value to the UserComment
|
|
|
|
Exif tag in µS/h"""
|
2020-03-01 17:17:37 +01:00
|
|
|
|
2020-03-07 21:29:25 +01:00
|
|
|
from datetime import datetime, timedelta
|
2020-03-01 23:21:52 +01:00
|
|
|
import os
|
2020-03-05 15:19:29 +01:00
|
|
|
import shutil
|
2020-03-01 22:03:04 +01:00
|
|
|
import csv
|
2020-03-01 22:35:28 +01:00
|
|
|
import argparse
|
2020-03-07 21:29:25 +01:00
|
|
|
import pytz
|
2020-03-04 15:17:56 +01:00
|
|
|
import pyexiv2
|
2020-03-06 14:36:02 +01:00
|
|
|
import gpxpy
|
2020-03-01 17:17:37 +01:00
|
|
|
|
2020-03-04 12:51:40 +01:00
|
|
|
# SIFACTOR for GQ Geiger counters
|
2020-03-01 18:05:47 +01:00
|
|
|
|
|
|
|
# 300 series: 0.0065 µSv/h / CPM
|
2020-03-01 17:17:37 +01:00
|
|
|
# 320 series: 0.0065 µSv/h / CPM
|
2020-03-01 18:05:47 +01:00
|
|
|
# 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
|
|
|
|
|
2020-03-01 23:40:49 +01:00
|
|
|
# Configure argument parser for cli options
|
2020-03-01 23:21:52 +01:00
|
|
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
2020-03-06 14:36:02 +01:00
|
|
|
description='''A unix-tyle tool that
|
|
|
|
extracts GPS and/or radiation data from GPX/CSV files and writes
|
|
|
|
them into the Exif tags of given photos.''')
|
2020-03-01 23:21:52 +01:00
|
|
|
parser.add_argument('-si', '--sifactor', type=float, default=0.0065,
|
|
|
|
help='Factor to multiply recorded CPM with.')
|
2020-03-09 15:56:28 +01:00
|
|
|
parser.add_argument('-tz', '--timezone', type=str, metavar='Timezone', default='utc',
|
2020-03-07 21:29:25 +01:00
|
|
|
help='''Manually set timezone of CSV / and Photo timestamp,
|
2020-03-09 15:56:28 +01:00
|
|
|
defaults to UTC if omitted. This is useful, if the GPS-Logger
|
|
|
|
saves the time incl. timezone''')
|
2020-03-07 19:10:56 +01:00
|
|
|
parser.add_argument('-d', '--dry', action='store_true',
|
|
|
|
help='Dry-run, do not actually write anything.')
|
2020-03-01 22:35:28 +01:00
|
|
|
parser.add_argument('csv', metavar='CSV', type=str,
|
|
|
|
help='Geiger counter history file in CSV format.')
|
2020-03-06 14:36:02 +01:00
|
|
|
parser.add_argument('-g', '--gpx', metavar='GPX', type=str,
|
|
|
|
help='GPS track in GPX format')
|
2020-03-01 22:35:28 +01:00
|
|
|
parser.add_argument('photos', metavar='Photo', type=str, nargs='+',
|
|
|
|
help='One or multiple photo image files to process.')
|
2020-03-01 23:21:52 +01:00
|
|
|
parser.add_argument('-o', '--outdir', type=str, default='.',
|
2020-03-06 14:36:02 +01:00
|
|
|
help='Directory to output processed photos.')
|
2020-03-01 22:35:28 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-03-07 21:29:25 +01:00
|
|
|
# Create timezone datetime object
|
|
|
|
localtz = pytz.timezone(args.timezone)
|
|
|
|
|
2020-03-05 16:34:35 +01:00
|
|
|
# Inform the user about what is going to happen
|
2020-03-09 16:09:19 +01:00
|
|
|
if args.dry is not None:
|
|
|
|
if args.outdir == ".":
|
|
|
|
print('Modifying photos in place (overwrite)')
|
|
|
|
else:
|
|
|
|
print('Modifying photos in', str(args.outdir), '(copy)')
|
2020-03-05 16:34:35 +01:00
|
|
|
else:
|
2020-03-09 16:09:19 +01:00
|
|
|
print('Not modifying anything. Just print what would happen without --dry')
|
|
|
|
|
2020-03-05 16:34:35 +01:00
|
|
|
# Print table header
|
2020-03-07 21:29:25 +01:00
|
|
|
print('{:<15} {:<25} {:<22}'.format('filename', 'date / time', 'Exif UserComment'))
|
2020-03-05 16:34:35 +01:00
|
|
|
|
2020-03-05 15:19:29 +01:00
|
|
|
for srcphoto in args.photos:
|
|
|
|
# Get image file name out of path
|
|
|
|
photo_basename = os.path.basename(srcphoto)
|
|
|
|
|
|
|
|
# Decide whether to modify photo in place or to copy it to outdir first
|
|
|
|
# Then set the destination file as 'photo' to work on
|
|
|
|
if args.outdir == ".":
|
|
|
|
photo = srcphoto
|
|
|
|
else:
|
|
|
|
# be os aware and use the correct directory delimiter for destfile
|
|
|
|
dstphoto = os.path.join(args.outdir, photo_basename)
|
2020-03-07 21:29:25 +01:00
|
|
|
# Don't copy image if in dry-run mode
|
2020-03-07 19:10:56 +01:00
|
|
|
if args.dry == 'True':
|
|
|
|
shutil.copy(srcphoto, dstphoto)
|
|
|
|
photo = dstphoto
|
|
|
|
else:
|
|
|
|
photo = srcphoto
|
2020-03-05 15:19:29 +01:00
|
|
|
|
2020-03-04 15:17:56 +01:00
|
|
|
# Load Exif data from image
|
|
|
|
metadata = pyexiv2.ImageMetadata(photo)
|
|
|
|
metadata.read()
|
|
|
|
tag = metadata['Exif.Photo.DateTimeOriginal']
|
2020-03-04 15:44:18 +01:00
|
|
|
# tag.value creates datetime object in pictime
|
2020-03-07 21:29:25 +01:00
|
|
|
picnaivetime = tag.value
|
|
|
|
# Set timezone
|
|
|
|
pictime = picnaivetime.astimezone(localtz)
|
|
|
|
|
2020-03-04 08:24:52 +01:00
|
|
|
# Import GeigerCounter log
|
|
|
|
with open(args.csv, "r") as f:
|
2020-03-04 11:15:58 +01:00
|
|
|
csvreader = csv.reader(filter(lambda row: row[0] != '#', f),
|
|
|
|
delimiter=',', skipinitialspace=True)
|
2020-03-01 22:35:28 +01:00
|
|
|
|
|
|
|
for _, csvrawtime, csvrawcpm, _ in csvreader:
|
2020-03-07 21:29:25 +01:00
|
|
|
csvnaivetime = datetime.fromisoformat(csvrawtime)
|
|
|
|
# Set timezone
|
|
|
|
csvtime = csvnaivetime.astimezone(localtz)
|
2020-03-05 16:34:35 +01:00
|
|
|
# Process image if its timestamp is found in CSV log (compares 2 datetime objects)
|
2020-03-04 15:44:18 +01:00
|
|
|
if csvtime == pictime:
|
2020-03-01 23:21:52 +01:00
|
|
|
rad = round(float(csvrawcpm) * args.sifactor, 2)
|
2020-03-01 22:35:28 +01:00
|
|
|
|
2020-03-05 13:36:01 +01:00
|
|
|
# Set key, value for new UserComment
|
|
|
|
key = 'Exif.Photo.UserComment'
|
2020-03-01 23:40:49 +01:00
|
|
|
new_comment = 'Radiation ☢ ' + str(rad) + ' µS/h'
|
2020-03-05 13:36:01 +01:00
|
|
|
metadata[key] = pyexiv2.ExifTag(key, new_comment)
|
2020-03-01 22:35:28 +01:00
|
|
|
|
2020-03-05 13:36:01 +01:00
|
|
|
# print found radiation levels
|
2020-03-07 21:29:25 +01:00
|
|
|
print('{:<15} {:<25} {:<22}'.format(photo_basename, str(pictime), new_comment))
|
|
|
|
# Write Exif tags to file, if not in dry-run mode
|
2020-03-07 19:10:56 +01:00
|
|
|
if args.dry == 'True':
|
|
|
|
metadata.write()
|
2020-03-01 22:35:28 +01:00
|
|
|
break
|
2020-03-01 23:40:49 +01:00
|
|
|
else:
|
2020-03-07 21:29:25 +01:00
|
|
|
print('{:<15} {:<25} {:<22}'.format(photo_basename, str(pictime), 'NOT FOUND!'))
|
2020-03-04 11:15:58 +01:00
|
|
|
# close CSV file
|
2020-03-04 08:24:52 +01:00
|
|
|
f.close()
|
2020-03-06 14:36:02 +01:00
|
|
|
|
|
|
|
# Import GPX track(s)
|
2020-03-07 19:10:56 +01:00
|
|
|
if args.gpx is not None:
|
2020-03-06 14:36:02 +01:00
|
|
|
gpx_file = open(args.gpx, 'r')
|
|
|
|
gpxreader = gpxpy.parse(gpx_file)
|
|
|
|
|
|
|
|
for waypoint in gpxreader.waypoints:
|
|
|
|
for track in gpxreader.tracks:
|
|
|
|
for segment in track.segments:
|
|
|
|
for point in segment.points:
|
2020-03-07 21:29:25 +01:00
|
|
|
# datetimes match with 1 minute precision
|
2020-03-09 17:02:17 +01:00
|
|
|
delta = timedelta(seconds=10)
|
2020-03-07 21:29:25 +01:00
|
|
|
if abs(point.time - pictime) < delta:
|
2020-03-09 17:02:17 +01:00
|
|
|
valuelist = []
|
|
|
|
row = [point.time, point.latitude, point.longitude]
|
|
|
|
valuelist.append(row)
|
|
|
|
print(valuelist)
|
|
|
|
#print(pictime, 'vs.', point.time, 'Delta:', pictime - point.time)
|
2020-03-06 14:36:02 +01:00
|
|
|
# print('Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.time))
|