%
% This auxiliary script sets and stores certain parameters used in m_map
% plots of individual tracks.
% Copyright WindRiskTech, L.L.C. 2026
%--------------------------------------------------------------------------
%
params    %  Load parameters
%
if strcmp(swap,'y')
   dummy=landcolor;
   landcolor=oceancolor;
   oceancolor=dummy;
end   
lc=tcolor;
ld=landcolor;
colormap('default')
%
if exist('shearstore','var') == 0
    shape='circ'; %#ok<*NASGU>
    load('temp.mat') %#ok<*LOAD>
else
    load('temp.mat','vt')
    load('temp.mat','ut')
    warning ('off','all');
    load('temp.mat','yearstore')
    load('temp.mat','vstore')
end  
%
warning ('on','all');
if exist('nsortc','var') == 0
    fs3='sorted';
    load(fs3)
end   
%
if strcmp(bas,'GB')
    projection=gproject;
end   
load sorted storm
%
nt=get_storm_num;
%
clf
ntstring=num2str(nt);
[~,m8max]=size(vstore);
[~,jmax]=min(vstore,[],2);
jmax(vstore(:,end)~=0)=m8max;
jmax=jmax-1;
iq=jmax(nt);
iqo=iq;
%
clear vtrans shear sst vpot monthm daym hour pres x y
vtrans=wunitsfac*(sqrt(ut(nt,1:iq).^2+vt(nt,1:iq).^2));
v=zeros(1,iq);   
v(1:iq)=wunitsfac*vnet(nt,1:iq);
pres(1:iq)=pstore(nt,1:iq);
x(1:iq)=longstore(nt,1:iq);
y(1:iq)=latstore(nt,1:iq);
for i=1:iq
    if x(1) > 200 && x(i) < 50
        x(i)=x(i)+360;
    end
end  
vpot(1:iq)=wunitsfac*vpstore(nt,1:iq);
shear(1:iq)=wunitsfac*shearstore(nt,1:iq);
monthm(1:iq)=monthstore(nt,1:iq);
daym(1:iq)=daystore(nt,1:iq);
hour(1:iq)=hourstore(nt,1:iq);  
if exist('sststore','var')
    sst(1:iq)=sststore(nt,1:iq);
end    
%
for i=1:iq
   if monthm(i) == 12 && daym(i) > 31
      monthm(i)=1;
      daym(i)=daym(i)-31;
   end   
end
amod=mod(hour(1),2);
if amod ~= 0
    hour=hour-amod;
end    
vtrans=smooth(vtrans);
shear=smooth(shear);
vpot=smooth(vpot);
months={'January','February','March','April','May','June','July','August','September','October','November','December'};
transcolor=0.5.*(landcolor+oceancolor);
colormap('default')
colormap(trackcolormap)
vcolor=colormap;
[qcolor,~]=size(vcolor);
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 
daytime=daym+hour./24;     
daymod=daytime;
delday=max((daytime(2)-daytime(1)),(daytime(3)-daytime(2)));
iswitch=0;
daymax=floor(daytime(1));
for i=2:iq
   if daytime(i) < daymod(i-1)
      daymod(i)=daymod(i-1)+delday;
      if iswitch == 0
         iswitch=1;
         daymax=floor(daytime(i-1));
      end   
   end
end  
n=0;
m=0;
iswitch=0;
for i=1:iq
    if hour(i) <= 0.5
        m=m+1;
    end
end
if m == 0
    msgbox('This track too short to display','Error','error')
    return
end    
xd=zeros(1,m);
yd=zeros(1,m);
day=zeros(1,m);
month=zeros(1,m);
date=cellstr(num2str(daym(1:m)));
m=0;
for i=1:iq
    if hour(i) <= 1.5
       m=m+1;
       xd(m)=x(i);
       yd(m)=y(i);
       day(m)=daym(i);
       month(m)=monthm(i);
       daystring=num2str(day(m));
       date(m)=cellstr(daystring);
    end      
end
bd=max(size(xd));
ixlab=months(month(bd));
if month(bd) > month(1)
      ixlab=[char(months(month(1))) char(' - ') char(months(month(bd)))];
elseif month(bd) < month(1)
      ixlab='December - January';
end   
if exist('yearstore','var') == 1 
    ixlab=[char(ixlab), ' ', char(num2str(yearstore(nt)))];
end    
titstring=['track number ' ntstring ',   ' ixlab ];
titstring2=['track number ' ntstring  ];
if isempty(storm)
    datea(1)=upper(datea(1));
    tit1=[datea ' ' titstring2];
else
    tit1=[storm(1:2) storm(3:4) ' ' datea(1:4) ' ' datea(5:6) ' ' datea(7:8)...
            ' ' datea(9:10) ' GMT'];
    tit2=titstring2;
    tit1=[tit1 '  ' tit2];
end    
tit3={tit1;char(ixlab)};
save templot nt ntstring tit3 date month day xd yd vcolor qcolor months ...
    vpot shear vtrans v x y monthm daym hour jmax iq iqo daymod ...
    tit1 pres daymax ixlab lc ld bd
%
% Clean up
%
if exist('Clearvlbs','var') && strcmp(Clearvlbs,'y')
    clearvars('-except',protected_variables{:})
end 