function so=fill_from_saxslab_header(si)
%SAXS/FILL_FROM_SAXSLAB_HEADER this function inserts the values found in the
%saxlab header into the SAXS structure.
% so=fill_from_saxslab_header(si,pixels);
global reduc_par_temp
so=si;
if isempty(si.reduction_state)
    so.reduction_state='000000000000';
end

det_rel_eff=1; %this is a calibration which we can later make room for in the header file
if isfield(si.metadata,'sample_transfact')
    if ~isempty(si.metadata.sample_transfact)
        if (si.metadata.sample_transfact~=0) && isnumeric(si.metadata.sample_transfact)
            so.transfact=si.metadata.sample_transfact;
            so.reduction_state(5)=char(49); % that is a 1
            reduc_par_temp.sample_trans=si.metadata.sample_transfact;
        else
            so.transfact=[];
            so.reduction_state(5)=char(48); % that is a 0
            reduc_par_temp.sample_trans=1;
        end
    end
end
if isfield(si.metadata,'saxsconf_abs_int_fact')
    if ~isempty(si.metadata.saxsconf_abs_int_fact)
        if (si.metadata.saxsconf_abs_int_fact~=0) && isnumeric(si.metadata.saxsconf_abs_int_fact)
            so.abs_int_fact=si.metadata.saxsconf_abs_int_fact;
            so.reduction_state(2)=char(49); % that is a 1
            reduc_par_temp.abs_int_factor=si.metadata.saxsconf_abs_int_fact;
        else
            so.abs_int_fact=[];
            so.reduction_state(2)=char(48); % that is a 0
            reduc_par_temp.abs_int_factor=1;
        end
    end
end
if isfield(si.metadata,'saxsconf_Izero')
    if (~isempty(si.metadata.saxsconf_Izero)&&(si.metadata.saxsconf_Izero~=6945240)&&(si.metadata.saxsconf_Izero~=1)) 
        %second term is a hack from early KU-days, 3rd is hack from middle
        %times where I0 was set to 1 in case
        so.exp_conf.Izero=si.metadata.saxsconf_Izero;
        if isfield(si.metadata,'saxsconf_abs_int_fact')
            if isempty(si.metadata.saxsconf_abs_int_fact)
                so.abs_int_fact=1E6/(so.exp_conf.Izero*det_rel_eff);
                so.reduction_state(2)=char(49); % that a 1
                reduc_par_temp.abs_int_factor=si.metadata.sample_transfact;
            end
        end     
    end
end
if isfield(si.metadata,'sample_thickness')
    if ~isempty(si.metadata.sample_thickness)
        if (si.metadata.sample_thickness~=0) && isnumeric(si.metadata.sample_thickness)
            so.sample_thickness=si.metadata.sample_thickness;
            so.reduction_state(1)=char(49); % that is a 1
            reduc_par_temp.sample_thickness=si.metadata.sample_thickness;
        else
            so.sample_thickness=[];
            so.reduction_state(1)=char(48); % that is a 0
            reduc_par_temp.sample_thickness=1;
        end
    end
end

if isfield(si.metadata,'beamcenter_nominal')
    if ~isempty(si.metadata.beamcenter_nominal)
        so=setcenter(so,[si.metadata.beamcenter_nominal(1)+1,si.metadata.beamcenter_nominal(2)+1]);
    end
end
if isfield(si.metadata,'bsmask_configuration')
    if ~isempty(si.metadata.bsmask_configuration)
        so.bsmask_configuration=si.metadata.bsmask_configuration;
        so.reduction_state(12)=char(49); % that is a 1
        a=add_beamstopmask(so); %mask file contains 1's and 0's
        if ~isempty(a.bsmask_pixels) 
            if size(a.bsmask_pixels)==size(so.raw_pixels)
                warning off MATLAB:divideByZero
                so.bsmask_pixels=double(a.bsmask_pixels)./double(a.bsmask_pixels); % this transforms it into 1's and NaN's
                warning on MATLAB:divideByZero
            else
                so.bsmask_configuration=[];
                so.bsmask_pixels=[];
                so.reduction_state(12)=char(48); % that is a 0
            end
        else
            so.bsmask_configuration=[];
            so.bsmask_pixels=[];
            so.reduction_state(12)=char(48); % that is a 0
        end
    else
        so.bsmask_configuration=[];
        so.bsmask_pixels=[];
        so.reduction_state(12)=char(48); % that is a 0
    end
else
    so.bsmask_configuration=[];
    so.bsmask_pixels=[];
    so.reduction_state(12)=char(48); % that is a 0
end
if isfield(si.metadata,'calibrationtype')
    if ~isempty(si.metadata.calibrationtype)
        so.calibrationtype=si.metadata.calibrationtype;
    end
end
if isfield(si.metadata,'Meas')
    if isfield(si.metadata.Meas,'Description')
        if ~isempty(si.metadata.Meas.Description)
            so.sample_description=si.metadata.Meas.Description;
        end
    end
end

if isfield(si.metadata,'wavelength')
    if ~isempty(si.metadata.wavelength)
        so.wavelength=si.metadata.wavelength;
    end
end
if isfield(si.metadata,'detector_dist')
    if ~isempty(si.metadata.detector_dist)
        so.detector_dist=si.metadata.detector_dist;
    end
end
if isfield(si.metadata,'pixelsize')
    if ~isempty(si.metadata.pixelsize)
        so.pixelsize=si.metadata.pixelsize;
    end
end
so=fill_from_metadata_correction(so);
try
    so = calibrate(so, so.kcal, so.pixelcal, so.koffset, so.wavelength, so.pixelsize, so.detector_dist, so.calibrationtype);
catch
    1;
end

