
%licorcalc.m   From Jon Ware, 5/6/99

function [licorval] = licorcalc(rawlicor);
%rawlicor=[h2ocnt co2cnt tempcnt prescnt];
%cnt2mv=4096/4096; max186 w/ tt8 internal ref
xLicor = rawlicor;
%% Calculate the concentration of CO2 using the raw licor data.

%% Device specific coefficients

coeff1 = [5.4253e-17 -7.1608e-13 5.9018e-9 1.337e-5 .14302 0];
coeff2 = [5.7465e-11 3.022e-6 6.892e-3 0];
%coeff2 = [2.724e-12 3.1448e-6 6.261e-3 0];
coeff3 = [0 0.01510 58.239];

p0 = 101.3;                      %Reference pressure
tref = 41.8+273;                 %Reference temperature for H2O

vh2oair = xLicor(1,:);                   %H20 Voltage (mV)
vco2air = xLicor(2,:);                   %CO2 Voltage (mV)
tcell = (50/4096)*xLicor(3,:);            %Cell temp (C)
pcell = polyval(coeff3,xLicor(4,:));     %Cell pressure (kPa)

tc = median(tcell)+273;           %Median Cell temperature (K)
pcell = median(pcell);

Vh20 = vh2oair *((p0/pcell)^0.9); %Corrected Voltage
fw = polyval(coeff2,Vh20);
wcell = fw .* (tc/tref) ;         %Mole fraction of water vapor(mmol/mol)

M = 29 + 11/1000*wcell ;          %Molecular weight of moist air
qcell = 18*wcell./M  ;            %Specific humidity
t0 = 36.5+273 ;                   %Reference temperature for CO2
eair = wcell*pcell/1000  ;        %Vapor Pressure (

Xw = wcell./1000;                 %Mole fraction (dimensionless)
X = 1.0 + 0.5*Xw;                 %Correction for pressure broadening
V = vco2air .*(p0/pcell./X);      %Corrected voltage
fc = polyval(coeff1,V);
co2air = fc .* X *(tc/t0);        %Corrected CO2
co2air_dry=co2air./(1-wcell/1000); %dilution correction
licorval(:,1)=co2air_dry';
licorval(:,2)=qcell';
