clear all;
close all;
set_machine_et;
%%% Info: program to save text file of PISTON dsd data from .mat file

%%%     --- code for PISTON run by E. Thompson
%              Elizabeth Thompson
%              Applied Physics Lab, University of Washington
%              eliz@apl.uw.edu

%%%     --- code originally developed by K. Drushka
%              Kyla Drushka
%              Applied Physics Lab, University of Washington
%              kdrushka@apl.uw.edu
%              see nc_init.m and nc_init_dsd.m

%  files processed by ET --------------------------------------------------

version_num = 'v3';

cruise = 'PISTON'; yrstr = '2018'; 
% cruise = 'SPURS2'; yrstr = '2016-2017';
% cruise = 'TGT'; yrstr = '2017-2018';
cruise_str = [cruise '_' yrstr];
cruise_title = [cruise ' ' yrstr];

ncfilename = [cruise_str '_dsd_level_1_' version_num '.nc'];

thepath = ['/Users/eliz/Dropbox/DSD/' cruise '/final/'];
metdir = ['/Users/eliz/Dropbox/DSD/' cruise '/ship_met/'];
plotdir = ['/Users/eliz/Dropbox/DSD/' cruise '/analysis/'];

ncload2([thepath ncfilename]);

if strcmp(cruise,'SPURS2') == 1
    m_s16 = load([metdir 'SPURS2_2016_OneMinute_Met_Rev3']); %%% SPURS-2 2016 met data
    m_s17 = load([metdir 'SPURS2_2017_OneMinute_Met_Rev3']); %%% SPURS-2 2017 met data
    R_m = [m_s16.Prate  m_s17.Prate]';
    yd_m = [m_s16.Yday  m_s17.Yday]';
    t_m = [m_s16.Yday + datenum(2016,0,0,0,0,0)  m_s17.Yday + datenum(2017,0,0,0,0,0)]';
elseif strcmp(cruise,'PISTON') == 1
    
end

%% check attributes 

disp('----------------------------------');

ncdisp([thepath ncfilename]);

disp('----------------------------------');

%% plot tests

plot_tests = 0;
if plot_tests == 1
    
figure; 
subplot(3,2,1); hold on;
plot(t, year,'.');
plot(t(bad_data == 1), year(bad_data == 1),'x');
ylabel('year');
datetick('x','mm/dd','keeplimits');
subplot(3,2,2); hold on;
plot(t, month,'.');
plot(t(bad_data == 1), month(bad_data == 1),'x');
ylabel('month');
datetick('x','mm/dd','keeplimits');
title(cruise_title);
subplot(3,2,3); hold on;
plot(t, day,'.');
plot(t(bad_data == 1), day(bad_data == 1),'x');
ylabel('day');
datetick('x','mm/dd','keeplimits');
subplot(3,2,4); hold on;
plot(t, hour,'.');
plot(t(bad_data == 1), hour(bad_data == 1),'x');
ylabel('hour');
datetick('x','mm/dd','keeplimits');
subplot(3,2,5); hold on;
plot(t, minute,'.');
plot(t(bad_data == 1), minute(bad_data == 1),'x');
ylabel('min');
datetick('x','mm/dd','keeplimits');
subplot(3,2,6); hold on;
plot(t, tsince1950,'.');
plot(t(bad_data == 1), tsince1950(bad_data == 1),'x');
legend('good','bad','location','best');
ylabel('sec since 1950 Jan 1');
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_time_' version_num],'-png','-nocrop','-r300');

figure;
subplot(2,1,1); hold on; grid on;
plot(t, voltage,'.');
plot(t(bad_data == 1), voltage(bad_data == 1),'x');
title(cruise_title);
ylabel('voltage');
datetick('x','mm/dd','keeplimits');
subplot(2,1,2); hold on; grid on;
plot(t, urel_anem,'.');
plot(t(bad_data == 1), urel_anem(bad_data == 1),'x');
legend('good','bad','location','best');
ylabel('urel');
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_time_meta_' version_num],'-png','-nocrop','-r300');

figure;
subplot(1,3,1); hold on; grid on;
plot(D, vfall,'.');
plot(D(D > 15), vfall(D > 15),'x');
ylabel('vfall');
xlabel('D');
subplot(1,3,2); hold on; grid on;
plot(D, mass,'.');
plot(D(D > 15), mass(D > 15),'x');
title(cruise_title);
ylabel('mass');
xlabel('D');
subplot(1,3,3); hold on; grid on;
plot(D, dD,'.');
plot(D(D > 15), dD(D > 15),'x');
legend('good','bad','location','best');
ylabel('dD');
xlabel('D');
export_fig([plotdir cruise '_nctest_D_meta_' version_num],'-png','-nocrop','-r300');


figure;
wh_bad_D = find(D > 15,1,'first');
subplot(2,1,1); hold on; grid on;
plot(t, Dmax,'.');
plot(t(bad_data == 1), Dmax(bad_data == 1),'x');
title(cruise_title);
hold on; grid on;
grid on;
plot(xlim,[D(wh_bad_D) D(wh_bad_D)],'--');
ylabel('Dmax mm');
datetick('x','mm/dd','keeplimits');
subplot(2,1,2); hold on; grid on;
plot(t, Dmax_i,'.');
plot(t(bad_data == 1), Dmax_i(bad_data == 1),'x');
ylabel('Dmax ID #');
hold on; grid on;
grid on;
plot(xlim,[bin_nums(wh_bad_D) bin_nums(wh_bad_D)],'--');
legend('good','bad','bad D limit','location','best');
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_Dmax_' version_num],'-png','-nocrop','-r300');

figure;
subplot(2,1,1); hold on; grid on;
plot(t, nsumdrops,'o');
plot(t(bad_data == 1), nsumdrops(bad_data == 1),'x');
title(cruise_title);
ylabel('nsumdrops');
datetick('x','mm/dd','keeplimits');
subplot(2,1,2); hold on; grid on;
plot(t, ndrops(50,:),'o');
plot(t(bad_data == 1), ndrops(50,bad_data == 1),'x');
legend('good','bad','location','best');
ylabel(['N(D) for D = ' sprintf('%5.2f',D(50)) ' mm']);
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_nd_' version_num],'-png','-nocrop','-r300');

figure;
subplot(2,1,1); hold on; grid on;
if strcmp(cruise,'SPURS2') == 1
    plot(t_m, R_m,'-','color',rgb('gray'));
end
plot(t, P,'o');
plot(t, Pfortran,'vk','markersize',3);
plot(t(bad_data == 1), P(bad_data == 1),'x');
if strcmp(cruise,'SPURS2') == 1
    legend('ship gauge','good ET','Kyla fortran','bad ET','location','best');
end
ylabel('P');
yt = get(gca,'ylim');
title(cruise_title);
datetick('x','mm/dd','keeplimits');
subplot(2,1,2); hold on; grid on;
plot(t, Pd(50,:),'o');
plot(t(bad_data == 1), Pd(50,bad_data == 1),'x');
legend('good','bad','location','best');
ylim(yt);
ylabel(['P(D) for D = ' sprintf('%5.2f',D(50)) ' mm']);
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_P_' version_num],'-png','-nocrop','-r300');

figure;
subplot(2,1,1); hold on; grid on;
plot(t, z,'o');
plot(t(bad_data == 1), z(bad_data == 1),'x');
ylabel('z');
yt = get(gca,'ylim');
title(cruise_title);
datetick('x','mm/dd','keeplimits');
subplot(2,1,2); hold on; grid on;
plot(t, zd(50,:),'o');
plot(t(bad_data == 1), zd(50,bad_data == 1),'x');
legend('good','bad','location','best');
ylim(yt);
ylabel(['z(D) for D = ' sprintf('%5.2f',D(50)) ' mm']);
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_z_' version_num],'-png','-nocrop','-r300');

figure;
subplot(2,1,1); hold on; grid on;
plot(t, Z,'o');
plot(t(bad_data == 1), Z(bad_data == 1),'x');
ylabel('Z');
yt = get(gca,'ylim');
title(cruise_title);
datetick('x','mm/dd','keeplimits');
subplot(2,1,2); hold on; grid on;
plot(t, Zd(50,:),'o');
plot(t(bad_data == 1), Zd(50,bad_data == 1),'x');
legend('good','bad','location','best');
ylim(yt);
ylabel(['Z(D) for D = ' sprintf('%5.2f',D(50)) ' mm']);
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_Z_' version_num],'-png','-nocrop','-r300');

figure;
subplot(2,1,1); hold on; grid on;
plot(t, W,'o');
plot(t(bad_data == 1), W(bad_data == 1),'x');
ylabel('W');
yt = get(gca,'ylim');
title(cruise_title);
datetick('x','mm/dd','keeplimits');
subplot(2,1,2); hold on; grid on;
plot(t, Wd(50,:),'o');
plot(t(bad_data == 1), Wd(50,bad_data == 1),'x');
legend('good','bad','location','best');
ylim(yt);
ylabel(['W(D) for D = ' sprintf('%5.2f',D(50)) ' mm']);
datetick('x','mm/dd','keeplimits');
export_fig([plotdir cruise '_nctest_W_' version_num],'-png','-nocrop','-r300');

end