function catseriesn(filenum,netw,csvw)
%
%  This script concatenates multiple matlab binary files time series 
%  output from WRT.
%  Last modified October, 2014
%  Copyright 2009 WindRiskTech L.L.C.
%  Last modified May, 2022
%
%clear
%-------------------------------------------------------------------------
direc='.\';           % Subdirectory where matlab binaries stored
zip_utility='zip';    % Local zip utility command name    
yint=1;               % Year interval in time series
%-------------------------------------------------------------------------
%
filenum=num2str(filenum);
mfile=strcat('mlist',filenum);
sfile=strcat('stats',filenum,'.txt');
fid=fopen(mfile,'r');
C=textscan(fid,'%s %s %s %s %d %d');
g=fclose(fid);
model=C{1};
flavor=C{2};
prefix=strcat(model,flavor);
suffix=C{3};
masterfile=C{4};
yearbeg=C{5};
yearend=C{6};

qy=1+(yearend-yearbeg)/yint;
file=struct('name',qy);
for year=yearbeg:yint:yearend,
    i=(year-yearbeg)/yint+1;
    file(i).name=strcat(prefix, num2str(year), suffix);
end    
%-------------------------------------------------------------------------
n=max(size(file));
freqyear=zeros(1,n);
%
filedum=char(file(1).name);
filename=char(strcat(char('matfiles/'),filedum));
load(filename)
[q,dd]=size(vstore);
%
%  Initialize arrays
%
vdum=zeros(2.*q.*n,dd);
hourdum=vdum;
monthdum=vdum;
daydum=vdum;
rmdum=vdum;
latdum=vdum;
longdum=vdum;
pdum=vdum;
sheardum=vdum;
vpdum=vdum;
rhdum=vdum;
vsedum=vdum;
rmsedum=vdum;
%
vdum(1:q,:)=vstore;
daydum(1:q,:)=daystore;
hourdum(1:q,:)=hourstore;
monthdum(1:q,:)=monthstore;
rmdum(1:q,:)=rmstore;
latdum(1:q,:)=latstore;
longdum(1:q,:)=longstore;
pdum(1:q,:)=pstore;
sheardum(1:q,:)=shearstore;
vpdum(1:q,:)=vpstore;
rhdum(1:q,:)=rhstore;
vsedum(1:q,:)=vsestore;
rmsedum(1:q,:)=rmsestore;
u850dum=vdum;
v850dum=vdum;
u850dum(1:q,:)=u850store;
v850dum(1:q,:)=v850store;
T600dum=vdum;
T600dum(1:q,:)=T600store;
%
clear yearstore
yearstore(1:q)=yearbeg;
fdum=freq;
freqyear(1)=freq;
nnold=q;
for i=2:n,
    filedum=char(file(i).name);
    filename=char(strcat(char('matfiles/'),filedum));
    load(filename)
    [nn,~]=size(vstore);
    year=yearbeg+yint.*(i-1);
    nnew=nnold+nn;
    yearstore(nnold+1:nnew)=year;
    vdum(nnold+1:nnew,:)=vstore(1:nn,:);
    daydum(nnold+1:nnew,:)=daystore(1:nn,:);
    hourdum(nnold+1:nnew,:)=hourstore(1:nn,:);
    monthdum(nnold+1:nnew,:)=monthstore(1:nn,:);
    rmdum(nnold+1:nnew,:)=rmstore(1:nn,:);
    latdum(nnold+1:nnew,:)=latstore(1:nn,:);
    longdum(nnold+1:nnew,:)=longstore(1:nn,:);
    pdum(nnold+1:nnew,:)=pstore(1:nn,:);
    sheardum(nnold+1:nnew,:)=shearstore(1:nn,:);
    vpdum(nnold+1:nnew,:)=vpstore(1:nn,:);
    rhdum(nnold+1:nnew,:)=rhstore(1:nn,:);
    vsedum(nnold+1:nnew,:)=vsestore(1:nn,:);
    rmsedum(nnold+1:nnew,:)=rmsestore(1:nn,:);
    u850dum(nnold+1:nnew,:)=u850store(1:nn,:);
    v850dum(nnold+1:nnew,:)=v850store(1:nn,:);
    T600dum(nnold+1:nnew,:)=T600store(1:nn,:);
    nnold=nnew;
    %
    fdum=fdum+freq;
    freqyear(i)=freq;
end
freq=fdum./n;
%
vdum(nnold+1:end,:)=[];
daydum(nnold+1:end,:)=[];
monthdum(nnold+1:end,:)=[];
hourdum(nnold+1:end,:)=[];
rmdum(nnold+1:end,:)=[];
latdum(nnold+1:end,:)=[];
longdum(nnold+1:end,:)=[];
pdum(nnold+1:end,:)=[];
sheardum(nnold+1:end,:)=[];
vpdum(nnold+1:end,:)=[];
rhdum(nnold+1:end,:)=[];
vsedum(nnold+1:end,:)=[];
rmsedum(nnold+1:end,:)=[];
%
vstore=vdum;
daystore=daydum;
hourstore=hourdum;
monthstore=monthdum;
rmstore=rmdum;
latstore=latdum;
longstore=longdum;
pstore=pdum;
shearstore=sheardum;
vpstore=vpdum;
rhstore=rhdum;
vsestore=vsedum;
rmsestore=rmsedum;
u850dum(nnold+1:end,:)=[];
v850dum(nnold+1:end,:)=[];
u850store=u850dum;
v850store=v850dum;
T600dum(nnold+1:end,:)=[];
T600store=T600dum;
%
clear vdum daydum hourdum monthdum rmdum latdum longdum pdum sheardum vpdum rhdum vseum rmsedum
%
savefile=char(strcat(masterfile,'.mat'));
save(char(savefile),'vstore', 'daystore', 'hourstore', 'monthstore', 'rmstore', 'latstore', 'longstore', ...
    'pstore', 'shearstore', 'vpstore', 'bas', 'city_radius', 'clat', 'clong', 'factor', 'freq', 'gmeth', ...
    'shape', 'freqyear', 'yearstore', 'u850store', 'v850store','model','flavor','vcrit','polyfile')
save(char(savefile),'T600store','rhstore','vsestore','rmsestore','-append')
%
savefileb=char(masterfile);
if strcmpi(netw,'y')
   netcdfwrite(savefileb)
end
if strcmpi(csvw,'y')
   csvconvoctf(savefileb);
end
comm=['rm -f ' char(savefile)];
system(comm);
save(char(savefile),'-v7','vstore', 'daystore', 'hourstore', 'monthstore', 'rmstore', 'latstore', 'longstore', ...
    'pstore', 'shearstore', 'vpstore', 'bas', 'city_radius', 'clat', 'clong', 'factor', 'freq', 'gmeth', ...
    'shape', 'freqyear', 'yearstore', 'u850store', 'v850store','model','flavor','vcrit','polyfile')
save(char(savefile),'-v7','T600store','rhstore','vsestore','rmsestore','-append')
%
fid=fopen(sfile,'r');
C=textscan(fid,'%s %s','delimiter',':');
g=fclose(fid);
u=C{2};
vcrit=u(8);
nt=str2double(u(9));
nun=str2double(u(10));
ftemp=str2double(u(11));
[ndum,~]=size(vstore);
nseeded=nun*ftemp*sum(1./freqyear);
nseeded=round(nseeded);
%
fs2=['rm -f ',sfile];
[~,~]=system(fs2);
%
fid=fopen('stats.txt','w');
fprintf(fid,'File:                       %s\n',char(masterfile));
fprintf(fid,'Model:                      %s\n',char(model));
fprintf(fid,'Type:                       %s\n',char(flavor));
if yearbeg == yearend
fprintf(fid,'Years or period:            %s\n',char(num2str(yearbeg)));
else
yearange=strcat(num2str(yearbeg),'-',num2str(yearend));
fprintf(fid,'Years:                      %s\n',char(yearange));
end
fprintf(fid,'Basin:                      %s\n',bas);
fprintf(fid,'Seeding method:             %s\n',char(gmeth));
fprintf(fid,'Filter type:                %s\n',char(shape));
fprintf(fid,'Critical wind speed (kts):  %s\n',char(vcrit));
if strcmp(shape,'circ')
    fprintf(fid,'POI Latitude:               %s\n',char(num2str(clat)));
    fprintf(fid,'POI Longitude:              %s\n',char(num2str(clong)));
    fprintf(fid,'Search Radius (km):         %s km\n',char(num2str(city_radius)));
end    

fprintf(fid,'Number of tracks:           %s\n',char(num2str(ndum)));
fprintf(fid,'Number of tracks seeded:    %s\n',char(num2str(nseeded)));
fprintf(fid,'Average annual frequency:   %s\n',char(num2str(freq)));
if strcmp(shape,'poly')
   fprintf(fid,'Poly file:                   %s\n',char(polyfile));
end
g=fclose(fid);
%
end
