Warning, /utils/matlab/cs_grid/read_cs/read_ijk.m is written in an unsupported language. File is not indexed.
view on githubraw file Latest commit 602828db on 2009-11-21 19:16:16 UTC
602828dbcc Dimi*0001 function fld=read_ijk(fnam,ix,jx,kx,nx,ny,nz,prec)
0002
0003 % Function fld=read_ijk(fnam,ix,jx,kx,nx,ny,nz,prec)
0004 % read in specific indices (ix,jx,kx) from a 3D
0005 % binary file of dimension(nx,ny,nz)
0006 %
0007 % INPUTS
0008 % fnam input path and file name
0009 % ix i-indices, 1 to nx, to read (default 1)
0010 % jx j-indices, 1 to ny, to read (default 1)
0011 % kx k-indices, 1 to nz, to read (e.g., 1:50, default 1)
0012 % nx x-dimension of binary file (default 1440)
0013 % ny y-dimension of binary file (default 640)
0014 % nz z-dimension of binary file (default 50)
0015 % prec numeric precision (see fread; default 'real*4')
0016 %
0017 % OUTPUTS
0018 % fld output array of dimension length(ix)*length(jx)*length(kx)
0019 %
0020 % SEE ALSO
0021 % readbin read_ijkt writebin
0022
0023 if nargin < 8, prec='real*4'; end
0024 if nargin < 7, nz=50; end
0025 if nargin < 6, ny=640; end
0026 if nargin < 5, nx=1440; end
0027 if nargin < 4, kx=1; end
0028 if nargin < 3, jx=1; end
0029 if nargin < 2, ix=1; end
0030 if nargin < 1, error('please specify input file name'); end
0031
0032 fld=zeros(length(ix),length(jx),length(kx));
0033 fid=fopen(fnam,'r','ieee-be');
0034
0035 switch prec
0036 case {'int8','integer*1'}
0037 preclength=1;
0038 case {'int16','integer*2','uint16','integer*2'}
0039 preclength=2;
0040 case {'int32','integer*4','uint32','single','real*4','float32'}
0041 preclength=4;
0042 case {'int64','integer*8','uint64','double','real*8','float64'}
0043 preclength=8;
0044 end
0045
0046 for k=1:length(kx)
0047 for j=1:length(jx)
0048 for i=1:length(ix)
0049 skip = (kx(k)-1)*ny*nx + (jx(j)-1)*nx + ix(i)-1;
0050 if(fseek(fid,skip*preclength,'bof')<0), error('past end of file'); end
0051 fld(i,j,k)=fread(fid,1,prec);
0052 end
0053 end
0054 end
0055
0056 fid=fclose(fid);
0057 fld=squeeze(fld);