%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% rescale and shift JHU TCF data to experimental coordinate system
% experimental coordinate system is based on a volume centred on z=0

%% input parameters
src_dir			= 'L:/array/tcf/';
src_pat			= '%s/dns/z%03d_t%03d.mat';	% pattern of initial conditions file
dst_pat			= '%s/rescaled/z%03d_t%03d.mat';
dst_dir			= src_dir;% [src_dir '/rescaled/'];

% scale for database to reconstruction coordinates
dns_length_mm	= 300 / 2;	% channel height = 300mm
% match viscosity to water, i.e. 1mm^2/s
nu_dns			= 5E-5;		% dns viscosity
nu_mms			= 1;
dns_time_s		= nu_mms / nu_dns / dns_length_mm^2;

n_z				= 4;
n_t				= 100;

%% iterate over timesteps and slab positions
for iz = 1 : n_z
	for it = 1 : n_t
		%% load
		src_name	= sprintf(src_pat, src_dir, iz, it);
		dst_name	= sprintf(dst_pat, dst_dir, iz, it);
		if exist(dst_name, 'file') ~= 0
			warning('destination file exists, cannot overwrite');
			continue;
		end
		
		t_start		= tic;
		fs			= load(src_name);
		fprintf('[z%03d, t%03d] %s\n', iz, it, src_name);
		fprintf('   z = %0.3f - %0.3f, t = %0.3f\n', fs.z_lim, fs.t);
		
		if ~isfield(fs, 'u')
			fprintf('   missing velocity, skipping\n');
			continue;
		end
		
		%% rescale
		fs.x_lim	= fs.x_lim * dns_length_mm;
		fs.y_lim	= fs.y_lim * dns_length_mm;
		z_offset_mm	= -mean(fs.z_lim) * dns_length_mm;
		fs.z_lim	= fs.z_lim * dns_length_mm + z_offset_mm;
		re_offset	= [0;0;z_offset_mm];
		fs.x		= fs.x * dns_length_mm + re_offset;
		fs.u		= fs.u * dns_length_mm / dns_time_s;
		
		%% save
		dst_name	= sprintf(dst_pat, dst_dir, iz, it);
		fprintf('   save %s\n', dst_name);
		if exist(dst_name, 'file') ~= 0
			warning('file exists, cannot overwrite');
		else
			save(dst_name, '-struct', 'fs');
		end
	end
end