%% beginning
clear all;
clc;
pathname = 'C:\Users\dell\Desktop\matlab4FLEXPART\';
% anonymous helper function to skip some bytes
skip = @(fid) fread(fid,2,'int32');

nest=0;  
readp=1; % default: do not read release points
calcarea=0;
%
datespath=pathname;
header.path=pathname;
fhd=fopen([header.path 'header']);
%fprintf('\nreading header');
header.nest=0;
%%
% default settings
header.decayconstant=0;

  % start reading if header was opened
if fhd~=-1
  rl=fread(fhd,1,'int32');  
  header.open=1;
  header.ibdate=fread(fhd,1,'int32');
  header.ibtime=fread(fhd,1,'int32');
  pivot_vec=(datevec(date));
  header.simulationstart=datenum([num2str(header.ibdate)  num2str(header.ibtime,'%06d')],'yyyymmddhhMMss');
  header.ver=char(transpose(fread(fhd,13,'char'))); 
  %%
  skip(fhd);
  header.loutstep=fread(fhd,1,'int32');
  header.loutaver=fread(fhd,1,'int32');
  header.loutsample=fread(fhd,1,'int32');
    
  skip(fhd);
  header.outlon0=fread(fhd,1,'float');
  header.outlat0=fread(fhd,1,'float');
  header.numxgrid=fread(fhd,1,'int32');
  header.numygrid=fread(fhd,1,'int32');    
  header.dxout=fread(fhd,1,'float');
  header.dyout=fread(fhd,1,'float',8);
  header.numzgrid=fread(fhd,1,'int32');
  header.outheight=fread(fhd,header.numzgrid,'float');
  header.outheight=cat(1,[0],header.outheight);
  skip(fhd);
  header.jjjjmmdd=fread(fhd,1,'int32');    
  header.hhmmss=fread(fhd,1,'int32',8);    
  header.nspec=fread(fhd,1,'int32')/3;
end
%%