ucftools/matlab/read_field_chunk_ucf.m

88 lines
3.2 KiB
Matlab

function [data,ib,jb,kb,nxl,nyl,nzl,ighost] = read_field_chunk_ucf(file,field,varargin)
% [data,ib,jb,kb,nxl,nyl,nzl,ighost] = read_field_chunk_ucf(file,field,varargin)
% Reads single field from one processor chunk.
% Input
% file file name (if tar-mode: ustar handle)
% field field to be read
% {'u','v','w','p','s1','s2',...}
% or integer with dataset index (not in tar-mode)
% ? step index of timestep to be read (default: 1)
% ? ghost keep ghost cells? (default: yes)
% ? verbosity verbose output? (default: no)
% ? debug debug output? (default: no)
% ? tarmode read from tar-file? (default: 0)
% ? rank rank of processor, ignored if not in tarmode (default: 0)
% Output
% data partial field with dim(nxl+2*ighost,nyl+2*ighost,nzl+2*ighost)
% ib,jb,kb global index of first grid point of the partial field w/o ghost cells
% nxl,nyl,nzl local field size w/o ghost cells
% ighost ghost cell flag
% Parse optional input arguments
par = inputParser;
addParamValue(par,'step',1,@isnumeric);
addParamValue(par,'ghost',1,@isnumeric);
addParamValue(par,'verbosity',0,@isnumeric);
addParamValue(par,'debug',0,@isnumeric);
addParamValue(par,'tarmode',0,@isnumeric); % deprecated
addParamValue(par,'rank',0,@isnumeric);
parse(par,varargin{:});
istep = par.Results.step;
keepghost = par.Results.ghost;
% Parse field
if ischar(field)
switch field(1)
case 'u'; ifield=1; fbase='uvwp';
case 'v'; ifield=2; fbase='uvwp';
case 'w'; ifield=3; fbase='uvwp';
case 'p'; ifield=4; fbase='uvwp';
case 's'; ifield=str2double(field(2:end)); fbase='scal';
end
elseif isnumeric(field)
if par.Results.tarmode
error('field cannot be numeric, if tar-mode is used.');
end
ifield=field; fbase='';
else
error('field must either be numeric or string.');
end
% Open file
obj = ucf('verbosity',par.Results.verbosity,'debug',par.Results.debug);
switch class(file)
case 'char'
obj.open(file);
case {'ustar','ucfmulti'}
subfile = sprintf('%s.%05d',fbase,par.Results.rank);
obj.opentar(file.pointer(subfile));
otherwise
error('Input file type not supported: %s',class(file));
end
% Read raw data
if ~obj.validateType('field')
error('read error: no field data.');
end
[data,params] = obj.readSet(istep,ifield);
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);
data = reshape(data,nxl+2*ighost,nyl+2*ighost,nzl+2*ighost);
if ighost && ~keepghost
data = data(2:end-1,2:end-1,2:end-1);
ighost = 0;
end
% Close UCF file
obj.close();
end