From 7b6ab7f8d69094d0caaaf3251b02528def6c2ef9 Mon Sep 17 00:00:00 2001
From: Cenk Yildiz <cenk.yildiz@epfl.ch>
Date: Wed, 10 Jan 2024 14:01:11 +0100
Subject: [PATCH] Add possibility to chose hdf5 backend for saving ids data

* Add imas_backend option to 2 functions
* Use imas_open_env_backend instead of imas_create_env
---
 matlab/TCV_IMAS/ids2database.m     | 30 ++++++++++++++++++++++++------
 matlab/TCV_IMAS/tcv2ids2database.m |  9 +++++++--
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/matlab/TCV_IMAS/ids2database.m b/matlab/TCV_IMAS/ids2database.m
index 40f83746..eb2c2ed7 100644
--- a/matlab/TCV_IMAS/ids2database.m
+++ b/matlab/TCV_IMAS/ids2database.m
@@ -1,4 +1,4 @@
-function [ids_put_status] = ids2database(shot,run,occurence,ids2put,tree_user,tree_tokamak,tree_majorversion,varargin);
+function [ids_put_status] = ids2database(shot,run,occurence,ids2put,tree_user,tree_tokamak,tree_majorversion,imas_backend,varargin);
 %
 %   [ids_put_status] = ids2database(shot,run,occurence,ids2put,varargin);
 %
@@ -7,6 +7,7 @@ function [ids_put_status] = ids2database(shot,run,occurence,ids2put,tree_user,tr
 % tree_name: getenv('USER') by default, can be 'public'
 % tree_tokamak: 'tcv' by default
 % tree_majorversion: '3' by default
+% imas_backend: "MDSplus" or "HDF5"
 %
 % varargin{1}: tbd
 %
@@ -16,6 +17,7 @@ ids_put_status = 0;
 tree_user_default = getenv('USER');
 tree_tokamak_default = 'tcv';
 tree_majorversion_default = '3';
+imas_backend_default = 'MDSplus';
 
 % initialize input parser
 p = inputParser;
@@ -28,6 +30,7 @@ p.addOptional('ids2put', struct([]), @(x) (isstruct(x)));
 p.addOptional('tree_user', tree_user_default, @(x) (isempty(x) || ischar(x)));
 p.addOptional('tree_tokamak', tree_tokamak_default, @(x) (isempty(x) || ischar(x)));
 p.addOptional('tree_majorversion', tree_majorversion_default, @(x) (ischar(x)));
+p.addOptional('imas_backend', imas_backend_default, @(x) (isempty(x) || ischar(x)));
 
 p.parse;
 defaults_ids2database = p.Results; % to keep track of defaults
@@ -53,8 +56,11 @@ elseif nargin==6
 elseif nargin==7
   p.parse(shot,run,occurence,ids2put,tree_user,tree_tokamak,tree_majorversion);
   params = p.Results;
-elseif nargin>=8
-  p.parse(shot,run,occurence,ids2put,tree_user,tree_tokamak,tree_majorversion,varargin{:});
+elseif nargin==8
+  p.parse(shot,run,occurence,ids2put,tree_user,tree_tokamak,tree_majorversion,imas_backend);
+  params = p.Results;
+elseif nargin>=9
+  p.parse(shot,run,occurence,ids2put,tree_user,tree_tokamak,tree_majorversion,imas_backend,varargin{:});
   params = p.Results;
 else
   p.parse;
@@ -100,6 +106,19 @@ if isempty(ids_names_ok)
   return
 end
 
+if ~ismember(params_ids2database.imas_backend, {'MDSplus', 'HDF5'})
+    warning(['Unknown imas_backend: ', imas_backend, ', selecting default: ',imas_backend_default]);
+    params_ids2database.imas_backend = imas_backend_default;
+end
+
+% Get IMAS backend ID
+switch params_ids2database.imas_backend
+  case "HDF5"
+    backendid=13;
+  case "MDSplus"
+    backendid=12;
+end
+
 try
   %% Initialize the three
   shot_is_new = 1; % seems only create is ok even if does already exist
@@ -109,10 +128,9 @@ try
     shot_is_new = 0;
   end
   if shot_is_new
-    idx  = imas_create_env('ids',shot,run,0,0,params_ids2database.tree_user,params_ids2database.tree_tokamak, ...
-          params_ids2database.tree_majorversion); %
+    idx = imas_create_env_backend(shot, run, params_ids2database.tree_user,params_ids2database.tree_tokamak, params_ids2database.tree_majorversion, backendid)
   else
-    idx  = imas_open_env('ids',shot,run,params_ids2database.tree_user,params_ids2database.tree_tokamak,params_ids2database.tree_majorversion); %
+    idx  = imas_open_env_backend(shot,run,params_ids2database.tree_user,params_ids2database.tree_tokamak,params_ids2database.tree_majorversion,backendid); %
   end
 
   %% Put the field
diff --git a/matlab/TCV_IMAS/tcv2ids2database.m b/matlab/TCV_IMAS/tcv2ids2database.m
index 9166d42d..664175a4 100644
--- a/matlab/TCV_IMAS/tcv2ids2database.m
+++ b/matlab/TCV_IMAS/tcv2ids2database.m
@@ -10,6 +10,7 @@ function [ids_from_tcv,varargout] = tcv2ids2database(shot,run_out,varargin);
 % varargin: 'occurence': occurence value
 % varargin: 'ids_names': cell, ids to load, by default all defined so far (if empty or empty string or not given)
 %             see tcv_available_ids for the list of IDSs available for TCV
+% varargin: 'imas_backend': "MDSplus" or "HDF5"
 % varargin: 'error_bar': type (string)
 %                'delta' or empty (default): meaning difference in upper is set (standard error_bar
 %                'added': errorbar is added: upper=data+delta and lower=data-delta
@@ -27,6 +28,7 @@ function [ids_from_tcv,varargout] = tcv2ids2database(shot,run_out,varargin);
 % example:
 %   ids_from_tcv = tcv2ids2database(62745,9999,'ids_names',{'pf_active'},'error_bar','added'); % to test only one ids
 %   ids_from_tcv = tcv2ids2database(62745,9999,'error_bar','added'); % to get all default ids's and old errorbar type
+%   ids_from_tcv = tcv2ids2database(62745,9999,'ids_names',{'summary'},'imas_backend','HDF5'); % Save data in imas HDF5 backend instead of MDSplus
 %
 
 ids_from_tcv = [];
@@ -50,6 +52,7 @@ p.addOptional('run_out', [], @(x) (isnumeric(x) && isscalar(x) && (x == round(x)
 p.addOptional('occurence', 0, @(x) isempty(x) || (isnumeric(x) && isscalar(x) && (x == round(x)))); % integer
 ids_names = tcv_available_ids;
 p.addOptional('ids_names', ids_names, @(x) isempty(x) || iscell(x)); % char or cell array
+p.addOptional('imas_backend', 'MDSplus', @(x) isempty(x) || ischar(x));
 p.addOptional('error_bar', 'delta', @(x) isempty(x) || ischar(x) ); % char or cell array
 p.addOptional('tree_user', getenv('USER'), @(x) isempty(x) || ischar(x) ); % char
 p.addOptional('tree_tokamak', 'tcv', @(x) isempty(x) || ischar(x) ); % char
@@ -101,8 +104,9 @@ for i=1:length(params.ids_names)
 end
 params.ids_names = ids_names_ok;
 params_tcv2ids2database = params;
-params_not_in_tcv2ids = {'run_out','occurence','tree_user','tree_tokamak','tree_majorversion'};
+params_not_in_tcv2ids = {'run_out','occurence','tree_user','tree_tokamak','tree_majorversion','imas_backend'};
 params_tcv2ids = rmfield(params_tcv2ids2database,params_not_in_tcv2ids);
+
 [ids_from_tcv,idsok] = tcv2ids(shot,params_tcv2ids);
 ids_from_tcv.params_tcv2ids2database = params_tcv2ids2database;
 
@@ -128,7 +132,8 @@ end
 %%
 
 [ids_put_status] = ids2database(shot,run_out,ids_from_tcv.params_tcv2ids2database.occurence,ids_from_tcv, ...
-          params_tcv2ids2database.tree_user,params_tcv2ids2database.tree_tokamak,params_tcv2ids2database.tree_majorversion);
+          params_tcv2ids2database.tree_user,params_tcv2ids2database.tree_tokamak,params_tcv2ids2database.tree_majorversion, ...
+          params_tcv2ids2database.imas_backend);
 
 ids_from_tcv.ids_put_status = ids_put_status;
 
-- 
GitLab