#include PDF-Generate-0.inc

macro & X0_(& x) { (2 x - X1 - X2) / (X2 - X1) }
macro EXT_ { xy }
 
macro GUI_PDF_Fit_Peaks_0 
   {
      #prm fit_to_data             = operation == 0;   
      #prm generate_fq_gr_from_fit = operation == 1;    
      
      #if (generate_fq_gr_from_fit)
			#if (use_Xo_Is_phase) 
	         num_runs 4
	         #prm convert_to_Q_space = Run_Number == 1;
	         #prm create_FQ          = Run_Number == 2;
	         #prm generate_pdf       = Run_Number == 3;
			#else
	         num_runs 3
	         #prm convert_to_Q_space = Run_Number == 0;
	         #prm create_FQ          = Run_Number == 1;
	         #prm generate_pdf       = Run_Number == 2;
			#endif
      #else
         #prm convert_to_Q_space = 0;
         #prm create_FQ          = 0;
         #prm generate_pdf       = 0;
      #endif
   }
macro GUI_PDF_fit_to_data_3
   {
      #if (fit_to_data)
			print("--- Fitting to data ---\n")
			#if (And(use_Xo_Is_phase)) 
			#else	
				Out_XDD_##EXT_(Intermediate_file_prefix##_decon.xy,  (Yobs - Ycalc) / (LP_Factor_ Average_f^2))
			#endif
         ' Define background
         ' Set mid-point of x-axis to zero to reduce paramater correlation
         ' where X0_(X1) = -1 and X0_(X2) = 1
         local bkg_tot_ = 0;
         Bkg_Empty_Capillary_3(Capillary_Scan, Capillary_Hat, X0_(X), cap_, bkg_tot_, pc0, pc1)
         
         weighting = 1 / Max(Yobs, 1);
         
         inp_text_insert fluorescence_bkg
         if Obj_There(bkg) {
            existing_prm bkg_tot_ += Get(bkg);
         }
         inp_text_insert fluorescence_bkg_penalty
         
         #if (use_bkg_penalty)
            #if (use_simple_bkg_penalty)
               PDF_Generation_Bkg_Penalty_Simple(bkg_tot_, Bkg_Weighting, Yobs)
            #else
               PDF_Generation_Bkg_Penalty(bkg_tot_, Bkg_Weighting, Yobs, 10) 
            #endif
         #endif
      #endif
   }
macro GUI_PDF_Generation_Intensity_Penalty(fn_name, & wt, & scale_peaks)
   {
      scale = scale_peaks;
      extra_X_left = Max(X1 - Max(X1 - 1, 0.1), 0);
      extra_X_right = Max(Min(X2 + 1, 179.9) - X2, 0);

      fn fn_name (x, a0, a1) = wt (a0 - a1)^2 / Max(a0 + a1, 1e-6);

      default_I_attributes 1e-6 min 0 val_on_continue = Val Rand(0.5, 2) + 1e-4;
      create_pks_fn fn_name
   }
macro GUI_PDF_Gen_Fit_Peaks_3 
   {
      xo_Is
         GUI_PDF_Generation_Intensity_Penalty(dfn, Intensity_Penalty_Weighting, Scale_Peaks)
   }                          
macro & LP_Factor_           
   {
      #if (lab_no_monochromator) 
         (1 + Cos(X Pi/ 180)^2) 
      #endif
      1 / (Sin(X Pi/360)^2 Cos(X Pi/360)) ' Lorentz factor
   }
macro GUI_create_decon_file_from_reciprocal_space_data_3
   {
		#if (And(use_Xo_Is_phase, generate_fq_gr_from_fit, Run_Number == 0))
			print("--- Creating deconvoluted 2Th file ---\n")
			iters 0       	
         yobs_eqn aac.##EXT_ = 1; min 0.01 max = Finish_X; del 0.0025
            gui_ignore
            EMP_for_ideal_Q_3	
            PK_shape_for_ideal_Q_3
            Out_XDD_##EXT_(Intermediate_file_prefix##_decon.xy,  Ycalc / (LP_Factor_ Average_f^2))
		#endif     
   }
macro GUI_convert_to_q_space_3
   {
      #if (convert_to_Q_space)
			print("--- Converting deconvoluted file to Q space ---\n")
         iters 0   		 
         xdd Intermediate_file_prefix##_decon.xy
            gui_reload
				finish_X = Th2_Deg_from_Q(Qmax);
            EMP_for_ideal_Q_3
            prm !Q = Q_from_Th2_Deg(X);
            xdd_out Intermediate_file_prefix##_ideal_Q.xy load out_record out_fmt out_eqn { " %.9g  " = Q; " %.9g\n" = 10000 Yobs / Q; }
            /*
            xdd_out ideal_q.xy load out_record out_fmt out_eqn { " %.9g  " = Q; " %.9g\n" = 10000 Yobs Tan(X Pi/360) / Q^2; }
            */
      #endif     
   }
macro GUI_generate_FQ_3
   {
      #if (create_FQ) 
			print("--- Creating F(Q) ---\n")
         penalties_weighting_K1 10
         xdd Intermediate_file_prefix##_ideal_Q.xy
            gui_reload
            rebin_with_dx_of 0.00125
            start_X = Qmin;
            weighting 1
            FQ_poly_3
            if Obj_There(bkg) {
               prm fq = Yobs - Get(bkg);
               FQ_bkg_penalty_3
            } else {
               prm fq = Yobs;
            }
            Out_XDD_##EXT_(Intermediate_file_prefix##_FQ.xy,  fq)
      #endif
   }
macro GUI_generate_Gr_3
   {
      #if (generate_pdf)   
			print("--- Creating G(r) ---\n")
         iters 0   
         xdd Intermediate_file_prefix##_FQ.xy
            gui_reload   
            EMP_for_ideal_Q_3   
            rebin_with_dx_of 0.001   
            pdf_generate {   
               dr = dR;   
               r_max = Rmax;   
               gr_##EXT_##_file = Concat(String(Intermediate_file_prefix),"_Gr");   
               #if (Num_Hats)   
                  hat = Hat_Size; num_hats = Num_Hats;   
               #endif   
            }   
      #endif   
   }         
 