function ring_input = define_ring()

%USEAGE: ring_input = define_ring();
%
%INPUT ARGUMENTS: none;
%
% This function prompts the user for arguments to define a ring parameter
% file.  It packages the information into the structures necessary to call
% the MatLab function ring_gen.m.
%
% Robert Harrison, 2011-03-21

screen = 1;

% Request information for the file header (text)
fprintf(screen, '\n\nCreating a detector ring parameter file for SimSET.\n')
fprintf(screen, '\n')

% Describe this program
fprintf(screen, 'This program fills one ring with block detectors,\n')
fprintf(screen, 'prompting the user for the block filenames and positioning\n')
fprintf(screen, 'information for each series of blocks.\n')
fprintf(screen, 'See the website for more info.\n')
fprintf(screen, '\n')

ring_data.filename = input('Please enter the file name for the ring parameters: ', 's');

fprintf(screen, 'Please enter the following information for the header.\n')
header.author = input('Author name: ', 's');

comment_num = 1;
fprintf(screen, 'Enter comment -\n')
fprintf(screen, 'terminate with 2 consecutive carriage returns:\n')
while (comment_num > 0)
    comment{comment_num} = input('', 's');
    if ( strcmp( comment{comment_num},'' ) )
        comment_num = 0;
    else
        comment_num = comment_num + 1;
    end
end

header.comments = comment;

% Request the bounding values of the ring.
fprintf(screen, '\n')
fprintf(screen, 'Define the outer bounds of the ring:  the minimum and maximum\n')
fprintf(screen, 'axial values in the ring; the radius of a cylinder separating\n')
fprintf(screen, 'the detectors from the collimator or object (the inner radius of the the\n')
fprintf(screen, 'detector system); and the radius of cylinder that lies entirely\n')
fprintf(screen, 'outside the detector system (the outer radius, separating the\n')
fprintf(screen, 'tomograph from the rest of the universe).\n')
fprintf(screen, '\n')

fprintf(screen, '\nRadial extent of ring.\n')
ring_data.r_min = input('Radius of a cylinder separating the detectors from the collimator (cm): ');
ring_data.r_max = input('Radius of a cylinder separating the detectors from the universe (cm): ');

fprintf(screen, '\nAxial extent of ring: this is not the final axial position of the ring.\n')
fprintf(screen, 'Multiple copies of the ring can be given different axial shifts in\n')
fprintf(screen, 'the detector parameter file.\n')
ring_data.z_min = input('Minimum axial extent of the ring (cm): ');
ring_data.z_max = input('Maximum axial extent of the ring (cm): ');
fprintf(screen, '\n')
fprintf(screen, '\n')

% Request the information defining the blocks in the ring.
fprintf(screen, '\n')
fprintf(screen, 'Now specify the blocks in the ring.  The program will prompt\n')
fprintf(screen, 'you for a block parameter file, then ask you how many of\n')
fprintf(screen, 'this block type are in the ring and how to place them.  This\n')
fprintf(screen, 'will be repeated until you have no more blocks you wish to\n')
fprintf(screen, 'include in the ring.\n')

% Get and position the blocks
block_type = 1;
while (block_type > 0)
    fprintf(screen, '\n\nEnter the data for block type %d\n', block_type)
    blocks(block_type).filename = input('    Enter file name the block parameters: ', 's');
    blocks(block_type).filename = sprintf('%s%s%s', '"', blocks(block_type).filename, '"');
    blocks(block_type).r_min = input('    Radius at which to place the blocks (cm): ');
    blocks(block_type).z_pos = input('    Axial position of blocks relative to the ring (cm): ');    
    blocks(block_type).count = input('    How many of this block type? ');
    
    % distribute blocks angularly
    blocks(block_type).azimuth(1) = input('\n    Angular position (azimuth) for first block (degrees): ');
    resp = input('    Are the rest of this block type evenly distributed around the ring? (y/n) [y]: ', 's');
    if ( not(isempty(resp) || strcmp( resp,'y' ) || strcmp( resp,'Y') ) )
        for angle_num = 2:blocks(block_type).count
            blocks(block_type).azimuth(angle_num) = input('    Angular position for next block (degrees): ');
        end
    end
    
    % tilt the detectors
    fprintf(screen, '\n    The blocks can be rotated in the transaxial plane. A rotation\n')
    fprintf(screen, '    of 0 degrees will position the block with its front face tangent\n')
    fprintf(screen, '    to the cylinder at the radius given above.  A poistive value\n')
    fprintf(screen, '    will rotate the block counterclockwise, a negative value clockwise.\n')
    blocks(block_type).tilt(1) = input('\n    Angular tilt for first block (degrees): ');
    resp = input('    Do the rest of this block type have this same tilt? (y/n) [y]: ', 's');
    if ( not(isempty(resp) || strcmp( resp,'y' ) || strcmp( resp,'Y') ) )
        for tilt_num = 2:blocks(block_type).count
            blocks(block_type).tilt(tilt_num) = input('    Tilt for next block (degrees): ');
        end
    end
    
    resp = input('\n\n    Add more blocks to the ring? (y/n) [y]: ', 's');
    if ( isempty(resp) || strcmp( resp,'y' ) || strcmp( resp,'Y') )
        block_type = block_type + 1;
    else
        block_type = 0;
    end

end

ring_input = { header , ring_data, blocks };

ring_out = ring_gen(ring_data, blocks, header);

fprintf(screen, '\n\nRing parameter file generation complete.\n')
