% Adjustment to da results.  Assumes DA variables for the project have been
% loaded in the HiWinGS2013_da_red1.m script.

disp('HiWinGS2013_fixit_da1')
% oo = find(MMF.yday>=jdy(1) &MMF.yday<=jdy(end)); size(oo)
% pp = find(jdy>=MMF.yday(1) & jdy<=MMF.yday(end)); size(pp)

% zi = 600*ones(size(U));
% bulk_flux10m = coare30vn_ref(U,zu_etl,ta,zt_etl,PSDvais,zq_etl,press,tsnk,rs,rl,Lat,zi,10,10,10);

%---------  WS ------------------------------------------------------------
figure;plot(jdy,U,'r.',jdy,U_scs,'.');
title(' True wind speed from relative to earth(m/s), blue=ship, red=PSD');
% U_scs(U_scs>22)=NaN;
% figure;plot(jdy,U,'r.',jdy,bulk_flux10m(:,19),'.',MMF.yday,MMF.U10,'k');
% title(' True wind speed from relative to earth(m/s), blue=PSD10m, red=PSD, black=MeanMetFiles (10m)');
%---------  WD ------------------------------------------------------------   
figure;plot(jdy,dir,'r.',jdy,dir_scs,'.');
title(' True wind direction from relative to earth (deg), blue=ship, red=PSD');
%---------  Tsea ------------------------------------------------------------      
figure;plot(jdy,x(:,18),'r.',jdy,tsg,'.');
title(' SST(degC), blue=ship tsg, red=PSD seasnake');ylim([0 12])
% fixes preiod when the snake was flying off the air due to strong winds
% or when it was on deck...
x(jdy>=288.2& jdy<=288.7,18) = tsg(jdy>=288.2& jdy<=288.7)-nanmedian(abs(x(:,18)-tsg));
oo = find(abs(x(:,18)-tsg)>=0.15);
x(oo,18) = tsg(oo)-nanmedian(abs(x(:,18)-tsg));
% x(oo,18)=tsg(oo)-0.1;
hold on;plot(jdy,(x(:,18)),'g.')
tsnk = x(:,18);
%---------  RH ------------------------------------------------------------      
figure;plot(jdy,x(:,47),'r.',jdy,x(:,48));
title(' Rh (%), blue=ship tsg, red=PSD');grid
PSDvais = x(:,47);
% oo=find(MMF.yday>=jdy(1) &MMF.yday<=jdy(end));size(oo)
% pp=find(jdy>=MMF.yday(1) & jdy<=MMF.yday(end));size(pp)
% figure;plot(x(pp,47),MMF.RH10(oo)','.',[65 100],[65 100],'k--');grid
   
% % ref=[0.03 11.26 32.88 74.55 97.6];
% % PSDvais=[-0.83 10.31 31.31 69.89 91.8];
% % figure;plot(ref,PSDvais,'+',[0 100],[0 100],'k--');grid
% % %-->PSD=0.95*ref-0.41
% % figure;plot(jdy,((x(:,47)-3)+0.41)/0.95,'r.',jdy,x(:,48),'.',MMF.yday,MMF.RH10,'k');title(' SST(degC), blue=ship tsg, red=PSD, black=MeanMetFiles (10m)');grid
% % PSDvais=((x(:,47)-3)+0.41)/0.95; %uses cal from Vaisala
% % oo=find(abs(PSDvais(pp)-MMF.RH10')>=2);PSDvais(pp(oo))=MMF.RH10(oo); %gets rid of funky period where fan behaved badly
% % hold on;;plot(jdy,PSDvais,'m+')
%--------  Ta ------------------------------------------------------------        
figure;plot(jdy,ta,'r.',jdy,ta_im,'.');
title('Air temperature (degC), blue=ship, red=PSD');
% figure;plot(ta(pp),MMF.T10(oo)','.',[23 30],[23 30],'k--');grid
%---------  Qa ------------------------------------------------------------        
qa = qair_p([ta PSDvais press]);
figure;plot(jdy,qa,'r.',jdy,qa_im,'.');
title('Air specific humidity (g/kg), blue=ship, red=PSD');grid   
% figure;plot(qa(pp),MMF.Q10(oo)','.',[15 21],[15 21],'k--');grid
%---------  Qsea ------------------------------------------------------------       
qse = qsea_p([tsnk press]); 
figure;plot(jdy,qse,'r.',jdy,qs_tsg,'.');
title(' Specific humidity (g/Kg) ''near'' ocean surface, blue=ship, red=PSD');grid
%---------  Rs ------------------------------------------------------------         
figure;plot(jdy,rs,'r.',jdy,rs_im,'.');
title('downward solar flux, W/m^2, blue=ship, red=PSD');grid
% figure;plot(rs(pp),-MMF.Solardn(oo)','.',[-200 1200],[-200 1200],'k--');grid
%---------  Rl ------------------------------------------------------------           
figure;plot(jdy,rl,'r.',jdy,rl_im,'.');
title('downward ir flux, W/m^2, blue=ship, red=PSD');grid
%---------  Rain ------------------------------------------------------------           
figure;plot(jdy,org,'r.',jdy,rain_im,'.');
title('rainrate, mm/hr, blue=ship, red=PSD');grid
%---------  SOG/COG/Heading/Lat/Lon ------------------------------------------------------------           
figure;plot(jdy,ushp,'r.');title('SOG, red=PSD');grid
figure;plot(jdy,x(:,105),'r.');title('COG, red=PSD');grid
figure;plot(jdy,head,'r.');title('Heading, red=PSD');grid
figure;plot(jdy,Lat,'r.');title('Lat, red=PSD');grid
figure;plot(jdy,Lon,'r.');title('Lon, red=PSD');grid
%---------  Pressure ------------------------------------------------------------           
figure;plot(jdy,press,'r.');title('Pressure (mb), red=PSD');grid

%---------  fast humidity sensors 
q_lic = yxx(:,2);       % specific humidity from Licor unit (g/kg)
% figure;plot(jdy,qa,'g.--',jdy,q_lic,'r.--')
ii1 = find(q_lic>6 | q_lic<1);
yxx(ii1,2:5) = NaN;
yxx(ii1,8:end)=NaN;

wco2_lic = yxx(:,10);
P_lic = yxx(:,6);       % internal pressure on Licor
sgP_lic = yxx(:,16);

co2_lic = yxx(:,4);
ii = find(co2_lic<350 | co2_lic>450);
co2_lic(ii) = NaN;
yxx(ii,2:5) = NaN;
yxx(ii,8:end) = NaN;

sgq_lic = yxx(:,12);
ii1 = find(sgq_lic>3 | sgq_lic<0);
yxx(ii1,2:5) = NaN;
yxx(ii1,8:end) = NaN;
sgq_lic(ii1) = NaN;

sgco2_lic = yxx(:,14);
ii2 = find(sgco2_lic>20);
yxx(ii2,2:5) = NaN;
yxx(ii2,08:end) = NaN;
sgco2_lic(ii2) = NaN;

q_lic = yxx(:,2);
wq_lic = yxx(:,09);
ii = find(wq_lic<-.02 | wq_lic>0.1);
wq_lic(ii) = NaN;
wco2_lic(ii) = NaN;
wco2_lic = yxx(:,10);
co2_lic = yxx(:,4);
ii = find(wco2_lic<-.5 | wco2_lic>0.2);
wco2_lic(ii) = NaN;

% rq = qa./q_lic;
% fq = 1.05*max(1,1+.89*(rq-1.044));
% ii = find(isnan(q_lic));fq(ii)=NaN;
% wq_lic = wq_lic.*fq;  % correct for separation

% ii = find((tsnk-tsg>.2) | tsnk-tsg<-.4);
% tsnk(ii) = tsg(ii);   % snake out of the water

% fix some issues found in logbook
% jj = find(tsnk<16 | tsnk>22);tsnk(jj)=tsg(jj);  % tsg data limits
% % jj = find(jdy<=296.9);
% % tsnk(jj) = tsg(jj);  %replace tsnk by tsg when the snake was forgotten on the deck...
% % to be checked!

% seems to be some issues (NaN) going on with ship's speed...odec seems to
% have been out in leg2 and at the end of leg1
% jj = find(isnan(sig_u));
% sig_u(jj) = sig_uim(jj);
% jj = find(isnan(ushp));
% ushp(jj) = ushp_im(jj);

% jj = find(ta<02); ta(jj) = ta_im(jj);  % tsg data limits
% ii = find(qa>50); qa(ii) = qa_im(ii);
% ii = find(qa_im>50); qa_im(ii) = qa(ii);

% check covariances for reasonable limits
ii = find(wuj<-5 | wuj>.05); wuj(ii) = NaN;
ii = find(wu<-5| wu>.05); wu(ii) = NaN;
ii = find(wv<-1 | wv>1); wv(ii) = NaN;
ii = find(wvj<-1 | wvj>1); wvj(ii) = NaN;
ii = find(wt_son<-.1 | wt_son>.25); wt_son(ii) = NaN;

% toto = find(jdy<=315);
% ii = find(wuj(toto)<-.2 | wuj(toto)>.1); wuj(toto(ii)) = NaN;  %removes weird points at beginning and end
% ii = find(wu(toto)<-.2 | wu(toto)>.1); wu(toto(ii)) = NaN;     %removes weird points at beginning and end

% toto = find(jdy>340);
% ii = find(wuj(toto)<-.2 | wuj(toto)>.1); wuj(toto(ii)) = NaN;  %removes weird points at beginning and end
% ii = find(wu(toto)<-.2 | wu(toto)>.1); wu(toto(ii)) = NaN;     %removes weird points at beginning and end

% ii = find(qa<10); qa(ii) = qa_im(ii);

% check variances for reasonable limits
% ii = find(uu>6); uu(ii) = NaN;   % uu
% ii = find(vv>8); vv(ii) = NaN;   % vv
% ii = find(ww>1); ww(ii) = NaN;   % ww
% ii = find(tt>0.2); tt(ii) = NaN; % tt
% ii = find(uuj>6); uuj(ii) = NaN; % uu edson
% ii = find(vvj>6); vvj(ii) = NaN; % vv edson
% ii = find(wwj>1); wwj(ii) = NaN; % ww edson

% cu and cw
cup = cu;
cwp = cw;
ii = find(cup<0 |cup >1.3); cup(ii) = NaN; cu(ii) = NaN;
kk = find(cwp<0 |cwp >1); cwp(kk) = NaN; cw(kk) = NaN;

% ii = find(wq_lic<-.02 | wq_lic>0.12); wq_lic(ii) = NaN; wco2_lic(ii) = NaN;
%--> done in load_and_fix for specific Licors...

% ct & cq
ii = find(cql<1e-4 | cql>2); cql(ii) = NaN;
% toto = find(jdy<325); 
% ii = find(cql(toto)<1e-4 | cql(toto)>.3); wuj(toto(ii)) = NaN;   %removes weird points at beginning and end
% toto = find(jdy>335);
% ii = find(cql(toto)<1e-4 | cql(toto)>.25); wuj(toto(ii)) = NaN;  %removes weird points at beginning and end
%--> done in load_and_fix for specific Licors...

ii = find(ct<1e-6 | ct>0.6); ct(ii) = NaN;

% finds weird stuff with COG SCS and replace with PSD GPS
% x((jdy>314.8 & jdy<336.5),101) = x((jdy>314.8 & jdy<336.5),105);

% save changes
HiWinGS2013_Xfixit_da2;
