% Code for extract radar radial o vector current data from original wera 
%files given the grid coordinates.


% Julian day
year=2008; stryear=num2str(year);
first_day=001;
last_day=013;
jd=first_day:last_day; jd=jd(:);
%strjd=num2str(jd); 

for ij=jd(1):jd(end)
if length(num2str(ij))==1
    strjd(ij,:)=['00' num2str(ij)];
elseif length(num2str(ij))==2
    strjd(ij,:)=['0' num2str(ij)];
else
    strjd(ij,:)=num2str(ij);
end
end

% Time in day
%hour=0:23;
%for ji=1:length(hour)
%    if length(num2str(ji))==1
%    strhour(ji,:)=['0' num2str(ji)];
%    else
%        strhour(ji,:)=num2str(hour(ji));
%    end
%end

hour=0:1:23; hour=hour(:);
strhour=num2str(hour);
indzero=find(hour==0)
induno=find(hour==1);
inddos=find(hour==2);
indtr=find(hour==3);
indcua=find(hour==4);
indcinc=find(hour==5);
indsei=find(hour==6);
indsiet=find(hour==7);
indoch=find(hour==8);
indnue=find(hour==9);
 strhour(indzero,:)=['00'];
 strhour(induno,:)=['01'];
 strhour(inddos,:)=['02'];
 strhour(indtr,:)=['03'];
 strhour(indcua,:)=['04'];
 strhour(indcinc,:)=['05'];
 strhour(indsei,:)=['06'];
 strhour(indsiet,:)=['07'];
 strhour(indoch,:)=['08'];
 strhour(indnue,:)=['09'];

min=10:20:50; min=min(:);
strmin=num2str(min);
%indzero=find(min==0)
%    strmin(indzero,:)=['00'];
    
%load XI.dat 
%load YI.dat

%l=(999.9*ones([1813,2]));



%tmp=[];

%for ji=numero de archivos;
 for ii=001:013           % Julian Day
     for jj=1:length(hour)          % Hours
         for kk=1:length(min)    % Minutes
         
 wera_waves = ['/home/wera/www/efs/data/cran_nkl_jan_feb_2008'];

 filename = [wera_waves '/' stryear strjd(ii,:) strhour(jj,:) strmin(kk,:) '_nkl.SPEC']; 
 fid=fopen(filename,'rb');
 % read header (first 512 bytes)
 head=fread(fid,512,'char');
 % this makes the header readable
 [header,grid_lon,grid_lat]=parseHeader(head);
 head=char(head');
 %frequency stuff
 fbragg=sqrt(9.81*num2str(header.freq)/300.0*pi);
 dt=str2num(header.delta_t);
 freq_start=-1/(2*dt);
 freq_end=1/(2*dt)*255/256;
 freq_grad=-(freq_start-freq_end)/512;
 freq=(freq_start+freq_grad):freq_grad:freq_end;
 %output power
 nx=str2num(header.nx);
  ny=str2num(header.ny);
  
  for i=1:nx
    for j=1:ny
      dum=fread(fid,512,'float32');
      data(i,j,1:512)=single(dum);
    end
  end
  data = double(data);
  fname = [filename(1:57),'_nkl_spec'];
  eval(['save ',fname,' data']);
end
end
  
%load temp.dat
%eval(['!rm ',wera_waves,'/',stryear,strjd(ii,:),strhour(jj,:),strmin(kk,:),'_cdn.SORT']);
end



