%function plot_donglai
% plot_adcp.m
% =====================================================
% plot velocity vectors using adcp data
% =====================================================
% current time
clear all
close all
tnow = now;
datestr(now);

% define uptodate eddy center waypoint
ec = 2317;

% set time duration to plot shiptrack (in GMT +4 hr)
tN = now + 4/24.;
t0 = tN - 1;

% load adcp data + navigation from .mat file
%load imotion_init

%%% load *.00 formated navigation data %%%

% define navigation data directory
navdatadir = '/Users/donglai/RO/EDDIES/scripts/athena/';

% list files in navigation data dir
navfilestmp = dir([navdatadir '*.00']);

% define navigation data format
navdataformat = '%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%*[^\n]';

% load nav data
nfiles = length(navfilestmp);
yyyy=[];mm=[];dd=[];hh=[];mn=[];ss=[];depth12=[];depth35=[];air_temp=[];llat=[];llon=[];sscnd=[];sstemp=[];gps_cog=[];gps_sog=[];gyro=[];imet_bpr=[];imet_swr=[];prc=[];hrh=[];sbe45c=[];sbe45s=[];sbe45t=[];sbe45v=[];sal=[];fluor=[];spd=[];wnd_dir=[];wnd_spd=[];
for ii = 1:nfiles,
    % convert from array of structures of cell arry
    navfiles{ii} = navfilestmp(ii).name;
    navdatafile = navfiles{ii};

    % if navigation data exists, load it
    %if isempty(strmatch(navdatafile,navfiles)) ~= 1
    if nfiles > 0
        [yyyy0,mm0,dd0,hh0,mn0,ss0,depth12_0,depth35_0,air_temp0,llat0,llon0,sscnd0,sstemp0,gps_cog0,gps_sog0,gyro0,imet_bpr0,imet_swr0,prc0,hrh0,sbe45c0,sbe45s0,sbe45t0,sbe45v0,sal0,fluor0,spd0,wnd_dir0,wnd_spd0] = textread([navdatadir navdatafile],navdataformat,'headerlines',1,'delimiter','/:, ');
        %[yyyy,mm,dd,hh,mn,ss,depth12,depth35,air_temp,llat,llon,sscnd,sstemp,gps_cog,gps_sog,gyro,imet_bpr,imet_swr,prc,hrh,sbe45c,sbe45s,sbe45t,sbe45v,sal,fluor,spd,wnd_dir,wnd_spd] = textread([navdatadir navdatafile],navdataformat,'headerlines',1,'delimiter','/:, ');
        yyyy = [yyyy;yyyy0];
        mm = [mm;mm0];
        dd = [dd;dd0];
        hh = [hh;hh0];
        mn = [mn;mn0];
        ss = [ss;ss0];
        depth12 = [depth12;depth12_0];
        depth35 = [depth35;depth35_0];
        air_temp = [air_temp;air_temp0];
        llat = [llat;llat0];
        llon = [llon;llon0];
        sscnd = [sscnd;sscnd0];
        sstemp = [sstemp;sstemp0];
        gps_cog = [gps_cog;gps_cog0];
        gps_sog = [gps_sog;gps_sog0];
        gyro = [gyro;gyro0];
        imet_bpr = [imet_bpr;imet_bpr0];
        imet_swr = [imet_swr;imet_swr0];
        prc = [prc;prc0];
        hrh = [hrh;hrh0];
        sbe45c = [sbe45c;sbe45c0];
        sbe45s = [sbe45s;sbe45s0];
        sbe45t = [sbe45t;sbe45t0];
        sbe45v = [sbe45v;sbe45v0];
        sal = [sal;sal0];
        fluor = [fluor;fluor0];
        spd = [spd;spd0];
        wnd_dir = [wnd_dir;wnd_dir0];
        wnd_spd = [wnd_spd;wnd_spd0];
    end %if
end %for

% define ship time indices (previous 1 day)
shipt = datenum(yyyy,mm,dd,hh,mn,ss);
stind = find(shipt >= t0 & shipt <= tN);

% extract shiptrack & time of interest
lllon = llon(stind);
lllat = llat(stind);
shiptt = shipt(stind);

% define glider time indices (previous 2 days)
%glidert = [];
%gtind = find(glidert <= tN & glidert >= tN - 2);


% define navigation file name for today and yesterday
%navdatafile = ['OC05' datestr(tnow,5) datestr(tnow,7) '.00'];
%navdatafile0 = ['OC05' datestr(tnow-1,5) datestr(tnow-1,7) '.00'];
%
% if navigation data exists for yesterday, load it
%if isempty(strmatch(navdatafile0,navfiles)) ~= 1
%    [yyyy0,mm0,dd0,hh0,mn0,ss0,depth12_0,depth35_0,air_temp0,llat0,llon0,sscnd0,sstemp0,gps_cog0,gps_sog0,gyro0,imet_bpr0,imet_swr0,prc0,hrh0,sbe45c0,sbe45s0,sbe45t0,sbe45v0,sal0,fluor0,spd0,wnd_dir0,wnd_spd0] = textread([navdatadir navdatafile0],navdataformat,'headerlines',1,'delimiter','/,: ');
%end %if
%if exist('yyyy') ==1 & exist('yyyy0') == 1
%    yyyy = [yyyy0;yyyy];
%    mm = [mm0,mm];
%    dd = [dd0,dd];
%    hh = [hh0,hh];
%end %if

% load waypoint list
c=load('coord_mtx_05new.txt');
cw=fix(c(:,1)); % extract waypoint number only



%%% load GLIDER track & next waypoint %%%
% yyyy mm dd hh mn ss glat glon
[gyyyy, gmm, gdd, ghh, gmn, gss, glat, glon]=textread('glidertrack.txt','%n%n%n%n%n%n%n%n','commentstyle','shell','delimiter','/: ');
% wptnum glat glon
[gwpts, gllat, gllon] =textread('gliderwpts.txt','%n%n%n','commentstyle','shell');

% determine glider track time index
gt = datenum(gyyyy,gmm,gdd,ghh,gmn,gss);
gtind = find(gt >= t0 & gt <= tN);

% convert from glider lat/lon format to decimal degrees
gglat = fix(glat/100) + (glat - fix(glat/100)*100)/60;
gglon = fix(glon/100) + (glon - fix(glon/100)*100)/60;
ggllat = fix(gllat/100) + (gllat - fix(gllat/100)*100)/60;
ggllon = fix(gllon/100) + (gllon - fix(gllon/100)*100)/60;

alat = gglat(end);
alon = gglon(end);
blat = ggllat(end);
blon = ggllon(end);
distowpt = dist(alat,alon,blat,blon)
distravelled = dist(gglat(end-1),gglon(end-1),alat,alon)

% ETA at waypoint
etawpt = distowpt/1/24. + gt(gtind(end)) + 25./60/24*fix(distowpt/6.);

% load predict ship waypoints (psw) for next day
psw=textread('shippredict.txt','','commentstyle','shell');

% obtain lat/lon for predict ship waypoints
if isempty(psw) ~= 1
    for ii = 1:length(psw),
        cwind(ii) = find(cw == fix(psw(ii)));
    end %for

    pslat = c(cwind,2);
    pslon = c(cwind,3);
end %if

ff=figure('visible','on');

% plot navigation (ship track)
set(gcf,'color','w');
h_strack=plot(lllon,lllat,'r--','linewidth',1.5);  %color
%h_strack=plot(lllon,lllat,'k-.','linewidth',1.5);  %b/w
hold on

% plot beginning and end waypoints for ship in the last 24 hours
%hs1=scatter(lllon(1),lllat(1),50,'r','d','filled');
hs2=scatter(lllon(end),lllat(end),150,'r','d','filled');

% plot predicted ship waypoints
if isempty(psw) ~= 1
    h_ps=plot([lllon(end);pslon],[lllat(end);pslat],'m*--','linewidth',1.5);
%h_ps=plot([lllon(end);pslon],[lllat(end);pslat],'r*--','linewidth',1.5);
end %if

% plot glider track for past 24 hrs and direct line to next waypoint
h_gtrack=plot(gglon,gglat,'k.-','linewidth',2,'markersize',15);
hg1 = scatter(gglon(end),gglat(end),150,'k','^','filled');
h_gwpt=plot([gglon(end);ggllon(end)] , [gglat(end);ggllat(end)], 'b-','linewidth',2);
hg2 = scatter(ggllon(end),ggllat(end),80,'b','o','filled');

% plot other stuff
drifter1 = [-66 - 17.01/60, 30 + 52.88/60];
drifter2 = [-66 - 16.78/60, 30 + 49.38/60];
drifters = [drifter1; drifter2];
scatter(drifters(:,1),drifters(:,2),150,'k','x');

% extend axis         
axis([-67.4 -65 29.5 32.1]);         
pp=axis;
% axis([pp(1)-.1 pp(2)+.1 pp(3)-.1 pp(4)+.1])
grid on
hold off
% axis equal
starttime = datestr(shiptt(1),31);
endtime = datestr(shiptt(end),31);
shiptitle = ['shiptrack time (GMT): ',starttime,' -- ',endtime];
timetitle = ['Time (GMT): ', endtime];
gliderstatus = ['last glider surfacing (GMT): ', datestr(gt(gtind(end)),31), '  *** dist to next wpt: ', num2str(distowpt/1.852), ' nm *** dist travelled: ', num2str(distravelled/1.852), ' nm'];
gliderstatus1 = ['last glider surfacing (GMT): ', datestr(gt(gtind(end)),31)];
glidernextsurf = ['next glider surfacing (GMT): ', datestr(gt(gtind(end))+.25+15/60/24.,31) ' *** next wpt ETA: ' datestr(etawpt,31)];
gliderposition = [num2str(glat(end)) '  ' num2str(glon(end))];
%title([shiptitle '    latest glider pos:  ' gliderposition]);
title([timetitle ', ' gliderstatus1 '   glider lat/lon:  ' gliderposition]);
ca = axis;
text(ca(2)-2.5,ca(3)-.2,gliderstatus);
text(ca(2)-2.5,ca(3)-.31,glidernextsurf);
%text(alon+.1,alat,gliderposition);

% if plot station positions (=1)
pgrd=1;
% plot station positions
if(pgrd==1)
    %load coord_mtx_05new.txt;
    %c=coord_mtx_05new;
    pp=axis;
    % plot the only numbers inside axis
    ff=find(c(:,3)>pp(1) & c(:,3)<pp(2) & c(:,2)>pp(3) & c(:,2)<pp(4));
    for i=[1:length(ff)]         
        text(c(ff(i),3),c(ff(i),2),['   ',num2str(c(ff(i),1))],'fontsize',7);
    end 
    hold on
    plot(c(ff,3),c(ff,2),'sk','markersize',7)  
end 
hold off
%xlabel('Longitude')
%ylabel('Latitude')
%gtext([datestr(now,0)])


gliderfigdir = '/Users/donglai/RO/EDDIES/scripts/';
glidershipfig = ['gs' datestr(tN,30) '.jpg'];
eval(['print -djpeg100 -r180 ' gliderfigdir glidershipfig]);

%[xx,]=ginput;