function IDS_out = complete_IDS_CORSICA(IDS_in,varargin)
%
%
%
doread = 0;
dosave = 0;

if doread
% $$$   clear all
% $$$   close all
% $$$   clc

  % This script will complete the IDS of CORSICA with the missing fields recomputing the missing quantities from available information

  data_path_in = '/NoTivoli/carpanes/LIU_RAP_ITER/CORSICA_ids/CORSICA_130510.mat';
  %path_IDS = '/NoTivoli/carpanes/LIU_RAP_ITER/CORSICA_ids/CORSICA_130506.mat';

  data_path_out = '/NoTivoli/carpanes/LIU_RAP_ITER/CORSICA_ids/CORSICA_130510_extended_COCOS17.mat';

  % Load the IDS_in file
  IDS_out = load(data_path_in);
else
  IDS_out = IDS_in;
end

%% Correct COCOS convention on original data

% Put homogenous = 0 on magnetic but 1 on the coils.
% Sign of the angle of the magnetic probes.

%%
G = G_ITER_generator;
% Get the structure from generating files

Pliuqe = liupiter(1000,'uuu',ones(G.na,1),'vvv',ones(G.nm,1), 'www', ones(G.nf,1)); % Some default setting that could be removed probably to use the liupiter directly
G = liug(G,Pliuqe);

L = liuc(Pliuqe,G);

% Extract information from CORSICA IDS_out
[LC,LXC,LYC] = LLXLY_IDS_corsica(L, IDS_out);

%%  -------------------- ADD the missing information on the existing IDS_out CORSICA -----------------------

%% Consider each circuit to be composed by only one coil
% TODO NEED TO ADD THE VERTICAL STABILIZATION COIL STILL
tmp = data_coils();
for ii = 1:numel(tmp.names)
    for jj=1:numel(IDS_out.pf_active.coil)
        % Remove strange character in coil names
        IDS_out.pf_active.coil{jj}.name = regexprep(IDS_out.pf_active.coil{jj}.name,'[\n\r]+','');
        if strcmp(tmp.names{ii},IDS_out.pf_active.coil{jj}.name)
            IDS_out.pf_active.coil{jj}.element{1}.geometry.geometry_type = 2; % Rectangle description
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.r = tmp.R(ii);
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.z = tmp.Z(ii);
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.width = tmp.dR(ii);
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.height = tmp.dZ(ii);
            IDS_out.pf_active.coil{jj}.element{1}.turns_with_sign = tmp.N(ii);
        end
    end
end

%% Add the data to the circuit
Ncoils = numel(IDS_out.pf_active.coil);
for ii=1:Ncoils-2
    IDS_out.pf_active.circuit{ii}.name = IDS_out.pf_active.coil{ii}.name;
    IDS_out.pf_active.supply{ii}.name = IDS_out.pf_active.coil{ii}.name;
    IDS_out.pf_active.circuit{ii}.current.data = IDS_out.pf_active.coil{ii}.current.data/IDS_out.pf_active.coil{ii}.element{1}.turns_with_sign;
    IDS_out.pf_active.circuit{ii}.current.time = IDS_out.pf_active.coil{ii}.current.time;
    IDS_out.pf_active.circuit{ii}.connections = zeros(2, 4*Ncoils);
    IDS_out.pf_active.circuit{ii}.connections(1, 2*ii) =1;
    IDS_out.pf_active.circuit{ii}.connections(2, 2*ii-1) = 1;
    IDS_out.pf_active.circuit{ii}.connections(1, 2*Ncoils + 2*ii-1) =1;
    IDS_out.pf_active.circuit{ii}.connections(2, 2*Ncoils + 2*ii) =1;
end
% Vertical stabilization coil needs to be added still
for ii=(Ncoils-1):Ncoils
    IDS_out.pf_active.circuit{ii}.name = IDS_out.pf_active.coil{ii}.name;
    IDS_out.pf_active.supply{ii}.name = IDS_out.pf_active.coil{ii}.name;
    IDS_out.pf_active.circuit{ii}.name = IDS_out.pf_active.coil{ii}.name;
    IDS_out.pf_active.circuit{ii}.current.data = zeros(LXC.nt, 1);
    IDS_out.pf_active.circuit{ii}.current.time = LXC.t;
    IDS_out.pf_active.circuit{ii}.connections = zeros(2, 4*Ncoils);
    IDS_out.pf_active.circuit{ii}.connections(1, 2*ii) =1;
    IDS_out.pf_active.circuit{ii}.connections(2, 2*ii-1) = 1;
    IDS_out.pf_active.circuit{ii}.connections(1, 2*Ncoils + 2*ii-1) =1;
    IDS_out.pf_active.circuit{ii}.connections(2, 2*Ncoils + 2*ii) =1;
end

%% Limiter description
tmp = data_limiter();
IDS_out.wall.description_2d{1}.limiter.unit{1}.outline.r = tmp.r;
IDS_out.wall.description_2d{1}.limiter.unit{1}.outline.z = tmp.z;


%% Vessel description
% Understand what I need to do for the double layer vessel

%%  -------------- Synthetic diagnostics------------ Need to be recomputed from CORSICA flux map

IDS_out.magnetics.method{1}.ip.time = LXC.t;
IDS_out.magnetics.method{1}.ip.data = LXC.Ip;

% Correct IDS magnetics
aa = gdat([],'ids','source','magnetics');
magnetics_default = aa.magnetics;
mag_fluxloop_def_fields = fieldnames(magnetics_default.flux_loop{1});
if isfield(IDS_out.magnetics,'flux_loop') && length(IDS_out.magnetics.flux_loop) > 0
  mag_fluxloop_fields = fieldnames(IDS_out.magnetics.flux_loop{1});
else
  mag_fluxloop_fields = {};
end
fields_to_add = setdiff(mag_fluxloop_def_fields,mag_fluxloop_fields);

%% Ff
tmp = data_Ff();
for ii=1:numel(tmp.r)
  if ~isempty(fields_to_add)
    for idef=1:length(fields_to_add)
      % add first defaults and fill in after
      IDS_out.magnetics.flux_loop{ii}.(fields_to_add{idef}) = magnetics_default.flux_loop{1}.(fields_to_add{idef});
    end
    if ii==1; warning(['following fields added to magnetics.flux_loop: ' sprintf('%s ',fields_to_add{:})]); end
  end
  IDS_out.magnetics.flux_loop{ii}.position{1}.r = tmp.r(ii);
  IDS_out.magnetics.flux_loop{ii}.position{1}.z = tmp.z(ii);
  IDS_out.magnetics.flux_loop{ii}.name = tmp.name{ii};
end

for ii=1:numel(tmp.r)
    IDS_out.magnetics.flux_loop{ii}.flux.data = -LXC.Ff(ii,:)';
    IDS_out.magnetics.flux_loop{ii}.flux.time = LXC.t;
end

%% Bm
% Correct IDS magnetics
magnetics_default = ids_gen('magnetics');
mag_bpol_probe_def_fields = fieldnames(magnetics_default.bpol_probe{1});
if isfield(IDS_out.magnetics,'bpol_probe') && length(IDS_out.magnetics.bpol_probe) > 0
  mag_bpol_probe_fields = fieldnames(IDS_out.magnetics.bpol_probe{1});
else
  mag_bpol_probe_fields = {};
end
fields_to_add = setdiff(mag_bpol_probe_def_fields,mag_bpol_probe_fields);
mag_bpol_probe_def_fields = fieldnames(magnetics_default.bpol_probe{1}.position);
if length(IDS_out.magnetics.bpol_probe) > 0 && isfield(IDS_out.magnetics.bpol_probe{1},'position')
  mag_bpol_probe_fields = fieldnames(IDS_out.magnetics.bpol_probe{1}.position);
else
  mag_bpol_probe_fields = {};
end
fields_to_add_position = setdiff(mag_bpol_probe_def_fields,mag_bpol_probe_fields);

tmp = data_Bm();
for ii=1:numel(tmp.name)
  if ~isempty(fields_to_add)
    for idef=1:length(fields_to_add)
      % add first defaults and fill in after
      IDS_out.magnetics.bpol_probe{ii}.(fields_to_add{idef}) = magnetics_default.bpol_probe{1}.(fields_to_add{idef});
    end
    if ii==1; warning(['following fields added to magnetics.bpol_probe: ' sprintf('%s ',fields_to_add{:})]); end
  end
  if ~isempty(fields_to_add_position)
    for idef=1:length(fields_to_add_position)
      % add_position first defaults and fill in after
      IDS_out.magnetics.bpol_probe{ii}.position.(fields_to_add_position{idef}) = magnetics_default.bpol_probe{1}.position.(fields_to_add_position{idef});
    end
    if ii==1; warning(['following fields add_positioned to magnetics.bpol_probe.position: ' sprintf('%s ',fields_to_add_position{:})]); end
  end
  IDS_out.magnetics.bpol_probe{ii}.position.r = tmp.r(ii);
  IDS_out.magnetics.bpol_probe{ii}.position.z = tmp.z(ii);
  IDS_out.magnetics.bpol_probe{ii}.poloidal_angle = -tmp.am(ii); % Correct the sign to be consistent with COCOS 11
  IDS_out.magnetics.bpol_probe{ii}.name = tmp.name{ii};
end

for ii=1:numel(tmp.name)
    IDS_out.magnetics.bpol_probe{ii}.field.data = LXC.Bm(ii,:)';
    IDS_out.magnetics.bpol_probe{ii}.field.time = LXC.t;
end

%% Ft
mag_method_def_fields = fieldnames(magnetics_default.method{1});
if isfield(IDS_out.magnetics,'method') && length(IDS_out.magnetics.method) > 0
  mag_method_fields = fieldnames(IDS_out.magnetics.method{1});
else
  mag_method_fields = {};
end
fields_to_add = setdiff(mag_method_def_fields,mag_method_fields);
if ~isempty(fields_to_add)
  for idef=1:length(fields_to_add)
    % add first defaults and fill in after
    IDS_out.magnetics.method{1}.(fields_to_add{idef}) = magnetics_default.method{1}.(fields_to_add{idef});
  end
  warning(['following fields added to magnetics.method: ' sprintf('%s ',fields_to_add{:})]);
end
IDS_out.magnetics.method{1}.diamagnetic_flux.data = -LXC.Ft;
IDS_out.magnetics.method{1}.diamagnetic_flux.time = LXC.t;

%% rBt
%IDS_out.tf.time = LXC.t;
IDS_out.tf.b_field_tor_vacuum_r.time  = LXC.t;
IDS_out.tf.b_field_tor_vacuum_r.data  = LXC.rBt;

%% Convert from cocos_in to cocos_out
cocos_in = 17;
cocos_out = 11;
to_transform = {'pf_active','pf_passive','magnetics','tf','wall','equilibrium'};
for i=1:length(to_transform)
  IDS_out.(to_transform{i}) = ids_generic_cocos_nodes_transformation_symbolic(IDS_out.(to_transform{i}), to_transform{i}, cocos_in, cocos_out,[],[],[],[],[],3);
end

%% Store the resulting data
if dosave
  save(data_path_out, '-struct', 'IDS_out')
  fprintf('\n wrote file %s \n', data_path_out);
end
