function [meta]=read_meta(fileName,dirIn);
%Read metafile information and store it in a structure: meta
%The first time step file will be read 
%Input:
%       fileName        Name of the file to be read 
%                       If a .data file is given, its associated .meta will be read 
%                       If file is given without extension (.meta or .data) AND without time step, the first time step in the directory is used
%       dirIn           Directory where the file lives (complete path)

meta=[];
if(strcmp(fileName(end-4:end),'.data'));
   fileName(end-4:end)='.meta';
end;
tmp1=[dirIn fileName];

tmp=dir(tmp1);
if(isempty(tmp));
   tmp1=dir([dirIn filesep fileName '*.meta']);
   if(~isempty(tmp1)); tmp1=tmp1(1).name; tmp1=[dirIn tmp1]; end;
end;
if(isempty(tmp1));error('read_meta: Meta file was not found, check names and directories');end;

%read meta file
fid=fopen(tmp1);
while 1;
    tline = fgetl(fid);
    if ~ischar(tline), break, end
    if isempty(whos('tmp3')); tmp3=tline; else; tmp3=[tmp3 ' ' tline]; end;
end
fclose(fid);

%add meta variables to workspace
eval(tmp3);

%reformat to meta structure
if ~isempty(who('dataprec'));meta.dataprec=dataprec;end;
if ~isempty(who('nDims'));meta.nDims=nDims;end;
if ~isempty(who('nFlds'));meta.nFlds=nFlds;end;
if ~isempty(who('nrecords'));meta.nrecords=nrecords;end;
if ~isempty(who('fldList')); meta.fldList=fldList;end;
if ~isempty(who('dimList')); meta.dimList=dimList;end;
if ~isempty(who('timeInterval')); meta.timeInterval=timeInterval; end;
if ~isempty(who('timeStepNumber'));  meta.timeStepNumber=timeStepNumber; end;

