%**************************************************************************
%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
%11/16/10

function ppm_und_dat_block_config_varphase(dat_file, param)

%Get undulator parameters
und_period = param.und_period;
und_up_z_shift = param.und_up_z_shift;
und_lo_z_shift = param.und_lo_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 upper block is specified, the rest are determined
m_dir_up = zeros(1, num_z_pos);
m_dir_lo = zeros(1, num_z_pos);
m_dir_up(1) = end_pm1_dir;
for ii = 2 : num_z_pos
    m_dir_up(ii) = m_dir_up(ii - 1) + 1;
    if m_dir_up(ii) == 5, m_dir_up(ii) = 1; end;
end
if m_dir_up(1) == 1, m_dir_lo(1) = 1; end;
if m_dir_up(1) == 2, m_dir_lo(1) = 4; end;
if m_dir_up(1) == 3, m_dir_lo(1) = 3; end;
if m_dir_up(1) == 4, m_dir_lo(1) = 2; end;
for ii = 2 : num_z_pos
    m_dir_lo(ii) = m_dir_lo(ii - 1) - 1;
    if m_dir_lo(ii) == 0, m_dir_lo(ii) = 4; end;
end

%Fill strings with the magnetization directions for the upper blocks
if m_dir_up(1) == 1, str_dir_up = '. ^ '; end;
if m_dir_up(1) == 2, str_dir_up = '. > '; end;
if m_dir_up(1) == 3, str_dir_up = '. v '; end;
if m_dir_up(1) == 4, str_dir_up = '. < '; end;
for ii = 2 : num_z_pos
    if m_dir_up(ii) == 1, str_dir_up = [str_dir_up, '. ^ ']; end;
    if m_dir_up(ii) == 2, str_dir_up = [str_dir_up, '. > ']; end;
    if m_dir_up(ii) == 3, str_dir_up = [str_dir_up, '. v ']; end;
    if m_dir_up(ii) == 4, str_dir_up = [str_dir_up, '. < ']; end;
end
str_dir_up = [str_dir_up, '.'];

%Fill strings with the magnetization directions for the lower blocks
if m_dir_lo(1) == 1, str_dir_lo = '. ^ '; end;
if m_dir_lo(1) == 2, str_dir_lo = '. > '; end;
if m_dir_lo(1) == 3, str_dir_lo = '. v '; end;
if m_dir_lo(1) == 4, str_dir_lo = '. < '; end;
for ii = 2 : num_z_pos
    if m_dir_lo(ii) == 1, str_dir_lo = [str_dir_lo, '. ^ ']; end;
    if m_dir_lo(ii) == 2, str_dir_lo = [str_dir_lo, '. > ']; end;
    if m_dir_lo(ii) == 3, str_dir_lo = [str_dir_lo, '. v ']; end;
    if m_dir_lo(ii) == 4, str_dir_lo = [str_dir_lo, '. < ']; end;
end
str_dir_lo = [str_dir_lo, '.'];

%Shift parameters
num_char_per_period = 4 * num_char_per_mag;
num_char_shift_up = round(num_char_per_period * und_up_z_shift / und_period);
num_char_shift_lo = round(num_char_per_period * und_lo_z_shift / und_period);

%Make shift strings
str_shift_up_enter = blanks(num_char_per_period + num_char_shift_up);
str_shift_up_exit = blanks(num_char_per_period - num_char_shift_up);
str_shift_lo_enter = blanks(num_char_per_period + num_char_shift_lo);
str_shift_lo_exit = blanks(num_char_per_period - num_char_shift_lo);

%Apply shift strings
str_dir_up = ['Q1:', str_shift_up_enter, str_dir_up, str_shift_up_exit];
str_dir_lo = ['Q4:', str_shift_lo_enter, str_dir_lo, str_shift_lo_exit];

%Try different looks
str_dir_up = strrep(str_dir_up, '.', '|');
str_dir_lo = strrep(str_dir_lo, '.', '|');

%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_up);
fprintf(fid, '%s\r\n', str_dir_lo);
fclose(fid);

%Done
end
