import os
import sys
import math
import subprocess

# Put your calibration data in the line below. Connect to camera wifi and visit http://192.168.10.254/calibration/rig_relatives for the string
# For example: data = {"1":[0.1,0.2,0.3],"2":[0,0,0],"3":[0.4,0.5,0.6],"4":[0.7,0.8,0.9],"5":[1.0,1.1,1.2]}

data = {"1":[0.1,0.2,0.3],"2":[0,0,0],"3":[0.4,0.5,0.6],"4":[0.7,0.8,0.9],"5":[1.0,1.1,1.2]} # Paste rig relatives from your camera here. 

if data == None:
	print "Paste rig relatives string from your camera into the 'data' variable. Connect to camera wifi and visit http://192.168.10.254/calibration/rig_relatives to obtain the string"
	exit(1)

if len(sys.argv) < 2:
   print("Please provide a directory to update.\nUsage:\n\tpython %s /path/to/your/images" %sys.argv[0])
   exit(1)

img_dir = sys.argv[1] 

def rad2deg(x):
	return x * 180/math.pi

for f in os.listdir(img_dir):
	if ".tif" not in f[-4:]:
		continue
	i = int(f[-5:-4])
	modrels = map(rad2deg, data[str(i)])
	cal = ",".join(str(s) for s in modrels)
	command = 'exiv2 -M"reg Camera http://pix4d.com/camera/1.0" -M"del Xmp.Camera.RigRelatives" -M"set Xmp.Camera.RigRelatives XmpText ' + cal + '" "' + img_dir + f + '"' 
	#print(command) 
	os.system(command)
 