function [AA] = rdmeta(fname,varargin)
%
% Read MITgcmUV Meta/Data files
%
% A = RDMETA(FNAME) reads data described by meta/data file format.
% FNAME is a string containing the "head" of the file names.
%
% eg. To load the meta-data files
%     T.0000002880.p0000.t0000.meta, T.0000002880.p0000.t0000.data
%     T.0000002880.p0001.t0000.meta, T.0000002880.p0001.t0000.data
%     T.0000002880.p0002.t0000.meta, T.0000002880.p0002.t0000.data
%     T.0000002880.p0003.t0000.meta, T.0000002880.p0003.t0000.data
% use
%    >> A=rdmeta('T.0000002880');
%
% A = RDMETA(FNAME,MACHINEFORMAT) allows the machine format to be specified
% which MACHINEFORMAT is on of the following strings:
%
%   'native'      or 'n' - local machine format - the default
%   'ieee-le'     or 'l' - IEEE floating point with little-endian
%                          byte ordering
%   'ieee-be'     or 'b' - IEEE floating point with big-endian
%                          byte ordering
%   'vaxd'        or 'd' - VAX D floating point and VAX ordering
%   'vaxg'        or 'g' - VAX G floating point and VAX ordering
%   'cray'        or 'c' - Cray floating point with big-endian
%                          byte ordering
%   'ieee-le.l64' or 'a' - IEEE floating point with little-endian
%                          byte ordering and 64 bit long data type
%   'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
%                          ordering and 64 bit long data type.
%

% Default options
ieee='n';

% Check optional arguments
args=char(varargin);
while (size(args,1) > 0)
 if deblank(args(1,:)) == 'n' | deblank(args(1,:)) == 'native'
  ieee='n';
 elseif deblank(args(1,:)) == 'l' | deblank(args(1,:)) == 'ieee-le'
  ieee='l';
 elseif deblank(args(1,:)) == 'b' | deblank(args(1,:)) == 'ieee-be'
  ieee='b';
 elseif deblank(args(1,:)) == 'c' | deblank(args(1,:)) == 'cray'
  ieee='c';
 elseif deblank(args(1,:)) == 'a' | deblank(args(1,:)) == 'ieee-le.l64'
  ieee='a';
 elseif deblank(args(1,:)) == 's' | deblank(args(1,:)) == 'ieee-be.l64'
  ieee='s';
 else
  sprintf(['Optional argument ' args(1,:) ' is unknown'])
  return
 end
 args=args(2:end,:);
end

% Match name of all meta-files
eval(['ls ' fname '*.meta;']);
allfiles=ans;

% Beginning and end of strings
Iend=findstr(allfiles,'.meta')+4;
Ibeg=[1 Iend(1:end-1)+2];

% Loop through allfiles
for j=1:prod(size(Ibeg)),

% Read meta- and data-file
[A,N] = localrdmeta(allfiles(Ibeg(j):Iend(j)),ieee);

bdims=N(1,:);
r0=N(2,:);
rN=N(3,:);
ndims=prod(size(bdims));
if     (ndims == 1)
 AA(r0(1):rN(1))=A;
elseif (ndims == 2)
 AA(r0(1):rN(1),r0(2):rN(2))=A;
elseif (ndims == 3)
 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3))=A;
elseif (ndims == 4)
 AA(r0(1):rN(1),r0(2):rN(2),r0(3):rN(3),r0(4):rN(4))=A;
else
 sprintf('Dimension of data set is larger than currently coded. Sorry!')
 return
end

end

%-------------------------------------------------------------------------------

function [A,N] = localrdmeta(fname,ieee)

mname=fname;
dname=strrep(mname,'.meta','.data');

% Read and interpret Meta file
fid = fopen(mname,'r');
if (fid == -1)
 sprintf(['Fila e' mname ' could not be opened'])
 return
end

% Scan each line of the Meta file
allstr=' ';
keepgoing = 1;
while keepgoing > 0,
 line = fgetl(fid);
 if (line == -1)
  keepgoing=-1;
 else
% Strip out "(PID.TID *.*)" by finding first ")"
  ind=findstr([line ')'],')'); line=line(ind(1)+1:end);
% Remove comments of form //
  line=[line ' //']; ind=findstr(line,'//'); line=line(1:ind(1)-1);
% Add to total string
  allstr=[allstr line];
 end
end

% Close meta file
fclose(fid);

% Strip out comments of form /* ... */
ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
if size(ind1) ~= size(ind2)
 sprintf('The /* ... */ comments are not properly paired')
 return
end
while size(ind1,2) > 0
 allstr=[allstr(1:ind1(1)-1) allstr(ind2(1)+3:end)];
 ind1=findstr(allstr,'/*'); ind2=findstr(allstr,'*/');
end

eval(lower(allstr));

N=reshape( dimlist , 3 , prod(size(dimlist))/3 );

A=allstr;
% Open data file
fid=fopen(dname,'r',ieee);

% Read record size in bytes
recsz=fread(fid,1,'uint32');
ldims=N(3,:)-N(2,:)+1;
numels=prod(ldims);

rat=recsz/numels;
if rat == 4
 A=fread(fid,numels,'real*4');
elseif rat == 8
 A=fread(fid,numels,'real*8');
else
 sprintf('Ratio between record size and size in meta-file inconsistent')
 sprintf(' Implied size in meta-file = %d', numels )
 sprintf(' Record size in data-file = %d', recsz )
 return
end

erecsz=fread(fid,1,'uint32');
if erecsz ~= recsz
 sprintf('WARNING: Record sizes at beginning and end of file are inconsistent')
end

fclose(fid);

A=reshape(A,ldims);



