% This script plots an individual track on an m_map map background, with
% colors indicating intensity and dates marked every 24 hours. 
% Copyright 2009 WindRiskTech L.L.C.
%--------------------------------------------------------------------------
params
if ~exist('templot.mat','file') || ~exist('nt','var')
    setplot;
end
load templot
clf('reset')
colormap('default')
colormap(trackcolormap)
vcolor=colormap;
if strcmp(cbath,'n') && strcmp(ctopo,'y')
   colormap(topocolor)
else
   colormap(bathcolor)
end   
if cres == 0 && strcmp(ctopo,'y') && strcmp(tfill,'y')
   uc=colormap;
   topocolor=[landcolor;uc];
   colormap(topocolor); 
end   

if strcmp(mapmode,'auto')
   delcrit=15;
   if (max(max(x))-min(min(x)))<delcrit
      dellong=dellong+delcrit-max(max(x))+min(min(x));
   end   
   xmin=min(min(x))-dellong;
   xmax=max(max(x))+dellong;
   ymin=min(min(y))-dellat;
   ymax=max(max(y))+dellat;
end   
m_proj(projection,'long',[xmin xmax], ...
   'lat',[ymin ymax]);
if strcmp(cbath,'y')
   if strcmp(bfill,'y')
      if tres == 2
         m_tbase('contourf',blevels);
       else
         m_elev('contourf',blevels);
      end   
   else
      if tres == 2
         m_tbase('contour',blevels);
       else
         m_elev('contour',blevels);
      end   
   end  
end   
hold on
if cres == 1
      m_coast('patch',ld,'edgecolor',shorecolor);
elseif cres == 2  
      m_gshhs_l('patch',ld,'edgecolor',shorecolor);
elseif cres == 3  
      m_gshhs_i('patch',ld,'edgecolor',shorecolor);
elseif cres == 4   
      m_gshhs_h('patch',ld,'edgecolor',shorecolor);
end   
if strcmp(pstates,'y')
   m_states(xmin,xmax,ymin,ymax,'color',stcolor)
end
if strcmp(ctopo,'y')
   if strcmp(tfill,'y')
      if tres == 2
         m_tbase('contourf',tlevels);
      else   
         m_elev('contourf',tlevels);
      end   
   else
      if tres == 2
         m_tbase('contour',tlevels);
      else   
         m_elev('contour',tlevels);
      end   
   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);
      vta=(1852/3600)*v(i);
      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(0.4267.*(qcolor/64)*(3600.*vta./1852-30));
         vtr=min(vtr,qcolor);
         vtr=max(vtr,1);
         vtcolor=vcolor(vtr,:);
      end   
      m_plot(xt,yt,'color',vtcolor,'linewidth',thick)
      lc=vtcolor;
   end   
else
   m_plot(x,y,lc,'linewidth',thick)
end
if strcmp(dplot,'y')  
    offset=zeros(1,bd);
if bd > 1   
    ul=zeros(1,bd); 
    vl=zeros(1,bd);
   for kl=2:bd-1
      ul(kl)=0.5*(xd(kl+1)-xd(kl-1));
      vl(kl)=0.5*(yd(kl+1)-yd(kl-1));
   end
   ul(1)=4*(x(2)-x(1));
   vl(1)=4*(y(2)-y(1));
   ul(bd)=4*(x(b)-x(b-1));
   vl(bd)=4*(y(b)-y(b-1));
else
   ul(1)=x(2)-x(1);
   vl(1)=y(2)-y(1);
end   
for kl=1:bd
   offset(kl)=-0.2;
   if day(kl)>9
      offset(kl)=-0.5;
   end
end   
vabs=0.001+sqrt(ul.^2+vl.^2);
mfactorx=(xmax-xmin)/30;
mfactory=(ymax-ymin)/30;
dx=mfactorx.*textdfactor.*((vl./vabs)+offset);
dy=mfactory.*textdfactor.*(-ul./vabs);
xdb=xd+dx;
ydb=yd+dy;
g=m_text(xdb,ydb,date); %#ok<DATE>
set(g,'fontname','arial','fontweight','bold','fontsize',textfontsize,'color',textcolor)
end
ua=x(a(1,2))-x(a(1,2)-1);
va=y(a(1,2))-y(a(1,2)-1);
uabs=sqrt(ua^2+va^2)+0.001;
scalef=sqrt(mfactorx^2+mfactory^2);
ua=2*ua*scalef/uabs;
va=2*va*scalef/uabs;
xq=x(a(1,2));
yq=y(a(1,2));
g1=m_quiver(xq,yq,ua,va,0);
set(g1,'maxheadsize',4.5);
set(g1,'showarrowhead','off')
set(g1,'showarrowhead','on')
set(g1,'linewidth',thick,'color',lc);
if strcmp(cplot,'y')
   m_plot(xd,yd,'.','color',ccol,'MarkerSize',csize)   
end   
m_gridm('fontname',axisfont,'fontsize',axisfontsize,'fontweight',axisfontweight,'linestyle',gridline,'bac',oceancolor)
title(tit3,'fontsize',11,'fontweight','bold','interpreter','none')
if colorcoded > 0 && strcmp(cbar,'y')
   if colorcoded == 1
      ucolor=[vcolor(1,:);vcolor(round(0.2*qcolor),:);vcolor(round(0.47*qcolor),:); ...
          vcolor(round(0.6*qcolor),:);vcolor(round(0.77*qcolor),:);vcolor(round(0.95*qcolor),:)];
      aa={'TS';'1';'2';'3';'4';'5';' '};
      bb=1/6;
      tstring='Category';
   else
      ucolor=vcolor;
      aa=40:10:180;
      bb=1/14;
      tstring=wunitslabel;
   end   
   colormap(ucolor);
   rts=colorbar('horiz');
   posit=get(gca,'position');
   rr=get(rts,'position');
   xl=get(gca,'xlim');
   yl=get(gca,'ylim');
   yfrac=(yl(2)-yl(1))./(xl(2)-xl(1));
   rr2=rr;
   rr2(3)=0.75.*rr(3);
   rr2(3)=max(rr2(3),0.4);
   rr2(1)=posit(1)+0.5.*posit(3)-0.5*rr2(3);
   rr2(4)=0.015;
   %
   set(rts,'position',rr2)
   set(rts,'ticklength',0)
   set(rts,'xaxislocation','bottom')
   set(rts,'fontname',axisfont,'fontweight','bold')
   set(rts,'xtick',0:bb:1,'xticklabel',aa)
   set(gca,'position',posit)
   set(get(rts,'xlabel'),'String',tstring,'fontweight','bold')
end
%
% Add button to access documentation of script
%
if strcmp(Docdisp,'y')
    try
        delete(hButton)
    catch
    end    
    tempname='trplot';
    mname = strcat('graphics_docs/html/',tempname,'_doc.html');
    comstr=['open ',mname,';delete(hButton)'];
    labelStr = '<html><center><a href="">Doc';
    hButton = uicontrol('string',labelStr,'pos',[10,10,50,20],'callback',comstr); 
end    