function [par_out,rho_out]=appr_lor_iono_line(h,fr,pw,h_appr_dsc,pth_appr,fil_appr,fil_out,lin_dsc,ar_dsc,dlt_par, eps_par,cf_pw_rat,dat_tim,bcgr_dsc)
% Lorentian type approximation of a critical frequency for an ionogram
% with initial approximation parameters read from a fil_appr file
fl_appr=sprintf('%s', pth_appr, fil_appr);
fl_out=sprintf('%s', pth_appr, fil_out);
par_out=zeros(1,4)-1e11;
rho_out=zeros(1,2)-1e11;

s_fr=size(fr);
w_fr=s_fr(2);
L_h=length(h);
i_at=1;
k_at=0;
key_at=1;
key_out=0;
while key_at>0

    i_f_out=fopen(fl_out);
    i_f_appr=fopen(fl_appr);
        

if i_at==1
if i_f_out>0   
        fclose(i_f_out);
        
    appr_00=load(fl_out);
    if i_f_appr>0
        fclose(i_f_appr);
    end
    
elseif i_f_appr>0
        fclose(i_f_appr);
       appr_00=load(fl_appr);
       if i_f_out>0
        fclose(i_f_out);
       end
    
else
   
    h1=220;
    h2=450;
    h0=215;
    k=8e-3;
    alf=1.4;
    f1=4;
    df=6;
end
else
    if i_f_appr>0
        fclose(i_f_appr);
        
    appr_00=load(fl_appr);
    if i_f_out>0
        fclose(i_f_out);
    end
    else
   
    h1=220;
    h2=450;
    h0=215;
    k=8e-3;
    alf=1.4;
    f1=4;
    df=6;
    end
end
    sz_00=size(appr_00);
    L_00=sz_00(1);
    
    year_0=appr_00(1,1);
    year_2=appr_00(L_00,1);
    year_c=dat_tim(:,1);
    days_prev=0;
    for year=year_0:year_2-1
        days_prev=days_prev+day_of_year(year,12,31);
    end
    days_prev_c=0;
    for year=year_0:year_c-1
        days_prev_c=days_prev_c+day_of_year(year,12,31);
    end
    t_mnt=appr_00(:,4)+appr_00(:,3)*60+appr_00(:,2)*1440+(appr_00(:,1)-year_0)*days_prev*1440;
    t_mnt_c=dat_tim(4)+dat_tim(3)*60+dat_tim(2)*1440+(dat_tim(:,1)-year_0)*days_prev_c*1440;
    if i_at==1
     f_prev=find(t_mnt<=t_mnt_c);
    else
     f_prev=[1:L_00]';
    end
    L_prev=length(f_prev);
    i_prev=0;
    if L_prev>0
        if i_at==1
        i_prev=f_prev(L_prev);
        else
            ii_prev=L_00-i_at+2;
            if ii_prev>0
         i_prev=ii_prev;
            else
                key_at=0;
                
            end
        end
        if i_prev>0
    appr_0=appr_00(i_prev,:);    
        end
    else
    appr_0=appr_00(L_00,:);
    end
  
    dat_tim_0=appr_0(1:4);
    year=appr_0(1);
    doy=appr_0(2);
    i_hr=appr_0(3);
    i_mnt=appr_0(4);
   f1=appr_0(5);
    df=appr_0(6);
    k=appr_0(7);
    alf=appr_0(8);
    rho_lin_in=appr_0(9);
    rho_up_in=appr_0(10);
   
    
    h1=h_appr_dsc(1);
    h2=h_appr_dsc(2);
    h0=h_appr_dsc(3);

  if key_at>0
f_h_appr=find(h>=h1 & h<=h2);
L_h_appr=length(f_h_appr);
if L_h_appr>0
    ha=h(f_h_appr);
    fra=fr(f_h_appr,:);
    pwa=pw(f_h_appr,:);
    y=f1+df*(k*(ha-h0).^alf./(k*(ha-h0).^alf+1));
  %  aab=figure;
   %     ffb=subplot(1,1,1);
    %    ppb=plot(ha,y,'m');
    y1=y-lin_dsc(1);
    y2=y+lin_dsc(1);
    y_up=y2+ar_dsc(1);
    pw_lin=zeros(L_h_appr,w_fr);
    pw_up=zeros(L_h_appr,w_fr);
    for i_col=1:w_fr
    f_lin=[];
    f_up=[];
    f_lin=find(fra(:,i_col)>=y1 & fra(:,i_col)<y2);
    f_up=find(fra(:,i_col)>=y2 & fra(:,i_col)<=y_up);
    L_lin=length(f_lin);
    L_up=length(f_up);
    if L_up>0
        %fr_up=fra(f_up);
        pw_up(f_up,i_col)=pwa(f_up,i_col);
        
       
    else
        pw_up(:,i_col)=0;
    
    end
    
    if L_lin>0
       %  fr_lin=fra(f_lin);
        pw_lin(f_lin,i_col)=pwa(f_lin,i_col);
       
    else
        pw_lin(:,i_col)=0;
    end
    end
    rho_lin=sum(sum(pw_lin))/2/lin_dsc(1);
    rho_up=sum(sum(pw_up))/(lin_dsc(1)+ar_dsc(1));
    if rho_lin>0
        par_in=[f1,df,k,alf];
        rho_in=[rho_lin,rho_up];
        if rho_up>0
        [par_out,rho_out]=appr_lor_grad(ha,fra,pwa,h0,par_in,lin_dsc,ar_dsc,dlt_par, eps_par,cf_pw_rat);
        fr_c=par_out(1);
        df_c=par_out(2);
        k_c=par_out(3);
        alf_c=par_out(4);
        ya=fr_c+df_c*(k_c*(ha-h0).^alf_c./(k_c*(ha-h0).^alf_c+1));
    %    aab=figure;
     %   ffb=subplot(1,1,1);
      %  ppb=plot(ha,ya,'r');
        else
            cf_pw_rat=1;
        [par_out,rho_out]=appr_lor_grad(ha,fra,pwa,h0,par_in,lin_dsc,ar_dsc,dlt_par, eps_par,cf_pw_rat);
        
        end
     %L_out=L_00;
     
     %f_same_dat=find(dat_tim==dat_tim_0);
     %L_same_dat=length(f_same_dat);
      %      if L_same_dat<4
       % L_out=L_00+1
        dat_par_out=[dat_tim,par_out,rho_out];
        
        i_f_out=fopen(fl_out);
        if i_f_out>0
            fclose(i_f_out);
            appr_out_0=load(fl_out);
            
        appr_out=[appr_out_0;dat_par_out];
        year_c=dat_par_out(1);
        days_prev_c=0;
        for year=year_0:year_c-1
        days_prev_c=days_prev_c+day_of_year(year,12,31);
    end
         t_mnt_out=appr_out(:,4)+appr_out(:,3)*60+appr_out(:,2)*1440+(appr_out(:,1)-year_0)*days_prev*1440;
        else
                appr_out=dat_par_out;
        end
            sz_out=size(appr_out);
            L_out=sz_out(1);
            L_bckgr=bcgr_dsc(1);
            dev_bckgr=bcgr_dsc(2);
            if L_out>L_bckgr
                key_bckgr=1;
                pw_rat_out=rho_out(1)/rho_out(2);
                bcgr_par=appr_out(L_out-L_bckgr:L_out-1,:);
                 t_mnt_bcgr=t_mnt_out(L_out-L_bckgr:L_out-1);               
               t_mnt_last=t_mnt_out(L_out-1);
               dt_mnt_last=t_mnt_out(L_out)-t_mnt_out(L_out-1);
                rho_lin_b=mean(bcgr_par(:,9));
                bcgr_par_up=bcgr_par(:,9)./bcgr_par(:,10);
                rho_up_b=mean(bcgr_par(:,9)./bcgr_par(:,10));
                rho_lin_sq=mean((bcgr_par(:,9)-rho_lin_b).^2);
                rho_up_sq=mean((bcgr_par_up-rho_up_b).^2);
                
               par_bs=mean(bcgr_par(:,5:8));
               par_b=ones(L_bckgr,1)*par_bs;
               par_sq=mean((bcgr_par(:,5:8)-par_b).^2);
               if dt_mnt_last<=L_bckgr/3
                   dt_mnt_cf=sqrt(dt_mnt_last*2-1);
                rho_lin_t=rho_lin_b-dt_mnt_cf*dev_bckgr*sqrt(rho_lin_sq);
                if rho_lin_t<lin_dsc(5)
                    rho_lin_t=lin_dsc(5);
                end
                rho_lin_ins=rho_lin_b-dt_mnt_cf*sqrt(rho_lin_sq);
                if rho_lin_ins<lin_dsc(6)
                    rho_lin_ins=lin_dsc(6);
                end
                rho_up_t=rho_up_b-dt_mnt_cf*dev_bckgr*sqrt(rho_up_sq);
                if rho_up_t<ar_dsc(2)
                    rho_up_t=ar_dsc(2);
                end
                d_yyy_b=dlt_par(9)*dt_mnt_cf;
                d_par_b=dev_bckgr*dt_mnt_cf*sqrt(par_sq);
               for iip=1:4
                   if d_par_b(iip)<dt_mnt_cf*dlt_par(iip+4)
                       d_par_b(iip)=dt_mnt_cf*dlt_par(iip+4);
                   end
               end
               d_par_c=abs(par_bs-par_out);
                yyy_c=par_out(1)+par_out(2)*(par_out(3)*(ha-h0).^par_out(4)./(1+par_out(3)*(ha-h0).^par_out(4)));
                yyy_b=par_bs(1)+par_bs(2)*(par_bs(3)*(ha-h0).^par_bs(4)./(1+par_bs(3)*(ha-h0).^par_bs(4)));
                d_yyy_2=mean((yyy_c-yyy_b).^2);
                d_yyy=sqrt(d_yyy_2);
               else
                 key_bckgr=0; 
                 d_par_c=zeros(1,4);
                 d_par_b=ones(1,4);
                 rho_lin_t=lin_dsc(5);
                 rho_lin_ins=lin_dsc(6);
                 rho_up_t=ar_dsc(2);
                 d_yyy=0;
                 d_yyy_b=dlt_par(9);
               end
                
                
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
               
               f_par_by=find(d_par_c<=d_par_b);
               L_par_by=length(f_par_by);
                    if  rho_out(1)>=rho_lin_ins && pw_rat_out>=rho_up_t && (key_bckgr==0  || L_par_by==4 && d_yyy<=d_yyy_b && key_bckgr==1);
                   
                    key_at=0;
                    key_out=1;
                    else
                        par_out_tmp_c=[i_at,par_out,rho_out,pw_rat_out,d_yyy,d_par_c,d_par_b];   
                        k_at=k_at+1;
                        if k_at==1
                          par_out_tmp=par_out_tmp_c;
                      else
                          par_out_tmp=[par_out_tmp;par_out_tmp_c];
                        end
                      
                        if i_at<L_00+1
                      
                      
                      i_at=i_at+1;
                      
                        else
                        sz_tmp=size(par_out_tmp);
                        L_tmp=sz_tmp(1);
                        w_tmp=sz_tmp(2);
                        key_at=0;
                        if L_tmp>0
                            dr_tmp=par_out_tmp(:,w_tmp-7:w_tmp-4)./par_out_tmp(:,w_tmp-3:w_tmp);
                            dr_tmp_abs=dr_tmp(:,1).^2;
                            for icc=2:4
                                dr_tmp_abs=dr_tmp_abs+dr_tmp(:,icc).^2;
                            end
                            dat_tim_tmp=ones(L_tmp,1)*dat_tim;
                        dat_par_tmp=[dat_tim_tmp,par_out_tmp(:,2:7)];
                        if L_tmp>1
                            f_dyy=find(par_out_tmp(:,w_tmp-8)<=d_yyy_b);
                            L_dyy=length(f_dyy);
                            if L_dyy>0
                                par_out_tmp_dyy=par_out_tmp(f_dyy,:);
                                f_rho=find(par_out_tmp_dyy(:,6)>=rho_lin_t & par_out_tmp_dyy(:,8)>=rho_up_t);
                                L_rho=length(f_rho);
                                if L_rho>0
                                    par_out_tmp_rho=par_out_tmp_dyy(f_rho,:);
                                     dr_tmp=par_out_tmp_rho(:,w_tmp-7:w_tmp-4)./par_out_tmp_rho(:,w_tmp-3:w_tmp);
                            dr_tmp_abs=dr_tmp(:,1).^2;
                            for icc=2:4
                                dr_tmp_abs=dr_tmp_abs+dr_tmp(:,icc).^2;
                            end
                            dat_tim_tmp=ones(L_rho,1)*dat_tim;
                        dat_par_tmp=[dat_tim_tmp,par_out_tmp_rho(:,2:7)];
                        [dr_tmp_min,i_dr_tmp_min]=min(dr_tmp_abs);
                        dat_par_out=dat_par_tmp(i_dr_tmp_min,:);
                        sz_a_out=size(appr_out);
                        L_a_out=sz_a_out(1);
                        appr_out(L_a_out,:)=dat_par_out;
                        key_out=1;
                        par_out=dat_par_out(5:8);
                        rho_out=dat_par_out(9:10);
                                end
                            end
                        end
                        end    
                        end
                    end
                
            
                    
            else
                key_at=0;
                key_out=1;
            
            end
    end
    end
end
end
if key_out>0
save(fl_out, 'appr_out', '-ascii');
end