function [Gain, Offset] = CalculateOptodeGain(CTDTemperature, ...
    AtmosphericPressure, RelativeHumidity, OptodeReadingRAW_100, ...
    OptodeReadingRAW_0)
    
    % Calculate the gain of the SBE63 dissolved oxygen optode.
    % Ben Werb, bwerb@mbari.org, 9/25/2024

    % 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
    % [2] "Drift Characteristics of Sea-Bird Dissolved Oxygen Optode
    % Sensors" Ren et al. 2023. 
    % DOI: https://doi.org/10.1175/JTECH-D-22-0103.1

    % Inputs:
    % CTDTemperature: CTD temperature reading (Celsius)
    % RelativeHumidity: Humidity of air for calibration (fractional)
    % OptodeReadingRAW_100: Raw optode output at 100% O2 saturation (mL/L)
    % OptodeReadingRAW_0: Raw 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: y-intercept based on zero saturation data, if available.
    
    % Constants and assumptions:
    O2sat = 100; % Air/water sample is assumed fully saturated
    S = 0; % DI water, salinity is 0
    P = 0; % Surface calibration, hydrostatic pressure is 0
    O2_conversion_factor = 44.6596; % Conversion factor mL/L to umol/L [1]
    
    % Validate inputs
    if RelativeHumidity < 0 || RelativeHumidity > 1
        error('Relative Humidity must be between 0 and 1.');
    end
    
    % Convert oxygen saturation to molar oxygen concentration [1]
    O2conc_STP = O2stoO2c_Modified(O2sat, CTDTemperature, S, P, ...
        AtmosphericPressure, RelativeHumidity);
    
    % Convert 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