function []=write_meta(myFile,myDim,prec,varargin);

%Write a generic metafile 
%Input
%
%       myFile			name of the .meta file, with full path; if the name does not end with .meta, extension added
%       myDim			vector of dimensions (e.g. [90 90 50])
%       prec			precission of the data either as 'real*n' or 'float*xx' or n or xx, where n=4/8 and xx=32/64
%	fldList(optional)	list of field names (cell array)
%	nRec(optional)		Number of records in the data file


if nargin>3; fldList=varargin{1}; else; fldList=[]; end;
if nargin>4; nRec=varargin{2};    else; nRec=[];    end;

%Dimensions
nDim = max(size(myDim));
if(nDim==3 & myDim(3)==1); myDim=myDim(1:2); end;
if(nDim==1); myDim=[1 1 myDim]; end;
nDim = max(size(myDim));

%Records
if (isempty(nRec)); 
   if (isempty(fldList)); 
      nRec = 1;
   else;
      nRec = size(fldList,2);
   end;
end;

%Precission
if(isa(prec,'char'));
   if(strcmp(prec,'real*4')|strcmp(prec,'float32'));
      myPrec = 32; fPrec = '4';
   elseif(strcmp(prec,'real*8')|strcmp(prec,'float64'));
      myPrec = 64; fPrec = '8';
   else;
      disp('Data precission not recognized (real*4 or real*8 or float32 or float64 or 4 or 8 or 32 or 64) -> No meta file was written');
      return;
   end;
elseif(isa(prec,'double'));
   if(prec==4|prec==32);
      myPrec = 32; fPrec = '4';
   elseif(prec==8|prec==64);
      myPrec = 64; fPrec = '8';
   else;
      disp('Data precission not recognized (real*4 or real*8 or float32 or float64 or 4 or 8 or 32 or 64) -> No meta file was written');
      return;
   end;
end;

%Filename
if(~strcmp(myFile(end-4:end),'.meta'));
   disp('write_meta: WARNING! Filename does not end with .meta; adding extension to Filename');
   myFile=[myFile '.meta'];
end;

%keyboard
fid=fopen(myFile,'wt');
%%
fprintf(fid,' nDims = [   %i ];\n',min(nDim,3));
fprintf(fid,' dimList = [\n');
fprintf(fid,' %5i, %5i, %5i,\n',myDim(1),1,myDim(1));
fprintf(fid,' %5i, %5i, %5i,\n',myDim(2),1,myDim(2));
if nDim>2; fprintf(fid,' %5i, %5i, %5i,\n',myDim(3),1,myDim(3)); end;
fprintf(fid,' ];\n');
fprintf(fid,' dataprec = [ ''float%2i'' ];\n',myPrec);
fprintf(fid,' nrecords = [ %5i ];\n',nRec);
if ~isempty(fldList);
  nFlds=length(fldList);
  fprintf(fid,' nFlds = [   %i ];\n',nFlds);
  fprintf(fid,' fldList = {\n');
  txt=' '; for ii=1:length(fldList); txt=[txt '''' fldList{ii} '''' ' ']; end;
  fprintf(fid,[txt '\n']);
  fprintf(fid,' };\n');
end;

%%
fclose(fid);

