From 726cf9d45f584b89e37cedd5fc92c3f5bda104de Mon Sep 17 00:00:00 2001
From: Olivier Sauter <Olivier.Sauter@epfl.ch>
Date: Sun, 13 Oct 2019 20:06:46 +0200
Subject: [PATCH] bpol and ec_antennas according to latest IMAS update,
 grid_type transform all except tensors

---
 matlab/TCV_IMAS/tcv_get_ids_core_profiles.m | 23 +++++++--
 matlab/TCV_IMAS/tcv_get_ids_ec_antennas.m   |  7 ++-
 matlab/TCV_IMAS/tcv_get_ids_equilibrium.m   | 19 ++++++-
 matlab/TCV_IMAS/tcv_get_ids_magnetics.m     |  7 ++-
 matlab/TCV_IMAS/tcv_get_ids_nbi.m           |  7 ++-
 matlab/TCV_IMAS/tcv_get_ids_pf_active.m     |  6 ++-
 matlab/TCV_IMAS/tcv_get_ids_tf.m            |  6 ++-
 matlab/TCV_IMAS/tcv_get_ids_wall.m          |  6 ++-
 matlab/TCV_IMAS/tcv_ids_bpol_probe.m        |  2 +-
 matlab/TCV_IMAS/tcv_ids_headpart.m          | 57 +++++++++++++++++++--
 10 files changed, 123 insertions(+), 17 deletions(-)

diff --git a/matlab/TCV_IMAS/tcv_get_ids_core_profiles.m b/matlab/TCV_IMAS/tcv_get_ids_core_profiles.m
index a3ddde24..b0d2703e 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_core_profiles.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_core_profiles.m
@@ -21,7 +21,11 @@ machine = 'tcv';
 tens_time = -1;
 tens_rho = -0.1;
 
-[ids_core_profiles, params_cores_profiles] = tcv_ids_headpart(shot,ids_equil_empty,'cores_profiles',varargin{:});
+if exist('gdat_params')
+  [ids_core_profiles, params_cores_profiles] = tcv_ids_headpart(shot,ids_equil_empty,'cores_profiles','gdat_params',gdat_params,varargin{:});
+else
+  [ids_core_profiles, params_cores_profiles] = tcv_ids_headpart(shot,ids_equil_empty,'cores_profiles',varargin{:});
+end
 ids_core_profiles_description = [];
 
 % base all from times fro nete_rho (which should be conf by default)
@@ -269,11 +273,20 @@ for ir=1:length(temp_1d.q.x)
   q_cpt(ir,:) = interpos(temp_1d.q.t,temp_1d.q.data(ir,:),ids_core_profiles.time,tens_time);
 end
 for it=1:length(ids_core_profiles.time)
-  [ids_core_profiles.profiles_1d{it}.q] = interpos(temp_1d.q.x,q_cpt(:,it),temp_1d.fit.te_rho.grids_1d.rhopolnorm, ...
+  ij=isfinite(q_cpt(:,it));
+  if sum(ij) >= 5
+    [ids_core_profiles.profiles_1d{it}.q] = interpos(temp_1d.q.x(ij),q_cpt(ij,it),temp_1d.fit.te_rho.grids_1d.rhopolnorm, ...
           tens_rho,[1 0],[0 0]);
-  [qfit,dqdrhotor] = interpos(ids_core_profiles.profiles_1d{it}.grid.rho_tor,ids_core_profiles.profiles_1d{it}.q,tens_rho,[1 0],[0 0]);
-  ids_core_profiles.profiles_1d{it}.magnetic_shear = ids_core_profiles.profiles_1d{it}.grid.rho_tor./ids_core_profiles.profiles_1d{it}.q ...
-      .* dqdrhotor;
+    [qfit,dqdrhotor] = interpos(ids_core_profiles.profiles_1d{it}.grid.rho_tor,ids_core_profiles.profiles_1d{it}.q,tens_rho,[1 0],[0 0]);
+    ids_core_profiles.profiles_1d{it}.magnetic_shear = ids_core_profiles.profiles_1d{it}.grid.rho_tor./ids_core_profiles.profiles_1d{it}.q ...
+        .* dqdrhotor;
+  elseif sum(ij) > 0
+    ids_core_profiles.profiles_1d{it}.q = interp1(temp_1d.q.x(ij),q_cpt(ij,it),temp_1d.fit.te_rho.grids_1d.rhopolnorm);
+    ids_core_profiles.profiles_1d{it}.magnetic_shear = -9.e40;
+  else
+    ids_core_profiles.profiles_1d{it}.q = -9.e40;
+    ids_core_profiles.profiles_1d{it}.magnetic_shear = -9.e40;
+  end
 end
 
 if nargin <= 2
diff --git a/matlab/TCV_IMAS/tcv_get_ids_ec_antennas.m b/matlab/TCV_IMAS/tcv_get_ids_ec_antennas.m
index b7a15132..353aac4a 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_ec_antennas.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_ec_antennas.m
@@ -6,7 +6,12 @@ function [ids_ec_antennas,ids_ec_antennas_description,varargout] = tcv_get_ids_e
 % gdat_params: gdat_data.gdat_params to get all params passed from original call, in particular error_bar options
 %
 
-[ids_ec_antennas, params_ec_antennas] = tcv_ids_headpart(shot,ids_ec_antennas_empty,'ec_antennas','homogeneous_time',0,varargin{:});
+if exist('gdat_params')
+  [ids_ec_antennas, params_ec_antennas] = tcv_ids_headpart(shot,ids_ec_antennas_empty,'ec_antennas','homogeneous_time',0, ...
+          'gdat_params',gdat_params,varargin{:});
+else
+  [ids_ec_antennas, params_ec_antennas] = tcv_ids_headpart(shot,ids_ec_antennas_empty,'ec_antennas','homogeneous_time',0,varargin{:});
+end
 
 % 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:
diff --git a/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m b/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m
index f283c6db..6abbae2f 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_equilibrium.m
@@ -6,7 +6,11 @@ function [ids_equilibrium,ids_equilibrium_description,varargout] = tcv_get_ids_e
 % gdat_params: gdat_data.gdat_params to get all params passed from original call, in particular error_bar and cocos_out options
 %
 
-[ids_equilibrium, params_equilibrium] = tcv_ids_headpart(shot,ids_equil_empty,'equilibrium',varargin{:});
+if exist('gdat_params')
+  [ids_equilibrium, params_equilibrium] = tcv_ids_headpart(shot,ids_equil_empty,'equilibrium','gdat_params',gdat_params,varargin{:});
+else
+  [ids_equilibrium, params_equilibrium] = tcv_ids_headpart(shot,ids_equil_empty,'equilibrium',varargin{:});
+end
 
 % 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:
@@ -352,7 +356,6 @@ for it=1:numel(ids_equilibrium.time)
       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)
@@ -373,6 +376,18 @@ for it=1:numel(ids_equilibrium.time_slice)
   ids_equilibrium.time_slice{it}.constraints.x_point = {};
 end
 
+% special test matrix cocos transform
+% $$$ ldim1=129;
+% $$$ ldim2=257;
+% $$$ it=1;
+% $$$ ids_equilibrium.time_slice{it}.coordinate_system.grid_type.index = 13;
+% $$$ ids_equilibrium.time_slice{it}.coordinate_system.grid.dim1 = linspace(0,1,ldim1)';
+% $$$ ids_equilibrium.time_slice{it}.coordinate_system.grid.dim2 = linspace(0,2*pi,ldim2);
+% $$$ ids_equilibrium.time_slice{it}.coordinate_system.tensor_contravariant = 2.*ones(ldim1,ldim2,3,3);
+% $$$ ids_equilibrium.time_slice{it}.coordinate_system.tensor_covariant = 2.*ones(ldim1,ldim2,3,3);
+% $$$ ids_equilibrium.time_slice{it}.coordinate_system.g11_contravariant = 2.*ones(ldim1,ldim2,3,3);
+
+
 % cocos automatic transform
 if exist('ids_generic_cocos_nodes_transformation_symbolic') == 2
   [ids_equilibrium,cocoscoeff]=ids_generic_cocos_nodes_transformation_symbolic(ids_equilibrium,'equilibrium',gdat_params.cocos_in, ...
diff --git a/matlab/TCV_IMAS/tcv_get_ids_magnetics.m b/matlab/TCV_IMAS/tcv_get_ids_magnetics.m
index cecb76c6..bf1ea850 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_magnetics.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_magnetics.m
@@ -5,7 +5,12 @@ function [ids_magnetics,ids_magnetics_description,varargout] = tcv_get_ids_magne
 % gdat_params: gdat_data.gdat_params to get all params passed from original call, in particular error_bar options
 %
 
-[ids_magnetics, params_magnetics] = tcv_ids_headpart(shot, ids_magnetics_empty,'magnetics','homogeneous_time',0,varargin{:});
+if exist('gdat_params')
+  [ids_magnetics, params_magnetics] = tcv_ids_headpart(shot, ids_magnetics_empty,'magnetics','homogeneous_time',0, ...
+          'gdat_params',gdat_params,varargin{:});
+else
+  [ids_magnetics, params_magnetics] = tcv_ids_headpart(shot, ids_magnetics_empty,'magnetics','homogeneous_time',0,varargin{:});
+end
 
 [ids_magnetics.bpol_probe,ids_magnetics_description.bpol_probe]= tcv_ids_bpol_probe(params_magnetics.shot, ids_magnetics.bpol_probe(1),gdat_params);
 [ids_magnetics.flux_loop,ids_magnetics_description.flux_loop]= tcv_ids_flux_loop(params_magnetics.shot, ids_magnetics.flux_loop(1),gdat_params);
diff --git a/matlab/TCV_IMAS/tcv_get_ids_nbi.m b/matlab/TCV_IMAS/tcv_get_ids_nbi.m
index 073bf1d4..8da3b80b 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_nbi.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_nbi.m
@@ -6,7 +6,12 @@ function [ids_nbi,ids_nbi_description,varargout] = tcv_get_ids_nbi(shot,ids_nbi_
 % gdat_params: gdat_data.gdat_params to get all params passed from original call, in particular error_bar options
 %
 
-[ids_nbi, params_nbi] = tcv_ids_headpart(shot,ids_nbi_empty,'nbi','homogeneous_time',0,varargin{:});
+if exist('gdat_params')
+  [ids_nbi, params_nbi] = tcv_ids_headpart(shot,ids_nbi_empty,'nbi','homogeneous_time',0,'gdat_params',gdat_params,varargin{:});
+else
+  [ids_nbi, params_nbi] = tcv_ids_headpart(shot,ids_nbi_empty,'nbi','homogeneous_time',0,varargin{:});
+end
+
 ids_nbi_description='';
 % 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:
diff --git a/matlab/TCV_IMAS/tcv_get_ids_pf_active.m b/matlab/TCV_IMAS/tcv_get_ids_pf_active.m
index 4ffa647c..8d3ef3e2 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_pf_active.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_pf_active.m
@@ -4,7 +4,11 @@ function [ids_pf_active,ids_pf_active_description,varargout] = tcv_get_ids_pf_ac
 %
 
 % Input pharser
-[ids_pf_active, params] = tcv_ids_headpart(shot, ids_pf_active_empty,'pf_active','homogeneous_time',0,varargin{:});
+if exist('gdat_params')
+  [ids_pf_active, params] = tcv_ids_headpart(shot, ids_pf_active_empty,'pf_active','homogeneous_time',0,'gdat_params',gdat_params,varargin{:});
+else
+  [ids_pf_active, params] = tcv_ids_headpart(shot, ids_pf_active_empty,'pf_active','homogeneous_time',0,varargin{:});
+end
 
 % Get subfield
 [ids_pf_active.coil,ids_pf_active_description.coil]= tcv_ids_coil(params.shot, ids_pf_active.coil(1),gdat_params);
diff --git a/matlab/TCV_IMAS/tcv_get_ids_tf.m b/matlab/TCV_IMAS/tcv_get_ids_tf.m
index 03650ac4..2a5774ec 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_tf.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_tf.m
@@ -11,7 +11,11 @@ function [ids_tf,ids_tf_description] =  tcv_get_ids_tf(shot, ids_tf_empty, gdat_
 
 ids_tf = ids_tf_empty;
 
-[ids_tf, params_tf] = tcv_ids_headpart(shot, ids_tf_empty,'tf','homogeneous_time',0,varargin{:});
+if exist('gdat_params')
+  [ids_tf, params_tf] = tcv_ids_headpart(shot, ids_tf_empty,'tf','homogeneous_time',0,'gdat_params',gdat_params,varargin{:});
+else
+  [ids_tf, params_tf] = tcv_ids_headpart(shot, ids_tf_empty,'tf','homogeneous_time',0,varargin{:});
+end
 
 tmp = gdat_tcv(shot, 'b0');
 if ~ischar(tmp.data)
diff --git a/matlab/TCV_IMAS/tcv_get_ids_wall.m b/matlab/TCV_IMAS/tcv_get_ids_wall.m
index 506dc5c7..fca0dfa2 100644
--- a/matlab/TCV_IMAS/tcv_get_ids_wall.m
+++ b/matlab/TCV_IMAS/tcv_get_ids_wall.m
@@ -5,7 +5,11 @@ function [ids_wall,ids_wall_description,varargout] = tcv_get_ids_wall(shot, ids_
 %
 
 % Input pharser
-[ids_wall, params] = tcv_ids_headpart(shot, ids_wall_empty,'wall','homogeneous_time',0,varargin{:});
+if exist('gdat_params')
+  [ids_wall, params] = tcv_ids_headpart(shot, ids_wall_empty,'wall','homogeneous_time',0,'gdat_params',gdat_params,varargin{:});
+else
+  [ids_wall, params] = tcv_ids_headpart(shot, ids_wall_empty,'wall','homogeneous_time',0,varargin{:});
+end
 
 % Get data of outline
 [ids_wall.description_2d,ids_wall_description.description_2d]= tcv_ids_wall_description_2d(params.shot, ids_wall.description_2d(1));
diff --git a/matlab/TCV_IMAS/tcv_ids_bpol_probe.m b/matlab/TCV_IMAS/tcv_ids_bpol_probe.m
index 1eef311b..5027b8e8 100644
--- a/matlab/TCV_IMAS/tcv_ids_bpol_probe.m
+++ b/matlab/TCV_IMAS/tcv_ids_bpol_probe.m
@@ -53,7 +53,7 @@ for ii=1:Nprobes
 
     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;
+    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);
diff --git a/matlab/TCV_IMAS/tcv_ids_headpart.m b/matlab/TCV_IMAS/tcv_ids_headpart.m
index 90914a7a..911f2baf 100644
--- a/matlab/TCV_IMAS/tcv_ids_headpart.m
+++ b/matlab/TCV_IMAS/tcv_ids_headpart.m
@@ -7,8 +7,9 @@ function [ids_generic, params_ids_generic] = tcv_ids_headpart(shot,ids_in,ids_na
 %
 % varargin options:
 %
-% 'comment': comment to include in ids_properties
+% 'comment': comment to include in ids_properties, using gdat_params for example cocos_in and cocos_out
 % 'homogeneous_time': homogeneous_time in ids_properties: 1 (default) if the whole ids has same time, 0 otherwise
+% 'gdat_params': gdat params structure
 %
 %
 % example:
@@ -23,7 +24,8 @@ p = inputParser;
 p.addOptional('shot', [], @(x) (isnumeric(x) && isscalar(x) && (x == round(x)))); % integer
 p.addOptional('ids_in', struct([]), @(x) (isstruct(x)));
 p.addOptional('ids_name', '', @(x) (ischar(x))); % char
-p.addOptional('comment', 'default comment', @(x) isempty(x) || ischar(x)); % char
+p.addOptional('gdat_params', [], @(x) (isempty(x) || isstruct(x))); % char
+p.addOptional('comment', '', @(x) isempty(x) || ischar(x)); % char
 p.addOptional('homogeneous_time', 1, @(x) (isnumeric(x) && isscalar(x) && (x == round(x))));
 
 p.parse(shot,ids_in,ids_name);
@@ -39,6 +41,8 @@ names = fieldnames(params);
 mask = structfun(@isempty,params);
 if any(mask),
   params = rmfield(params,unique([names(mask); p.UsingDefaults.']));
+  p.parse(params);
+  params = p.Results;
 end
 
 params_ids_generic = params;
@@ -48,8 +52,55 @@ ids_generic = ids_in;
 %
 % ids_properties
 %
+subcall='';
+% $$$ if nargin>=5 && ~isempty(varargin)
+% $$$   for i=1:length(varargin)
+% $$$     if ~isempty(varargin{i})
+% $$$       if ischar(varargin{i})
+% $$$         subcall_add = [',' varargin{i}];
+% $$$       elseif isnumeric(varargin{i})
+% $$$         subcall_add = [',' num2str(varargin{i})];
+% $$$       elseif isstruct(varargin{i})
+% $$$         subcall_add = [',' 'struct_in'];
+% $$$       elseif iscell(varargin{i})
+% $$$         subcall_add = [',' 'cell_in'];
+% $$$       end
+% $$$     else
+% $$$       subcall_add = [',[]'];
+% $$$     end
+% $$$     subcall(end+1:end+length(subcall_add)) = subcall_add;
+% $$$   end
+% $$$   subcall(end+1:end+2) = '; ';
+% $$$ end
 ids_generic.ids_properties.comment = params_ids_generic.comment;
+if ~isempty(subcall)
+  add_to_comment = ['varargin: ' subcall];
+  if isempty(ids_generic.ids_properties.comment)
+    ids_generic.ids_properties.comment = add_to_comment;
+  else
+    ids_generic.ids_properties.comment(end+1:end+length(add_to_comment)+2) = ['; ' add_to_comment];
+  end
+end
+
+ids_generic.ids_properties.provider = ['tcv_get_ids_' ids_name];
+if ~isempty(params_ids_generic.gdat_params)
+  if isfield(params_ids_generic.gdat_params,'cocos_in') && isfield(params_ids_generic.gdat_params,'cocos_out')
+    comment_add = ['from TCV cocos_in=' num2str(params_ids_generic.gdat_params.cocos_in) ' transformed to cocos_out=' num2str(params_ids_generic.gdat_params.cocos_out)];
+    if isempty(ids_generic.ids_properties.comment)
+      ids_generic.ids_properties.comment = comment_add;
+    else
+      ids_generic.ids_properties.comment(end+1:end+length(comment_add)+2) = ['; ' comment_add];
+    end
+  else
+    warning('cocos_in and cocos_out expected to be fields of gdat_params')
+  end
+  if isfield(params_ids_generic.gdat_params,'error_bar')
+    provider_add = ['; error_bar option: ' params_ids_generic.gdat_params.error_bar];
+    ids_generic.ids_properties.provider(end+1:end+length(provider_add)) = provider_add;
+  else
+    warning('cocos_in and cocos_out expected to be fields of gdat_params')
+  end
+end
 ids_generic.ids_properties.homogeneous_time = params_ids_generic.homogeneous_time;
 ids_generic.ids_properties.source = ['TCV shot #' num2str(params_ids_generic.shot)];
-ids_generic.ids_properties.provider = ['tcv_get_ids_' ids_name];
 ids_generic.ids_properties.creation_date = date;
-- 
GitLab