disp('nau_process_AMMA.m')

F1_1= 1 - (wu_1(1,1) / 9 - 2 * wv_1(1,1) / 3 - 2 * wu_1(2,2) / 3) / Ustr_1 / Ustr_1;
F1_2= 1 - (wu_2(1,1) / 9 - 2 * wv_2(1,1) / 3 - 2 * wu_2(2,2) / 3) / Ustr_2 / Ustr_2;
F1_3= 1 - (wu_3(1,1) / 9 - 2 * wv_3(1,1) / 3 - 2 * wu_3(2,2) / 3) / Ustr_3 / Ustr_3;
F1_4= 1 - (wu_4(1,1) / 9 - 2 * wv_4(1,1) / 3 - 2 * wu_4(2,2) / 3) / Ustr_4 / Ustr_4;
F1_5= 1 - (wu_5(1,1) / 9 - 2 * wv_5(1,1) / 3 - 2 * wu_5(2,2) / 3) / Ustr_5 / Ustr_5;
F1_6= 1 - (wu_6(1,1) / 9 - 2 * wv_6(1,1) / 3 - 2 * wu_6(2,2) / 3) / Ustr_6 / Ustr_6;

F2_1= 1 - (wu_1(1,1) / 9 - wv_1(1,1) / 12 - wu_1(2,2) / 3) / Ustr_1 / Ustr_1;
F2_2= 1 - (wu_2(1,1) / 9 - wv_2(1,1) / 12 - wu_2(2,2) / 3) / Ustr_2 / Ustr_2;
F2_3= 1 - (wu_3(1,1) / 9 - wv_3(1,1) / 12 - wu_3(2,2) / 3) / Ustr_3 / Ustr_3;
F2_4= 1 - (wu_4(1,1) / 9 - wv_4(1,1) / 12 - wu_4(2,2) / 3) / Ustr_4 / Ustr_4;
F2_5= 1 - (wu_5(1,1) / 9 - wv_5(1,1) / 12 - wu_5(2,2) / 3) / Ustr_5 / Ustr_5;
F2_6= 1 - (wu_6(1,1) / 9 - wv_6(1,1) / 12 - wu_6(2,2) / 3) / Ustr_6 / Ustr_6;

F3_1= 1 - (wu_1(1,1) / 9 - wv_1(1,1) / 3 - wu_1(2,2) / 3) / Ustr_1 / Ustr_1;
F3_2= 1 - (wu_2(1,1) / 9 - wv_2(1,1) / 3 - wu_2(2,2) / 3) / Ustr_2 / Ustr_2;
F3_3= 1 - (wu_3(1,1) / 9 - wv_3(1,1) / 3 - wu_3(2,2) / 3) / Ustr_3 / Ustr_3;
F3_4= 1 - (wu_4(1,1) / 9 - wv_4(1,1) / 3 - wu_4(2,2) / 3) / Ustr_4 / Ustr_4;
F3_5= 1 - (wu_5(1,1) / 9 - wv_5(1,1) / 3 - wu_5(2,2) / 3) / Ustr_5 / Ustr_5;
F3_6= 1 - (wu_6(1,1) / 9 - wv_6(1,1) / 3 - wu_6(2,2) / 3) / Ustr_6 / Ustr_6;

%if length(Ustream_1) > 8191,specsize_1=8192;else,specsize_1=256;end;
%if length(Ustream_2) > 8191,specsize_2=8192;else,specsize_2=256;end;
%if length(Ustream_3) > 8191,specsize_3=8192;else,specsize_3=256;end;
%if length(Ustream_4) > 8191,specsize_4=8192;else,specsize_4=256;end;
%if length(Ustream_5) > 8191,specsize_5=8192;else,specsize_5=256;end;
%if length(Ustream_6) > 8191,specsize_6=8192;else,specsize_6=256;end;

specsize_1=length(Ustream_1);
specsize_2=length(Ustream_2);
specsize_3=length(Ustream_3);
specsize_4=length(Ustream_4);
specsize_5=length(Ustream_5);
specsize_6=length(Ustream_6);

[Su_1,Fson_u]=psd2(detrend(Ustream_1),specsize_1,sampleRate,hamming(specsize_1));[Su_1,Fson_u]=specsmoo(Su_1,sampleRate);
[Su_2,Fson_u]=psd2(detrend(Ustream_2),specsize_2,sampleRate,hamming(specsize_2));[Su_2,Fson_u]=specsmoo(Su_2,sampleRate);
[Su_3,Fson_u]=psd2(detrend(Ustream_3),specsize_3,sampleRate,hamming(specsize_3));[Su_3,Fson_u]=specsmoo(Su_3,sampleRate);
[Su_4,Fson_u]=psd2(detrend(Ustream_4),specsize_4,sampleRate,hamming(specsize_4));[Su_4,Fson_u]=specsmoo(Su_4,sampleRate);
[Su_5,Fson_u]=psd2(detrend(Ustream_5),specsize_5,sampleRate,hamming(specsize_5));[Su_5,Fson_u]=specsmoo(Su_5,sampleRate);
[Su_6,Fson_u]=psd2(detrend(Ustream_6),specsize_6,sampleRate,hamming(specsize_6));[Su_6,Fson_u]=specsmoo(Su_6,sampleRate);

[Sv_1,Fson_v]=psd2(detrend(Vstream_1),specsize_1,sampleRate,hamming(specsize_1));[Sv_1,Fson_v]=specsmoo(Sv_1,sampleRate);
[Sv_2,Fson_v]=psd2(detrend(Vstream_2),specsize_2,sampleRate,hamming(specsize_2));[Sv_2,Fson_v]=specsmoo(Sv_2,sampleRate);
[Sv_3,Fson_v]=psd2(detrend(Vstream_3),specsize_3,sampleRate,hamming(specsize_3));[Sv_3,Fson_v]=specsmoo(Sv_3,sampleRate);
[Sv_4,Fson_v]=psd2(detrend(Vstream_4),specsize_4,sampleRate,hamming(specsize_4));[Sv_4,Fson_v]=specsmoo(Sv_4,sampleRate);
[Sv_5,Fson_v]=psd2(detrend(Vstream_5),specsize_5,sampleRate,hamming(specsize_5));[Sv_5,Fson_v]=specsmoo(Sv_5,sampleRate);
[Sv_6,Fson_v]=psd2(detrend(Vstream_6),specsize_6,sampleRate,hamming(specsize_6));[Sv_6,Fson_v]=specsmoo(Sv_6,sampleRate);

[Sw_1,Fson_w]=psd2(detrend(Wstream_1),specsize_1,sampleRate,hamming(specsize_1));[Sw_1,Fson_w]=specsmoo(Sw_1,sampleRate);
[Sw_2,Fson_w]=psd2(detrend(Wstream_2),specsize_2,sampleRate,hamming(specsize_2));[Sw_2,Fson_w]=specsmoo(Sw_2,sampleRate);
[Sw_3,Fson_w]=psd2(detrend(Wstream_3),specsize_3,sampleRate,hamming(specsize_3));[Sw_3,Fson_w]=specsmoo(Sw_3,sampleRate);
[Sw_4,Fson_w]=psd2(detrend(Wstream_4),specsize_4,sampleRate,hamming(specsize_4));[Sw_4,Fson_w]=specsmoo(Sw_4,sampleRate);
[Sw_5,Fson_w]=psd2(detrend(Wstream_5),specsize_5,sampleRate,hamming(specsize_5));[Sw_5,Fson_w]=specsmoo(Sw_5,sampleRate);
[Sw_6,Fson_w]=psd2(detrend(Wstream_6),specsize_6,sampleRate,hamming(specsize_6));[Sw_6,Fson_w]=specsmoo(Sw_6,sampleRate);

[Suj_1,Fson_uj]=psd2(detrend(Uj_1),specsize_1,sampleRate,hamming(specsize_1));[Suj_1,Fson_uj]=specsmoo(Suj_1,sampleRate);
[Suj_2,Fson_uj]=psd2(detrend(Uj_2),specsize_2,sampleRate,hamming(specsize_2));[Suj_2,Fson_uj]=specsmoo(Suj_2,sampleRate);
[Suj_3,Fson_uj]=psd2(detrend(Uj_3),specsize_3,sampleRate,hamming(specsize_3));[Suj_3,Fson_uj]=specsmoo(Suj_3,sampleRate);
[Suj_4,Fson_uj]=psd2(detrend(Uj_4),specsize_4,sampleRate,hamming(specsize_4));[Suj_4,Fson_uj]=specsmoo(Suj_4,sampleRate);
[Suj_5,Fson_uj]=psd2(detrend(Uj_5),specsize_5,sampleRate,hamming(specsize_5));[Suj_5,Fson_uj]=specsmoo(Suj_5,sampleRate);
[Suj_6,Fson_uj]=psd2(detrend(Uj_6),specsize_6,sampleRate,hamming(specsize_6));[Suj_6,Fson_uj]=specsmoo(Suj_6,sampleRate);

[Svj_1,Fson_vj]=psd2(detrend(Vj_1),specsize_1,sampleRate,hamming(specsize_1));[Svj_1,Fson_vj]=specsmoo(Svj_1,sampleRate);
[Svj_2,Fson_vj]=psd2(detrend(Vj_2),specsize_2,sampleRate,hamming(specsize_2));[Svj_2,Fson_vj]=specsmoo(Svj_2,sampleRate);
[Svj_3,Fson_vj]=psd2(detrend(Vj_3),specsize_3,sampleRate,hamming(specsize_3));[Svj_3,Fson_vj]=specsmoo(Svj_3,sampleRate);
[Svj_4,Fson_vj]=psd2(detrend(Vj_4),specsize_4,sampleRate,hamming(specsize_4));[Svj_4,Fson_vj]=specsmoo(Svj_4,sampleRate);
[Svj_5,Fson_vj]=psd2(detrend(Vj_5),specsize_5,sampleRate,hamming(specsize_5));[Svj_5,Fson_vj]=specsmoo(Svj_5,sampleRate);
[Svj_6,Fson_vj]=psd2(detrend(Vj_6),specsize_6,sampleRate,hamming(specsize_6));[Svj_6,Fson_vj]=specsmoo(Svj_6,sampleRate);

[Swj_1,Fson_wj]=psd2(detrend(Wj_1),specsize_1,sampleRate,hamming(specsize_1));[Swj_1,Fson_wj]=specsmoo(Swj_1,sampleRate);
[Swj_2,Fson_wj]=psd2(detrend(Wj_2),specsize_2,sampleRate,hamming(specsize_2));[Swj_2,Fson_wj]=specsmoo(Swj_2,sampleRate);
[Swj_3,Fson_wj]=psd2(detrend(Wj_3),specsize_3,sampleRate,hamming(specsize_3));[Swj_3,Fson_wj]=specsmoo(Swj_3,sampleRate);
[Swj_4,Fson_wj]=psd2(detrend(Wj_4),specsize_4,sampleRate,hamming(specsize_4));[Swj_4,Fson_wj]=specsmoo(Swj_4,sampleRate);
[Swj_5,Fson_wj]=psd2(detrend(Wj_5),specsize_5,sampleRate,hamming(specsize_5));[Swj_5,Fson_wj]=specsmoo(Swj_5,sampleRate);
[Swj_6,Fson_wj]=psd2(detrend(Wj_6),specsize_6,sampleRate,hamming(specsize_6));[Swj_6,Fson_wj]=specsmoo(Swj_6,sampleRate);

[St_1,Fson_t]=psd2(detrend(Tsonic_1),specsize_1,sampleRate,hamming(specsize_1));[St_1,Fson_t]=specsmoo(St_1,sampleRate);
[St_2,Fson_t]=psd2(detrend(Tsonic_2),specsize_2,sampleRate,hamming(specsize_2));[St_2,Fson_t]=specsmoo(St_2,sampleRate);
[St_3,Fson_t]=psd2(detrend(Tsonic_3),specsize_3,sampleRate,hamming(specsize_3));[St_3,Fson_t]=specsmoo(St_3,sampleRate);
[St_4,Fson_t]=psd2(detrend(Tsonic_4),specsize_4,sampleRate,hamming(specsize_4));[St_4,Fson_t]=specsmoo(St_4,sampleRate);
[St_5,Fson_t]=psd2(detrend(Tsonic_5),specsize_5,sampleRate,hamming(specsize_5));[St_5,Fson_t]=specsmoo(St_5,sampleRate);
[St_6,Fson_t]=psd2(detrend(Tsonic_6),specsize_6,sampleRate,hamming(specsize_6));[St_6,Fson_t]=specsmoo(St_6,sampleRate);


% [So_1,Fson_o]=psd2(detrend(Licor_H2O_1)./rho_1,Nlenlic,Licrate,hamming(Nlenlic));[So_1,Fson_o]=specsmoo(So_1,Licrate);  %g/kg
% [So_2,Fson_o]=psd2(detrend(Licor_H2O_2)./rho_2,Nlenlic,Licrate,hamming(Nlenlic));[So_2,Fson_o]=specsmoo(So_2,Licrate);
% [So_3,Fson_o]=psd2(detrend(Licor_H2O_3)./rho_3,Nlenlic,Licrate,hamming(Nlenlic));[So_3,Fson_o]=specsmoo(So_3,Licrate);
% [So_4,Fson_o]=psd2(detrend(Licor_H2O_4)./rho_4,Nlenlic,Licrate,hamming(Nlenlic));[So_4,Fson_o]=specsmoo(So_4,Licrate);
% [So_5,Fson_o]=psd2(detrend(Licor_H2O_5)./rho_5,Nlenlic,Licrate,hamming(Nlenlic));[So_5,Fson_o]=specsmoo(So_5,Licrate);
% [So_6,Fson_o]=psd2(detrend(Licor_H2O_6)./rho_6,Nlenlic,Licrate,hamming(Nlenlic));[So_6,Fson_o]=specsmoo(So_6,Licrate);
[So_1,Fson_o]=psd2(detrend(Licor_H2O_1),Nlenlic,Licrate,hamming(Nlenlic));[So_1,Fson_o]=specsmoo(So_1,Licrate);  %g/kg
[So_2,Fson_o]=psd2(detrend(Licor_H2O_2),Nlenlic,Licrate,hamming(Nlenlic));[So_2,Fson_o]=specsmoo(So_2,Licrate);
[So_3,Fson_o]=psd2(detrend(Licor_H2O_3),Nlenlic,Licrate,hamming(Nlenlic));[So_3,Fson_o]=specsmoo(So_3,Licrate);
[So_4,Fson_o]=psd2(detrend(Licor_H2O_4),Nlenlic,Licrate,hamming(Nlenlic));[So_4,Fson_o]=specsmoo(So_4,Licrate);
[So_5,Fson_o]=psd2(detrend(Licor_H2O_5),Nlenlic,Licrate,hamming(Nlenlic));[So_5,Fson_o]=specsmoo(So_5,Licrate);
[So_6,Fson_o]=psd2(detrend(Licor_H2O_6),Nlenlic,Licrate,hamming(Nlenlic));[So_6,Fson_o]=specsmoo(So_6,Licrate);

[Sc_1,Fson_c]=psd2(detrend(Licor_CO2_1),Nlenlic,Licrate,hamming(Nlenlic));[Sc_1,Fson_c]=specsmoo(Sc_1,Licrate);
[Sc_2,Fson_c]=psd2(detrend(Licor_CO2_2),Nlenlic,Licrate,hamming(Nlenlic));[Sc_2,Fson_c]=specsmoo(Sc_2,Licrate);
[Sc_3,Fson_c]=psd2(detrend(Licor_CO2_3),Nlenlic,Licrate,hamming(Nlenlic));[Sc_3,Fson_c]=specsmoo(Sc_3,Licrate);
[Sc_4,Fson_c]=psd2(detrend(Licor_CO2_4),Nlenlic,Licrate,hamming(Nlenlic));[Sc_4,Fson_c]=specsmoo(Sc_4,Licrate);
[Sc_5,Fson_c]=psd2(detrend(Licor_CO2_5),Nlenlic,Licrate,hamming(Nlenlic));[Sc_5,Fson_c]=specsmoo(Sc_5,Licrate);
[Sc_6,Fson_c]=psd2(detrend(Licor_CO2_6),Nlenlic,Licrate,hamming(Nlenlic));[Sc_6,Fson_c]=specsmoo(Sc_6,Licrate);

[Cuw_1,Fson_uw]=csd2(detrend(Wstream_1),detrend(Ustream_1),specsize_1,sampleRate,hamming(specsize_1));[Cuw_1,Fson_uw]=specsmoo(Cuw_1,sampleRate);
[Cuw_2,Fson_uw]=csd2(detrend(Wstream_2),detrend(Ustream_2),specsize_2,sampleRate,hamming(specsize_2));[Cuw_2,Fson_uw]=specsmoo(Cuw_2,sampleRate);
[Cuw_3,Fson_uw]=csd2(detrend(Wstream_3),detrend(Ustream_3),specsize_3,sampleRate,hamming(specsize_3));[Cuw_3,Fson_uw]=specsmoo(Cuw_3,sampleRate);
[Cuw_4,Fson_uw]=csd2(detrend(Wstream_4),detrend(Ustream_4),specsize_4,sampleRate,hamming(specsize_4));[Cuw_4,Fson_uw]=specsmoo(Cuw_4,sampleRate);
[Cuw_5,Fson_uw]=csd2(detrend(Wstream_5),detrend(Ustream_5),specsize_5,sampleRate,hamming(specsize_5));[Cuw_5,Fson_uw]=specsmoo(Cuw_5,sampleRate);
[Cuw_6,Fson_uw]=csd2(detrend(Wstream_6),detrend(Ustream_6),specsize_6,sampleRate,hamming(specsize_6));[Cuw_6,Fson_uw]=specsmoo(Cuw_6,sampleRate);

[Cvw_1,Fson_vw]=csd2(detrend(Wstream_1),detrend(Vstream_1),specsize_1,sampleRate,hamming(specsize_1));[Cvw_1,Fson_vw]=specsmoo(Cvw_1,sampleRate);
[Cvw_2,Fson_vw]=csd2(detrend(Wstream_2),detrend(Vstream_2),specsize_2,sampleRate,hamming(specsize_2));[Cvw_2,Fson_vw]=specsmoo(Cvw_2,sampleRate);
[Cvw_3,Fson_vw]=csd2(detrend(Wstream_3),detrend(Vstream_3),specsize_3,sampleRate,hamming(specsize_3));[Cvw_3,Fson_vw]=specsmoo(Cvw_3,sampleRate);
[Cvw_4,Fson_vw]=csd2(detrend(Wstream_4),detrend(Vstream_4),specsize_4,sampleRate,hamming(specsize_4));[Cvw_4,Fson_vw]=specsmoo(Cvw_4,sampleRate);
[Cvw_5,Fson_vw]=csd2(detrend(Wstream_5),detrend(Vstream_5),specsize_5,sampleRate,hamming(specsize_5));[Cvw_5,Fson_vw]=specsmoo(Cvw_5,sampleRate);
[Cvw_6,Fson_vw]=csd2(detrend(Wstream_6),detrend(Vstream_6),specsize_6,sampleRate,hamming(specsize_6));[Cvw_6,Fson_vw]=specsmoo(Cvw_6,sampleRate);

[Ctw_1,Fson_tw]=csd2(detrend(Wstream_1),detrend(Tsonic_1),specsize_1,sampleRate,hamming(specsize_1));[Ctw_1,Fson_tw]=specsmoo(Ctw_1,sampleRate);
[Ctw_2,Fson_tw]=csd2(detrend(Wstream_2),detrend(Tsonic_2),specsize_2,sampleRate,hamming(specsize_2));[Ctw_2,Fson_tw]=specsmoo(Ctw_2,sampleRate);
[Ctw_3,Fson_tw]=csd2(detrend(Wstream_3),detrend(Tsonic_3),specsize_3,sampleRate,hamming(specsize_3));[Ctw_3,Fson_tw]=specsmoo(Ctw_3,sampleRate);
[Ctw_4,Fson_tw]=csd2(detrend(Wstream_4),detrend(Tsonic_4),specsize_4,sampleRate,hamming(specsize_4));[Ctw_4,Fson_tw]=specsmoo(Ctw_4,sampleRate);
[Ctw_5,Fson_tw]=csd2(detrend(Wstream_5),detrend(Tsonic_5),specsize_5,sampleRate,hamming(specsize_5));[Ctw_5,Fson_tw]=specsmoo(Ctw_5,sampleRate);
[Ctw_6,Fson_tw]=csd2(detrend(Wstream_6),detrend(Tsonic_6),specsize_6,sampleRate,hamming(specsize_6));[Ctw_6,Fson_tw]=specsmoo(Ctw_6,sampleRate);

% [Cow_1,Fson_ow]=csd2(detrend(Licor_w_1),detrend(Licor_H2O_1)./rho_1,Nlenlic,Licrate,hamming(Nlenlic));[Cow_1,Fson_ow]=specsmoo(Cow_1,Licrate);
% [Cow_2,Fson_ow]=csd2(detrend(Licor_w_2),detrend(Licor_H2O_2)./rho_2,Nlenlic,Licrate,hamming(Nlenlic));[Cow_2,Fson_ow]=specsmoo(Cow_2,Licrate);
% [Cow_3,Fson_ow]=csd2(detrend(Licor_w_3),detrend(Licor_H2O_3)./rho_3,Nlenlic,Licrate,hamming(Nlenlic));[Cow_3,Fson_ow]=specsmoo(Cow_3,Licrate);
% [Cow_4,Fson_ow]=csd2(detrend(Licor_w_4),detrend(Licor_H2O_4)./rho_4,Nlenlic,Licrate,hamming(Nlenlic));[Cow_4,Fson_ow]=specsmoo(Cow_4,Licrate);
% [Cow_5,Fson_ow]=csd2(detrend(Licor_w_5),detrend(Licor_H2O_5)./rho_5,Nlenlic,Licrate,hamming(Nlenlic));[Cow_5,Fson_ow]=specsmoo(Cow_5,Licrate);
% [Cow_6,Fson_ow]=csd2(detrend(Licor_w_6),detrend(Licor_H2O_6)./rho_6,Nlenlic,Licrate,hamming(Nlenlic));[Cow_6,Fson_ow]=specsmoo(Cow_6,Licrate);
[Cow_1,Fson_ow]=csd2(detrend(Licor_w_1),detrend(Licor_H2O_1),Nlenlic,Licrate,hamming(Nlenlic));[Cow_1,Fson_ow]=specsmoo(Cow_1,Licrate);
[Cow_2,Fson_ow]=csd2(detrend(Licor_w_2),detrend(Licor_H2O_2),Nlenlic,Licrate,hamming(Nlenlic));[Cow_2,Fson_ow]=specsmoo(Cow_2,Licrate);
[Cow_3,Fson_ow]=csd2(detrend(Licor_w_3),detrend(Licor_H2O_3),Nlenlic,Licrate,hamming(Nlenlic));[Cow_3,Fson_ow]=specsmoo(Cow_3,Licrate);
[Cow_4,Fson_ow]=csd2(detrend(Licor_w_4),detrend(Licor_H2O_4),Nlenlic,Licrate,hamming(Nlenlic));[Cow_4,Fson_ow]=specsmoo(Cow_4,Licrate);
[Cow_5,Fson_ow]=csd2(detrend(Licor_w_5),detrend(Licor_H2O_5),Nlenlic,Licrate,hamming(Nlenlic));[Cow_5,Fson_ow]=specsmoo(Cow_5,Licrate);
[Cow_6,Fson_ow]=csd2(detrend(Licor_w_6),detrend(Licor_H2O_6),Nlenlic,Licrate,hamming(Nlenlic));[Cow_6,Fson_ow]=specsmoo(Cow_6,Licrate);

[Ccw_1,Fson_cw]=csd2(detrend(Licor_w_1),detrend(Licor_CO2_1),Nlenlic,Licrate,hamming(Nlenlic));[Ccw_1,Fson_cw]=specsmoo(Ccw_1,Licrate);
[Ccw_2,Fson_cw]=csd2(detrend(Licor_w_2),detrend(Licor_CO2_2),Nlenlic,Licrate,hamming(Nlenlic));[Ccw_2,Fson_cw]=specsmoo(Ccw_2,Licrate);
[Ccw_3,Fson_cw]=csd2(detrend(Licor_w_3),detrend(Licor_CO2_3),Nlenlic,Licrate,hamming(Nlenlic));[Ccw_3,Fson_cw]=specsmoo(Ccw_3,Licrate);
[Ccw_4,Fson_cw]=csd2(detrend(Licor_w_4),detrend(Licor_CO2_4),Nlenlic,Licrate,hamming(Nlenlic));[Ccw_4,Fson_cw]=specsmoo(Ccw_4,Licrate);
[Ccw_5,Fson_cw]=csd2(detrend(Licor_w_5),detrend(Licor_CO2_5),Nlenlic,Licrate,hamming(Nlenlic));[Ccw_5,Fson_cw]=specsmoo(Ccw_5,Licrate);
[Ccw_6,Fson_cw]=csd2(detrend(Licor_w_6),detrend(Licor_CO2_6),Nlenlic,Licrate,hamming(Nlenlic));[Ccw_6,Fson_cw]=specsmoo(Ccw_6,Licrate);

[Cuwj_1,Fson_uwj]=csd2(detrend(Wj_1),detrend(Uj_1),specsize_1,sampleRate,hamming(specsize_1));[Cuwj_1,Fson_uwj]=specsmoo(Cuwj_1,sampleRate);
[Cuwj_2,Fson_uwj]=csd2(detrend(Wj_2),detrend(Uj_2),specsize_2,sampleRate,hamming(specsize_2));[Cuwj_2,Fson_uwj]=specsmoo(Cuwj_2,sampleRate);
[Cuwj_3,Fson_uwj]=csd2(detrend(Wj_3),detrend(Uj_3),specsize_3,sampleRate,hamming(specsize_3));[Cuwj_3,Fson_uwj]=specsmoo(Cuwj_3,sampleRate);
[Cuwj_4,Fson_uwj]=csd2(detrend(Wj_4),detrend(Uj_4),specsize_4,sampleRate,hamming(specsize_4));[Cuwj_4,Fson_uwj]=specsmoo(Cuwj_4,sampleRate);
[Cuwj_5,Fson_uwj]=csd2(detrend(Wj_5),detrend(Uj_5),specsize_5,sampleRate,hamming(specsize_5));[Cuwj_5,Fson_uwj]=specsmoo(Cuwj_5,sampleRate);
[Cuwj_6,Fson_uwj]=csd2(detrend(Wj_6),detrend(Uj_6),specsize_6,sampleRate,hamming(specsize_6));[Cuwj_6,Fson_uwj]=specsmoo(Cuwj_6,sampleRate);

[Cvwj_1,Fson_vwj]=csd2(detrend(Wj_1),detrend(Vj_1),specsize_1,sampleRate,hamming(specsize_1));[Cvwj_1,Fson_vwj]=specsmoo(Cvwj_1,sampleRate);
[Cvwj_2,Fson_vwj]=csd2(detrend(Wj_2),detrend(Vj_2),specsize_2,sampleRate,hamming(specsize_2));[Cvwj_2,Fson_vwj]=specsmoo(Cvwj_2,sampleRate);
[Cvwj_3,Fson_vwj]=csd2(detrend(Wj_3),detrend(Vj_3),specsize_3,sampleRate,hamming(specsize_3));[Cvwj_3,Fson_vwj]=specsmoo(Cvwj_3,sampleRate);
[Cvwj_4,Fson_vwj]=csd2(detrend(Wj_4),detrend(Vj_4),specsize_4,sampleRate,hamming(specsize_4));[Cvwj_4,Fson_vwj]=specsmoo(Cvwj_4,sampleRate);
[Cvwj_5,Fson_vwj]=csd2(detrend(Wj_5),detrend(Vj_5),specsize_5,sampleRate,hamming(specsize_5));[Cvwj_5,Fson_vwj]=specsmoo(Cvwj_5,sampleRate);
[Cvwj_6,Fson_vwj]=csd2(detrend(Wj_6),detrend(Vj_6),specsize_6,sampleRate,hamming(specsize_6));[Cvwj_6,Fson_vwj]=specsmoo(Cvwj_6,sampleRate);

%subtract noise

%if length(Su_1)>5,sev=5;else;sev=1;end;
%Su_1=Su_1-median(Su_1(length(Su_1)-sev:length(Su_1)));
%Su_2=Su_2-median(Su_2(length(Su_2)-sev:length(Su_2)));
%Su_3=Su_3-median(Su_3(length(Su_3)-sev:length(Su_3)));
%Su_4=Su_4-median(Su_4(length(Su_4)-sev:length(Su_4)));
%Su_5=Su_5-median(Su_5(length(Su_5)-sev:length(Su_5)));
%Su_6=Su_6-median(Su_6(length(Su_6)-sev:length(Su_6)));

%if length(Sv_1)>5,sev=5;else;sev=1;end;
%Sv_1=Sv_1-median(Sv_1(length(Sv_1)-sev:length(Sv_1)));
%Sv_2=Sv_2-median(Sv_2(length(Sv_2)-sev:length(Sv_2)));
%Sv_3=Sv_3-median(Sv_3(length(Sv_3)-sev:length(Sv_3)));
%Sv_4=Sv_4-median(Sv_4(length(Sv_4)-sev:length(Sv_4)));
%Sv_5=Sv_5-median(Sv_5(length(Sv_5)-sev:length(Sv_5)));
%Sv_6=Sv_6-median(Sv_6(length(Sv_6)-sev:length(Sv_6)));

%if length(Sw_1)>5,sev=5;else;sev=1;end;
%Sw_1=Sw_1-median(Sw_1(length(Sw_1)-sev:length(Sw_1)));
%Sw_2=Sw_2-median(Sw_2(length(Sw_2)-sev:length(Sw_2)));
%Sw_3=Sw_3-median(Sw_3(length(Sw_3)-sev:length(Sw_3)));
%Sw_4=Sw_4-median(Sw_4(length(Sw_4)-sev:length(Sw_4)));
%Sw_5=Sw_5-median(Sw_5(length(Sw_5)-sev:length(Sw_5)));
%Sw_6=Sw_6-median(Sw_6(length(Sw_6)-sev:length(Sw_6)));

if length(St_1)>5,sev=5;else;sev=1;end;
St_1a=St_1-median(St_1(length(St_1)-sev:length(St_1)));
St_2a=St_2-median(St_2(length(St_2)-sev:length(St_2)));
St_3a=St_3-median(St_3(length(St_3)-sev:length(St_3)));
St_4a=St_4-median(St_4(length(St_4)-sev:length(St_4)));
St_5a=St_5-median(St_5(length(St_5)-sev:length(St_5)));
St_6a=St_6-median(St_6(length(St_6)-sev:length(St_6)));

if length(So_1)>5,sev=5;else;sev=1;end;
So_1a=So_1-median(So_1(length(So_1)-sev:length(So_1)));
So_2a=So_2-median(So_2(length(So_2)-sev:length(So_2)));
So_3a=So_3-median(So_3(length(So_3)-sev:length(So_3)));
So_4a=So_4-median(So_4(length(So_4)-sev:length(So_4)));
So_5a=So_5-median(So_5(length(So_5)-sev:length(So_5)));
So_6a=So_6-median(So_6(length(So_6)-sev:length(So_6)));

if length(Sc_1)>5,sev=5;else;sev=1;end;
Sc_1a=Sc_1-median(Sc_1(length(Sc_1)-sev:length(Sc_1)));
Sc_2a=Sc_2-median(Sc_2(length(Sc_2)-sev:length(Sc_2)));
Sc_3a=Sc_3-median(Sc_3(length(Sc_3)-sev:length(Sc_3)));
Sc_4a=Sc_4-median(Sc_4(length(Sc_4)-sev:length(Sc_4)));
Sc_5a=Sc_5-median(Sc_5(length(Sc_5)-sev:length(Sc_5)));
Sc_6a=Sc_6-median(Sc_6(length(Sc_6)-sev:length(Sc_6)));

fminU=min(find(Fson_u>.8));
fminU=fminU-2;if fminU<1,fminU=1,end;
fmaxU=fminU+2;if fmaxU>length(Fson_u),fmaxU=length(Fson_u);end;

%cS=4*2/20;
cS=4;           %PSD2 and CSD2 should take care of the factor 2/20

Cu2_1_1=median(cS*((2*pi/Ustr_1)^(2/3))*Su_1(fminU:fmaxU).*Fson_u(fminU:fmaxU).^(5/3));
Cu2_2_1=median(cS*((2*pi/Ustr_2)^(2/3))*Su_2(fminU:fmaxU).*Fson_u(fminU:fmaxU).^(5/3));
Cu2_3_1=median(cS*((2*pi/Ustr_3)^(2/3))*Su_3(fminU:fmaxU).*Fson_u(fminU:fmaxU).^(5/3));
Cu2_4_1=median(cS*((2*pi/Ustr_4)^(2/3))*Su_4(fminU:fmaxU).*Fson_u(fminU:fmaxU).^(5/3));
Cu2_5_1=median(cS*((2*pi/Ustr_5)^(2/3))*Su_5(fminU:fmaxU).*Fson_u(fminU:fmaxU).^(5/3));
Cu2_6_1=median(cS*((2*pi/Ustr_6)^(2/3))*Su_6(fminU:fmaxU).*Fson_u(fminU:fmaxU).^(5/3));

Cw2_1_1=median(cS*((2*pi/Ustr_1)^(2/3))*Sw_1(fminU:fmaxU).*Fson_w(fminU:fmaxU).^(5/3));
Cw2_2_1=median(cS*((2*pi/Ustr_2)^(2/3))*Sw_2(fminU:fmaxU).*Fson_w(fminU:fmaxU).^(5/3));
Cw2_3_1=median(cS*((2*pi/Ustr_3)^(2/3))*Sw_3(fminU:fmaxU).*Fson_w(fminU:fmaxU).^(5/3));
Cw2_4_1=median(cS*((2*pi/Ustr_4)^(2/3))*Sw_4(fminU:fmaxU).*Fson_w(fminU:fmaxU).^(5/3));
Cw2_5_1=median(cS*((2*pi/Ustr_5)^(2/3))*Sw_5(fminU:fmaxU).*Fson_w(fminU:fmaxU).^(5/3));
Cw2_6_1=median(cS*((2*pi/Ustr_6)^(2/3))*Sw_6(fminU:fmaxU).*Fson_w(fminU:fmaxU).^(5/3));

fminT=min(find(Fson_t>.35));
fminT=fminT-2;if fminT<1,fminT=1,end;
fmaxT=fminT+2;if fmaxT>length(Fson_t),fmaxT=length(Fson_t);end;

Ct2_1_1=median(cS*((2*pi/Ustr_1)^(2/3))*St_1a(fminT:fmaxT).*Fson_t(fminT:fmaxT).^(5/3));
Ct2_2_1=median(cS*((2*pi/Ustr_2)^(2/3))*St_2a(fminT:fmaxT).*Fson_t(fminT:fmaxT).^(5/3));
Ct2_3_1=median(cS*((2*pi/Ustr_3)^(2/3))*St_3a(fminT:fmaxT).*Fson_t(fminT:fmaxT).^(5/3));
Ct2_4_1=median(cS*((2*pi/Ustr_4)^(2/3))*St_4a(fminT:fmaxT).*Fson_t(fminT:fmaxT).^(5/3));
Ct2_5_1=median(cS*((2*pi/Ustr_5)^(2/3))*St_5a(fminT:fmaxT).*Fson_t(fminT:fmaxT).^(5/3));
Ct2_6_1=median(cS*((2*pi/Ustr_6)^(2/3))*St_6a(fminT:fmaxT).*Fson_t(fminT:fmaxT).^(5/3));

%cO=4*2/20;
cO=4;           %PSD2 and CSD2 should take care of the factor 2/20

fminO=min(find(Fson_o>0.7));
fminO=fminO-2;if fminO<1,fminO=1,end;
fmaxO=fminO+2;if fmaxO>length(Fson_o),fmaxO=length(Fson_o);end;

%cO=4*2/20;
cO=4;           %PSD2 and CSD2 should take care of the factor 2/20

Co2_1_1=median(cO*((2*pi/Ustr_1)^(2/3))*So_1a(fminO:fmaxO).*Fson_o(fminO:fmaxO).^(5/3));
Co2_2_1=median(cO*((2*pi/Ustr_2)^(2/3))*So_2a(fminO:fmaxO).*Fson_o(fminO:fmaxO).^(5/3));
Co2_3_1=median(cO*((2*pi/Ustr_3)^(2/3))*So_3a(fminO:fmaxO).*Fson_o(fminO:fmaxO).^(5/3));
Co2_4_1=median(cO*((2*pi/Ustr_4)^(2/3))*So_4a(fminO:fmaxO).*Fson_o(fminO:fmaxO).^(5/3));
Co2_5_1=median(cO*((2*pi/Ustr_5)^(2/3))*So_5a(fminO:fmaxO).*Fson_o(fminO:fmaxO).^(5/3));
Co2_6_1=median(cO*((2*pi/Ustr_6)^(2/3))*So_6a(fminO:fmaxO).*Fson_o(fminO:fmaxO).^(5/3));

fminO=min(find(Fson_c>0.7));
fminO=fminO-2;if fminO<1,fminO=1,end;
fmaxO=fminO+2;if fmaxO>length(Fson_c),fmaxO=length(Fson_c);end;

%cO=4*2/20;
cO=4;           %PSD2 and CSD2 should take care of the factor 2/20

Cc2_1_1=median(cO*((2*pi/Ustr_1)^(2/3))*Sc_1a(fminO:fmaxO).*Fson_c(fminO:fmaxO).^(5/3));
Cc2_2_1=median(cO*((2*pi/Ustr_2)^(2/3))*Sc_2a(fminO:fmaxO).*Fson_c(fminO:fmaxO).^(5/3));
Cc2_3_1=median(cO*((2*pi/Ustr_3)^(2/3))*Sc_3a(fminO:fmaxO).*Fson_c(fminO:fmaxO).^(5/3));
Cc2_4_1=median(cO*((2*pi/Ustr_4)^(2/3))*Sc_4a(fminO:fmaxO).*Fson_c(fminO:fmaxO).^(5/3));
Cc2_5_1=median(cO*((2*pi/Ustr_5)^(2/3))*Sc_5a(fminO:fmaxO).*Fson_c(fminO:fmaxO).^(5/3));
Cc2_6_1=median(cO*((2*pi/Ustr_6)^(2/3))*Sc_6a(fminO:fmaxO).*Fson_c(fminO:fmaxO).^(5/3));

%   Apply Wyngaard and Clifford Correction to the Structure Function Parameters
Cu2_1_1=Cu2_1_1/F1_1;Cu2_2_1=Cu2_2_1/F1_2;Cu2_3_1=Cu2_3_1/F1_3;Cu2_4_1=Cu2_4_1/F1_4;Cu2_5_1=Cu2_5_1/F1_5;Cu2_6_1=Cu2_6_1/F1_6;
Cw2_1_1=Cw2_1_1/F2_1;Cw2_2_1=Cw2_2_1/F2_2;Cw2_3_1=Cw2_3_1/F2_3;Cw2_4_1=Cw2_4_1/F2_4;Cw2_5_1=Cw2_5_1/F2_5;Cw2_6_1=Cw2_6_1/F2_6;
Ct2_1_1=Ct2_1_1/F3_1;Ct2_2_1=Ct2_2_1/F3_2;Ct2_3_1=Ct2_3_1/F3_3;Ct2_4_1=Ct2_4_1/F3_4;Ct2_5_1=Ct2_5_1/F3_5;Ct2_6_1=Ct2_6_1/F3_6;
Cc2_1_1=Cc2_1_1/F3_1;Cc2_2_1=Cc2_2_1/F3_2;Cc2_3_1=Cc2_3_1/F3_3;Cc2_4_1=Cc2_4_1/F3_4;Cc2_5_1=Cc2_5_1/F3_5;Cc2_6_1=Cc2_6_1/F3_6;
Co2_1_1=Co2_1_1/F3_1;Co2_2_1=Co2_2_1/F3_2;Co2_3_1=Co2_3_1/F3_3;Co2_4_1=Co2_4_1/F3_4;Co2_5_1=Co2_5_1/F3_5;Co2_6_1=Co2_6_1/F3_6;

Cu2_1_1=(Cu2_1_1+Cw2_1_1.*3./4)./2;
Cu2_2_1=(Cu2_2_1+Cw2_2_1.*3./4)./2;
Cu2_3_1=(Cu2_3_1+Cw2_3_1.*3./4)./2;
Cu2_4_1=(Cu2_4_1+Cw2_4_1.*3./4)./2;
Cu2_5_1=(Cu2_5_1+Cw2_5_1.*3./4)./2;
Cu2_6_1=(Cu2_6_1+Cw2_6_1.*3./4)./2;

y=yship_1;x=xship_1;Cu2=Cu2_1_1;Ct2=Ct2_1_1;Co2=Co2_1_1;Cc2=Cc2_1_1;Q=Qbarv_1;idiss_AMMA;usid_1_1=us0;tsid_1_1=ts_ib;osid_1_1=os_ib;csid_1_1=cs_ib;Lid_1_1=z_u/zet;%qsid_1_1=qs_ib;
y=yship_2;x=xship_2;Cu2=Cu2_2_1;Ct2=Ct2_2_1;Co2=Co2_2_1;Cc2=Cc2_2_1;Q=Qbarv_2;idiss_AMMA;usid_2_1=us0;tsid_2_1=ts_ib;osid_2_1=os_ib;csid_2_1=cs_ib;Lid_2_1=z_u/zet;%qsid_2_1=qs_ib;
y=yship_3;x=xship_3;Cu2=Cu2_3_1;Ct2=Ct2_3_1;Co2=Co2_3_1;Cc2=Cc2_3_1;Q=Qbarv_3;idiss_AMMA;usid_3_1=us0;tsid_3_1=ts_ib;osid_3_1=os_ib;csid_3_1=cs_ib;Lid_3_1=z_u/zet;%qsid_3_1=qs_ib;
y=yship_4;x=xship_4;Cu2=Cu2_4_1;Ct2=Ct2_4_1;Co2=Co2_4_1;Cc2=Cc2_4_1;Q=Qbarv_4;idiss_AMMA;usid_4_1=us0;tsid_4_1=ts_ib;osid_4_1=os_ib;csid_4_1=cs_ib;Lid_4_1=z_u/zet;%qsid_4_1=qs_ib;
y=yship_5;x=xship_5;Cu2=Cu2_5_1;Ct2=Ct2_5_1;Co2=Co2_5_1;Cc2=Cc2_5_1;Q=Qbarv_5;idiss_AMMA;usid_5_1=us0;tsid_5_1=ts_ib;osid_5_1=os_ib;csid_5_1=cs_ib;Lid_5_1=z_u/zet;%qsid_5_1=qs_ib;
y=yship_6;x=xship_6;Cu2=Cu2_6_1;Ct2=Ct2_6_1;Co2=Co2_6_1;Cc2=Cc2_6_1;Q=Qbarv_6;idiss_AMMA;usid_6_1=us0;tsid_6_1=ts_ib;osid_6_1=os_ib;csid_6_1=cs_ib;Lid_6_1=z_u/zet;%qsid_6_1=qs_ib;

fminU=min(find(Fson_u>.6*sampleRate/20));if fminU<1,fminU=1,end;
fmaxU=max(find(Fson_u<3.5*sampleRate/20));if fmaxU>length(Fson_u),fmaxU=length(Fson_u);end;

%cS=4*2/20;
cS=4;           %PSD2 and CSD2 should take care of the factor 2/20

if fminU>fmaxU,fminU=fmaxU-4;end;

%Zpoly=polyfit(log(Fson(fminU:fmaxU)),log(cS*((2*pi/Ustr_1)^(2/3))*Su_1(fminU:fmaxU).*(Fson(fminU:fmaxU).^(5/3))),4);
%if Zpoly(4)<0.25,
%   Cu2_1_2=exp(Zpoly(5));
%else,
%   plus=0;
%   while 1
%      zold=Zpoly(4);
%      plus=plus+1;
%      if fminU+plus+4==fmaxU,
%         if fmaxU<4;
%            fmaxU=4;
%         end;
%         Zpoly=polyfit(log(Fson(fmaxU-4:fmaxU)),log(cS*((2*pi/Ustr_1)^(2/3))*Su_1(fmaxU-4:fmaxU).*(Fson(fmaxU-4:fmaxU).^(5/3))),4);
%         break;
%      end;            
%      Zpoly=polyfit(log(Fson(fminU+plus:fmaxU)),log(cS*((2*pi/Ustr_1)^(2/3))*Su_1(fminU+plus:fmaxU).*(Fson(fminU+plus:fmaxU).^(5/3))),4);
%      if Zpoly(4)>zold | abs(Zpoly(4))<0.2,
%         break,
%      end
%   end;
%   Cu2_1_2=exp(Zpoly(5));
%end;

Zpoly=polyfit(log(Fson_u(fminU:fmaxU)),log(cS*((2*pi/Ustr_1)^(2/3))*Su_1(fminU:fmaxU).*(Fson_u(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cu2_1_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cu2_1_2=exp(Zpoly(1).*(log(Fson_u(fminU+plus)).^4)+Zpoly(2).*(log(Fson_u(fminU+plus)).^3)+Zpoly(3).*(log(Fson_u(fminU+plus)).^2)+Zpoly(4).*log(Fson_u(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_u(fminU:fmaxU)),log(cS*((2*pi/Ustr_2)^(2/3))*Su_2(fminU:fmaxU).*(Fson_u(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cu2_2_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cu2_2_2=exp(Zpoly(1).*(log(Fson_u(fminU+plus)).^4)+Zpoly(2).*(log(Fson_u(fminU+plus)).^3)+Zpoly(3).*(log(Fson_u(fminU+plus)).^2)+Zpoly(4).*log(Fson_u(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_u(fminU:fmaxU)),log(cS*((2*pi/Ustr_3)^(2/3))*Su_3(fminU:fmaxU).*(Fson_u(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cu2_3_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cu2_3_2=exp(Zpoly(1).*(log(Fson_u(fminU+plus)).^4)+Zpoly(2).*(log(Fson_u(fminU+plus)).^3)+Zpoly(3).*(log(Fson_u(fminU+plus)).^2)+Zpoly(4).*log(Fson_u(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_u(fminU:fmaxU)),log(cS*((2*pi/Ustr_4)^(2/3))*Su_4(fminU:fmaxU).*(Fson_u(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cu2_4_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
%      Zslope=Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cu2_4_2=exp(Zpoly(1).*(log(Fson_u(fminU+plus)).^4)+Zpoly(2).*(log(Fson_u(fminU+plus)).^3)+Zpoly(3).*(log(Fson_u(fminU+plus)).^2)+Zpoly(4).*log(Fson_u(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_u(fminU:fmaxU)),log(cS*((2*pi/Ustr_5)^(2/3))*Su_5(fminU:fmaxU).*(Fson_u(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cu2_5_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cu2_5_2=exp(Zpoly(1).*(log(Fson_u(fminU+plus)).^4)+Zpoly(2).*(log(Fson_u(fminU+plus)).^3)+Zpoly(3).*(log(Fson_u(fminU+plus)).^2)+Zpoly(4).*log(Fson_u(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_u(fminU:fmaxU)),log(cS*((2*pi/Ustr_6)^(2/3))*Su_6(fminU:fmaxU).*(Fson_u(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cu2_6_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cu2_6_2=exp(Zpoly(1).*(log(Fson_u(fminU+plus)).^4)+Zpoly(2).*(log(Fson_u(fminU+plus)).^3)+Zpoly(3).*(log(Fson_u(fminU+plus)).^2)+Zpoly(4).*log(Fson_u(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_w(fminU:fmaxU)),log(cS*((2*pi/Ustr_1)^(2/3))*Sw_1(fminU:fmaxU).*(Fson_w(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cw2_1_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
%      Zslope=Zpoly(1).*(log(Fson_w(fminU+plus)).^3)+Zpoly(2).*(log(Fson_w(fminU+plus)).^2)+Zpoly(3).*log(Fson_w(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cw2_1_2=exp(Zpoly(1).*(log(Fson_w(fminU+plus)).^4)+Zpoly(2).*(log(Fson_w(fminU+plus)).^3)+Zpoly(3).*(log(Fson_w(fminU+plus)).^2)+Zpoly(4).*log(Fson_w(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_w(fminU:fmaxU)),log(cS*((2*pi/Ustr_2)^(2/3))*Sw_2(fminU:fmaxU).*(Fson_w(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cw2_2_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
%       Zslope=Zpoly(1).*(log(Fson_w(fminU+plus)).^3)+Zpoly(2).*(log(Fson_w(fminU+plus)).^2)+Zpoly(3).*log(Fson_w(fminU+plus))+Zpoly(4);
    Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
    plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cw2_2_2=exp(Zpoly(1).*(log(Fson_w(fminU+plus)).^4)+Zpoly(2).*(log(Fson_w(fminU+plus)).^3)+Zpoly(3).*(log(Fson_w(fminU+plus)).^2)+Zpoly(4).*log(Fson_w(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_w(fminU:fmaxU)),log(cS*((2*pi/Ustr_3)^(2/3))*Sw_3(fminU:fmaxU).*(Fson_w(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cw2_3_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_w(fminU+plus)).^3)+Zpoly(2).*(log(Fson_w(fminU+plus)).^2)+Zpoly(3).*log(Fson_w(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cw2_3_2=exp(Zpoly(1).*(log(Fson_w(fminU+plus)).^4)+Zpoly(2).*(log(Fson_w(fminU+plus)).^3)+Zpoly(3).*(log(Fson_w(fminU+plus)).^2)+Zpoly(4).*log(Fson_w(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_w(fminU:fmaxU)),log(cS*((2*pi/Ustr_4)^(2/3))*Sw_4(fminU:fmaxU).*(Fson_w(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cw2_4_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_w(fminU+plus)).^3)+Zpoly(2).*(log(Fson_w(fminU+plus)).^2)+Zpoly(3).*log(Fson_w(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cw2_4_2=exp(Zpoly(1).*(log(Fson_w(fminU+plus)).^4)+Zpoly(2).*(log(Fson_w(fminU+plus)).^3)+Zpoly(3).*(log(Fson_w(fminU+plus)).^2)+Zpoly(4).*log(Fson_w(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_w(fminU:fmaxU)),log(cS*((2*pi/Ustr_5)^(2/3))*Sw_5(fminU:fmaxU).*(Fson_w(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cw2_5_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_w(fminU+plus)).^3)+Zpoly(2).*(log(Fson_w(fminU+plus)).^2)+Zpoly(3).*log(Fson_w(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cw2_5_2=exp(Zpoly(1).*(log(Fson_w(fminU+plus)).^4)+Zpoly(2).*(log(Fson_w(fminU+plus)).^3)+Zpoly(3).*(log(Fson_w(fminU+plus)).^2)+Zpoly(4).*log(Fson_w(fminU+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_w(fminU:fmaxU)),log(cS*((2*pi/Ustr_6)^(2/3))*Sw_6(fminU:fmaxU).*(Fson_w(fminU:fmaxU).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cw2_6_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_w(fminU+plus)).^3)+Zpoly(2).*(log(Fson_w(fminU+plus)).^2)+Zpoly(3).*log(Fson_w(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_u(fminU+plus)).^3)+3*Zpoly(2).*(log(Fson_u(fminU+plus)).^2)+2*Zpoly(3).*log(Fson_u(fminU+plus))+Zpoly(4);
      plus=plus+1;
      if fminU+plus>=fmaxU,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cw2_6_2=exp(Zpoly(1).*(log(Fson_w(fminU+plus)).^4)+Zpoly(2).*(log(Fson_w(fminU+plus)).^3)+Zpoly(3).*(log(Fson_w(fminU+plus)).^2)+Zpoly(4).*log(Fson_w(fminU+plus))+Zpoly(5));
end;

fminT=min(find(Fson_t>.35*sampleRate/20));if fminT<1,fminT=1,end;
fmaxT=max(find(Fson_t<3*sampleRate/20));if fmaxT>length(Fson_t),fmaxT=length(Fson_t);end;

%cS=4*2/20;
cS=4;           %PSD2 and CSD2 should take care of the factor 2/20

if fminT>fmaxT,fminT=fmaxT-4;end;

Zpoly=polyfit(log(Fson_t(fminT:fmaxT)),log(cS*((2*pi/Ustr_1)^(2/3))*St_1a(fminT:fmaxT).*(Fson_t(fminT:fmaxT).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Ct2_1_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_t(fminU+plus)).^3)+Zpoly(2).*(log(Fson_t(fminU+plus)).^2)+Zpoly(3).*log(Fson_t(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_t(fminT+plus)).^3)+3*Zpoly(2).*(log(Fson_t(fminT+plus)).^2)+2*Zpoly(3).*log(Fson_t(fminT+plus))+Zpoly(4);
      plus=plus+1;
      if fminT+plus>=fmaxT,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Ct2_1_2=exp(Zpoly(1).*(log(Fson_t(fminT+plus)).^4)+Zpoly(2).*(log(Fson_t(fminT+plus)).^3)+Zpoly(3).*(log(Fson_t(fminT+plus)).^2)+Zpoly(4).*log(Fson_t(fminT+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_t(fminT:fmaxT)),log(cS*((2*pi/Ustr_2)^(2/3))*St_2a(fminT:fmaxT).*(Fson_t(fminT:fmaxT).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Ct2_2_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_t(fminU+plus)).^3)+Zpoly(2).*(log(Fson_t(fminU+plus)).^2)+Zpoly(3).*log(Fson_t(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_t(fminT+plus)).^3)+3*Zpoly(2).*(log(Fson_t(fminT+plus)).^2)+2*Zpoly(3).*log(Fson_t(fminT+plus))+Zpoly(4);
      plus=plus+1;
      if fminT+plus>=fmaxT,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Ct2_2_2=exp(Zpoly(1).*(log(Fson_t(fminT+plus)).^4)+Zpoly(2).*(log(Fson_t(fminT+plus)).^3)+Zpoly(3).*(log(Fson_t(fminT+plus)).^2)+Zpoly(4).*log(Fson_t(fminT+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_t(fminT:fmaxT)),log(cS*((2*pi/Ustr_3)^(2/3))*St_3a(fminT:fmaxT).*(Fson_t(fminT:fmaxT).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Ct2_3_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_t(fminT+plus)).^3)+Zpoly(2).*(log(Fson_t(fminT+plus)).^2)+Zpoly(3).*log(Fson_t(fminT+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_t(fminT+plus)).^3)+3*Zpoly(2).*(log(Fson_t(fminT+plus)).^2)+2*Zpoly(3).*log(Fson_t(fminT+plus))+Zpoly(4);
      plus=plus+1;
      if fminT+plus>=fmaxT,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Ct2_3_2=exp(Zpoly(1).*(log(Fson_t(fminT+plus)).^4)+Zpoly(2).*(log(Fson_t(fminT+plus)).^3)+Zpoly(3).*(log(Fson_t(fminT+plus)).^2)+Zpoly(4).*log(Fson_t(fminT+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_t(fminT:fmaxT)),log(cS*((2*pi/Ustr_4)^(2/3))*St_4a(fminT:fmaxT).*(Fson_t(fminT:fmaxT).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Ct2_4_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_t(fminU+plus)).^3)+Zpoly(2).*(log(Fson_t(fminU+plus)).^2)+Zpoly(3).*log(Fson_t(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_t(fminT+plus)).^3)+3*Zpoly(2).*(log(Fson_t(fminT+plus)).^2)+2*Zpoly(3).*log(Fson_t(fminT+plus))+Zpoly(4);
      plus=plus+1;
      if fminT+plus>=fmaxT,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Ct2_4_2=exp(Zpoly(1).*(log(Fson_t(fminT+plus)).^4)+Zpoly(2).*(log(Fson_t(fminT+plus)).^3)+Zpoly(3).*(log(Fson_t(fminT+plus)).^2)+Zpoly(4).*log(Fson_t(fminT+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_t(fminT:fmaxT)),log(cS*((2*pi/Ustr_5)^(2/3))*St_5a(fminT:fmaxT).*(Fson_t(fminT:fmaxT).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Ct2_5_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_t(fminU+plus)).^3)+Zpoly(2).*(log(Fson_t(fminU+plus)).^2)+Zpoly(3).*log(Fson_t(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_t(fminT+plus)).^3)+3*Zpoly(2).*(log(Fson_t(fminT+plus)).^2)+2*Zpoly(3).*log(Fson_t(fminT+plus))+Zpoly(4);
      plus=plus+1;
      if fminT+plus>=fmaxT,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Ct2_5_2=exp(Zpoly(1).*(log(Fson_t(fminT+plus)).^4)+Zpoly(2).*(log(Fson_t(fminT+plus)).^3)+Zpoly(3).*(log(Fson_t(fminT+plus)).^2)+Zpoly(4).*log(Fson_t(fminT+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_t(fminT:fmaxT)),log(cS*((2*pi/Ustr_6)^(2/3))*St_6a(fminT:fmaxT).*(Fson_t(fminT:fmaxT).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Ct2_6_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_t(fminU+plus)).^3)+Zpoly(2).*(log(Fson_t(fminU+plus)).^2)+Zpoly(3).*log(Fson_t(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_t(fminT+plus)).^3)+3*Zpoly(2).*(log(Fson_t(fminT+plus)).^2)+2*Zpoly(3).*log(Fson_t(fminT+plus))+Zpoly(4);
      plus=plus+1;
      if fminT+plus>=fmaxT,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Ct2_6_2=exp(Zpoly(1).*(log(Fson_t(fminT+plus)).^4)+Zpoly(2).*(log(Fson_t(fminT+plus)).^3)+Zpoly(3).*(log(Fson_t(fminT+plus)).^2)+Zpoly(4).*log(Fson_t(fminT+plus))+Zpoly(5));
end;


fminO=min(find(Fson_o>0.7*Licrate/20));if fminO<1,fminO=1,end;                              %before Licor=20Hz -->Fson_o>0.7
fmaxO=max(find(Fson_o<3.6*Licrate/20));if fmaxO>length(Fson_o),fmaxO=length(Fson_o);end;    %Now, Licor=10Hz -->Fson_o>0.35 (=0.7*Licrate/20...)


%cO=4*2/20;
cO=4;           %PSD2 and CSD2 should take care of the factor 2/20

if fminO>fmaxO,fminO=fmaxO-4;end

Zpoly=polyfit(log(Fson_o(fminO:fmaxO)),log(cS*((2*pi/Ustr_1)^(2/3))*So_1a(fminO:fmaxO).*(Fson_o(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Co2_1_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Co2_1_2=exp(Zpoly(1).*(log(Fson_o(fminO+plus)).^4)+Zpoly(2).*(log(Fson_o(fminO+plus)).^3)+Zpoly(3).*(log(Fson_o(fminO+plus)).^2)+Zpoly(4).*log(Fson_o(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_o(fminO:fmaxO)),log(cS*((2*pi/Ustr_2)^(2/3))*So_2a(fminO:fmaxO).*(Fson_o(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Co2_2_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Co2_2_2=exp(Zpoly(1).*(log(Fson_o(fminO+plus)).^4)+Zpoly(2).*(log(Fson_o(fminO+plus)).^3)+Zpoly(3).*(log(Fson_o(fminO+plus)).^2)+Zpoly(4).*log(Fson_o(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_o(fminO:fmaxO)),log(cS*((2*pi/Ustr_3)^(2/3))*So_3a(fminO:fmaxO).*(Fson_o(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Co2_3_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Co2_3_2=exp(Zpoly(1).*(log(Fson_o(fminO+plus)).^4)+Zpoly(2).*(log(Fson_o(fminO+plus)).^3)+Zpoly(3).*(log(Fson_o(fminO+plus)).^2)+Zpoly(4).*log(Fson_o(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_o(fminO:fmaxO)),log(cS*((2*pi/Ustr_4)^(2/3))*So_4a(fminO:fmaxO).*(Fson_o(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Co2_4_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Co2_4_2=exp(Zpoly(1).*(log(Fson_o(fminO+plus)).^4)+Zpoly(2).*(log(Fson_o(fminO+plus)).^3)+Zpoly(3).*(log(Fson_o(fminO+plus)).^2)+Zpoly(4).*log(Fson_o(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_o(fminO:fmaxO)),log(cS*((2*pi/Ustr_5)^(2/3))*So_5a(fminO:fmaxO).*(Fson_o(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Co2_5_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Co2_5_2=exp(Zpoly(1).*(log(Fson_o(fminO+plus)).^4)+Zpoly(2).*(log(Fson_o(fminO+plus)).^3)+Zpoly(3).*(log(Fson_o(fminO+plus)).^2)+Zpoly(4).*log(Fson_o(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_o(fminO:fmaxO)),log(cS*((2*pi/Ustr_6)^(2/3))*So_6a(fminO:fmaxO).*(Fson_o(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Co2_6_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_o(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_o(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_o(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Co2_6_2=exp(Zpoly(1).*(log(Fson_o(fminO+plus)).^4)+Zpoly(2).*(log(Fson_o(fminO+plus)).^3)+Zpoly(3).*(log(Fson_o(fminO+plus)).^2)+Zpoly(4).*log(Fson_o(fminO+plus))+Zpoly(5));
end;

fminO=min(find(Fson_c>0.7*Licrate/20));if fminO<1,fminO=1,end;
fmaxO=max(find(Fson_c<3.6*Licrate/20));if fmaxO>length(Fson_c),fmaxO=length(Fson_c);end;

%cO=4*2/20;
cO=4;           %PSD2 and CSD2 should take care of the factor 2/20

if fminO>fmaxO,fminO=fmaxO-4;end

Zpoly=polyfit(log(Fson_c(fminO:fmaxO)),log(cS*((2*pi/Ustr_1)^(2/3))*Sc_1a(fminO:fmaxO).*(Fson_c(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cc2_1_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_c(fminU+plus)).^3)+Zpoly(2).*(log(Fson_c(fminU+plus)).^2)+Zpoly(3).*log(Fson_c(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_c(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_c(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_c(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cc2_1_2=exp(Zpoly(1).*(log(Fson_c(fminO+plus)).^4)+Zpoly(2).*(log(Fson_c(fminO+plus)).^3)+Zpoly(3).*(log(Fson_c(fminO+plus)).^2)+Zpoly(4).*log(Fson_c(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_c(fminO:fmaxO)),log(cS*((2*pi/Ustr_2)^(2/3))*Sc_2a(fminO:fmaxO).*(Fson_c(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cc2_2_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_c(fminO+plus)).^3)+Zpoly(2).*(log(Fson_c(fminO+plus)).^2)+Zpoly(3).*log(Fson_c(fminO+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_c(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_c(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_c(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cc2_2_2=exp(Zpoly(1).*(log(Fson_c(fminU+plus)).^4)+Zpoly(2).*(log(Fson_c(fminU+plus)).^3)+Zpoly(3).*(log(Fson_c(fminU+plus)).^2)+Zpoly(4).*log(Fson_c(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_c(fminO:fmaxO)),log(cS*((2*pi/Ustr_3)^(2/3))*Sc_3a(fminO:fmaxO).*(Fson_c(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cc2_3_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_c(fminU+plus)).^3)+Zpoly(2).*(log(Fson_c(fminU+plus)).^2)+Zpoly(3).*log(Fson_c(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_c(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_c(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_c(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cc2_3_2=exp(Zpoly(1).*(log(Fson_c(fminO+plus)).^4)+Zpoly(2).*(log(Fson_c(fminO+plus)).^3)+Zpoly(3).*(log(Fson_c(fminO+plus)).^2)+Zpoly(4).*log(Fson_c(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_c(fminO:fmaxO)),log(cS*((2*pi/Ustr_4)^(2/3))*Sc_4a(fminO:fmaxO).*(Fson_c(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cc2_4_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_c(fminU+plus)).^3)+Zpoly(2).*(log(Fson_c(fminU+plus)).^2)+Zpoly(3).*log(Fson_c(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_c(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_c(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_c(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cc2_4_2=exp(Zpoly(1).*(log(Fson_c(fminO+plus)).^4)+Zpoly(2).*(log(Fson_c(fminO+plus)).^3)+Zpoly(3).*(log(Fson_c(fminO+plus)).^2)+Zpoly(4).*log(Fson_c(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_c(fminO:fmaxO)),log(cS*((2*pi/Ustr_5)^(2/3))*Sc_5a(fminO:fmaxO).*(Fson_c(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cc2_5_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_c(fminU+plus)).^3)+Zpoly(2).*(log(Fson_c(fminU+plus)).^2)+Zpoly(3).*log(Fson_c(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_c(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_c(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_c(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cc2_5_2=exp(Zpoly(1).*(log(Fson_c(fminO+plus)).^4)+Zpoly(2).*(log(Fson_c(fminO+plus)).^3)+Zpoly(3).*(log(Fson_c(fminO+plus)).^2)+Zpoly(4).*log(Fson_c(fminO+plus))+Zpoly(5));
end;

Zpoly=polyfit(log(Fson_c(fminO:fmaxO)),log(cS*((2*pi/Ustr_6)^(2/3))*Sc_6a(fminO:fmaxO).*(Fson_c(fminO:fmaxO).^(5/3))),4);
Zslope=Zpoly(4);
if Zslope<0.25,
   Cc2_6_2=exp(Zpoly(5));
else
   plus=0;
   while 1
      zold=Zslope;
      %Zslope=Zpoly(1).*(log(Fson_c(fminU+plus)).^3)+Zpoly(2).*(log(Fson_c(fminU+plus)).^2)+Zpoly(3).*log(Fson_c(fminU+plus))+Zpoly(4);
      Zslope=4*Zpoly(1).*(log(Fson_c(fminO+plus)).^3)+3*Zpoly(2).*(log(Fson_c(fminO+plus)).^2)+2*Zpoly(3).*log(Fson_c(fminO+plus))+Zpoly(4);
      plus=plus+1;
      if fminO+plus>=fmaxO,
         break
      end
      if Zslope>zold | abs(Zslope)<0.2,
         break,
      end
   end;
   Cc2_6_2=exp(Zpoly(1).*(log(Fson_c(fminO+plus)).^4)+Zpoly(2).*(log(Fson_c(fminO+plus)).^3)+Zpoly(3).*(log(Fson_c(fminO+plus)).^2)+Zpoly(4).*log(Fson_c(fminO+plus))+Zpoly(5));
end;

%   Apply Wyngaard and Clifford Correction to the Structure Function Parameters
Cu2_1_2=Cu2_1_2/F1_1;Cu2_2_2=Cu2_2_2/F1_2;Cu2_3_2=Cu2_3_2/F1_3;Cu2_4_2=Cu2_4_2/F1_4;Cu2_5_2=Cu2_5_2/F1_5;Cu2_6_2=Cu2_6_2/F1_6;
Cw2_1_2=Cw2_1_2/F2_1;Cw2_2_2=Cw2_2_2/F2_2;Cw2_3_2=Cw2_3_2/F2_3;Cw2_4_2=Cw2_4_2/F2_4;Cw2_5_2=Cw2_5_2/F2_5;Cw2_6_2=Cw2_6_2/F2_6;
Ct2_1_2=Ct2_1_2/F3_1;Ct2_2_2=Ct2_2_2/F3_2;Ct2_3_2=Ct2_3_2/F3_3;Ct2_4_2=Ct2_4_2/F3_4;Ct2_5_2=Ct2_5_2/F3_5;Ct2_6_2=Ct2_6_2/F3_6;
Cc2_1_2=Cc2_1_2/F3_1;Cc2_2_2=Cc2_2_2/F3_2;Cc2_3_2=Cc2_3_2/F3_3;Cc2_4_2=Cc2_4_2/F3_4;Cc2_5_2=Cc2_5_2/F3_5;Cc2_6_2=Cc2_6_2/F3_6;
Co2_1_2=Co2_1_2/F3_1;Co2_2_2=Co2_2_2/F3_2;Co2_3_2=Co2_3_2/F3_3;Co2_4_2=Co2_4_2/F3_4;Co2_5_2=Co2_5_2/F3_5;Co2_6_2=Co2_6_2/F3_6;

Cu2_1_2 = (Cu2_1_2 + Cw2_1_2.*3./4)./2;
Cu2_2_2 = (Cu2_2_2 + Cw2_2_2.*3./4)./2;
Cu2_3_2 = (Cu2_3_2 + Cw2_3_2.*3./4)./2;
Cu2_4_2 = (Cu2_4_2 + Cw2_4_2.*3./4)./2;
Cu2_5_2 = (Cu2_5_2 + Cw2_5_2.*3./4)./2;
Cu2_6_2 = (Cu2_6_2 + Cw2_6_2.*3./4)./2;

y=yship_1;x=xship_1;Cu2=Cu2_1_2;Ct2=Ct2_1_2;Cc2=Cc2_1_2;Co2=Co2_1_2;Q=Qbarv_1;idiss_AMMA;usid_1_2=us0;tsid_1_2=ts_ib;osid_1_2=os_ib;csid_1_2=cs_ib;Lid_1_2=z_u/zet;%qsid_1_2=qs_ib;
y=yship_2;x=xship_2;Cu2=Cu2_2_2;Ct2=Ct2_2_2;Cc2=Cc2_2_2;Co2=Co2_2_2;Q=Qbarv_2;idiss_AMMA;usid_2_2=us0;tsid_2_2=ts_ib;osid_2_2=os_ib;csid_2_2=cs_ib;Lid_2_2=z_u/zet;%qsid_2_2=qs_ib;
y=yship_3;x=xship_3;Cu2=Cu2_3_2;Ct2=Ct2_3_2;Cc2=Cc2_3_2;Co2=Co2_3_2;Q=Qbarv_3;idiss_AMMA;usid_3_2=us0;tsid_3_2=ts_ib;osid_3_2=os_ib;csid_3_2=cs_ib;Lid_3_2=z_u/zet;%qsid_3_2=qs_ib;
y=yship_4;x=xship_4;Cu2=Cu2_4_2;Ct2=Ct2_4_2;Cc2=Cc2_4_2;Co2=Co2_4_2;Q=Qbarv_4;idiss_AMMA;usid_4_2=us0;tsid_4_2=ts_ib;osid_4_2=os_ib;csid_4_2=cs_ib;Lid_4_2=z_u/zet;%qsid_4_2=qs_ib;
y=yship_5;x=xship_5;Cu2=Cu2_5_2;Ct2=Ct2_5_2;Cc2=Cc2_5_2;Co2=Co2_5_2;Q=Qbarv_5;idiss_AMMA;usid_5_2=us0;tsid_5_2=ts_ib;osid_5_2=os_ib;csid_5_2=cs_ib;Lid_5_2=z_u/zet;%qsid_5_2=qs_ib;
y=yship_6;x=xship_6;Cu2=Cu2_6_2;Ct2=Ct2_6_2;Cc2=Cc2_6_2;Co2=Co2_6_2;Q=Qbarv_6;idiss_AMMA;usid_6_2=us0;tsid_6_2=ts_ib;osid_6_2=os_ib;csid_6_2=cs_ib;Lid_6_2=z_u/zet;%qsid_6_2=qs_ib;

%######################## plot tests###############################
%     yu=mean([Cuw_1' Cuw_2' Cuw_3' Cuw_4' Cuw_5' Cuw_6']');yuj=mean([Cuwj_1' Cuwj_2' Cuwj_3' Cuwj_4' Cuwj_5' Cuwj_6']');yt=mean([Ctw_1' Ctw_2' Ctw_3' Ctw_4' Ctw_5' Ctw_6']');yq=mean([Cqw_1' Cqw_2' Cqw_3' Cqw_4' Cqw_5' Cqw_6']');
%     yqL=mean([Cow_1' Cow_2' Cow_3' Cow_4' Cow_5' Cow_6']');
%     figure;semilogx(Fson_uwj,Fson_uwj.*yuj,'--',Fson_uw,Fson_uw.*yu,'g',Fson_tw,Fson_tw.*yt,'r',Fson_qw,Fson_qw.*yq,'c',Fson_cw,Fson_cw.*yqL,'b',[1e-3 10 ],[0 0]);title('Cospectrum (u,w) Edson (dash line), Hare ( green solid line), (temperature,w) in red, (q,w) in cyan, (H2O,w) in blue')
%     figure;semilogx(Fson_uw,Fson_uw.*yu,'g',Fson_tw,Fson_tw.*yt,'r',Fson_cw,Fson_cw.*yqL,'b',[1e-3 10 ],[0 0]);title('Cospectrum (u,w) in green, (temperature,w) in red, (H2O,w) in blue')
%     ywc=mean([Ccw_1' Ccw_2' Ccw_3' Ccw_4' Ccw_5' Ccw_6']');
%     figure;semilogx(Fson_cw,Fson_cw.*ywc);title('Cospectrum between velocity w and C (CO2 concentration)')
%     yww=mean([Sw_1' Sw_2' Sw_3' Sw_4' Sw_5' Sw_6']');ywwj=mean([Swj_1' Swj_2' Swj_3' Swj_4' Swj_5' Swj_6']');
%     figure;loglog(Fson_wj,Fson_wj.*ywwj,'--',Fson_w,Fson_w.*yww); title('Power spectrum of velocity w. Edson (dash line), Hare (solid line)')
%####################################################################

%y_y= y_1 Ubar_1 Tbar_1 Tsea_1 Qbar_1 wu_1(1,2) wv_1(1,2) wt_1(1,2) wa_1(1,2) wh_1(1,2) rho_1 stdC_1 stdA_1 medC_1 medA_1 Ss_1 Sc_1 Sp_1 rain_1
%     y_2 Ubar_2 Tbar_2 Tsea_2 Qbar_2 wu_2(1,2) wv_2(1,2) wt_2(1,2) wa_2(1,2) wh_2(1,2) rho_2 stdC_2 stdA_2 medC_2 medA_2 Ss_2 Sc_2 Sp_2 rain_2
%     y_3 Ubar_3 Tbar_3 Tsea_3 Qbar_3 wu_3(1,2) wv_3(1,2) wt_3(1,2) wa_3(1,2) wh_3(1,2) rho_3 stdC_3 stdA_3 medC_3 medA_3 Ss_3 Sc_3 Sp_3 rain_3
%     y_4 Ubar_4 Tbar_4 Tsea_4 Qbar_4 wu_4(1,2) wv_4(1,2) wt_4(1,2) wa_4(1,2) wh_4(1,2) rho_4 stdC_4 stdA_4 medC_4 medA_4 Ss_4 Sc_4 Sp_4 rain_4
%     y_5 Ubar_5 Tbar_5 Tsea_5 Qbar_5 wu_5(1,2) wv_5(1,2) wt_5(1,2) wa_5(1,2) wh_5(1,2) rho_5 stdC_5 stdA_5 medC_5 medA_5 Ss_5 Sc_5 Sp_5 rain_5
%     y_6 Ubar_6 Tbar_6 Tsea_6 Qbar_6 wu_6(1,2) wv_6(1,2) wt_6(1,2) wa_6(1,2) wh_6(1,2) rho_6 stdC_6 stdA_6 medC_6 medA_6 Ss_6 Sc_6 Sp_6 rain_6];
% % %%%%  Quenching correction 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% qwater=[Licor_H2O_1 Licor_H2O_2 Licor_H2O_3 Licor_H2O_4 Licor_H2O_5 Licor_H2O_6]';
% mq=nanmedian(qwater(qwater<35 & qwater>=0));
% urel=sqrt(U.^2 + V.^2 + W.^2);
% 
% qwater(qwater>mq+3)=mq;
% qwater(qwater<mq-3)=mq;
% 
% %calculating mixing ratio
% mr=qwater./1000./(1-qwater/1000);%watch out, Licor H2O is in g/kg (line 916 nau_correct)-->/1000
% 
% mr_att=TubingAttenuation(mr,sampleRate,0.4);
% mr_att(1:30)=nanmedian(mr_att);
% 
% % ozone=(1+5.*mr).*ozone;
% % ozone=(1+5.*mr_att).*ozone; % Quenching only
% % ozone=(1+6.85.*mr_att).*ozone; %includes density fluctuation corrections (see Lee et al, 2000)


%Ozone old method

O3ppb_real=(fastO3_real-(-79.136*ddd+13697))/(-12.025*ddd+4219.3);       %Calibration average over all calibrations ecxcept
slowO3_real=1.0371*slowO3_real+2.9742;                                   % incluesd calibration of the ML8810, this formula is also inclueded in the calibration of the FRCI

% save([way_rawdata, '\AMMA\flux_data\O3ppb_real_interp' , jd,hr '.txt'], 'O3ppb_real', '-ascii')

[c1, lags1]=xcov(Wj_1,O3ppb_real(1:6000),100,'unbiased');
    x=1:1:6000;        
    figure;[AX,H1,H2] = plotyy(x,Wj_1,x,O3ppb_real(1:6000),'plot');
    set(get(AX(1),'Ylabel'),'string','W (m*s-1)'); 
    set(get(AX(2),'Ylabel'),'string','O3 (ppb)');
    print([way_rawdata,'\AMMA\flux_data\lagtime_', jd, hr ,'.jpg'],'-djpeg');
    p = polyfit(lags1,c1,26);
    covEpol = polyval(p,lags1);
    figure;plot(lags1,c1,'.',lags1,covEpol,'r--')
    ll=find(-100<lags1 & lags1<-35);
    jj=find(covEpol(ll)==min(covEpol(ll)));
    lagtime_1=-lags1(ll(jj));  %lagtime in points
    flux_1_o=min(covEpol(ll(jj)));

[c2, lags2]=xcov(Wj_2,O3ppb_real(6001:12000),100,'unbiased');
    %figure;plot(Wj_2);hold on;plot(O3ppb_real(6001:12000),'r');
    p = polyfit(lags2,c2,26);
    covEpol = polyval(p,lags2);
    %figure;plot(lags2,c2,'.',lags2,covEpol,'r--')
    ll=find(-100<lags2 & lags2<-35);
    jj=find(covEpol(ll)==min(covEpol(ll)));
    lagtime_2=-lags2(ll(jj));  %lagtime in points
    flux_2_o=min(covEpol(ll(jj)));

[c3, lags3]=xcov(Wj_3,O3ppb_real(12001:18000),100,'unbiased');
    %figure;plot(Wj_3);hold on;plot(O3ppb_real(12001:18000),'r');
    p = polyfit(lags3,c3,26);
    covEpol = polyval(p,lags3);
    %figure;plot(lags3,c3,'.',lags3,covEpol,'r--')
    ll=find(-100<lags3 & lags3<-35);
    jj=find(covEpol(ll)==min(covEpol(ll)));
    lagtime_3=-lags3(ll(jj));  %lagtime in points
    flux_3_o=min(covEpol(ll(jj)));

[c4, lags4]=xcov(Wj_4,O3ppb_real(18001:24000),100,'unbiased');
    %figure;plot(Wj_4);hold on;plot(O3ppb_real(18001:24000),'r');
    p = polyfit(lags4,c4,26);
    covEpol = polyval(p,lags4);
    %figure;plot(lags4,c4,'.',lags4,covEpol,'r--')
    ll=find(-100<lags4 & lags4<-35);
    jj=find(covEpol(ll)==min(covEpol(ll)));
    lagtime_4=-lags4(ll(jj));  %lagtime in points
    flux_4_o=min(covEpol(ll(jj)));
    
[c5, lags5]=xcov(Wj_5,O3ppb_real(24001:30000),100,'unbiased');
    %figure;plot(Wj_5);hold on;plot(O3ppb_real(24001:30000),'r');
    p = polyfit(lags5,c5,26);
    covEpol = polyval(p,lags5);
    %figure;plot(lags5,c5,'.',lags5,covEpol,'r--')
    ll=find(-100<lags5 & lags5<-35);
    jj=find(covEpol(ll)==min(covEpol(ll)));
    lagtime_5=-lags5((ll(jj)));  %lagtime in points
    flux_5_o=min(covEpol(ll(jj)));

[c6, lags6]=xcov(Wj_6,O3ppb_real(30001:36000),100,'unbiased');
   %figure;plot(Wj_6);hold on;plot(O3ppb_real(30001:36000),'r'); 
    p = polyfit(lags6,c6,26);
    covEpol = polyval(p,lags6);
    %figure;plot(lags6,c6,'.',lags6,covEpol,'r--')
    ll=find(-100<lags6 & lags6<-35);
    jj=find(covEpol(ll)==min(covEpol(ll)));
    lagtime_6=-lags6(ll(jj));  %lagtime in points
    flux_6_o=min(covEpol(ll(jj)));

    
O3mean_1_o=mean(O3ppb_real(1:6000));
O3mean_2_o=mean(O3ppb_real(6001:12000));
O3mean_3_o=mean(O3ppb_real(12001:18000));
O3mean_4_o=mean(O3ppb_real(18001:24000));
O3mean_5_o=mean(O3ppb_real(24001:30000));
O3mean_6_o=mean(O3ppb_real(30001:36000));

O3stdev_1_o=std(O3ppb_real(1:6000));
O3stdev_2_o=std(O3ppb_real(6001:12000));
O3stdev_3_o=std(O3ppb_real(12001:18000));
O3stdev_4_o=std(O3ppb_real(18001:24000));
O3stdev_5_o=std(O3ppb_real(24001:30000));
O3stdev_6_o=std(O3ppb_real(30001:36000));

slowO3mean_1_o=mean(slowO3_real(1:6000));
slowO3mean_2_o=mean(slowO3_real(6001:12000));
slowO3mean_3_o=mean(slowO3_real(12001:18000));
slowO3mean_4_o=mean(slowO3_real(18001:24000));
slowO3mean_5_o=mean(slowO3_real(24001:30000));
slowO3mean_6_o=mean(slowO3_real(30001:36000));

slowO3stdev_1_o=std(slowO3_real(1:6000));
slowO3stdev_2_o=std(slowO3_real(6001:12000));
slowO3stdev_3_o=std(slowO3_real(12001:18000));
slowO3stdev_4_o=std(slowO3_real(18001:24000));
slowO3stdev_5_o=std(slowO3_real(24001:30000));
slowO3stdev_6_o=std(slowO3_real(30001:36000));



%Ozone new method

O3ppb_real=O3ppb_real(54:35993); 
% mr_att=mr_att(1:35940);


O3mean_1=mean(O3ppb_real(1:5990));
O3mean_2=mean(O3ppb_real(5991:11980));
O3mean_3=mean(O3ppb_real(11981:17970));
O3mean_4=mean(O3ppb_real(17971:23960));
O3mean_5=mean(O3ppb_real(23961:29950));
O3mean_6=mean(O3ppb_real(29951:35940));

O3stdev_1=std(O3ppb_real(1:5990));
O3stdev_2=std(O3ppb_real(5991:11980));
O3stdev_3=std(O3ppb_real(11981:17970));
O3stdev_4=std(O3ppb_real(17971:23960));
O3stdev_5=std(O3ppb_real(23961:29950));
O3stdev_6=std(O3ppb_real(29951:35940));


flux_1=cov(Wcat(1:5990),O3ppb_real(1:5990));%.*(1+5*Licor_H2O_1(1:5990)/1000)
flux_1=flux_1(1,2);

flux_2=cov(Wcat(5991:11980),O3ppb_real(5991:11980));
flux_2=flux_2(1,2);

flux_3=cov(Wcat(11981:17970),O3ppb_real(11981:17970));
flux_3=flux_3(1,2);

flux_4=cov(Wcat(17971:23960),O3ppb_real(17971:23960));
flux_4=flux_4(1,2);

flux_5=cov(Wcat(23961:29950),O3ppb_real(23961:29950)');
flux_5=flux_5(1,2);

flux_6=cov(Wcat(29951:35940),O3ppb_real(29951:35940));
flux_6=flux_6(1,2);

        
if hhh==0 | hhh==12 | hhh==18,
    
%            Ustr=[Ustr_1 Ustr_2 Ustr_3 Ustr_4 Ustr_5 Ustr_6];
%            Cw2=mean([Cw2_1_2' Cw2_2_2' Cw2_3_2' Cw2_4_2' Cw2_5_2' Cw2_6_2']);
           disp(['Calculating power spectra for ',num2str(hhh)])
           [Sw,F]=psd2(detrend(O3ppb_real),length(O3ppb_real),10.);
           [Sws,Fsw]=specsmoo(Sw,10.);
           
           figure; loglog(Fsw(length(Fsw)-31:length(Fsw)),((Fsw(length(Fsw)-31:length(Fsw))).^(-2/3))./(Fsw(length(Fsw)-18).^(-2/3)).*Sws(length(Fsw)-18).*Fsw(length(Fsw)-18),'r');
           %figure;loglog(Fsw,Cw2*(2*pi/mean(Ustr))^-.667*Fsw.^-.66/4,'r');
           hold on;loglog(Fsw,Sws.*Fsw,'g');
           
           title(['O3 Spectra:   Day ',jd,hr]);
           xlabel('Frequency in Hz');
           ylabel('Spectrum * Frequency');
           print([way_rawdata,'\AMMA\Analysis\spectraO3_' jd, hr,  '.jpg'],'-djpeg');
           
            
           disp(['Calculating cross spectra for ',num2str(hhh)])
           [C_wO3,Fson_wO3]=csd2(detrend(Wcat(1:35940)),detrend(O3ppb_real),length(O3ppb_real),10,hamming(length(O3ppb_real)));
           [C_wO3s,Fson_wO3s]=specsmoo(C_wO3,10);
           figure;semilogx(Fson_wO3s,C_wO3s.*Fson_wO3s,'g');
           
           title(['wO3 Cross Spectra:   Day ',jd,hr]);
           %legend('5/3','O3');       xlabel('Frequency in Hz');
           xlabel('Frequency in Hz');
           ylabel('Spectrum * Frequency');
           print([way_rawdata,'\AMMA\Analysis\cross_spectraWO3_' jd, hr,  '.jpg'],'-djpeg');
           
        end;%end hhh=0 or 12 or 18           

        

newview=[ddd+hhh/24+00/24/60 xship_1 xsnake_1 mean_wind_1 min_wind_1 max_wind_1 std_wind_1 mean_dir_1 min_dir_1 max_dir_1 std_dir_1 Ustr_1 Un_1 Ue_1 Twindir_1 Reldir_1 alpha_1 beta_1 Tseasnake_1 Tbar_1 RHv_1 ship_RH_1 Qbarv_1 Qship_1 Qseasnake_1 rho_1 yship_1 ysnake_1 std_Rl_1 std_Rs_1 mean_sog_1 std_sog_1 min_sog_1 max_sog_1 mean_cog_1 std_cog_1 min_cog_1 max_cog_1 mean_gpscog_1 std_gpscog_1 min_gpscog_1 max_gpscog_1 mean_gpsspeed_1 min_gpsspeed_1 max_gpsspeed_1 mean_gyro_1 std_gyro_1 min_gyro_1 max_gyro_1 wu_1(1,2) wuj_1(1,2) wv_1(1,2) wvj_1(1,2) wt_1(1,2) wtj_1(1,2) wu_1(1,1) wv_1(1,1) wu_1(2,2) wuj_1(1,1) wvj_1(1,1) wuj_1(2,2) wt_1(2,2) ...
   Sp1_1 Sp2_1 Sp3_1 Ssin_1 Scos_1 Cu2_1_1 Cu2_1_2 Cw2_1_1 Cw2_1_2 Ct2_1_1 Ct2_1_2 Co2_1_1 Co2_1_2 usid_1_1 usid_1_2 tsid_1_1 tsid_1_2 osid_1_1 osid_1_2 Lid_1_1 Lid_1_2 Lon_1 Lat_1 Ubarj_1 mean(odec(0001:0300)) std(odec(0001:0300)) min(odec(0001:0300)) max(odec(0001:0300)) std_imir_1
   ddd+hhh/24+10/24/60 xship_2 xsnake_2 mean_wind_2 min_wind_2 max_wind_2 std_wind_2 mean_dir_2 min_dir_2 max_dir_2 std_dir_2 Ustr_2 Un_2 Ue_2 Twindir_2 Reldir_2 alpha_2 beta_2 Tseasnake_2 Tbar_2 RHv_2 ship_RH_2 Qbarv_2 Qship_2 Qseasnake_2 rho_2 yship_2 ysnake_2 std_Rl_2 std_Rs_2 mean_sog_2 std_sog_2 min_sog_2 max_sog_2 mean_cog_2 std_cog_2 min_cog_2 max_cog_2 mean_gpscog_2 std_gpscog_2 min_gpscog_2 max_gpscog_2 mean_gpsspeed_2 min_gpsspeed_2 max_gpsspeed_2 mean_gyro_2 std_gyro_2 min_gyro_2 max_gyro_2 wu_2(1,2) wuj_2(1,2) wv_2(1,2) wvj_2(1,2) wt_2(1,2) wtj_2(1,2) wu_2(1,1) wv_2(1,1) wu_2(2,2) wuj_2(1,1) wvj_2(1,1) wuj_2(2,2) wt_2(2,2) ...
   Sp1_2 Sp2_2 Sp3_2 Ssin_2 Scos_2 Cu2_2_1 Cu2_2_2 Cw2_2_1 Cw2_2_2 Ct2_2_1 Ct2_2_2 Co2_2_1 Co2_2_2 usid_2_1 usid_2_2 tsid_2_1 tsid_2_2 osid_2_1 osid_2_2 Lid_2_1 Lid_2_2 Lon_2 Lat_2 Ubarj_2 mean(odec(0301:0600)) std(odec(0301:0600)) min(odec(0301:0600)) max(odec(0301:0600)) std_imir_2
   ddd+hhh/24+20/24/60 xship_3 xsnake_3 mean_wind_3 min_wind_3 max_wind_3 std_wind_3 mean_dir_3 min_dir_3 max_dir_3 std_dir_3 Ustr_3 Un_3 Ue_3 Twindir_3 Reldir_3 alpha_3 beta_3 Tseasnake_3 Tbar_3 RHv_3 ship_RH_3 Qbarv_3 Qship_3 Qseasnake_3 rho_3 yship_3 ysnake_3 std_Rl_3 std_Rs_3 mean_sog_3 std_sog_3 min_sog_3 max_sog_3 mean_cog_3 std_cog_3 min_cog_3 max_cog_3 mean_gpscog_3 std_gpscog_3 min_gpscog_3 max_gpscog_3 mean_gpsspeed_3 min_gpsspeed_3 max_gpsspeed_3 mean_gyro_3 std_gyro_3 min_gyro_3 max_gyro_3 wu_3(1,2) wuj_3(1,2) wv_3(1,2) wvj_3(1,2) wt_3(1,2) wtj_3(1,2) wu_3(1,1) wv_3(1,1) wu_3(2,2) wuj_3(1,1) wvj_3(1,1) wuj_3(2,2) wt_3(2,2) ...
   Sp1_3 Sp2_3 Sp3_3 Ssin_3 Scos_3 Cu2_3_1 Cu2_3_2 Cw2_3_1 Cw2_3_2 Ct2_3_1 Ct2_3_2 Co2_3_1 Co2_3_2 usid_3_1 usid_3_2 tsid_3_1 tsid_3_2 osid_3_1 osid_3_2 Lid_3_1 Lid_3_2 Lon_3 Lat_3 Ubarj_3 mean(odec(0601:0900)) std(odec(0601:0900)) min(odec(0601:0900)) max(odec(0601:0900)) std_imir_3
   ddd+hhh/24+30/24/60 xship_4 xsnake_4 mean_wind_4 min_wind_4 max_wind_4 std_wind_4 mean_dir_4 min_dir_4 max_dir_4 std_dir_4 Ustr_4 Un_4 Ue_4 Twindir_4 Reldir_4 alpha_4 beta_4 Tseasnake_4 Tbar_4 RHv_4 ship_RH_4 Qbarv_4 Qship_4 Qseasnake_4 rho_4 yship_4 ysnake_4 std_Rl_4 std_Rs_4 mean_sog_4 std_sog_4 min_sog_4 max_sog_4 mean_cog_4 std_cog_4 min_cog_4 max_cog_4 mean_gpscog_4 std_gpscog_4 min_gpscog_4 max_gpscog_4 mean_gpsspeed_4 min_gpsspeed_4 max_gpsspeed_4 mean_gyro_4 std_gyro_4 min_gyro_4 max_gyro_4 wu_4(1,2) wuj_4(1,2) wv_4(1,2) wvj_4(1,2) wt_4(1,2) wtj_4(1,2) wu_4(1,1) wv_4(1,1) wu_4(2,2) wuj_4(1,1) wvj_4(1,1) wuj_4(2,2) wt_4(2,2) ...
   Sp1_4 Sp2_4 Sp3_4 Ssin_4 Scos_4 Cu2_4_1 Cu2_4_2 Cw2_4_1 Cw2_4_2 Ct2_4_1 Ct2_4_2 Co2_4_1 Co2_4_2 usid_4_1 usid_4_2 tsid_4_1 tsid_4_2 osid_4_1 osid_4_2 Lid_4_1 Lid_4_2 Lon_4 Lat_4 Ubarj_4 mean(odec(0901:1200)) std(odec(0901:1200)) min(odec(0901:1200)) max(odec(0901:1200)) std_imir_4
   ddd+hhh/24+40/24/60 xship_5 xsnake_5 mean_wind_5 min_wind_5 max_wind_5 std_wind_5 mean_dir_5 min_dir_5 max_dir_5 std_dir_5 Ustr_5 Un_5 Ue_5 Twindir_5 Reldir_5 alpha_5 beta_5 Tseasnake_5 Tbar_5 RHv_5 ship_RH_5 Qbarv_5 Qship_5 Qseasnake_5 rho_5 yship_5 ysnake_5 std_Rl_5 std_Rs_5 mean_sog_5 std_sog_5 min_sog_5 max_sog_5 mean_cog_5 std_cog_5 min_cog_5 max_cog_5 mean_gpscog_5 std_gpscog_5 min_gpscog_5 max_gpscog_5 mean_gpsspeed_5 min_gpsspeed_5 max_gpsspeed_5 mean_gyro_5 std_gyro_5 min_gyro_5 max_gyro_5 wu_5(1,2) wuj_5(1,2) wv_5(1,2) wvj_5(1,2) wt_5(1,2) wtj_5(1,2) wu_5(1,1) wv_5(1,1) wu_5(2,2) wuj_5(1,1) wvj_5(1,1) wuj_5(2,2) wt_5(2,2) ...
   Sp1_5 Sp2_5 Sp3_5 Ssin_5 Scos_5 Cu2_5_1 Cu2_5_2 Cw2_5_1 Cw2_5_2 Ct2_5_1 Ct2_5_2 Co2_5_1 Co2_5_2 usid_5_1 usid_5_2 tsid_5_1 tsid_5_2 osid_5_1 osid_5_2 Lid_5_1 Lid_5_2 Lon_5 Lat_5 Ubarj_5 mean(odec(1201:1500)) std(odec(1201:1500)) min(odec(1201:1500)) max(odec(1201:1500)) std_imir_5
   ddd+hhh/24+50/24/60 xship_6 xsnake_6 mean_wind_6 min_wind_6 max_wind_6 std_wind_6 mean_dir_6 min_dir_6 max_dir_6 std_dir_6 Ustr_6 Un_6 Ue_6 Twindir_6 Reldir_6 alpha_6 beta_6 Tseasnake_6 Tbar_6 RHv_6 ship_RH_6 Qbarv_6 Qship_6 Qseasnake_6 rho_6 yship_6 ysnake_6 std_Rl_6 std_Rs_6 mean_sog_6 std_sog_6 min_sog_6 max_sog_6 mean_cog_6 std_cog_6 min_cog_6 max_cog_6 mean_gpscog_6 std_gpscog_6 min_gpscog_6 max_gpscog_6 mean_gpsspeed_6 min_gpsspeed_6 max_gpsspeed_6 mean_gyro_6 std_gyro_6 min_gyro_6 max_gyro_6 wu_6(1,2) wuj_6(1,2) wv_6(1,2) wvj_6(1,2) wt_6(1,2) wtj_6(1,2) wu_6(1,1) wv_6(1,1) wu_6(2,2) wuj_6(1,1) wvj_6(1,1) wuj_6(2,2) wt_6(2,2) ...
   Sp1_6 Sp2_6 Sp3_6 Ssin_6 Scos_6 Cu2_6_1 Cu2_6_2 Cw2_6_1 Cw2_6_2 Ct2_6_1 Ct2_6_2 Co2_6_1 Co2_6_2 usid_6_1 usid_6_2 tsid_6_1 tsid_6_2 osid_6_1 osid_6_2 Lid_6_1 Lid_6_2 Lon_6 Lat_6 Ubarj_6 mean(odec(1501:1800)) std(odec(1501:1800)) min(odec(1501:1800)) max(odec(1501:1800)) std_imir_6];

%disp(['uw=  ',num2str(wu_1(1,2))])
%disp(num2str(length(Fson)))

specview=[ddd+hhh/24+00/24/60 Fson_u Su_1 Sv_1 Sw_1 St_1 So_1 Suj_1 Svj_1 Swj_1 Cuw_1 Cvw_1 Ctw_1 Cow_1 Cuwj_1 Cvwj_1
          ddd+hhh/24+10/24/60 Fson_u Su_2 Sv_2 Sw_2 St_2 So_2 Suj_2 Svj_2 Swj_2 Cuw_2 Cvw_2 Ctw_2 Cow_2 Cuwj_2 Cvwj_2
          ddd+hhh/24+20/24/60 Fson_u Su_3 Sv_3 Sw_3 St_3 So_3 Suj_3 Svj_3 Swj_3 Cuw_3 Cvw_3 Ctw_3 Cow_3 Cuwj_3 Cvwj_3
          ddd+hhh/24+30/24/60 Fson_u Su_4 Sv_4 Sw_4 St_4 So_4 Suj_4 Svj_4 Swj_4 Cuw_4 Cvw_4 Ctw_4 Cow_4 Cuwj_4 Cvwj_4
          ddd+hhh/24+40/24/60 Fson_u Su_5 Sv_5 Sw_5 St_5 So_5 Suj_5 Svj_5 Swj_5 Cuw_5 Cvw_5 Ctw_5 Cow_5 Cuwj_5 Cvwj_5
          ddd+hhh/24+50/24/60 Fson_u Su_6 Sv_6 Sw_6 St_6 So_6 Suj_6 Svj_6 Swj_6 Cuw_6 Cvw_6 Ctw_6 Cow_6 Cuwj_6 Cvwj_6];
       
co2view=[ddd+hhh/24+00/24/60  Fson_cw Sc_1 So_1 Ccw_1 Cow_1 Co2_1_1 Co2_1_2
          ddd+hhh/24+10/24/60 Fson_cw Sc_2 So_2 Ccw_2 Cow_2 Co2_2_1 Co2_2_2
          ddd+hhh/24+20/24/60 Fson_cw Sc_3 So_3 Ccw_3 Cow_3 Co2_3_1 Co2_3_2
          ddd+hhh/24+30/24/60 Fson_cw Sc_4 So_4 Ccw_4 Cow_4 Co2_4_1 Co2_4_2
          ddd+hhh/24+40/24/60 Fson_cw Sc_5 So_5 Ccw_5 Cow_5 Co2_5_1 Co2_5_2
          ddd+hhh/24+50/24/60 Fson_cw Sc_6 So_6 Ccw_6 Cow_6 Co2_6_1 Co2_6_2];
       
co2res=[ddd+hhh/24+00/24/60 mean(Licor_H2O_1) mean(LicH2O_1) mean(Licor_CO2_1) mean(LicCO2_1) mean(Licor_Press_1) mean(Licor_Temp_1) wohj_1 wlhj_1 wocj_1 wlcj_1 std(Licor_H2O_1) std(LicH2O_1) std(Licor_CO2_1) std(LicCO2_1) std(Licor_Press_1) 
         ddd+hhh/24+10/24/60 mean(Licor_H2O_2) mean(LicH2O_2) mean(Licor_CO2_2) mean(LicCO2_2) mean(Licor_Press_2) mean(Licor_Temp_2) wohj_2 wlhj_2 wocj_2 wlcj_2 std(Licor_H2O_2) std(LicH2O_2) std(Licor_CO2_2) std(LicCO2_2) std(Licor_Press_2) 
         ddd+hhh/24+20/24/60 mean(Licor_H2O_3) mean(LicH2O_3) mean(Licor_CO2_3) mean(LicCO2_3) mean(Licor_Press_3) mean(Licor_Temp_3) wohj_3 wlhj_3 wocj_3 wlcj_3 std(Licor_H2O_3) std(LicH2O_3) std(Licor_CO2_3) std(LicCO2_3) std(Licor_Press_3) 
         ddd+hhh/24+30/24/60 mean(Licor_H2O_4) mean(LicH2O_4) mean(Licor_CO2_4) mean(LicCO2_4) mean(Licor_Press_4) mean(Licor_Temp_4) wohj_4 wlhj_4 wocj_4 wlcj_4 std(Licor_H2O_4) std(LicH2O_4) std(Licor_CO2_4) std(LicCO2_4) std(Licor_Press_4) 
         ddd+hhh/24+40/24/60 mean(Licor_H2O_5) mean(LicH2O_5) mean(Licor_CO2_5) mean(LicCO2_5) mean(Licor_Press_5) mean(Licor_Temp_5) wohj_5 wlhj_5 wocj_5 wlcj_5 std(Licor_H2O_5) std(LicH2O_5) std(Licor_CO2_5) std(LicCO2_5) std(Licor_Press_5) 
         ddd+hhh/24+50/24/60 mean(Licor_H2O_6) mean(LicH2O_6) mean(Licor_CO2_6) mean(LicCO2_6) mean(Licor_Press_6) mean(Licor_Temp_6) wohj_6 wlhj_6 wocj_6 wlcj_6 std(Licor_H2O_6) std(LicH2O_6) std(Licor_CO2_6) std(LicCO2_6) std(Licor_Press_6)];
%                  1                2             3               4                 5               6                    7              8     9     10     11           12             13               14            15              16            
if isempty(lagtime_1)
    lagtime_1=NaN;
    flux_1_o=NaN;
end;
if isempty(lagtime_2)
    lagtime_2=NaN;
    flux_2_o=NaN;
end;
if isempty(lagtime_3)
    lagtime_3=NaN;
    flux_3_o=NaN;
end;
if isempty(lagtime_4)
    lagtime_4=NaN;
    flux_4_o=NaN;
end;
if isempty(lagtime_5)
    lagtime_5=NaN
    flux_5_o=NaN;
end;
if isempty(lagtime_6)
    lagtime_6=NaN;
    flux_6_o=NaN; 
end;

fluxes=[ddd+hhh/24+00/24/60 lagtime_1 O3mean_1_o O3stdev_1_o flux_1_o O3mean_1 O3stdev_1 flux_1 slowO3mean_1_o slowO3stdev_1_o
         ddd+hhh/24+10/24/60 lagtime_2 O3mean_2_o O3stdev_2_o flux_2_o O3mean_2 O3stdev_2 flux_2 slowO3mean_2_o slowO3stdev_2_o
         ddd+hhh/24+20/24/60 lagtime_3 O3mean_3_o O3stdev_3_o flux_3_o O3mean_3 O3stdev_3 flux_3 slowO3mean_3_o slowO3stdev_3_o
         ddd+hhh/24+30/24/60 lagtime_4 O3mean_4_o O3stdev_4_o flux_4_o O3mean_4 O3stdev_4 flux_4 slowO3mean_4_o slowO3stdev_4_o
         ddd+hhh/24+40/24/60 lagtime_5 O3mean_5_o O3stdev_5_o flux_5_o O3mean_5 O3stdev_5 flux_5 slowO3mean_5_o slowO3stdev_5_o
         ddd+hhh/24+50/24/60 lagtime_6 O3mean_6_o O3stdev_6_o flux_6_o O3mean_6 O3stdev_6 flux_6 slowO3mean_6_o slowO3stdev_6_o];
%                  1              2         3       4            5       6
%                                                                                   7       8       9

%2-4=unfiltered O3 old method
%5-7=unfiltered O3 new method

if ddd>9,
   if hhh>9,
      dfx=[num2str(ddd),num2str(hhh)];
   else
      dfx=[num2str(ddd),'0',num2str(hhh)];
   end;
else
   if hhh>9,
      dfx=['0',num2str(ddd),num2str(hhh)];
   else
      dfx=['0',num2str(ddd),'0',num2str(hhh)];
   end;
end;

sdg=['save ', drive_w, '\procturb\da_0',dfx,'.prn newview -ascii'];
disp(sdg)
eval(sdg)

sdg=['save ', drive_w, '\procturb\sp_0',dfx,'.prn specview -ascii'];
disp(sdg)
eval(sdg)

sdg=['save ', drive_w, '\procturb\co_0',dfx,'.prn co2view -ascii'];
disp(sdg)
eval(sdg)

sdg=['save ', drive_w, '\procturb\cr_0',dfx,'.prn co2res -ascii'];
disp(sdg)
eval(sdg)

sdg=['save ', drive_w, '\procturb\fl_0',dfx,'.prn fluxes -ascii'];
disp(sdg)
eval(sdg)
if clearit
clear U V W
clear Uj_1 Uj_2 Uj_3 Uj_4 Uj_5 Uj_6 
clear Vj_1 Vj_2 Vj_3 Vj_4 Vj_5 Vj_6 
%clear Wj_1 Wj_2 Wj_3 Wj_4 Wj_5 Wj_6 
clear Ustream Ustream_1 Ustream_2 Ustream_3 Ustream_4 Ustream_5 Ustream_6 
clear Vstream Vstream_1 Vstream_2 Vstream_3 Vstream_4 Vstream_5 Vstream_6 
clear Wstream Wstream_1 Wstream_2 Wstream_3 Wstream_4 Wstream_5 Wstream_6 
clear Tsonic Tsonic_1 Tsonic_2 Tsonic_3 Tsonic_4 Tsonic_5 Tsonic_6 
%clear abshum abshum_1 abshum_2 abshum_3 abshum_4 abshum_5 abshum_6clear CO2 H2O
clear sst abshum
%clear Uj_1 Vj_1 Wj_1 Uj_2 Vj_2 Wj_2 Uj_3 Vj_3 Wj_3 Uj_4 Vj_4 Wj_4 Uj_5 Vj_5 Wj_5 Uj_6 Vj_6 Wj_6
clear specsize_1 specsize_2 specsize_3 specsize_4 specsize_5 specsize_6
end;

if clearit
clear Fson Su_1 Sw_1 St_1 Sq_1 So_1 Sc_1 Ch_1 Ch_2 Ch_3 Ch_4 Ch_5 Ch_6 Chs_1 Chs_2 qsid_1_1
clear Chs_3 Chs_4 Chs_5 Chs_6 Cq2 Ct2 Cu2 F Fo Ct_1 Ct_2 Ct_3 Ct_4 Ct_5 Ct_6 qsid_1_2
clear Cts_1 Cts_2 Cts_3 Cts_4 Cts_5 Cts_6 Cu_1 Cu_2 Cu_3 Cu_4 Cu_5 Cu_6 qsid_2_1 qsid_2_2
clear Cus_1 Cus_2 Cus_3 Cus_4 Cus_5 Cus_6 Cv_1 Cv_2 Cv_3 Cv_4 Cv_5 Cv_6 qsid_3_1 qsid_3_2
clear Cvs_1 Cvs_2 Cvs_3 Cvs_4 Cvs_5 Cvs_6 Cw_1 Cw_2 Cw_3 Cw_4 Cw_5 Cw_6 qsid_4_1 qsid_4_2
clear Cws_1 Cws_2 Cws_3 Cws_4 Cws_5 Cws_6 Esat_1 Esat_2 Esat_3 Esat_4 Esat_5 Esat_6 rain_1
clear Q_1 Q_2 Q_3 Q_4 Q_5 Q_6 L LongIR1 LongIR2 P Q Qsea_1 Qsea_2 Qsea_3 qsid_5_1
clear Qsea_4 Qsea_5 Qsea_6 RHv Solar1 Solar2 T TT Tairv Tsea Tsea_snake Tv Us alphau c
clear Uid_1 Uid_2 Uid_3 Uid_4 Uid_5 Uid_6 c_coeff cog daytest fmax g qsid_5_2 qsid_6_1
clear head hourtest iii lat long qs0 medA_1 medA_2 medA_3 medA_4 medA_5 medA_6 qsid_6_2
clear stdA_1 stdA_2 stdA_3 stdA_4 stdA_5 stdA_6 qs_ib rho sog ts0 u_id us0 rain_2 rain_3
clear us_ib vkon x zet z zi x_1 x_2 x_3 x_4 x_5 x_6 zq zt zu zzz Qs Qs_10 RH rain_4 rain_5
clear F1_1 F1_2 F1_3 F1_4 F1_5 F1_6 F2_1 F2_2 F2_3 F2_4 F2_5 F2_6 F3_1 F3_2 F3_3 F3_4 
clear F3_5 F3_6 Cc2 Cc2_1_1 Cc2_1_2 Cc2_2_1 Cc2_2_2 Cc2_3_1 Cc2_3_2 Cc2_4_1 Cc2_4_2 rain_6
clear Cc2_5_1 Cc2_5_2 Cc2_6_1 Cc2_6_2 Cle Co2 Co2_1_1 Co2_1_2 Co2_2_1 Co2_2_2 Co2_3_1 
clear Co2_3_2 Co2_4_1 Co2_4_2 Co2_5_1 Co2_5_2 Co2_6_1 Co2_6_2 Cp Cq2_1_1 Cq2_1_2 rho_1
clear Cq2_2_1 Cq2_2_2 Cq2_3_1 Cq2_3_2 Cq2_4_1 Cq2_4_2 Cq2_5_1 Cq2_5_2 Cq2_6_1 Cq2_6_2 rho_2
clear Cqw_1 Cqw_2 Cqw_3 Cqw_4 Cqw_5 Cqw_6 Ct2_1_1 Ct2_2_1 Ct2_3_1 Ct2_4_1 Ct2_5_1 rho_3
clear Ct2_6_1 Ct2_1_2 Ct2_2_2 Ct2_3_2 Ct2_4_2 Ct2_5_2 Ct2_6_2 Ctw_1 Ctw_2 Ctw_3 Ctw_4 rho_4
clear Ctw_5 Ctw_6 Cu2_1_1 Cu2_2_1 Cu2_3_1 Cu2_4_1 Cu2_5_1 Cu2_6_1 Cuw_1 Cuw_2 Cuw_3 Cuw_4
clear Cu2_1_2 Cu2_2_2 Cu2_3_2 Cu2_4_2 Cu2_5_2 Cu2_6_2 Cuw_5 Cuw_6 Cuwj_1 Cuwj_2 Cuwj_3
clear Cuwj_4 Cuwj_5 Cuwj_6 Cvw_1 Cvw_2 Cvw_3 Cvw_4 Cvw_5 Cvw_6 Cw2_1_1 Cw2_1_2 Cw2_2_1 sev
clear Cvwj_1 Cvwj_2 Cvwj_3 Cvwj_4 Cvwj_5 Cvwj_6 Cw2_2_2 Cw2_3_1 Cw2_3_2 Cw2_4_1 Cw2_4_2
clear Cw2_5_1 Cw2_5_2 Cw2_6_1 Cw2_6_2 Esatsnake_1 Esatsnake_2 Esatsnake_3 rho_5 rho_6 sdg
clear Esatsnake_4 Esatsnake_5 Esatsnake_6 Lat_1 Lat_2 Lat_3 Lat_4 Lat_5 Lat_6 ship_RH_1
clear Lon_1 Lon_2 Lon_3 Lon_4 Lon_5 Lon_6 Lid_1_1 Lid_1_2 ship_RH_2 ship_RH_3 ship_RH_4
clear Lid_2_1 Lid_2_2 Lid_3_1 Lid_3_2 Lid_4_1 Lid_4_2 Lid_5_1 Lid_5_2 Lid_6_1 Lid_6_2 
clear Qbarv_1 Qbarv_2 Qbarv_3 Qbarv_4 Qbarv_5 Qbarv_6 Qseasnake_1 Qseasnake_2 Qseasnake_3
clear Qseasnake_4 Qseasnake_5 Qseasnake_6 Qship_1 Qship_2 Qship_3 Qship_4 Qship_5 Qship_6
clear RHv_1 RHv_2 RHv_3 RHv_4 RHv_5 RHv_6 Reldir_1 Reldir_2 Reldir_3 Reldir_4 Reldir_5
clear Reldir_6 Rl_1 Rl_2 Rl_3 Rl_4 Rl_5 Rl_6 Rs_1 Rs_2 Rs_3 Rs_4 Rs_5 Rs_6 Scos_4 Scos_5
clear Rs_ship_1 Rs_ship_2 Rs_ship_3 Rs_ship_4 Rs_ship_5 Rs_ship_6 Scos_1 Scos_2 Scos_3
clear Scos_6 Sp1_1 Sp1_2 Sp2_1 Sp2_2 Sp3_1 Sp3_2 Sq_2 Sq_3 Sq_1 Sq_4 Sq_5 Sq_6 Ssin_1 
clear Sp1_1 Sp1_2 Sp1_3 Sp1_4 Sp1_5 Sp1_6 Sp2_1 Sp2_2 Sp2_3 Sp2_4 Sp2_5 Sp2_6 ship_RH_5
clear Sp3_1 Sp3_2 Sp3_3 Sp3_4 Sp3_5 Sp3_6 betaj_1 betaj_2 betaj_3 betaj_4 betaj_5 betaj_6 
clear min_cog_1 min_cog_2 min_cog_3 min_cog_4 min_cog_5 min_cog_6 ship_RH_6 ship_rain_1
clear min_dir_1 min_dir_2 min_dir_3 min_dir_4 min_dir_5 min_dir_6 ship_rain_2 ship_rain_3
clear min_gpscog_1 min_gpscog_2 min_gpscog_3 min_gpscog_4 min_gpscog_5 min_gpscog_6 
clear min_gpsspeed_1 min_gpsspeed_2 min_gpsspeed_3 min_gpsspeed_4 min_gyro_6 ship_rain_4
clear min_gpsspeed_5 min_gpsspeed_6 min_gyro_1 min_gyro_2 min_gyro_3 min_gyro_4 min_gyro_5
clear min_head_1 min_head_2 min_head_3 min_head_4 min_head_5 min_head_6 ship_rain_5
clear min_sog_1 min_sog_2 min_sog_3 min_sog_4 min_sog_5 min_sog_6 osid_6_1 osid_6_2
clear min_wind_1 min_wind_2 min_wind_3 min_wind_4 min_wind_5 min_wind_6 osid_4_2 osid_5_1
clear newview os0 os_ib osid_1_1 osid_1_2 osid_2_1 osid_2_2 osid_3_1 osid_3_2 osid_4_1
clear Sp1_1 Sp1_2 Sp1_3 Sp1_4 Sp1_5 Sp1_6 Sp2_1 Sp2_2 Sp2_3 Sp2_4 Sp2_5 Sp2_6 osid_5_2
clear Ssin_2 Ssin_3 Ssin_4 Ssin_5 Ssin_6 St_1 St_2 St_3 St_4 St_5 St_6 Su_1 Su_2 Su_3 
clear Su_4 Su_5 Su_6 Suj_1 Suj_2 Suj_3 Suj_4 Suj_5 Suj_6 Sv_1 Sv_2 Sv_3 Sv_4 Sv_5 Sv_6
clear Svj_1 Svj_2 Svj_3 Svj_4 Svj_5 Svj_6 Sw_1 Sw_2 Sw_3 Sw_4 Sw_5 Sw_6 Swj_1 Swj_2 Swj_3
clear Swj_4 Swj_5 Swj_6 Tbar_1 Tbar_2 Tbar_3 Tbar_4 Tbar_5 Tbar_6 Tsea_1 Tsea_2 Tsea_3
clear Tsea_4 Tsea_5 Tsea_6 Tseasnake_1 Tseasnake_2 Tseasnake_3 Tseasnake_4 Tseasnake_5
clear Tseasnake_6 Tship_1 Tship_2 Tship_3 Tship_4 Tship_5 Tship_6 Twindir_1 Twindir_2
%clear Twindir_3 Twindir_4 Twindir_5 Twindir_6 Ubar_1 Ubar_2 Ubar_3 Ubar_4 Ubar_5 Ubar_6
clear Ubarj_1 Ubarj_2 Ubarj_3 Ubarj_4 Ubarj_5 Ubarj_6 Ue_1 Ue_2 Ue_3 Ue_4 Ue_5 Ue_6
clear Un_1 Un_2 Un_3 Un_4 Un_5 Un_6 Ustr_1 Ustr_2 Ustr_3 Ustr_4 Ustr_5 Ustr_6 Zpoly
%clear abshum_6 alpha_1 alpha_2 alpha_3 alpha_4 alpha_5 alpha_6 beta_1 beta_2 beta_3 beta_4
clear beta_5 beta_6 alphaj_1 alphaj_2 alphaj_3 alphaj_4 alphaj_5 alphaj_6 betaj_1 betaj_2 
clear betaj_3 betaj_4 beta_5 beta_6 cO cS cs0 cs_ib csid_1_1 csid_1_2 csid_2_1 csid_2_2
clear csid_3_1 csid_3_2 csid_4_1 csid_4_2 csid_5_1 csid_5_2 csid_6_1 csid_6_2 dfx fmaxO
clear fmaxT fmaxU fminO fminT fminU max_cog_1 max_cog_2 max_cog_3 max_cog_4 max_cog_5 
clear max_cog_6 max_dir_1 max_dir_2 max_dir_3 max_dir_4 max_dir_5 max_dir_6 max_gpscog_1
clear max_gpscog_2 max_gpscog_3 max_gpscog_4 max_gpscog_5 max_gpscog_6 max_gpsspeed_1
clear max_gpsspeed_2 max_gpsspeed_3 max_gpsspeed_4 max_gpsspeed_5 max_gpsspeed_6 specview
clear max_gyro_1 max_gyro_2 max_gyro_3 max_gyro_4 max_gyro_5 max_gyro_6 ship_rain_6 
clear max_head_1 max_head_2 max_head_3 max_head_4 max_head_5 max_head_6 std_H2Op_1
clear max_sog_1 max_sog_2 max_sog_3 max_sog_4 max_sog_5 max_sog_6 std_H2Op_2 std_H2Op_3
clear max_wind_1 max_wind_2 max_wind_3 max_wind_4 max_wind_5 max_wind_6 std_H2Op_4
clear mean_H2Op_1 mean_H2Op_2 mean_H2Op_3 mean_H2Op_4 mean_H2Op_5 mean_H2Op_6 std_H2Op_5
clear mean_Tsonic_1 mean_Tsonic_2 mean_Tsonic_3 mean_Tsonic_4 mean_Tsonic_5 mean_Tsonic_6 
clear mean_cog_1 mean_cog_2 mean_cog_3 mean_cog_4 mean_cog_5 mean_cog_6 std_H2Op_6
clear mean_dir_1 mean_dir_2 mean_dir_3 mean_dir_4 mean_dir_5 mean_dir_6 mean_gpsspeed_6
clear mean_gpscog_1 mean_gpscog_2 mean_gpscog_3 mean_gpscog_4 mean_gpscog_5 mean_gpscog_6
clear mean_gpsspeed_1 mean_gpsspeed_2 mean_gpsspeed_3 mean_gpsspeed_4 mean_gpsspeed_5
clear mean_gyro_1 mean_gyro_2 mean_gyro_3 mean_gyro_4 mean_gyro_5 mean_gyro_6 std_Rl_1
clear mean_gyro_1 mean_gyro_2 mean_gyro_3 mean_gyro_4 mean_gyro_5 mean_gyro_6 std_Rl_2
clear mean_sog_1 mean_sog_2 mean_sog_3 mean_sog_4 mean_sog_5 mean_sog_6 std_Rl_3 std_Rl_4
clear mean_wind_1 mean_wind_2 mean_wind_3 mean_wind_4 mean_wind_5 mean_wind_6 std_Rl_5
clear med_H2Op_1 med_H2Op_2 med_H2Op_3 med_H2Op_4 med_H2Op_5 med_H2Op_6 std_Rl_6 std_Rs_1
clear med_absorb_1 med_absorb_2 med_absorb_3 med_absorb_4 med_absorb_5 med_absorb_6
clear med_clear_1 med_clear_2 med_clear_3 med_clear_4 med_clear_5 med_clear_6 std_Rs_2
clear Cvw_1 Cvw_2 std_Rs_3 std_Rs_4 std_Rs_5 std_Rs_6 std_absorb_1 std_absorb_2 std_clear_1
clear std_absorb_3 std_absorb_4 std_absorb_5 std_absorb_6 std_clear_2 std_clear_3
clear std_clear_4 std_clear_5 std_clear_6 std_cog_1 std_cog_2 std_cog_3 std_cog_4 std_cog_5
clear std_cog_6 std_dir_1 std_dir_2 std_dir_3 std_dir_4 std_dir_5 std_dir_6 std_gpscog_1
clear std_gpscog_2 std_gpscog_3 std_gpscog_4 std_gpscog_5 std_gpscog_6 std_gyro_1
clear std_gyro_2 std_gyro_3 std_gyro_4 std_gyro_5 std_gyro_6 std_head_1 std_head_2
clear std_head_3 std_head_4 std_head_5 std_head_6 std_sog_1 std_sog_2 std_sog_3 std_sog_4
clear std_sog_5 std_sog_6 std_wind_1 std_wind_2 std_wind_3 std_wind_4 std_wind_5
clear std_wind_6 ts_ib tsid_1_1 tsid_1_2 tsid_2_1 tsid_2_2 tsid_3_1 tsid_3_2 tsid_4_1
clear tsid_4_2 tsid_5_1 tsid_5_2 tsid_6_1 tsid_6_2 usid_1_1 usid_1_2 usid_2_1 usid_2_2
clear usid_3_1 usid_3_2 usid_4_1 usid_4_2 usid_5_1 usid_5_2 usid_6_1 usid_6_2 wa_1 wa_2
clear wa_3 wa_4 wa_5 wa_6 waj_1 waj_2 waj_3 waj_4 waj_5 waj_6 wt_1 wt_2 wt_3 wt_4 wt_5
clear wt_6 wtj_1 wtj_2 wtj_4 wtj_3 wtj_5 wtj_6 wu_1 wu_2 wu_3 wu_4 wu_5 wu_6 wuj_1 wuj_2
clear wuj_3 wuj_4 wuj_5 wuj_6 wv_1 wv_2 wv_3 wv_4 wv_5 wv_6 wvj_1 wvj_2 wvj_3 wvj_4 wvj_5
clear wvj_6 xship_1 xship_2 xship_3 xship_4 xship_5 xship_6 xsnake_1 xsnake_2 xsnake_3
clear xsnake_4 xsnake_5 xsnake_6 yship_1 yship_2 yship_3 yship_4 yship_5 yship_6 ysnake_1
clear ysnake_2 ysnake_3 ysnake_4 ysnake_5 ysnake_6 y z_q z_u zet_q zet_u zq_ship zt_ship
clear zu_ship odec abshum
clear vdO3E vdO3H suH svH swH sTH sOH cwuH cwvH cwTH cwOH suE svE swE sTE sOE cwuE cwvE cwTE cwOE ozone
clear dtO wO3 wO3j dtOj 
end;

