Back to home page

MITgcm

 
 

    


Warning, /verification/advect_cs/input/rdda.m is written in an unsupported language. File is not indexed.

view on githubraw file Latest commit 5ce54005 on 2010-03-30 13:59:46 UTC
5ce5400572 Davi*0001 % result = RDDA( file, dim, irec [options] )
                0002 %
                0003 % This routine reads the irec'th record of shape 'dim' from the
                0004 % direct-access binary file (float or double precision) 'file'.
                0005 %
                0006 % Required arguments:
                0007 %
                0008 %   file  - string  - name of file to read from
                0009 %   dim   - vector  - dimensions of the file records and the resulting array
                0010 %   irec  - integer - record number in file in which to write data
                0011 %
                0012 % Optional arguments (must appear after the required arguments):
                0013 %   prec  - string  - precision of storage in file. Default = 'real*8'.
                0014 %   ieee  - string  - IEEE bit-wise representation in file. Default = 'b'.
                0015 %
                0016 % 'prec' may take the values:
                0017 %       'real*4' - floating point, 32 bits.
                0018 %       'real*8' - floating point, 64 bits - the efault.
                0019 %
                0020 % 'ieee' may take values:
                0021 %    'ieee-be'     or 'b' - IEEE floating point with big-endian
                0022 %                           byte ordering - the default
                0023 %    'ieee-le'     or 'l' - IEEE floating point with little-endian
                0024 %                           byte ordering
                0025 %    'native'      or 'n' - local machine format
                0026 %    'cray'        or 'c' - Cray floating point with big-endian
                0027 %                           byte ordering
                0028 %    'ieee-le.l64' or 'a' - IEEE floating point with little-endian
                0029 %                           byte ordering and 64 bit long data type
                0030 %    'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
                0031 %                           ordering and 64 bit long data type.
                0032 %
                0033 % eg.   T=rdda('t.data',[64 64 32],1);
                0034 %       T=rdda('t.data',[256],4,'real*4');
                0035 %       T=rdda('t.data',[128 64],2,'real*4','b');
                0036 function [arr] = rdda(file,N,k,varargin)
                0037 
                0038 % Defaults
                0039 WORDLENGTH=8;
                0040 rtype='real*8';
                0041 ieee='b';
                0042 
                0043 % Check optional arguments
                0044 args=char(varargin);
                0045 while (size(args,1) > 0)
                0046  if deblank(args(1,:)) == 'real*4'
                0047   WORDLENGTH=4;
                0048   rtype='real*4';
                0049  elseif deblank(args(1,:)) == 'real*8'
                0050   WORDLENGTH=8;
                0051   rtype='real*8';
                0052  elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
                0053   ieee='n';
                0054  elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
                0055   ieee='l';
                0056  elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
                0057   ieee='b';
                0058  elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
                0059   ieee='c';
                0060  elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
                0061   ieee='a';
                0062  elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
                0063   ieee='s';
                0064  else
                0065   sprintf(['Optional argument ' args(1,:) ' is unknown'])
                0066   return
                0067  end
                0068  args=args(2:end,:);
                0069 end
                0070 
                0071 nnn=prod(N);
                0072 
                0073 [fid mess]=fopen(file,'r',ieee);
                0074 if fid == -1
                0075  sprintf('Error while opening file:\n%s',mess)
                0076  arr=0;
                0077  return
                0078 end
                0079 st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof');
                0080 if st ~= 0
                0081  mess=ferror(fid);
                0082  sprintf('There was an error while positioning the file pointer:\n%s',mess)
                0083  arr=0;
                0084  return
                0085 end
                0086 [arr count]=fread(fid,nnn,rtype);
                0087 if count ~= nnn
                0088  sprintf('Not enough data was available to be read: off EOF?')
                0089  arr=0;
                0090  return
                0091 end
                0092 st=fclose(fid);
                0093 arr=reshape(arr,N);