function ElectronFullFile = mc_create_ElectronFile(Fname,Thick)
%[t,FR]=mc_layers_multipf('Silicon_30kV_0p25nm_7mrad_2e5',200,1)

MainFileExtension='MC1N';
DataFileExtension='MC1NE';

[filename, pathname, filterindex] = uigetfile(['*.',MainFileExtension], 'Pick an MC1N-file');
if isequal(filename,0) || isequal(pathname,0)
   return
else
   FullFileName=fullfile(pathname, filename);
end

[PATHSTR,NAME,EXT]=fileparts(FullFileName);

st=0;
if length(EXT)==(length(MainFileExtension)+1),
   if strcmpi(EXT(2:end),MainFileExtension),
      st=1;
   end
end

if st,
   ff=dir([NAME,'__f*.',DataFileExtension]);
   if isempty(ff)
      st=2;
   else
      NrDataFiles=length(ff);
   end
end

if st==1, %OK
   
   load(FullFileName,'-mat');
   
   [Xsample,st_sample]=samplpar(sample.EqOrig,sample.Density,sample.E0/1000,sample.maxT/10,sample.Theta0*1000,sample.probeRad/10);
   
   wS=TotalTime;
   wH=fix(TotalTime/(60*60));
   wS=wS-wH*60*60;
   wM=fix(wS/60);
   wS=wS-wM*60;
   sH=sprintf('%02.0f',wH);
   sM=sprintf('%02.0f',wM);
   sS=sprintf('%02.0f',wS);   
   
   s=sprintf('\n');
   s=[s,sprintf('File: %s\n',NAME)];
   s=[s,sprintf('Number of data files: %0.0f\n',NrDataFiles)];
   s=[s,sprintf('Sample: %s;   density=%0.3fg/ml\n',sample.EqRead,sample.Density)];
   s=[s,sprintf('Energy=%3.0fkeV  Th0=%0.1fmrad  PR=%0.2fnm\n',sample.E0/1000,sample.Theta0/1000,sample.probeRad/10)];
   s=[s,sprintf('Elements: %0.0f    L=%0.2f  sel=%0.5f  sin=%0.5f\n',length(sample.Z),Xsample.meanLamb/10,Xsample.SigElT,Xsample.SigInT)];
   normTime=TotalTime/((NMAX*(sample.maxT/10)/(Xsample.meanLamb/10))*sqrt(Xsample.SigElT/Xsample.SigInT));
   s=[s,sprintf('Type of MC: %s  Time: %s (%0.0fs) [norm=%0.5e]\n',NAME(end),[sH,':',sM,':',sS],TotalTime,normTime)];
   s=[s,sprintf('NMAX: %s      Tmax=%0.0fnm\n',strrep(sprintf('%1.0e',NMAX),'e+00','e'),sample.maxT/10)];
   
   disp(s);
   
else
   
   s='Something wrong!';
   disp(s);
   return
   
end

Proc='L';
T=0;
dT=0;
while 1,
%    R=input('Procesing (L), T [nm], dT [nm] (ABORT): ','s');
   R = strcat('L,',num2str(Thick),',',num2str(0.1));
   if strcmpi(R,'ABORT'),
      return
   else
      R = strtrim(R);
      if length(R)>3,
         st_Proc=0;
         if upper(R(1))=='L',
            Proc=upper(R(1));
            R(1)='';
            st_Proc=1;
         end
         if st_Proc,
            p=str2num(R);
            if numel(p)==2,
               T=p(1);
               dT=p(2);
               if (T>0) && (T<=(sample.maxT/10)) && (dT<T),
                  st_Proc=1;
               else
                  st_Proc=0;
               end
            end
         end
      end
   end
   if st_Proc,
      break
   end
end

Th2=21;  % mrad
Th3=171; % mrad

t=[]; FR=[]; Th=[]; BSE=[]; IHIST=[];

if nargout==5,
   stIHIST=1;
else
   stIHIST=0;
end

%__
fnameS = NAME;
tmax = T;
dt = dT;

if exist([fnameS,'.MC1N'],'file')==2,
   load([fnameS,'.MC1N'],'NMAX','maxT','-mat');
else
   beep;
   disp(['File ''',fnameS,'.MC1N',''' does not exist !'])
   return
end

if tmax>maxT,
   disp(' ')
   disp(['Max T used for MONCA was ',num2str(D{end}/10),' nm'])
   return
end


ff=dir([fnameS,'__f*.MC1NE']);
if isempty(ff)
   return
end
filenameS=cell(length(ff),1);
for ii=1:length(ff),
   filenameS{ii}=ff(ii).name;
end
filenameS=sort(filenameS);

Th2=Th2/1000; %rad
Th3=Th3/1000; %rad

t=tmax:-dt:0;
if t(end)==0,
   t(end)=[];
end
t2 = t;
if stIHIST,
   C1=12.3;
   dC=1;
   C2=1;
   sizeIHIST=100;
   %t=1:1:100;
   %x=C1*log(t+dx)+C2;  %[mrad]
   %x=exp((t-C2)/C1)-dC;%[mrad]
   IHIST=cell(1,length(t));
   for ii=1:length(t),
      IHIST{ii}=zeros(1,sizeIHIST);
   end
end

FR=zeros(size(t));

sumNMAX_Electron=0;
BSE=zeros(size(t));

%_____________z viacerych suborov Electron.mat uklada jeden velky___________________
X = [];
Y = [];
Z = [];
Th = [];
Ps = [];
SE = [];
dE = [];

h = waitbar(0,'Preparing Full File .. ')
for NrFile=1:length(filenameS),
    waitbar(NrFile/length(filenameS))  
   load(filenameS{NrFile},'Electron','ii_Electron','NMAX_Electron','-mat');
   if NrFile == 1
       X = Electron.X;
       Y = Electron.Y;
       Z = Electron.Z;
       Th = Electron.Th;
       Ps = Electron.Ps;
       SE = Electron.SE;
       dE = Electron.dE;
   else
       Xi = Electron.X;
       Yi = Electron.Y;
       Zi = Electron.Z;
       Thi = Electron.Th;
       Psi = Electron.Ps;
       SEi = Electron.SE;
       dEi = Electron.dE;
        
       [s1,s2] = size(Xi);
       [s3,s4] = size(X);
       if s2==s4   % velkost matic
          X = [X; Xi];
          Y = [Y; Yi];
          Z = [Z; Zi];
          Th = [Th; Thi];
          Ps = [Ps; Psi];
          SE = [SE; SEi];
          dE = [dE; dEi];
       elseif s2>s4
          ds = s2-s4;
          Xadd = zeros(s3,ds);
          X = [X,Xadd];
          Y = [Y,Xadd];
          Z = [Z,Xadd];
          Th = [Th,Xadd];
          Ps = [Ps,Xadd];
          dE = [dE,Xadd];
          SE = [SE,Xadd];
          
          X = [X; Xi];
          Y = [Y; Yi];
          Z = [Z; Zi];
          Th = [Th; Thi];
          Ps = [Ps; Psi];
          SE = [SE; SEi];
          dE = [dE; dEi];
          
       elseif s4>s2
          ds = s4-s2;
          Xadd = zeros(s1,ds);
          Xi = [Xi,Xadd];
          Yi = [Yi,Xadd];
          Zi = [Zi,Xadd];
          Thi = [Thi,Xadd];
          Psi = [Psi,Xadd];
          dEi = [dEi,Xadd];
          SEi = [SEi,Xadd];
          
          
          X = [X; Xi];
          Y = [Y; Yi];
          Z = [Z; Zi];
          Th = [Th; Thi];
          Ps = [Ps; Psi];
          SE = [SE; SEi];
          dE = [dE; dEi];
       end
       
   end
       
       
       
       
end
ElectronFULL.X = X;
ElectronFULL.Y = Y;
ElectronFULL.Z = Z;
ElectronFULL.Th = Th;
ElectronFULL.Ps = Ps;
ElectronFULL.dE = dE;
ElectronFULL.SE = SE;
       ElectronFullFile = ElectronFULL;
save(Fname,'ElectronFULL')       
       close(h)
% ElectronFULL2.X = ElectronFULL.X(1:500000,:);
% ElectronFULL2.Z = ElectronFULL.Z(1:500000,:);
% ElectronFULL2.Y = ElectronFULL.Y(1:500000,:);
% ElectronFULL2.Th = ElectronFULL.Th(1:500000,:);
% ElectronFULL2.Ps = ElectronFULL.Ps(1:500000,:);
% ElectronFULL2.SE = ElectronFULL.SE(1:500000,:);
% ElectronFULL2.dE = ElectronFULL.dE(1:500000,:);
% ElectronFULL3.X = ElectronFULL.X(500001:end,:);
% ElectronFULL3.Y = ElectronFULL.Y(500001:end,:);
% ElectronFULL3.Z = ElectronFULL.Z(500001:end,:);
% ElectronFULL3.Th = ElectronFULL.Th(500001:end,:);
% ElectronFULL3.Ps = ElectronFULL.Ps(500001:end,:);
% ElectronFULL3.dE = ElectronFULL.dE(500001:end,:);
% ElectronFULL3.SE = ElectronFULL.SE(500001:end,:);
% NAME2 = '30kV_50nm_18dens_2nd';
% NAME1 = '30kV_50nm_18dens_1st';
% save(NAME1,'ElectronFULL2')
% save(NAME2,'ElectronFULL3')       
       
       
       
       
       
   