function AT_2_Elegant(AT_ring,linename)
% This function converts the AT lattice AT_ring in elegant format.
% Written by Simone Liuzzo and Nicola Carmignani, ESRF.
%
% The first input of the function is the AT beamline, the second one is 
% a string with the desired name of the Elegant beamline.
%
% This function works with elements with BetaCode and FamName fields, 
% if your elements have not these fields, you have to modify the function.
%
% Skew elements and other strange elements are not converted.
%
% Number of kicks of the elements depends on the element length, you can
% choose the number of kicks per metre (nkickperL variable)
% 
% To have a good RF cavity, you have to write the length of the ring and 
% the energy lost per turn. If you have many RF cavities, the phases will 
% be wrong.
%


outfile='elegantconvertedlattice.lte';
elelat=['!!\n!!  Elegant lattice: ' linename '\n!!  Created: ' datestr(now) '\n!!\n!!\n\n'];
nkickperL=100; % kicks per metre. You can change if you want, 100 is a large number, probably 40 is enough
U0=4.875433e6; % ESRF value, put here your value.
Length=8.443906924799943e02; % ESRF ring length, put here the length of your synchrotron.

%% get family names for definitions
[families,ind_first_oc_ring]=...
    unique(getcellstruct(AT_ring,'FamName',1:length(AT_ring)),'first');

elelat=[elelat '! DEFINITIONS \n\n'];

form='%8.8f';

%% loop families for definitions
for i=1:length(families)
   el= AT_ring{ind_first_oc_ring(i)}
    switch el.('BetaCode')
        case 'DI' % dipole
            di=[el.('FamName')   ': &\n'...
                ' CSBEND,L=' num2str(el.('Length'),form) ', &\n'...
                ' ANGLE = ' num2str(el.('BendingAngle'),form) ', &\n'...
                ' K1 = ' num2str(el.('PolynomB')(2),form) ', &\n'...
                ' E1= ' num2str(el.('EntranceAngle'),form) ', &\n'...
                ' E2= ' num2str(el.('ExitAngle'),form) ', &\n'...
                ' SYNCH_RAD = 1, &\n'...
                ' N_KICKS= ' num2str(ceil(el.('Length')*nkickperL),'%d') ' \n'...
...%                ' N_KICKS= 40 \n'...
               ];
            elelat=[elelat di '\n'];
        
        case 'QP' % quadrupole
            qp=[el.('FamName')   ': &\n'...
                ' KQUAD,  L= ' num2str(el.('Length'),form)  ', &\n'...
                ' K1= ' num2str(el.('PolynomB')(2),form) ', &\n'...
                ' N_KICKS= ' num2str(ceil(el.('Length')*nkickperL),'%d') ' \n'...
...%                ' N_KICKS= 40 \n'...
                ];   
            elelat=[elelat qp '\n'];

        case 'SX' % sextupole
            sx=[el.('FamName')   ': &\n'...
                ' KSEXT,  L= ' num2str(el.('Length'),form)  ', &\n'...
                ' K2= ' num2str(2*el.('PolynomB')(3),form) ', &\n'...
                ' N_KICKS= ' num2str(ceil(el.('Length')*nkickperL),'%d') ' \n'...
...%                ' N_KICKS= 40 \n'...
                ];
            elelat=[elelat sx '\n'];
    
        case 'OC' % octupole
            sx=[el.('FamName')   ': &\n'...
                ' KOCT,  L= ' num2str(el.('Length'),form)  ', &\n'...
                ' K3= ' num2str(6*el.('PolynomB')(4),form) ', &\n'...
                ' N_KICKS= ' num2str(ceil(el.('Length')*nkickperL),'%d') ' \n'...
                ];
            elelat=[elelat sx '\n'];

        case 'MP' % multipole
            ord=find(el.('PolynomB'));
            if isempty(ord)
                ord=length(el.('PolynomB'));
            end
                mp=[el.('FamName')   ': &\n'...
                    ' MULT,  L= ' num2str(el.('Length'),form)  ', &\n'...
                    'ORDER= ' num2str(ord(1)-1) ', &\n' ... 
 %                  'KNL= ' num2str(el.('PolynomB')(ord(1)),form) ', \n'...
                    'KNL= ' num2str(factorial(ord(1)-1)*el.('PolynomB')(ord(1)),form) ', \n'...
                %' N_KICKS= ' num2str(ceil(el.('Length')*nkickperL+1),'%d') ' \n'...
                ];
            elelat=[elelat mp '\n'];
        case 'PU' % bpm
            pu=[el.('FamName') ': MONI' ' \n'...
                ];
            elelat=[elelat pu '\n'];
        case 'MRK'% marker
            mrk=[el.('FamName') ': MARK' ' \n'...
                ];
            elelat=[elelat mrk '\n'];
        case 'KI' % kicker
            ki=[el.('FamName') ': KICKER' ' \n'...
                ];
            
            elelat=[elelat ki '\n'];
        case 'SD' % drift
            dr=[el.('FamName') ': EDRIFT, L= ' num2str(el.('Length'),form) ' \n'];
            elelat=[elelat dr '\n']; 	
         case 'CA' % RF Cavity
            rfc=[el.('FamName') ' : RFCA, L=' num2str(el.('Length'),form)...
                ', VOLT='  num2str(el.('Voltage'),form) ', &\n'...
                ' phase="180 ' num2str(U0) ' ' num2str(el.('Voltage'),form) ' / dasin -",' ' &\n'...
                ' freq="c_mks ' num2str(Length,'%8.12f') ' / ' num2str(el.('HarmNumber'),form) ' *" ' ' \n'];
      
            elelat=[elelat rfc '\n'];
           
              otherwise
    end
    
end

otherlines=['!Always useful Malign and watch elements \n'...
'M1: MALIGN,on_pass=0\n'...
'W1: watch, filename="%%s.w1", mode ="centroid"\n\n'...
'!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!  \n'...
'!	full 32-cell ring(line):            ! \n'...
'!	             						! \n'...
'!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~! \n\n'
];
elelat=[elelat otherlines '\n'];

elelat=[elelat '! LINE \n\n'];

elelat=[elelat linename ' : LINE = (M1,W1,' ' &\n'];

%% define lattice line
% loop all elements
for i=1:(length(AT_ring)-1)
    
 elelat=[elelat AT_ring{i}.('FamName') ', '];
    if (floor(i/8)==ceil(i/8))
     elelat = [elelat '&\n'];
     end
end

elelat=[elelat AT_ring{length(AT_ring)}.('FamName')  ') \n\n'];


%% print to file

of=fopen(outfile,'w');
fprintf(of,elelat);

fclose('all');




return