function [T] = csvconvoctf(datea)
%
%  This script concatenates all the track events into a single .csv
%  file, with a single header row. A second file is produced containing the
%  annual frequency (or frequencies). 
%  Copyright 2012 WindRiskTech L.L.C.
%  Last modified August, 2020. 
%-------------------------------------------------------------------------
%
clear yearstore
comm='rm -f poly.in';
%[~, ~]=system(comm);
outf=datea;
file=[datea '.mat'];
load(file)
%
outfile=['csv_files/' outf '.csv'];
outfile2=['csv_files/' outf '2.csv'];
comm=['rm -f ' outfile];
[~, ~]=system(comm);
%
[n,m1]=size(vstore);
%
[~,jmax]=min(vstore,[],2);
jmax=jmax-1;
dum=zeros(n,400);
yeartemp=zeros(1,jmax(1));
%
[~,jmax]=min(vstore,[],2);
jmax=jmax-1;
%
%  Calculate translation velocity and wind speed increments
%
if exist('u850store','var')
    [ut,vt,uinc,vinc ] = utransfull(latstore,longstore,vstore,u850store,v850store);
    if exist('vsestore','var')
        [~,~,uincse,vincse ] = utransfull(latstore,longstore,vsestore,u850store,v850store);
    end    
else
    [ut,vt,uinc,vinc ] = utransfull(latstore,longstore);
    if exist('vsestore','var')
        uincse=uinc;
        vincse=vinc;
    end
end
% 
vmax=vstore+sqrt(uinc.^2+vinc.^2);
%
% Create mask for secondary eyewall events and find maximum ground relative
% wind for them
%
if exist('vsestore','var')
    semask=min(vsestore,1);
    vsemax=semask.*(vsestore+sqrt(uincse.^2+vincse.^2));
end 
%
if char(shape) == char('poly')
    a=load('poly.in');
    xa=a(:,1);
    ya=a(:,2);
    xb=a(:,3);
    yb=a(:,4);  
    [nseg,~]=size(a);
    for i=1:nseg
        if xa(i) < 0
            xa(i)=xa(i)+360;
        end
        if xb(i) < 0
             xb(i)=xb(i)+360;
        end
    end   
    [nint,xint,yint,jint,kint] = boxm(longstore,latstore,xa,ya,xb,yb);
    nstorm=1:max(size(nint));
end
if exist('yearstore','var')   
    M=zeros(n*m1,22);
    C={'Storm','Year','Month','Day','UTZ','Lat','Long','Vc (kts)','Vm (kts)', ...
        'Rmw (km)','Vcse (kts)', 'Vmse (kts)','Rmwse (km)','P (hPa)','Shear (m/s)', ...
        'Vp (kts)','u850 (kts)','v850 (kts)','T600 (K)','RH600 (%)','U_trans(kts)','V_trans(kts)'};
    dum(1,1:jmax(1))=1;
    yeartemp(1,1:jmax(1))=yearstore(1);
    M(1:jmax(1),:)=transpose([dum(1,1:jmax(1));yeartemp; monthstore(1,1:jmax(1)); daystore(1,1:jmax(1)); ...
            hourstore(1,1:jmax(1));latstore(1,1:jmax(1));longstore(1,1:jmax(1)); ...
            vstore(1,1:jmax(1));vmax(1,1:jmax(1));rmstore(1,1:jmax(1)); ...
            vsestore(1,1:jmax(1));vsemax(1,1:jmax(1));rmsestore(1,1:jmax(1)); ...
            pstore(1,1:jmax(1));shearstore(1,1:jmax(1)); ...
            vpstore(1,1:jmax(1));u850store(1,1:jmax(1));v850store(1,1:jmax(1)); ...
            T600store(1,1:jmax(1));rhstore(1,1:jmax(1));ut(1,1:jmax(1));vt(1,1:jmax(1))]);
    nmold=jmax(1);   
    for i=2:n
        dum(1,1:jmax(i))=i;
        clear yeartemp
        yeartemp=zeros(1,jmax(i));
        yeartemp(1,1:jmax(i))=yearstore(i);
        nmnew=nmold+jmax(i);
        M(nmold+1:nmnew,:)=transpose([dum(1,1:jmax(i));yeartemp; ...
            monthstore(i,1:jmax(i)); daystore(i,1:jmax(i)); ...
            hourstore(i,1:jmax(i));latstore(i,1:jmax(i));longstore(i,1:jmax(i)); ...
            vstore(i,1:jmax(i));vmax(i,1:jmax(i));rmstore(i,1:jmax(i)); ...
            vsestore(i,1:jmax(i));vsemax(i,1:jmax(i));rmsestore(i,1:jmax(i)); ...
            pstore(i,1:jmax(i));shearstore(i,1:jmax(i)); ...
            vpstore(i,1:jmax(i));u850store(i,1:jmax(i));v850store(i,1:jmax(i)); ...
            T600store(i,1:jmax(i));rhstore(i,1:jmax(i));ut(i,1:jmax(i));vt(i,1:jmax(i))]);
        nmold=nmnew;
    end 
else    
    M=zeros(n*m1,14);
    %
    C={'Storm','Month','Day','UTZ','Lat','Long','Vc (kts)','Vm (kts)', ...
        'Rmw (km)','Vcse (kts)', 'Vmse (kts)','Rmwse (km)','P (hPa)','Shear (m/s)', ...
        'Vp (kts)','u850 (kts)','v850 (kts)','T600 (K)','RH600 (%)','U_trans(kts)','V_trans(kts)'};
    dum(1,1:jmax(1))=1;
    M(1:jmax(1),:)=transpose([dum(1,1:jmax(1));monthstore(1,1:jmax(1)); daystore(1,1:jmax(1)); ...
            hourstore(1,1:jmax(1));latstore(1,1:jmax(1));longstore(1,1:jmax(1)); ...
            vstore(1,1:jmax(1));vmax(1,1:jmax(1));rmstore(1,1:jmax(1)); ...
            vsestore(1,1:jmax(1));vsemax(1,1:jmax(1));rmsestore(1,1:jmax(1)); ...
            pstore(1,1:jmax(1));shearstore(1,1:jmax(1)); ...
            vpstore(1,1:jmax(1));u850store(1,1:jmax(1));v850store(1,1:jmax(1)); ...
            T600store(1,1:jmax(1));rhstore(1,1:jmax(1));ut(1,1:jmax(1));vt(1,1:jmax(1))]);
    nmold=jmax(1);       
    for i=2:n
        %disp(['Processing storm ' num2str(i)])
        dum(1,1:jmax(i))=i;
        nmnew=nmold+jmax(i);
        M(nmold+1:nmnew,:)=transpose([dum(1,1:jmax(i));monthstore(i,1:jmax(i)); daystore(i,1:jmax(i)); ...
            hourstore(i,1:jmax(i));latstore(i,1:jmax(i));longstore(i,1:jmax(i)); ...
            vstore(i,1:jmax(i));vmax(i,1:jmax(i));rmstore(i,1:jmax(i)); ...
            vsestore(i,1:jmax(i));vsemax(i,1:jmax(i));rmsestore(i,1:jmax(i)); ...
            pstore(i,1:jmax(i));shearstore(i,1:jmax(i)); ...
            vpstore(i,1:jmax(i));u850store(i,1:jmax(i));v850store(i,1:jmax(i));ut(i,1:jmax(i));vt(i,1:jmax(i))]);
        nmold=nmnew;
    end  
end
M(nmnew+1:end,:)=[];
%
[nsize,~]=size(M);
nrows=floor(nsize*1e-6)+1;
warning('off')
for i=1:nrows
   % outfile=['csv_files/' outf '_' num2str(i) '.xlsx'];
    if exist('yearstore','var')
        yearmax=max(yearstore);
        yearmin=min(yearstore);
        %nyear=max(size(yearstore))./(yearmax-yearmin+1);
        nyear=1;
        year=yearmin:nyear:yearmax;
        fid=fopen(outfile,'w');
        fprintf(fid,'%s %s\n','Year,','Frequency');
        fclose(fid);
        dlmwrite(outfile,[single(year'),single(freqyear')],'-append');
    else
        fid=fopen(outfile,'w');
        fprintf(fid,'%s\n','Frequency');
        fclose(fid);
        dlmwrite(outfile,[single(freq)],'-append');
    end    
    fid=fopen(outfile2,'w');
    fprintf(fid,'%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s\n', ...
        'Storm number,','Year,','Month,','Day,','UTZ,','Lat,','Long,','Vc (kts),','Vm (kts),', ... 
        'Rmw (km),','Vcse (kts),', 'Vmse (kts),','Rmwse (km),','P (hPa),','Shear (m/s),', ... 
        'Vp (kts),','u850 (kts),','v850 (kts),','T600 (K),','RH600 (%),','U_trans(kts),','V_trans(kts)');
    fclose(fid);
    nbeg=1+1e6*(i-1);
    nend=1e6*(i);
    nend=min(nend,nsize);
    dlmwrite(outfile2,M(nbeg:nend,:),'-append')
end
warning('on')
end

