function save_model_3(m_i, varargin)
% save_model_3(m_i, maxdeg_MF, maxdeg_SV, maxdeg_SV_2, maxdeg_SV_3, maxdeg_ext, epoch, model_name, model_header);
% save model in mfsynt format

if nargin < 9
    model_header = 'Oersted Field Model'; 
else    
    model_header = varargin{8};
end
if nargin < 8
    model_name = 'oersted.cof'; 
else    
    model_name = varargin{7};
end
if nargin < 7
    epoch = 2000; 
else    
    epoch = varargin{6};
end
if nargin < 6
    maxdeg_ext = 0; 
else    
    maxdeg_ext = varargin{5};
end
if nargin < 5 
    maxdeg_SV_3 = 0; 
else    
    maxdeg_SV_3 = varargin{4};
end
if nargin < 4 
    maxdeg_SV_2 = 0; 
else    
    maxdeg_SV_2 = varargin{3};
end
if nargin < 3
    maxdeg_SV = 0; 
else    
    maxdeg_SV = varargin{2};
end
if nargin < 2
    maxdeg_MF = sqrt(length(m_i)+1)-1;
else    
    maxdeg_MF = varargin{1};
end

if exist('a', 'var')   ~= 1;          a = 6371.2; end

N_koeff_MF_tmp  = maxdeg_MF*(maxdeg_MF+2);
N_koeff_SV_tmp  = maxdeg_SV*(maxdeg_SV+2);
N_koeff_SV_tmp_2 = maxdeg_SV_2*(maxdeg_SV_2+2);
N_koeff_SV_tmp_3 = maxdeg_SV_3*(maxdeg_SV_3+2);
fid=fopen(model_name,'w');
fprintf(fid,'%s, %s\n', model_header, datestr(now));
fprintf(fid,'   ** Reserved for future use **\n');
fprintf(fid,'    %3i  %3i max degree of quadratic and cubic secular variation\n', maxdeg_SV_2, maxdeg_SV_3);
fprintf(fid,'%7.2f  epoch\n', epoch);
fprintf(fid,'    %3i  max degree of static field\n', maxdeg_MF);
fprintf(fid,'    %3i  max degree of linear secular variation\n', maxdeg_SV);
fprintf(fid,'    %3i  max degree of external field\n', maxdeg_ext);
fprintf(fid,'%7.2f  reference radius [km]\n', a);
fprintf(fid,'  n   m        gnm        hnm    gdotnm        hdotnm       gddotnm       hddotnm       gd3otnm       hd3otnm\n');

i=1;
for n=1:maxdeg_MF
    for m=0:n
        if m == 0
            if n > maxdeg_SV
                fprintf(fid,'%3i %3i %12.5f          %3i       %3i           %3i\n',n,m,...
                    m_i(i),0,...
                    0,0);
            else
                if n > maxdeg_SV_2
                    fprintf(fid,'%3i %3i %12.5f          %3i %9.4f           %3i\n',n,m,...
                        m_i(i),0,...
                        m_i(N_koeff_MF_tmp+i),0);
                else
                    if n > maxdeg_SV_3
                        fprintf(fid,'%3i %3i %12.5f          %3i %9.4f           %3i     %9.4f           %3i\n',n,m,...
                            m_i(i),0,...
                            m_i(N_koeff_MF_tmp+i),0,...
                            m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+i),0);
                    else
                        fprintf(fid,'%3i %3i %12.5f          %3i %9.4f           %3i     %9.4f           %3i     %9.4f           %3i\n',n,m,...
                            m_i(i),0,...
                            m_i(N_koeff_MF_tmp+i),0,...
                            m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+i),0, ...
                            m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+N_koeff_SV_tmp_2+i),0);
                    end
                end
            end
            i=i+1;
        elseif m > 0
            if n > maxdeg_SV
                fprintf(fid,'%3i %3i %12.5f %12.5f       %3i           %3i\n',n,m,...
                    m_i(i),m_i(i+1),...
                    0,0);
            else
                if n > maxdeg_SV_2
                    fprintf(fid,'%3i %3i %12.5f %12.5f %9.4f     %9.4f\n',n,m,...
                        m_i(i),m_i(i+1),...
                        m_i(N_koeff_MF_tmp+i),m_i(N_koeff_MF_tmp+i+1));
                else
                    if n > maxdeg_SV_3
                        fprintf(fid,'%3i %3i %12.5f %12.5f %9.4f     %9.4f     %9.4f     %9.4f\n',n,m,...
                            m_i(i),m_i(i+1),...
                            m_i(N_koeff_MF_tmp+i),m_i(N_koeff_MF_tmp+i+1),...
                            m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+i),m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+i+1));
                    else
                        fprintf(fid,'%3i %3i %12.5f %12.5f %9.4f     %9.4f     %9.4f     %9.4f     %9.4f     %9.4f\n',n,m, ...
                            m_i(i),m_i(i+1),...
                            m_i(N_koeff_MF_tmp+i),m_i(N_koeff_MF_tmp+i+1),...
                            m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+i),m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+i+1), ...
                            m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+N_koeff_SV_tmp_2+i),m_i(N_koeff_MF_tmp+N_koeff_SV_tmp+N_koeff_SV_tmp_2+i+1));
                    end
                end
            end	
            i=i+2;
        end
    end
end
for i=N_koeff_MF_tmp+N_koeff_SV_tmp+N_koeff_SV_tmp_2+N_koeff_SV_tmp_3+1:length(m_i)
    fprintf(fid,'%9.4f\n',m_i(i));
end
fclose(fid);

% return

if exist('d_m', 'var') == 1 & length(d_m) == length(m_i)
    fid=fopen('oersted.cof_error','w');
    fid_tex=fopen('oersted_model.tex','w');
    fprintf(fid,'%s, %s\n', model_header, datestr(now));
    fprintf(fid,'   ** Reserved for future use **\n');
    fprintf(fid,'   ** Reserved for future use **\n');
    fprintf(fid,'%7.2f  epoch\n', epoch);
    fprintf(fid,'    %2i  max degree of main field\n', maxdeg_MF);
    fprintf(fid,'    %2i  max degree of secular field\n', maxdeg_SV);
    fprintf(fid,'    %2i  max degree of external field\n', maxdeg_ext);
    fprintf(fid,'%7.2f  reference radius\n', a);
    fprintf(fid,'  n   m    gnm       hnm      gdotnm      hdotnm\n');    
    i=1;
    for n=1:maxdeg_MF
        for m=0:n
            if m == 0
                if n > maxdeg_SV
                    fprintf(fid,'%3i %3i %12.5f          %3i       %3i          %3i %12.5f        %3i       %3i          %3i\n',n,m,m_i(i),0,0,0,d_m(i),0,0,0);
                else
                    fprintf(fid,'%3i %3i %12.5f          %3i %9.3f          %3i %12.5f        %3i %9.3f          %3i\n',n,m,m_i(i),0,m_i(N_koeff_MF_tmp+i),0,d_m(i),0,d_m(N_koeff_MF_tmp+i),0);
                end	
                fprintf(fid_tex,'%3i & %3i & %9.2f &           & %9.2f &           \\\\  \n',n,m,m_i(i),d_m(i));
                i=i+1;
            elseif m > 0
                if n > maxdeg_SV
                    fprintf(fid,'%3i %3i %12.5f %12.5f       %3i          %3i %12.5f %12.5f       %3i          %3i\n',n,m,m_i(i),m_i(i+1),0,0,d_m(i),d_m(i+1),0,0);
                else
                    fprintf(fid,'%3i %3i %12.5f %12.5f %9.3f    %9.3f %12.5f %12.5f %9.3f    %9.3f\n',n,m,m_i(i),m_i(i+1),m_i(N_koeff_MF_tmp+i),m_i(N_koeff_MF_tmp+i+1),d_m(i),d_m(i+1),d_m(N_koeff_MF_tmp+i),d_m(N_koeff_MF_tmp+i+1));
                end	
                fprintf(fid_tex,'%3i & %3i & %12.5f & %12.5f & %12.5f & %12.5f \\\\ \n',n,m,m_i(i),m_i(i+1),d_m(i),d_m(i+1));
                i=i+2;
            end
        end
    end
    for i=N_koeff_MF_tmp+N_koeff_SV_tmp+1:length(m_i)
        fprintf(fid,'%9.4f %9.4f \n', m_i(i), d_m(i));
    end
    fclose(fid);
    
end
