% need to include the dataset_positions thing here.  All of that stuff.

ParametersRun500
dataset_positions = [{2,0}; {2,1}; {2,2}; {2,3}; {2,4}; {4,0}; {4,2}; {4,3}; {4,4}; {4,5}; {5,0}; {5,1}; {5,2}; {5,3}; {6,0}; {6,1}; {6,2}; {6,3}; {6,4}; {6,5}; {7,0}; {7,1}; {7,2}; {7,3}; {7,4}; {7,5}; {10,0}; {10,1}; {10,2}; {10,3}; {10,5}];



all_rotations = zeros(1,size(dataset_positions,1));
all_rotations_acos = zeros(1,size(dataset_positions,1));

cross_rotations = zeros(1,size(dataset_positions,1));
cross_translations = zeros(size(dataset_positions,1),2);

running_asin_angles = zeros(1,size(dataset_positions,1));

moving_run_number = 500;

first_target_stack =20;
last_target_stack = 89;

composed_cpd_conjugates = zeros(size(dataset_positions,1),4,4);

counter =0;

clear extra_angles
clear extra_distances

for i = 1:size(dataset_positions,1)
% for i = 1:1

    dataset = dataset_positions{i,1};
    position = dataset_positions{i,2};
    first_stack = calculated_stacks{dataset,position+1}(1);
    last_stack = calculated_stacks{dataset,position+1}(2);
    time_offset = time_offsets{dataset,position+1};
    % this might need to be replaced with calculated stac
    
    moving_start_time = moving_start_times{dataset,position+1};
    moving_moments_time = moments_times{dataset,position+1};
    
    % stacks are the time point bounds held in calculated_params).
    
    if (dataset~= 2)&&(dataset~=4)
        moving_dataset_directory =  ['L:\seg2 5-31\Set ' num2str(dataset) '\Stacks\Position ' num2str(position) '\resized images'];
    elseif (dataset==2)&&(position == 4)
        moving_dataset_directory = 'L:\seg2 5-31\2-4 (cp from E drive 5-30)\Position 4\resized images\';
        moving_start_time = '15-Nov-2015 12.12.47';
    else
        moving_dataset_directory =  ['K:\Laptop F drive copy\new reg 4-8 classifier 6-13-15\' num2str(dataset) '-' num2str(position)];
    end
    
    cd(moving_dataset_directory)
    cd(['Run ' num2str(moving_run_number) '/SelfTransforms']);
    
    if (dataset==2)&&(position==4)
    load(['composed cpd conjugate_Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(first_stack) ' al Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(last_stack) '_r' num2str(moving_run_number) '. ' moving_start_time '.mat']);
    else
    load(['composed cpd conjugate_Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(last_stack) ' al Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(first_stack) '_r' num2str(moving_run_number) '. ' moving_start_time '.mat']);
    end
    
    rotation = composed_cpd_conjugate.';
    rotation = rotation(2:3,2:3);
    rotation_angle = -asin(rotation(2,1));
    rotation_angle_acos = acos(rotation(1,1));
    
    if (dataset==2)&&(position==4)
        rotation_angle = -rotation_angle;
        rotation_angle_acos = -rotation_angle_acos;
        i
    end
    
    all_rotations(i) = rotation_angle;
    all_rotations_acos(i) = rotation_angle_acos;
    
    composed_cpd_conjugates(i,:,:) = composed_cpd_conjugate;
    
      for this_stack = first_stack:last_stack
          counter = counter+1;
          if (dataset==2)&&(position==4)
          this_stack = this_stack -1;
          'make sure you know that the cross transform angles are being incorrectly calculated; giving 0 for 2,4; average will be wrong; delete 2,4 from list of datasets to get right answer'
          else
          cd('../CrossTransforms');
          
          if max(first_target_stack,this_stack+time_offset) > last_target_stack
              correct_target_stack = last_target_stack;
          else
              correct_target_stack = max(first_target_stack,this_stack+time_offset);
          end
          
          load(['complete cross transform correctedconvtest_Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(this_stack) ' al Btc_Ds2_Pos4_Co0_St' num2str(correct_target_stack) '_r500. ' moving_start_time '.mat']);
          load(['complete cross transform_Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(this_stack) ' al Btc_Ds2_Pos4_Co0_St' num2str(89) '_rm500_rt500. ' moving_start_time '.mat']);
          end
          
          tmp = inv(complete_cross_transform)*complete_cross_transform_corrected;
          extra_angle = acos((trace(tmp(1:3,1:3))-1)/2);
          test1 = ([176 126 ((optreg.num_slices/2)+.5) 1]*complete_cross_transform);
          test2 = ([176 126 ((optreg.num_slices/2)+.5) 1]*complete_cross_transform_corrected);
          extra_vector = test1 - test2;
          extra_vector_corr = extra_vector.*([1 1 3.077 1]);
          extra_distance = norm(extra_vector_corr(1:3));
          
          
          extra_angles(counter) = extra_angle;
          extra_distances(counter) = extra_distance;
      end
          
%     for this_stack = first_stack+1:last_stack
%         load(['cpd transform conjugate_Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(this_stack) ' al Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(this_stack-1) '_r500. ' moving_start_time '.mat']);
%         acos_angle = acos(cpd_transform_conjugate(2,2));
%         asin_angle = asin(cpd_transform_conjugate(3,2));
%         running_asin_angle = asin_angle + running_asin_angle;
%     end
%     
%     running_asin_angles(i) = running_asin_angle;

    if (dataset==2)&&(position ==4)
        % do nothign
    else
    cd('../CrossTransforms');
    this_time_offset = time_offsets{dataset,position + 1};
    target_for_first_stack = first_stack + this_time_offset;
    load(['cpd transform conjugate_Btc_Ds' num2str(dataset) '_Pos' num2str(position) '_Co0_St' num2str(first_stack) ' al Btc_Ds2_Pos4_Co0_St' num2str(target_for_first_stack) '_r500. ' moving_start_time '.mat']);
    cross_rotations(i) = acos(cpd_transform_conjugate(2,2));
    cross_translations(i,:) = cpd_transform_conjugate(4,2:3);
    end
end

all_rotations_corrected = all_rotations;

for i = 1:length(all_rotations)
    if all_rotations_acos(i)>pi/2
        if all_rotations_corrected(i) > 0
            all_rotations_corrected(i) = pi-all_rotations(i);
        else
            all_rotations_corrected(i) = -pi - all_rotations(i);
        end
    end
end
            

x = 1:length(all_rotations_corrected);

figure; scatter(x,all_rotations_corrected*180/pi);

fprintf(['n = ' num2str(length(all_rotations_corrected))])
