%10/2013 - LB
% Same as previous code version but 
% 1) attempt better calculation using Tvais+Tsonic and hydrostatic pressure correction (Pvais + heave)
% 2) adjust CO2/H2O mean to qvais and CO2 7200 PSD
% 2) detrend signal in cov flux computation
% spectral decorrelation + PKT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%program that compute cov flux from LICOR (7200, 7500) units for
%HiWinGS2013
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
hr=sprintf('%02i',hhh);
jd=sprintf('%03i',ddd);

fs=10;         %Sonic are output at 20 Hz
fc=1/30;       %Cutoff frequency for angle calculation
nfreq = fs/2.0;                   %Nyquist frequency
wp = fc/nfreq;
ws = .7*wp;
[n,wn] = buttord(wp,ws,10,25);        %Filter coefficients
[bhi,ahi] = butter(n,wn,'high');      %High order Butterworth filter
R=8.314472; % Pa m3 /K /mol
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%first setup the hydrostatic correction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% %%%%%%%%%%%%%%%%%  gets the Vaisala data %%%%%%%%%%%%%%
% read_hp_HiWinGS_2013;  %get Tvais, Rhvais, Tsea and press
%%%%%%%%%%%%%%%%%  USes QC met data instead  %%%%%%%%%%%%%%
A =importdata('C:\data\CRUISES\HiWinGS_2013\Knorr\flux\Processed\HiWinGS_2013_1minMeans_r1.txt');
A=A.data;
yday=A(:,1); %Pressure (mb) adjusted to 10 m
ii=find(yday>=ddd+hhh/24 & yday<ddd+(hhh+1)/24);
press=A(ii,14)'; %Pressure (mb) 
Rhvais=A(ii,15)';   %Relative humidity(%)
Tvais=A(ii,16)';    %Temperature (C) 
Tsea=A(ii,18)';    %Temperature (C) 

% press=press+0.12*(10.6-17.2); %adjust for height
% Rhvais=((Rhvais-3)+0.41)/0.95; %uses cal from Vaisala
[Press,Tseak,Tairk,Qsatsea,Qsatms,Qms,Rhoair]=scalarv(press,Tsea,Tvais,Rhvais);
%outputs: ATMOSPHERIC PRESSURE (Pa), SEA SURFACE TEMPERATURE (K), AIR TEMPERATURE (K)
% %SPEC HUM SEA (g/kg), Qsatms,  %SPEC HUM AIR (g/kg) and density
qair=Qms; %g/Kg
tsonicV=Tairk.*(1+0.51*Qms./1000);  %sonic temperature
rairV = Qms./(1-Qms./1000);                      %Mixing ratio g/Kg
TvirtV = Tairk.*(1+0.61.*Qms./1000); %Fluctuating air temperature
rhovV = Rhoair.*rairV;                           %Corrected density
meanP = nanmean1(press)/10;
meanQ = nanmean1(Qms);
meanTv = nanmean1(TvirtV);
h2oV=29*meanQ/18/8.314/meanTv*meanP*1000;        %Humidity mmol/m^3
clear Press Qsatms Qsatms Qsatsea Tairk Tseak
Rhoair=nanmean1(Rhoair);

%%%%%%%%%%%%%%%%%  Get the latitude position from PSD GPS %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gpslat=A(ii,2)';
% dfl=[way_raw_data_flux, year(3:4),jd,'\gps0' year(3:4) jd hr '_raw.txt'];
% [gpslat] = GetPSDlat_HIWINGS2013(dfl,ddd,hhh);
g=grv(nanmedian(gpslat));
% % clear gpslat

%%%%%%%%%%%%%%%%%  Loads the Heave data and get pressure from it %%%%%%%%%%%%%%
motdata=load([drive_w '\MotionData\HiWinGS2013_UHmotionfile_',jd hr,'.prn']);
% tmotion=motdata(:,1);
heave=motdata(:,7);
%removes first and last 20s for motcorr filter tapering issues
heave(1:20)=NaN;
heave(end-19:end)=NaN;

heave=heave-nanmean1(heave); 
dp=-nanmean1(Rhoair)*g*heave/100;
dp(isnan(dp))=nanmedian1(dp);
% 
% S1=length(heave);t1=(1:S1)/S1;
% V1=length(press); tV=(1:V1)./V1;
% Pair=interp1(tV,press,t1);

PairL=(nanmean1(press)+dp)/10; %<<<<<<<<<<<<<<<<<<<<<< Pressure to use in 7500 computation (in kPa)
PairL=PairL';
PairL(isnan(PairL))=nanmean1(PairL);

% pressf=speedup(dp,press)';
% PairL=pressf-filtfilt(bhi,ahi,pressf)+filtfilt(bhi,ahi,dp); %mb here!
% PairL=PairL'/10;
% PairL(isnan(PairL))=nanmean1(PairL);


clear dp motdata pressf
% figure;plot(tref,Licor_Press,tref,dpair/10,'r')


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %Then get the sonic temperature
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
read_son_HiWinGS_2013;  %get virtual temperature Tsonic
% Tvirt=Tsonic-nanmean1(Tsonic)+nanmean1(Tvais);  %Let's keep it simple
% Tair1 = Tvirt./(1+0.51*nanmean1(qair)./1000);    %Fluctuating air temperature
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Uses Tvais for Licor computation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S1=length(heave);t1=(1:S1)/S1;
V1=length(press); tV=(1:V1)./V1;
TairL=interp1(tV,Tvais,t1);
TairL(isnan(TairL))=nanmean1(TairL);
clear S1 t1 V1 tV
% figure;plot(tV*3600,Tvais,'b',t1*3600,TairL,'g--',tref,Licor_Temp,'k')

RHL=speedup(TairL,Rhvais);
tvirt=speedup(TairL,TvirtV);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%reads in the PSD 7500
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
licor_prefix='lic';
dfl=[way_raw_data_flux, year(3:4),jd,'\' licor_prefix '0', year(3:4) jd hr '_raw.txt'];
[agc_0,Licor_CO2,Licor_H2O,Licor_Temp,Licor_Press] = read_7500co2_HiWinGs_2013(dfl);
% [agc_0,Licor_CO2,Licor_H2O,~,~] = read_7500co2_DYNAMO2011(dfl);
%       Licor_CO2  ;%mmol/m^3
%       Licor_H2O ;%mmol/m^3
%       Licor_Temp ;%C
%       Licor_Press ;%kPa

% [licorval7500PSD,h2ommol7500PSD] = licorWebbPTK(Licor_CO2,Licor_H2O,PairL,TairL,RHL,tvirt,10);
% Rc7500PTK_PSD=licorval7500PSD(1,:);  %CO2 Mixing ratio mg/Kg
% Rv7500PTK_PSD=licorval7500PSD(2,:);  %H2O Mixing ratio g/Kg
% Tair7500PTK_PSD=licorval7500PSD(4,:); %Fluctuating air temperature (degC)
% % rhfast7500PTK_PSD=licorval7500PSD(6,:); %%fast rh from Licor and
% Vaisala(%)
% % 

% convert units from mol/m3 to mol/mol
Rgas=287.1;
Rgas_universal=8.314472; % Pa m3 /K /mol
% Licor_CO2_a=(Licor_CO2*1e-3).*Rgas_universal.*(Licor_Temp+273.15)./(Licor_Press*1e3); %mmol/m3 to mol/mol
% converter=29/44/8.314*meanP/meanTv/rhoair;
Licor_CO2_a=(Licor_CO2*1e-3).*Rgas_universal.*(TairL+273.15)./(PairL*1e3); %mmol/m3 to mol/mol   <<<<<<<<<<<<< much better!
% figure;plot(tref,Licor_CO2_a*1e6,tref,Licor_CO2_a2*1e6,'g')
% [SCO2_7500PSD2,Fpic]=psd2(detrend(Licor_CO2_a2*1e6),length(Licor_CO2_a2*1e6),10,hamming(length(Licor_CO2_a2*1e6)));[SCO2_7500PSD2,Fpics]=specsmoo(SCO2_7500PSD2,10);
% [SCO2_7500PSD,Fpic]=psd2(detrend(Licor_CO2_a*1e6),length(Licor_CO2_a*1e6),10,hamming(length(Licor_CO2_a*1e6)));[SCO2_7500PSD,Fpics]=specsmoo(SCO2_7500PSD,10);
% figure;loglog(Fpics,Fpics.*SCO2_7500PSD,Fpics,Fpics.*SCO2_7500PSD2,'g','LineWidth',2);grid
% legend('Using 7500 measurements','using Tvais and Pvais+hydrostatic dp')

% wcell = 8.314/1000*Licor_H2O.*(TairL+273.15)./PairL; %H2O mass concentration mmol/mol
% M = 29 - 11/1000*wcell;                         %Molecular weight of moist air
% qair2 = 18*wcell./M;                             %Specific humidity g/Kg
% rair = qair./(1-qair/1000);                     %Mixing ratio g/Kg

% [licortest,h2otest] = licorWebb(Licor_CO2,Licor_H2O,PairL*10,TairL,nanmean1(Rhvais),Tsonic+273.15);


LI7500_CO2mf=Licor_CO2_a*1e6;  %7500 CO2 in ppm
LI7500_H2O=Licor_H2O;  %7500 CO2 in mmol/m3

LI7500_AGC=reshape(agc_0,6000,6); %%
LI7500_AGC_10m=nanmean1(LI7500_AGC,1); %%
LI7500_AGC_10s=nanstd1(LI7500_AGC); %%

clear Lenlic Licor_CO2_1 Licor_H2O Licor_Press Licor_Temp Licor_diag_0  agc_0 LI7500_AGC Nlenlic Licor_CO2_a Licrate Licor_CO2_a2
clear Tsea t_case1_adjustment t_case2_adjustment t_dome1_adjustment t_dome2_adjustment ta_etl_adjustment ta_im_adjustment tsnake_adjustment
% clear rh_etl_adjustment rotationsonic sonicmodel bp_etl_adjustment

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%reads in the PSD 7200
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read_7200_DYNAMO2011all;
    % %from cal in lab
% Zw0=0.9940 ; Sw0=1.0047;
% Zc0=0.9209  ; Sc0=1.0025 ;
    % %from cal in Darwin
% Zw0=0.994338 ; Sw0=0.996341;
% Zc0=0.921509  ; Sc0=1.00245 ;
    % %from cal on Nov 6 beginning leg3
% Zw0=0.992925 ; Sw0=0.996341;
% Zc0=0.922294  ; Sc0=1.01891 ;
    % %from cal on Nov 6 beginning leg3
% Zw0=0.992925 ; Sw0=0.996341;
% Zc0=0.9222216  ; Sc0=1.01817 ;
%     % %LB - 01/17/2013 << to match output from Licor
% Zw0=0.99553 ; Sw0=0.99473;
% Zc0=0.92308;Sc0=1.01813;
% [alphac,CO2dnew,CO2mfnew,alphaw,H2Odnew,H2Omfnew,Xc,Xw,Pec] = LicorH2OCO2fromequations(LI7200_Press,LI7200_H2Omf/1000,LI7200_Tmean,LI7200_cooler,LI7200_CO2AW,LI7200_CO2AWO,LI7200_H2OAW,LI7200_H2OAWO,Zc0,Zw0,1.15,0.0024,Sc0,Sw0);
% % [alphac,CO2dnew,CO2mfnew,alphaw,H2Odnew,H2Omfnew,Xc,Xw,Pec] = LicorH2OCO2fromequations(PairL-nanmedian1(LI7200_APress-LI7200_Press),LI7200_H2Omf/1000,LI7200_Tmean,LI7200_cooler,LI7200_CO2AW,LI7200_CO2AWO,LI7200_H2OAW,LI7200_H2OAWO,Zc0,Zw0,1.15,0.0024,Sc0,Sw0);
% CO2mfnew=(CO2mfnew./(1-1e-6.*H2Odnew.*R.*(LI7200_Tmean+273.15)./LI7200_Press)); %dry
% % Cnewm=CO2mfnew./(1-1.6*LI7200_H2Omf*1e-3);  %dry
% figure;plot(LI7200_CO2mfd);hold on;plot(CO2mfnew,'r')
% figure;plot(LI7200_CO2raw);hold on;plot(alphac,'r') %if absorptance not right already, why bother with Span...must come from Zc0...
% XX=LI7200_CO2mfd;YY=CO2mfnew;
% [SCO2_7200PSD,Fpic]=psd2(detrend(XX),length(XX),10,hamming(length(XX)));[SCO2_7200PSD,Fpics]=specsmoo(SCO2_7200PSD,10);%clear XX
% [SCO2_7200PSDf,Fpic]=psd2(detrend(YY),length(YY),10,hamming(length(YY)));[SCO2_7200PSDf,Fpics]=specsmoo(SCO2_7200PSDf,10); %clear YY
% figure;loglog(Fpics,Fpics.*SCO2_7200PSD,'g',Fpics,Fpics.*SCO2_7200PSDf,'r','LineWidth',2);grid

read_7200_HiWinGS_2013_all; %reads most of the variables
%     % %LB - 01/17/2013 << to match output from Licor
Zw0=0.99553 ; Sw0=0.99473;
Zc0=0.92308; Sc0=1.01813;
aw=1.72;Xwc=0.0024;Sco2=Sc0;Sw=Sw0;
% [~,~,CO2mfnew,~,H2Odnew,~,~,~,~] = LicorH2OCO2fromequations(LI7200_Press,LI7200_H2Omf/1000,LI7200_Tmean,LI7200_cooler,LI7200_CO2AW,LI7200_CO2AWO,LI7200_H2OAW,LI7200_H2OAWO,Zc0,Zw0,aw,Xwc,Sco2,Sw);
[alphac,CO2dnew,CO2mfnew,alphaw,H2Odnew,H2Omfnew,Xc,Xw,Pec] = LicorH2OCO2fromequations(LI7200_Press,LI7200_H2Omf/1000,LI7200_Tmean,LI7200_cooler,LI7200_CO2AW,LI7200_CO2AWO,LI7200_H2OAW,LI7200_H2OAWO,Zc0,Zw0,aw,Xwc,Sco2,Sw);
clear alphac CO2dnew alphaw H2Omfnew Xc Xw Pec
% [alphac,CO2dnew,CO2mfnew,alphaw,H2Odnew,H2Omfnew,Xc,Xw,Pec] = LicorH2OCO2fromequations(PairL-nanmedian1(LI7200_APress-LI7200_Press),LI7200_H2Omf/1000,LI7200_Tmean,LI7200_cooler,LI7200_CO2AW,LI7200_CO2AWO,LI7200_H2OAW,LI7200_H2OAWO,Zc0,Zw0,1.15,0.0024,Sc0,Sw0);
CO2mfnew=(CO2mfnew./(1-1e-6.*H2Odnew.*R.*(LI7200_Tmean+273.15)./LI7200_Press)); %dry
clear LI7200_Press LI7200_H2Omf LI7200_Tmean LI7200_cooler LI7200_CO2AW LI7200_CO2AWO LI7200_H2OAW LI7200_H2OAWO Zc0 Zw0 Sc0 Sw0
% read_7200_DYNAMO2011;
% if length(LI7200_CO2mf) < 35900 
%     disp('<<<<<<<<<<<<<<<<<<  achtung >>>>>>>>>>>>>>>>>>>>>>>>')
% %     break
%     LI7200_CO2mf=interp1(t7200,LI7200_CO2mf,tmotion,'linear');
%     
% else
if length(LI7200_CO2mf)< 36000
    LI7200_CO2mf=[LI7200_CO2mf (1:36000-length(LI7200_CO2mf)).*0+LI7200_CO2mf(length(LI7200_CO2mf))];
    LI7200_CO2mfd=[LI7200_CO2mfd (1:36000-length(LI7200_CO2mfd)).*0+LI7200_CO2mfd(length(LI7200_CO2mfd))];
    LI7200_H2Od=[LI7200_H2Od (1:36000-length(LI7200_H2Od)).*0+LI7200_H2Od(length(LI7200_H2Od))];
    LI7200_agc=[LI7200_agc (1:36000-length(LI7200_agc)).*0+LI7200_agc(length(LI7200_agc))];
    CO2mfnew=[CO2mfnew (1:36000-length(CO2mfnew)).*0+CO2mfnew(length(CO2mfnew))];
    H2Odnew=[H2Odnew (1:36000-length(H2Odnew)).*0+H2Odnew(length(H2Odnew))];
elseif length(LI7200_CO2mf)> 36000    
    LI7200_CO2mf=LI7200_CO2mf(1:36000);
    LI7200_CO2mfd=LI7200_CO2mfd(1:36000);
    LI7200_H2Od=LI7200_H2Od(1:36000);
    LI7200_agc=LI7200_agc(1:36000);
    CO2mfnew=CO2mfnew(1:36000);
    H2Odnew=H2Odnew(1:36000);
end
% LI7200_CO2mf=  LI7200_CO2mfd;  %dry output from PSD 7200
% % 
% % fs=10;         %Sonic are output at 10 Hz
% % fc=1/200;       %Cutoff frequency for angle calculation
% % wp=1/25/(fs/2);
% % ws=0.8*wp;
% % [n,wn]=buttord(wp,ws,3,7);
% % % [n,wn]=buttord(wp,ws,10,25);
% % [bhi,ahi]=butter(n,wn,'high');
% % Ptest=filtfilt(bhi,ahi,LI7200_Press);
% % Ttest=filtfilt(bhi,ahi,LI7200_Tmean);
% % 
% % % % % N=21; bf=ones(1,N)/N; af=1;
% % % % % Ttestf=filtfilt(bf,af,Ttest);
% % % % % [bhi,ahi]=butter(n,wn,'low');
% % % % % Ttestf=filtfilt(bhi,ahi,LI7200_Tmean);
% % % % Ttestf=medfilt1(Ttestf,2400);
% % % % Ttestf(1:600)=Ttestf(601);  Ttestf(end-600:end)=Ttestf(end-601);  %removes funky points
% % % % figure;plot(t7200,(LI7200_Tmean),t7200,LI7200_Tmean-Ttest,'r',t7200,Ttestf,'k')
% % 
% % % LI7200_CO2mf2=(LI7200_CO2d*1e-3).*Rgas_universal.*(TairL+273.15)./((PairL-6.5)*1e3); %mmol/m3 to mol/mol   <<<<<<<<<<<<< much better!
% % LI7200_CO2mf2=(LI7200_CO2d*1e-3).*Rgas_universal.*((LI7200_Tmean-Ttest)+273.15)./((LI7200_Press-Ptest)*1e3); %mmol/m3 to mol/mol   <<<<<<<<<<<<< much better!
% % 
% % XX=LI7200_CO2mf;
% % % XX=LI7200_Tmean;
% % [SCO2_7200PSD,Fpic]=psd2(detrend(XX),length(XX),10,hamming(length(XX)));[SCO2_7200PSD,Fpics]=specsmoo(SCO2_7200PSD,10);clear XX
% % YY=LI7200_CO2mf2*1e6;
% % % YY=LI7200_Tmean-Ttest;
% % [SCO2_7200PSDf,Fpic]=psd2(detrend(YY),length(YY),10,hamming(length(YY)));[SCO2_7200PSDf,Fpics]=specsmoo(SCO2_7200PSDf,10); clear YY
% % figure;loglog(Fpics,Fpics.*SCO2_7200PSD,'g',Fpics,Fpics.*SCO2_7200PSDf,'r','LineWidth',2);grid
% % 

LI7200_AGC_PSD=reshape(LI7200_agc,6000,6); %%
LI7200_AGC_PSD_10m=nanmean1(LI7200_AGC_PSD); %%
LI7200_AGC_PSD_10s=nanstd1(LI7200_AGC_PSD); %%

clear LI7200_AGC_PSD Nlenlic 

%-------------------------------------------------------------------------

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Loads the motion corrected w' 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% uvwmat=[tmotion' Uj' Vj' Wj' Reldirby'];
try
    uvwmat=load(['C:\Data\CRUISES\HiWinGS_2013\flux_data\MotionData\HiWinGS2013_UH_UVWstream_',jd hr,dcname,'.prn']);
    tmotion=uvwmat(:,1);
    uj=uvwmat(:,2);
    vj=uvwmat(:,3);
    wj=uvwmat(:,4);
catch
    uj=LI7200_CO2mf*0+NaN;
    vj=LI7200_CO2mf*0+NaN;
    wj=LI7200_CO2mf*0+NaN;
end
% motmatall=[tref' uvwplat(1,:)' uvwplat(2,:)' uvwplat(3,:)' xyzplat(1,:)' xyzplat(2,:)' xyzplat(3,:)' euler(1,:)' euler(2,:)' euler(3,:)' deg_rate(1,:)' deg_rate(2,:)' deg_rate(3,:)'];
% sdg=['save ', drive_w '\MotionData\DYNAMO2011_motionfile_',jd hr,'.prn motmatall -ascii'];
read_motion_HiWinGS_2013;  %  get the acceleration for decorrelation

Plat=[accx
      accy
      accz];
  
motmat=load(['C:\Data\CRUISES\HiWinGS_2013\flux_data\MotionData\HiWinGS2013_UHmotionfile_',jd hr,'.prn']);
uvwplat=[motmat(:,1) motmat(:,2) motmat(:,3)]';
xyzplat=[motmat(:,4) motmat(:,5) motmat(:,6)]';
% euler=[motmat(:,7) motmat(:,8) motmat(:,9)]';
% clear motmat;
% Platx=motmat(:,2);
% Platy=motmat(:,3);
% Platz=motmat(:,4);
Plat=[motmat(:,14) motmat(:,15) motmat(:,16)]';


%-------------------------------------------------------------------------
%removes residual effects from the puff spike (removes first 1min to be safe)
%-------------------------------------------------------------------------
% if hhh==00
   LI7200_CO2mf(1:600)=NaN;LI7200_CO2mfd(1:600)=NaN;
% end   

%-------------------------------------------------------------------------
% Apply various correction, mainly Webb to PSD 7500. UC 7500 Webb is
% already apply in ludodatagrabber_lb.m
%-------------------------------------------------------------------------
%>>>>> PSD 7500
LI7500_H2O=LI7500_H2O.*18./1000./Rhoair;         %convert from mmol/m3 to g/kg
LI7500_H2O=LI7500_H2O+nanmedian1((nanmedian1(qair)-LI7500_H2O));  %adjust to PSD Vaisala
% % LI7500_CO2mf=LI7500_CO2mf./(1-LI7500_H2O./18.*1000.*Rhoair.*Rgas_universal.*(nanmedian1(Tvais)+273.15)./1000./nanmedian1(press)./1000);   %dilution correction
LI7500_CO2mf=LI7500_CO2mf./(1-LI7500_H2O./18.*1000.*Rhoair.*Rgas_universal.*(TairL+273.15)./1000./PairL./1000);   %dilution correction
% % LI7500_CO2mf=LI7500_CO2mf+nanmedian1(CO27200_UCwebb-LI7500_CO2mf);  %adjust 7500 CO2 mean to UC7200webb or UC7200
LI7500_CO2mf=LI7500_CO2mf+nanmedian1(LI7200_CO2mf-LI7500_CO2mf);  %adjust 7500 CO2 mean to PSD7200
%>>>>> PSD 7200
LI7200_H2O=LI7200_H2Od.*18./1000./Rhoair;         %convert from mmol/m3 to g/kg
H2Odnew=H2Odnew.*18./1000./Rhoair;    
% if ddd+hhh/24>=341.25  %when Nafion OFF
%     LI7200_H2O=LI7200_H2O+nanmedian1((nanmedian1(qair)-LI7200_H2O));  %adjust to PSD Vaisala
%     H2Odnew=H2Odnew+nanmedian1((nanmedian1(qair)-H2Odnew));  %adjust to PSD Vaisala
% end
%>>>>> UC 7200 
% H2O7200_UC=H2O7200_UC./Rhoair; %from g/m3 to g/kg
% H2O7200_UC=H2O7200_UC+nanmedian1((nanmedian1(qair)-H2O7200_UC));  %adjust to PSD Vaisala
% CO27200_UC=CO27200_UC+nanmedian1((nanmedian1(LI7200_CO2mf)-CO27200_UC)); %adjust 7200 CO2 mean to PSD7200
%>>>>> UC 7500
% H2O7500_UC=H2O7500_UC./Rhoair; %from g/m3 to g/kg
% H2O7500_UC=H2O7500_UC+nanmedian1((nanmedian1(qair)-H2O7500_UC));  %adjust to PSD Vaisala
% CO27500_UC=CO27500_UC+nanmedian1(CO27200_UCwebb-CO27500_UC); %adjust 7500 CO2 mean to UC7200webb
% CO27500_UC=CO27500_UC+nanmedian1(CO27200_UC-CO27500_UC); %adjust 7500 CO2 mean to UC7200webb


% % % 
% % % [SH2O_7500PSD,Fpic]=psd2(detrend(LI7500_H2O),length(LI7500_H2O),10,hamming(length(LI7500_H2O)));[SH2O_7500PSD,Fpics]=specsmoo(SH2O_7500PSD,10);
% % % [SH2O_7500UC,Fpic]=psd2(detrend(H2O7500_UC),length(H2O7500_UC),10,hamming(length(H2O7500_UC)));[SH2O_7500UC,Fpics]=specsmoo(SH2O_7500UC,10);
% % % [SH2O_7200UC,Fpic]=psd2(detrend(H2O7200_UC),length(H2O7200_UC),10,hamming(length(H2O7200_UC)));[SH2O_7200UC,Fpics]=specsmoo(SH2O_7200UC,10);
% % % figure;loglog(Fpics,Fpics.*SH2O_7500PSD,'g',Fpics,Fpics.*SH2O_7500UC,'m',Fpics,Fpics.*SH2O_7200UC,'r','LineWidth',2);grid
% % % title(sprintf('%3i/%02i',ddd,hhh))
% % % % Hf=1./(1+(2.*pi.*0.53.*Fpics).^2);hold on;loglog(Fpics,Fpics.*SH2O_7200UC./Hf,'k','LineWidth',2);grid
% % % 
% % % [SH2O_7200UC,Fpic]=psd2(detrend(Rair),length(Rair),10,hamming(length(Rair)));[SH2O_7200UC,Fpics]=specsmoo(SH2O_7200UC,10);
% % % [SH2O2_7200UC,Fpic]=psd2(detrend(Rv7200),length(Rv7200),10,hamming(length(Rv7200)));[SH2O2_7200UC,Fpics]=specsmoo(SH2O2_7200UC,10);
% % % figure;loglog(Fpics,Fpics.*SH2O_7200UC,Fpics,Fpics.*SH2O2_7200UC,'r','LineWidth',2);grid
% % % 
% % % figure;plot(tref,LI7500_H2O,tref,H2O7500_UC,'r',tref,H2O7200_UC,'k')
% % % Xlagflag1 = findlag(H2O7200_UC,LI7500_H2O, 1000,1);
% % % Xlagflag2 = findlag(H2O7500_UC,LI7500_H2O, 1000,1);
% % % 
% % % % Xlagflag/10 + ClockDrift
% % % % Xlagflag = findlag(CO27500_UC,LI7500_CO2mf, 1000,1)
% % % figure;plot(tref,LI7500_H2O-nanmean1(LI7500_H2O),tref+Xlagflag2/10,H2O7500_UC-nanmean1(H2O7500_UC),'g');
% % % hold on;plot(tref+Xlagflag1/10,H2O7200_UC-nanmean1(H2O7200_UC),'k','LineWidth',2)
% % % legend('7500 PSD','7500 UC','7200 UC');grid
% % % 
% % % figure;plot(tref,LI7500_CO2mf-nanmean1(LI7500_CO2mf),tref+Xlagflag2/10,CO27500_UC-nanmean1(CO27500_UC),'g');
% % % hold on;plot(tref+Xlagflag1/10,CO27200_UC-nanmean1(CO27200_UC),'k','LineWidth',2)
% % % legend('7500 PSD','7500 UC','7200 UC');grid
% % % % figure(3);plot(tmotion,LI7200_CO2mf-nanmean1(LI7200_CO2mf),(Tlicor_UC)+Xlagflag/10,CO27200_UC-nanmean1(CO27200_UC),'r',(Tlicor_UC)+Xlagflag/10,CO27500_UC-nanmean1(CO27500_UC),'k',tmotion,LI7500_CO2mf-nanmean1(LI7500_CO2mf),'m')
% % % % legend('7200 PSD','7200 UC','7500 UC','7500 PSD');grid
% % % [SH2O_7500PSD,Fpic]=psd2(detrend(LI7500_CO2mf),length(LI7500_H2O),10,hamming(length(LI7500_H2O)));[SH2O_7500PSD,Fpics]=specsmoo(SH2O_7500PSD,10);
% % % [SH2O_7500UC,Fpic]=psd2(detrend(CO27500_UC),length(H2O7500_UC),10,hamming(length(H2O7500_UC)));[SH2O_7500UC,Fpics]=specsmoo(SH2O_7500UC,10);
% % % [SH2O_7200UC,Fpic]=psd2(detrend(CO27200_UC),length(H2O7200_UC),10,hamming(length(H2O7200_UC)));[SH2O_7200UC,Fpics]=specsmoo(SH2O_7200UC,10);
% % % figure;loglog(Fpics,Fpics.*SH2O_7500PSD,'g',Fpics,Fpics.*SH2O_7500UC,'m',Fpics,Fpics.*SH2O_7200UC,'r','LineWidth',2);grid
% % % title(sprintf('%3i/%02i',ddd,hhh))


%-------------------------------------------------------------------------
%>>>>>brings PSD 7500 H2O to PSD 7200 --useless as poor 7500 during HiWinGS2013
% but to keep it in in case...
%-------------------------------------------------------------------------
mr_att=TubingAttenuation(LI7500_H2O,10,0.4);  %brings UC 7200 H2O to PSD 7200
mr_att(1:30)=nanmedian1(mr_att);
mr_att2=[];
mr_att2(1:57)=mr_att(57+2);
mr_att2(57+1:36000)=mr_att(1:36000-57); %kg/kg  %delays H2O by 5.7s
clear mr_att

% figure;plot(tref,LI7500_CO2mf,'b',tref,LI7200_CO2mf,'g',tref,CO27200_UC,'m',tref,CO27500_UC,'r')

%-------------------------------------------------------------------------
% Decorr CO2 for motion, mainly accelerations
%-------------------------------------------------------------------------
% % % XX=LI7500_CO2mf; [SCO2_7200PSD,Fpic]=psd2(detrend(XX),length(XX),10,hamming(length(XX)));[SCO2_7200PSD,Fpics]=specsmoo(SCO2_7200PSD,10);%clear XX
% % % XXj=motiondecorr(XX,uvwplat);
% % % XXj=motiondecorr(XXj,xyzplat);
% % % XXj=motiondecorr(XXj,Plat);
% % % XXj=motiondecorr(XXj,euler);
% % % % LI7200_CO2mfdj=motiondecorr(LI7500_CO2mfd,Plat); 
% % % % LI7200_CO2mfdj=motiondecorr(wj',xyzplat); LI7200_CO2mfdj=motiondecorr(wj,euler); 
% % % 
% % % YY=XXj;[SCO2_7200PSDf,Fpic]=psd2(detrend(YY),length(YY),10,hamming(length(YY)));[SCO2_7200PSDf,Fpics]=specsmoo(SCO2_7200PSDf,10); %clear YY
% % % figure;loglog(Fpics,Fpics.*SCO2_7200PSD,'g',Fpics,Fpics.*SCO2_7200PSDf,'r','LineWidth',2);grid
% % % 
% % % YY=XXj;
% % % [CXX_i,Fpic_i]=csd2(detrend(wj),detrend(XX),length(XX),10,hamming(length(XX)));  [CXX,Fpics_i]=specsmoo(CXX_i,10);  
% % % [CYY_i,Fpic_i]=csd2(detrend(wj),detrend(YY),length(YY),10,hamming(length(YY)));  [CYY,Fpics_i]=specsmoo(CYY_i,10);   
% % % figure;semilogx(Fpics_i,Fpics_i.*CXX,'b',Fpics_i,Fpics_i.*CYY,'g','LineWidth',2);grid
% 
%  XX=CO2mfnew; [SCO2_7200PSD,Fpic]=psd2(detrend(XX),length(XX),10,hamming(length(XX)));[SCO2_7200PSD,Fpics]=specsmoo(SCO2_7200PSD,10);%clear XX
% XXj=motiondecorr(XX,Plat);XXj=motiondecorr(XXj,uvwplat);%XXj=motiondecorr(XXj,Pair7200_UC');
% YY=XXj;[SCO2_7200PSDf,Fpic]=psd2(detrend(YY),length(YY),10,hamming(length(YY)));[SCO2_7200PSDf,Fpics]=specsmoo(SCO2_7200PSDf,10); %clear YY
% figure;loglog(Fpics,Fpics.*SCO2_7200PSD,'g',Fpics,Fpics.*SCO2_7200PSDf,'r','LineWidth',2);grid
% [CXX_i,Fpic_i]=csd2(detrend(wj),detrend(XX),length(XX),10,hamming(length(XX)));  [CXX,Fpics_i]=specsmoo(CXX_i,10);
% [CYY_i,Fpic_i]=csd2(detrend(wj),detrend(YY),length(YY),10,hamming(length(YY)));  [CYY,Fpics_i]=specsmoo(CYY_i,10);
% figure;semilogx(Fpics_i,Fpics_i.*CXX,'b',Fpics_i,Fpics_i.*CYY,'g','LineWidth',2);grid
% %
% XX=LI7200_CO2mfd; range=rng7200PSD;
% [SCO2_7200PSD,Fpic]=psd2(detrend(XX(range)),length(XX(range)),10,hamming(length(XX(range))));[SCO2_7200PSD,Fpics]=specsmoo(SCO2_7200PSD,10);%clear XX
% XXj=motiondecorr(XX(range),Plat(:,rngw7200PSD));
% XXj=motiondecorr(XXj,uvwplat(:,rngw7200PSD));
% % XXj=motiondecorr(XX(range),Plat(:,range));
% % XXj=motiondecorr(XXj,uvwplat(:,range));
% YY=XXj;[SCO2_7200PSDf,Fpic]=psd2(detrend(YY),length(YY),10,hamming(length(YY)));[SCO2_7200PSDf,Fpics]=specsmoo(SCO2_7200PSDf,10); %clear YY
% figure;loglog(Fpics,Fpics.*SCO2_7200PSD,'b',Fpics,Fpics.*SCO2_7200PSDf,'g','LineWidth',2);grid
% [CXX_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(XX(range)),length(XX(range)),10,hamming(length(XX(range))));  [CXX,Fpics_i]=specsmoo(CXX_i,10);
% [CYY_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(YY),length(YY),10,hamming(length(YY)));  [CYY,Fpics_i]=specsmoo(CYY_i,10);
% figure;semilogx(Fpics_i,Fpics_i.*CXX,'b',Fpics_i,Fpics_i.*CYY,'g','LineWidth',2);grid

% test=CYY;ftest=Fpics_i;
% test(Fpics_i>=0.09 & Fpics_i<0.3)=NaN;
% CYY2=polyval(polyfit(Fpics_i(~isnan(test)),test(~isnan(test)),3),Fpics_i);
% figure;semilogx(Fpics_i,Fpics_i.*CXX,'b',Fpics_i,Fpics_i.*test,'g',Fpics_i,Fpics_i.*CYY2,'r','LineWidth',2);grid
% test(Fpics_i>=0.09 & Fpics_i<0.3)=CYY2(Fpics_i>=0.09 & Fpics_i<0.3);
% figure;semilogx(Fpics_i,Fpics_i.*CXX,'b',Fpics_i,Fpics_i.*CYY,'g',Fpics_i,Fpics_i.*test,'r','LineWidth',2);grid
% % CYYl= chopchop(0.06,0.3,Fpics_i,CYY,3);
% % figure(1);semilogx(Fpics_i,Fpics_i.*CXX,'b',Fpics_i,Fpics_i.*CYY,'g',Fpics_i,Fpics_i.*CYYl,'r','LineWidth',2);grid% 
% % 
% % [Cc,Fc,dfc]=specsmooludot([CYY_i],10);
% % wCYY=real(nansum(Cc.*dfc))
% % [Cc,Fc,dfc]=specsmooludot([CYYl_i],10);
% % wCYYl=real(nansum(Cc.*dfc))

% %PSD
% LI7200_CO2mf=motiondecorr(LI7200_CO2mf,Plat);
% LI7200_CO2mfd=motiondecorr(LI7200_CO2mfd,Plat);
% LI7200_H2O=motiondecorr(LI7200_H2O,Plat);
% LI7500_H2O=motiondecorr(LI7500_H2O,Plat);
% LI7500_CO2mf=motiondecorr(LI7500_CO2mf,Plat);
% %UC
% CO27200_UC=motiondecorr(CO27200_UC,Plat);
% CO27200_UCwebb=motiondecorr(CO27200_UCwebb,Plat);
% H2O7200_UC=motiondecorr(H2O7200_UC,Plat);
% CO27500_UC=motiondecorr(CO27500_UC,Plat);
% H2O7500_UC=motiondecorr(H2O7500_UC,Plat);

%-------------------------------------------------------------------------
Exx=6.1121*exp(17.502*TairL./(240.97+TairL));
Exx=Exx.*(1.0007+PairL*10*3.46E-6);
Qsat=.622*Exx./(PairL*10-.375*Exx)*1000;
rhfast7500_PSD=LI7500_H2O./Qsat;  
rhfast7200_PSD=LI7200_H2O./Qsat;  clear Qsat Exx

% rhoair = nanmedian1(PairL)*100/287/nanmedian1(Tair7500PTK_UC);                  %Density of dry air
rhoair = Rhoair;

%removes some NaN and replace by median
if ~isempty(find(isnan(wj))); wj(isnan(wj))=nanmedian1(wj);  end;
% if ~isempty(find(isnan(CO27200_UC ))); CO27200_UC (isnan(CO27200_UC ))=nanmedian1(CO27200_UC );  end;
% if ~isempty(find(isnan(CO27200_UCwebb))); CO27200_UCwebb(isnan(CO27200_UCwebb))=nanmedian1(CO27200_UCwebb);  end;
% if ~isempty(find(isnan(H2O7200_UC))); H2O7200_UC(isnan(H2O7200_UC))=nanmedian1(H2O7200_UC);  end;
% if ~isempty(find(isnan(CO27500_UC ))); CO27500_UC (isnan(CO27500_UC ))=nanmedian1(CO27500_UC );  end;
% if ~isempty(find(isnan(H2O7500_UC))); H2O7500_UC(isnan(H2O7500_UC))=nanmedian1(H2O7500_UC);  end;
if ~isempty(find(isnan(LI7200_CO2mf ))); LI7200_CO2mf (isnan(LI7200_CO2mf ))=nanmedian1(LI7200_CO2mf);  end;
if ~isempty(find(isnan(LI7200_CO2mfd ))); LI7200_CO2mfd (isnan(LI7200_CO2mfd ))=nanmedian1(LI7200_CO2mfd);  end;
if ~isempty(find(isnan(LI7200_H2O))); LI7200_H2O(isnan(LI7200_H2O))=nanmedian1(LI7200_H2O);  end;
if ~isempty(find(isnan(LI7500_CO2mf ))); LI7500_CO2mf (isnan(LI7500_CO2mf ))=nanmedian1(LI7500_CO2mf);  end;
if ~isempty(find(isnan(LI7500_H2O))); LI7500_H2O(isnan(LI7500_H2O))=nanmedian1(LI7500_H2O);  end;
if ~isempty(find(isnan(CO2mfnew))); CO2mfnew(isnan(CO2mfnew))=nanmedian1(CO2mfnew);  end;
if ~isempty(find(isnan(H2Odnew))); H2Odnew(isnan(H2Odnew))=nanmedian1(H2Odnew);  end;

%-------------------------------------------------------------------------
%start flux computations
%-------------------------------------------------------------------------
% Xlagflag = findlag(CO27500_UC,LI7500_CO2mf, 1600,0); %look at two 7500 CO2 to compute lag via xcov method
% ClockDrift=round(ClockDrift*10);
% Xlagflag=-Xlagflag;
PSD7200lag=57;

Nlendata=6000;


%allocate variables
rngw=[];rng7200UC=[];rng7200UCcov=[];rng7200PSD=[];
% wCO2_7200UC=ones(1,6)+NaN;wH2O_7200UC=ones(1,6)+NaN;wCO2_7200UCwebb=ones(1,6)+NaN; wCO2_7200UCcov=ones(1,6)+NaN;
% wCO2_7500UC=ones(1,6)+NaN;wH2O_7500UC=ones(1,6)+NaN;
wCO2_7200PSD=ones(1,6)+NaN;wH2O_7200PSD=ones(1,6)+NaN;
wCO2_7500PSD=ones(1,6)+NaN;wH2O_7500PSD=ones(1,6)+NaN;

% wCO2H2O_7200UC=ones(1,6)+NaN;
% wCO2H2O_7500UC=ones(1,6)+NaN;
wCO2H2O_7500PSD=ones(1,6)+NaN;
wCO2H2O_7200PSD=ones(1,6)+NaN;
wCO2H2O_7200PSD2=ones(1,6)+NaN;


% muco2h2o_7200UC=ones(1,6)+NaN;
% muco2h2o_7500UC=ones(1,6)+NaN;
muco2h2o_7500PSD=ones(1,6)+NaN;
muco2h2o_7200PSD=ones(1,6)+NaN;
% UCPSDdrift=ones(1,6)+NaN;
% slopeCO2_7200UC=ones(2,6)+NaN; stdCO2_7200UC=ones(1,6)+NaN;  meanCO2_7200UC=ones(1,6)+NaN;
% slopeCO2_7200UCcov=ones(2,6)+NaN;  stdCO2_7200UCcov=ones(1,6)+NaN;meanCO2_7200UCcov=ones(1,6)+NaN;
% slopeCO2_7200UCwebb=ones(2,6)+NaN; stdCO2_7200UCwebb=ones(1,6)+NaN;meanCO2_7200UCwebb=ones(1,6)+NaN;
% slopeH2O_7200UC=ones(2,6)+NaN; stdH2O_7200UC=ones(1,6)+NaN;  meanH2O_7200UC=ones(1,6)+NaN;
slopeCO2_7200PSD=ones(2,6)+NaN; stdCO2_7200PSD=ones(1,6)+NaN;  meanCO2_7200PSD=ones(1,6)+NaN;
slopeH2O_7200PSD=ones(2,6)+NaN; stdH2O_7200PSD=ones(1,6)+NaN;  meanH2O_7200PSD=ones(1,6)+NaN;
% slopeCO2_7500UC=ones(2,6)+NaN; stdCO2_7500UC=ones(1,6)+NaN;  meanCO2_7500UC=ones(1,6)+NaN;
% slopeH2O_7500UC=ones(2,6)+NaN; stdH2O_7500UC=ones(1,6)+NaN;  meanH2O_7500UC=ones(1,6)+NaN;
slopeCO2_7500PSD=ones(2,6)+NaN; stdCO2_7500PSD=ones(1,6)+NaN;  meanCO2_7500PSD=ones(1,6)+NaN;
slopeH2O_7500PSD=ones(2,6)+NaN; stdH2O_7500PSD=ones(1,6)+NaN;  meanH2O_7500PSD=ones(1,6)+NaN;

Fpic=ones(48,6)+NaN;
% SCO2_7200UC=ones(48,6)+NaN; CwCO2_7200UC=ones(48,6)+NaN;
% SCO2_7500UC=ones(48,6)+NaN; CwCO2_7500UC=ones(48,6)+NaN;
SCO2_7200PSD=ones(48,6)+NaN; CwCO2_7200PSD=ones(48,6)+NaN;
SCO2_7500PSD=ones(48,6)+NaN; CwCO2_7500PSD=ones(48,6)+NaN;
% SH2O_7200UC=ones(48,6)+NaN; CwH2O_7200UC=ones(48,6)+NaN;
% SH2O_7500UC=ones(48,6)+NaN; CwH2O_7500UC=ones(48,6)+NaN;
SH2O_7200PSD=ones(48,6)+NaN; CwH2O_7200PSD=ones(48,6)+NaN;
SH2O_7500PSD=ones(48,6)+NaN; CwH2O_7500PSD=ones(48,6)+NaN;

% rqc_7200UC=ones(1,6)+NaN;
rqc_7200PSD=ones(1,6)+NaN;
% rqc_7500UC=ones(1,6)+NaN;
rqc_7500PSD=ones(1,6)+NaN;
% wco2tson_7200UC=ones(1,6)+NaN;
% muco2tson_7200UC=ones(1,6)+NaN;


%added 12/2012
% wCO2PKT_7500UC=ones(1,6)+NaN;rhoc7500UCPKT=ones(1,6)+NaN;
% wCO2PKT_7200UC=ones(1,6)+NaN;rhoc7200UCPKT=ones(1,6)+NaN;
wCO2PKT_7500PSD=ones(1,6)+NaN;rhoc7500PSDPKT=ones(1,6)+NaN;
wCO2PKT_7200PSD=ones(1,6)+NaN;rhoc7200PSDPKT=ones(1,6)+NaN;
% CCO2H2O_7200UC=ones(48,6)+NaN;
% CCO2H2O_7500UC=ones(48,6)+NaN;
CCO2H2O_7500PSD=ones(48,6)+NaN;
CCO2H2O_7200PSD=ones(48,6)+NaN;
% wCO2JE0_7500UC=ones(1,6)+NaN;wCO2JE1_7500UC=ones(1,6)+NaN;gamPSDJE_7500UC=ones(1,6)+NaN;gamPKTJE_7500UC=ones(1,6)+NaN;
% wCO2JE0_7200UC=ones(1,6)+NaN;wCO2JE1_7200UC=ones(1,6)+NaN;gamPSDJE_7200UC=ones(1,6)+NaN;gamPKTJE_7200UC=ones(1,6)+NaN;
wCO2JE0_7500PSD=ones(1,6)+NaN;wCO2JE1_7500PSD=ones(1,6)+NaN;gamPSDJE_7500PSD=ones(1,6)+NaN;gamPKTJE_7500PSD=ones(1,6)+NaN;
wCO2JE0_7200PSD=ones(1,6)+NaN;wCO2JE1_7200PSD=ones(1,6)+NaN;gamPSDJE_7200PSD=ones(1,6)+NaN;gamPKTJE_7200PSD=ones(1,6)+NaN;
% Cqc7500UC=ones(48,6)+NaN;Scc7500UC=ones(48,6)+NaN;muw7500UC=ones(48,6)+NaN;
% Cqc7200UC=ones(48,6)+NaN;Scc7200UC=ones(48,6)+NaN;muw7200UC=ones(48,6)+NaN;
Cqc7500PSD=ones(48,6)+NaN;Scc7500PSD=ones(48,6)+NaN;muw7500PSD=ones(48,6)+NaN;
Cqc7200PSD=ones(48,6)+NaN;Scc7200PSD=ones(48,6)+NaN;muw7200PSD=ones(48,6)+NaN;
% C_H2O7200PSD_H2O7500UC=ones(48,6)+NaN;
C_H2O7200PSD_H2O7500PSD=ones(48,6)+NaN;
% C_H2O7200UC_H2O7500UC=ones(48,6)+NaN;

SCO2d_7200PSD=ones(48,6)+NaN; CwCO2d_7200PSD=ones(48,6)+NaN;
slopeCO2d_7200PSD=ones(2,6)+NaN; stdCO2d_7200PSD=ones(1,6)+NaN;  meanCO2d_7200PSD=ones(1,6)+NaN;
wCO2d_7200PSD=ones(1,6)+NaN;
% SCO2_7200UCwebb=ones(48,6)+NaN;CwCO2_7200UCwebb=ones(48,6)+NaN;
% wCO2PKTlb_7500UC=ones(1,6)+NaN;rhoc7500UCPKTlb=ones(1,6)+NaN;
% wCO2PKTlb_7200UC=ones(1,6)+NaN;rhoc7200UCPKTlb=ones(1,6)+NaN;
wCO2PKTlb_7500PSD=ones(1,6)+NaN;rhoc7500PSDPKTlb=ones(1,6)+NaN;
wCO2PKTlb_7200PSD=ones(1,6)+NaN;rhoc7200PSDPKTlb=ones(1,6)+NaN;

%added 01/12/2013
uCO2d_7200PSD=ones(1,6)+NaN;vCO2d_7200PSD=ones(1,6)+NaN;
uCO2_7200UC=ones(1,6)+NaN;vCO2_7200UC=ones(1,6)+NaN;
uCO2_7500PSD=ones(1,6)+NaN;vCO2_7500PSD=ones(1,6)+NaN;
uCO2_7500UC=ones(1,6)+NaN;vCO2_7500UC=ones(1,6)+NaN;
% CwCO2c7500UC=ones(48,6)+NaN;CwCO2c7200UC=ones(48,6)+NaN;   %Cwc after cross-talk removed (using gamma=0.88)
CwCO2c7500PSD=ones(48,6)+NaN;CwCO2c7200PSD=ones(48,6)+NaN;
Srhoc7200PSDPKT=ones(48,6)+NaN;Cwrhoc7200PSDPKT=ones(48,6)+NaN;  Srhoc7500PSDPKT=ones(48,6)+NaN;Cwrhoc7500PSDPKT=ones(48,6)+NaN;
% Srhoc7200UCPKT=ones(48,6)+NaN;Cwrhoc7200UCPKT=ones(48,6)+NaN;  Srhoc7500UCPKT=ones(48,6)+NaN;Cwrhoc7500UCPKT=ones(48,6)+NaN;
CCO2H2Or_7200PSD=ones(48,6)+NaN; %to compute mucc in frequency domain
CwCO2d_7200PSD_ogive=ones(48,6)+NaN;
CwCO2d_7200PSD_cumtrapz=ones(48,6)+NaN;
Cwtson=ones(48,6)+NaN;
Cwt_ogive=ones(48,6)+NaN;
Cwt_cumtrapz=ones(48,6)+NaN;
FwCO2d_7200PSD_ogive=ones(48,6)+NaN;
Fwt_ogive=ones(48,6)+NaN;

slopeCO2_7200PSDrecomp=ones(2,6)+NaN; stdCO2_7200PSDrecomp=ones(1,6)+NaN;  meanCO2_7200PSDrecomp=ones(1,6)+NaN;
slopeH2O_7200PSDrecomp=ones(2,6)+NaN; stdH2O_7200PSDrecomp=ones(1,6)+NaN;  meanH2O_7200PSDrecomp=ones(1,6)+NaN;
wCO2_7200PSDrecomp=ones(1,6)+NaN;wH2O_7200PSDrecomp=ones(1,6)+NaN;
SCO2_7200PSDrecomp=ones(48,6)+NaN; CwCO2_7200PSDrecomp=ones(48,6)+NaN;
SH2O_7200PSDrecomp=ones(48,6)+NaN; CwH2O_7200PSDrecomp=ones(48,6)+NaN;

%added 02/05/2013
% CTH2O_7500UC=ones(48,6)+NaN;  
% CTH2O_7200UC=ones(48,6)+NaN;
% STson_UC=ones(48,6)+NaN;
CTH2O_7500PSD=ones(48,6)+NaN;  
CTH2O_7200PSD=ones(48,6)+NaN;
STson_PSD=ones(48,6)+NaN;

% newmu7200UC=ones(48,6)+NaN;
% CwCO2_7200UCdecorr=ones(48,6)+NaN;
newmu7500PSD=ones(48,6)+NaN;
CwCO2_7500PSDdecorr=ones(48,6)+NaN;
% RqT7200UC=ones(48,6)+NaN;
RqT7500PSD=ones(48,6)+NaN;

CwCO2d_7200PSDi=ones(49,6)+NaN;  
wCO2d_7200PSDi=ones(1,6)+NaN;  
CwCO2d_7200PSDichop=ones(49,6)+NaN;  
wCO2d_7200PSDichop=ones(1,6)+NaN;  
    
    
for tt=00:5
%     %UC indices
%     if tt==0
%         rng7200UC=Nlendata*tt+1:Nlendata*(tt+1)-abs(ClockDrift); rngw7200UC=Nlendata*tt+1+abs(ClockDrift):Nlendata*(tt+1);
%         rng7200UCcov=Nlendata*tt+1:Nlendata*(tt+1)-abs(Xlagflag);rngw7200UCcov=Nlendata*tt+1+abs(Xlagflag):Nlendata*(tt+1);
%     else
%         rng7200UC=Nlendata*tt+1-abs(ClockDrift):Nlendata*(tt+1)-abs(ClockDrift); rngw7200UC=Nlendata*tt+1:Nlendata*(tt+1);
%         rng7200UCcov=Nlendata*tt+1-abs(Xlagflag):Nlendata*(tt+1)-abs(Xlagflag);  rngw7200UCcov=Nlendata*tt+1:Nlendata*(tt+1);
%     end
        
    %PSD indices    
    if tt==5
        rng7200PSD=Nlendata*tt+1+abs(PSD7200lag):Nlendata*6;  rngw7200PSD=Nlendata*tt+1:Nlendata*tt+length(rng7200PSD);
    else
        rng7200PSD=Nlendata*tt+1+abs(PSD7200lag):Nlendata*(tt+1)+abs(PSD7200lag);rngw7200PSD=Nlendata*tt+1:Nlendata*(tt+1);
    end        

    if hhh==00 & tt==0   %get rids of puff spike in cov computation
        rng7200PSD(1:600)=[];     rngw7200PSD(1:600)=[];
    end 
 
    %%%%%%%%%%%% decorr CO2 and motion %%%%%%%%%%%%%%%%%
    %PSD
%     LI7200_CO2mf(rng7200PSD)=motiondecorr(LI7200_CO2mf(rng7200PSD),Plat(:,rngw7200PSD));  %probably should not be lagged as motion effects instantaneous on sensor...
    LI7200_CO2mf(rng7200PSD)=motiondecorr(LI7200_CO2mf(rng7200PSD),Plat(:,rng7200PSD));  %probably should not be lagged as motion effects instantaneous on sensor...
    LI7200_CO2mfd(rng7200PSD)=motiondecorr(LI7200_CO2mfd(rng7200PSD),Plat(:,rng7200PSD));
    LI7200_H2O(rng7200PSD)=motiondecorr(LI7200_H2O(rng7200PSD),Plat(:,rng7200PSD));
    LI7500_H2O(rngw7200PSD)=motiondecorr(LI7500_H2O(rngw7200PSD),Plat(:,rngw7200PSD));
    LI7500_CO2mf(rngw7200PSD)=motiondecorr(LI7500_CO2mf(rngw7200PSD),Plat(:,rngw7200PSD));
%     LI7200_CO2mf(rng7200PSD)=motiondecorr(LI7200_CO2mf(rng7200PSD),uvwplat(:,rngw7200PSD));
    LI7200_CO2mf(rng7200PSD)=motiondecorr(LI7200_CO2mf(rng7200PSD),uvwplat(:,rng7200PSD));
    LI7200_CO2mfd(rng7200PSD)=motiondecorr(LI7200_CO2mfd(rng7200PSD),uvwplat(:,rng7200PSD));
    LI7200_H2O(rng7200PSD)=motiondecorr(LI7200_H2O(rng7200PSD),uvwplat(:,rngw7200PSD));
    LI7500_H2O(rngw7200PSD)=motiondecorr(LI7500_H2O(rngw7200PSD),uvwplat(:,rngw7200PSD));
    LI7500_CO2mf(rngw7200PSD)=motiondecorr(LI7500_CO2mf(rngw7200PSD),uvwplat(:,rngw7200PSD));
    %UC
%     CO27200_UC(rng7200UC)=motiondecorr(CO27200_UC(rng7200UC),Plat(:,rngw7200UC));
%     CO27200_UCwebb(rng7200UC)=motiondecorr(CO27200_UCwebb(rng7200UC),Plat(:,rngw7200UC));
%     H2O7200_UC(rng7200UC)=motiondecorr(H2O7200_UC(rng7200UC),Plat(:,rngw7200UC));
%     CO27500_UC(rng7200UC)=motiondecorr(CO27500_UC(rng7200UC),Plat(:,rngw7200UC));
%     H2O7500_UC(rng7200UC)=motiondecorr(H2O7500_UC(rng7200UC),Plat(:,rngw7200UC));
%     CO27200_UC(rng7200UC)=motiondecorr(CO27200_UC(rng7200UC),uvwplat(:,rngw7200UC));
%     CO27200_UCwebb(rng7200UC)=motiondecorr(CO27200_UCwebb(rng7200UC),uvwplat(:,rngw7200UC));
%     H2O7200_UC(rng7200UC)=motiondecorr(H2O7200_UC(rng7200UC),uvwplat(:,rngw7200UC));
%     CO27500_UC(rng7200UC)=motiondecorr(CO27500_UC(rng7200UC),uvwplat(:,rngw7200UC));
%     H2O7500_UC(rng7200UC)=motiondecorr(H2O7500_UC(rng7200UC),uvwplat(:,rngw7200UC));


%     figure;
%     subplot(2,1,1);     
%         plot(tmotion(rng7200UC),CO27200_UC(rng7200UC)-nanmean1(CO27200_UC(rng7200UC)),tmotion(rngw7200UC),wj(rngw7200UC),'g--');grid;xlim([tmotion(Nlendata*tt+1) tmotion(Nlendata*(tt+1))])
%     subplot(2,1,2);     
%         plot(tmotion(rng7200PSD),LI7200_CO2mf(rng7200PSD)-nanmean1(LI7200_CO2mf(rng7200PSD)),tmotion(rngw7200PSD),wj(rngw7200PSD),'g--');grid;xlim([tmotion(Nlendata*tt+1) tmotion(Nlendata*(tt+1))])
% 
%     figure;
%     subplot(2,1,1);     
%         plot(CO27200_UC(rng7200UC)-nanmean1(CO27200_UC(rng7200UC)));hold on;plot(wj(rngw7200UC),'g--');grid;
%     subplot(2,1,2); 
%         plot(LI7200_CO2mf(rng7200PSD)-nanmean1(LI7200_CO2mf(rng7200PSD)));hold on;plot(wj(rngw7200PSD),'g--');grid;
%     
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(CO27200_UC(rng7200UC)));        wCO2_7200UC(tt+1)=coj1(1,2); %uses ClockDrift
%         coj1=nancov1(detrend(uvwmat(rngw7200UCcov,4)), detrend(CO27200_UC(rng7200UCcov)));        wCO2_7200UCcov(tt+1)=coj1(1,2);  %uses Xlagflag
%         coj1=nancov1(detrend(wj(rngw7200UC)), detrend(CO27200_UCwebb(rng7200UC)));    wCO2_7200UCwebb(tt+1)=coj1(1,2); %uses webb corrected CO2 as in contrast with dry output
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(H2O7200_UC(rng7200UC)));        wH2O_7200UC(tt+1)=coj1(1,2);
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(CO27500_UC(rng7200UC)));        wCO2_7500UC(tt+1)=coj1(1,2);
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(H2O7500_UC(rng7200UC)));        wH2O_7500UC(tt+1)=coj1(1,2);
%         coj1=nancov1(detrend(uj(rngw7200UC)), detrend(CO27200_UC(rng7200UC)));        uCO2_7200UC(tt+1)=coj1(1,2);
%         coj1=nancov1(detrend(vj(rngw7200UC)), detrend(CO27200_UC(rng7200UC)));        vCO2_7200UC(tt+1)=coj1(1,2);
%         coj1=nancov1(detrend(uj(rngw7200UC)), detrend(CO27500_UC(rng7200UC)));        uCO2_7500UC(tt+1)=coj1(1,2); 
%         coj1=nancov1(detrend(vj(rngw7200UC)), detrend(CO27500_UC(rng7200UC)));        vCO2_7500UC(tt+1)=coj1(1,2); 
    
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(LI7200_CO2mf(rng7200PSD)));    wCO2_7200PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(LI7200_CO2mfd(rng7200PSD)));    wCO2d_7200PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(CO2mfnew(rng7200PSD)));    wCO2_7200PSDrecomp(tt+1)=coj1(1,2);
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(LI7200_H2O(rng7200PSD)));      wH2O_7200PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(H2Odnew(rng7200PSD)));      wH2O_7200PSDrecomp(tt+1)=coj1(1,2);
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(LI7500_CO2mf(rngw7200PSD)));   wCO2_7500PSD(tt+1)=coj1(1,2);
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(LI7500_H2O(rngw7200PSD)));     wH2O_7500PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(uj(rngw7200PSD)), detrend(LI7200_CO2mfd(rng7200PSD)));    uCO2d_7200PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(vj(rngw7200PSD)), detrend(LI7200_CO2mfd(rng7200PSD)));    vCO2d_7200PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(uj(rngw7200PSD)), detrend(LI7500_CO2mf(rngw7200PSD)));    uCO2_7500PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(vj(rngw7200PSD)), detrend(LI7500_CO2mf(rngw7200PSD)));    vCO2_7500PSD(tt+1)=coj1(1,2);

    
%      coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(Rc7500PTK_UC(rng7200PSD)*29/44));
%      [wCO2d_7200PSD(tt+1) wCO2_7200UC(tt+1) wCO2_7500UC(tt+1)  wCO2_7500PSD(tt+1) coj1(1,2)]

    % [rhoc1] = PKTcode(Rv7500_UC(rng7200UC),rhfast7500_UC(rng7200UC),Rc7500_UC(rng7200UC),wj(rngw7200UC));
    % coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhoc1*29/44));coj1(1,2)
    % [rhocPTK1] = PKTcode(Rv7500_UC(rng7200UC),rhfast7500_UC(rng7200UC),Rc7500PTK_UC(rng7200UC),wj(rngw7200UC));  %not much difference.
    % coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhocPTK1*29/44));coj1(1,2)
    % rhov=rhoair.*Rv7500_UC(rng7200UC);rhfast=rhfast7500_UC(rng7200UC);rhoc=rhoair.*Rc7500_UC(rng7200UC);W=wj(rngw7200UC);

    %%uses PKT approach
%     [rhoc7500UCJE] = PKTcodeJE(rhoair.*Rv7500_UC(rng7200UC),rhfast7500_UC(rng7200UC),rhoair.*Rc7500_UC(rng7200UC),wj(rngw7200UC));
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhoc7500UC./rhoair*29/44));     wCO2PKT_7500UC(tt+1)=coj1(1,2);
%     rhoc7500UCPKT(tt+1)=nanmean1(rhoc7500UC./rhoair*29/44); %ppm
%     [rhoc7200UCJE] = PKTcodeJE(rhoair.*Rv7200_UC(rng7200UC),rhfast7200PTK_UC(rng7200UC),rhoair.*Rc7200_UC(rng7200UC),wj(rngw7200UC));
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhoc7200UCJE./rhoair*29/44));     wCO2PKT_7200UC(tt+1)=coj1(1,2);
%     rhoc7200UCPKT(tt+1)=nanmean1(rhoc7200UC./rhoair*29/44); %ppm
%     [rhoc7500PSDJE] = PKTcodeJE(rhoair*LI7500_H2O(rngw7200PSD),rhfast7500_PSD(rngw7200PSD),rhoair.*LI7500_CO2mf(rngw7200PSD)*44/29,wj(rngw7200PSD));
%     coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(rhoc7500PSD./rhoair*29/44));   wCO2PKT_7500PSD(tt+1)=coj1(1,2);
%     rhoc7500PSDPKT(tt+1)=nanmean1(rhoc7500PSD./rhoair*29/44); %ppm
%     [rhoc7200PSDJE] = PKTcodeJE(rhoair*LI7200_H2O(rngw7200PSD),rhfast7200_PSD(rngw7200PSD),rhoair.*LI7200_CO2mf(rngw7200PSD)*44/29,wj(rngw7200PSD));
%     coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(rhoc7200PSD./rhoair*29/44));   wCO2PKT_7200PSD(tt+1)=coj1(1,2);
%     rhoc7200PSDPKT(tt+1)=nanmean1(rhoc7200PSD./rhoair*29/44);     %ppm
%     
%     [rhoc7500UC] = PKTcode(H2O7500_UC(rng7200UC)'/1000,rhfast7500_UC(rng7200UC)*100,CO27500_UC(rng7200UC)'/1e6,wj(rngw7200UC),rhoair);
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhoc7500UC));     wCO2PKT_7500UC(tt+1)=coj1(1,2);
%     rhoc7500UCPKT(tt+1)=nanmean1(rhoc7500UC); %ppm
%     [rhoc7200UC] = PKTcode(H2O7200_UC(rng7200UC)'/1000,rhfast7200PTK_UC(rng7200UC)*100,CO27200_UC(rng7200UC)'/1e6,wj(rngw7200UC),rhoair);
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhoc7200UC));     wCO2PKT_7200UC(tt+1)=coj1(1,2);
%     rhoc7200UCPKT(tt+1)=nanmean1(rhoc7200UC); %ppm
    [rhoc7500PSD] = PKTcode(LI7500_H2O(rngw7200PSD)/1000,rhfast7500_PSD(rngw7200PSD)*100,LI7500_CO2mf(rngw7200PSD)/1e6,wj(rngw7200PSD),rhoair);
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(rhoc7500PSD));   wCO2PKT_7500PSD(tt+1)=coj1(1,2);
    rhoc7500PSDPKT(tt+1)=nanmean1(rhoc7500PSD); %ppm
    [rhoc7200PSD] = PKTcode(LI7200_H2O(rngw7200PSD)/1000,rhfast7200_PSD(rngw7200PSD)*100,LI7200_CO2mfd(rngw7200PSD)/1e6,wj(rngw7200PSD),rhoair);
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(rhoc7200PSD));   wCO2PKT_7200PSD(tt+1)=coj1(1,2);
    rhoc7200PSDPKT(tt+1)=nanmean1(rhoc7200PSD);     %ppm
    
%     [rhoc7500UClb] = PKTcode_lb(H2O7500_UC(rng7200UC)'/1000,rhfast7500_UC(rng7200UC)*100,CO27500_UC(rng7200UC)'/1e6,wj(rngw7200UC),rhoair);
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhoc7500UClb));     wCO2PKTlb_7500UC(tt+1)=coj1(1,2);
%     rhoc7500UCPKTlb(tt+1)=nanmean1(rhoc7500UClb); %ppm
%     [rhoc7200UClb] = PKTcode_lb(H2O7200_UC(rng7200UC)'/1000,rhfast7200PTK_UC(rng7200UC)*100,CO27200_UC(rng7200UC)'/1e6,wj(rngw7200UC),rhoair);
%     coj1=nancov1(detrend(wj(rngw7200UC)), detrend(rhoc7200UClb));     wCO2PKTlb_7200UC(tt+1)=coj1(1,2);
%     rhoc7200UCPKTlb(tt+1)=nanmean1(rhoc7200UClb); %ppm
    [rhoc7500PSDlb] = PKTcode_lb(LI7500_H2O(rngw7200PSD)/1000,rhfast7500_PSD(rngw7200PSD)*100,LI7500_CO2mf(rngw7200PSD)/1e6,wj(rngw7200PSD),rhoair);
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(rhoc7500PSDlb));   wCO2PKTlb_7500PSD(tt+1)=coj1(1,2);
    rhoc7500PSDPKTlb(tt+1)=nanmean1(rhoc7500PSDlb); %ppm
    [rhoc7200PSDlb] = PKTcode_lb(LI7200_H2O(rngw7200PSD)/1000,rhfast7200_PSD(rngw7200PSD)*100,LI7200_CO2mfd(rngw7200PSD)/1e6,wj(rngw7200PSD),rhoair);
    coj1=nancov1(detrend(wj(rngw7200PSD)), detrend(rhoc7200PSDlb));   wCO2PKTlb_7200PSD(tt+1)=coj1(1,2);
    rhoc7200PSDPKTlb(tt+1)=nanmean1(rhoc7200PSDlb);     %ppm
    
    %% New 7/16/2012 - add some flux for H2O decorrelation approach to 7500 and UC 7200 Licors
%         coj1=nancov1(detrend(CO27200_UC(rng7200UC)), detrend(H2O7200_UC(rng7200UC)));        wCO2H2O_7200UC(tt+1)=coj1(1,2);
%         coj1=nancov1(detrend(CO27500_UC(rng7200UC)), detrend(H2O7500_UC(rng7200UC)));        wCO2H2O_7500UC(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(LI7500_CO2mf(rngw7200PSD)), detrend(LI7500_H2O(rngw7200PSD)));   wCO2H2O_7500PSD(tt+1)=coj1(1,2);
        coj1=nancov1(detrend(LI7200_CO2mfd(rng7200PSD)), detrend(LI7200_H2O(rng7200PSD)));   wCO2H2O_7200PSD2(tt+1)=coj1(1,2);

        coj1=nancov1(mr_att2(rngw7200PSD),LI7200_CO2mfd(rng7200PSD));   wCO2H2O_7200PSD(tt+1)=coj1(1,2);
        muco2h2o_7200PSD(tt+1) = coj1(1,2)/coj1(1,1);    
        rqc_7200PSD(tt+1)=coj1(1,2)./(sqrt(coj1(1,1).*coj1(2,2)));  %correlation
            
%         if length(rng7200UC)>length(rng7200PSD)
%             coj1=nancov1(mr_att2(rng7200UC(1:length(rng7200PSD))),LI7200_CO2mfd(rng7200PSD));   wCO2H2O_7200PSD(tt+1)=coj1(1,2);
%             muco2h2o_7200PSD(tt+1) = coj1(1,2)/coj1(1,1);
%             rqc_7200PSD(tt+1)=coj1(1,2)./(sqrt(coj1(1,1).*coj1(2,2)));  %correlation
%         elseif length(rng7200UC)<length(rng7200PSD)
%             coj1=nancov1(mr_att2(rng7200UC),LI7200_CO2mfd(rng7200PSD(1:length(rng7200UC))));   wCO2H2O_7200PSD(tt+1)=coj1(1,2);
%             muco2h2o_7200PSD(tt+1) = coj1(1,2)/coj1(1,1);
%             rqc_7200PSD(tt+1)=coj1(1,2)./(sqrt(coj1(1,1).*coj1(2,2)));  %correlation
%         else
%             coj1=nancov1(mr_att2(rng7200UC),LI7200_CO2mfd(rng7200PSD));   wCO2H2O_7200PSD(tt+1)=coj1(1,2);
%             muco2h2o_7200PSD(tt+1) = coj1(1,2)/coj1(1,1);    
%             rqc_7200PSD(tt+1)=coj1(1,2)./(sqrt(coj1(1,1).*coj1(2,2)));  %correlation
%         end
        %% New 7/16/2012 - add H2O decorrelation to 7500 and UC 7200 Licors
%     Cua = nancov1(detrend(CO27200_UC(rng7200UC)),detrend(H2O7200_UC(rng7200UC)));
%         muco2h2o_7200UC(tt+1) = Cua(1,2)/Cua(2,2);
%         rqc_7200UC(tt+1)=Cua(1,2)./(sqrt(Cua(1,1).*Cua(2,2)));  %correlation
%     Cua = nancov1(detrend(CO27500_UC(rng7200UC)),detrend(H2O7500_UC(rng7200UC)));
%         muco2h2o_7500UC(tt+1) = Cua(1,2)/Cua(2,2);
%         rqc_7500UC(tt+1)=Cua(1,2)./(sqrt(Cua(1,1).*Cua(2,2)));  %correlation
     Cua = nancov1(detrend(LI7500_CO2mf(rngw7200PSD)),detrend(LI7500_H2O(rngw7200PSD)));
        muco2h2o_7500PSD(tt+1) = Cua(1,2)/Cua(2,2);
        rqc_7500PSD(tt+1)=Cua(1,2)./(sqrt(Cua(1,1).*Cua(2,2)));  %correlation
    
        %% add some other components
%     coj1=nancov1(detrend(CO27200_UC(rng7200UC)), detrend(Tsonic(rngw7200UC)));        wco2tson_7200UC(tt+1)=coj1(1,2); %uses ClockDrift
%     muco2tson_7200UC(tt+1) = coj1(1,2)/coj1(2,2);
        
    %computes criteria to determine and reject unstationary conditions
%     slopeCO2_7200UC(:,tt+1)=polyfit(rng7200UC-rng7200UC(1),CO27200_UC(rng7200UC)-nanmean1(CO27200_UC(rng7200UC)),1);       stdCO2_7200UC(tt+1)=nanstd1(CO27200_UC(rng7200UC));   meanCO2_7200UC(tt+1)=nanmean1(CO27200_UC(rng7200UC));
%     [slopeCO2_7200UC(:,tt+1), stdCO2_7200UC(tt+1), meanCO2_7200UC(tt+1)] = stationcrit (rng7200UC, CO27200_UC);
%     [slopeCO2_7200UCcov(:,tt+1), stdCO2_7200UCcov(tt+1), meanCO2_7200UCcov(tt+1)] = stationcrit (rng7200UCcov, CO27200_UC);
%     [slopeCO2_7200UCwebb(:,tt+1), stdCO2_7200UCwebb(tt+1), meanCO2_7200UCwebb(tt+1)] = stationcrit (rng7200UC, CO27200_UCwebb);
%     [slopeH2O_7200UC(:,tt+1), stdH2O_7200UC(tt+1), meanH2O_7200UC(tt+1)] = stationcrit (rng7200UC, H2O7200_UC);
%     [slopeCO2_7500UC(:,tt+1), stdCO2_7500UC(tt+1),  meanCO2_7500UC(tt+1)] = stationcrit (rng7200UC,CO27500_UC);
%     [slopeH2O_7500UC(:,tt+1), stdH2O_7500UC(tt+1),  meanH2O_7500UC(tt+1)] = stationcrit (rng7200UC,H2O7500_UC);
    [slopeCO2_7200PSD(:,tt+1), stdCO2_7200PSD(tt+1),  meanCO2_7200PSD(tt+1)] = stationcrit (rng7200PSD, LI7200_CO2mf);   
    [slopeCO2d_7200PSD(:,tt+1), stdCO2d_7200PSD(tt+1),  meanCO2d_7200PSD(tt+1)] = stationcrit (rng7200PSD, LI7200_CO2mfd);  
    [slopeH2O_7200PSD(:,tt+1), stdH2O_7200PSD(tt+1),  meanH2O_7200PSD(tt+1)] = stationcrit (rng7200PSD, LI7200_H2O);
    [slopeCO2_7500PSD(:,tt+1), stdCO2_7500PSD(tt+1),  meanCO2_7500PSD(tt+1)] = stationcrit (rngw7200PSD,LI7500_CO2mf);
    [slopeH2O_7500PSD(:,tt+1), stdH2O_7500PSD(tt+1),  meanH2O_7500PSD(tt+1)] = stationcrit (rngw7200PSD,LI7500_H2O);
    [slopeCO2_7200PSDrecomp(:,tt+1), stdCO2_7200PSDrecomp(tt+1),  meanCO2_7200PSDrecomp(tt+1)] = stationcrit (rng7200PSD, CO2mfnew);  
    [slopeH2O_7200PSDrecomp(:,tt+1), stdH2O_7200PSDrecomp(tt+1),  meanH2O_7200PSDrecomp(tt+1)] = stationcrit (rng7200PSD, H2Odnew);

       
    %%%%%%%%%%%%%%%%%%%%%%%    now computes specta/cospectra   %%%%%%%%%%%%%%%%%%%%%%%   
%     %UC
%     [SCO2_7200UC_i,Fpic_i]=psd2(detrend(CO27200_UC(rng7200UC)),length(CO27200_UC(rng7200UC)),10,hamming(length(CO27200_UC(rng7200UC))));   
%         [P,D]=specsmoo(SCO2_7200UC_i,10);  %fix changing spec size from 48 to 47 to...       
%     [SCO2_7200UC(1:length(P),tt+1),Fpics_i]=specsmoo(SCO2_7200UC_i,10);
%         [SCO2_7200UCwebb_i,Fpic_i]=psd2(detrend(CO27200_UCwebb(rng7200UC)),length(CO27200_UC(rng7200UC)),10,hamming(length(CO27200_UC(rng7200UC))));   [SCO2_7200UCwebb(1:length(P),tt+1),Fpics_i]=specsmoo(SCO2_7200UCwebb_i,10);
%     [CwCO2_7200UC_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(CO27200_UC(rng7200UC)),length(CO27200_UC(rng7200UC)),10,hamming(length(CO27200_UC(rng7200UC))));  [CwCO2_7200UC(1:length(P),tt+1),Fpics_i]=specsmoo(CwCO2_7200UC_i,10);   
%     [CwCO2_7200UCwebb_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(CO27200_UCwebb(rng7200UC)),length(CO27200_UC(rng7200UC)),10,hamming(length(CO27200_UC(rng7200UC))));  [CwCO2_7200UCwebb(1:length(P),tt+1),Fpics_i]=specsmoo(CwCO2_7200UCwebb_i,10);   
% 
%      Fpic(1:length(P),tt+1)=Fpics_i;
%     [SH2O_7200UC_i,Fpic_i]=psd2(detrend(H2O7200_UC(rng7200UC)),length(H2O7200_UC(rng7200UC)),10,hamming(length(H2O7200_UC(rng7200UC))));   [SH2O_7200UC(1:length(P),tt+1),Fpics_i]=specsmoo(SH2O_7200UC_i,10);
%     [CwH2O_7200UC_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(H2O7200_UC(rng7200UC)),length(H2O7200_UC(rng7200UC)),10,hamming(length(H2O7200_UC(rng7200UC))));  [CwH2O_7200UC(1:length(P),tt+1),Fpics_i]=specsmoo(CwH2O_7200UC_i,10);   
%     [SCO2_7500UC_i,Fpic_i]=psd2(detrend(CO27500_UC(rng7200UC)),length(CO27500_UC(rng7200UC)),10,hamming(length(CO27500_UC(rng7200UC))));   [SCO2_7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(SCO2_7500UC_i,10);
%     [CwCO2_7500UC_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(CO27500_UC(rng7200UC)),length(CO27500_UC(rng7200UC)),10,hamming(length(CO27500_UC(rng7200UC))));  [CwCO2_7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(CwCO2_7500UC_i,10);   
%     [SH2O_7500UC_i,Fpic_i]=psd2(detrend(H2O7500_UC(rng7200UC)),length(H2O7500_UC(rng7200UC)),10,hamming(length(H2O7500_UC(rng7200UC))));   [SH2O_7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(SH2O_7500UC_i,10);
%     [CwH2O_7500UC_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(H2O7500_UC(rng7200UC)),length(H2O7500_UC(rng7200UC)),10,hamming(length(H2O7500_UC(rng7200UC))));  [CwH2O_7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(CwH2O_7500UC_i,10);   
%     %uses spectral crosstalk approach
%     [CCO2H2O_7200UC_i,Fpic_i]=csd2(detrend(CO27200_UC(rng7200UC)),detrend(H2O7200_UC(rng7200UC)),length(H2O7200_UC(rng7200UC)),10,hamming(length(H2O7200_UC(rng7200UC))));  [CCO2H2O_7200UC(1:length(P),tt+1),Fpics_i]=specsmoo(CCO2H2O_7200UC_i,10);   
%     [CCO2H2O_7500UC_i,Fpic_i]=csd2(detrend(CO27500_UC(rng7200UC)),detrend(H2O7500_UC(rng7200UC)),length(H2O7500_UC(rng7200UC)),10,hamming(length(H2O7500_UC(rng7200UC))));  [CCO2H2O_7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(CCO2H2O_7500UC_i,10);   
%         [CTH2O_7200UC_i,Fpic_i]=csd2(detrend(Tsonic(rngw7200UC)),detrend(H2O7200_UC(rng7200UC)),length(H2O7200_UC(rng7200UC)),10,hamming(length(H2O7200_UC(rng7200UC))));  [CTH2O_7200UC(1:length(P),tt+1),Fpics_i]=specsmoo(CTH2O_7200UC_i,10);   
%         [CTH2O_7500UC_i,Fpic_i]=csd2(detrend(Tsonic(rngw7200UC)),detrend(H2O7500_UC(rng7200UC)),length(H2O7500_UC(rng7200UC)),10,hamming(length(H2O7500_UC(rng7200UC))));  [CTH2O_7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(CTH2O_7500UC_i,10);   
%         [STson_i,Fpic_i]=psd2(detrend(Tsonic(rng7200UC)),length(Tsonic(rng7200UC)),10,hamming(length(Tsonic(rng7200UC))));   [STson_UC(1:length(P),tt+1),Fpics_i]=specsmoo(STson_i,10);
% 
%     %Computes corresponding fluxes from spectral crosstalk approach   
%     [wCO2JE0_7500UC(tt+1),wCO2JE1_7500UC(tt+1),gamPSDJE_7500UC(tt+1),gamPKTJE_7500UC(tt+1),Cqc7500UC(1:length(P),tt+1),Scc7500UC(1:length(P),tt+1),muw7500UC(1:length(P),tt+1),CwCO2c7500UC(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7500UC_i,SH2O_7500UC_i,CwCO2_7500UC_i,CwH2O_7500UC_i,wCO2d_7200PSD(tt+1),wCO2PKT_7500UC(tt+1),SCO2_7500UC_i,1.11);    
% %     [wCO2JE0_7200UC(tt+1),wCO2JE1_7200UC(tt+1),gamPSDJE_7200UC(tt+1),gamPKTJE_7200UC(tt+1),Cqc7200UC(1:length(P),tt+1),Scc7200UC(1:length(P),tt+1),muw7200UC(1:length(P),tt+1),CwCO2c7200UC(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7200UC_i,SH2O_7200UC_i,CwCO2_7200UC_i,CwH2O_7200UC_i,wCO2d_7200PSD(tt+1),wCO2PKT_7200UC(tt+1),SCO2_7200UC_i,0.93);    
% %     [wCO2JE0_7500UC(tt+1),wCO2JE1_7500UC(tt+1),gamPSDJE_7500UC(tt+1),gamPKTJE_7500UC(tt+1),Cqc7500UC(1:length(P),tt+1),Scc7500UC(1:length(P),tt+1),muw7500UC(1:length(P),tt+1),CwCO2c7500UC(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7500UC_i,SH2O_7500UC_i,CwCO2_7500UC_i,CwH2O_7500UC_i,wCO2d_7200PSD(tt+1),wCO2PKT_7500UC(tt+1),SCO2_7500UC_i);    
% %     [wCO2JE0_7200UC(tt+1),wCO2JE1_7200UC(tt+1),gamPSDJE_7200UC(tt+1),gamPKTJE_7200UC(tt+1),Cqc7200UC(1:length(P),tt+1),Scc7200UC(1:length(P),tt+1),muw7200UC(1:length(P),tt+1),CwCO2c7200UC(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7200UC_i,SH2O_7200UC_i,CwCO2_7200UC_i,CwH2O_7200UC_i,wCO2d_7200PSD(tt+1),wCO2PKT_7200UC(tt+1),SCO2_7200UC_i);    
% %uses w'q' from 7500 mast as 7200 mast attenuated. Not sure why yet...    
% %also correct for attenuation (0.5s)
%     [CwH2O_7500PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(LI7500_H2O(rngw7200UC)),length(LI7500_H2O(rngw7200UC)),10,hamming(length(LI7500_H2O(rngw7200UC))));  
%  Hf=1./(1+(2.*pi.*0.53.*Fpic_i).^2);
% [wCO2JE0_7200UC(tt+1),wCO2JE1_7200UC(tt+1),gamPSDJE_7200UC(tt+1),gamPKTJE_7200UC(tt+1),Cqc7200UC(1:length(P),tt+1),Scc7200UC(1:length(P),tt+1),muw7200UC(1:length(P),tt+1),CwCO2c7200UC(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7200UC_i,SH2O_7200UC_i./Hf,CwCO2_7200UC_i,CwH2O_7500PSD_i,wCO2d_7200PSD(tt+1),wCO2PKT_7200UC(tt+1),SCO2_7200UC_i,0.93);    
% 
% 
%     %to look at response spectrum
%     [C_H2O7200UC_H2O7500UC_i,Fpic_i]=csd2(detrend(H2O7200_UC(rng7200UC)),detrend(H2O7500_UC(rng7200UC)),length(H2O7200_UC(rng7200UC)),10,hamming(length(H2O7200_UC(rng7200UC))));  [C_H2O7200UC_H2O7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(C_H2O7200UC_H2O7500UC_i,10);   
%        
%     %computes spec/cospec of PKT approach
%     [Srhoc7500UC_i,Fpic_i]=psd2(detrend(rhoc7500UC),length(rhoc7500UC),10,hamming(length(rhoc7500UC)));   [Srhoc7500UCPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Srhoc7500UC_i,10);
%     [Cwrhoc7500UC_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(rhoc7500UC),length(rhoc7500UC),10,hamming(length(rhoc7500UC)));  [Cwrhoc7500UCPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Cwrhoc7500UC_i,10);   
%     [Srhoc7200UC_i,Fpic_i]=psd2(detrend(rhoc7200UC),length(rhoc7200UC),10,hamming(length(rhoc7200UC)));   [Srhoc7200UCPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Srhoc7200UC_i,10);
%     [Cwrhoc7200UC_i,Fpic_i]=csd2(detrend(wj(rngw7200UC)),detrend(rhoc7200UC),length(rhoc7200UC),10,hamming(length(rhoc7200UC)));  [Cwrhoc7200UCPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Cwrhoc7200UC_i,10);   
%    
    
    %PSD
    [SCO2_7200PSD_i,Fpic_i]=psd2(detrend(LI7200_CO2mf(rng7200PSD)),length(LI7200_CO2mf(rng7200PSD)),10,hamming(length(LI7200_CO2mf(rng7200PSD))));   
        [P,D]=specsmoo(SCO2_7200PSD_i,10);
    [SCO2_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(SCO2_7200PSD_i,10);
    [SCO2d_7200PSD_i,Fpic_i]=psd2(detrend(LI7200_CO2mfd(rng7200PSD)),length(LI7200_CO2mfd(rng7200PSD)),10,hamming(length(LI7200_CO2mfd(rng7200PSD))));   [SCO2d_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(SCO2d_7200PSD_i,10);
    [CwCO2d_7200PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(LI7200_CO2mfd(rng7200PSD)),length(LI7200_CO2mfd(rng7200PSD)),10,hamming(length(LI7200_CO2mfd(rng7200PSD))));  [CwCO2d_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CwCO2d_7200PSD_i,10);   
    %11/02 - LB - add chopchop to get rid of some weird spikes in spectra
    %coming from sonic w'...vibration? Couldn't get rid of it with decorr
    %or Edson
    [Cc,Fc,dfc]=specsmooludot([CwCO2d_7200PSD_i],10);wc0=real(nansum(Cc.*dfc)); CwCO2d_7200PSDi(1:length(Cc),tt+1)=Cc; wCO2d_7200PSDi(tt+1)=wc0;
    [Y] = chopchop(5e-2,9e-2,Fc,Cc,1); wc1=real(nansum(Y.*dfc)); CwCO2d_7200PSDichop(1:length(Y),tt+1)=Y; wCO2d_7200PSDichop(tt+1)=wc1  ;
 
    [CwCO2_7200PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(LI7200_CO2mf(rng7200PSD)),length(LI7200_CO2mf(rng7200PSD)),10,hamming(length(LI7200_CO2mf(rng7200PSD))));  [CwCO2_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CwCO2_7200PSD_i,10);   
    [SH2O_7200PSD_i,Fpic_i]=psd2(detrend(LI7200_H2O(rng7200PSD)),length(LI7200_H2O(rng7200PSD)),10,hamming(length(LI7200_H2O(rng7200PSD))));   [SH2O_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(SH2O_7200PSD_i,10);
    [CwH2O_7200PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(LI7200_H2O(rng7200PSD)),length(LI7200_H2O(rng7200PSD)),10,hamming(length(LI7200_H2O(rng7200PSD))));  [CwH2O_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CwH2O_7200PSD_i,10);   
    [SCO2_7500PSD_i,Fpic_i]=psd2(detrend(LI7500_CO2mf(rngw7200PSD)),length(LI7500_CO2mf(rngw7200PSD)),10,hamming(length(LI7500_CO2mf(rngw7200PSD))));   [SCO2_7500PSD(1:length(P),tt+1),Fpics_i]=specsmoo(SCO2_7500PSD_i,10);
    [CwCO2_7500PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(LI7500_CO2mf(rngw7200PSD)),length(LI7500_CO2mf(rngw7200PSD)),10,hamming(length(LI7500_CO2mf(rngw7200PSD))));  [CwCO2_7500PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CwCO2_7500PSD_i,10);   
    [SH2O_7500PSD_i,Fpic_i]=psd2(detrend(LI7500_H2O(rngw7200PSD)),length(LI7500_H2O(rngw7200PSD)),10,hamming(length(LI7500_H2O(rngw7200PSD))));   [SH2O_7500PSD(1:length(P),tt+1),Fpics_i]=specsmoo(SH2O_7500PSD_i,10);
    [CwH2O_7500PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(LI7500_H2O(rngw7200PSD)),length(LI7500_H2O(rngw7200PSD)),10,hamming(length(LI7500_H2O(rngw7200PSD))));  [CwH2O_7500PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CwH2O_7500PSD_i,10);   
    
    [SCO2_7200PSD_recompi,Fpic_i]=psd2(detrend(CO2mfnew(rng7200PSD)),length(CO2mfnew(rng7200PSD)),10,hamming(length(CO2mfnew(rng7200PSD))));   [SCO2_7200PSDrecomp(1:length(P),tt+1),Fpics_i]=specsmoo(SCO2_7200PSD_recompi,10);
    [CwCO2_7200PSD_recompi,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(CO2mfnew(rng7200PSD)),length(CO2mfnew(rng7200PSD)),10,hamming(length(CO2mfnew(rng7200PSD))));  [CwCO2_7200PSDrecomp(1:length(P),tt+1),Fpics_i]=specsmoo(CwCO2_7200PSD_recompi,10);   
    [SH2O_7200PSD_recompi,Fpic_i]=psd2(detrend(H2Odnew(rng7200PSD)),length(H2Odnew(rng7200PSD)),10,hamming(length(H2Odnew(rng7200PSD))));   [SH2O_7200PSDrecomp(1:length(P),tt+1),Fpics_i]=specsmoo(SH2O_7200PSD_recompi,10);
    [CwH2O_7200PSD_recompi,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(H2Odnew(rng7200PSD)),length(H2Odnew(rng7200PSD)),10,hamming(length(H2Odnew(rng7200PSD))));  [CwH2O_7200PSDrecomp(1:length(P),tt+1),Fpics_i]=specsmoo(CwH2O_7200PSD_recompi,10);   

    
    %uses spectral crosstalk approach     
    [CCO2H2O_7500PSD_i,Fpic_i]=csd2(detrend(LI7500_CO2mf(rngw7200PSD)),detrend(LI7500_H2O(rngw7200PSD)),length(LI7500_H2O(rngw7200PSD)),10,hamming(length(LI7500_H2O(rngw7200PSD))));  [CCO2H2O_7500PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CCO2H2O_7500PSD_i,10);   
    [CCO2H2O_7200PSD_i,Fpic_i]=csd2(detrend(LI7200_CO2mfd(rngw7200PSD)),detrend(LI7200_H2O(rngw7200PSD)),length(LI7200_H2O(rngw7200PSD)),10,hamming(length(LI7200_H2O(rngw7200PSD))));  [CCO2H2O_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CCO2H2O_7200PSD_i,10);   
        [CTH2O_7500PSD_i,Fpic_i]=csd2(detrend(Tsonic(rngw7200PSD)),detrend(LI7500_H2O(rngw7200PSD)),length(LI7500_H2O(rngw7200PSD)),10,hamming(length(LI7500_H2O(rngw7200PSD))));  [CTH2O_7500PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CTH2O_7500PSD_i,10);   
        [CTH2O_7200PSD_i,Fpic_i]=csd2(detrend(Tsonic(rngw7200PSD)),detrend(LI7200_H2O(rngw7200PSD)),length(LI7200_H2O(rngw7200PSD)),10,hamming(length(LI7200_H2O(rngw7200PSD))));  [CTH2O_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CTH2O_7200PSD_i,10);   
        [STson_i,Fpic_i]=psd2(detrend(Tsonic(rngw7200PSD)),length(Tsonic(rngw7200PSD)),10,hamming(length(Tsonic(rngw7200PSD))));   [STson_PSD(1:length(P),tt+1),Fpics_i]=specsmoo(STson_i,10);

    %to compute mucc in freqeuncy domain
%     if length(rng7200UC)>length(rng7200PSD)          
%         [CCO2H2Or_7200PSD_i,Fpic_i]=csd2(detrend(LI7200_CO2mfd(rng7200PSD)),detrend(mr_att2(rng7200UC(1:length(rng7200PSD)))),length(LI7200_CO2mfd(rng7200PSD)),10,hamming(length(LI7200_CO2mfd(rng7200PSD))));  
%         [CCO2H2Or_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CCO2H2Or_7200PSD_i,10);
%     elseif length(rng7200UC)<length(rng7200PSD) 
% %         P=ones(1,47)+NaN;
%         [CCO2H2Or_7200PSD_i,Fpic_i]=csd2(detrend(LI7200_CO2mfd(rng7200PSD(1:length(rng7200UC)))),detrend(mr_att2(rng7200UC)),length(LI7200_CO2mfd(rng7200PSD(1:length(rng7200UC)))),10,hamming(length(LI7200_CO2mfd(rng7200PSD(1:length(rng7200UC))))));  
% %         [CCO2H2Or_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CCO2H2Or_7200PSD_i,10);
%         [CCO2H2Or_7200PSD(1:length(specsmoo(CCO2H2Or_7200PSD_i,10)),tt+1),Fpics_i]=specsmoo(CCO2H2Or_7200PSD_i,10);
%     else
        [CCO2H2Or_7200PSD_i,Fpic_i]=csd2(detrend(LI7200_CO2mfd(rng7200PSD)),detrend(mr_att2(rngw7200PSD)),length(LI7200_CO2mfd(rng7200PSD)),10,hamming(length(LI7200_CO2mfd(rng7200PSD))));  
        [CCO2H2Or_7200PSD(1:length(P),tt+1),Fpics_i]=specsmoo(CCO2H2Or_7200PSD_i,10);
%     end
        
        Fpic(1:length(P),tt+1)=Fpics_i;         
    %Computes corresponding fluxes from spectral crosstalk approach   
    [wCO2JE0_7500PSD(tt+1),wCO2JE1_7500PSD(tt+1),gamPSDJE_7500PSD(tt+1),gamPKTJE_7500PSD(tt+1),Cqc7500PSD(1:length(P),tt+1),Scc7500PSD(1:length(P),tt+1),muw7500PSD(1:length(P),tt+1),CwCO2c7500PSD(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7500PSD_i,SH2O_7500PSD_i,CwCO2_7500PSD_i,CwH2O_7500PSD_i,wCO2d_7200PSD(tt+1),wCO2PKT_7500PSD(tt+1),SCO2_7500PSD_i,1.15);    
    [wCO2JE0_7200PSD(tt+1),wCO2JE1_7200PSD(tt+1),gamPSDJE_7200PSD(tt+1),gamPKTJE_7200PSD(tt+1),Cqc7200PSD(1:length(P),tt+1),Scc7200PSD(1:length(P),tt+1),muw7200PSD(1:length(P),tt+1),CwCO2c7200PSD(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7200PSD_i,SH2O_7200PSD_i,CwCO2d_7200PSD_i,CwH2O_7200PSD_i,wCO2d_7200PSD(tt+1),wCO2PKT_7200PSD(tt+1),SCO2d_7200PSD_i,0.88);    
%     [wCO2JE0_7500PSD(tt+1),wCO2JE1_7500PSD(tt+1),gamPSDJE_7500PSD(tt+1),gamPKTJE_7500PSD(tt+1),Cqc7500PSD(1:length(P),tt+1),Scc7500PSD(1:length(P),tt+1),muw7500PSD(1:length(P),tt+1),CwCO2c7500PSD(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7500PSD_i,SH2O_7500PSD_i,CwCO2_7500PSD_i,CwH2O_7500PSD_i,wCO2d_7200PSD(tt+1),wCO2PKT_7500PSD(tt+1),SCO2_7500PSD_i);    
%     [wCO2JE0_7200PSD(tt+1),wCO2JE1_7200PSD(tt+1),gamPSDJE_7200PSD(tt+1),gamPKTJE_7200PSD(tt+1),Cqc7200PSD(1:length(P),tt+1),Scc7200PSD(1:length(P),tt+1),muw7200PSD(1:length(P),tt+1),CwCO2c7200PSD(1:length(P),tt+1)]=SpectralCrosstalk(CCO2H2O_7200PSD_i,SH2O_7200PSD_i,CwCO2d_7200PSD_i,CwH2O_7200PSD_i,wCO2d_7200PSD(tt+1),wCO2PKT_7200PSD(tt+1),SCO2d_7200PSD_i);    

    %to look at response spectrum
%     [C_H2O7200PSD_H2O7500UC_i,Fpic_i]=csd2(detrend(LI7200_H2O(rngw7200UC)),detrend(H2O7500_UC(rngw7200UC)),length(LI7200_H2O(rng7200PSD)),10,hamming(length(LI7200_H2O(rng7200PSD)))); [C_H2O7200PSD_H2O7500UC(1:length(P),tt+1),Fpics_i]=specsmoo(C_H2O7200PSD_H2O7500UC_i,10);   
    [C_H2O7200PSD_H2O7500PSD_i,Fpic_i]=csd2(detrend(LI7200_H2O(rngw7200PSD)),detrend(LI7500_H2O(rngw7200PSD)),length(LI7200_H2O(rng7200PSD)),10,hamming(length(LI7200_H2O(rng7200PSD)))); [C_H2O7200PSD_H2O7500PSD(1:length(P),tt+1),Fpics_i]=specsmoo(C_H2O7200PSD_H2O7500PSD_i,10);   

        %computes spec/cospec of PKT approach
    [Srhoc7200PSD_i,Fpic_i]=psd2(detrend(rhoc7200PSD),length(rhoc7200PSD),10,hamming(length(rhoc7200PSD)));   [Srhoc7200PSDPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Srhoc7200PSD_i,10);
    [Cwrhoc7200PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(rhoc7200PSD),length(rhoc7200PSD),10,hamming(length(rhoc7200PSD)));  [Cwrhoc7200PSDPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Cwrhoc7200PSD_i,10);   
    [Srhoc7500PSD_i,Fpic_i]=psd2(detrend(rhoc7500PSD),length(rhoc7500PSD),10,hamming(length(rhoc7500PSD)));   [Srhoc7500PSDPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Srhoc7500PSD_i,10);
    [Cwrhoc7500PSD_i,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(rhoc7500PSD),length(rhoc7500PSD),10,hamming(length(rhoc7500PSD)));  [Cwrhoc7500PSDPKT(1:length(P),tt+1),Fpics_i]=specsmoo(Cwrhoc7500PSD_i,10);   

    %computes Ogive
    [Ct,Fl]=Ogive(CwCO2d_7200PSD_i,fs);%similar to test=cumtrapz(Fpic_i,CwCO2d_7200PSD_i);
    CwCO2d_7200PSD_ogive(1:length(Ct),tt+1)=Ct;  FwCO2d_7200PSD_ogive(1:length(Ct),tt+1)=Fl;  
    [Ctemp,Ftemp]=specsmoo(cumtrapz(Fpic_i,CwCO2d_7200PSD_i),10);
    CwCO2d_7200PSD_cumtrapz(1:length(P),tt+1)=Ctemp; 
    
     [Cwti,Fpic_i]=csd2(detrend(wj(rngw7200PSD)),detrend(Tsonic(rngw7200PSD)),length(Tsonic(rngw7200PSD)),10,hamming(length(Tsonic(rngw7200PSD))));  [Cwtson(1:length(P),tt+1),Fpics_i]=specsmoo(Cwti,10);   
     [Ct,Fl]=Ogive(Cwti,fs);  %similar to test=cumtrapz(Fpic_i,CwCO2d_7200PSD_i);
    Cwt_ogive(1:length(Ct),tt+1)=Ct;  Fwt_ogive(1:length(Ct),tt+1)=Fl; 
    [Ctemp,Ftemp]=specsmoo(cumtrapz(Fpic_i,Cwti),10);
    Cwt_cumtrapz(1:length(P),tt+1)=Ctemp;

    % 02/12/2013
    %add computation from A8 in Edson 2011 here instead at the mean level
    %and see...
    
    load('C:\Data\CRUISES\HiWinGS_2013\7200\Rtq_functions.mat');

%     RqT7200UC=(CTH2O_7200UC.*CTH2O_7200UC)./(STson_UC.*SH2O_7200UC);
%     RqT7200UC=RqTf7200UC';
%     newmu7200UC(1:48,tt+1)=(CCO2H2O_7200UC(:,tt+1)./SH2O_7200UC(:,tt+1))-sqrt(RqT7200UC.*SCO2_7200UC(:,tt+1)./SH2O_7200UC(:,tt+1));
%     CwCO2_7200UCdecorr(1:48,tt+1)=CwCO2_7200UC(:,tt+1)-newmu7200UC(:,tt+1).*CwH2O_7200UC(:,tt+1);
%  RqT7200UC(1:48,tt+1)=(CTH2O_7200UC(:,tt+1).*CTH2O_7200UC(:,tt+1))./(STson_UC(:,tt+1).*SH2O_7200UC(:,tt+1));
%     RqT7500PSD=(CTH2O_7500PSD.*CTH2O_7500PSD)./(STson_PSD.*SH2O_7500PSD);
    RqT7500PSD=RqTf7500PSD';
    newmu7500PSD(1:48,tt+1)=(CCO2H2O_7500PSD(:,tt+1)./SH2O_7500PSD(:,tt+1))-sqrt(RqT7500PSD.*SCO2_7500PSD(:,tt+1)./SH2O_7500PSD(:,tt+1));
    CwCO2_7500PSDdecorr(1:48,tt+1)=CwCO2_7500PSD(:,tt+1)-newmu7500PSD(:,tt+1).*CwH2O_7500PSD(:,tt+1);
 RqT7500PSD(1:48,tt+1)=(CTH2O_7500PSD(:,tt+1).*CTH2O_7500PSD(:,tt+1))./(STson_PSD(:,tt+1).*SH2O_7500PSD(:,tt+1));
    
    
    
    
% disp('### uncorrected flux:   7200PSD       7200UC       7500UC       7500PSD       ###')
% [wCO2_7200PSD(tt+1) wCO2_7200UC(tt+1) wCO2_7500UC(tt+1)  wCO2_7500PSD(tt+1)]    
% disp('### PKT corrected flux:   7200PSD       7200UC       7500UC       7500PSD       ###')
% [wCO2_7200PSD(tt+1) wCO2PKT_7200UC(tt+1) wCO2PKT_7500UC(tt+1)  wCO2PKT_7500PSD(tt+1)]    
% disp('### JE corrected flux:   7200PSD       7200UC       7500UC       7500PSD       ###')
% [wCO2_7200PSD(tt+1) wCO2JE1_7200UC(tt+1) wCO2JE1_7500UC(tt+1)  wCO2JE1_7500PSD(tt+1)]    


% figure;semilogx(Fpics_i,Fpics_i.*CwCO2_7200UC(:,tt+1)','r',Fpics_i,Fpics_i.*CwCO2_7200PSD(:,tt+1)','g',Fpics_i,Fpics_i.*CwCO2_7500UC(:,tt+1)','m',Fpics_i,Fpics_i.*CwCO2_7500PSD(:,tt+1)','b');legend('7200 UC','7200 PSD','7500 UC','7500 PSD');grid
% [wCO2_7200UC(tt+1) wCO2_7500UC(tt+1) wCO2_7200PSD(tt+1) wCO2_7500PSD(tt+1) wCO2_7200UCwebb(tt+1)]
% [wCO2_7200UC'  wCO2_7500UC'  wCO2_7200PSD'  wCO2_7500PSD'  wCO2_7200UCwebb' ]
% figure;plot(Tlicor_UC+Xlagflag/10, H2O7500_UC./rho-nanmean1(H2O7500_UC./rho),'k',Tlicor_UC+Xlagflag/10, H2O7200_UC./rho-nanmean1(H2O7200_UC./rho),'b',tmotion,LI7500_H2O-nanmean1(LI7500_H2O),'m',tmotion,LI7200_H2O-nanmean1(LI7200_H2O),'r',[1:length(qvais)]*60,qvais-nanmean1(qvais),'g');legend('7200 UC','7200 PSD','7500 UC','7500 PSD','TRH');grid
end    

%-------------------------------------------------------------------------
%save results
%-------------------------------------------------------------------------


newview=[(ddd+hhh/24+(00:10:50)/24/60)' ...
    wCO2_7200PSD' wH2O_7200PSD' wCO2_7500PSD' wH2O_7500PSD' ...
    slopeCO2_7200PSD'  stdCO2_7200PSD'   meanCO2_7200PSD'  ...
    slopeH2O_7200PSD'  stdH2O_7200PSD'   meanH2O_7200PSD'  ...
    slopeCO2_7500PSD'  stdCO2_7500PSD'   meanCO2_7500PSD'  ...
    slopeH2O_7500PSD'  stdH2O_7500PSD'   meanH2O_7500PSD'  ...
    LI7200_AGC_PSD_10m' LI7200_AGC_PSD_10s' LI7500_AGC_10m' LI7500_AGC_10s'...
    ones(6,1)*length(Licor_CO2) ones(6,1)*length(LI7200_CO2mf) ...
    wCO2PKT_7500PSD' rhoc7500PSDPKT' ...
    wCO2PKT_7200PSD' rhoc7200PSDPKT' ...
    wCO2JE0_7500PSD' wCO2JE1_7500PSD' gamPSDJE_7500PSD' gamPKTJE_7500PSD' ... 
    wCO2JE0_7200PSD' wCO2JE1_7200PSD' gamPSDJE_7200PSD' gamPKTJE_7200PSD' ...
    slopeCO2d_7200PSD'  stdCO2d_7200PSD'   meanCO2d_7200PSD'  wCO2d_7200PSD' ...
    wCO2PKTlb_7500PSD' rhoc7500PSDPKTlb' ...
    wCO2PKTlb_7200PSD' rhoc7200PSDPKTlb' ...
    uCO2d_7200PSD' vCO2d_7200PSD' ...%added 01/12/2013
    uCO2_7500PSD' vCO2_7500PSD' ...
    slopeCO2_7200PSDrecomp' stdCO2_7200PSDrecomp' meanCO2_7200PSDrecomp' ...
    slopeH2O_7200PSDrecomp' stdH2O_7200PSDrecomp' meanH2O_7200PSDrecomp'...
    wCO2_7200PSDrecomp' wH2O_7200PSDrecomp' ...
    muco2h2o_7500PSD'  muco2h2o_7200PSD' ...
    wCO2H2O_7500PSD' wCO2H2O_7200PSD'...
    rqc_7200PSD' rqc_7500PSD' wCO2H2O_7200PSD2'...
    wCO2d_7200PSDi' wCO2d_7200PSDichop'];

specview=[(ddd+hhh/24+(00:10:50)/24/60)' Fpic' ...
            SCO2_7200PSD' CwCO2_7200PSD' ...
            SCO2_7500PSD' CwCO2_7500PSD' ...
            SH2O_7200PSD' CwH2O_7200PSD' ...
            SH2O_7500PSD' CwH2O_7500PSD' ... 
            CCO2H2O_7500PSD' ... 
            CCO2H2O_7200PSD' ... 
            Cqc7500PSD' Scc7500PSD' muw7500PSD' ... 
            Cqc7200PSD' Scc7200PSD' muw7200PSD' ... 
            C_H2O7200PSD_H2O7500PSD' ...
            SCO2d_7200PSD' CwCO2d_7200PSD' ...
            CwCO2c7500PSD' CwCO2c7200PSD' ...
            Srhoc7200PSDPKT' Cwrhoc7200PSDPKT' Srhoc7500PSDPKT' Cwrhoc7500PSDPKT' ...
            CCO2H2Or_7200PSD' ... %to compute mucc in frequency domain
            CwCO2d_7200PSD_ogive' FwCO2d_7200PSD_ogive' CwCO2d_7200PSD_cumtrapz' Cwtson' Cwt_ogive' Fwt_ogive' Cwt_cumtrapz' ...
            SCO2_7200PSDrecomp' CwCO2_7200PSDrecomp' SH2O_7200PSDrecomp' CwH2O_7200PSDrecomp' ...
            CTH2O_7500PSD' CTH2O_7200PSD' STson_PSD'...
            RqT7500PSD'  newmu7500PSD'  CwCO2_7500PSDdecorr'...
            CwCO2d_7200PSDi' CwCO2d_7200PSDichop'];

%  dcnames='_vCO2decorr3';  %with gam fluctuating
% dcnames='_vCO2decorr1';  %with gam fixed
dcnames='_UH_vCO2decorr1';  %with gam fixed

%         dcnames='_vCO2decorr_noWebbon7500mast';
sdg=['save ', 'C:\Data\CRUISES\HiWinGS_2013\7200\flux\CO2flux',jd hr,dcname,dcnames, '.prn newview -ascii'];
disp(sdg);eval(sdg)

sdg=['save ', 'C:\Data\CRUISES\HiWinGS_2013\7200\flux\CO2spec',jd hr,dcname,dcnames, '.prn specview -ascii'];
disp(sdg);eval(sdg)




clear newview specview
clear AGC                    Lenlic                 dcfsdata               rng7200UCcov           
clear AGC7200_UC             Licor_CO2                                  rngw                   
clear AGC7200_UC_10m         Licrate                degrate                rngw7200PSD            
clear AGC7200_UC_10s         Lonmax                 degrateD               rngw7200UC             
clear AGC7500_UC             Lonmin                 dfl                    rngw7200UCcov          
clear AGC7500_UC_10m         Mdec                   dfx                    roll                   
clear AGC7500_UC_10s         Nlendata               directory              rollA                  
clear AGCclosed              P                      dmp                    rollC                  
clear C                      PSD7200lag             dr                     rollD                  
clear C2K                    PSDdetT                                  rollT                  
clear CO2                    Pair                                   sN                     
clear CO27200_UC             PairC                  e                      sW                     
clear CO27200_UCwebb         PairM                                    sdg                    
clear CO27200dry             Press                  fid                    ship                   
clear CO27200drywebb         Qms                    filename               slopeCO2_7200PSD       
clear CO27500                Qsatms                 filestub               slopeCO2_7200UC        
clear CO27500_UC             Qsatsea                flicor                 slopeCO2_7200UCcov     
clear CO2Cmf                 RH                     flist                  slopeCO2_7200UCwebb    
clear CO2CmfP                RH1                    flow                   slopeCO2_7500PSD       
clear CO2abs                 RH2                    fsonic                 slopeCO2_7500UC        
clear CO2cal                 Rain                   g                      slopeH2O_7200PSD       
clear CO2calC                Rgas                   gyro                   slopeH2O_7200UC        
clear CO2closed              Rgas_universal                             slopeH2O_7500PSD       
clear CO2dry                 Rhoair                 hr                     slopeH2O_7500UC        
clear CO2mf                  Rhvais                 hrmn                   sog                    
clear CO2mfP                 Roll                   i                      solar                  
clear COG                    Rt                     ii                     specview               
clear CS                     SCO2_7200PSD           indi                   status                 
clear ClockDrift             SCO2_7200PSD_i         ir                     stdCO2_7200PSD         
clear Comp                   SCO2_7200UC            iv                     stdCO2_7200UC          
clear CwCO2_7200PSD          SCO2_7200UC_i          jd                     stdCO2_7200UCcov       
clear CwCO2_7200PSD_i        SCO2_7500PSD           jd1                    stdCO2_7200UCwebb      
clear CwCO2_7200UC           SCO2_7500PSD_i         jd_pc                  stdCO2_7500PSD         
clear CwCO2_7200UC_i         SCO2_7500UC            k1                     stdCO2_7500UC          
clear CwCO2_7500PSD          SCO2_7500UC_i          k2                     stdH2O_7200PSD         
clear CwCO2_7500PSD_i        SH2O_7200PSD           latdeg                 stdH2O_7200UC          
clear CwCO2_7500UC           SH2O_7200PSD_i         latmin                 stdH2O_7500PSD         
clear CwCO2_7500UC_i         SH2O_7200UC            stdH2O_7500UC          
clear CwH2O_7200PSD          SH2O_7200UC_i          licor_prefix           sun                    
clear CwH2O_7200PSD_i        SH2O_7500PSD           londeg                 t7200                  
clear CwH2O_7200UC           SH2O_7500PSD_i         lonmin                 ta1                    
clear CwH2O_7200UC_i         SH2O_7500UC                                  ta2                    
clear CwH2O_7500PSD          SH2O_7500UC_i          mean1                  tair                   
clear CwH2O_7500PSD_i        SOG                    meanCO2_7200PSD        tair1                  
clear CwH2O_7500UC           SST                    meanCO2_7200UC         tair2                  
clear CwH2O_7500UC_i         Solar                  meanCO2_7200UCcov      tairC                  
clear D                      T                      meanCO2_7200UCwebb     ti                     
clear Flow                   T1                     meanCO2_7500PSD        tmotion                
clear Flow7200               T2                     meanCO2_7500UC         tsea                   
clear Flow7200_UC            TDCFS                  meanH2O_7200PSD        tsonic                 
clear Flow7200_UC_10m        TGPS                   meanH2O_7200UC         tt                     
clear Flow7200_UC_10s        Tair                   meanH2O_7500PSD        uN                     
clear Fpic                   TairC                  meanH2O_7500UC         uW                     
clear Fpic_i                 TairM                  monstr                 uvwmat                 
clear Fpics_i                Tairk                  month                  wCO2_7200PSD           
clear GPStime                Tcompass               mtemp                  wCO2_7200UC            
clear Gpc                    Tcompass_UC            ncol                   wCO2_7200UCcov         
clear Gyro                   Tlicor                 newview                wCO2_7200UCwebb        
clear Gyrochk                TlicorC                nmang                  wCO2_7500PSD           
clear H                      Tlicor_UC              org                    wCO2_7500UC            
clear H2O                    Tmisc                  outputsonic            wH2O_7200PSD           
clear H2O7200                Tseak                  oz                     wH2O_7200UC            
clear H2O7200_UC             Tsens                  pa1                    wH2O_7500PSD           
clear H2O7500                Tson                   pa2                    wH2O_7500UC            
clear H2O7500_UC             Tvais                  pair                        
clear H2OCmd                 Tvaisala               pair1                   
clear H2OCmdP                Tvirt                  pair2                  
clear H2Oabs                 U                      pairC                     
clear H2Ocal                 UN                     pir                    
clear H2OcalC                UW                     pitch                    
clear H2Oclosed              Ur                     pitchA                   
clear H2Odry                 Ut                     pitchD                 
clear H2Omd                  V                      pitchT                 
clear H2OmdP                                   platform              
clear ID                     W                      platformD              
clear IR                     Webb                   ppp                      
clear LI7200_AGC_PSD_10m     Wspd                   press                   
clear LI7200_AGC_PSD_10s     Xlagflag               printit                      
clear LI7200_CO2d            af                     ps                        
clear LI7200_CO2mf           agc                    psp                       
clear LI7200_CO2mfd          ans                    qvais                   
clear LI7200_H2O             b4                     r1                     
clear LI7200_H2Od            b5                     r2                        
clear LI7200_Headpress       b6                     r3                     wdir                   
clear LI7200_Tdew            b7                     r4                     wj                     
clear LI7200_Tin             battV                  r5                     wspd                   
clear LI7200_Tout            bf                     rates                  xx                     
clear LI7200_agc             chk                    rdir                   y                      
clear LI7500_AGC_10m                         relwspd                yday                   
clear LI7500_AGC_10s         cog                                                          
clear LI7500_CO2mf           coj1                   rh1                                     
clear LI7500_CO2mf2          comp                   rh2                    zqs                    
clear LI7500_H2O                              rho                    zts                    
clear LL                                          rhov1                  zus                    
clear Lacie_drive            data_drive             rhov2                  zy                     
clear Latmax                 data_drive_reprocess   rng7200PSD             zz                     
clear Latmin                 day                    rng7200UC              
clear wCO2PKT_7500UC  rhoc7500UCPKT  
clear wCO2PKT_7200UC  rhoc7200UCPKT  
clear wCO2PKT_7500PSD  rhoc7500PSDPKT  
clear wCO2PKT_7200PSD  rhoc7200PSDPKT  
clear CCO2H2O_7200UC  
clear CCO2H2O_7500UC  
clear CCO2H2O_7500PSD  
clear CCO2H2O_7200PSD  
clear wCO2JE0_7500UC  wCO2JE1_7500UC  gamPSDJE_7500UC  gamPKTJE_7500UC  
clear wCO2JE0_7200UC  wCO2JE1_7200UC  gamPSDJE_7200UC  gamPKTJE_7200UC  
clear wCO2JE0_7500PSD  wCO2JE1_7500PSD  gamPSDJE_7500PSD  gamPKTJE_7500PSD  
clear wCO2JE0_7200PSD  wCO2JE1_7200PSD  gamPSDJE_7200PSD  gamPKTJE_7200PSD  
clear Cqc7500UC  Scc7500UC  muw7500UC  
clear Cqc7200UC  Scc7200UC  muw7200UC  
clear Cqc7500PSD  Scc7500PSD  muw7500PSD  
clear Cqc7200PSD  Scc7200PSD  muw7200PSD  
clear C_H2O7200PSD_H2O7500UC  C_H2O7200PSD_H2O7500PSD  
clear C_H2O7200UC_H2O7500UC  

clear CwCO2c7500UC CwCO2c7200UC CwCO2c7500PSD CwCO2c7200PSD   Srhoc7200PSDPKT Cwrhoc7200PSDPKT Srhoc7500PSDPKT Cwrhoc7500PSDPKT
clear Srhoc7200UCPKT Cwrhoc7200UCPKT Srhoc7500UCPKT Cwrhoc7500UCPKT
clear CCO2H2Or_7200PSD  CwCO2d_7200PSD_ogive CwCO2d_7200PSD_cumtrapz Cwtson Cwt_ogive Cwt_cumtrapz
clear uCO2d_7200PSD vCO2d_7200PSD
clear uCO2_7200UC vCO2_7200UC
clear uCO2_7500PSD vCO2_7500PSD
clear uCO2_7500UC vCO2_7500UC
            
fclose all;