Warning, /utils/matlab/cs_grid/latloncap/quikread_ik.m is written in an unsupported language. File is not indexed.
view on githubraw file Latest commit 86830fc5 on 2016-12-16 18:19:43 UTC
86830fc597 Dimi*0001 function fld=quikread_ik(fnam,ix,kx,nx,nz,prec,mform)
c387a29a2c Dimi*0002
86830fc597 Dimi*0003 % Function fld=quikread_ik(fnam,ix,kx,nx,nz,prec,mform)
c387a29a2c Dimi*0004 % read in specific indices (ix,kx) from a 2D
0005 % binary file of dimension(nx,nz)
0006 %
0007 % INPUTS
0008 % fnam input path and file name
0009 % ix i-indices, 1 to nx, to read (default 1)
0010 % kx k-indices, 1 to nz, to read (e.g., 1:50, default 1)
0011 % nx x-dimension of binary file (default 1440)
0012 % nz z-dimension of binary file (default 50)
86830fc597 Dimi*0013 % prec numeric precision (see fread; default: 'real*4')
0014 % mform machine format (see fopen; default: 'ieee-be')
c387a29a2c Dimi*0015 %
0016 % OUTPUTS
0017 % fld output array of dimension length(ix)*length(kx)
0018 %
0019 % SEE ALSO
0020 % readbin read_ijk read_ijkt writebin
0021
86830fc597 Dimi*0022 if nargin < 7, mform='ieee-be'; end
0023 if nargin < 6, prec='real*4'; end
c387a29a2c Dimi*0024 if nargin < 5, nz=50; end
0025 if nargin < 4, nx=1440; end
0026 if nargin < 3, kx=1; end
0027 if nargin < 2, ix=1; end
0028 if nargin < 1, error('please specify input file name'); end
0029
86830fc597 Dimi*0030 if ~exist(fnam)
0031 error(['File ' fnam ' does not exist.'])
0032 end
0033
0034 dx=min(diff(ix(:)));
0035
0036 if dx ~= max(diff(ix(:)))
0037 % Use memory mapping. Can handle arbitrary i-indices but
0038 % can be slow and require a lot of memory for large files.
0039
0040 switch prec
0041 case {'integer*1'}
0042 prec='int8';
0043 case {'integer*2'}
0044 prec='int16';
0045 case {'integer*4'}
0046 prec='int32';
0047 case {'real*4','float32'}
0048 prec='single';
0049 case {'integer*8'}
0050 prec='int64';
0051 case {'real*8','float64'}
0052 prec='double';
0053 end
0054
0055 m=memmapfile(fnam,'Format',{prec [nx,nz] 'fld'});
0056 fld=swapbytes(m.Data.fld(ix,kx));
0057
0058 else
0059 % Use fread SKIP capability. Much faster and memory efficient
0060 % for large files but requires that i-indices be equidistant.
0061
0062 if isempty(dx), dx=0;, end
0063
0064 switch prec
0065 case {'int8','integer*1'}
0066 reclength=1;
0067 case {'int16','integer*2','uint16'}
0068 reclength=2;
0069 case {'int32','integer*4','uint32','single','real*4','float32'}
0070 reclength=4;
0071 case {'int64','integer*8','uint64','double','real*8','float64'}
0072 reclength=8;
0073 end
0074
0075 fid=fopen(fnam,'r',mform);
0076 fld=zeros(length(ix),length(kx));
0077 for k=1:length(kx)
0078 skip=(kx(k)-1)*nx+ix(1)-1;
0079 if(fseek(fid,skip*reclength,'bof')<0), error('past end of file'); end
0080 fld(:,k)=fread(fid,length(ix),prec,(dx-1)*reclength,mform);
0081 end
0082 fid=fclose(fid);
0083
0084 end