function status = writeDIFfile( DIF_struct, DIF_path )

% DIF_full_filename     a string containing the output filename (including path)

err_dlgname = 'Error writing DIF file';

% Assemble filename:
d_round = round(10*DIF_struct.d)/10;
d0_round = round(10*DIF_struct.d0)/10;
n_m = size(d_round,1);
d_symbol = 'WLHR';
DIF_filename = ['DIF '];
for i = 1:4
    if any(abs(d_round(:,i)-d0_round(i))>10^-3)
        d_i_str = [num2str(d0_round(i)) '(' num2str(d_round(1,i)) ];
        if n_m>1
            for j = 2:n_m
                 d_i_str = [d_i_str ',' num2str(d_round(j,i))];
            end
        end
        d_i_str = [d_i_str ')'];
    else
        d_i_str = num2str(d0_round(i));
    end
    DIF_filename = [DIF_filename d_symbol(i) '=' d_i_str ', '];
end
DIF_filename = [DIF_filename DIF_struct.terrain(1) '.csv'];

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

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

fprintf( fw, '%s\n','*UNITS');
fprintf( fw, '%s\n','Length, Force, Wind speed');
fprintf( fw, '%s\n',[DIF_struct.length_units ',' DIF_struct.force_units ',' DIF_struct.ws_units ]);
fprintf( fw, '%s\n','*BUILDING_DIMENSIONS');
fprintf( fw, '%s\n','W0,L0,H0,R0');
fprintf( fw, '%g, ', DIF_struct.d0);
fprintf( fw, '\n');
fprintf( fw, '%s\n','*MODEL_DIMENSIONS');
fprintf( fw, '%s\n','W, L, H, R');
d = DIF_struct.d;
n_m = size(d,1); % number of models (>1 for interpolated results)
for i = 1:n_m
    fprintf( fw, '%g,', d(i,:));
    fprintf( fw, '\n');
end
if isfield(DIF_struct, 'wf')
    fprintf( fw, '%s\n','*WEIGHTING_FACTORS');
    for i = 1:n_m
        fprintf( fw, '%s,', ['Model ' num2str(i)]);
    end
    fprintf( fw, '\n');
    fprintf( fw, '%g, ', DIF_struct.wf);
    fprintf( fw, '\n');
end
fprintf( fw, '%s\n','*MODEL_TERRAIN');
fprintf( fw, '%s\n', DIF_struct.terrain);
fprintf( fw, '%s\n','*FRAME_LOCATIONS');
fprintf( fw, '%s\n','Frame #, y-previous, y-current, y-subsequent');
n_f = size(DIF_struct.frame_coords,1);
for i = 1:n_f
    fprintf( fw, '%g,%g,%g,%g\n', i, DIF_struct.frame_coords(i,:));
end
fprintf( fw, '%s\n','*RESPONSE_NAMES');
fprintf( fw, '%s\n','Number, Name, Units');
n_r = length(DIF_struct.resp_names);
for i = 1:n_r
    fprintf( fw, '%g,%s\n', i, [DIF_struct.resp_names{i} ',' DIF_struct.resp_units{i}]);
end
n_q = length(DIF_struct.theta);
fprintf( fw, '%s\n','*ATTACHMENT_LOCATIONS');
fprintf( fw, '%s\n','Index, Face, s-coordinate');
n_a = size(DIF_struct.attach_pts,1);
for i = 1:n_a
    fprintf( fw, '%g,%g,%g\n', i, DIF_struct.attach_pts(i,:) );
end

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

if isfield(DIF_struct, 'X_max_obs')
    fprintf( fw, '%s\n','*DIF_OBS_MAX');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        fprintf( fw, '%s','Wind Dir,');
        for j = 1:n_r
            fprintf( fw, '%s,',  DIF_struct.resp_names{j});
        end
        fprintf( fw, '\n');
        for j = 1:n_q
            fprintf( fw, '%g,', DIF_struct.theta(j), DIF_struct.X_max_obs(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'X_min_obs')
    fprintf( fw, '%s\n','*DIF_OBS_MIN');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        fprintf( fw, '%s','Wind Dir,');
        for j = 1:n_r
            fprintf( fw, '%s,',  DIF_struct.resp_names{j});
        end
        fprintf( fw, '\n');
        for j = 1:n_q
            fprintf( fw, '%g, ', DIF_struct.theta(j), DIF_struct.X_min_obs(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'theta_all')
    n_qa = length(DIF_struct.theta_all);
    p = n_qa/4;
end
if isfield(DIF_struct, 'X_max_obs_all')
    fprintf( fw, '%s\n','*DIF_OBS_MAX_ALL');
    for i = 1:n_f
        fprintf( fw, '%s\n ', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_qa
            if j==1
                fprintf( fw, '%s','Wind Dir A,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==p+1
                fprintf( fw, '%s','Wind Dir B,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==2*p+1
                fprintf( fw, '%s','Wind Dir C,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==3*p+1
                fprintf( fw, '%s','Wind Dir D,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            end
            fprintf( fw, '%g,', DIF_struct.theta_all(j), DIF_struct.X_max_obs_all(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'X_min_obs_all')
    fprintf( fw, '%s\n','*DIF_OBS_MIN_ALL');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_qa
            if j==1
                fprintf( fw, '%s','Wind Dir A,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==p+1
                fprintf( fw, '%s','Wind Dir B,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==2*p+1
                fprintf( fw, '%s','Wind Dir C,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==3*p+1
                fprintf( fw, '%s','Wind Dir D,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            end
            fprintf( fw, '%g, ', DIF_struct.theta_all(j), DIF_struct.X_min_obs_all(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'X_max_obs_bnd')
    fprintf( fw, '%s\n','*DIF_OBS_MAX_BND');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_m
            fprintf( fw, '%s', ['Model ' num2str(j) ': ']);
            fprintf( fw, '%s = %g' , 'W', d(j,1), '; L', d(j,2), '; H', d(j,3), '; R', d(j,4) );
            fprintf( fw, '\n');
            fprintf( fw, '%s','Wind Dir,');
            for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
            fprintf( fw, '\n');
            for k = 1:n_q
                ind = n_q*(j-1)+k;
                fprintf( fw, '%g, ', DIF_struct.theta(k), DIF_struct.X_max_obs_bnd(:,ind,i));
                fprintf( fw, '\n');
            end
        end
    end
end
if isfield(DIF_struct, 'X_min_obs_bnd')
    fprintf( fw, '%s\n','*DIF_OBS_MIN_BND');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_m
            fprintf( fw, '%s', ['Model ' num2str(j) ': ']);
            fprintf( fw, '%s = %g' , 'W', d(j,1), '; L', d(j,2), '; H', d(j,3), '; R', d(j,4) );
            fprintf( fw, '\n');
            fprintf( fw, '%s','Wind Dir,');
            for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
            fprintf( fw, '\n');
            for k = 1:n_q
                ind = n_q*(j-1)+k;
                fprintf( fw, '%g, ', DIF_struct.theta(k), DIF_struct.X_min_obs_bnd(:,ind,i));
                fprintf( fw, '\n');
            end
        end
    end
end

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

if isfield(DIF_struct, 'X_max_est')
    fprintf( fw, '%s\n','*DIF_EST_MAX');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        fprintf( fw, '%s','Wind Dir,');
        for j = 1:n_r
            fprintf( fw, '%s,',  DIF_struct.resp_names{j});
        end
        fprintf( fw, '\n');
        for j = 1:n_q
            fprintf( fw, '%g,', DIF_struct.theta(j), DIF_struct.X_max_est(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'X_min_est')
    fprintf( fw, '%s\n','*DIF_EST_MIN');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        fprintf( fw, '%s','Wind Dir,');
        for j = 1:n_r
            fprintf( fw, '%s,',  DIF_struct.resp_names{j});
        end
        fprintf( fw, '\n');
        for j = 1:n_q
            fprintf( fw, '%g, ', DIF_struct.theta(j), DIF_struct.X_min_est(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'theta_all')
    n_qa = length(DIF_struct.theta_all);
    p = n_qa/4;
end
if isfield(DIF_struct, 'X_max_est_all')
    fprintf( fw, '%s\n','*DIF_EST_MAX_ALL');
    for i = 1:n_f
        fprintf( fw, '%s\n ', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_qa
            if j==1
                fprintf( fw, '%s','Wind Dir A,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==p+1
                fprintf( fw, '%s','Wind Dir B,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==2*p+1
                fprintf( fw, '%s','Wind Dir C,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==3*p+1
                fprintf( fw, '%s','Wind Dir D,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            end
            fprintf( fw, '%g,', DIF_struct.theta_all(j), DIF_struct.X_max_est_all(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'X_min_est_all')
    fprintf( fw, '%s\n','*DIF_EST_MIN_ALL');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_qa
            if j==1
                fprintf( fw, '%s','Wind Dir A,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==p+1
                fprintf( fw, '%s','Wind Dir B,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==2*p+1
                fprintf( fw, '%s','Wind Dir C,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            elseif j==3*p+1
                fprintf( fw, '%s','Wind Dir D,');
                for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
                fprintf( fw, '\n');
            end
            fprintf( fw, '%g, ', DIF_struct.theta_all(j), DIF_struct.X_min_est_all(:,j,i));
            fprintf( fw, '\n');
        end
    end
end
if isfield(DIF_struct, 'X_max_est_bnd')
    fprintf( fw, '%s\n','*DIF_EST_MAX_BND');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_m
            fprintf( fw, '%s', ['Model ' num2str(j) ': ']);
            fprintf( fw, '%s = %g' , 'W', d(j,1), '; L', d(j,2), '; H', d(j,3), '; R', d(j,4) );
            fprintf( fw, '\n');
            fprintf( fw, '%s','Wind Dir,');
            for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
            fprintf( fw, '\n');
            for k = 1:n_q
                ind = n_q*(j-1)+k;
                fprintf( fw, '%g, ', DIF_struct.theta(k), DIF_struct.X_max_est_bnd(:,ind,i));
                fprintf( fw, '\n');
            end
        end
    end
end
if isfield(DIF_struct, 'X_min_est_bnd')
    fprintf( fw, '%s\n','*DIF_EST_MIN_BND');
    for i = 1:n_f
        fprintf( fw, '%s\n', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
        for j = 1:n_m
            fprintf( fw, '%s', ['Model ' num2str(j) ': ']);
            fprintf( fw, '%s = %g' , 'W', d(j,1), '; L', d(j,2), '; H', d(j,3), '; R', d(j,4) );
            fprintf( fw, '\n');
            fprintf( fw, '%s','Wind Dir,');
            for k = 1:n_r, fprintf( fw, '%s,',  DIF_struct.resp_names{k}); end;
            fprintf( fw, '\n');
            for k = 1:n_q
                ind = n_q*(j-1)+k;
                fprintf( fw, '%g, ', DIF_struct.theta(k), DIF_struct.X_min_est_bnd(:,ind,i));
                fprintf( fw, '\n');
            end
        end
    end
end

% ================== L O A D   D I S T R I B U T I O N S ==================

if isfield(DIF_struct, 'load_max')
    n_a = size(DIF_struct.load_max,1);
    fprintf( fw, '%s\n','*LOAD_DISTRIBUTION_MAX');
    for i = 1:n_f
        for j = 1:n_r
            fprintf( fw, '%s', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s\n', ['; Response ' num2str(j) ': ' DIF_struct.resp_names{j}]);
            fprintf( fw, '%s,', 'Wind Dir','DIF_OBS_MAX');
            for k = 1:n_a
                fprintf( fw, '%s, ', ['Index ' num2str(k)]);
            end
            fprintf( fw, '\n');
            for k = 1:n_q
                fprintf( fw, '%g, ', DIF_struct.theta(k) );
                fprintf( fw, '%g, ', DIF_struct.X_max_obs(j,k,i) ); 
                fprintf( fw, '%g, ', DIF_struct.load_max(:,j,k,i) ); fprintf( fw, '\n');
            end
        end
    end
end
if isfield(DIF_struct, 'load_min')
    n_a = size(DIF_struct.load_min,1);
    fprintf( fw, '%s\n','*LOAD_DISTRIBUTION_MIN');
    for i = 1:n_f
        for j = 1:n_r
            fprintf( fw, '%s', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s\n', ['; Response ' num2str(j) ': ' DIF_struct.resp_names{j}]);
            fprintf( fw, '%s,','Wind Dir','DIF_OBS_MIN');
            for k = 1:n_a
                fprintf( fw, '%s, ', ['Index ' num2str(k)]);
            end
            fprintf( fw, '\n');
            for k = 1:n_q
                fprintf( fw, '%g, ', DIF_struct.theta(k) );
                fprintf( fw, '%g, ', DIF_struct.X_min_obs(j,k,i) ); 
                fprintf( fw, '%g, ', DIF_struct.load_min(:,j,k,i) ); fprintf( fw, '\n');
            end
        end
    end
end
if isfield(DIF_struct, 'load_max_all')
    n_a = size(DIF_struct.load_max_all,1);
    fprintf( fw, '%s\n','*LOAD_DISTRIBUTION_MAX_ALL');
    for i = 1:n_f
        for j = 1:n_r
            fprintf( fw, '%s', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s\n', ['; Response ' num2str(j) ': ' DIF_struct.resp_names{j}]);
            for k = 1:n_qa
                if k==1
                    fprintf( fw, '%s,','Wind Dir A','DIF_OBS_MAX_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                elseif k==p+1
                    fprintf( fw, '%s,','Wind Dir B','DIF_OBS_MAX_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                elseif k==2*p+1
                    fprintf( fw, '%s,','Wind Dir C','DIF_OBS_MAX_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                elseif k==3*p+1
                    fprintf( fw, '%s,','Wind Dir D','DIF_OBS_MAX_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                end
                fprintf( fw, '%g,', DIF_struct.theta_all(k) );
                fprintf( fw, '%g,', DIF_struct.X_max_obs_all(j,k,i) );
                fprintf( fw, '%g,', DIF_struct.load_max_all(:,j,k,i) ); fprintf( fw, '\n');
            end
        end
    end
end
if isfield(DIF_struct, 'load_min_all')
    n_a = size(DIF_struct.load_min_all,1);
    fprintf( fw, '%s\n','*LOAD_DISTRIBUTION_MIN_ALL');
    for i = 1:n_f
        for j = 1:n_r
            fprintf( fw, '%s', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s\n', ['; Response ' num2str(j) ': ' DIF_struct.resp_names{j}]);
            for k = 1:n_qa
                if k==1
                    fprintf( fw, '%s,','Wind Dir A','DIF_OBS_MIN_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                elseif k==p+1
                    fprintf( fw, '%s,','Wind Dir B','DIF_OBS_MIN_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                elseif k==2*p+1
                    fprintf( fw, '%s,','Wind Dir C','DIF_OBS_MIN_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                elseif k==3*p+1
                    fprintf( fw, '%s,','Wind Dir D','DIF_OBS_MIN_ALL');
                    for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                    fprintf( fw, '\n');
                end
                fprintf( fw, '%g,', DIF_struct.theta_all(k) );
                fprintf( fw, '%g,', DIF_struct.X_min_obs_all(j,k,i) );
                fprintf( fw, '%g,', DIF_struct.load_min_all(:,j,k,i) ); fprintf( fw, '\n');
            end
        end
    end
end

if isfield(DIF_struct, 'load_max_bnd')
    n_a = size(DIF_struct.load_max_bnd,1);
    fprintf( fw, '%s\n','*LOAD_DISTRIBUTION_MAX_BND');
    for i = 1:n_f
        for j = 1:n_r
            fprintf( fw, '%s', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s\n', ['; Response ' num2str(j) ': ' DIF_struct.resp_names{j}]);
            for k = 1:n_m
                fprintf( fw, '%s', ['Model ' num2str(k) ': ']);
                fprintf( fw, '%s = %g' , 'W', d(k,1), '; L', d(k,2), '; H', d(k,3), '; R', d(k,4) );
                fprintf( fw, '\n');
                fprintf( fw, '%s,','Wind Dir','DIF_OBS_MAX_BND');
                for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                fprintf( fw, '\n');
                for ii = 1:n_q
                    ind = n_q*(k-1)+ii;
                    fprintf( fw, '%g,', DIF_struct.theta(ii), DIF_struct.X_max_obs_bnd(j,ind,i));
                    fprintf( fw, '%g,', DIF_struct.load_max_bnd(:,j,ind,i) ); fprintf( fw, '\n');
                end
            end
        end
    end
end

if isfield(DIF_struct, 'load_min_bnd')
    n_a = size(DIF_struct.load_min_bnd,1);
    fprintf( fw, '%s\n','*LOAD_DISTRIBUTION_MIN_BND');
    for i = 1:n_f
        for j = 1:n_r
            fprintf( fw, '%s', ['Frame ' num2str(i) ':  y = ' num2str(DIF_struct.frame_coords(i,2))  ]);
            fprintf( fw, '%s\n', ['; Response ' num2str(j) ': ' DIF_struct.resp_names{j}]);
            for k = 1:n_m
                fprintf( fw, '%s', ['Model ' num2str(k) ': ']);
                fprintf( fw, '%s = %g' , 'W', d(k,1), '; L', d(k,2), '; H', d(k,3), '; R', d(k,4) );
                fprintf( fw, '\n');
                fprintf( fw, '%s,','Wind Dir','DIF_OBS_MIN_BND');
                for ii = 1:n_a, fprintf( fw, '%s,',  ['Index ' num2str(ii)]); end;
                fprintf( fw, '\n');
                for ii = 1:n_q
                    ind = n_q*(k-1)+ii;
                    fprintf( fw, '%g,', DIF_struct.theta(ii), DIF_struct.X_min_obs_bnd(j,ind,i));
                    fprintf( fw, '%g,', DIF_struct.load_min_bnd(:,j,ind,i) ); fprintf( fw, '\n');
                end
            end
        end
    end
end


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