%**************************************************************************
%This file writes a schematic of the block configuration to the data file.
%
%Input:
%dat_file, data file for results
%param, parameter structure
%
%Zachary Wolf
%8/28/10

function ppm_und_dat_block_config_epu(dat_file, param)

%Get undulator parameters
und_period = param.und_period;
und_q1_z_shift = param.und_q1_z_shift;
und_q2_z_shift = param.und_q2_z_shift;
und_q3_z_shift = param.und_q3_z_shift;
und_q4_z_shift = param.und_q4_z_shift;
end_pm_num_z_pos = param.end_pm_num_z_pos;
ctr_pm_num_z_pos = param.ctr_pm_num_z_pos;
end_pm1_dir = param.end_pm1_dir;

%Determine the number of block z positions for the schematic
%Show ends plus two periods plus symmetrizing blocks
num_z_pos = end_pm_num_z_pos + 2 * 4 + mod(ctr_pm_num_z_pos, 4) + end_pm_num_z_pos;

%Parameter for display
num_char_per_mag = 4;

%Determine the block magnetization directions
%Convention: up = 1, right = 2, down = 3, left = 4
%Once the first q1 block is specified, the rest are determined
m_dir_q1 = zeros(1, num_z_pos);
m_dir_q2 = zeros(1, num_z_pos);
m_dir_q3 = zeros(1, num_z_pos);
m_dir_q4 = zeros(1, num_z_pos);
m_dir_q1(1) = end_pm1_dir;
for ii = 2 : num_z_pos
    m_dir_q1(ii) = m_dir_q1(ii - 1) + 1;
    if m_dir_q1(ii) == 5, m_dir_q1(ii) = 1; end;
end
if m_dir_q1(1) == 1, m_dir_q4(1) = 1; end;
if m_dir_q1(1) == 2, m_dir_q4(1) = 4; end;
if m_dir_q1(1) == 3, m_dir_q4(1) = 3; end;
if m_dir_q1(1) == 4, m_dir_q4(1) = 2; end;
for ii = 2 : num_z_pos
    m_dir_q4(ii) = m_dir_q4(ii - 1) - 1;
    if m_dir_q4(ii) == 0, m_dir_q4(ii) = 4; end;
end
m_dir_q2 = m_dir_q1;
m_dir_q3 = m_dir_q4;

%Fill strings with the magnetization directions for Q1
if m_dir_q1(1) == 1, str_dir_q1 = '. ^ '; end;
if m_dir_q1(1) == 2, str_dir_q1 = '. > '; end;
if m_dir_q1(1) == 3, str_dir_q1 = '. v '; end;
if m_dir_q1(1) == 4, str_dir_q1 = '. < '; end;
for ii = 2 : num_z_pos
    if m_dir_q1(ii) == 1, str_dir_q1 = [str_dir_q1, '. ^ ']; end;
    if m_dir_q1(ii) == 2, str_dir_q1 = [str_dir_q1, '. > ']; end;
    if m_dir_q1(ii) == 3, str_dir_q1 = [str_dir_q1, '. v ']; end;
    if m_dir_q1(ii) == 4, str_dir_q1 = [str_dir_q1, '. < ']; end;
end
str_dir_q1 = [str_dir_q1, '.'];

%Fill strings with the magnetization directions for Q2
if m_dir_q2(1) == 1, str_dir_q2 = '. ^ '; end;
if m_dir_q2(1) == 2, str_dir_q2 = '. > '; end;
if m_dir_q2(1) == 3, str_dir_q2 = '. v '; end;
if m_dir_q2(1) == 4, str_dir_q2 = '. < '; end;
for ii = 2 : num_z_pos
    if m_dir_q2(ii) == 1, str_dir_q2 = [str_dir_q2, '. ^ ']; end;
    if m_dir_q2(ii) == 2, str_dir_q2 = [str_dir_q2, '. > ']; end;
    if m_dir_q2(ii) == 3, str_dir_q2 = [str_dir_q2, '. v ']; end;
    if m_dir_q2(ii) == 4, str_dir_q2 = [str_dir_q2, '. < ']; end;
end
str_dir_q2 = [str_dir_q2, '.'];

%Fill strings with the magnetization directions for Q3
if m_dir_q3(1) == 1, str_dir_q3 = '. ^ '; end;
if m_dir_q3(1) == 2, str_dir_q3 = '. > '; end;
if m_dir_q3(1) == 3, str_dir_q3 = '. v '; end;
if m_dir_q3(1) == 4, str_dir_q3 = '. < '; end;
for ii = 2 : num_z_pos
    if m_dir_q3(ii) == 1, str_dir_q3 = [str_dir_q3, '. ^ ']; end;
    if m_dir_q3(ii) == 2, str_dir_q3 = [str_dir_q3, '. > ']; end;
    if m_dir_q3(ii) == 3, str_dir_q3 = [str_dir_q3, '. v ']; end;
    if m_dir_q3(ii) == 4, str_dir_q3 = [str_dir_q3, '. < ']; end;
end
str_dir_q3 = [str_dir_q3, '.'];

%Fill strings with the magnetization directions for Q4
if m_dir_q4(1) == 1, str_dir_q4 = '. ^ '; end;
if m_dir_q4(1) == 2, str_dir_q4 = '. > '; end;
if m_dir_q4(1) == 3, str_dir_q4 = '. v '; end;
if m_dir_q4(1) == 4, str_dir_q4 = '. < '; end;
for ii = 2 : num_z_pos
    if m_dir_q4(ii) == 1, str_dir_q4 = [str_dir_q4, '. ^ ']; end;
    if m_dir_q4(ii) == 2, str_dir_q4 = [str_dir_q4, '. > ']; end;
    if m_dir_q4(ii) == 3, str_dir_q4 = [str_dir_q4, '. v ']; end;
    if m_dir_q4(ii) == 4, str_dir_q4 = [str_dir_q4, '. < ']; end;
end
str_dir_q4 = [str_dir_q4, '.'];

%Shift parameters
num_char_per_period = 4 * num_char_per_mag;
num_char_shift_q1 = round(num_char_per_period * und_q1_z_shift / und_period);
num_char_shift_q2 = round(num_char_per_period * und_q2_z_shift / und_period);
num_char_shift_q3 = round(num_char_per_period * und_q3_z_shift / und_period);
num_char_shift_q4 = round(num_char_per_period * und_q4_z_shift / und_period);

%Make shift strings
str_shift_q1_enter = blanks(num_char_per_period + num_char_shift_q1);
str_shift_q1_exit = blanks(num_char_per_period - num_char_shift_q1);
str_shift_q2_enter = blanks(num_char_per_period + num_char_shift_q2);
str_shift_q2_exit = blanks(num_char_per_period - num_char_shift_q2);
str_shift_q3_enter = blanks(num_char_per_period + num_char_shift_q3);
str_shift_q3_exit = blanks(num_char_per_period - num_char_shift_q3);
str_shift_q4_enter = blanks(num_char_per_period + num_char_shift_q4);
str_shift_q4_exit = blanks(num_char_per_period - num_char_shift_q4);

%Apply shift strings
str_dir_q1 = ['Q1:', str_shift_q1_enter, str_dir_q1, str_shift_q1_exit];
str_dir_q2 = ['Q2:', str_shift_q2_enter, str_dir_q2, str_shift_q2_exit];
str_dir_q3 = ['Q3:', str_shift_q3_enter, str_dir_q3, str_shift_q3_exit];
str_dir_q4 = ['Q4:', str_shift_q4_enter, str_dir_q4, str_shift_q4_exit];

%Try different looks
str_dir_q1 = strrep(str_dir_q1, '.', '|');
str_dir_q2 = strrep(str_dir_q2, '.', '|');
str_dir_q3 = strrep(str_dir_q3, '.', '|');
str_dir_q4 = strrep(str_dir_q4, '.', '|');

%Write the block information to the data file
fid = fopen(dat_file, 'a');
fprintf(fid, '\r\n');
fprintf(fid, 'Magnet Block Configuration:\r\n');
fprintf(fid, '%s\r\n', str_dir_q1);
fprintf(fid, '%s\r\n', str_dir_q2);
fprintf(fid, '%s\r\n', str_dir_q3);
fprintf(fid, '%s\r\n', str_dir_q4);
fclose(fid);

%Done
end
