% This script plots an individual track in Google Earth Pro, with colors
% indicating intensity and dates marked every 24 hours. The free KML
% toolbox must be installed in MATLAB for this script to work. 
% Copyright 2009 WindRiskTech L.L.C.
%------------------------------------------------------------------
twidthge=4;         %  Width of track
arrowangle=45;      %  Angle of arrowheads
arrowlength=1;      %  Length of arrowheads
dotsize=1;          %  Size of dots at date positions
dotcolor=[0 1 1];   %  Color of date dots
textoffset=1;       %  Latitude and Longitude offset of text (degrees)
getextsize=2;       %  Scale factor of text size
%------------------------------------------------------------------       
try
    ktrplot=kml('getempfile');
catch err
    errordlg('The free KML toolbox must be installed into MATLAB to use this script')
    return
end    
params
if ~exist('templot.mat','file') || ~exist('nt','var')
    setplot;
end
load templot
%close
vcolor=jet;
pifac=pi/180;
n=0;
m=0;
iswitch=0;
for i=1:iqo
    if hour(i) <= 0.5
        m=m+1;
    end
end
xd=zeros(1,m);
yd=zeros(1,m);
day=zeros(1,m);
date=cellstr(num2str(daym(1:m)));
month=date;
m=0;
for i=1:iq
    if hour(i) <= 0.5
       m=m+1;
       if x(i) <= 180
           xd(m)=x(i);
       else
           xd(m)=x(i)-360;
       end    
       yd(m)=y(i);
       day(m)=daym(i);
       monthstring=num2str(monthm(i));
       month(m)=cellstr(monthstring);
       daystring=num2str(day(m));
       date(m)=cellstr(daystring);
    end      
end        
%        
if strcmp(cspline,'y')
   a=size(x);
   b=a(1,2);
   bx=1:b;
   tint=floor(6/trackres);
   tintin=1/tint;
   bxm=1:tintin:b;
   x=spline(bx,x,bxm);
   y=spline(bx,y,bxm);
   v=spline(bx,v,bxm);
   pres=spline(bx,pres,bxm);
   vpot=spline(bx,vpot,bxm);
   shear=spline(bx,shear,bxm);
   monthm=spline(bx,monthm,bxm);
   daym=spline(bx,daym,bxm);
   hour=spline(bx,hour,bxm);
   vtrans=spline(bx,vtrans,bxm);
end   
a=size(x);
b=a(1,2);
if colorcoded > 0
    xt=zeros(1,2);
    yt=zeros(1,2);
   for i=2:b
      xt(1)=x(i-1);
      xt(2)=x(i);
      yt(1)=y(i-1);
      yt(2)=y(i);
      vta=(1852/3600)*v(i-1);
      if colorcoded == 1
        if vta < 33
            vtcolor=vcolor(1,:);
         elseif vta >= 33 && vta < 45
            vtcolor=vcolor(round(0.2*qcolor),:);
         elseif vta >= 45 && vta < 54.4
            vtcolor=vcolor(round(0.47*qcolor),:);
         elseif vta >= 54.4 && vta < 62.4
            vtcolor=vcolor(round(0.6*qcolor),:);
         elseif vta >= 62.4 && vta < 69.5
            vtcolor=vcolor(round(.77*qcolor),:);
         else
            vtcolor=vcolor(round(0.95*qcolor),:);
         end
      else   
         vtr=round(vta)-15;
         vtr=round(vtr*(qcolor/64));
         vtr=min(vtr,qcolor);
         vtr=max(vtr,1);
         vtcolor=vcolor(vtr,:);
      end   
      s=(strcat('FF',rgbconv(fliplr(vtcolor))));
      ktrplot.plot(xt,yt,'linecolor',s,'linewidth',twidthge);
   end                  
else
   ss=rgb(tcolor);
   s=(strcat('FF',rgbconv(fliplr(ss)))); 
   ktrplot.plot(x,y,'linecolor',s,'linewidth',twidthge);
   xt(1)=x(end-1);
   xt(2)=x(end);
   yt(1)=y(end-1);
   yt(2)=y(end);
end
if strcmp(cplot,'y')
   sd=(strcat('FF',rgbconv(fliplr(ccol))));
   ktrplot.scatter(xd,yd,'iconcolor',sd,'iconscale',dotsize);
end
if strcmp(dplot,'y')  
    qd=max(size(xd));
    for kk=1:qd
        ud=strcat(month(kk),'/',date(kk));
        ktrplot.text(xd(kk)+textoffset,yd(kk)+textoffset,0,ud,'labelscale',getextsize);
    end
end
xquiv=xt(2);
yquiv=yt(2);
uquiv=(xt(2)-xt(1));
vquiv=(yt(2)-yt(1));    
di=1./sqrt(uquiv^2+vquiv^2)+1e-8;
ct=uquiv.*di;
st=vquiv.*di;
angle=pifac.*(180-arrowangle);
san=sin(angle);
can=cos(angle);
dx1=arrowlength*(ct*can+st*san);
dx2=arrowlength*(ct*can-st*san);
dy1=arrowlength*(st*can-ct*san);
dy2=arrowlength*(st*can+ct*san);
xa=[xt(2) xt(2)+dx1];
ya=[yt(2) yt(2)+dy1];
xb=[xt(2) xt(2)+dx2];
yb=[yt(2) yt(2)+dy2];
ktrplot.plot(xa,ya,'linecolor',s,'linewidth',twidthge); 
ktrplot.plot(xb,yb,'linecolor',s,'linewidth',twidthge);  
ktrplot.run