ucftools/matlab/read_scal_complete_ucf.m

132 lines
4.0 KiB
Matlab

function [s] = read_scal_complete_ucf(file,varargin)
% [s] = read_scal_complete_ucf(file,varargin)
% Reads u,v,w,p from all processor chunks and combines them to a single field.
% Input
% file file name (if tar-mode: ustar handle)
% ? step index of timestep to be read (default: 1)
% ? verbosity verbose output? (default: 0)
% ? debug debug output? (default: 0)
% ? tarmode read from tar-file? (default: 0)
% Output
% s complete scalar fields with dim(nx,ny,nz,nscal)
% Parse optional input arguments
par = inputParser;
addParamValue(par,'step',1,@isnumeric);
addParamValue(par,'verbosity',0,@isnumeric);
addParamValue(par,'debug',0,@isnumeric);
addParamValue(par,'tarmode',0,@isnumeric); % deprecated
parse(par,varargin{:});
istep = par.Results.step;
% Open first file
obj = ucf('verbosity',par.Results.verbosity,'debug',par.Results.debug);
switch class(file)
case 'char'
[fdir,fbase,fext] = fileparts(file);
fname = sprintf('%s/%s.%05d',fdir,fbase,0);
obj.open(fname);
case {'ustar','ucfmulti'}
fname = sprintf('scal.%05d',0);
obj.opentar(file.pointer(fname));
otherwise
error('Input file type not supported: %s',class(file));
end
if ~obj.validateType('field')
error('read error: no field data.');
end
nscal = obj.getNumDatasets(istep);
% Read raw data
q = cell(1,nscal);
for iset=1:nscal
[data,params] = obj.readSet(istep,iset);
params = cast(params,'double');
ighost = params(1);
ib = params(2);
jb = params(3);
kb = params(4);
nxl = params(5);
nyl = params(6);
nzl = params(7);
nx = params(8);
ny = params(9);
nz = params(10);
q{iset} = reshape(data,nxl+2*ighost,nyl+2*ighost,nzl+2*ighost);
if ighost
q{iset} = q{iset}(2:end-1,2:end-1,2:end-1);
end
end
% Reassign content from loop and create global arrays
s = zeros(nx,ny,nz,nscal,class(q{1}));
for iscal=1:nscal
s(ib:ib+nxl-1,jb:jb+nyl-1,kb:kb+nzl-1,iscal) = q{iscal};
end
% Close the first file
obj.close();
% Now loop consecutively through files
ifile = 1;
switch class(file)
case 'char'
loopCondition = @(x) exist(x,'file');
fname = sprintf('%s/%s.%05d',fdir,fbase,ifile);
case {'ustar','ucfmulti'}
loopCondition = @(x) file.isSubfile(x);
fname = sprintf('scal.%05d',ifile);
end
while loopCondition(fname)
% Open file
obj = ucf('verbosity',par.Results.verbosity,'debug',par.Results.debug);
switch class(file)
case 'char'
obj.open(fname);
case {'ustar','ucfmulti'}
obj.opentar(file.pointer(fname));
end
if ~obj.validateType('field')
error('read error: no field data.');
end
% Read raw data
q = cell(1,nscal);
for iset=1:nscal
[data,params] = obj.readSet(istep,iset);
params = cast(params,'double');
ighost = params(1);
ib = params(2);
jb = params(3);
kb = params(4);
nxl = params(5);
nyl = params(6);
nzl = params(7);
q{iset} = reshape(data,nxl+2*ighost,nyl+2*ighost,nzl+2*ighost);
if ighost
q{iset} = q{iset}(2:end-1,2:end-1,2:end-1);
end
end
% Reassign content from loop and create global arrays
for iscal=1:nscal
s(ib:ib+nxl-1,jb:jb+nyl-1,kb:kb+nzl-1,iscal) = q{iscal};
end
% Close file
obj.close();
% Move on to next file
ifile = ifile+1;
switch class(file)
case 'char'
fname = sprintf('%s/%s.%05d',fdir,fbase,ifile);
case {'ustar','ucfmulti'}
fname = sprintf('scal.%05d',ifile);
end
end
end