function [sub_ids_struct_out,sub_ids_struct_out_description, varargout] =  tcv_ids_bpol_probe(shot, ids_structures, gdat_params, varargin)
%
% [sub_ids_struct_out,sub_ids_struct_out_description, varargout] =  tcv_ids_bpol_probe(shot, ids_structures, gdat_params, varargin)
%
% Get magnetics.bpol_probe
%
% error_bar: from gdat_params.error_bar
%            'delta' (default): error_bar to be added inserted in "upper" only as mentioned in description
%            'delta_with_lower' : error_bar (abs) inserted in both lower and upper
%            'added': value already added to data: upper/lower = data +/- error_bar
%

error_bar = 'delta';
if exist('gdat_params') && isfield(gdat_params,'error_bar') && ~isempty(gdat_params.error_bar)
  error_bar = gdat_params.error_bar;
end

% Get data
tmp = gdat_tcv( shot, '\MAGNETICS::BPOL_003');
names = tmp.dim{2};
time = tmp.dim{1};
data = tmp.data;
ang_m = gdat_tcv( shot, 'static("ANG_M")');

% from mapping file from Holger:
probes_name={ '001' '002' '003' '004' '005' '006' '007' '008' '009' '010' '011' '012' '013' '014' '015' '016' '017' '018' '019' ...
             '020' '021' '022' '023' '024' '025' '026' '027' '028' '029' '030' '031' '032' '033' '034' '035' '036' '037' '038'};
probes_area = [9.200040D-03, 9.126160D-03, 9.163320D-03, 9.158160D-03, 9.147700D-03, 9.141400D-03, 9.155220D-03, 9.160560D-03, 9.058100D-03, ...
              9.228630D-03, 9.251260D-03, 9.149670D-03, 9.271000D-03, 9.112610D-03, 9.114790D-03, 9.180870D-03, 9.131410D-03, 9.120490D-03, ...
              9.154670D-03, 9.115670D-03, 9.190590D-03, 9.176150D-03, 9.186540D-03, 9.207520D-03, 9.176340D-03, 9.085060D-03, 9.155950D-03, ...
              9.135940D-03, 9.112780D-03, 9.059920D-03, 9.316970D-03, 9.114470D-03, 9.132330D-03, 9.086280D-03, 9.122720D-03, 9.045430D-03, ...
              9.106670D-03, 9.124440D-03];
probes_length = [2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, ...
                2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, ...
                2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, ...
                2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, 2.400000D-02, ...
                2.400000D-02, 2.400000D-02];

% Preallocate output structure
Nprobes = size(tmp.data,2);
sub_ids_struct_out(1:Nprobes) = ids_structures;

for ii=1:Nprobes
    sub_ids_struct_out{ii}.name  = [names{ii}];
    sub_ids_struct_out_description{ii}.name  = [names{ii}];
    sub_ids_struct_out{ii}.position.r  = mdsvalue('STATIC("R_M"  )[$1]',sub_ids_struct_out{ii}.name);
    sub_ids_struct_out_description{ii}.position_r  = ['from ''STATIC("R_M"  )[' sub_ids_struct_out{ii}.name ']'''];
    sub_ids_struct_out{ii}.position.z  = mdsvalue('STATIC("Z_M"  )[$1]',sub_ids_struct_out{ii}.name);
    sub_ids_struct_out_description{ii}.position_z  = ['from ''STATIC("Z_M"  )[' sub_ids_struct_out{ii}.name ']'''];
    sub_ids_struct_out{ii}.field.data  =  data(:,ii);
    sub_ids_struct_out_description{ii}.field_data  =  'from ''\MAGNETICS::BPOL_003''';
    sub_ids_struct_out{ii}.field.time  =  time;

    sub_ids_struct_out{ii}.toroidal_angle  =  0.; % to see if should match sector 3 (bpol003)
    aa =  gdat_tcv( shot, ['static("ANG_M")[$1]'',''' sub_ids_struct_out{ii}.name '']);
    sub_ids_struct_out{ii}.poloidal_angle  = aa.data;
    ij=strmatch(names{ii},probes_name);
    sub_ids_struct_out{ii}.area = probes_area(ij);
    sub_ids_struct_out{ii}.length = probes_length(ij);
    sub_ids_struct_out_description{ii}.poloidal_angle  = ['from ' aa.data_fullpath];
    sub_ids_struct_out_description{ii}.toroidal_angle = 'set to 0';
    sub_ids_struct_out_description{ii}.area = 'from array in machine description file';
    sub_ids_struct_out_description{ii}.length = 'from array in machine description file';
end

fixed_error = 0.009999999776483;
switch error_bar
 case 'delta'
  for ii=1:Nprobes
    sub_ids_struct_out{ii}.field.data_error_upper = fixed_error.*ones(size(sub_ids_struct_out{ii}.field.data));% TO BE FOUND;
  sub_ids_struct_out_description{ii}.field_data_error_upper = ['from fixed error value in case ' error_bar];
  sub_ids_struct_out_description{ii}.field_data_error_lower = ['not provided since symmetric'];
  end
 case 'delta_with_lower'
  for ii=1:Nprobes
    sub_ids_struct_out{ii}.field.data_error_upper = fixed_error.*ones(size(sub_ids_struct_out{ii}.field.data));
    sub_ids_struct_out{ii}.field.data_error_lower = sub_ids_struct_out{ii}.field.data_error_upper;
    sub_ids_struct_out_description{ii}.field_data_error_upper = ['from fixed error value in case ' error_bar];
    sub_ids_struct_out_description{ii}.field_data_error_lower = ['from fixed error value in case ' error_bar];
  end
 case 'added'
  for ii=1:Nprobes
    sub_ids_struct_out{ii}.field.data_error_upper = sub_ids_struct_out{ii}.field.data + fixed_error.*ones(size(sub_ids_struct_out{ii}.field.data));
    sub_ids_struct_out{ii}.field.data_error_lower = sub_ids_struct_out{ii}.field.data - fixed_error.*ones(size(sub_ids_struct_out{ii}.field.data));
    sub_ids_struct_out_description{ii}.field_data_error_upper = ['from data + fixed error value in case ' error_bar];
    sub_ids_struct_out_description{ii}.field_data_error_lower = ['from data - fixed error value in case ' error_bar];
  end
 otherwise
  error(['tcv_ids_bpol_loop: error_bar option not known: ' error_bar])
end
