From c2ed0e1c2a3e17e436fcee81a05431529e06ce07 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Sat, 21 Mar 2020 17:17:42 +0100 Subject: [PATCH] Added Output class that formats matched data and returns string for printing. Added beautified output. --- functions.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++---- rad_tag.py | 27 ++++++++++++++++--------- 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/functions.py b/functions.py index 98a2cc9..9274b42 100644 --- a/functions.py +++ b/functions.py @@ -74,10 +74,10 @@ class Photo: # Copy photo to dest_dir and return its (new) filename # if not in dry_run mode or if dest_dir is different from src_dir. if dry_run is True: - return photo + return photo_basename, photo if dest_dir != '.': shutil.copy(photo, dest_photo) - return dest_photo + return photo_basename, dest_photo def _get_creation_date(self, photo, local_timezone): # Load Exif data from photo @@ -114,9 +114,14 @@ class Match: radiation = round(self.radiation[1], 2) else: radiation = None + + if self.position[1][3]: + altitude = round(self.position[1][3]) + else: + altitude = None return 'Radiation: %s µS/h (Δt %s) \nPosition: Lat: %s, Long: %s, Alt: %sm (Δt %s)' % \ (str(radiation), str(self.radiation[0]), str(self.position[1][1]), \ - str(self.position[1][2]), str(self.position[1][3]), str(self.position[0])) + str(self.position[1][2]), altitude, str(self.position[0])) def _find_radiation_match(self, photo_time, list): valuelist = [] @@ -153,6 +158,7 @@ class Match: if valuelist: #print(min(valuelist, key=lambda x: x[0])) return min(valuelist, key=lambda x: x[0]) + # Return Nones in the same cascaded manner as if it matched. return [None, [None, None, None, None]] class Exif: @@ -161,7 +167,7 @@ class Exif: Arguments: photo: file name of photo to modify - radiation: radiation levels in µS/h + radiation: radiation levels float latitude: latitude as float longitude: longitude as float elevation: elevation as float @@ -238,3 +244,46 @@ class Exif: metadata.write() return latitude_degree, longitude_degree, new_comment + +class Output: + ''' + Receives values to be printed, formats them and returns a string for printing. + + Arguments: + radiation: radiation as float + latitude: latitude as float + longitude: longitude as float + elevation: elevation as float + + Returns: + A String that can be printed in output + ''' + + def __init__(self, radiation, latitude, longitude, altitude): + self.get_string = self._get_string(radiation, latitude, longitude, altitude) + + def __repr__(self): + return self.get_string + + def _get_string(self, radiation, latitude, longitude, altitude): + # Convert values to styled strings + if radiation: + rad = '☢: %sµS/h ' % str(round(radiation, 2)) + else: + rad = '☢: N/A ' + + if latitude and longitude: + latlon = 'Lat.: %s Long.: %s ' % (str(latitude), str(longitude)) + else: + latlon = 'Lat.: N/A, Long.: N/A ' + + if altitude: + alt = 'Alt.: %sm' % str(round(altitude, 1)) + else: + alt = 'Alt.: N/A' + + data = rad + latlon + alt + + # Return data string + return data + diff --git a/rad_tag.py b/rad_tag.py index f0f5a58..182dd79 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, Match, Exif +from functions import Radiation, Photo, Match, Exif, Output # SIFACTOR for GQ Geiger counters @@ -64,7 +64,7 @@ with open(args.csv, "r") as f: f.close() # Import GPX track(s)print -if args.gpx is not None: +if args.gpx: gpx_file = open(args.gpx, 'r') gpx_reader = gpxpy.parse(gpx_file) #for waypoint in gpx_reader.waypoints: @@ -72,13 +72,12 @@ if args.gpx is not None: 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) position_list.append(position) # Inform the user about what is going to happen -if args.dry is True: +if args.dry: print('Not modifying anything. Just print what would happen without --dry') else: if args.outdir == ".": @@ -86,15 +85,23 @@ else: else: print('Modifying photos in', str(args.outdir), '(copy)') +# Print table header +print('{:<15} {:<25} {:<22}'.format('filename', 'date / time', 'Matched Data')) + for src_photo in args.photos: + # Instantiate photo, copy it to destdir if needed and receive filename to work on photo = Photo(src_photo, local_timezone, args.outdir, args.dry) - print(photo.get_target_photo, photo.get_date) - - # Here the matching magic has to happen + # Here the matching magic takes place match = Match(photo.get_date, radiation_list, position_list) + + # Formatted output: + #print(Output(photo.get_target_photo[0], photo.get_date, match.radiation[1], + # match.position[1][1], match.position[1][2], match.position[1][3])) + data = Output(match.radiation[1], match.position[1][1], match.position[1][2], match.position[1][3]) + + print('{:<15} {:<25} {:<22}'.format(photo.get_target_photo[0], str(photo.get_date), str(data))) # Write exif data - exif_tags = Exif(photo.get_target_photo, args.dry, match.radiation[1], - match.position[1][1], match.position[1][2], match.position[1][3]) - print(exif_tags) + Exif(photo.get_target_photo[1], args.dry, match.radiation[1], + match.position[1][1], match.position[1][2], match.position[1][3])