#prm fit_to_data             = operation == 0;   
#prm generate_fq_gr_from_fit = operation == 1;    
#prm fit_to_gr               = operation == 2;   ' Fitting a structure to G(r)

#if (generate_fq_gr_from_fit)
	num_runs 4
   #prm convert_to_Q_space = Run_Number == 1;
   #prm create_FQ          = Run_Number == 2;
   #prm generate_pdf       = Run_Number == 3;
#else
   #prm convert_to_Q_space = 0;
   #prm create_FQ          = 0;
   #prm generate_pdf       = 0;
#endif
macro EXT_ { xy }
'----------------------------------------------------   
#if (convert_to_Q_space)
	print("convert_to_Q_space\n")
   iters 0
   xdd decon.##EXT_ 
      gui_reload
      Deconvoluted_Emmision_Profile
      finish_X = Th2_Deg_from_Q(Qmax);
      prm !Q = Q_from_Th2_Deg(X);
		xdd_out 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
'----------------------------------------------------
#if (create_FQ)
	print("create_FQ\n")
   penalties_weighting_K1 10
   xdd ideal_q.xy
      gui_reload
      rebin_with_dx_of 0.00125
      start_X = Qmin;
      weighting 1

		inp_text_insert fq_poly
      if Obj_There(bkg) {
         prm fq = Yobs - Get(bkg);
         FQ_Bkg_Penalty
		} else {
         prm fq = Yobs;
      }
      #if (Num_Hats == 0)
         prm !Q = X;
         prm Qmax_ = X2;
         #if (Lorch_Constant)
            prm w1 = Lorch_Constant Pi / Qmax_;
            prm w2 = Sin(w1 Q) / (w1 Q);
         #elseif (Soper_Lorch_Constant)
            prm w1 = Q Soper_Lorch_Constant 4.4934 / Qmax_;
            prm w2 = (3 / w1^3) (Sin(w1) - w1 Cos(w1));
         #elseif (Exp_Constant)
            macro & Gauss_x(& x, & fwhm) { (2 Sqrt(Ln(2) / Pi) / fwhm) Exp(-4 Ln(2)(x / fwhm)^2) }
            prm !fwhm = Exp_Constant Qmax_;
            prm w2 = Gauss_x(Q, fwhm);
         #endif
         if (Prm_There(w2)) {
            existing_prm fq *= w2;
         }
      #endif
		Out_XDD_##EXT_(fq.##EXT_,  fq)
#endif
'----------------------------------------------------   
#if (generate_pdf)   
	print("generate_pdf\n")
	iters 0   
	xdd fq.##EXT_   
		gui_reload   
		Deconvoluted_Emmision_Profile   
		rebin_with_dx_of 0.001   
		pdf_generate {   
			dr = dR;   
			r_max = R_Max;   
			gr_##EXT_##_file = String(Gr_File_Name);   
			#if (Num_Hats)   
				hat = Hat_Size; num_hats = Num_Hats;   
			#endif   
		}   
#endif   
'----------------------------------------------------   
#if (fit_to_data)	 
	print("fit_to_data\n")
   PDF_Deconvolution_Init
   #if (Abs(Scale_Yobs_By - 1))
      ' Scale Yobs
      user_y data_file Data_File
      yobs_eqn data.##EXT_ = data_file Scale_Yobs_By;
         min = Start_X;
         max = Finish_X;
         del = Step_X;
   #else
      xdd Data_File
         start_X = Start_X;
         finish_X = Finish_X;
         #if (Step_X)
            rebin_with_dx_of = Step_X;
         #endif
   #endif
   '------------------------------------------------------
   Full_Emmision_Profile
   #if (use_Xo_Is_phase)
      Full_Peak_Shape
   #else
		Out_XDD_##EXT_(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
   macro & X0_(& x) { (2 x - X1 - X2) / (X2 - X1) }
   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 fit_extra
	
	#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
#if (And(use_Xo_Is_phase, generate_fq_gr_from_fit, Run_Number == 0))
   iters 0
   yobs_eqn aac.##EXT_ = 1; min 0.01 max = Finish_X; del 0.0025
	
      gui_ignore ' dont load into GUI
      Deconvoluted_Peak_Shape
		Out_XDD_##EXT_(decon.##EXT_,  Ycalc / (LP_Factor_ Average_f^2))
#endif  	
