From 952f2726ba4dc35a68d32aa30a4813eecdead323 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 20 Mar 2020 20:05:58 +0100 Subject: [PATCH] Added a working match class that returns best match for radiation and position. Still ugly and a lot of debug output. Also not finished. --- functions.py | 38 +++++++++++++++++++++++++++----------- rad_tag.py | 24 +++++++++++++----------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/functions.py b/functions.py index 49b2909..2f7a45b 100644 --- a/functions.py +++ b/functions.py @@ -11,7 +11,7 @@ import pyexiv2 class Radiation: ''' - Reiceives Values vom CSV file and creates a list of the relevant data + Reiceives values vom CSV file and creates a list of the relevant data Arguments: timestamp: Date/time string from CSV as string @@ -101,27 +101,24 @@ class Match: position_list: list of timestamp / position / elevation values Returns: - timestamp: as datetime object - radiation: in µS/h as string - latitude: in decimal format as float - longitude: in decimal format as float - elevation: in meters as float + minimal timedelta: as timedelta object + best matching valuerow ''' def __init__(self, photo_time, radiation_list, position_list): -# self.radiation = self._find_match(photo_time, radiation_list) - self.position = self._find_match(photo_time, position_list) + self.radiation = self._find_radiation_match(photo_time, radiation_list) + self.position = self._find_position_match(photo_time, position_list) def __repr__(self): pass - def _find_match(self, photo_time, list): + def _find_radiation_match(self, photo_time, list): valuelist = [] for row in list: # Define timedelta and define timedelta datetime object. delta = timedelta(seconds=60) - time_delta = abs(row.timestamp - photo_time) -# time_delta = abs(row[0] - photo_time) + if row.timestamp: + time_delta = abs(row.timestamp - photo_time) # datetime objects should match with 1 minute precision. if time_delta < delta: element = (time_delta, row) @@ -129,6 +126,25 @@ class Match: # Return the list item with the lowest timedelta in column 0. # Column 2 contains the source objects untouched. if valuelist: + print(min(valuelist, key=lambda x: x[0])) + return min(valuelist, key=lambda x: x[0]) + return None + + def _find_position_match(self, photo_time, list): + valuelist = [] + for row in list: + # Define timedelta and define timedelta datetime object. + delta = timedelta(seconds=60) + if row[0]: + time_delta = abs(row[0] - photo_time) + # datetime objects should match with 1 minute precision. + if time_delta < delta: + element = (time_delta, row) + valuelist.append(element) + # Return the list item with the lowest timedelta in column 0. + # Column 2 contains the source objects untouched. + if valuelist: + print(min(valuelist, key=lambda x: x[0])) return min(valuelist, key=lambda x: x[0]) return None diff --git a/rad_tag.py b/rad_tag.py index 24e5bb6..318999b 100755 --- a/rad_tag.py +++ b/rad_tag.py @@ -10,7 +10,7 @@ import csv import argparse import pytz import gpxpy -from functions import Radiation, Photo, Exif, Match +from functions import Radiation, Photo, Match, Exif # SIFACTOR for GQ Geiger counters @@ -60,21 +60,22 @@ with open(args.csv, "r") as f: for _, csv_raw_time, csv_raw_cpm, _ in csv: radiation = Radiation(csv_raw_time, csv_raw_cpm, local_timezone, args.sifactor) radiation_list.append(radiation) - # close CSV file f.close() -# Import GPX track(s) +# Import GPX track(s)print 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, point.elevation] - position_list.append(position) + #for waypoint in gpx_reader.waypoints: + for track in gpx_reader.tracks: + for segment in track.segments: + for point in segment.points: + point_aware_time = point.time.astimezone(local_timezone) + #point_aware_time = point_naive_time.astimezone(local_timezone) + position = (point_aware_time, point.latitude, point.longitude, + point.elevation, local_timezone) + position_list.append(position) # Inform the user about what is going to happen if args.dry is True: @@ -92,6 +93,7 @@ for src_photo in args.photos: # Here the matching magic has to happen match = Match(photo.get_date, radiation_list, position_list) + #print(match) latitude = 51.0234024 longitude = 7.248347 @@ -100,7 +102,7 @@ for src_photo in args.photos: # Write exif data exif_tags = Exif(photo.get_target_photo, args.dry, radiation, latitude, longitude, elevation) - print(exif_tags) + #print(exif_tags) # Print table header print('{:<15} {:<25} {:<22}'.format('filename', 'date / time', 'Exif UserComment')) \ No newline at end of file