def make_pngs(clist):
	'''
	Get all the images from yesterday and make pngs 
	for the daily montage. PNGs will be stored in 
	
	/ngts/dasXX/movie/ 
	
	These will be removed each day once the movie is made
	'''
	if me=='ops':
		# 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)
			
		# get the action ids for each camera (and dome 899)
		for row in cur:
			if row[2] != 'stow':
				cams[row[1]].append("action%s_%s" % (row[0],row[2]))
		
	if me=='James':
		for i in cams:
			cams[i].append('action100000_focusSweep')
	
	# remove the roof entry, not needed
	del cams[899]
	
	# now we have the action ids go through and create the images for each 
	for i in cams:
		# only do those listed on command line
		if str(i) in clist:
			if len(cams[i]) > 0 and das[i] != None:
				if os.path.exists(movie_dir) == False:
					os.mkdir(movie_dir)	
				logger.info(movie_dir)
				for j in cams[i]:
					logger.info("%s%s/%s/*.fits" % (top_dir,das[i],j))
					t=sorted(g.glob('%s%s/%s/*.fits' % (top_dir,das[i],j)))
					
					camera_movie_dir=movie_dir+das[i]
					create_movie(t,images_directory=camera_movie_dir,include_increment=False,
						clobber_images_directory=False,resize_factor=4)
			else:
				logger.warn('No images for %d' % (i))
		else:
			continue
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)
	
	# get the action ids for each camera (and dome 899)
	for row in cur:
		if row[3] != 'stow':
			cams[row[1]].append("action%s_%s" % (row[2],row[3]))
	
	if len(cams[cam]) > 0 and cam != 899:
		# go into the last action directory
		if das[cam] != None:
			os.chdir("%s/%s" % (das[cam],cams[cam][-1]))
			logger.info("Moving to %s/%s" % (das[cam],cams[cam][-1]))
			# get the last image
			t=sorted(g.glob('*.fits'))
			
			if len(t)>0:
			 	pngfile="%s.png" % (t[-1])
				logger.info("PNG file to make is %s.png" % (t[-1]))
			 	if pngfile not in os.listdir('%s/last_imgs/%s/' % (cron_dir,cam)):
					create_movie([t[-1]],images_directory='%s/last_imgs/%s' % (cron_dir,cam),
						no_time_series=True,include_increment=False,clobber_images_directory=False,resize_factor=4)
				
					here=os.getcwd()
					os.chdir("%s/last_imgs/%s" % (cron_dir,cam))
					logger.info("Moving to %s/last_imgs/%s" % (cron_dir,cam))
					
					try:
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)