clear
addpath([matlabroot '/toolbox/local/m_map'])

% try again.
% 2011/4/14 add etl raw wind back in, for
% use with sonic flux
set_up
anchor_over=datenum(2011,4,6,19,30,0);  % 96.8125
left_area = datenum(2011,4,7,5,0,0);
revisit= datenum(2011,4,10);
figure(1); clf
% argos
load Argos.mat

% start at anchor over,
% second pass, start after we left the area,
% and also strip off short re-visit on 4/10
ix = find(sys1.mday > left_area & sys1.mday < revisit);
sys1=structtrunc(sys1,ix);
subplot(2,1,1);
plot(sys1.mday,sys1.atmp,sys1.mday,sys1.atmp,'b.');
title ('selecting stratus 11 comparison times');
ylabel('Argos throughput (atmp)');
hold on
ix = find(sys2.mday > left_area & sys2.mday < revisit);
sys2=structtrunc(sys2,ix);
plot(sys2.mday,sys2.atmp,'g',sys2.mday,sys2.atmp,'g.')
datetick('x','keeplimits')
grid on
stmd=sys1.mday(1); enmd=sys1.mday(end);

% gps
s11_lat=-19 -(41.4783/60); s11_lon=-85 -(34.0093/60);
load gps.mat
ix = find (gps.mday >= stmd & gps.mday < enmd);
gps=structtrunc(gps,ix);
dist_km=ones(length(gps.mday),1);
for iil=1:length(gps.mday),
    jla=[s11_lat gps.lat(iil)];
    jlo=[s11_lon gps.lon(iil)];
    dist_km(iil) = m_lldist(jlo,jla);
end
subplot(2,1,2);
plot(gps.mday,dist_km,gps.mday,dist_km);
grid on
ax=axis;
datetick('x','keeplimits');

ylabel('Distance (km)')
xlabel([datestr(ax(1),'yyyy/mm/dd') ' - ' datestr(ax(2),'yyyy/mm/dd')]);


% now find the first point closer than 5 miles;
% when we return, we pick up 6 hours of old data

ix = find(dist_km < 7,1,'first');
gps=structtrunc(gps,ix:length(gps.mday));
% want to start at even hour
dv=datevec(gps.mday(1:60));
ix=find(dv(:,5)==0,1,'first');
gps=structtrunc(gps,ix:length(gps.mday));
dist_km=ones(length(gps.mday),1);
for iil=1:length(gps.mday),
    jla=[s11_lat gps.lat(iil)];
    jlo=[s11_lon gps.lon(iil)];
    dist_km(iil) = m_lldist(jlo,jla);
end
hold on; plot(gps.mday,dist_km,gps.mday,dist_km,'r.');
stmd=gps.mday(1);
dt=diff(gps.mday) *24*60;
fprintf('gps %s at %.2f min\n', prmmd(gps),median(dt));

% now re-truncate sys1, sys2
ix = find(sys1.mday > stmd );
sys1=structtrunc(sys1,ix);
subplot(2,1,1)
plot(sys1.mday,sys1.atmp,sys1.mday,sys1.atmp,'r.');
hold on
ix = find(sys2.mday > stmd );
sys2=structtrunc(sys2,ix);
plot(sys2.mday,sys2.atmp,'g',sys2.mday,sys2.atmp,'r.')
datetick('x','keeplimits')
grid on
ax=axis; 
xlabel([datestr(ax(1),'yyyy/mm/dd') ' - ' datestr(ax(2),'yyyy/mm/dd')]);
halfhour=1/48;
dt=diff(sys1.mday) *24*60;
fprintf('sys1 %s at %.2f mins\n', prmmd(sys1), median(dt));
fprintf('sys2 %s\n', prmmd(sys2));
stmd=sys1.mday(1)-halfhour; enmd=sys1.mday(end)+halfhour;

% vos
load vos.mat
ix = find(vos.mday > stmd & vos.mday <enmd );
vos=structtrunc(vos,ix);
dt=diff(vos.mday) *24*60;
fprintf('vos %s at %.2f mins\n', prmmd(vos),median(dt));
subplot(2,1,1); hold on
plot(vos.mday,vos.atmp,'k');

% etl_flux
load etl_flux.mat
if ~issorted(flux.mday)
    [jj, ix,jj]=unique(flux.mday);
    flux=structsort(flux,ix);
end
etl_flux=flux;
ix=find(etl_flux.mday >= stmd & etl_flux.mday <= enmd);
etl_flux=structtrunc(etl_flux,ix);
etl_flux.yday=etl_flux.mday-day1;
dt=diff(etl_flux.mday) *24*60;
fprintf('etl_flux %s at %.2f mins\n', prmmd(etl_flux),median(dt));
subplot(2,1,1); hold on
plot(etl_flux.mday,etl_flux.atmp,'m');

% etl
load etl.mat
 if ~issorted(etl.mday)
     [jj, ix,jj]=unique(etl.mday);
     etl=structsort(etl,ix);
 end
ix=find(etl.mday > stmd & etl.mday < enmd);
etl=structtrunc(etl,ix);
etl.yday=etl.mday-day1;
dt=diff(etl.mday) *24*60;
fprintf('etl %s at %.2f mins\n', prmmd(etl),median(dt));
subplot(2,1,1); hold on
plot(etl.mday,etl.atmp,'c-');

% stmd:  07-Apr-2011 22:00:00
% enmd:  09-Apr-2011 11:00:00
% gps       2011/04/07 22:00 to 2011/04/09 10:29 at 1.00 min
% sys1      2011/04/07 22:30 to 2011/04/09 10:30 at 60.00 mins
% sys2      2011/04/07 22:30 to 2011/04/09 10:30
% vos       2011/04/07 22:01 to 2011/04/09 10:59 at 2.00 mins
% etl_flux  2011/04/07 22:00 to 2011/04/09 10:59 at 5.00 mins
% etl       2011/04/07 22:00 to 2011/04/09 10:59 at 1.00 mins

dist_s11=struct('mday', gps.mday,'dist_km',dist_km');
save s11comp.mat gps sys1 sys2 vos etl etl_flux dist_s11
