Warning, /verification/tutorial_global_oce_latlon/diags_matlab/rdmeta.m is written in an unsupported language. File is not indexed.
view on githubraw file Latest commit add29e06 on 2018-01-31 20:35:05 UTC
051ee7f715 Jean*0001 function [AA] = rdmds(fname,varargin)
0002 %
0003 % Read MITgcmUV Meta/Data files
0004 %
0005 % A = RDMDS(FNAME) reads data described by meta/data file format.
0006 % FNAME is a string containing the "head" of the file names.
0007 %
0008 % eg. To load the meta-data files
0009 % T.0000002880.000.000.meta, T.0000002880.000.000.data
0010 % T.0000002880.001.000.meta, T.0000002880.001.000.data
0011 % T.0000002880.002.000.meta, T.0000002880.002.000.data
0012 % T.0000002880.003.000.meta, T.0000002880.003.000.data
0013 % use
0014 % >> A=rdmds('T.0000002880');
0015 %
0016 % A = RDMDS(FNAME,MACHINEFORMAT) allows the machine format to be specified
0017 % which MACHINEFORMAT is on of the following strings:
0018 %
0019 % 'native' or 'n' - local machine format - the default
0020 % 'ieee-le' or 'l' - IEEE floating point with little-endian
0021 % byte ordering
0022 % 'ieee-be' or 'b' - IEEE floating point with big-endian
0023 % byte ordering
0024 % 'vaxd' or 'd' - VAX D floating point and VAX ordering
0025 % 'vaxg' or 'g' - VAX G floating point and VAX ordering
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 % Default options
0034 ieee='b';
0035
0036 % Check optional arguments
0037 args=char(varargin);
0038 while (size(args,1) > 0)
0039 if deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
0040 ieee='n';
0041 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
0042 ieee='l';
0043 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
0044 ieee='b';
0045 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
0046 ieee='c';
0047 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
0048 ieee='a';
0049 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
0050 ieee='s';
0051 else
0052 error(['Optional argument ' args(1,:) ' is unknown'])
0053 end
0054 args=args(2:end,:);
0055 end
0056
0057 % Match name of all meta-files
0058 eval(['ls ' fname '*.meta;']);
0059 allfiles=ans;
0060
0061 % Beginning and end of strings
0062 Iend=findstr(allfiles,'.meta')+4;
0063 Ibeg=[1 Iend(1:end-1)+2];
0064
0065 % Loop through allfiles
0066 for j=1:prod(size(Ibeg)),
0067
0068 % Read meta- and data-file
0069 [A,N] = localrdmds(allfiles(Ibeg(j):Iend(j)),ieee);
0070
0071 bdims=N(1,:);
0072 r0=N(2,:);
0073 rN=N(3,:);
0074 ndims=prod(size(bdims));
0075 if (ndims == 1)
0076 AA(r0(1):rN(1))=A;
0077 elseif (ndims == 2)
0078 AA(r0(1):rN(1),r0(2):rN(2))=A;
0079 elseif (ndims == 3)
0080 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3))=A;
0081 elseif (ndims == 4)
0082 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4))=A;
0083 else
0084 error('Dimension of data set is larger than currently coded. Sorry!')
0085 end
0086
0087 end
0088
0089 %-------------------------------------------------------------------------------
0090
0091 function [A,N] = localrdmds(fname,ieee)
0092
0093 mname=strrep(fname,' ','');
0094 dname=strrep(mname,'.meta','.data');
0095
0096 % Read and interpret Meta file
0097 fid = fopen(mname,'r');
0098 if (fid == -1)
0099 error(['File' mname ' could not be opened'])
0100 end
0101
0102 % Scan each line of the Meta file
0103 allstr=' ';
0104 keepgoing = 1;
0105 while keepgoing > 0,
0106 line = fgetl(fid);
0107 if (line == -1)
0108 keepgoing=-1;
0109 else
0110 % Strip out "(PID.TID *.*)" by finding first ")"
0111 %old ind=findstr([line ')'],')'); line=line(ind(1)+1:end);
0112 ind=findstr(line,')');
0113 if size(ind) ~= 0
0114 line=line(ind(1)+1:end);
0115 end
0116 % Remove comments of form //
0117 line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1);
0118 % Add to total string
0119 allstr=[allstr line];
0120 end
0121 end
0122
0123 % Close meta file
0124 fclose(fid);
0125
0126 % Strip out comments of form /* ... */
0127 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
0128 if size(ind1) ~= size(ind2)
0129 error('The /* ... */ comments are not properly paired')
0130 end
0131 while size(ind1,2) > 0
0132 allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)];
0133 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
0134 end
0135
0136 % This is a kludge to catch whether the meta-file is of the
0137 % old or new type. nrecords does not exist in the old type.
0138 nrecords = -987;
0139
0140 % Everything in lower case
0141 allstr=lower(allstr);
0142
0143 % Fix the unfortunate choice of 'format'
0144 allstr=strrep(allstr,'format','dataprec');
0145
0146 % Evaluate meta information
0147 eval(allstr);
0148
0149 N=reshape( dimlist , 3 , prod(size(dimlist))/3 );
0150
0151 if nrecords == -987
0152 % This is the old 'meta' method that used sequential access
0153
0154 A=allstr;
0155 % Open data file
0156 fid=fopen(dname,'r',ieee);
0157
0158 % Read record size in bytes
0159 recsz=fread(fid,1,'uint32');
0160 ldims=N(3,:)-N(2,:)+1;
0161 numels=prod(ldims);
0162
0163 rat=recsz/numels;
0164 if rat == 4
0165 A=fread(fid,numels,'real*4');
0166 elseif rat == 8
0167 A=fread(fid,numels,'real*8');
0168 else
0169 sprintf(' Implied size in meta-file = %d', numels )
0170 sprintf(' Record size in data-file = %d', recsz )
0171 error('Ratio between record size and size in meta-file inconsistent')
0172 end
0173
0174 erecsz=fread(fid,1,'uint32');
0175 if erecsz ~= recsz
0176 sprintf('WARNING: Record sizes at beginning and end of file are inconsistent')
0177 end
0178
0179 fclose(fid);
0180
0181 A=reshape(A,ldims);
0182
0183 else
0184 % This is the new MDS format that uses direct access
0185
0186 ldims=N(3,:)-N(2,:)+1;
0187 if dataprec == 'float32'
0188 A=myrdda(dname,ldims,1,'real*4',ieee);
0189 elseif dataprec == 'float64'
0190 A=myrdda(dname,ldims,1,'real*8',ieee);
0191 else
aadd12719a Jean*0192 error(['Unrecognized dataprec in meta-file = ' dataprec]);
051ee7f715 Jean*0193 end
0194
0195 end
0196
0197 %-------------------------------------------------------------------------------
0198
0199 % result = RDDA( file, dim, irec [options] )
0200 %
0201 % This routine reads the irec'th record of shape 'dim' from the
0202 % direct-access binary file (float or double precision) 'file'.
0203 %
0204 % Required arguments:
0205 %
0206 % file - string - name of file to read from
0207 % dim - vector - dimensions of the file records and the resulting array
0208 % irec - integer - record number in file in which to write data
0209 %
0210 % Optional arguments (must appear after the required arguments):
0211 % prec - string - precision of storage in file. Default = 'real*8'.
0212 % ieee - string - IEEE bit-wise representation in file. Default = 'b'.
0213 %
0214 % 'prec' may take the values:
0215 % 'real*4' - floating point, 32 bits.
0216 % 'real*8' - floating point, 64 bits - the efault.
0217 %
0218 % 'ieee' may take values:
0219 % 'ieee-be' or 'b' - IEEE floating point with big-endian
0220 % byte ordering - the default
0221 % 'ieee-le' or 'l' - IEEE floating point with little-endian
0222 % byte ordering
0223 % 'native' or 'n' - local machine format
0224 % 'cray' or 'c' - Cray floating point with big-endian
0225 % byte ordering
0226 % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
0227 % byte ordering and 64 bit long data type
0228 % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
0229 % ordering and 64 bit long data type.
0230 %
0231 % eg. T=rdda('t.data',[64 64 32],1);
0232 % T=rdda('t.data',[256],4,'real*4');
0233 % T=rdda('t.data',[128 64],2,'real*4','b');
0234 function [arr] = myrdda(file,N,k,varargin)
0235
0236 % Defaults
0237 WORDLENGTH=8;
0238 rtype='real*8';
0239 ieee='b';
0240
0241 % Check optional arguments
0242 args=char(varargin);
0243 while (size(args,1) > 0)
0244 if deblank(args(1,:)) == 'real*4'
0245 WORDLENGTH=4;
0246 rtype='real*4';
0247 elseif deblank(args(1,:)) == 'real*8'
0248 WORDLENGTH=8;
0249 rtype='real*8';
0250 elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
0251 ieee='n';
0252 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
0253 ieee='l';
0254 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
0255 ieee='b';
0256 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
0257 ieee='c';
0258 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
0259 ieee='a';
0260 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
0261 ieee='s';
0262 else
0263 error(['Optional argument ' args(1,:) ' is unknown'])
0264 end
0265 args=args(2:end,:);
0266 end
0267
0268 nnn=prod(N);
0269
0270 [fid mess]=fopen(file,'r',ieee);
0271 if fid == -1
0272 error('Error while opening file:\n%s',mess)
0273 end
0274 st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof');
0275 if st ~= 0
0276 mess=ferror(fid);
0277 error('There was an error while positioning the file pointer:\n%s',mess)
0278 end
0279 [arr count]=fread(fid,nnn,rtype);
0280 if count ~= nnn
0281 error('Not enough data was available to be read: off EOF?')
0282 end
0283 st=fclose(fid);
0284 arr=reshape(arr,N);