 function sr=makesupercell(s,a)

#   usage:  sr=makesupercell(s,a)
#
#      creates supercell based on structure s
#
#       s     input structure 
#       a     new brave lattice in the basis of old lattice vectors
#             (rowwise), works only with non-centered WIEN structures
#       
#   example:  sr=makesupercell(s,[1 0 0; 0 1 0; 0 0 2]) 
#

     if (s.lattic == "H" | s.lattic == "P")

      a1max=floor(sum(a(1:3,1))+1);
      a2max=floor(sum(a(1:3,2))+1);
      a3max=floor(sum(a(1:3,3))+1);
   
      a1min=floor(sum(a(1:3,1))-1);
      a2min=floor(sum(a(1:3,2))-1);
      a3min=floor(sum(a(1:3,3))-1);


      if (a1min >= 0) 
         a1min=-1;
      endif
      if (a2min >= 0) 
         a2min=-1;
      endif
      if (a3min >= 0) 
         a3min=-1;
      endif
   
      if (a1max <= 0) 
         a1max=1;
      endif
      if (a2max <= 0) 
         a2max=1;
      endif
      if (a3max <= 0) 
         a3max=1;
      endif
   
      inva=inv(a);
   
      sr.nat=0;

      for i=1:s.nat

          for i1=a1min:a1max
              for i2=a2min:a2max
                  for i3=a3min:a3max

                         poso(1:3)=s.pos(i,1:3)+[i1 i2 i3];              
                         posn(1:3)=poso*inva;

                         if ((posn <= 0.99999999) & (posn >= -0.00001) )
                            jest=0;  

                            for j=1:sr.nat

                                vec=sr.pos(j,1:3)-posn(1:3);
                                dd=sqrt(vec*vec');
                                
                                if (dd < 0.000001) 
                                   jest=1;
                                   break
                                endif
                            end
                            if (!jest)
                               sr=copyatom2(s,sr,i,0,posn);
                            endif
                         endif
   
                  end
              end
          end

      end

      sr.brlat=a*s.brlat;
      sr.lat2car=sr.brlat;

      sr.a(1)=sqrt(sr.brlat(1,1:3)*sr.brlat(1,1:3)');
      sr.a(2)=sqrt(sr.brlat(2,1:3)*sr.brlat(2,1:3)');
      sr.a(3)=sqrt(sr.brlat(3,1:3)*sr.brlat(3,1:3)');

      sr.alpha(1)=180.0d0*acos((sr.brlat(2,1:3)*sr.brlat(3,1:3)')/(sr.a(2)*sr.a(3)))/pi;
      sr.alpha(2)=180.0d0*acos((sr.brlat(1,1:3)*sr.brlat(3,1:3)')/(sr.a(1)*sr.a(3)))/pi;
      sr.alpha(3)=180.0d0*acos((sr.brlat(2,1:3)*sr.brlat(1,1:3)')/(sr.a(2)*sr.a(1)))/pi;

      sr.lattic='P';

     else
        printf("makesupercell:  remove centering with makeprimitive or makeconventional\n")
        sr=s;        
     endif
     
 end

         
