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 file
IDS = load(data_path_in);

%% 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
P = 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,P);
L = liuc(P,G);

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

%%  -------------------- ADD the missing information on the existing IDS 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.pf_active.coil)
        % Remove strange character in coil names
        IDS.pf_active.coil{jj}.name = regexprep(IDS.pf_active.coil{jj}.name,'[\n\r]+','');
        if strcmp(tmp.names{ii},IDS.pf_active.coil{jj}.name)
            IDS.pf_active.coil{jj}.element{1}.geometry.geometry_type = 2; % Rectangle description
            IDS.pf_active.coil{jj}.element{1}.geometry.rectangle.r = tmp.R(ii); 
            IDS.pf_active.coil{jj}.element{1}.geometry.rectangle.z = tmp.Z(ii); 
            IDS.pf_active.coil{jj}.element{1}.geometry.rectangle.width = tmp.dR(ii);
            IDS.pf_active.coil{jj}.element{1}.geometry.rectangle.height = tmp.dZ(ii);
            IDS.pf_active.coil{jj}.element{1}.turns_with_sign = tmp.N(ii);
        end
    end
end

%% Add the data to the circuit
Ncoils = numel(IDS.pf_active.coil);
for ii=1:Ncoils-2
    IDS.pf_active.circuit{ii}.name = IDS.pf_active.coil{ii}.name;
    IDS.pf_active.supply{ii}.name = IDS.pf_active.coil{ii}.name;
    IDS.pf_active.circuit{ii}.current.data = IDS.pf_active.coil{ii}.current.data/IDS.pf_active.coil{ii}.element{1}.turns_with_sign;
    IDS.pf_active.circuit{ii}.current.time = IDS.pf_active.coil{ii}.current.time;
    IDS.pf_active.circuit{ii}.connections = zeros(2, 4*Ncoils);
    IDS.pf_active.circuit{ii}.connections(1, 2*ii) =1;
    IDS.pf_active.circuit{ii}.connections(2, 2*ii-1) = 1;
    IDS.pf_active.circuit{ii}.connections(1, 2*Ncoils + 2*ii-1) =1;
    IDS.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.pf_active.circuit{ii}.name = IDS.pf_active.coil{ii}.name;
    IDS.pf_active.supply{ii}.name = IDS.pf_active.coil{ii}.name;
    IDS.pf_active.circuit{ii}.name = IDS.pf_active.coil{ii}.name;
    IDS.pf_active.circuit{ii}.current.data = zeros(LXC.nt, 1);
    IDS.pf_active.circuit{ii}.current.time = LXC.t;
    IDS.pf_active.circuit{ii}.connections = zeros(2, 4*Ncoils);
    IDS.pf_active.circuit{ii}.connections(1, 2*ii) =1;
    IDS.pf_active.circuit{ii}.connections(2, 2*ii-1) = 1;
    IDS.pf_active.circuit{ii}.connections(1, 2*Ncoils + 2*ii-1) =1;
    IDS.pf_active.circuit{ii}.connections(2, 2*Ncoils + 2*ii) =1;
end

%% Limiter description
tmp = data_limiter();
IDS.wall.description_2d{1}.limiter.unit{1}.outline.r = tmp.r;
IDS.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.magnetics.method{1}.ip.time = LXC.t; 
IDS.magnetics.method{1}.ip.data = LXC.Ip;

%% Ff
tmp = data_Ff();
for ii=1:numel(tmp.r)
    IDS.magnetics.flux_loop{ii}.position{1}.r = tmp.r(ii);
    IDS.magnetics.flux_loop{ii}.position{1}.z = tmp.z(ii);
    IDS.magnetics.flux_loop{ii}.name = tmp.name{ii};
end

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

%% Bm
tmp = data_Bm();
for ii=1:numel(tmp.name)
    IDS.magnetics.bpol_probe{ii}.position.r = tmp.r(ii);
    IDS.magnetics.bpol_probe{ii}.position.z = tmp.z(ii);
    IDS.magnetics.bpol_probe{ii}.poloidal_angle = -tmp.am(ii); % Correct the sign to be consistent with COCOS 11
    IDS.magnetics.bpol_probe{ii}.name = tmp.name{ii};
end

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

%% Ft 
IDS.magnetics.method{1}.diamagnetic_flux.data = -LXC.Ft;
IDS.magnetics.method{1}.diamagnetic_flux.time = LXC.t;

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

%% Convert from 11 to 17
IDS.magnetics = ids_generic_cocos_nodes_transformation_symbolic(IDS.magnetics, 'magnetics', 11, 17);
IDS.pf_active = ids_generic_cocos_nodes_transformation_symbolic(IDS.pf_active, 'pf_active', 11, 17);
IDS.tf = ids_generic_cocos_nodes_transformation_symbolic(IDS.tf, 'tf', 11, 17);

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


