function [imgary, fail] = loaddarkimages(indir, infilepattern, frame_indices)
	% imgary = loaddarkimages(indir, pattern, frame_indices)
	%
	% loads dark images, contained in the directory indir, with a regularised
	% filename described by pattern
	% 
	% pattern must be a string prepared for sprintf like
	% frame-%02d.tif
	% with a space for the frame index (integer)
	%
	% the images are loaded into the array imgary. they must all be the same
	% size in pixels. imgary is indexed imgary(i, j, k), with
	% increasing i corresponding to increasing x pixel
	% increasing j corresponding to increasing y pixel
	%            k corresponding to each image specified by frame_indices
	%
	
	% ensure globals are set
	global		DO_IMAGE_FLIPUPDOWN;
	global		DO_IMAGE_FLIPLEFTRIGHT;	
	
	% load the first image to find the size
	infilename	= sprintf(infilepattern, frame_indices(1));
	infilename  = sprintf('%s/%s', indir, infilename);
	[~,~,ext]	= fileparts(infilename);
	isRaw12		= strcmpi(ext, '.raw12');
	if isRaw12
		Ni			= 1024;
		Nj			= 1024;
	else	
		img		= imread(infilename);
		Ni			= size(img, 2);	% img is arranged in a funny way
		Nj			= size(img, 1);
	end
	Nk				= length(frame_indices);
	
	% allocate and populate imgary with images
	imgary		= zeros(Ni, Nj, Nk, 'single');
	for k = 1:Nk
		frame_index		= frame_indices(k);
		infilename		= sprintf(infilepattern, frame_index);
		infilename		= sprintf('%s/%s', indir, infilename);
		
		if isRaw12
			img			= imread_raw12(infilename, Ni, Nj);
			img			= fliplr(img);
		else
			img			= imread(infilename);
			img			= fliplr(img');	% this is the default manipulation which makes (1,1) correspond to (-Lx/2,-Ly/2)
		end
		
		if DO_IMAGE_FLIPUPDOWN
			img		= fliplr(img);		% counter-intuitive, but this is the right operation
		end
		if DO_IMAGE_FLIPLEFTRIGHT
			img		= flipud(img);
		end
		
		imgary(:,:,k)	= single(img);
	end
	
	% implement error checking later
	fail			= false;
end
