def main():	
	args=ArgParse()
	getDasLoc()
	
	# check all machines are up
	cont=0
	for i in das:
		if das[i]:
			x=os.popen('ping -w 0.2 -c 1 %s' % (das[i])).readlines()
			if ' 0% packet loss' in x[-2]:
				cont+=0
			else:
				cont+=1
				
	if cont > 0:
		logger.fatal("MACHINES ARE DOWN - ignoring image generation (NFS issues)")
		sys.exit(1)
	
	# get time of start
	t1=datetime.datetime.utcnow()
			
	if args.pngs:
		ex=0
		# check the camera list
		csplit=args.pngs.split(',')
		if len(csplit) < 1:
			ex+=1
		else:
			for i in csplit:
				if int(i) not in cams:
					ex+=1			
		if ex > 0:
			logger.fatal("Problem in pngs...")
			logger.fatal("Enter list like --pngs 801,802,803,...8[n]")
			logger.fatal("Exiting...")
			sys.exit(1)
		else:
			make_pngs(args.pngs)	
	if args.montage:
		make_montage(movie_dir,das)
	if args.movie:
		movie_date=(datetime.datetime.utcnow()-timedelta(days=1)).strftime('%Y%m%d')
		movie_name="%s/daily_movies/movie_%s.mp4" % (movie_dir,movie_date)
		make_movie(movie_dir,movie_name)
		# send to warwick webserver
		os.system('scp %s [email protected]:/srv/www/ngts/ops/daily_movies/' % (movie_name))
		
		# clean up the pngs
		os.system('/bin/rm /ngts/aux07/movie/tiled*.png')
		for i in das:
			if das[i] != None:
				os.system('/bin/rm /ngts/aux07/movie/%s/IMAGE*.png' % (das[i]))
		
	t2=datetime.datetime.utcnow()
	dt=(t2-t1).total_seconds()/60.
	
	print "Runtime: %.2f mins" % (dt) 
			print s
		
getDasLoc()

# check all machines are up
cont=0
for i in das:
	if das[i]:
		x=os.popen('ping -w 0.2 -c 1 %s' % (das[i])).readlines()
		if ' 0% packet loss' in x[-2]:
			cont+=0
		else:
			cont+=1
			
if cont > 0:
	logger.fatal("MACHINES ARE DOWN - ignoring image generation (NFS issues)")
	sys.exit(1)
	
# connect to database
conn=pymysql.connect(host='ds',db='ngts_ops')
cur=conn.cursor()
		
# get the actions from yesterday
#qry="SELECT action_id,camera_id,action FROM action_list WHERE schedule_start_utc BETWEEN date_sub(now(), INTERVAL 1 DAY) AND now()"
#cur.execute(qry)

os.chdir(topdir)	
for cam in cams:
	qry="SELECT image_id,raw_image_list.camera_id,raw_image_list.action_id,action FROM raw_image_list LEFT JOIN action_list USING (action_id) WHERE raw_image_list.camera_id=%d ORDER BY image_id DESC LIMIT 1 " % (cam)
	cur.execute(qry)
	
def make_montage(movie_dir,das):
	'''
	sync the pngs according to earliest image that day then
	montage all the pngs with imagemagick
	'''
	
	if os.path.exists(movie_dir) == False:
		os.mkdir(movie_dir)
	
	os.chdir(movie_dir)		
	logger.info("Moving to: " + os.getcwd())
	
	# empty lists for various things	
	t_refs=[]
	das_tracker=[]
	imlens=[]
	
	##############################
	# scan all folders looking for 
	# earliest image of the night
	##############################
	
	noimages=0
	
	for i in das:
		if das[i] != None:
			os.chdir(das[i])
			logger.info("Moving to: " + os.getcwd())
			
			t=sorted(g.glob('*.png'))
			if len(t) == 0:
				os.chdir('../')
				logger.info("Moving to: " + os.getcwd())
				continue
			
			x=getDatetime(t[0])				
			t_refs.append(x)
			imlens.append(len(t))
			das_tracker.append(das[i])
			noimages+=1
			
			os.chdir('../')
			logger.info("Moving to: " + os.getcwd())
	
	# check for no data
	# exit if so
	if noimages == 0:
		logger.fatal("No pngs found, exiting...")
		sys.exit(1)
	
	# list of earliest times per camera
	# and length of imaging run		
	t_refs=np.array(t_refs)		
	imlens=np.array(imlens)
	
	# now work out which was the earliest and go there to start the time series
	n=np.where(t_refs==min(t_refs))[0]
	if len(n) > 1:
		n=n[0]
	logger.info("Reference DAS machine: " + das_tracker[n])

	##############################
	# start in earliest folder and
	# generate a list of reference times
	##############################	
	
	os.chdir(das_tracker[n])
	logger.info("Moving to: " + os.getcwd())

	# these are the time slots, match the other images to start with a certain slot
	slots=np.arange(0,imlens[n],1)
	
	# reset t_refs for start_id calculations
	t=sorted(g.glob('*.png'))
	t_refs=[]
	for i in range(0,len(t)):		
		x=getDatetime(t[i])
		t_refs.append(x)
	
	os.chdir('../')
	logger.info("Moving to: " + os.getcwd())
	
	
	##############################
	# now go through each other dir and
	# generate their starting points
	##############################
	
	for i in das:
		if das[i] != None:
			os.chdir(das[i])
			logger.info("Moving to: " + os.getcwd())
			
			t=sorted(g.glob('*.png'))
			if len(t) == 0:
				os.chdir('../')
				logger.info("Moving to: " + os.getcwd())
				continue
				
			x=getDatetime(t[0])	
			diff=[]
			for j in range(0,len(t_refs)):
				diff.append(abs((t_refs[j]-x).total_seconds()))
			
			z=diff.index(min(diff))
			start_id[i]=z
			
			os.chdir('../')
			logger.info("Moving to: " + os.getcwd())
	
	logger.info("Dictionary of start_ids:")
	logger.info(start_id)
	
	##############################
	# work out the new video size for
	# non time overlapping images
	##############################
	 
	max_start=0
	for i in start_id:
		if start_id[i]>max_start:
			max_start=start_id[i]
	run_len=int(max(imlens)+max_start)
	
	##############################
	# montage based on start_ids
	##############################
	
	# keep a dictionary of the directory contents from 
	# first glob as to not check each time we loop around...
	t={801:[],
		802:[],
		803:[],
		804:[],
		805:[],
		806:[],
		807:[],
		808:[],
		809:[],
		810:[],
		811:[],
		812:[]}
	
	for i in range(0,run_len):
		files=""
		
		for j in das:
			if i==0:
				if das[j] != None:
					t[j].append(sorted(g.glob('%s/*.png' % (das[j]))))
				else:
					t[j].append([])
			
			if start_id[j] == -1 or i < start_id[j]:
				files=files+"empty/empty.png "
			else:
				try:
					files=files+t[j][0][i-start_id[j]]+" " 
				except IndexError:
					files=files+"empty/empty.png "
					
		logger.debug("[%d/%d] %s" % (i+1,run_len,files))
		
		# now montage them together
		comm="/usr/local/bin/montage %s -tile 6x2 -geometry 400x300-80+3 tiled_%05d.png" % (files,i)
		logger.debug(comm)
		os.system(comm)