function spear2resp
%SPEAR2RESP  example SPEAR2 lattice with orbit correctors and BPMS
%
% Has Horizontal corrector HC at the entrance of each magnet
% and 31 BPMs consitent with SPEAR2 actual BPM locations
global FAMLIST THERING GLOBVAL

GLOBVAL.E0 = 3e9;
GLOBVAL.LatticeFile = 'spear2resp';
FAMLIST = cell(0);

disp(' ');
disp('** Loading SPEAR lattice **');


AP  =  aperture('AP', [-0.05, 0.05, -0.05, 0.05],'AperturePass');
DR01   =    drift('DR01' ,1.344800,'DriftPass');
DR02   =    drift('DR02' ,0.860000,'DriftPass');
DR02H   =   drift('DR02H' ,0.860000/2,'DriftPass'); %for correctors
DR03   =    drift('DR03' ,6.413180,'DriftPass');
DR04   =    drift('DR04' ,0.611890,'DriftPass');
DR04A  =    drift('DR04A',0.617123,'DriftPass');
DR05   =    drift('DR05' ,2.823700,'DriftPass');
DR06A  =    drift('DR06A',0.151205,'DriftPass');
DR06B  =    drift('DR06B',0.229935,'DriftPass');
DR07A  =    drift('DR07A',0.229948,'DriftPass');
DR07B  =    drift('DR07B',0.151205,'DriftPass');
DR08A  =    drift('DR08A',0.151205,'DriftPass');
DR08B  =    drift('DR08B',0.227335,'DriftPass');
DR09   =    drift('DR09' ,2.981660,'DriftPass');


%QF and QD valus set to have the tune at (7.13,5.23)
Q3     =    quadrupole('Q3'  , 1.00000, 0.0000000,'QuadLinearPass');
Q2     =    quadrupole('Q2'  , 1.34274, 0.0790090,'QuadLinearPass');
Q1     =    quadrupole('Q1'  , 0.51834,-0.2595850,'QuadLinearPass');
QFA    =    quadrupole('QFA' , 0.51834, 0.7931150,'QuadLinearPass');
QDA    =    quadrupole('QDA' , 0.51834,-0.6546270,'QuadLinearPass');
QFB    =    quadrupole('QFB' , 0.51834, 0.5169680,'QuadLinearPass');
QF     =    quadrupole('QF'  , 0.51834,  0.4498960277 ,'QuadLinearPass');
QD     =    quadrupole('QD' , 0.51834,-0.669244391,'QuadLinearPass');



% Fitted values to produce normalized chromaticities 0,0 
SF     =    sextupole('SF'  , 0.23335, 1.6768688886, 'StrMPoleSymplectic4Pass');
SDA    =    sextupole('SDA' , 0.23335,-1.29030148931,'StrMPoleSymplectic4Pass');
SDB    =    sextupole('SDB' , 0.23335,-1.29030148931,'StrMPoleSymplectic4Pass');

BB     =    rbend('BB'  ,2.35785400,  ...
            0.1848, 0.0924, 0.0924, 0,'BendLinearPass');

B      =    rbend('B'   ,1.17766900,   ...
            0.0924, 0.0462, 0.0462, 0,'BendLinearPass');

BPM =  marker('BPM', 'IdentityPass');
HC     =    corrector('HC',0,[ 0 0 ],'CorrectorPass');
VC     =    corrector('VC',0,[ 0 0 ],'CorrectorPass');

BBHC = [BB HC];
BHC = [B HC];

% Begin Lattice
SWSE =[AP	DR01 BPM Q3 DR02H DR02H Q2 DR03 BPM Q1 DR04... 
      BBHC DR04A BBHC BPM DR05...
      QFA DR06A SF DR06B BHC DR07A SDA DR07B QDA DR08A ...
      SDA DR08B BBHC DR08B SF DR08A QFB DR09 ...
   	  BPM QF DR04 BBHC DR08B SDB DR08A ...
      QD DR08A SDB DR08B BBHC DR08B ...
      SF DR08A QF BPM DR09...
      BPM QF ...
      DR04 BBHC DR08B SDA DR08A QD ...
      DR08A SDA DR08B BBHC DR04 QF BPM DR09...
      BPM QF DR08A SF DR08B BBHC ...
    	 DR08B SDB DR08A QD DR08A SDB DR08B BBHC ...
      DR08B SF DR08A QF DR09...
      BPM QF DR04 BBHC DR08B SDA DR08A QD ...
      DR08A SDA DR08B BBHC DR04 ...
      QF BPM DR09 ...
      BPM QF DR08A SF DR08B BBHC ...
      DR08B SDB DR08A QD DR08A SDB ...
      DR08B BBHC DR04 QF BPM DR09...
      BPM QFB DR08A ...
      SF DR08B BBHC DR08B SDA DR08A QDA ...
      DR07B SDA DR07A BHC DR06B ...
      SF DR06A QFA DR05 ...
      BPM BBHC DR04A BBHC DR04... 
      Q1 BPM DR03 Q2 DR02H DR02H Q3 BPM DR01 ];

 NENW =  [DR01 BPM Q3 DR02H DR02H Q2 DR03...
    	 BPM Q1 DR04 BBHC DR04A ...
      BBHC DR05 BPM QFA DR06A ...
      SF DR06B BHC DR07A SDA DR07B QDA DR08A ...
      SDA DR08B BBHC DR08B SF DR08A QFB BPM DR09 ...
      BPM QF DR04 BBHC DR08B SDB DR08A QD ...
      DR08A SDB DR08B BBHC DR08B ...
      SF DR08A QF BPM DR09 BPM QF ...
      DR04 BBHC DR08B SDA DR08A QD ...
      DR08A SDA DR08B BBHC DR04 QF ... 
      DR09 BPM QF DR08A SF DR08B BBHC ...
      DR08B SDB DR08A QD ...
 		DR08A SDB DR08B BBHC ...
      DR08B SF DR08A QF BPM DR09 BPM QF ...
      DR04 BBHC DR08B SDA DR08A  ...
      QD DR08A SDA DR08B BBHC DR04 ...
      QF DR09 BPM QF DR08A SF DR08B BBHC ...
      DR08B SDB DR08A QD DR08A SDB ...
      DR08B BBHC DR04 QF DR09 BPM QFB DR08A ...
      SF DR08B BBHC DR08B SDA DR08A QDA ...
      DR07B SDA DR07A BHC DR06B ...
      SF DR06A QFA DR05 BPM BBHC DR04A BBHC DR04 Q1 BPM DR03 ...
      Q2 DR02H DR02H Q3 BPM DR01 AP];


ELIST =  [SWSE NENW AP]; 

ELIST = reverse(ELIST);

buildlat(ELIST);
evalin('base','global THERING FAMLIST GLOBVAL');
disp('** Done **');








