function Gain = CalcO2Gain(CTDTempAircal, RelHumidity, O2_mL_L_aircal, Atmpressure)
    % Function to calculate Gain of the SBE63 optode. The gain is the 
    % the ratio of the theoretical value of  temperature, salinity, and pressure.
    % Gain = O2*(T,S,P) / [O2]full - [O2]zero 
    % O2*: full saturation concentration of dissolved oxygen at 1 atm (mL
    % L-1) (Ren et al. 2023)
    % [O2]full: sensor reading at 100% dissolved oxygen saturation
    % [O2]zero: sensor reading at 0% dissolved oxygen saturation
    
    % Inputs:
    %   CTDTempAircal  - CTD temperature reading (in degrees Celsius)
    %   RelHumidity    - Relative humidity of air during calibration (fractional, 1 for 100%)
    %   O2_mL_L_aircal - Raw SBE63 doxy sensor output (in mL/L)
    %   Atmpressure    - Barometer reading during calibration (in mbar)
    %
    % Output:
    %   Gain           - Calculated Gain

    % Calculate pH2O_100_hum
    pH2O_100_hum = exp(52.57 - (6690.9 / (CTDTempAircal + 273.15)) - 4.681 * log(CTDTempAircal + 273.15));
    
    % Calculate pH2O_real
    pH2O_real = pH2O_100_hum * RelHumidity;
    
    % Calculate T_O2soly. Scaled Temperature sca_T. Bittig 2018
    T_O2soly = log((298.15 - CTDTempAircal) / (273.15 + CTDTempAircal));
    
    % Calculate O2_soly_umol_kg. O2*(T,S).
    O2_soly_umol_kg = exp(5.80818 + 3.20684 * T_O2soly + 4.1189 * T_O2soly^2 + 4.93845 * T_O2soly^3 + 1.01567 * T_O2soly^4 + 1.41575 * T_O2soly^5);
    
    % Calculate Density_waterkg_L
    Density_waterkg_L = (999.84847 + 0.06337563 * CTDTempAircal - 0.008523829 * CTDTempAircal^2 + 0.00006943248 * CTDTempAircal^3 - 0.0000003821216 * CTDTempAircal^4) / 1000;
    
    % Calculate O2_soly_uM
    O2_soly_uM = O2_soly_umol_kg * Density_waterkg_L;
    
    % Calculate O2raw_air_uM. 
    % Garcia and Gordon (1992), Benson and Krause
    % (1984) refit mL(STP) L-1; and conversion from mL(STP) L-1 to umol
    % L-1. This comment line from Bittig https://archimer.ifremer.fr/doc/00348/45915/

    % 44.6596 is the conversion factor for mL/L(S,T,P) to umol
    O2raw_air_uM = O2_mL_L_aircal * 44.6596;
    
    % Calculate Gain
    Gain = (O2_soly_uM * (Atmpressure - pH2O_real)) / (1013.25 - pH2O_100_hum) / O2raw_air_uM;
end
