Warning, /verification/tutorial_global_oce_latlon/diags_matlab/rdmds.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
aadd12719a Jean*0001 function [AA,itrs,MM] = rdmds(fnamearg,varargin)
051ee7f715 Jean*0002 % RDMDS Read MITgcmUV meta/data files
0003 %
0004 % A = RDMDS(FNAME)
0005 % A = RDMDS(FNAME,ITER)
0006 % A = RDMDS(FNAME,[ITER1 ITER2 ...])
0007 % A = RDMDS(FNAME,NaN)
0008 % A = RDMDS(FNAME,Inf)
aadd12719a Jean*0009 % [A,ITS,M] = RDMDS(FNAME,[...])
051ee7f715 Jean*0010 % A = RDMDS(FNAME,[...],'rec',RECNUM)
0011 %
0012 % A = RDMDS(FNAME) reads data described by meta/data file format.
0013 % FNAME is a string containing the "head" of the file names.
aadd12719a Jean*0014 %
051ee7f715 Jean*0015 % eg. To load the meta-data files
0016 % T.0000002880.000.000.meta, T.0000002880.000.000.data
0017 % T.0000002880.001.000.meta, T.0000002880.001.000.data
0018 % T.0000002880.002.000.meta, T.0000002880.002.000.data
0019 % T.0000002880.003.000.meta, T.0000002880.003.000.data
0020 % use
0021 % >> A=rdmds('T.0000002880');
0022 % >> size(A)
0023 % ans =
0024 % 64 32 5
0025 % eg. To load a multiple record file
0026 % >> A=rdmds('pickup.0000002880');
0027 % >> size(A)
0028 % ans =
0029 % 64 32 5 61
aadd12719a Jean*0030 %
0031 %
051ee7f715 Jean*0032 % A = RDMDS(FNAME,ITER) reads data described by meta/data file format.
0033 % FNAME is a string containing the "head" of the file name excluding the
0034 % 10-digit iterartion number.
0035 % ITER is a vector of positive integers that will expand to the 10-digit
0036 % number in the file name.
0037 % If ITER=NaN, all iterations will be read.
0038 % If ITER=Inf, the last (highest) iteration will be read.
0039 %
0040 % eg. To repeat above operation
0041 % >> A=rdmds('T',2880);
0042 % eg. To read multiple time steps
0043 % >> A=rdmds('T',[0 1440 2880]);
0044 % eg. To read all time steps
0045 % >> [A,ITS]=rdmds('T',NaN);
0046 % eg. To read the last time step
0047 % >> [A,IT]=rdmds('T',Inf);
0048 % Note: this form can not read files with no iteration count in file name.
0049 %
0050 %
0051 % A = RDMDS(FNAME,[...],'rec',RECNUM) reads individual records from
0052 % multiple record files.
aadd12719a Jean*0053 %
051ee7f715 Jean*0054 % eg. To read a single record from a multi-record file
0055 % >> [A,IT]=rdmds('pickup.ckptA',11);
0056 % eg. To read several records from a multi-record file
0057 % >> [A,IT]=rdmds('pickup',Inf,'rec',[1:5 8 12]);
0058 %
aadd12719a Jean*0059 %
051ee7f715 Jean*0060 % A = RDMDS(FNAME,ITER,MACHINEFORMAT) allows the machine format to be
0061 % A = RDMDS(FNAME,MACHINEFORMAT)
0062 % specified which MACHINEFORMAT is on of the following strings:
0063 % 'n' 'l' 'b' 'd' 'g' 'c' 'a' 's' - see FOPEN for more details
0064 %
0065
0066 AA=[];
aadd12719a Jean*0067 itrs=[];
0068 MM=[];
051ee7f715 Jean*0069
0070 % Default options
0071 ieee='b';
0072 fname=fnamearg;
0073 userecords=0;
0074 recnum=[];
0075
0076 % Check optional arguments
0077 for ind=1:size(varargin,2);
0078 arg=varargin{ind};
0079 if ischar(arg)
0080 if strcmp(arg,'n') | strcmp(arg,'native')
0081 ieee='n';
0082 elseif strcmp(arg,'l') | strcmp(arg,'ieee-le')
0083 ieee='l';
0084 elseif strcmp(arg,'b') | strcmp(arg,'ieee-be')
0085 ieee='b';
0086 elseif strcmp(arg,'c') | strcmp(arg,'cray')
0087 ieee='c';
0088 elseif strcmp(arg,'a') | strcmp(arg,'ieee-le.l64')
0089 ieee='a';
0090 elseif strcmp(arg,'s') | strcmp(arg,'ieee-be.l64')
0091 ieee='s';
0092 elseif strcmp(arg,'rec')
0093 userecords=1;
0094 else
0095 error(['Optional argument ' arg ' is unknown'])
0096 end
0097 else
0098 if userecords==1
0099 recnum=arg;
aadd12719a Jean*0100 elseif isempty(itrs)
051ee7f715 Jean*0101 if isnan(arg)
aadd12719a Jean*0102 itrs=scanforfiles(fname);
0103 disp([ sprintf('Reading %i time levels:',size(itrs,2)) sprintf(' %i',itrs) ]);
051ee7f715 Jean*0104 elseif isinf(arg)
aadd12719a Jean*0105 itrs=scanforfiles(fname);
0106 if isempty(itrs)
0107 AA=[];itrs=[];return;
051ee7f715 Jean*0108 end
aadd12719a Jean*0109 disp([ sprintf('Found %i time levels, reading %i',size(itrs,2),itrs(end)) ]);
0110 itrs=itrs(end);
051ee7f715 Jean*0111 % elseif prod(double(arg>=0)) & prod(double(round(arg)==arg))
0112 % elseif prod(arg>=0) & prod(round(arg)==arg)
0113 elseif min(arg)>=0 & isempty(find(round(arg)~=arg))
0114 if arg>=9999999999
0115 error(sprintf('Argument %i > 9999999999',arg))
0116 end
aadd12719a Jean*0117 itrs=arg;
051ee7f715 Jean*0118 else
0119 error(sprintf('Argument %i must be a positive integer',arg))
0120 end
0121 else
0122 error('Multiple iterations should be specified as a vector')
0123 end
0124 end
0125 end
0126
aadd12719a Jean*0127 if isempty(itrs)
0128 itrs=-1;
051ee7f715 Jean*0129 end
0130
0131 % Loop over each iteration
aadd12719a Jean*0132 for iter=1:size(itrs,2);
0133 if itrs(iter)>=0
0134 fname=sprintf('%s.%10.10i',fnamearg,itrs(iter));
0135 end
051ee7f715 Jean*0136
0137 % Figure out if there is a path in the filename
aadd12719a Jean*0138 NS=findstr('/',fname);
0139 if size(NS)>0
0140 Dir=fname(1:NS(end));
0141 else
0142 Dir='./';
0143 end
051ee7f715 Jean*0144
0145 % Match name of all meta-files
aadd12719a Jean*0146 allfiles=dir( sprintf('%s*.meta',fname) );
051ee7f715 Jean*0147
aadd12719a Jean*0148 if size(allfiles,1)==0
0149 disp(sprintf('No files match the search: %s*.meta',fname));
0150 %allow partial reads% error('No files found.')
0151 end
051ee7f715 Jean*0152
0153 % Loop through allfiles
aadd12719a Jean*0154 for j=1:size(allfiles,1);
051ee7f715 Jean*0155
0156 % Read meta- and data-file
aadd12719a Jean*0157 [A,N,M,mG] = localrdmds([Dir allfiles(j).name],ieee,recnum);
0158
0159 %- Merge local Meta file content (M) to MM string:
0160 if j > 0, %- to comment out this block: "if j < 0" (since j is always > 0)
0161 ii=findstr(M,' timeStepNumber');
0162 if isempty(ii), ii1=0; ii2=0;
0163 else ii1=ii; ii2=ii+min(findstr(M(1+ii:end),'];')); end
0164 ii=findstr(M,' timeInterval');
0165 if isempty(ii), jj1=0; jj2=0;
0166 else jj1=ii; jj2=ii+min(findstr(M(1+ii:end),'];')); end
0167 if iter==1 & j==1,
0168 MM=M; ind1=0; ind2=0; is1=ii1; js1=jj1; M3='';
0169 if ii1*jj1 > 0,
0170 %ind1=min(ii1,jj1); ind2=max(ii2,jj2);
0171 %if ii1 < jj1, ii3=ii2+1; jj3=jj1-1;
0172 %else ii3=jj2+1; jj3=ii1-1; end
0173 order=sort([ii1 ii2 jj1 jj2]);
0174 ind1=order(1); ii3=order(2)+1; jj3=order(3)-1; ind2=order(4);
0175 M2=M(ii1:ii2); M4=M(jj1:jj2); M3=M(ii3:jj3);
0176 elseif ii1 > 0,
0177 ind1=ii1; ind2=ii2;
0178 M2=M(ii1:ii2); M4='';
0179 elseif jj1 > 0,
0180 ind1=jj1; ind2=jj2;
0181 M4=M(jj1:jj2); M2='';
0182 end
0183 M5=M(1+ind2:end);
0184 %fprintf(' ii1,ii2 = %i %i ; jj1,jj2= %i %i ;', ii1,ii2, jj1,jj2);
0185 %fprintf(' ii3,jj3= %i %i ; ind1,ind2= %i %i\n',ii3,jj3,ind1,ind2);
0186 %fprintf('M(1:ind1)=%s<\n',M(1:ind1));
0187 %fprintf(' M2=%s<\n',M2);
0188 %fprintf(' M3=%s<\n',M3);
0189 %fprintf(' M4=%s<\n',M4);
0190 %fprintf(' M5=%s<\n',M5);
0191 else
0192 if ii1*jj1 > 0,
0193 order=sort([ii1 ii2 jj1 jj2]);
0194 ind=order(1); ii3=order(2)+1; jj3=order(3)-1; ind2=order(4);
0195 else ind=max(ii1,jj1); ind2=ii2+jj2; end
0196 compar=(ind == ind1); ii=0;
0197 if compar & ind1 == 0, ii=1; compar=strcmp (MM,M); end
0198 if compar & ind1 > 0, ii=2; compar=strncmp(MM,M,ind1) ; end
0199 if compar & ind1 > 0, ii=3; compar=strcmp(M5,M(1+ind2:end)); end
0200 if compar & ii1*jj1 > 0, ii=4; compar=strcmp(M3,M(ii3:jj3)); end
0201 if ~compar,
0202 fprintf('WARNING: Meta file (%s) is different (%i) from 1rst one:\n', ...
0203 allfiles(j).name,ii);
0204 fprintf(' it=%i :MM:%s\n',itrs(1),MM);
0205 fprintf(' it=%i :M :%s\n\n',itrs(iter),M);
0206 elseif ind1 > 0,
0207 if ii1 > 0,
0208 Mj=M(ii1:ii2); ii=findstr(Mj,'['); Mj=Mj(1+ii:end);
0209 % add it-number from Mj to M2 (if different):
0210 if isempty(findstr(M2,Mj)), M2=[deblank(M2(1:end-1)),Mj]; end
0211 end
0212 if jj1 > 0,
0213 Mj=M(jj1:jj2); ii=findstr(Mj,'['); Mj=Mj(1+ii:end);
0214 % add time interval from Mj to M4 (if different):
0215 if isempty(findstr(M4,Mj)), M4=[deblank(M4(1:end-1)),' ;',Mj]; end
0216 end
0217 end
0218 end
0219 % save modifications:
0220 if ind1>0 & j==size(allfiles,1) & iter==size(itrs,2),
0221 if ii1 < jj1, MM=[MM(1:ind1-1),M2,M3,M4,M5];
0222 else MM=[MM(1:ind1-1),M4,M3,M2,M5]; end
0223 end
0224 end
051ee7f715 Jean*0225
aadd12719a Jean*0226 %- put local data file content in global array AA:
0227 bdims=N(1,:);
0228 r0=N(2,:);
0229 rN=N(3,:);
0230 ndims=prod(size(bdims));
0231 if j==1 & iter==1, AA=zeros([bdims size(itrs,2)]); end
0232 if mG(1)==0 & mG(2)==1,
0233 if (ndims == 1)
0234 AA(r0(1):rN(1),iter)=A;
0235 elseif (ndims == 2)
0236 AA(r0(1):rN(1),r0(2):rN(2),iter)=A;
0237 elseif (ndims == 3)
0238 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),iter)=A;
0239 elseif (ndims == 4)
0240 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),iter)=A;
0241 elseif (ndims == 5)
0242 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A;
0243 else
0244 error('Dimension of data set is larger than currently coded. Sorry!')
0245 end
0246 elseif (ndims == 1)
0247 AA(r0(1):rN(1),iter)=A;
0248 else
0249 %- to debug: do simple stransfert (with a loop on 2nd index);
0250 % will need to change this later, to improve efficiency:
0251 for i=0:rN(2)-r0(2),
0252 if (ndims == 2)
0253 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2),iter)=A(:,1+i);
0254 elseif (ndims == 3)
0255 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
0256 r0(3):rN(3),iter)=A(:,1+i,:);
0257 elseif (ndims == 4)
0258 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
0259 r0(3):rN(3),r0(4):rN(4),iter)=A(:,1+i,:,:);
0260 elseif (ndims == 5)
0261 AA(r0(1)+i*mG(1):rN(1)+i*mG(1),r0(2)+i*mG(2), ...
0262 r0(3):rN(3),r0(4):rN(4),r0(5):rN(5),iter)=A(:,1+i,:,:,:);
0263 else
0264 error('Dimension of data set is larger than currently coded. Sorry!')
0265 end
0266 end
0267 end
0268
0269 end % files
051ee7f715 Jean*0270 end % iterations
0271
0272 %-------------------------------------------------------------------------------
0273
aadd12719a Jean*0274 function [A,N,M,map2glob] = localrdmds(fname,ieee,recnum)
051ee7f715 Jean*0275
0276 mname=strrep(fname,' ','');
0277 dname=strrep(mname,'.meta','.data');
0278
aadd12719a Jean*0279 %- set default mapping from tile to global file:
0280 map2glob=[0 1];
0281
051ee7f715 Jean*0282 % Read and interpret Meta file
0283 fid = fopen(mname,'r');
0284 if (fid == -1)
0285 error(['File' mname ' could not be opened'])
0286 end
0287
0288 % Scan each line of the Meta file
0289 allstr=' ';
0290 keepgoing = 1;
0291 while keepgoing > 0,
0292 line = fgetl(fid);
0293 if (line == -1)
0294 keepgoing=-1;
0295 else
0296 % Strip out "(PID.TID *.*)" by finding first ")"
0297 %old ind=findstr([line ')'],')'); line=line(ind(1)+1:end);
0298 ind=findstr(line,')');
0299 if size(ind) ~= 0
0300 line=line(ind(1)+1:end);
0301 end
0302 % Remove comments of form //
aadd12719a Jean*0303 line=[line,' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1);
0304 % Add to total string (without starting & ending blanks)
0305 while line(1:1) == ' ', line=line(2:end); end
0306 if strncmp(line,'map2glob',8), eval(line);
0307 else allstr=[allstr,deblank(line),' '];
0308 end
051ee7f715 Jean*0309 end
0310 end
0311
0312 % Close meta file
0313 fclose(fid);
0314
0315 % Strip out comments of form /* ... */
0316 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
0317 if size(ind1) ~= size(ind2)
0318 error('The /* ... */ comments are not properly paired')
0319 end
0320 while size(ind1,2) > 0
aadd12719a Jean*0321 allstr=[deblank(allstr(1:ind1(1)-1)) allstr(ind2(1)+2:end)];
0322 %allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)];
051ee7f715 Jean*0323 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
0324 end
0325
0326 % This is a kludge to catch whether the meta-file is of the
0327 % old or new type. nrecords does not exist in the old type.
0328 nrecords = NaN;
0329
aadd12719a Jean*0330 %- store the full string for output:
0331 M=strrep(allstr,'format','dataprec');
0332
051ee7f715 Jean*0333 % Everything in lower case
0334 allstr=lower(allstr);
0335
0336 % Fix the unfortunate choice of 'format'
0337 allstr=strrep(allstr,'format','dataprec');
0338
0339 % Evaluate meta information
0340 eval(allstr);
0341
0342 N=reshape( dimlist , 3 , prod(size(dimlist))/3 );
aadd12719a Jean*0343 rep=[' dimList = [ ',sprintf('%i ',N(1,:)),']'];
051ee7f715 Jean*0344 if ~isnan(nrecords) & nrecords > 1 & isempty(recnum)
0345 N=[N,[nrecords 1 nrecords]'];
0346 elseif ~isempty(recnum) & recnum>nrecords
0347 error('Requested record number is higher than the number of available records')
0348 end
0349
aadd12719a Jean*0350 %- make "dimList" shorter (& fit output array size) in output "M":
0351 pat=' dimList = \[(\s*\d+\,?)*\s*\]';
0352 M=regexprep(M,pat,rep);
0353 % and remove double space within sq.brakets:
0354 ind1=findstr(M,'['); ind2=findstr(M,']');
0355 if length(ind1) == length(ind2),
0356 for i=length(ind1):-1:1, if ind1(i) < ind2(i),
0357 M=[M(1:ind1(i)),regexprep(M(ind1(i)+1:ind2(i)-1),'(\s+)',' '),M(ind2(i):end)];
0358 end; end
0359 else error('The [ ... ] brakets are not properly paired')
0360 end
0361
051ee7f715 Jean*0362 if isempty(recnum)
0363 recnum=1;
0364 end
0365
0366 if isnan(nrecords)
0367 % This is the old 'meta' method that used sequential access
0368
0369 A=allstr;
0370 % Open data file
0371 fid=fopen(dname,'r',ieee);
0372
0373 % Read record size in bytes
0374 recsz=fread(fid,1,'uint32');
0375 ldims=N(3,:)-N(2,:)+1;
0376 numels=prod(ldims);
0377
0378 rat=recsz/numels;
0379 if rat == 4
0380 A=fread(fid,numels,'real*4');
0381 elseif rat == 8
0382 A=fread(fid,numels,'real*8');
0383 else
0384 sprintf(' Implied size in meta-file = %d', numels )
0385 sprintf(' Record size in data-file = %d', recsz )
0386 error('Ratio between record size and size in meta-file inconsistent')
0387 end
0388
0389 erecsz=fread(fid,1,'uint32');
0390 if erecsz ~= recsz
0391 sprintf('WARNING: Record sizes at beginning and end of file are inconsistent')
0392 end
0393
0394 fclose(fid);
0395
0396 A=reshape(A,ldims);
0397
0398 else
0399 % This is the new MDS format that uses direct access
0400
0401 ldims=N(3,:)-N(2,:)+1;
0402 for r=1:size(recnum(:),1);
0403 if dataprec == 'float32'
0404 A(:,r)=myrdda(dname,ldims,recnum(r),'real*4',ieee);
0405 elseif dataprec == 'float64'
0406 A(:,r)=myrdda(dname,ldims,recnum(r),'real*8',ieee);
0407 else
0408 error(['Unrecognized format in meta-file = ' format]);
0409 end
0410 end
0411
0412 A=reshape(A,[ldims size(recnum(:),1)]);
0413 if size(recnum(:),1)>1
0414 N(1,end+1)=size(recnum(:),1);
0415 N(2,end)=1;
0416 N(3,end)=size(recnum(:),1);
0417 end
0418
0419 end
0420
0421 %-------------------------------------------------------------------------------
0422
0423 % result = RDDA( file, dim, irec [options] )
0424 %
0425 % This routine reads the irec'th record of shape 'dim' from the
0426 % direct-access binary file (float or double precision) 'file'.
0427 %
0428 % Required arguments:
0429 %
0430 % file - string - name of file to read from
0431 % dim - vector - dimensions of the file records and the resulting array
0432 % irec - integer - record number in file in which to write data
0433 %
0434 % Optional arguments (must appear after the required arguments):
0435 % prec - string - precision of storage in file. Default = 'real*8'.
0436 % ieee - string - IEEE bit-wise representation in file. Default = 'b'.
0437 %
0438 % 'prec' may take the values:
0439 % 'real*4' - floating point, 32 bits.
0440 % 'real*8' - floating point, 64 bits - the efault.
0441 %
0442 % 'ieee' may take values:
0443 % 'ieee-be' or 'b' - IEEE floating point with big-endian
0444 % byte ordering - the default
0445 % 'ieee-le' or 'l' - IEEE floating point with little-endian
0446 % byte ordering
0447 % 'native' or 'n' - local machine format
0448 % 'cray' or 'c' - Cray floating point with big-endian
0449 % byte ordering
0450 % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
0451 % byte ordering and 64 bit long data type
0452 % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
0453 % ordering and 64 bit long data type.
0454 %
0455 % eg. T=rdda('t.data',[64 64 32],1);
0456 % T=rdda('t.data',[256],4,'real*4');
0457 % T=rdda('t.data',[128 64],2,'real*4','b');
0458 function [arr] = myrdda(file,N,k,varargin)
0459
0460 % Defaults
0461 WORDLENGTH=8;
0462 rtype='real*8';
0463 ieee='b';
0464
0465 % Check optional arguments
0466 args=char(varargin);
0467 while (size(args,1) > 0)
0468 if deblank(args(1,:)) == 'real*4'
0469 WORDLENGTH=4;
0470 rtype='real*4';
0471 elseif deblank(args(1,:)) == 'real*8'
0472 WORDLENGTH=8;
0473 rtype='real*8';
0474 elseif deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
0475 ieee='n';
0476 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
0477 ieee='l';
0478 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
0479 ieee='b';
0480 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
0481 ieee='c';
0482 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
0483 ieee='a';
0484 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
0485 ieee='s';
0486 else
0487 error(['Optional argument ' args(1,:) ' is unknown'])
0488 end
0489 args=args(2:end,:);
0490 end
0491
0492 nnn=prod(N);
0493
0494 [fid mess]=fopen(file,'r',ieee);
0495 if fid == -1
0496 error('Error while opening file:\n%s',mess)
0497 end
0498 st=fseek(fid,nnn*(k-1)*WORDLENGTH,'bof');
0499 if st ~= 0
0500 mess=ferror(fid);
0501 error('There was an error while positioning the file pointer:\n%s',mess)
0502 end
0503 [arr count]=fread(fid,nnn,rtype);
0504 if count ~= nnn
0505 error('Not enough data was available to be read: off EOF?')
0506 end
0507 st=fclose(fid);
0508 %arr=reshape(arr,N);
0509
0510 %
aadd12719a Jean*0511 function [itrs] = scanforfiles(fname)
051ee7f715 Jean*0512
aadd12719a Jean*0513 itrs=[];
051ee7f715 Jean*0514 allfiles=dir([fname '.*.001.001.meta']);
0515 if isempty(allfiles)
0516 allfiles=dir([fname '.*.meta']);
0517 ioff=0;
0518 else
0519 ioff=8;
0520 end
0521 for k=1:size(allfiles,1);
0522 hh=allfiles(k).name;
aadd12719a Jean*0523 itrs(k)=str2num( hh(end-14-ioff:end-5-ioff) );
051ee7f715 Jean*0524 end
aadd12719a Jean*0525 itrs=sort(itrs);