function data = tuna_dude(Energy,save_data)

%Energy = 42;
if nargin < 2
    save_data = false;
end

% Magnet and data taking settings
set_magnets = true;
min_magnet_pause = 1.0;
n_avg = 20;
readout_delay = 0.2;
vmp = ''; %'vm1@erp122:';

bpms = {'IFABPM01','IFABPM02','IFABPM03','IFABPM04'};

qpvs = {'MS1QUA01','MS1QUA02','MS1QUA03','MS1QUA04','MS1QUA05','MS1QUA06','MS1QUA07','MS1QUA08'};

dips = {'MD1DIP01','MS1DPB01','MS1DIP02','MS1DIP03','MS1DIP04','MS1DIP05','MS1DIP06','MS1DIP07','MS1DPB08'};

hpvs = {'MS1DIP02','MS1DIP03','MS1DIP04','MS1DIP05','MS1DIP06','MS1DIP07',};
hscales = linspace(-1,1,5);

vpvs = {'MS1CRV01','MS1CRV02','MS1CRV03','MS1CRV04'};
vscales = linspace(-1,1,5);

% Get full pv names with virtual machine prefix if wanted
dips = strcat(vmp,dips);
hpvs = strcat(vmp,hpvs);
vpvs = strcat(vmp,vpvs);
qpvs = strcat(vmp,qpvs);
bpms = strcat(vmp,bpms);

% Find your energy and settings:
inputs = load('settings.txt');
Es = inputs(:,1);
for ii=1:length(Es)   
    
    if(Energy==Es(ii))
        display(['Energy: ' num2str(Es(ii)) ' MeV, settings found.'])
        
        dip_Is = inputs(ii,2:10);
        quad_Is = inputs(ii,11:18);
        
        h1cs = inputs(ii,21:28);
        h2cs = inputs(ii,29:36);
        
        v1cs = inputs(ii,37:40);
        v2cs = inputs(ii,41:44);

        nux0 = inputs(ii,45);
        nuy0 = inputs(ii,46);
        break
    end
end

% Make the settings structure:
settings.set_magnets = set_magnets;
settings.min_magnet_pause = min_magnet_pause;
settings.n_avg = n_avg;
settings.readout_delay = readout_delay;
settings.vmp = vmp;

%% Do the two horizontal knobs
h1_settings = settings;
h1_settings.min_magnet_pause = min_magnet_pause;
h1_settings.scales = hscales;
h1_settings.dips = hpvs;
h1_settings.bpms = bpms;
h1_settings.coeffs = h1cs;
h1data = tune_measurement(h1_settings);

h2_settings = settings;
h2_settings.scales = hscales;
h2_settings.dips = hpvs;
h2_settings.bpms = bpms;
h2_settings.coeffs = h2cs;
h2data = tune_measurement(h2_settings);

%% Do the vertical knobs
v1_settings = settings;
v1_settings.scales = vscales;
v1_settings.dips = vpvs;
v1_settings.bpms = bpms;
v1_settings.coeffs = v1cs;
v1data = tune_measurement(v1_settings);

v2_settings = settings;
v2_settings.scales = vscales;
v2_settings.dips = vpvs;
v2_settings.bpms = bpms;
v2_settings.coeffs = v2cs;
v2data = tune_measurement(v2_settings);

data.Energy = Energy;
data.h1data = h1data;
data.h2data = h2data;
data.v1data = v1data;
data.v2data = v2data;

data.nux0 = nux0;
data.nuy0 = nuy0;

if(save_data)
    save(['./data/tune.' num2str(Energy) '.mat'],'data');
end

end

