2020-03-09 22:42:08 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Iterates over a bunch of .jpg or .cr2 files and matches
|
|
|
|
DateTimeOriginal from Exif tag to DateTime in a csv log
|
|
|
|
of a GeigerMuellerCounter and writes its value to the UserComment
|
|
|
|
Exif tag in µS/h"""
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import csv
|
|
|
|
import argparse
|
|
|
|
import pytz
|
|
|
|
import pyexiv2
|
|
|
|
import gpxpy
|
2020-03-09 23:43:01 +01:00
|
|
|
from functions import Radiation
|
2020-03-09 22:42:08 +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
|
|
|
|
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 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 UTC if omitted. This is useful, if the GPS-Logger
|
|
|
|
saves the time incl. timezone''')
|
|
|
|
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.')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Create timezone datetime object
|
|
|
|
local_timezone = pytz.timezone(args.timezone)
|
|
|
|
|
2020-03-09 23:52:03 +01:00
|
|
|
# Initialize two empty lists for comparison
|
|
|
|
radiation_list = []
|
|
|
|
position_list = []
|
|
|
|
|
|
|
|
# Import GeigerCounter log
|
|
|
|
with open(args.csv, "r") as f:
|
|
|
|
csv_reader = csv.reader(filter(lambda row: row[0] != '#', f),
|
|
|
|
delimiter=',', skipinitialspace=True)
|
|
|
|
|
|
|
|
for _, csv_raw_time, csv_raw_cpm, _ in csv_reader:
|
|
|
|
radiation = Radiation(csv_raw_time, csv_raw_cpm, local_timezone, args.sifactor)
|
|
|
|
radiation_list.append(radiation)
|
|
|
|
#print(radiation_list)
|
|
|
|
|
|
|
|
# close CSV file
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Import GPX track(s)
|
|
|
|
if args.gpx is not None:
|
|
|
|
gpx_file = open(args.gpx, 'r')
|
|
|
|
gpx_reader = gpxpy.parse(gpx_file)
|
|
|
|
|
|
|
|
for waypoint in gpx_reader.waypoints:
|
|
|
|
for track in gpx_reader.tracks:
|
|
|
|
for segment in track.segments:
|
|
|
|
for point in segment.points:
|
|
|
|
position = [point.time, point.latitude, point.longitude]
|
|
|
|
position_list.append(position)
|
|
|
|
#print(position_list)
|
|
|
|
|
|
|
|
|
2020-03-09 22:42:08 +01:00
|
|
|
# Inform the user about what is going to happen
|
2020-03-09 23:43:01 +01:00
|
|
|
if args.dry is True:
|
|
|
|
print('Not modifying anything. Just print what would happen without --dry')
|
|
|
|
else:
|
2020-03-09 22:42:08 +01:00
|
|
|
if args.outdir == ".":
|
|
|
|
print('Modifying photos in place (overwrite)')
|
|
|
|
else:
|
|
|
|
print('Modifying photos in', str(args.outdir), '(copy)')
|
|
|
|
|
2020-03-09 23:43:01 +01:00
|
|
|
for src_photo in args.photos:
|
2020-03-09 22:42:08 +01:00
|
|
|
# Get image file name out of path
|
2020-03-09 23:43:01 +01:00
|
|
|
photo_basename = os.path.basename(src_photo)
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
# 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 == ".":
|
2020-03-09 23:43:01 +01:00
|
|
|
photo = src_photo
|
2020-03-09 22:42:08 +01:00
|
|
|
else:
|
|
|
|
# be os aware and use the correct directory delimiter for destfile
|
2020-03-09 23:43:01 +01:00
|
|
|
dst_photo = os.path.join(args.outdir, photo_basename)
|
2020-03-09 22:42:08 +01:00
|
|
|
# Don't copy image if in dry-run mode
|
|
|
|
if args.dry == 'True':
|
2020-03-09 23:43:01 +01:00
|
|
|
shutil.copy(src_photo, dst_photo)
|
|
|
|
photo = dst_photo
|
2020-03-09 22:42:08 +01:00
|
|
|
else:
|
2020-03-09 23:43:01 +01:00
|
|
|
photo = src_photo
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
# Load Exif data from image
|
|
|
|
metadata = pyexiv2.ImageMetadata(photo)
|
|
|
|
metadata.read()
|
|
|
|
tag = metadata['Exif.Photo.DateTimeOriginal']
|
|
|
|
# tag.value creates datetime object in pictime
|
2020-03-09 23:43:01 +01:00
|
|
|
pic_naive_time = tag.value
|
2020-03-09 22:42:08 +01:00
|
|
|
# Set timezone
|
2020-03-09 23:43:01 +01:00
|
|
|
pic_time = pic_naive_time.astimezone(local_timezone)
|
2020-03-09 22:42:08 +01:00
|
|
|
|
2020-03-09 23:43:01 +01:00
|
|
|
# Print table header
|
|
|
|
print('{:<15} {:<25} {:<22}'.format('filename', 'date / time', 'Exif UserComment'))
|