function data=readMPL(name,path_f)
% Read MPL lidar binary data file
% and performe deadtime correction
% New version for MPL with depolarisation
% kmark 30.07.2010 
%path_f=pwd;
%name='200907141900_0.mpl';
%name='201007270300.mpl';
path_f=pwd;
if nargin==0
 [name,path_f]=uigetfile('*.mpl');   % choose the file
end
path_old=pwd;
cd(path_f)
fid=fopen (name,'r');                % open the file and read data as big endian
if fid<0
    data=[];
    cd (path_old);
    return
end    
x=fread(fid,'uint8');       
nrec=ftell(fid);                        
frewind(fid);                        
k=0;
while 1
    k=k+1;
    data.UnitNumer=fread(fid,1,'uint16');
    data.version=fread(fid,1,'uint16')/100;
    data.year(k)=fread(fid,1,'uint16');
    data.month(k)=fread(fid,1,'uint16');
    data.day(k)=fread(fid,1,'uint16');
    data.hour(k)=fread(fid,1,'uint16');
    data.minute(k)=fread(fid,1,'uint16');
    data.sec(k)=fread(fid,1,'uint16');
    data.shots_sum=fread(fid,1,'uint32');
    data.trigger_fre=fread(fid,1,'int32');
    data.E(k)=fread(fid,1,'uint32')/1000;
    data.temp0(k)=fread(fid,1,'uint32')/100;
    data.temp1(k)=fread(fid,1,'uint32')/100;
    data.temp2(k)=fread(fid,1,'uint32')/100;
    data.temp3(k)=fread(fid,1,'uint32')/100;
    data.temp4(k)=fread(fid,1,'uint32')/100;
    data.background(k)=fread(fid,1,'float');
    data.backgroundstd(k)=fread(fid,1,'float');
    data.numchannel(k)=fread(fid,1,'uint16');
    data.numbins(k)=fread(fid,1,'uint32');
    data.bintime(k)=fread(fid,1,'float');
    data.maxaltitude(k)=fread(fid,1,'float');
    data.deastimecorrected(k)=fread(fid,1,'uint16');    
    data.acanscenario(k)=fread(fid,1,'uint16'); 
    data.polarizflag(k)=fread(fid,1,'uint16');    
    data.azimuth(k)=fread(fid,1,'float');
    data.elevation(k)=fread(fid,1,'float');    
    data.compassdegrees(k)=fread(fid,1,'float');
    data.polvoltage0(k)=fread(fid,1,'float');
    data.polvoltage1(k)=fread(fid,1,'float');
    data.polvoltage2(k)=fread(fid,1,'float');
    data.polvoltage3(k)=fread(fid,1,'float');
    data.cloudbase(k)=fread(fid,1,'float');
    data.ADflag(k)=fread(fid,1,'int8');
    data.DataFileVersion(k)=fread(fid,1,'int8');
    data.background2(k)=fread(fid,1,'float');
    data.backgroundstd2(k)=fread(fid,1,'float');
    data.McsMode(k)=fread(fid,1,'ubit8');
    data.Firstdatabin(k)=fread(fid,1,'uint16');
    fread(fid,7,'int8');   % Unused data 
    if data.numchannel==1
        data.signal(k,:)=fread(fid,data.numbins(k),'float');
    end    
    if data.numchannel==2
        data.signalpol(k,:)=fread(fid,data.numbins(k),'float');
        data.signal(k,:)=fread(fid,data.numbins(k),'float');
    end    
    if ftell(fid)==nrec
     break
    end 
end 
fclose(fid);   
cd(path_old)
data.nrec=k;
data.time=data.hour+data.minute/60+data.sec/3600;
jd=day2jd(data.year,data.month,data.day);
data.doy=data.time/24+jd;
c=3.e8;                       % speed of light
s=c*data.bintime(1)/2;          % distance traveled by light in bintime/2
data.z=linspace(s,data.maxaltitude(1),data.numbins(1))/1000;
data.background=deadfun(data.background);
data.backgroundstd=deadfun(data.backgroundstd);
data.background2=deadfun(data.background2);
data.backgroundstd2=deadfun(data.backgroundstd2);

data.signal=deadfun(data.signal);
data.signalpol=deadfun(data.signalpol);


% Modification in new MPL version
nlev=data.numbins(1)-data.Firstdatabin+1;
data.z=data.z(1:nlev);
data.signal=data.signal(:,data.Firstdatabin:end);
data.signalpol=data.signalpol(:,data.Firstdatabin:end);