%**************************************************************************
%pid_anal_param
%This function sets the program parameters.
%
%Output:
%param, structure containing required program parameters
%	input_file_dir      %Input file directory
%   input_file_name     %Input file name
%   input_file_type     %Input file type, gives file format
%   corr_required       %Logical variable, apply a correction to the measured fields, true or false
%   corr_bx_mult        %Multiplicative correction factor for Bx
%   corr_by_mult        %Multiplicative correction factor for By
%   corr_bx_add         %Additive constant correction for Bx (T)
%   corr_by_add         %Additive constant correction for By (T)
%   corr_bxy_add_file   %Correction file name including directory, additive for Bx and By, 'none' for no correction
%   corr_bxy_add_file_type  %Correction file type, gives format
%   set_i1xy_required   %Logical variable whether to apply a correction to set the first field integrals to a specified value
%   set_i1x             %Value of I1x after additive correction (Tm)
%   set_i1y             %Value of I1y after additive correction (Tm)
%   gamma               %Lorentz factor
%   nom_lambda_u        %Nominal undulator period (m)
%   nom_k_value         %Nominal undulator K parameter value
%   nom_lambda_r        %Nominal radiation wavelength (m)
%   num_poles           %Number of poles in the undulator
%   num_end_poles       %Number of poles used to determine end effects
%   cell_length         %Cell length, undulator length plus drift length at each end
%   nom_phase_cell      %Nominal phase change through the cell (rad)
%   save_as_final_result%Save output as a final result, true or false
%   result_file_dir     %Directory where final results are saved
%   result_file_name    %Name of file for final result
%   fig_close_on_exit   %Close figures when program exits, true or false
%   fig_vis             %Figures visible on screen, true or false
%
%Zachary Wolf
%5/12/16

function [param] = pid_anal_param()

%Input file information
% INPUT_FILE_DIR = 'S:\MagData\LCLS\Undulator\HXU-32\DATASET0001\Fine Tuning\Z Scans\Run 023\';     %Directory where data is located, results go there also
% INPUT_FILE_NAME = 'x+00000_y+000_bscanz.dat';   %Name of data file to analyze
% INPUT_FILE_TYPE = 'ZVXYBXYH';                   %File type, gives format: ZVXYBXYH, ZBXYH, ZBYH, ZVYH, ZBYH_ANL, ZBYH_DESY
%INPUT_FILE_DIR = 'S:\MagData\LCLS2\Undulator\HGVPU\DATASET0001\Fine Tuning\Z Scans\Run 057\';     %Directory where data is located, results go there also
INPUT_FILE_DIR = 'S:\MagData\LCLS2\Undulator\HXU-32\DATASET0001\Fine Tuning\Z Scans\Run 092\';
INPUT_FILE_NAME = 'x+00000_y+000_bscanz.dat';   %Name of data file to analyze
INPUT_FILE_TYPE = 'ZVXYBXYH';                   %File type, gives format: ZVXYBXYH, ZBXYH, ZBYH, ZVYH, ZBYH_ANL, ZBYH_DESY

%Measurement data correction information
%Bx_corr = CORR_BX_MULT * Bx + CORR_BX_ADD + CORR_BX_ADD_FILE
%By_corr = CORR_BY_MULT * By + CORR_BY_ADD + CORR_BY_ADD_FILE
CORR_REQUIRED = false;                           %Logical variable whether to apply a correction to the measured fields
CORR_BX_MULT = 1.;                              %Multiplicative correction factor for Bx
CORR_BY_MULT = 1.;                              %Multiplicative correction factor for By
CORR_BX_ADD = 0.00007;                         %Additive constant correction for Bx (T)
CORR_BY_ADD = -0.00001;                         %Additive constant correction for By (T)
CORR_BXY_ADD_FILE = 'none';                     %No Bx or By correction from a file
%CORR_BXY_ADD_FILE = 'c:\Magdata\Earth Field Correction\earth_field_corr.dat';  %Name of file containing additive correction data for Bx and By (T), 'none' for no correction
CORR_BXY_ADD_FILE_TYPE = 'CORR_ZBXY';           %File type, gives format

%Set the first field integrals
SET_I1XY_REQUIRED = true;                       %Logical variable whether to apply a correction to set the first field integrals to a specified value
SET_I1X = -30e-6;                                %Value of I1x after additive correction (Tm)
SET_I1Y = -25e-6;                             %Value of I1y after additive correction (Tm)

%Analysis Parameters
ELEC_GAMMA = 7827.79;                          %Electron Lorentz factor
NOM_LAMBDA_U = .032;                             %Nominal undulator period (m)
NOM_K_VALUE = 3.053077;                         %Nominal K value that the undulator is shimmed to
NOM_LAMBDA_R = (1 / (2 * ELEC_GAMMA^2)) * (1 + (1 / 2) * NOM_K_VALUE^2) * NOM_LAMBDA_U; %Nominal radiation wavelength (m)
%NUM_POLES = 210;                                %Number of poles
%NUM_POLES = 258;                                %Number of poles
NUM_POLES = 210;                                %Number of poles
%NUM_END_POLES = 4;                              %Number of poles to neglect at ends for averages, etc.
NUM_END_POLES = 6;                              %Number of poles to neglect at ends for averages, etc.
%CELL_LENGTH = 3.656;                            %Undulator cell length (m)
CELL_LENGTH = 4.400;                            %Undulator cell length (m)
NOM_PHASE_CELL = 110 * 2 * pi;                  %Nominal phase change through the cell (rad)
% ELEC_GAMMA = 26689.0;                          %Electron Lorentz factor
% NOM_LAMBDA_U = .026;                             %Nominal undulator period (m)
% NOM_K_VALUE = 3.5;                         %Nominal K value that the undulator is shimmed to
% NOM_LAMBDA_R = (1 / (2 * ELEC_GAMMA^2)) * (1 + (1 / 2) * NOM_K_VALUE^2) * NOM_LAMBDA_U; %Nominal radiation wavelength (m)
% NUM_POLES = 258;                                %Number of poles
% NUM_END_POLES = 6;                              %Number of poles to neglect at ends for averages, etc.
% %CELL_LENGTH = 3.656;                            %Undulator cell length (m)
% CELL_LENGTH = 4.400;                            %Undulator cell length (m)
% NOM_PHASE_CELL = 129 * 2 * pi;                  %Nominal phase change through the cell (rad)

%Final results data set
SAVE_AS_FINAL_RESULT = false;                   %Save output as a final result, true or false
RESULT_FILE_DIR = 'c:\magdata\final results\';     %Directory where final results are stored
RESULT_FILE_NAME = 'final_results.dat';    %Name of file for final result

%General
FIG_CLOSE_ON_EXIT = false;                      %Close figures when program exits, true or false
FIG_VIS = true;                                 %Figures visible on the screen, true or false

%If running from C, overwrite previous parameters
MANUAL_MODE = true;
if MANUAL_MODE == false
    INPUT_FILE_NAME = evalin('base','INPUT_FILE_NAME');    %Name of data file to analyze
    INPUT_FILE_DIR = evalin('base','INPUT_FILE_DIR');    
    ELEC_GAMMA = evalin('base','ELEC_GAMMA');              %Electron Lorentz factor 
    FIG_VIS = evalin('base','FIG_VIS');                    %Figures visible on the screen, true or false
    CORR_BX_ADD = evalin('base','CORR_BX_ADD');            %Additive constant correction for Bx (T)
    CORR_BY_ADD = evalin('base','CORR_BY_ADD');            %Additive constant correction for By (T)
    NOM_K_VALUE = evalin('base','NOM_K_VALUE');            %Nominal K value that the undulator is shimmed to
end

%Put the parameters in the parameter structure
param.input_file_dir = INPUT_FILE_DIR;
param.input_file_name = INPUT_FILE_NAME;
param.input_file_type = INPUT_FILE_TYPE;
param.corr_required = CORR_REQUIRED;
param.corr_bx_mult = CORR_BX_MULT;
param.corr_by_mult = CORR_BY_MULT;
param.corr_bx_add = CORR_BX_ADD;
param.corr_by_add = CORR_BY_ADD;
param.corr_bxy_add_file = CORR_BXY_ADD_FILE;
param.corr_bxy_add_file_type = CORR_BXY_ADD_FILE_TYPE;
param.set_i1xy_required = SET_I1XY_REQUIRED;
param.set_i1x = SET_I1X;
param.set_i1y = SET_I1Y;
param.gamma = ELEC_GAMMA;
param.nom_lambda_u = NOM_LAMBDA_U;
param.nom_k_value = NOM_K_VALUE;
param.nom_lambda_r = NOM_LAMBDA_R;
param.num_poles = NUM_POLES;
param.num_end_poles = NUM_END_POLES;
param.cell_length = CELL_LENGTH;
param.nom_phase_cell = NOM_PHASE_CELL;
param.save_as_final_result = SAVE_AS_FINAL_RESULT;
param.result_file_dir = RESULT_FILE_DIR;
param.result_file_name = RESULT_FILE_NAME;
param.fig_close_on_exit = FIG_CLOSE_ON_EXIT;
param.fig_vis = FIG_VIS;

%Done
