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