%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% CONVERT HDF5 FIELD FROM FOURIER TO PHYSICAL SPACE %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



%------------------------------------
file     = 'tbl2-HR-Prod.270.u.h5';        %File to read

ibeg     = 5;                              %Start position in the X-grid
iend     = 10; 				   %End   position in the X-grid
					                                
jbeg     = 10; 				   %Start position in the Y-grid
jend     = 15; 				   %End   position in the Y-grid
%------------------------------------

itot     = iend-ibeg+1;                    %Total # planes in X 
jtot     = jend-jbeg+1; 		   %Total # planes in Y 
Nxy      = itot*jtot;   		   %Size of the XY plane

kzf      = double( h5read(file,'/nz2'))+1; %Number of Complex Modes
xx       =  2.^[0:15];                     %Radix 2 numbers
Nfou     =  xx(find(xx>=3/2*kzf,1));       %Radix 2 number for the fft

%Read portion of the TBL:
ini_v    = [1    , jbeg, ibeg ];                        %Position to seek		       
count_v  = [2*kzf, jtot, itot ]; 			%Number of elements in each direction.
strid_v  = [1    , 1   , 1    ]; 			%Stride in each direction.            
buff     = h5read(file,'/value',ini_v,count_v,strid_v); %Read file

%Cast buff as a complex array:
buff     = reshape(buff,2,kzf,Nxy);
buff     = squeeze(complex(buff(1,:,:),buff(2,:,:))); 
buff(kzf+1:Nfou,1:Nxy) = 0; %Zero padding

cb       = [buff; zeros(1,Nxy); conj(buff(end:-1:2,:))]; clear buff;
cb       = real(ifft(cb))*2*Nfou;

cb       = reshape(cb,Nfou*2,jtot,itot); %Real field 















