%SHMOVIE1BY1	Show 2D array movie of data as it evolves trough time.
%	shMovie( filename, resolution [20], frameStep [10, that is 1 frame/ms] )
%hit a key/press mouse button to move from one frame to the next

function shMovie( data, resolution, frameStep )


%--- Parameters ---

if( nargin <= 2 ), frameStep = 3;, end
if( nargin == 1 ), resolution= 20;, end


dataCSD = data;
aviobj=avifile('medmovie25.avi','fps',5);

%--- Adjust color scale ---

dataMax =  max( max( data(:, 1:64) ) );
dataMin =  min( min( data(:, 1:64) ) );
saturation = 0.3; % values above this fraction of the color scale are saturated
colorMax =  saturation * max( abs(dataMax), abs(dataMin) );
disp( ['[shMovieV] Automatically adjusted max. color scale value: ', num2str( colorMax, 3 )] )

maxV = colorMax;

%maxV = 0.025; % hand picked color limit


%--- Seed graph ---

steps = 8 * resolution;
[XI, YI] = meshgrid(1:(8-1)/(steps-1):8,1:(8-1)/(steps-1):8);
[X, Y] = meshgrid(1:8,1:8);

figure(1)
subplot(111)
imageSize = [size( XI, 1 ) + 7, size( XI, 2 )];
h1 = image( rand( imageSize(1), imageSize(2) ) );
hTitle1 = title('*');
set( hTitle1, 'EraseMode', 'xor' )
set( h1, 'EraseMode', 'none' )
colormap( [obMap; [1 1 1]] )
%colormap( [ycMap; [1 1 1]] )
brighten( 0.3 )
axis('image')
axis('off')


%--- Animation loop ---

gridX = (meshgrid( 1:8 ) - 1) * (8 / 7) / 8 * (resolution*8 - 1) + 1;
gridX = round(gridX);
gridY = gridX';

%sliceX = [1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9 10 10 10 11 11 12 12 13 13 14 14 15 15 16 17 17 18 19 19 20];
%sliceY = [115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71];

%sliceX2 = [21 22 23 24 25 26 27 28 29 30 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49];
%sliceY2 = [70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 44 43 42];

%sliceX3 = [50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80];
%sliceY3 = [42 41 40 40 39 38 38 37 36 36 35 34 34 33 32 32 31 31 30 30 29 29 28 28 27 27 27 26 26 26 26];

%sliceX4 = [81 82 83 84 85 86 87 88 89 90 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109];
%sliceY4 = [25 25 25 24 24 24 24 24 24 24 24 24 24 24 24 24 25 25 25 25  25  25  25  25  25  25  26  26  26  26];

%sliceX5 = [110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139];
%sliceY5 = [ 27  27  27  27  27  28  28  28  29  29  29  30  30  31  31  31  32  32  33  33  33  34  34  35  35  35  36  36  37  37]; 

%sliceX6 = [141 142 143 144 145 146 147 147 148 149 150 150 151 152 152 153 153 154 155 155 156 156 156 157 157 157 158 158 158 159];
%sliceY6 = [ 38  39  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66];

%sliceX7 = [159 159 159 159 159 158 158 158 158 157 157 157 157 156 156 156 155 155 154 154 154 153 153 152 152 152 151 151 150 150];
%sliceY7 = [ 67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96];

%sliceX8 = [150 151 152 152 153 153 154 154 155 155 156 156 156 156 157 157 157 158 158 158 159 159];
%sliceY8 = [110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131]; 

%sliceX9 = [ 92  92  92  92  91  91  91  91  91  91  91  91  91  92  92  92  92  92  92  92  92  93  93  93  93  94  94  94  94  94  95  95  95  96  96  96  96  96  96  97  97  97  98  98  99  99 100 100 100 100];
%sliceY9 = [115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164];
%for frame = 1:frameStep:size(data, 1)
for frame = 168:frameStep:168
   
   
    %for n=1:1:25000
    %    n=n^2;
    %end
   %--- interpolate array ---
   
   Z1 =  toMatrix( data(frame, 1:64), 8 );
   ZI1 = interp2( X, Y, Z1, XI, YI, 'spline' );    
   
      
   %--- Update data in plot ---
   
   ZI1 = 255 * ( 0.5 * ZI1 / maxV + 0.5);
   %ZI1 = trunc( ZI1, 1, 255 );
   ZI1(ZI1>255)=255; ZI1(ZI1<1)=1;
   
   %--- Electrode positions ---
   
   ZI1(gridX(1:64), gridY(1:64)) = 256;
   
   %--- Slice layout --
   
   for n=1:1:45
       %ZI1(sliceX(n),sliceY(n))=256;
   end
   for n=1:1:30
       %ZI1(sliceX2(n),sliceY2(n))=256;
   end
   for n=1:1:31
       %ZI1(sliceX3(n),sliceY3(n))=256;
   end
   for n=1:1:30
       %ZI1(sliceX4(n),sliceY4(n))=256;
   end
   for n=1:1:30
       %ZI1(sliceX5(n),sliceY5(n))=256;
   end
   for n=1:1:30
       %ZI1(sliceX6(n),sliceY6(n))=256;
   end
   for n=1:1:30
       %ZI1(sliceX7(n),sliceY7(n))=256;
   end
   for n=1:1:22
       %ZI1(sliceX8(n),sliceY8(n))=256;
   end
   for n=1:1:50
       %ZI1(sliceX9(n),sliceY9(n))=256;
   end
   
   %ZI1(sliceX(1),sliceY(1))=256;
   %ZI1(sliceX(2),sliceY(2))=256;
   
   %--- Time stamp ---
   
   %ZI1 = [ZI1; timeImg( data(frame, 1), 130, 256, imageSize(2) )];
   
   
   %--- Display image ---
   
   set( h1, 'cdata', ZI1 )
   set( hTitle1, 'String', ['Time: ', num2str( data(frame, 1), '%04.1f' ), ' [ms]'] );
   drawnow
 
   w = waitforbuttonpress;
if w == 0
    disp('Button press')
else
    disp('Key press')
end
   
   %--- Save as movie ---
   
   frame = getframe(gca);
   aviobj = addframe(aviobj,frame);
   
end

aviobj = close(aviobj);

