function status = writeMRIfile( MRI_struct, MRI_path )

err_dlgname = 'Error writing MRI file';

% Prompt user to select filename:
if ~exist('MRI_path') || isempty(MRI_path)
    % Load pathname for MRI file used in last analysis, if available, otherwise create empty string:
    if exist('tmp_MRI_path.mat','file'), load('tmp_MRI_path'); end
    if ~exist('MRI_path','var') || ~isstr(MRI_path), MRI_path = ''; end
end
% Prompt user to select filename and location:
[MRI_filename, MRI_path]= uiputfile({'*.csv','CSV (Comma delimited)(*.csv)'},'Save MRI file', [MRI_path filesep]);
if any(MRI_filename~=0)
    MRI_full_filename = fullfile( MRI_path, MRI_filename);
else
    err = errordlg('File save cancelled by user.', err_dlgname);
    uiwait(err); status = -1; return;
end

fw = fopen( MRI_full_filename, 'wt');
if fw==-1
    err = errordlg('Unable to open specified file for writing: make sure that the file is not currently open.', err_dlgname);
    uiwait(err); status = -1; return;
end
% Save pathname to be used as default values in next analysis:
save('tmp_MRI_path','MRI_path');

fprintf( fw, '%s\n','*UNITS');
fprintf( fw, '%s\n','Length, Force, Wind speed');
fprintf( fw, '%s\n',[MRI_struct.length_units ',' MRI_struct.force_units ',' MRI_struct.ws_units ]);
fprintf( fw, '%s\n','*BUILDING_DIMENSIONS');
fprintf( fw, '%s\n','W0, L0, H0, R0');
fprintf( fw, '%g, ', MRI_struct.d0);
fprintf( fw, '\n');
fprintf( fw, '%s\n','*MODEL_DIMENSIONS');
fprintf( fw, '%s\n','W, L, H, R');
d = MRI_struct.d;
for i = 1:size(d,1)
    fprintf( fw, '%g,', d(i,:));
    fprintf( fw, '\n');
end
fprintf( fw, '%s\n','*TERRAIN');
fprintf( fw, '%s\n', MRI_struct.terrain);
if strcmp(MRI_struct.terrain,'Directional');
    fprintf( fw, '%s\n','N,NE,E,SE,S,SW,W,NW');
    fprintf( fw, '%g,', MRI_struct.z0 );
    fprintf( fw, '\n');
end
fprintf( fw, '%s\n','*MODEL_TERRAIN');
for i = 1:length(MRI_struct.model_terrain)
    fprintf( fw, '%s,', MRI_struct.model_terrain{i});
end
fprintf( fw, '\n');
fprintf( fw, '%s\n','*MILEPOST');
fprintf( fw, '%s\n', num2str(MRI_struct.milepost));
fprintf( fw, '%s\n','*FRAME_LOCATIONS');
fprintf( fw, '%s\n','Frame #, y-previous, y-current, y-next');
n_f = size(MRI_struct.frame_coords,1);
for i = 1:n_f
    fprintf( fw, '%g,%g,%g,%g\n', i, MRI_struct.frame_coords(i,:));
end
fprintf( fw, '%s\n','*RESPONSE_NAMES');
fprintf( fw, '%s\n','Number, Name, Units');
n_r = length(MRI_struct.resp_names);
for i = 1:n_r
    fprintf( fw, '%g,%s\n', i, [MRI_struct.resp_names{i} ',' MRI_struct.resp_units{i}]);
end
fprintf( fw, '%s\n','*MRI_LIST');
MRI_list = MRI_struct.MRI_list;
fprintf( fw, '%g, ', MRI_list );
fprintf( fw, '\n');
orientations = MRI_struct.orientations;
n_o = length(orientations);

% ================== O B S E R V E D   P E A K S ==========================

if isfield(MRI_struct, 'MRI_max_obs')
    fprintf( fw, '%s\n','*MRI_OBS_MAX');
    for k=1:length(MRI_list)
        for i = 1:n_f
            fprintf( fw, '%s\n', ['MRI ' num2str(k) ': ' num2str(MRI_list(k))  ' yr']);
            fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(MRI_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s','Orientation,');
            for j = 1:n_r
                fprintf( fw, '%s,',  ['Response ' num2str(j)]);
            end
            fprintf( fw, '\n');
            for j = 1:n_o
                fprintf( fw, '%g,', orientations(j), MRI_struct.MRI_max_obs(k,i,j,:));
                fprintf( fw, '\n');
            end
            fprintf( fw, '%s', 'unknown,');
            fprintf( fw, '%g,', MRI_struct.MRI_UKO_max_obs(k,i,:));
            fprintf( fw, '\n');
        end
    end
end
if isfield(MRI_struct, 'MRI_min_obs')
    fprintf( fw, '%s\n','*MRI_OBS_MIN');
    for k=1:length(MRI_list)
        for i = 1:n_f
            fprintf( fw, '%s\n', ['MRI ' num2str(k) ': ' num2str(MRI_list(k))  ' yr']);
            fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(MRI_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s','Orientation,');
            for j = 1:n_r
                fprintf( fw, '%s,',  ['Response ' num2str(j)]);
            end
            fprintf( fw, '\n');
            for j = 1:n_o
                fprintf( fw, '%g,', orientations(j), MRI_struct.MRI_min_obs(k,i,j,:));
                fprintf( fw, '\n');
            end
            fprintf( fw, '%s', 'unknown,');
            fprintf( fw, '%g,', MRI_struct.MRI_UKO_min_obs(k,i,:));
            fprintf( fw, '\n');
        end
    end
end

% ================== E S T I M A T E D   P E A K S ========================

if isfield(MRI_struct, 'MRI_max_est')
    fprintf( fw, '%s\n','*MRI_EST_MAX');
    for k=1:length(MRI_list)
        for i = 1:n_f
            fprintf( fw, '%s\n', ['MRI ' num2str(k) ': ' num2str(MRI_list(k))  ' yr']);
            fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(MRI_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s','Orientation,');
            for j = 1:n_r
                fprintf( fw, '%s,',  ['Response ' num2str(j)]);
            end
            fprintf( fw, '\n');
            for j = 1:n_o
                fprintf( fw, '%g,', orientations(j), MRI_struct.MRI_max_est(k,i,j,:));
                fprintf( fw, '\n');
            end
            fprintf( fw, '%s', 'unknown,');
            fprintf( fw, '%g,', MRI_struct.MRI_UKO_max_est(k,i,:));
            fprintf( fw, '\n');
        end
    end
end
if isfield(MRI_struct, 'MRI_min_est')
    fprintf( fw, '%s\n','*MRI_EST_MIN');
    for k=1:length(MRI_list)
        for i = 1:n_f
            fprintf( fw, '%s\n', ['MRI ' num2str(k) ': ' num2str(MRI_list(k))  ' yr']);
            fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(MRI_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s','Orientation,');
            for j = 1:n_r
                fprintf( fw, '%s,',  ['Response ' num2str(j)]);
            end
            fprintf( fw, '\n');
            for j = 1:n_o
                fprintf( fw, '%g,', orientations(j), MRI_struct.MRI_min_est(k,i,j,:));
                fprintf( fw, '\n');
            end
            fprintf( fw, '%s', 'unknown,');
            fprintf( fw, '%g,', MRI_struct.MRI_UKO_min_est(k,i,:));
            fprintf( fw, '\n');
        end
    end
end

status = fclose(fw);
if status==-1
    err = errordlg('Unable to close specified file after writing.', err_dlgname);
    uiwait(err); status = -1; return;
end