| 1 |
function [AA,iters] = rdmds(fnamearg,varargin) |
| 2 |
% RDMDS Read MITgcmUV meta/data files |
| 3 |
% |
| 4 |
% A = RDMDS(FNAME) |
| 5 |
% A = RDMDS(FNAME,ITER) |
| 6 |
% A = RDMDS(FNAME,[ITER1 ITER2 ...]) |
| 7 |
% A = RDMDS(FNAME,NaN) |
| 8 |
% A = RDMDS(FNAME,Inf) |
| 9 |
% [A,ITS] = RDMDS(FNAME,[...]) |
| 10 |
% A = RDMDS(FNAME,[...],'rec',RECNUM) |
| 11 |
% |
| 12 |
% A = RDMDS(FNAME) reads data described by meta/data file format. |
| 13 |
% FNAME is a string containing the "head" of the file names. |
| 14 |
% |
| 15 |
% eg. To load the meta-data files |
| 16 |
% T.0000002880.000.000.meta, T.0000002880.000.000.data |
| 17 |
% T.0000002880.001.000.meta, T.0000002880.001.000.data |
| 18 |
% T.0000002880.002.000.meta, T.0000002880.002.000.data |
| 19 |
% T.0000002880.003.000.meta, T.0000002880.003.000.data |
| 20 |
% use |
| 21 |
% >> A=rdmds('T.0000002880'); |
| 22 |
% >> size(A) |
| 23 |
% ans = |
| 24 |
% 64 32 5 |
| 25 |
% eg. To load a multiple record file |
| 26 |
% >> A=rdmds('pickup.0000002880'); |
| 27 |
% >> size(A) |
| 28 |
% ans = |
| 29 |
% 64 32 5 61 |
| 30 |
% |
| 31 |
% |
| 32 |
% A = RDMDS(FNAME,ITER) reads data described by meta/data file format. |
| 33 |
% FNAME is a string containing the "head" of the file name excluding the |
| 34 |
% 10-digit iterartion number. |
| 35 |
% ITER is a vector of positive integers that will expand to the 10-digit |
| 36 |
% number in the file name. |
| 37 |
% If ITER=NaN, all iterations will be read. |
| 38 |
% If ITER=Inf, the last (highest) iteration will be read. |
| 39 |
% |
| 40 |
% eg. To repeat above operation |
| 41 |
% >> A=rdmds('T',2880); |
| 42 |
% eg. To read multiple time steps |
| 43 |
% >> A=rdmds('T',[0 1440 2880]); |
| 44 |
% eg. To read all time steps |
| 45 |
% >> [A,ITS]=rdmds('T',NaN); |
| 46 |
% eg. To read the last time step |
| 47 |
% >> [A,IT]=rdmds('T',Inf); |
| 48 |
% Note: this form can not read files with no iteration count in file name. |
| 49 |
% |
| 50 |
% |
| 51 |
% A = RDMDS(FNAME,[...],'rec',RECNUM) reads individual records from |
| 52 |
% multiple record files. |
| 53 |
% |
| 54 |
% eg. To read a single record from a multi-record file |
| 55 |
% >> [A,IT]=rdmds('pickup.ckptA',11); |
| 56 |
% eg. To read several records from a multi-record file |
| 57 |
% >> [A,IT]=rdmds('pickup',Inf,'rec',[1:5 8 12]); |
| 58 |
% |
| 59 |
% |
| 60 |
% A = RDMDS(FNAME,ITER,MACHINEFORMAT) allows the machine format to be |
| 61 |
% A = RDMDS(FNAME,MACHINEFORMAT) |
| 62 |
% specified which MACHINEFORMAT is on of the following strings: |
| 63 |
% 'n' 'l' 'b' 'd' 'g' 'c' 'a' 's' - see FOPEN for more details |
| 64 |
% |
| 65 |
% |
| 66 |
% $Header: /u/gcmpack/MITgcm/utils/matlab/rdmds.m,v 1.17 2004/06/04 17:03:50 adcroft Exp $ |
| 67 |
|
| 68 |
AA=[]; |
| 69 |
iters=[]; |
| 70 |
|
| 71 |
% Default options |
| 72 |
ieee='b'; |
| 73 |
fname=fnamearg; |
| 74 |
userecords=0; |
| 75 |
recnum=[]; |
| 76 |
|
| 77 |
% Check optional arguments |
| 78 |
for ind=1:size(varargin,2); |
| 79 |
arg=varargin{ind}; |
| 80 |
if ischar(arg) |
| 81 |
if strcmp(arg,'n') | strcmp(arg,'native') |
| 82 |
ieee='n'; |
| 83 |
elseif strcmp(arg,'l') | strcmp(arg,'ieee-le') |
| 84 |
ieee='l'; |
| 85 |
elseif strcmp(arg,'b') | strcmp(arg,'ieee-be') |
| 86 |
ieee='b'; |
| 87 |
elseif strcmp(arg,'c') | strcmp(arg,'cray') |
| 88 |
ieee='c'; |
| 89 |
elseif strcmp(arg,'a') | strcmp(arg,'ieee-le.l64') |
| 90 |
ieee='a'; |
| 91 |
elseif strcmp(arg,'s') | strcmp(arg,'ieee-be.l64') |
| 92 |
ieee='s'; |
| 93 |
elseif strcmp(arg,'rec') |
| 94 |
userecords=1; |
| 95 |
else |
| 96 |
error(['Optional argument ' arg ' is unknown']) |
| 97 |
end |
| 98 |
else |
| 99 |
if userecords==1 |
| 100 |
recnum=arg; |
| 101 |
elseif isempty(iters) |
| 102 |
if isnan(arg) |
| 103 |
iters=scanforfiles(fname); |
| 104 |
disp([ sprintf('Reading %i time levels:',size(iters,2)) sprintf(' %i',iters) ]); |
| 105 |
elseif isinf(arg) |
| 106 |
iters=scanforfiles(fname); |
| 107 |
if isempty(iters) |
| 108 |
AA=[];iters=[];return; |
| 109 |
end |
| 110 |
disp([ sprintf('Found %i time levels, reading %i',size(iters,2),iters(end)) ]); |
| 111 |
iters=iters(end); |
| 112 |
% elseif prod(double(arg>=0)) & prod(double(round(arg)==arg)) |
| 113 |
% elseif prod(arg>=0) & prod(round(arg)==arg) |
| 114 |
elseif min(arg)>=0 & isempty(find(round(arg)~=arg)) |
| 115 |
if arg>=9999999999 |
| 116 |
error(sprintf('Argument %i > 9999999999',arg)) |
| 117 |
end |
| 118 |
iters=arg; |
| 119 |
else |
| 120 |
error(sprintf('Argument %i must be a positive integer',arg)) |
| 121 |
end |
| 122 |
else |
| 123 |
error('Multiple iterations should be specified as a vector') |
| 124 |
end |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
if isempty(iters) |
| 129 |
iters=-1; |
| 130 |
end |
| 131 |
|
| 132 |
% Loop over each iteration |
| 133 |
for iter=1:size(iters,2); |
| 134 |
if iters(iter)>=0 |
| 135 |
fname=sprintf('%s.%10.10i',fnamearg,iters(iter)); |
| 136 |
end |
| 137 |
|
| 138 |
% Figure out if there is a path in the filename |
| 139 |
NS=findstr('/',fname); |
| 140 |
if size(NS)>0 |
| 141 |
Dir=fname(1:NS(end)); |
| 142 |
else |
| 143 |
Dir='./'; |
| 144 |
end |
| 145 |
|
| 146 |
% Match name of all meta-files |
| 147 |
allfiles=dir( sprintf('%s*.meta',fname) ); |
| 148 |
|
| 149 |
if size(allfiles,1)==0 |
| 150 |
disp(sprintf('No files match the search: %s*.meta',fname)); |
| 151 |
%allow partial reads% error('No files found.') |
| 152 |
end |
| 153 |
|
| 154 |
% Loop through allfiles |
| 155 |
for j=1:size(allfiles,1); |
| 156 |
|
| 157 |
% Read meta- and data-file |
| 158 |
[A,N] = localrdmds([Dir allfiles(j).name],ieee,recnum); |
| 159 |
|
| 160 |
bdims=N(1,:); |
| 161 |
r0=N(2,:); |
| 162 |
rN=N(3,:); |
| 163 |
ndims=prod(size(bdims)); |
| 164 |
if (ndims == 1) |
| 165 |
AA(r0(1):rN(1),iter)=A; |
| 166 |
elseif (ndims == 2) |
| 167 |
AA(r0(1):rN(1),r0(2):rN(2),iter)=A; |
| 168 |
elseif (ndims == 3) |
| 169 |
AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),iter)=A; |
| 170 |
elseif (ndims == 4) |
| 171 |
AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),iter)=A; |
| 172 |
elseif (ndims == 5) |
| 173 |
AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A; |
| 174 |
else |
| 175 |
error('Dimension of data set is larger than currently coded. Sorry!') |
| 176 |
end |
| 177 |
|
| 178 |
end % files |
| 179 |
end % iterations |
| 180 |
|
| 181 |
%------------------------------------------------------------------------------- |
| 182 |
|
| 183 |
function [A,N] = localrdmds(fname,ieee,recnum) |
| 184 |
|
| 185 |
mname=strrep(fname,' ',''); |
| 186 |
dname=strrep(mname,'.meta','.data'); |
| 187 |
|
| 188 |
% Read and interpret Meta file |
| 189 |
fid = fopen(mname,'r'); |
| 190 |
if (fid == -1) |
| 191 |
error(['File' mname ' could not be opened']) |
| 192 |
end |
| 193 |
|
| 194 |
% Scan each line of the Meta file |
| 195 |
allstr=' '; |
| 196 |
keepgoing = 1; |
| 197 |
while keepgoing > 0, |
| 198 |
line = fgetl(fid); |
| 199 |
if (line == -1) |
| 200 |
keepgoing=-1; |
| 201 |
else |
| 202 |
% Strip out "(PID.TID *.*)" by finding first ")" |
| 203 |
%old ind=findstr([line ')'],')'); line=line(ind(1)+1:end); |
| 204 |
ind=findstr(line,')'); |
| 205 |
if size(ind) ~= 0 |
| 206 |
line=line(ind(1)+1:end); |
| 207 |
end |
| 208 |
% Remove comments of form // |
| 209 |
line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1); |
| 210 |
% Add to total string |
| 211 |
allstr=[allstr line]; |
| 212 |
end |
| 213 |
end |
| 214 |
|
| 215 |
% Close meta file |
| 216 |
fclose(fid); |
| 217 |
|
| 218 |
% Strip out comments of form /* ... */ |
| 219 |
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); |
| 220 |
if size(ind1) ~= size(ind2) |
| 221 |
error('The /* ... */ comments are not properly paired') |
| 222 |
end |
| 223 |
while size(ind1,2) > 0 |
| 224 |
allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)]; |
| 225 |
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/'); |
| 226 |
end |
| 227 |
|
| 228 |
% This is a kludge to catch whether the meta-file is of the |
| 229 |
% old or new type. nrecords does not exist in the old type. |
| 230 |
nrecords = NaN; |
| 231 |
|
| 232 |
% Everything in lower case |
| 233 |
allstr=lower(allstr); |
| 234 |
|
| 235 |
% Fix the unfortunate choice of 'format' |
| 236 |
allstr=strrep(allstr,'format','dataprec'); |
| 237 |
|
| 238 |
% Evaluate meta information |
| 239 |
eval(allstr); |
| 240 |
|
| 241 |
N=reshape( dimlist , 3 , prod(size(dimlist))/3 ); |
| 242 |
if ~isnan(nrecords) & nrecords > 1 & isempty(recnum) |
| 243 |
N=[N,[nrecords 1 nrecords]']; |
| 244 |
elseif ~isempty(recnum) & recnum>nrecords |
| 245 |
error('Requested record number is higher than the number of available records') |
| 246 |
end |
| 247 |
|
| 248 |
if isempty(recnum) |
| 249 |
recnum=1; |
| 250 |
end |
| 251 |
|
| 252 |
if isnan(nrecords) |
| 253 |
% This is the old 'meta' method that used sequential access |
| 254 |
|
| 255 |
A=allstr; |
| 256 |
% Open data file |
| 257 |
fid=fopen(dname,'r',ieee); |
| 258 |
|
| 259 |
% Read record size in bytes |
| 260 |
recsz=fread(fid,1,'uint32'); |
| 261 |
ldims=N(3,:)-N(2,:)+1; |
| 262 |
numels=prod(ldims); |
| 263 |
|
| 264 |
rat=recsz/numels; |
| 265 |
if rat == 4 |
| 266 |
A=fread(fid,numels,'real*4'); |
| 267 |
elseif rat == 8 |
| 268 |
A=fread(fid,numels,'real*8'); |
| 269 |
else |
| 270 |
sprintf(' Implied size in meta-file = %d', numels ) |
| 271 |
sprintf(' Record size in data-file = %d', recsz ) |
| 272 |
error('Ratio between record size and size in meta-file inconsistent') |
| 273 |
end |
| 274 |
|
| 275 |
erecsz=fread(fid,1,'uint32'); |
| 276 |
if erecsz ~= recsz |
| 277 |
sprintf('WARNING: Record sizes at beginning and end of file are inconsistent') |
| 278 |
end |
| 279 |
|
| 280 |
fclose(fid); |
| 281 |
|
| 282 |
A=reshape(A,ldims); |
| 283 |
|
| 284 |
else |
| 285 |
% This is the new MDS format that uses direct access |
| 286 |
|
| 287 |
ldims=N(3,:)-N(2,:)+1; |
| 288 |
for r=1:size(recnum(:),1); |
| 289 |
if dataprec == 'float32' |
| 290 |
A(:,r)=myrdda(dname,ldims,recnum(r),'real*4',ieee); |
| 291 |
elseif dataprec == 'float64' |
| 292 |
A(:,r)=myrdda(dname,ldims,recnum(r),'real*8',ieee); |
| 293 |
else |
| 294 |
error(['Unrecognized format in meta-file = ' format]); |
| 295 |
end |
| 296 |
end |
| 297 |
|
| 298 |
A=reshape(A,[ldims size(recnum(:),1)]); |
| 299 |
if size(recnum(:),1)>1 |
| 300 |
N(1,end+1)=size(recnum(:),1); |
| 301 |
N(2,end)=1; |
| 302 |
N(3,end)=size(recnum(:),1); |
| 303 |
end |
| 304 |
|
| 305 |
end |
| 306 |
|
| 307 |
%------------------------------------------------------------------------------- |
| 308 |
|
| 309 |
% result = RDDA( file, dim, irec [options] ) |
| 310 |
% |
| 311 |
% This routine reads the irec'th record of shape 'dim' from the |
| 312 |
% direct-access binary file (float or double precision) 'file'. |
| 313 |
% |
| 314 |
% Required arguments: |
| 315 |
% |
| 316 |
% file - string - name of file to read from |
| 317 |
% dim - vector - dimensions of the file records and the resulting array |
| 318 |
% irec - integer - record number in file in which to write data |
| 319 |
% |
| 320 |
% Optional arguments (must appear after the required arguments): |
| 321 |
% prec - string - precision of storage in file. Default = 'real*8'. |
| 322 |
% ieee - string - IEEE bit-wise representation in file. Default = 'b'. |
| 323 |
% |
| 324 |
% 'prec' may take the values: |
| 325 |
% 'real*4' - floating point, 32 bits. |
| 326 |
% 'real*8' - floating point, 64 bits - the efault. |
| 327 |
% |
| 328 |
% 'ieee' may take values: |
| 329 |
% 'ieee-be' or 'b' - IEEE floating point with big-endian |
| 330 |
% byte ordering - the default |
| 331 |
% 'ieee-le' or 'l' - IEEE floating point with little-endian |
| 332 |
% byte ordering |
| 333 |
% 'native' or 'n' - local machine format |
| 334 |
% 'cray' or 'c' - Cray floating point with big-endian |
| 335 |
% byte ordering |
| 336 |
% 'ieee-le.l64' or 'a' - IEEE floating point with little-endian |
| 337 |
% byte ordering and 64 bit long data type |
| 338 |
% 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte |
| 339 |
% ordering and 64 bit long data type. |
| 340 |
% |
| 341 |
% eg. T=rdda('t.data',[64 64 32],1); |
| 342 |
% T=rdda('t.data',[256],4,'real*4'); |
| 343 |
% T=rdda('t.data',[128 64],2,'real*4','b'); |
| 344 |
function [arr] = myrdda(file,N,k,varargin) |
| 345 |
|
| 346 |
% Defaults |
| 347 |
WORDLENGTH=8; |
| 348 |
rtype='real*8'; |
| 349 |
ieee='b'; |
| 350 |
|
| 351 |
% Check optional arguments |
| 352 |
args=char(varargin); |
| 353 |
while (size(args,1) > 0) |
| 354 |
if deblank(args(1,:)) == 'real*4' |
| 355 |
WORDLENGTH=4; |
| 356 |
rtype='real*4'; |
| 357 |
elseif deblank(args(1,:)) == 'real*8' |
| 358 |
WORDLENGTH=8; |
| 359 |
rtype='real*8'; |
| 360 |
elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native' |
| 361 |
ieee='n'; |
| 362 |
elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le' |
| 363 |
ieee='l'; |
| 364 |
elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be' |
| 365 |
ieee='b'; |
| 366 |
elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray' |
| 367 |
ieee='c'; |
| 368 |
elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64' |
| 369 |
ieee='a'; |
| 370 |
elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64' |
| 371 |
ieee='s'; |
| 372 |
else |
| 373 |
error(['Optional argument ' args(1,:) ' is unknown']) |
| 374 |
end |
| 375 |
args=args(2:end,:); |
| 376 |
end |
| 377 |
|
| 378 |
nnn=prod(N); |
| 379 |
|
| 380 |
[fid mess]=fopen(file,'r',ieee); |
| 381 |
if fid == -1 |
| 382 |
error('Error while opening file:\n%s',mess) |
| 383 |
end |
| 384 |
st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof'); |
| 385 |
if st ~= 0 |
| 386 |
mess=ferror(fid); |
| 387 |
error('There was an error while positioning the file pointer:\n%s',mess) |
| 388 |
end |
| 389 |
[arr count]=fread(fid,nnn,rtype); |
| 390 |
if count ~= nnn |
| 391 |
error('Not enough data was available to be read: off EOF?') |
| 392 |
end |
| 393 |
st=fclose(fid); |
| 394 |
%arr=reshape(arr,N); |
| 395 |
|
| 396 |
% |
| 397 |
function [iters] = scanforfiles(fname) |
| 398 |
|
| 399 |
iters=[]; |
| 400 |
allfiles=dir([fname '.*.001.001.meta']); |
| 401 |
if isempty(allfiles) |
| 402 |
allfiles=dir([fname '.*.meta']); |
| 403 |
ioff=0; |
| 404 |
else |
| 405 |
ioff=8; |
| 406 |
end |
| 407 |
for k=1:size(allfiles,1); |
| 408 |
hh=allfiles(k).name; |
| 409 |
iters(k)=str2num( hh(end-14-ioff:end-5-ioff) ); |
| 410 |
end |
| 411 |
iters=sort(iters); |