function [ids_nbi,ids_nbi_description,varargout] = tcv_get_ids_nbi(shot,ids_nbi_empty, gdat_params,varargin);
%
%  [ids_nbi,ids_nbi_description,varargout] = tcv_get_ids_nbi(shot,ids_nbi_empty,varargin);
%
%
% gdat_params: gdat_data.gdat_params to get all params passed from original call, in particular error_bar options
%

[ids_nbi, params_nbi] = tcv_ids_headpart(shot,ids_nbi_empty,'nbi','homogeneous_time',0,varargin{:});
ids_nbi_description='';
% As a general rule, for a new substructure under the main ids, construct a local structure like:
% "global_quantities" with subfields being the relevant data to get and a local structure:
% "global_quantities_desc" which contains the same subfields themselves containing the gdat string aftre shot used
%


nb_units = 2; % assume 2 units: 1st NBH and DNBI
ids_nbi.unit(1:nb_units) = ids_nbi.unit(1); % copy empty structure for all units, then fill in

for iunit=1:nb_units
  switch iunit
   case 1
    ids_nbi.unit{iunit}.identifier = 'NBH1';
    ids_nbi.unit{iunit}.name = '25keV 1st NBH source';
    %% power
    pow=gdat_tcv(shot,'powers');
    ids_nbi.unit{iunit}.power_launched.time = pow.nbi.t;
    ids_nbi.unit{iunit}.power_launched.data = pow.nbi.data;
    %% energy
    ids_nbi.unit{iunit}.energy.time = pow.nbi.t;
    ids_nbi.unit{iunit}.energy.data = pow.nbi.energy;
    %% power & current fractions
    p_frac=gdat(shot,'\results::nbh:fraction');
    ids_nbi.unit{iunit}.beam_power_fraction.data = p_frac.data(:,1:3)*0.01;
    ids_nbi.unit{iunit}.beam_power_fraction.time = p_frac.t;
    i_frac = p_frac.data(:,1:3).*repmat([1 2 3],size(p_frac.data,1),1); % to be compatible with older matlab version .*[1 2 3] not ok
    i_frac = i_frac.*1./sum(i_frac);
    ids_nbi.unit{iunit}.beam_current_fraction.data = i_frac;
    ids_nbi.unit{iunit}.beam_current_fraction.time = p_frac.t;
    %% species
    ids_nbi.unit{iunit}.species.a   = 2.;
    ids_nbi.unit{iunit}.species.z_n = 1.;
    ids_nbi.unit{iunit}.species.label = ['D'];
    %% beamlets group, now only one single beamlet
    % https://spcwiki.epfl.ch/wiki/NB_Model
    ids_nbi.unit{iunit}.beamlets_group{1}.direction = -1; %clockwise
    ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius = 736*1e-3; %736mm
%     ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius_error_upper: -9.0000e+40
    ids_nbi.unit{iunit}.beamlets_group{1}.angle = 0.; %injection parallel to midplane
%     ids_nbi.unit{iunit}.beamlets_group{1}.angle_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.angle_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.angle_error_upper: -9.0000e+40
    ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal = 250*1e-3;
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal_error_upper: -9.0000e+40
    ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical = 250*1e-3;
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical_error_upper: -9.0000e+40
    %% focus struct
    focus  = struct();
    focus.focal_length_horizontal = 3.76;
%     focus.focal_length_horizontal_error_index = -999999999;
%     focus.focal_length_horizontal_error_lower = -9.0000e+40;
%     focus.focal_length_horizontal_error_upper = -9.0000e+40;
    focus.focal_length_vertical = 3.98;
%     focus.focal_length_vertical_error_index: -999999999;
%     focus.focal_length_vertical_error_lower: -9.0000e+40;
%     focus.focal_length_vertical_error_upper: -9.0000e+40;
    focus.width_min_horizontal = 21.6*1e-2;
%     focus.width_min_horizontal_error_index: -999999999;
%     focus.width_min_horizontal_error_lower: -9.0000e+40;
%     focus.width_min_horizontal_error_upper: -9.0000e+40;
    focus.width_min_vertical = 9.4*1e-2;
%     focus.width_min_vertical_error_index: -999999999;
%     focus.width_min_vertical_error_lower: -9.0000e+40;
%     focus.width_min_vertical_error_upper: -9.0000e+40;
    ids_nbi.unit{iunit}.beamlets_group{1}.focus = focus;
    %% divergence component struct
    div = struct();
    div.particle_fraction = 1.;
    div.vertical = 10.3*1e-3; %rad, in deg it is 0.59
    div.horizontal = 24.4*1e-3; %rad, in deg it is 1.4
    ids_nbi.unit{iunit}.beamlets_group{1}.divergence_component{1} = div;
    %% tilting
    % it is fixed in time. what should we do about it?

    %% position
    %dcd_NBH = psitbxdcd(4.5889, 0.0, 211.9535*pi/180, 0.0, -9.2308*pi/180);
    pos = struct();
    pos.phi= 211.9535*pi/180.;
%     pos.phi_error_index= -999999999;
%     pos.phi_error_lower= -9.0000e+40;
%     pos.phi_error_upper= -9.0000e+40;
    pos.r= 4.5889;
%     pos.r_error_index= -999999999;
%     pos.r_error_lower= -9.0000e+40;
%     pos.r_error_upper= -9.0000e+40;
    pos.z= 0.;
%     pos.z_error_index= -999999999;
%     pos.z_error_lower= -9.0000e+40;
%     pos.z_error_upper= -9.0000e+40;
    ids_nbi.unit{iunit}.beamlets_group{1}.position = pos;
    %% beamlets
    % M. Vallar thinks it is useless now

   case 2
    ids_nbi.unit{iunit}.identifier = 'DNBI';
    ids_nbi.unit{iunit}.name = 'diagnostic NBI';
    %% power
    pow=gdat_tcv(shot,'\RESULTS::DNBI:POWR_TCV');
    ids_nbi.unit{iunit}.power_launched.time = pow.t;
    ids_nbi.unit{iunit}.power_launched.data = pow.data*1e6;
    %% energy
    en=gdat(shot,'\RESULTS::DNBI:ENERGY');
    ids_nbi.unit{iunit}.energy.data = en.data*1e3;
    ids_nbi.unit{iunit}.energy.time = en.t;
    %% power & current fractions
    p_frac=gdat(shot,'\results::dnbi:fraction');
    ids_nbi.unit{iunit}.beam_power_fraction.data = p_frac.data(:,1:3)*0.01;
    ids_nbi.unit{iunit}.beam_power_fraction.time = p_frac.t;
    i_frac = p_frac.data(:,1:3).*repmat([1 2 3],size(p_frac.data,1),1); % to be compatible with older matlab version .*[1 2 3] not ok
    i_frac = i_frac.*1./sum(i_frac);
    ids_nbi.unit{iunit}.beam_current_fraction.data = i_frac(:,:);
    ids_nbi.unit{iunit}.beam_current_fraction.time = p_frac.t;
    %% species
    ids_nbi.unit{iunit}.species.a   = 1.;
    ids_nbi.unit{iunit}.species.z_n = 1.;
    ids_nbi.unit{iunit}.species.label = ['H'];
    %% beamlets group, now only one single beamlet
    % https://spcwiki.epfl.ch/wiki/NB_Model
    ids_nbi.unit{iunit}.beamlets_group{1}.direction = 1; %ctr-clockwise
    ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius = 235.3*1e-3; %736mm
%     ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.tangency_radius_error_upper: -9.0000e+40
    ids_nbi.unit{iunit}.beamlets_group{1}.angle = 0.; %injection parallel to midplane
%     ids_nbi.unit{iunit}.beamlets_group{1}.angle_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.angle_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.angle_error_upper: -9.0000e+40
    ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal = 87.2*1e-3;
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_horizontal_error_upper: -9.0000e+40
    ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical = 87.2*1e-3;
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical_error_index: -999999999
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical_error_lower: -9.0000e+40
%     ids_nbi.unit{iunit}.beamlets_group{1}.width_vertical_error_upper: -9.0000e+40
    %% focus struct
    focus  = struct();
    focus.focal_length_horizontal = 1.8;
%     focus.focal_length_horizontal_error_index = -999999999;
%     focus.focal_length_horizontal_error_lower = -9.0000e+40;
%     focus.focal_length_horizontal_error_upper = -9.0000e+40;
    focus.focal_length_vertical = 1.8;
%     focus.focal_length_vertical_error_index: -999999999;
%     focus.focal_length_vertical_error_lower: -9.0000e+40;
%     focus.focal_length_vertical_error_upper: -9.0000e+40;
    focus.width_min_horizontal = 12.1*1e-2;
%     focus.width_min_horizontal_error_index: -999999999;
%     focus.width_min_horizontal_error_lower: -9.0000e+40;
%     focus.width_min_horizontal_error_upper: -9.0000e+40;
    focus.width_min_vertical = 12.1*1e-2;
%     focus.width_min_vertical_error_index: -999999999;
%     focus.width_min_vertical_error_lower: -9.0000e+40;
%     focus.width_min_vertical_error_upper: -9.0000e+40;
    ids_nbi.unit{iunit}.beamlets_group{1}.focus = focus;
    %% divergence component struct
    div = struct();
    div.particle_fraction = 1.;
    div.vertical = 9.2*1e-3; %rad, in deg it is 0.53
    div.horizontal = 9.2*1e-3; %rad, in deg it is 0.53
    ids_nbi.unit{iunit}.beamlets_group{1}.divergence_component{1} = div;
    %% tilting
    % it is fixed in time. what should we do about it?

    %% position
    %dcd_NBH = psitbxdcd(4.5889, 0.0, 211.9535*pi/180, 0.0, -9.2308*pi/180);
    pos = struct();
    pos.phi= 295.2416*pi/180.;
%     pos.phi_error_index= -999999999;
%     pos.phi_error_lower= -9.0000e+40;
%     pos.phi_error_upper= -9.0000e+40;
    pos.r= 4.9274;
%     pos.r_error_index= -999999999;
%     pos.r_error_lower= -9.0000e+40;
%     pos.r_error_upper= -9.0000e+40;
    pos.z= 0.;
%     pos.z_error_index= -999999999;
%     pos.z_error_lower= -9.0000e+40;
%     pos.z_error_upper= -9.0000e+40;
    ids_nbi.unit{iunit}.beamlets_group{1}.position = pos;
    %% beamlets
    % M. Vallar thinks it is useless now
   otherwise
    warning(['iunit = ' num2str(iunit) ' not yet defined']);
  end

end
