function B = merge_beamlets(num_batches, pat_dir) % num_batches = 4; B = []; for k = 1:num_batches batch_doses = read_ryan_beamlets([pat_dir, '\batch_dose' num2str(k-1) '.bin']); B = [B batch_doses]; end for k = 1:numel(B) B{k}.num = k-1; end write_ryan_beamlets([pat_dir '\batch_dose_unshifted.bin'],B); % apply the shift (0.5 vox up and left) to make dose match geometry if true f = waitbar(0, 'Correcting beamlet shift'); for beam_i = 1:numel(B) new_dose_list = B{beam_i}; waitbar(beam_i/numel(B),f, ['Correcting beamlet shift: ' num2str(beam_i)]) tabula = zeros(B{1, beam_i}.y_count, B{1, beam_i}.x_count, B{1, beam_i}.z_count); tabula(B{1, beam_i}.non_zero_indices) = B{1, beam_i}.non_zero_values; shifted_img = imtranslate(tabula, [-0.5,-0.5,-0.5]); nonzero_idx = find(shifted_img>0.005*max(shifted_img(:))); B{beam_i}.non_zero_indices = nonzero_idx; B{beam_i}.non_zero_values = shifted_img(nonzero_idx); end close(f) end write_ryan_beamlets([pat_dir '\batch_dose.bin'],B); write_ryan_beamlets([pat_dir '\batch_dose_backup.bin'],B);