%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% obtain velocity of tracers for tracking through JHU turbulence database
addpath([pwd '/../../turbmat']);

%% dimensional parameters of TCF dataset
delta_nu	= 1.0006E-3;		% plus unit
Ub			= 0.99994;			% bulk velocity
Uc			= 1.1312;			% centerline velocity
u_tau		= 4.9968E-2;		% friction velocity
nu			= 5E-5;				% kinematic viscosity
T_sim		= [0 25.9935];		% simulation duration
t_sim_step	= 0.0013;			% simulation timestep
t_save_step	= 0.0065;			% timestep for snapshots
h			= 1;				% half height
load('L:/array/tcf/dns/grid.mat');

%% input parameters
dset_dir	= 'L:/array/tcf/dns/';
dset_pat	= '%s/z%03d_t%03d.mat';	% pattern of initial conditions file

n_t			= 100;
n_z			= 32;
n_query		= 1e5;				% limit queries to this size

%% iterate over qureying data from DB
for iz = 1 : n_z
	for it = 1 : n_t
		%% load
		fname		= sprintf(dset_pat, dset_dir, iz, it);
		t_start		= tic;
		fprintf('[z%03d, t%03d]\n', iz, it);
		fprintf('   %s\n', fname);
		fs			= load(fname);
		%% check if already done
		if isfield(fs, 'u')
			fprintf('already done\n');
			continue;
		end
		%% query database
		try
			fprintf('   query %d points...\n', fs.n_part);
			fs.u	= zeros(3, fs.n_part);
			for i0 = 1 : n_query : fs.n_part
				%% define range to query
				rng			= i0 : min(i0 + n_query - 1, fs.n_part);
				%% query
				fprintf('   %d - %d...\n', rng([1 end]));
				fs.u(:,rng)	= getVelocity(fs.auth_token, fs.dset_token, fs.t, ...
								fs.s_interp, fs.t_interp, length(rng), fs.x(:,rng));
			end

			fprintf('   save...\n');
			save(fname, '-struct', 'fs');
			fprintf('   done (%0.3f s)\n', toc(t_start));
		catch
			warning('caught an error querying database');
		end
	end
end


