% compare_rain.m
% loads rain data for entire cruise and does daily plots

%% initialize
clear
close all
fig_path = 'D:\Data\CAPRICORN_2016\Investigator\flux\Processed_Images\rain';
mkdir(fig_path);

% matlab script path
path_prog = fullfile('c:\Users\Sergio\Documents\MATLAB\2016_CAPRICORN\da_red_analysis');
cd(path_prog);                  % cd to matlab directory
restoredefaultpath              % reset matlab path
addpath(genpath(path_prog));	% add scripts to path
savepath; rehash;

%% load data
dist_path = 'D:\Data\CAPRICORN_2016\Investigator\flux\SCS\distdrometer\';
met_path = 'D:\Data\CAPRICORN_2016\Investigator\flux\Processed_Raw\';
dst = load_dist(dist_path);
[met,scs] = load_1min_met(met_path);
cd(path_prog); 
% dst columns: jd, rain_rate, cum_rain
% met columns: jd, pir1, psp1, tc1, td1, Ts, Ta, RH, rain_rate, org_V, aspir, Pmb, rain_cum
% scs columns: jd, lat, lon, sog, cog, hed, wspd_port, wdir_port, rwspd_port,
%              rwdir_port, wspd_stbd, wdir_stbd, rwspd_stbd, rwdir_stbd, wspd_sonic,
%              wdir_sonic, rwspd_sonic, rwdir_sonic, ta_port, ta_stbd, rh_port,
%              rh_stbd, Pmb, rain, rain_rate, rain_optical, rain_rate_optical,
%              PSP_port, PSP_stbd, PIR_port, PIR_stbd, SST_IR, SST, Sal, uship_long,
%              uship_trans, SST_depth, sog_n, sog_w, hed_n, hed_w

met= met(1441:end,:);  % remove doy 74 not included in distdrometer data...
scs= scs(1441:end,:);

jd_dst = dst(:,1);
dst_rate = dst(:,2);
dst_cum = dst(:,3);
jd = met(:,1);
org_rate = met(:,9);
org_cum = met(:,13);
rmy_rate = scs(:,25);
rmy_cum = scs(:,24);
ta = met(:,7);
Urel = scs(:,9);

%% plot daily rain
stjd = floor(jd(1));
endjd = floor(jd(end));

for ii = stjd:endjd
    [m,d] = yd2md(str2double(2016), ii);
    Vdate = [str2double('2016'),m,d];
    jj = find(floor(jd_dst)==ii);
    kk = find(floor(jd)==ii);
    figure;
    subplot(2,1,1);
    plot(jd_dst(jj),dst_rate(jj),'r',jd(kk),org_rate(kk),'b',jd(kk),rmy_rate(kk),'g');
    grid; ylabel('rain rate, mm/hr'); xlabel('day-of-year');
    title(['Rain Comparison for Day ', sprintf('%3i',ii)]);
    subplot(2,1,2);
    h = plot(jd_dst(jj),dst_cum(jj),'r',jd(kk),org_cum(kk),'b',jd(kk),rmy_cum(kk),'g');
    set(h,'LineWidth',2); grid; ylabel('cumulative rain, mm'); xlabel('day-of-year');
    legend('distdrometer','ORG','RM Young','location','northwest');
    orient tall
    ppath = fullfile(fig_path,['CAPRICORN_2016_Rain_',sprintf('%04i_%02i_%02i_%03i',Vdate(1),Vdate(2),Vdate(3),ii),'.png']);
    print('-dpng',ppath);
    close all
end;

%% plot comparisons

% rain v snow?
% yy = ta<=5;
% zz = ta>5;
% figure; plot(org_rate(zz),dst_rate(zz),'ro',org_rate(yy),dst_rate(yy),'bo');
% hold on; plot([0,50],[0,50],'k-'); axis([0,30,0,30]);
% xlabel('ORG Rain Rate, mm/hr'); ylabel('Distdrometer Rain Rate, mm/hr');

% rain v U?
% yy = Urel<=10;
% zz = Urel>10;
% figure; plot(org_rate(zz),dst_rate(zz),'ro',org_rate(yy),dst_rate(yy),'bo');
% hold on; plot([0,50],[0,50],'k-'); axis([0,30,0,30]);
% xlabel('ORG Rain Rate, mm/hr'); ylabel('Distdrometer Rain Rate, mm/hr');
