| 1 | 
enderton | 
1.1 | 
function [AA] = rdmds(fname,varargin) | 
| 2 | 
  | 
  | 
% | 
| 3 | 
  | 
  | 
% Read MITgcmUV Meta/Data files | 
| 4 | 
  | 
  | 
% | 
| 5 | 
  | 
  | 
% A = RDMDS(FNAME) reads data described by meta/data file format. | 
| 6 | 
  | 
  | 
% FNAME is a string containing the "head" of the file names. | 
| 7 | 
  | 
  | 
% | 
| 8 | 
  | 
  | 
% eg. To load the meta-data files | 
| 9 | 
  | 
  | 
%     T.0000002880.000.000.meta, T.0000002880.000.000.data | 
| 10 | 
  | 
  | 
%     T.0000002880.001.000.meta, T.0000002880.001.000.data | 
| 11 | 
  | 
  | 
%     T.0000002880.002.000.meta, T.0000002880.002.000.data | 
| 12 | 
  | 
  | 
%     T.0000002880.003.000.meta, T.0000002880.003.000.data | 
| 13 | 
  | 
  | 
% use | 
| 14 | 
  | 
  | 
%    >> A=rdmds('T.0000002880'); | 
| 15 | 
  | 
  | 
% | 
| 16 | 
  | 
  | 
% A = RDMDS(FNAME,MACHINEFORMAT) allows the machine format to be specified | 
| 17 | 
  | 
  | 
% which MACHINEFORMAT is on of the following strings: | 
| 18 | 
  | 
  | 
% | 
| 19 | 
  | 
  | 
%   'native'      or 'n' - local machine format - the default | 
| 20 | 
  | 
  | 
%   'ieee-le'     or 'l' - IEEE floating point with little-endian | 
| 21 | 
  | 
  | 
%                          byte ordering | 
| 22 | 
  | 
  | 
%   'ieee-be'     or 'b' - IEEE floating point with big-endian | 
| 23 | 
  | 
  | 
%                          byte ordering | 
| 24 | 
  | 
  | 
%   'vaxd'        or 'd' - VAX D floating point and VAX ordering | 
| 25 | 
  | 
  | 
%   'vaxg'        or 'g' - VAX G floating point and VAX ordering | 
| 26 | 
  | 
  | 
%   'cray'        or 'c' - Cray floating point with big-endian | 
| 27 | 
  | 
  | 
%                          byte ordering | 
| 28 | 
  | 
  | 
%   'ieee-le.l64' or 'a' - IEEE floating point with little-endian | 
| 29 | 
  | 
  | 
%                          byte ordering and 64 bit long data type | 
| 30 | 
  | 
  | 
%   'ieee-be.l64' or 's' - IEEE floating point with big-endian byte | 
| 31 | 
  | 
  | 
%                          ordering and 64 bit long data type. | 
| 32 | 
  | 
  | 
% | 
| 33 | 
  | 
  | 
% $Header: /u/gcmpack/MITgcm/utils/matlab/rdmeta.m,v 1.2 2001/05/29 14:01:41 adcroft Exp $ | 
| 34 | 
  | 
  | 
 | 
| 35 | 
  | 
  | 
% Default options | 
| 36 | 
  | 
  | 
ieee='b'; | 
| 37 | 
  | 
  | 
 | 
| 38 | 
  | 
  | 
% Check optional arguments | 
| 39 | 
  | 
  | 
args=char(varargin); | 
| 40 | 
  | 
  | 
while (size(args,1) > 0) | 
| 41 | 
  | 
  | 
 if deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' | 
| 42 | 
  | 
  | 
  ieee='n'; | 
| 43 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' | 
| 44 | 
  | 
  | 
  ieee='l'; | 
| 45 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' | 
| 46 | 
  | 
  | 
  ieee='b'; | 
| 47 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' | 
| 48 | 
  | 
  | 
  ieee='c'; | 
| 49 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' | 
| 50 | 
  | 
  | 
  ieee='a'; | 
| 51 | 
  | 
  | 
 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' | 
| 52 | 
  | 
  | 
  ieee='s'; | 
| 53 | 
  | 
  | 
 else | 
| 54 | 
  | 
  | 
  error(['Optional argument ' args(1,:) ' is unknown']) | 
| 55 | 
  | 
  | 
 end | 
| 56 | 
  | 
  | 
 args=args(2:end,:); | 
| 57 | 
  | 
  | 
end | 
| 58 | 
  | 
  | 
 | 
| 59 | 
  | 
  | 
% Match name of all meta-files | 
| 60 | 
  | 
  | 
eval(['ls ' fname '*.meta;']); | 
| 61 | 
  | 
  | 
allfiles=ans; | 
| 62 | 
  | 
  | 
 | 
| 63 | 
  | 
  | 
% Beginning and end of strings | 
| 64 | 
  | 
  | 
Iend=findstr(allfiles,'.meta')+4; | 
| 65 | 
  | 
  | 
Ibeg=[1 Iend(1:end-1)+2]; | 
| 66 | 
  | 
  | 
 | 
| 67 | 
  | 
  | 
% Loop through allfiles | 
| 68 | 
  | 
  | 
for j=1:prod(size(Ibeg)), | 
| 69 | 
  | 
  | 
 | 
| 70 | 
  | 
  | 
% Read meta- and data-file | 
| 71 | 
  | 
  | 
[A,N] = localrdmds(allfiles(Ibeg(j):Iend(j)),ieee); | 
| 72 | 
  | 
  | 
 | 
| 73 | 
  | 
  | 
bdims=N(1,:); | 
| 74 | 
  | 
  | 
r0=N(2,:); | 
| 75 | 
  | 
  | 
rN=N(3,:); | 
| 76 | 
  | 
  | 
ndims=prod(size(bdims)); | 
| 77 | 
  | 
  | 
if     (ndims == 1) | 
| 78 | 
  | 
  | 
 AA(r0(1):rN(1))=A; | 
| 79 | 
  | 
  | 
elseif (ndims == 2) | 
| 80 | 
  | 
  | 
 AA(r0(1):rN(1),r0(2):rN(2))=A; | 
| 81 | 
  | 
  | 
elseif (ndims == 3) | 
| 82 | 
  | 
  | 
 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3))=A; | 
| 83 | 
  | 
  | 
elseif (ndims == 4) | 
| 84 | 
  | 
  | 
 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4))=A; | 
| 85 | 
  | 
  | 
else | 
| 86 | 
  | 
  | 
 error('Dimension of data set is larger than currently coded. Sorry!') | 
| 87 | 
  | 
  | 
end | 
| 88 | 
  | 
  | 
 | 
| 89 | 
  | 
  | 
end | 
| 90 | 
  | 
  | 
 | 
| 91 | 
  | 
  | 
%------------------------------------------------------------------------------- | 
| 92 | 
  | 
  | 
 | 
| 93 | 
  | 
  | 
function [A,N] = localrdmds(fname,ieee) | 
| 94 | 
  | 
  | 
 | 
| 95 | 
  | 
  | 
mname=strrep(fname,' ',''); | 
| 96 | 
  | 
  | 
dname=strrep(mname,'.meta','.data'); | 
| 97 | 
  | 
  | 
 | 
| 98 | 
  | 
  | 
% Read and interpret Meta file | 
| 99 | 
  | 
  | 
fid = fopen(mname,'r'); | 
| 100 | 
  | 
  | 
if (fid == -1) | 
| 101 | 
  | 
  | 
 error(['File' mname ' could not be opened']) | 
| 102 | 
  | 
  | 
end | 
| 103 | 
  | 
  | 
 | 
| 104 | 
  | 
  | 
% Scan each line of the Meta file | 
| 105 | 
  | 
  | 
allstr=' '; | 
| 106 | 
  | 
  | 
keepgoing = 1; | 
| 107 | 
  | 
  | 
while keepgoing > 0, | 
| 108 | 
  | 
  | 
 line = fgetl(fid); | 
| 109 | 
  | 
  | 
 if (line == -1) | 
| 110 | 
  | 
  | 
  keepgoing=-1; | 
| 111 | 
  | 
  | 
 else | 
| 112 | 
  | 
  | 
% Strip out "(PID.TID *.*)" by finding first ")" | 
| 113 | 
  | 
  | 
%old  ind=findstr([line ')'],')'); line=line(ind(1)+1:end); | 
| 114 | 
  | 
  | 
  ind=findstr(line,')'); | 
| 115 | 
  | 
  | 
  if size(ind) ~= 0 | 
| 116 | 
  | 
  | 
    line=line(ind(1)+1:end); | 
| 117 | 
  | 
  | 
  end | 
| 118 | 
  | 
  | 
% Remove comments of form // | 
| 119 | 
  | 
  | 
  line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1); | 
| 120 | 
  | 
  | 
% Add to total string | 
| 121 | 
  | 
  | 
  allstr=[allstr line]; | 
| 122 | 
  | 
  | 
 end | 
| 123 | 
  | 
  | 
end | 
| 124 | 
  | 
  | 
 | 
| 125 | 
  | 
  | 
% Close meta file | 
| 126 | 
  | 
  | 
fclose(fid); | 
| 127 | 
  | 
  | 
 | 
| 128 | 
  | 
  | 
% Strip out comments of form /* ... */ | 
| 129 | 
  | 
  | 
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); | 
| 130 | 
  | 
  | 
if size(ind1) ~= size(ind2) | 
| 131 | 
  | 
  | 
 error('The /* ... */ comments are not properly paired') | 
| 132 | 
  | 
  | 
end | 
| 133 | 
  | 
  | 
while size(ind1,2) > 0 | 
| 134 | 
  | 
  | 
 allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)]; | 
| 135 | 
  | 
  | 
 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); | 
| 136 | 
  | 
  | 
end | 
| 137 | 
  | 
  | 
 | 
| 138 | 
  | 
  | 
% This is a kludge to catch whether the meta-file is of the | 
| 139 | 
  | 
  | 
% old or new type. nrecords does not exist in the old type. | 
| 140 | 
  | 
  | 
nrecords = -987; | 
| 141 | 
  | 
  | 
 | 
| 142 | 
  | 
  | 
% Everything in lower case | 
| 143 | 
  | 
  | 
allstr=lower(allstr); | 
| 144 | 
  | 
  | 
 | 
| 145 | 
  | 
  | 
% Fix the unfortunate choice of 'format' | 
| 146 | 
  | 
  | 
allstr=strrep(allstr,'format','dataprec'); | 
| 147 | 
  | 
  | 
 | 
| 148 | 
  | 
  | 
% Evaluate meta information | 
| 149 | 
  | 
  | 
eval(allstr); | 
| 150 | 
  | 
  | 
 | 
| 151 | 
  | 
  | 
N=reshape( dimlist , 3 , prod(size(dimlist))/3 ); | 
| 152 | 
  | 
  | 
 | 
| 153 | 
  | 
  | 
if nrecords == -987 | 
| 154 | 
  | 
  | 
% This is the old 'meta' method that used sequential access | 
| 155 | 
  | 
  | 
 | 
| 156 | 
  | 
  | 
A=allstr; | 
| 157 | 
  | 
  | 
% Open data file | 
| 158 | 
  | 
  | 
fid=fopen(dname,'r',ieee); | 
| 159 | 
  | 
  | 
 | 
| 160 | 
  | 
  | 
% Read record size in bytes | 
| 161 | 
  | 
  | 
recsz=fread(fid,1,'uint32'); | 
| 162 | 
  | 
  | 
ldims=N(3,:)-N(2,:)+1; | 
| 163 | 
  | 
  | 
numels=prod(ldims); | 
| 164 | 
  | 
  | 
 | 
| 165 | 
  | 
  | 
rat=recsz/numels; | 
| 166 | 
  | 
  | 
if rat == 4 | 
| 167 | 
  | 
  | 
 A=fread(fid,numels,'real*4'); | 
| 168 | 
  | 
  | 
elseif rat == 8 | 
| 169 | 
  | 
  | 
 A=fread(fid,numels,'real*8'); | 
| 170 | 
  | 
  | 
else | 
| 171 | 
  | 
  | 
 sprintf(' Implied size in meta-file = %d', numels ) | 
| 172 | 
  | 
  | 
 sprintf(' Record size in data-file = %d', recsz ) | 
| 173 | 
  | 
  | 
 error('Ratio between record size and size in meta-file inconsistent') | 
| 174 | 
  | 
  | 
end | 
| 175 | 
  | 
  | 
 | 
| 176 | 
  | 
  | 
erecsz=fread(fid,1,'uint32'); | 
| 177 | 
  | 
  | 
if erecsz ~= recsz | 
| 178 | 
  | 
  | 
 sprintf('WARNING: Record sizes at beginning and end of file are inconsistent') | 
| 179 | 
  | 
  | 
end | 
| 180 | 
  | 
  | 
 | 
| 181 | 
  | 
  | 
fclose(fid); | 
| 182 | 
  | 
  | 
 | 
| 183 | 
  | 
  | 
A=reshape(A,ldims); | 
| 184 | 
  | 
  | 
 | 
| 185 | 
  | 
  | 
else | 
| 186 | 
  | 
  | 
% This is the new MDS format that uses direct access | 
| 187 | 
  | 
  | 
 | 
| 188 | 
  | 
  | 
 ldims=N(3,:)-N(2,:)+1; | 
| 189 | 
  | 
  | 
 if dataprec == 'float32' | 
| 190 | 
  | 
  | 
  A=myrdda(dname,ldims,1,'real*4',ieee); | 
| 191 | 
  | 
  | 
 elseif dataprec == 'float64' | 
| 192 | 
  | 
  | 
  A=myrdda(dname,ldims,1,'real*8',ieee); | 
| 193 | 
  | 
  | 
 else | 
| 194 | 
  | 
  | 
  error(['Unrecognized format in meta-file = ' format]); | 
| 195 | 
  | 
  | 
 end | 
| 196 | 
  | 
  | 
 | 
| 197 | 
  | 
  | 
end | 
| 198 | 
  | 
  | 
 | 
| 199 | 
  | 
  | 
%------------------------------------------------------------------------------- | 
| 200 | 
  | 
  | 
 | 
| 201 | 
  | 
  | 
% result = RDDA( file, dim, irec [options] ) | 
| 202 | 
  | 
  | 
% | 
| 203 | 
  | 
  | 
% This routine reads the irec'th record of shape 'dim' from the | 
| 204 | 
  | 
  | 
% direct-access binary file (float or double precision) 'file'. | 
| 205 | 
  | 
  | 
% | 
| 206 | 
  | 
  | 
% Required arguments: | 
| 207 | 
  | 
  | 
% | 
| 208 | 
  | 
  | 
%   file  - string  - name of file to read from | 
| 209 | 
  | 
  | 
%   dim   - vector  - dimensions of the file records and the resulting array | 
| 210 | 
  | 
  | 
%   irec  - integer - record number in file in which to write data | 
| 211 | 
  | 
  | 
% | 
| 212 | 
  | 
  | 
% Optional arguments (must appear after the required arguments): | 
| 213 | 
  | 
  | 
%   prec  - string  - precision of storage in file. Default = 'real*8'. | 
| 214 | 
  | 
  | 
%   ieee  - string  - IEEE bit-wise representation in file. Default = 'b'. | 
| 215 | 
  | 
  | 
% | 
| 216 | 
  | 
  | 
% 'prec' may take the values: | 
| 217 | 
  | 
  | 
%       'real*4' - floating point, 32 bits. | 
| 218 | 
  | 
  | 
%       'real*8' - floating point, 64 bits - the efault. | 
| 219 | 
  | 
  | 
% | 
| 220 | 
  | 
  | 
% 'ieee' may take values: | 
| 221 | 
  | 
  | 
%    'ieee-be'     or 'b' - IEEE floating point with big-endian | 
| 222 | 
  | 
  | 
%                           byte ordering - the default | 
| 223 | 
  | 
  | 
%    'ieee-le'     or 'l' - IEEE floating point with little-endian | 
| 224 | 
  | 
  | 
%                           byte ordering | 
| 225 | 
  | 
  | 
%    'native'      or 'n' - local machine format | 
| 226 | 
  | 
  | 
%    'cray'        or 'c' - Cray floating point with big-endian | 
| 227 | 
  | 
  | 
%                           byte ordering | 
| 228 | 
  | 
  | 
%    'ieee-le.l64' or 'a' - IEEE floating point with little-endian | 
| 229 | 
  | 
  | 
%                           byte ordering and 64 bit long data type | 
| 230 | 
  | 
  | 
%    'ieee-be.l64' or 's' - IEEE floating point with big-endian byte | 
| 231 | 
  | 
  | 
%                           ordering and 64 bit long data type. | 
| 232 | 
  | 
  | 
% | 
| 233 | 
  | 
  | 
% eg.   T=rdda('t.data',[64 64 32],1); | 
| 234 | 
  | 
  | 
%       T=rdda('t.data',[256],4,'real*4'); | 
| 235 | 
  | 
  | 
%       T=rdda('t.data',[128 64],2,'real*4','b'); | 
| 236 | 
  | 
  | 
function [arr] = myrdda(file,N,k,varargin) | 
| 237 | 
  | 
  | 
 | 
| 238 | 
  | 
  | 
% Defaults | 
| 239 | 
  | 
  | 
WORDLENGTH=8; | 
| 240 | 
  | 
  | 
rtype='real*8'; | 
| 241 | 
  | 
  | 
ieee='b'; | 
| 242 | 
  | 
  | 
 | 
| 243 | 
  | 
  | 
% Check optional arguments | 
| 244 | 
  | 
  | 
args=char(varargin); | 
| 245 | 
  | 
  | 
while (size(args,1) > 0) | 
| 246 | 
  | 
  | 
 if deblank(args(1,:)) == 'real*4' | 
| 247 | 
  | 
  | 
  WORDLENGTH=4; | 
| 248 | 
  | 
  | 
  rtype='real*4'; | 
| 249 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'real*8' | 
| 250 | 
  | 
  | 
  WORDLENGTH=8; | 
| 251 | 
  | 
  | 
  rtype='real*8'; | 
| 252 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' | 
| 253 | 
  | 
  | 
  ieee='n'; | 
| 254 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' | 
| 255 | 
  | 
  | 
  ieee='l'; | 
| 256 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' | 
| 257 | 
  | 
  | 
  ieee='b'; | 
| 258 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' | 
| 259 | 
  | 
  | 
  ieee='c'; | 
| 260 | 
  | 
  | 
 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' | 
| 261 | 
  | 
  | 
  ieee='a'; | 
| 262 | 
  | 
  | 
 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' | 
| 263 | 
  | 
  | 
  ieee='s'; | 
| 264 | 
  | 
  | 
 else | 
| 265 | 
  | 
  | 
  error(['Optional argument ' args(1,:) ' is unknown']) | 
| 266 | 
  | 
  | 
 end | 
| 267 | 
  | 
  | 
 args=args(2:end,:); | 
| 268 | 
  | 
  | 
end | 
| 269 | 
  | 
  | 
 | 
| 270 | 
  | 
  | 
nnn=prod(N); | 
| 271 | 
  | 
  | 
 | 
| 272 | 
  | 
  | 
[fid mess]=fopen(file,'r',ieee); | 
| 273 | 
  | 
  | 
if fid == -1 | 
| 274 | 
  | 
  | 
 error('Error while opening file:\n%s',mess) | 
| 275 | 
  | 
  | 
end | 
| 276 | 
  | 
  | 
st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof'); | 
| 277 | 
  | 
  | 
if st ~= 0 | 
| 278 | 
  | 
  | 
 mess=ferror(fid); | 
| 279 | 
  | 
  | 
 error('There was an error while positioning the file pointer:\n%s',mess) | 
| 280 | 
  | 
  | 
end | 
| 281 | 
  | 
  | 
[arr count]=fread(fid,nnn,rtype); | 
| 282 | 
  | 
  | 
if count ~= nnn | 
| 283 | 
  | 
  | 
 error('Not enough data was available to be read: off EOF?') | 
| 284 | 
  | 
  | 
end | 
| 285 | 
  | 
  | 
st=fclose(fid); | 
| 286 | 
  | 
  | 
arr=reshape(arr,N); |