2020-03-09 22:42:08 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-03-10 21:09:25 +01:00
|
|
|
''' Classes used by main program. '''
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
from datetime import datetime
|
2020-03-10 21:09:25 +01:00
|
|
|
import pyexiv2
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
class Radiation:
|
2020-03-10 21:09:25 +01:00
|
|
|
''' Handles CSV processing.'''
|
2020-03-09 22:42:08 +01:00
|
|
|
def __init__(self, timestamp, radiation, local_timezone, si_factor):
|
|
|
|
self.timestamp = self._time_conversion(timestamp, local_timezone)
|
2020-03-10 21:09:25 +01:00
|
|
|
self.radiation = self._radiation_conversion(radiation, si_factor)
|
2020-03-09 22:42:08 +01:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '%s %f µS/h' % (str(self.timestamp), self.radiation)
|
|
|
|
|
|
|
|
def _time_conversion(self, timestamp, local_timezone):
|
|
|
|
csv_naive_time = datetime.fromisoformat(timestamp)
|
|
|
|
# Set timezone
|
|
|
|
csv_aware_time = csv_naive_time.astimezone(local_timezone)
|
|
|
|
return csv_aware_time
|
|
|
|
|
2020-03-10 21:09:25 +01:00
|
|
|
def _radiation_conversion(self, radiation, si_factor):
|
2020-03-09 22:42:08 +01:00
|
|
|
# Convert CP/M to µS/h using si_factor
|
|
|
|
radiation = round(float(radiation) * si_factor, 2)
|
|
|
|
return radiation
|
2020-03-10 21:09:25 +01:00
|
|
|
|
|
|
|
class Photo:
|
|
|
|
''' Reads and writes Exif metadata'''
|
|
|
|
def __init__(self, photo, local_timezone):
|
|
|
|
self.get_date = self._get_creation_date(photo, local_timezone)
|
|
|
|
self.photo = photo
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'Photo Creation Date: %s' % str(self.get_date)
|
|
|
|
|
|
|
|
def _get_creation_date(self, photo, local_timezone):
|
|
|
|
# Load Exif data from photo
|
|
|
|
metadata = pyexiv2.ImageMetadata(photo)
|
|
|
|
metadata.read()
|
|
|
|
date = metadata['Exif.Photo.DateTimeOriginal']
|
|
|
|
# date.value creates datetime object in pic_naive_time
|
|
|
|
pic_naive_time = date.value
|
|
|
|
# Set timezone
|
|
|
|
pic_aware_time = pic_naive_time.astimezone(local_timezone)
|
|
|
|
return pic_aware_time
|
|
|
|
|
|
|
|
def write_exif(self, radiation, latitude, longitude, dry_run):
|
|
|
|
|
|
|
|
''' UNTESTED ! '''
|
|
|
|
|
|
|
|
metadata = pyexiv2.ImageMetadata(self.photo)
|
|
|
|
|
|
|
|
# Set new UserComment
|
|
|
|
new_comment = 'Radiation ☢ ' + str(radiation) + ' µS/h'
|
|
|
|
# Exif tags to write
|
|
|
|
keys = ['Exif.Photo.UserComment', 'Exif.Photo.latitude', 'Exif.Photo.longitude']
|
|
|
|
# Values to write
|
|
|
|
values = [new_comment, latitude, longitude]
|
|
|
|
|
|
|
|
# Create metadata object with all data to write
|
|
|
|
for key, value in zip(keys, values):
|
2020-03-10 21:53:43 +01:00
|
|
|
# Only create object if there is anything to fill with
|
|
|
|
if value is not None:
|
|
|
|
metadata[key] = pyexiv2.ExifTag(key, value)
|
2020-03-10 21:09:25 +01:00
|
|
|
|
|
|
|
# Write Exif tags to file, if not in dry-run mode
|
|
|
|
if dry_run == 'True':
|
|
|
|
metadata.write()
|
|
|
|
return new_comment
|