From bc1d00037a4d466889b7f1e66941cd1ab59e7fa2 Mon Sep 17 00:00:00 2001
From: Antonia Frank <antonia.frank@epfl.ch>
Date: Mon, 14 Oct 2024 09:41:26 +0200
Subject: [PATCH] Add the B-fields to the equilibrium IDS and add permutations
 on L,LY outputs to get R,Z order dim

---
 matlab/TCV/gdat_tcv.m                     | 18 +++++++-------
 matlab/TCV_IMAS/tcv_get_ids_equilibrium.m | 30 ++++++++++++++---------
 2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/matlab/TCV/gdat_tcv.m b/matlab/TCV/gdat_tcv.m
index f89a3fd7..c41288d4 100644
--- a/matlab/TCV/gdat_tcv.m
+++ b/matlab/TCV/gdat_tcv.m
@@ -879,32 +879,32 @@ elseif strcmp(mapping_for_tcv.method,'switchcase')
        gdat_data.data_fullpath = datapath;
 
        % radial magnetic field
-       gdat_data.Br.data = LY.Brx;
+       gdat_data.Br.data = permute(LY.Brx,[2,1,3]); % permute for (Z,R)->(R,Z)
        gdat_data.Br.units = 'T';
-       gdat_data.Br.dim = {L.zx,L.rx,LY.t};
+       gdat_data.Br.dim = {L.rx,L.zx,LY.t};
        gdat_data.Br.dimunits = {'m','m','s'};
        gdat_data.Br.t = LY.t;
-       gdat_data.Br.x = {L.zx,L.rx};
+       gdat_data.Br.x = {L.rx,L.zx};
        gdat_data.Br.data_fullpath = datapath;
        gdat_data.Br.label = 'Radial magnetic field map in (R,Z)';
 
        % vertical magnetic field
-       gdat_data.Bz.data = LY.Bzx;
+       gdat_data.Bz.data = permute(LY.Bzx,[2,1,3]);
        gdat_data.Bz.units = 'T';
-       gdat_data.Bz.dim = {L.zx,L.rx,LY.t};
+       gdat_data.Bz.dim = {L.rx,L.zx,LY.t};
        gdat_data.Bz.dimunits = {'m','m','s'};
        gdat_data.Bz.t = LY.t;
-       gdat_data.Bz.x = {L.zx,L.rx};
+       gdat_data.Bz.x = {L.rx,L.zx};
        gdat_data.Bz.data_fullpath = datapath;
        gdat_data.Bz.label = 'Vertical magnetic field map in (R,Z)';
 
        % toroidal magnetic field
-       gdat_data.Btor.data = LY.Btx;
+       gdat_data.Btor.data = permute(LY.Btx,[2,1,3]);
        gdat_data.Btor.units = 'T';
-       gdat_data.Btor.dim = {L.zx,L.rx,LY.t};
+       gdat_data.Btor.dim = {L.rx,L.zx,LY.t};
        gdat_data.Btor.dimunits = {'m','m','s'};
        gdat_data.Btor.t = LY.t;
-       gdat_data.Btor.x = {L.zx,L.rx};
+       gdat_data.Btor.x = {L.rx,L.zx};
        gdat_data.Btor.data_fullpath = datapath;
        gdat_data.Btor.label = 'Toroidal magnetic field map in (R,Z)';
 
diff --git a/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m b/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m
index b0b22e2d..362b186d 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m
@@ -527,19 +527,27 @@ profiles_2d.r.data = repmat(repmat(profiles_2d.psi.dim{1},1,numel(profiles_2d.ps
 profiles_2d_desc.r = 'from dim{1} of ''psi'' repeated';
 profiles_2d.z.data = repmat(repmat(profiles_2d.psi.dim{2}',numel(profiles_2d.psi.dim{1}),1),1,1,numel(profiles_2d.psi.dim{3}));
 profiles_2d_desc.z = 'from dim{2} of ''psi'' repeated';
+params_eff.data_request = 'bfields';
+bfields_gdat = gdat(params_equilibrium.shot,params_eff);
+profiles_2d.b_field_r = bfields_gdat.Br;
+profiles_2d_desc.b_field_r = bfields_gdat.Br.label;
+profiles_2d.b_field_z= bfields_gdat.Bz;
+profiles_2d_desc.b_field_z = bfields_gdat.Bz.label;
+profiles_2d.b_field_tor= bfields_gdat.Btor;
+profiles_2d_desc.b_field_tor = bfields_gdat.Btor.label;
 
 % theta = gdat(params_equilibrium.shot,'theta','machine',gdat_params.machine);
-
+tic
 profiles_2d_fieldnames = fieldnames(profiles_2d);
 special_fields = {'grid', 'grid_type'}; % fields needing non-automatic treatments
 for it=1:ntime
-  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);
+  for i_name=1:numel(profiles_2d_fieldnames)
+    if ~any(strcmp(profiles_2d_fieldnames{i_name},special_fields))
+      if ~isstruct(ids_equilibrium.time_slice{it}.profiles_2d{1}.(profiles_2d_fieldnames{i_name}))
+        if ~ischar(profiles_2d.(profiles_2d_fieldnames{i_name}).data) && ~isempty(profiles_2d.(profiles_2d_fieldnames{i_name}).data) ...
+              && size(profiles_2d.(profiles_2d_fieldnames{i_name}).data,3)>=it
+          ids_equilibrium.time_slice{it}.profiles_2d{1}.(profiles_2d_fieldnames{i_name}) = ...
+              profiles_2d.(profiles_2d_fieldnames{i_name}).data(:,:,it);
         end
       else
         special_fields{end+1} = profiles_2d_fieldnames{i};
@@ -547,7 +555,6 @@ for it=1:ntime
     end
   end
 end
-
 % special cases
 for it=1:ntime
   ids_equilibrium.time_slice{it}.profiles_2d{1}.grid_type.name = profiles_2d.grid_type.name;
@@ -569,7 +576,7 @@ for it=1:numel(ids_equilibrium.time_slice)
   ids_equilibrium.time_slice{it}.boundary_separatrix.x_point = {};
   ids_equilibrium.time_slice{it}.boundary_separatrix.strike_point = {};
 end
-
+toc
 % special test matrix cocos transform
 % $$$ ldim1=129;
 % $$$ ldim2=257;
@@ -596,10 +603,11 @@ end
 % $$$   ids_equilibrium.time_slice{it}.profiles_2d{2}.psi_error_upper(:,:) = 12.*ones(ldim1,ldim2);
 % $$$   ids_equilibrium.time_slice{it}.profiles_2d{2}.psi_error_lower(:,:) = 10.*ones(ldim1,ldim2);
 % $$$ end
-
+tic
 % cocos automatic transform
 if ~isempty(which('ids_generic_cocos_nodes_transformation_symbolic'))
   [ids_equilibrium,cocoscoeff]=ids_generic_cocos_nodes_transformation_symbolic(ids_equilibrium,'equilibrium',gdat_params.cocos_in, ...
           gdat_params.cocos_out,gdat_params.ipsign_out,gdat_params.b0sign_out,gdat_params.ipsign_in,gdat_params.b0sign_in, ...
           gdat_params.error_bar,gdat_params.nverbose);
 end
+toc
-- 
GitLab