function [ids_equilibrium,ids_equilibrium_description,varargout] = tcv_get_ids_equilibrium(shot,ids_equil_empty, gdat_params,varargin);
%
%  [ids_equilibrium,ids_equilibrium_description,varargout] = tcv_get_ids_equilibrium(shot,ids_equil_empty,varargin);
%
%
% gdat_params: gdat_data.gdat_params to get all params passed from original call, in particular error_bar options
%

[ids_equilibrium, params_equilibrium] = tcv_ids_headpart(shot,ids_equil_empty,'equilibrium',varargin{:});

% 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
%
% vacuum_toroidal_field and time, using homogeneous
%
%% liuqe.m at this stage is missing correction 0.996, so use std source by time of default liuqe to make sure
bb = gdat(params_equilibrium.shot,'b0','source','liuqe','machine',gdat_params.machine,'liuqe',gdat_params.liuqe); % to get liuqe time array
vacuum_toroidal_field.b0=gdat(params_equilibrium.shot,'b0','machine',gdat_params.machine);
vacuum_toroidal_field.b0.data = interp1(vacuum_toroidal_field.b0.t,vacuum_toroidal_field.b0.data,bb.t);
vacuum_toroidal_field.b0.t = bb.t;
vacuum_toroidal_field.b0.dim = {vacuum_toroidal_field.b0.t};
vacuum_toroidal_field_desc.b0 = ['''b0'',''timing source'',''liuqe=' num2str(gdat_params.liuqe) ''''];
vacuum_toroidal_field_desc.r0 = '.r0 subfield from: [''b0'',''source'',''liuqe'']';
ids_equilibrium.vacuum_toroidal_field.r0 = vacuum_toroidal_field.b0.r0;
ids_equilibrium.vacuum_toroidal_field.b0 = vacuum_toroidal_field.b0.data;
ids_equilibrium_description.vacuum_toroidal_field = vacuum_toroidal_field_desc;

ids_equilibrium.time = vacuum_toroidal_field.b0.t;
ids_equilibrium_description.time = '.t subfield from: [''b0'',''source'',''liuqe'']';

ids_equilibrium.time_slice(1:numel(ids_equilibrium.time)) = ids_equilibrium.time_slice(1);

% load time array data to copy to time_slices

% global_quantities data into local global_quantities.* structure with correct end names and global_quantities_desc.* with description. Use temp.* and temp_desc.* structures for temporary data

% brute force solution load all eqdsks
% $$$ for it=1:numel(ids_equilibrium.time)
% $$$   ids_equilibrium.time(it)
% $$$   temp.eqdsks{it}=gdat(params_equilibrium.shot,'eqdsk','time',ids_equilibrium.time(it),'write',0,'machine',gdat_params.machine);
% $$$ end
% $$$ temp_desc.eqdsks{1} = '''eqdsk'',''time'',ids_equilibrium.time(it)';

global_quantities.area = gdat(params_equilibrium.shot,'area_edge','machine',gdat_params.machine);
global_quantities_desc.area = 'area_edge';
global_quantities.beta_normal = gdat(params_equilibrium.shot,'betan','machine',gdat_params.machine);
global_quantities_desc.beta_normal = 'betan';
global_quantities.beta_pol = gdat(params_equilibrium.shot,'betap','machine',gdat_params.machine);
global_quantities_desc.beta_pol = 'betap';
global_quantities.beta_tor = gdat(params_equilibrium.shot,'beta','machine',gdat_params.machine);
global_quantities_desc.beta_tor = 'beta';
global_quantities.energy_mhd = gdat(params_equilibrium.shot,'w_mhd','machine',gdat_params.machine);
global_quantities_desc.energy_mhd = 'w_mhd';
global_quantities.ip = gdat(params_equilibrium.shot,'ip','machine',gdat_params.machine);
global_quantities_desc.ip = 'ip';
% length_pol = gdat(params_equilibrium.shot,'length_pol','machine',gdat_params.machine); % to be added
global_quantities.li_3 = gdat(params_equilibrium.shot,'li','machine',gdat_params.machine);
global_quantities_desc.li_3 = 'li';
temp.r_magnetic_axis = gdat(params_equilibrium.shot,'r_axis','machine',gdat_params.machine);
temp_desc.r_magnetic_axis = 'r_axis';
temp.z_magnetic_axis = gdat(params_equilibrium.shot,'z_axis','machine',gdat_params.machine);
temp_desc.z_magnetic_axis = 'z_axis';
temp.psi_axis = gdat(params_equilibrium.shot,'psi_axis','machine',gdat_params.machine); % needs to add psi_edge sincepsi_axis liuqe assuming 0 dege value
temp_desc.psi_axis = 'psi_axis';
global_quantities.psi_boundary = gdat(params_equilibrium.shot,'psi_edge','machine',gdat_params.machine);
global_quantities_desc.psi_boundary = 'psi_edge';
global_quantities.q_95 = gdat(params_equilibrium.shot,'q95','machine',gdat_params.machine);
global_quantities_desc.q_95 = 'q95';
global_quantities.q_axis = gdat(params_equilibrium.shot,'q0','machine',gdat_params.machine); % will be checked with q_rho?
global_quantities_desc.q_axis = 'q0';
temp.q_rho = gdat(params_equilibrium.shot,'q_rho','machine',gdat_params.machine);
temp_desc.q_rho = 'q_rho';
% surface = gdat(params_equilibrium.shot,'surface','machine',gdat_params.machine); % to be added
global_quantities.volume = gdat(params_equilibrium.shot,'volume','machine',gdat_params.machine);
global_quantities_desc.volume = 'volume';
global_quantities.w_mhd = gdat(params_equilibrium.shot,'w_mhd','machine',gdat_params.machine);
global_quantities_desc.w_mhd = 'w_mhd';

global_quantities_fieldnames = fieldnames(global_quantities);
special_fields = {'magnetic_axis', 'psi_axis', 'q_min'}; % fields needing non-automatic treatments
for it=1:numel(ids_equilibrium.time)
  for i=1:numel(global_quantities_fieldnames)
    if ~any(strcmp(global_quantities_fieldnames{i},special_fields))
      if ~isstruct(ids_equilibrium.time_slice{it}.global_quantities.(global_quantities_fieldnames{i}))
        ids_equilibrium.time_slice{it}.global_quantities.(global_quantities_fieldnames{i}) = ...
            global_quantities.(global_quantities_fieldnames{i}).data(it);
      else
        special_fields{end+1} = global_quantities_fieldnames{i};
      end
    end
  end
end

% special case
for it=1:numel(ids_equilibrium.time)
  ids_equilibrium.time_slice{it}.global_quantities.magnetic_axis.r = temp.r_magnetic_axis.data(it);
  ids_equilibrium.time_slice{it}.global_quantities.magnetic_axis.z = temp.z_magnetic_axis.data(it);
  ids_equilibrium.time_slice{it}.global_quantities.psi_axis = temp.psi_axis.data(it) + ...
      ids_equilibrium.time_slice{it}.global_quantities.psi_boundary;
  [zz,izz]=min(temp.q_rho.data(:,it));
  ids_equilibrium.time_slice{it}.global_quantities.q_min.value = zz;
  ids_equilibrium.time_slice{it}.global_quantities.q_min.rho_tor_norm = temp.q_rho.grids_1d.rhotornorm(izz);
end

% for boundary in addition to lcfs
% active_limiter_point = gdat(params_equilibrium.shot,'active_limiter_point','machine',gdat_params.machine);
boundary.elongation = gdat(params_equilibrium.shot,'kappa','machine',gdat_params.machine);
boundary_desc.elongation = 'kappa';
% elongation_lower = gdat(params_equilibrium.shot,'elongation_lower','machine',gdat_params.machine);
% elongation_upper = gdat(params_equilibrium.shot,'elongation_upper','machine',gdat_params.machine);
boundary.minor_radius = gdat(params_equilibrium.shot,'a_minor','machine',gdat_params.machine);
boundary_desc.minor_radius = 'a_minor';
% squareness_lower_inner = gdat(params_equilibrium.shot,'squareness_lower_inner','machine',gdat_params.machine);
% squareness_lower_outer = gdat(params_equilibrium.shot,'squareness_lower_outer','machine',gdat_params.machine);
% squareness_upper_inner = gdat(params_equilibrium.shot,'squareness_upper_inner','machine',gdat_params.machine);
% squareness_upper_outer = gdat(params_equilibrium.shot,'squareness_upper_outer','machine',gdat_params.machine);
% strike_point = gdat(params_equilibrium.shot,'strike_point','machine',gdat_params.machine);
boundary.triangularity = gdat(params_equilibrium.shot,'delta','machine',gdat_params.machine);
boundary_desc.triangularity = 'delta';
boundary.triangularity_lower = gdat(params_equilibrium.shot,'delta_bottom','machine',gdat_params.machine);
boundary_desc.triangularity_lower = 'delta_bottom';
boundary.triangularity_upper = gdat(params_equilibrium.shot,'delta_top','machine',gdat_params.machine);
boundary_desc.triangularity_upper = 'delta_top';
temp.n_x_point = gdat(params_equilibrium.shot,'tcv_eq(''''n_xpts'''',''''liuqe.m'''')','machine',gdat_params.machine);
temp_desc.n_x_point = '''tcv_eq(''''n_xpts'''',''''liuqe.m'''')''';
temp.r_x_point = gdat(params_equilibrium.shot,'tcv_eq(''''r_xpts'''',''''liuqe.m'''')','machine',gdat_params.machine);
temp_desc.r_x_point = '''tcv_eq(''''r_xpts'''',''''liuqe.m'''')''';
temp.z_x_point = gdat(params_equilibrium.shot,'tcv_eq(''''z_xpts'''',''''liuqe.m'''')','machine',gdat_params.machine);
temp_desc.z_x_point = '''tcv_eq(''''z_xpts'''',''''liuqe.m'''')''';
temp.rgeom = gdat(params_equilibrium.shot,'rgeom','machine',gdat_params.machine);
temp_desc.rgeom = 'rgeom';
temp.zgeom = gdat(params_equilibrium.shot,'zgeom','machine',gdat_params.machine);
temp_desc.zgeom = 'zgeom';
temp.r_lcfs = gdat(params_equilibrium.shot,'r_contour_edge','machine',gdat_params.machine);
temp_desc.r_lcfs = 'r_contour_edge';
temp.z_lcfs = gdat(params_equilibrium.shot,'z_contour_edge','machine',gdat_params.machine);
temp_desc.z_lcfs = 'z_contour_edge';

boundary_fieldnames = fieldnames(boundary);
special_fields = {'lcfs', 'geometric_axis', 'x_point'}; % fields needing non-automatic treatments
for it=1:numel(ids_equilibrium.time)
  for i=1:numel(boundary_fieldnames)
    if ~any(strcmp(boundary_fieldnames{i},special_fields))
      if ~isstruct(ids_equilibrium.time_slice{it}.boundary.(boundary_fieldnames{i}))
        ids_equilibrium.time_slice{it}.boundary.(boundary_fieldnames{i}) = ...
            boundary.(boundary_fieldnames{i}).data(it);
      else
        special_fields{end+1} = boundary_fieldnames{i};
      end
    end
  end
end

% special cases
for it=1:numel(ids_equilibrium.time)
  ids_equilibrium.time_slice{it}.boundary.outline.r = temp.r_lcfs.data(:,it);
  ids_equilibrium.time_slice{it}.boundary.outline.z = temp.z_lcfs.data(:,it);
  ids_equilibrium.time_slice{it}.boundary.lcfs.r = ids_equilibrium.time_slice{it}.boundary.outline.r;
  ids_equilibrium.time_slice{it}.boundary.lcfs.z = ids_equilibrium.time_slice{it}.boundary.outline.z;
  ids_equilibrium.time_slice{it}.boundary.geometric_axis.r = temp.rgeom.data(it);
  ids_equilibrium.time_slice{it}.boundary.geometric_axis.z = temp.zgeom.data(it);
  if temp.n_x_point.data(it) > 0
    ids_equilibrium.time_slice{it}.boundary.x_point(1:temp.n_x_point.data(it)) = ids_equilibrium.time_slice{it}.boundary.x_point(1);
    for i=1:temp.n_x_point.data(it)
      ids_equilibrium.time_slice{it}.boundary.x_point{i}.r = temp.r_x_point.data(i,it);
      ids_equilibrium.time_slice{it}.boundary.x_point{i}.z = temp.z_x_point.data(i,it);
    end
  else
    ids_equilibrium.time_slice{it}.boundary.x_point = {};
  end
end

%
%% profiles_1d (cannot use eqdsk since not same radial mesh)
%
% area = gdat(params_equilibrium.shot,'area','machine',gdat_params.machine);
% b_average = gdat(params_equilibrium.shot,'b_average','machine',gdat_params.machine);
% beta_pol = gdat(params_equilibrium.shot,'beta_pol','machine',gdat_params.machine);
% b_field_average = gdat(params_equilibrium.shot,'b_field_average','machine',gdat_params.machine);
% b_field_max = gdat(params_equilibrium.shot,'b_field_max','machine',gdat_params.machine);
% b_field_min = gdat(params_equilibrium.shot,'b_field_min','machine',gdat_params.machine);
% b_max = gdat(params_equilibrium.shot,'b_max','machine',gdat_params.machine);
% b_min = gdat(params_equilibrium.shot,'b_min','machine',gdat_params.machine);
% darea_dpsi = gdat(params_equilibrium.shot,'darea_dpsi','machine',gdat_params.machine);
% darea_drho_tor = gdat(params_equilibrium.shot,'darea_drho_tor','machine',gdat_params.machine);
profiles_1d.dpressure_dpsi = gdat(params_equilibrium.shot,'pprime','machine',gdat_params.machine);
% dpsi_drho_tor = gdat(params_equilibrium.shot,'dpsi_drho_tor','machine',gdat_params.machine);
% dvolume_dpsi = gdat(params_equilibrium.shot,'dvolume_dpsi','machine',gdat_params.machine);
% dvolume_drho_tor = gdat(params_equilibrium.shot,'dvolume_drho_tor','machine',gdat_params.machine);
% elongation = gdat(params_equilibrium.shot,'elongation','machine',gdat_params.machine);
profiles_1d.f_df_dpsi = gdat(params_equilibrium.shot,'ttprime','machine',gdat_params.machine);
profiles_1d.f = gdat(params_equilibrium.shot,'rbphi_rho','machine',gdat_params.machine);
profiles_1d.f.data = 0.996 * profiles_1d.f.data;
profiles_1d.f_df_dpsi.data = 0.996.^2 .* profiles_1d.f_df_dpsi.data;
% geometric_axis = gdat(params_equilibrium.shot,'geometric_axis','machine',gdat_params.machine);
% gm1 = gdat(params_equilibrium.shot,'gm1','machine',gdat_params.machine);
% gm2 = gdat(params_equilibrium.shot,'gm2','machine',gdat_params.machine);
% gm3 = gdat(params_equilibrium.shot,'gm3','machine',gdat_params.machine);
% gm4 = gdat(params_equilibrium.shot,'gm4','machine',gdat_params.machine);
% gm5 = gdat(params_equilibrium.shot,'gm5','machine',gdat_params.machine);
% gm6 = gdat(params_equilibrium.shot,'gm6','machine',gdat_params.machine);
% gm7 = gdat(params_equilibrium.shot,'gm7','machine',gdat_params.machine);
% gm8 = gdat(params_equilibrium.shot,'gm8','machine',gdat_params.machine);
% gm9 = gdat(params_equilibrium.shot,'gm9','machine',gdat_params.machine);
% j_parallel = gdat(params_equilibrium.shot,'j_parallel','machine',gdat_params.machine);
% j_tor = gdat(params_equilibrium.shot,'j_tor','machine',gdat_params.machine);
% magnetic_shear = gdat(params_equilibrium.shot,'magnetic_shear','machine',gdat_params.machine);
% mass_density = gdat(params_equilibrium.shot,'mass_density','machine',gdat_params.machine);
profiles_1d.phi = gdat(params_equilibrium.shot,'phi_tor','machine',gdat_params.machine);
profiles_1d.phi.data = 0.996 * profiles_1d.phi.data;
profiles_1d.pressure = gdat(params_equilibrium.shot,'pressure','machine',gdat_params.machine);
% psi = gdat(params_equilibrium.shot,'psi_rho','machine',gdat_params.machine); % (could take from .x of any like rhotor and psi_axis, psi_edge from global_quantities)
profiles_1d.q = gdat(params_equilibrium.shot,'q_rho','machine',gdat_params.machine);
profiles_1d.rho_tor = gdat(params_equilibrium.shot,'rhotor','machine',gdat_params.machine);
%rho_tor_norm = gdat(params_equilibrium.shot,'rhotor_norm','machine',gdat_params.machine); % from rho_tor
profiles_1d.rho_volume_norm = gdat(params_equilibrium.shot,'rhovol','machine',gdat_params.machine);
% r_inboard = gdat(params_equilibrium.shot,'r_inboard','machine',gdat_params.machine);
% r_outboard = gdat(params_equilibrium.shot,'r_outboard','machine',gdat_params.machine);
% squareness_lower_inner = gdat(params_equilibrium.shot,'squareness_lower_inner','machine',gdat_params.machine);
% squareness_lower_outer = gdat(params_equilibrium.shot,'squareness_lower_outer','machine',gdat_params.machine);
% squareness_upper_inner = gdat(params_equilibrium.shot,'squareness_upper_inner','machine',gdat_params.machine);
% squareness_upper_outer = gdat(params_equilibrium.shot,'squareness_upper_outer','machine',gdat_params.machine);
% surface = gdat(params_equilibrium.shot,'surface','machine',gdat_params.machine);
% trapped_fraction = gdat(params_equilibrium.shot,'trapped_fraction','machine',gdat_params.machine);
% triangularity_lower = gdat(params_equilibrium.shot,'triangularity_lower','machine',gdat_params.machine);
% triangularity_upper = gdat(params_equilibrium.shot,'triangularity_upper','machine',gdat_params.machine);
profiles_1d.volume = gdat(params_equilibrium.shot,'volume_rho','machine',gdat_params.machine,'liuqe',gdat_params.liuqe);

liuqe_opt = gdat_params.liuqe; % default at this stage but could take from gdat params like error bar
switch liuqe_opt
 case {-1},   psitbx_str='FBTE';
 case {1,21}, psitbx_str='LIUQE.M';
 case {11},   psitbx_str='LIUQE';
 case {2, 3, 22, 23}, psitbx_str=['LIUQE.M' num2str(mod(liuqe_opt,10))];
 case {12,13}, psitbx_str=['LIUQE' num2str(mod(liuqe_opt,10))];
 otherwise, error(['Unknown LIUQE version, liuqe = ' num2str(liuqe_opt)]);
end
fsd = psitbxtcv2(shot,profiles_1d.volume.t,'FS',psitbx_str);
grho_metric_3D = metric(fsd,-1);
% Introduced new anonymous function to compute FS average ...
metric_FS = metric(grho_metric_3D.grid,[2,3]);
denom=sum(metric_FS./grho_metric_3D,[2,3]);
FS_av = @(x) sum(x.*metric_FS./grho_metric_3D,[2,3])./denom;
R=metric(fsd,3);
Rm2av=FS_av(1./R.^2);
profiles_1d.gm1.data = Rm2av.x;
%tmp_gm = FS_av(grho_metric_3D.^2./R.^2); % this gives (grad rhopol/R)^2 not gm2 which is grad rhotor^2
%profiles_1d.gm2.data = tmp_gm.x;
tmp_gm = FS_av(1./R.^1);
profiles_1d.gm9.data = tmp_gm.x;

tmp_gm = FS_av(grho_metric_3D.^2./R.^2); % grad rhopol^2 to get <grad psi^2>
for it=1:numel(ids_equilibrium.time)
  gradpsi_over_R_sq(:,it) = tmp_gm.x(:,it) .* 4 .* profiles_1d.volume.x.^2 .* ...
      (ids_equilibrium.time_slice{it}.global_quantities.psi_boundary-ids_equilibrium.time_slice{it}.global_quantities.psi_axis).^2;
end
mu0 = 4.e-7 * pi;
% Eq. (30) cocos paper cocos=17
% j_tor=<jphi/R>/<1/R>=-sigma_Bp (2pi)^e_Bp dp/dpsi / <1/R> - sigma_Bp (2pi)^e_Bp F dF/dpsi / mu0 <1/R^2> / <1/R>
% simaBp=-1 and eBp=1 for cocos=17 from TCV LIUQE
profiles_1d.j_tor.data = - (-1.) .* 2.*pi .* profiles_1d.dpressure_dpsi.data ./ profiles_1d.gm9.data ...
    - (-1.) .* 2.*pi .* profiles_1d.gm1.data ./ profiles_1d.gm9.data .* profiles_1d.f_df_dpsi.data ./ mu0;
%
% <j.B> = - sigma_Bp (2pi)^e_Bp dp/dpsi F - sigma_Bp F dF/dpsi / mu0 [ (2pi)^e_Bp F <1/R^2> + 1/(2pi)^e_Bp * <|grad psi|^2/R^2> / F ]
% simaBp=-1 and eBp=1 for cocos=17 from TCV LIUQE
%
j_par = - (-1.) .* 2*pi .* profiles_1d.dpressure_dpsi.data .* profiles_1d.f.data ...
        - (-1.) .* profiles_1d.f_df_dpsi.data ./ mu0 .* ...
        ( (2.*pi) .* profiles_1d.f.data .* profiles_1d.gm1.data + 1./(2.*pi) .* gradpsi_over_R_sq ./ profiles_1d.f.data);
profiles_1d.j_parallel.data = j_par./repmat(ids_equilibrium.vacuum_toroidal_field.b0',size(profiles_1d.f.data,1),1);

profiles_1d_fieldnames = fieldnames(profiles_1d);
special_fields = {'geometric_axis', 'rho_tor_norm', 'psi'}; % fields needing non-automatic treatments
for it=1:numel(ids_equilibrium.time)
  for i=1:numel(profiles_1d_fieldnames)
    if ~any(strcmp(profiles_1d_fieldnames{i},special_fields))
      if ~isstruct(ids_equilibrium.time_slice{it}.profiles_1d.(profiles_1d_fieldnames{i}))
        if ~ischar(profiles_1d.(profiles_1d_fieldnames{i}).data) && ~isempty(profiles_1d.(profiles_1d_fieldnames{i}).data) ...
              && size(profiles_1d.(profiles_1d_fieldnames{i}).data,2)>=it
          ids_equilibrium.time_slice{it}.profiles_1d.(profiles_1d_fieldnames{i}) = ...
              profiles_1d.(profiles_1d_fieldnames{i}).data(:,it);
        end
      else
        special_fields{end+1} = profiles_1d_fieldnames{i};
      end
    end
  end
end

% special cases
nrho = numel(profiles_1d.rho_tor.x);
ntime = numel(temp.psi_axis.data);
for it=1:numel(ids_equilibrium.time)
  ids_equilibrium.time_slice{it}.global_quantities.magnetic_axis.b_field_tor = ids_equilibrium.time_slice{it}.profiles_1d.f(1) ...
      ./ids_equilibrium.time_slice{it}.global_quantities.magnetic_axis.r;
  ids_equilibrium.time_slice{it}.global_quantities.magnetic_axis.b_tor = ids_equilibrium.time_slice{it}.global_quantities.magnetic_axis.b_field_tor;
  ids_equilibrium.time_slice{it}.profiles_1d.rho_tor_norm = ids_equilibrium.time_slice{it}.profiles_1d.rho_tor./ ...
      ids_equilibrium.time_slice{it}.profiles_1d.rho_tor(end);
  ids_equilibrium.time_slice{it}.profiles_1d.psi = ids_equilibrium.time_slice{it}.global_quantities.psi_axis + ...
      profiles_1d.rho_tor.x.^2 .* ...
      (ids_equilibrium.time_slice{it}.global_quantities.psi_boundary- ids_equilibrium.time_slice{it}.global_quantities.psi_axis);
end

%
%% profiles_2d{1} ala eqdsk, only this one thus grid_type=1
%
% b_field_r = gdat(params_equilibrium.shot,'b_field_r','machine',gdat_params.machine);
% b_field_tor = gdat(params_equilibrium.shot,'b_field_tor','machine',gdat_params.machine);
% b_field_z = gdat(params_equilibrium.shot,'b_field_z','machine',gdat_params.machine);
% b_r = gdat(params_equilibrium.shot,'b_r','machine',gdat_params.machine);
% b_tor = gdat(params_equilibrium.shot,'b_tor','machine',gdat_params.machine);
% b_z = gdat(params_equilibrium.shot,'b_z','machine',gdat_params.machine);
% grid = gdat(params_equilibrium.shot,'grid','machine',gdat_params.machine); % special
profiles_2d.grid_type.name = 'rectangular';
profiles_2d.grid_type.index = 1;
profiles_2d.grid_type.description = 'Cylindrical R,Z ala eqdsk';
% j_parallel = gdat(params_equilibrium.shot,'j_parallel','machine',gdat_params.machine);
% j_tor = gdat(params_equilibrium.shot,'j_tor','machine',gdat_params.machine);
% phi = gdat(params_equilibrium.shot,'phi','machine',gdat_params.machine);
profiles_2d.psi = gdat(params_equilibrium.shot,'psi','machine',gdat_params.machine); % add psi_bound in a second step in special cases
% r = gdat(params_equilibrium.shot,'r','machine',gdat_params.machine); % not to be filled since in grid.dim1
% theta = gdat(params_equilibrium.shot,'theta','machine',gdat_params.machine);
% z = gdat(params_equilibrium.shot,'z','machine',gdat_params.machine); % not to be filled since in grid.dim2

profiles_2d_fieldnames = fieldnames(profiles_2d);
special_fields = {'grid', 'grid_type'}; % fields needing non-automatic treatments
for it=1:numel(ids_equilibrium.time)
  for i=1:numel(profiles_2d_fieldnames)
    if ~any(strcmp(profiles_2d_fieldnames{i},special_fields))
      if ~isstruct(ids_equilibrium.time_slice{it}.profiles_2d{1}.(profiles_2d_fieldnames{i}))
        if ~ischar(profiles_2d.(profiles_2d_fieldnames{i}).data) && ~isempty(profiles_2d.(profiles_2d_fieldnames{i}).data) ...
              && size(profiles_2d.(profiles_2d_fieldnames{i}).data,3)>=it
          ids_equilibrium.time_slice{it}.profiles_2d{1}.(profiles_2d_fieldnames{i}) = ...
              profiles_2d.(profiles_2d_fieldnames{i}).data(:,:,it);
        end
      else
        special_fields{end+1} = profiles_2d_fieldnames{i};
      end
    end
  end
end

% special cases
for it=1:numel(ids_equilibrium.time)
  ids_equilibrium.time_slice{it}.profiles_2d{1}.grid_type.name = profiles_2d.grid_type.name;
  ids_equilibrium.time_slice{it}.profiles_2d{1}.grid_type.index = profiles_2d.grid_type.index;
  ids_equilibrium.time_slice{it}.profiles_2d{1}.grid_type.description = profiles_2d.grid_type.description;
  ids_equilibrium.time_slice{it}.profiles_2d{1}.grid.dim1 = profiles_2d.psi.dim{1};
  ids_equilibrium.time_slice{it}.profiles_2d{1}.grid.dim2 = profiles_2d.psi.dim{2};
  ids_equilibrium.time_slice{it}.profiles_2d{1}.psi(:,:) = ids_equilibrium.time_slice{it}.profiles_2d{1}.psi(:,:) + ...
      global_quantities.psi_boundary.data(it);
end


% make arrays not filled in empty:
ids_equilibrium.grids_ggd = {};
for it=1:numel(ids_equilibrium.time_slice)
  ids_equilibrium.time_slice{it}.ggd = {};
end
