fid=fopen('y2.out');
C=textscan(fid,'%s %s %d %d');
stormname=char(C{2});
date2=char(C{1});
n=C{3};
nt=C{4};
suffix='.txt';
%--------------------------
pifac=acos(-1)./180;
fac=1852.*60./(12.*3600);
tsize=100;
stormnamelc=stormname;
if stormname(1:2) == 'AL'
  stormnamelc(1:2)='al';
elseif stormname(1:2) == 'EP'
  stormnamelc(1:2)='ep';
elseif stormname(1:2) == 'WP'
  stormnamelc(1:2)='wp'; 
elseif stormname(1:2) == 'IO'
  stormnamelc(1:2)='io';
elseif stormname(1:2) == 'SH'
  stormnamelc(1:2)='sh';
end
nn=zeros(1,n);
nfiles=0;
for i=1:n,
%    g=strcat(stormname,'.',date2,'.e0',num2str(i),suffix);
    if i < 10
     enum=strcat('0',num2str(i));
    else
     enum=num2str(i);
    end
    g=strcat(stormnamelc,'/doy_EE',enum,'_',date2,'_',stormnamelc(1:4),suffix);
    A=exist(g);
    if A ~= 0
    file=load(g);
    nfiles=nfiles+1;
    [nn(nfiles),m]=size(file);
    tsize=min(tsize,nn(nfiles));
    for j=1:nn(nfiles),
      da(nfiles,j)=file(j,1);
      la(nfiles,j)=file(j,2);
      lo(nfiles,j)=file(j,3);
    end  
    if la(nfiles,nn(nfiles)) == 0
	[dum nn(nfiles)]=min(la(nfiles,:));
	nn(nfiles)=nn(nfiles)-1;
	nn(nfiles)=max(nn(nfiles),1);
    end
    end
end
%
n=nfiles;
tsize=max(nn);
u=zeros(n,tsize);
v=zeros(n,tsize);
coslat=cos(pifac.*la);
%
for i=1:n,
    for j=1:nn(i)-1,
        u(i,j)=fac.*coslat(j).*(lo(i,j+1)-lo(i,j));
        v(i,j)=fac.*(la(i,j+1)-la(i,j));
	if v(i,j) == 0
	 v(i,j)=0.01;
	end
	if u(i,j) == 0
	 u(i,j) = 0.01;
	end
    end
end    
um=zeros(1,tsize);
vm=zeros(1,tsize);
uvar=zeros(1,tsize);
vvar=zeros(1,tsize);
uv=zeros(1,tsize);
for j=1:tsize-1,
um(j)=mean(nonzeros(u(:,j)));
vm(j)=mean(nonzeros(v(:,j)));
uvar(j)=var(nonzeros(u(:,j)),1);
vvar(j)=var(nonzeros(v(:,j)),1);
uv(j)=mean((nonzeros(u(:,j))-um(j)).*(nonzeros(v(:,j))-vm(j)));
end
%
tsize=max(size(uv));
umn=zeros(1,nt);
vmn=zeros(1,nt);
uvarn=zeros(1,nt);
vvarn=zeros(1,nt);
uvn=zeros(1,nt);
for j=2:tsize;
    umn(j)=0.5.*(um(j)+um(j-1));
    vmn(j)=0.5.*(vm(j)+vm(j-1));
    uvarn(j)=0.5.*(uvar(j)+uvar(j-1));
    vvarn(j)=0.5.*(vvar(j)+vvar(j-1));
    uvn(j)=0.5.*(uv(j)+uv(j-1));
end
umn(1)=um(1);
vmn(1)=vm(1);
uvarn(1)=uvar(1);
vvarn(1)=vvarn(1);
uvn(1)=uv(1);
%
%umn(tsize)=um(tsize-1);
%vmn(tsize)=vm(tsize-1);
%uvarn(tsize)=uvar(tsize-1);
%vvarn(tsize)=vvarn(tsize-1);
%uvn(tsize)=uv(tsize-1);
%
uvarn=max(uvarn,0);
vvarn=max(vvarn,0);
%
dir2='data/ecmwf/';
%
file=strcat(dir2,date2,'/',stormname,'/uec');
fid=fopen(file,'w');
count=fwrite(fid,umn,'float64');
g=fclose(fid);
%
file=strcat(dir2,date2,'/',stormname,'/vec');
fid=fopen(file,'w');
count=fwrite(fid,vmn,'float64');
g=fclose(fid);
%
file=strcat(dir2,date2,'/',stormname,'/u2ec');
fid=fopen(file,'w');
count=fwrite(fid,uvarn,'float64');
g=fclose(fid);
%
file=strcat(dir2,date2,'/',stormname,'/v2ec');
fid=fopen(file,'w');
count=fwrite(fid,vvarn,'float64');
g=fclose(fid);
%
file=strcat(dir2,date2,'/',stormname,'/uvec');
fid=fopen(file,'w');
count=fwrite(fid,uvn,'float64');
g=fclose(fid);
%
file=strcat(dir2,date2,'/',stormname,'/tracks');
save(file,'da','la','lo','nn')
        
        
