function [Gain, Offset] = CalculateOptodeGain(CTDTemperature, AtmosphericPressure, RelativeHumidity, OptodeReadingRAW_100, OptodeReadingRAW_0)
    % Calculate the gain of the SBE63 dissolved oxygen optode.

    % Notes:
    % This function uses a modified version of the (Bittig et al. 2018)
    % Matlab routine to convert oxygen saturation to molar oxygen
    % concentration (O2stoO2c_Modified). [1]
    % References: [1] "SCOR WG 142: Quality Control Procedures for Oxygen
    % and Other Biogeochemical Sensors on Floats and Gliders. 
    % Recommendations on the conversion between oxygen quantities 
    % for Bio-Argo floats and other autonomous sensor platforms"
    % Bittig et al. 2018. DOI 10.13155/45915
    
    % Inputs:
    % CTDTemperature: CTD temperature reading (Celsius)
    % RelativeHumidity: Humidity of air for calibration (fractional)
    % OptodeReadingRAW_100: Raw SBE63 optode sensor output at 100% O2 saturation (mL/L)
    % OptodeReadingRAW_0: Raw SBE63 optode sensor output at 0% O2 saturation (mL/L), optional
    % AtmosphericPressure: Barometer reading during calibration (mbar)
    
    % Outputs:
    % Gain: The ratio of the 'true' value to the measured value. 
    % Offset: Calibration offset (y-intercept) based on zero saturation data, if available.
    
    % Constants and assumptions:
    O2sat = 100; % Air/water sample is fully saturated
    S = 0; % DI water, salinity is 0
    P = 0; % Hydrostatic pressure is 0 because calibration takes place at the surface.
    O2_conversion_factor = 44.6596; % Conversion factor for dissolved oxygen for mL/L to umol/L
    % Molar volume of oxygen: 22.3916 L_STP mol-1 (Garcia and Gordon 1992).
    % Its reciprocal gives the conversion factor of 44.6596 μmol mL_STP-1 
    
    % Validate inputs
    if RelativeHumidity < 0 || RelativeHumidity > 1
        error('Relative Humidity must be between 0 and 1.');
    end
    
    % Convert the oxygen saturation to molar oxygen concentration (umol/L)
    O2conc_STP = O2stoO2c_Modified(O2sat, CTDTemperature, S, P, AtmosphericPressure, RelativeHumidity);
    
    % Convert measured raw optode reading to molar oxygen concentration (umol/L)
    O2_Measured_full = OptodeReadingRAW_100 * O2_conversion_factor;
    
    % Avoid division by zero
    if O2_Measured_full == 0
        error('Optode reading at 100% saturation cannot be zero.');
    end
    
    % Calculate the Gain
    Gain = O2conc_STP / O2_Measured_full;
    
    % Calculate the offset if zero saturation data is provided
    if nargin == 5 && ~isempty(OptodeReadingRAW_0)
        O2_Measured_zero = OptodeReadingRAW_0 * O2_conversion_factor;
        Offset = -Gain * O2_Measured_zero;
    else
        Offset = []; % No offset calculated
    end
end