Skip to content
Snippets Groups Projects
ids2database.m 2.6 KiB
Newer Older
function [ids_put_status] = ids2database(shot,run,occurence,ids2put,varargin);
%
%   [ids_put_status] = ids2database(shot,run,occurence,ids2put,varargin);
%
% open/create shot,run and put the ids from ids2put.idsnames (names from fieldnames(ids2put) && within IDS_list)
%
% varargin{1}: tbd
%
%
ids_put_status = 0;

% initialize input parser
p = inputParser;
% no required inputs here so one can call with empty args and get defaults parameters
% effectively required inputs should have defaults as empty
p.addOptional('shot', [], @(x) (isnumeric(x) && isscalar(x) && (x == round(x)))); % integer
p.addOptional('run', [], @(x) (isnumeric(x) && isscalar(x) && (x == round(x)))); % integer
p.addOptional('occurence', 0, @(x) isempty(x) || (isnumeric(x) && isscalar(x) && (x == round(x)))); % integer
p.addOptional('ids2put', struct([]), @(x) (isstruct(x)));

p.parse;
defaults_ids2database = p.Results; % to keep track of defaults

if nargin==1
  p.parse(shot);
  params = p.Results;
elseif nargin==2
  p.parse(shot,run);
  params = p.Results;
elseif nargin==3 && ~ischar(occurence)
  p.parse(shot,run,occurence);
  params = p.Results;
elseif nargin>=4
  p.parse(shot,run,occurence,varargin{:});
  params = p.Results;
else
  p.parse;
  p.Results
  return
end

% replace empty inputs with defaults
names = fieldnames(params);
mask = structfun(@isempty,params);
if any(mask),
  params = rmfield(params,unique([names(mask); p.UsingDefaults.']));
end

params_ids2database = params;

% check ids_names
ids_names=fieldnames(ids2put);
ids_full_list = IDS_list;
ids_names_notok = setdiff(ids_names,ids_full_list);
if ~isempty(ids_names_notok)
  disp(['these subfields are not ids names, so not used: ' sprintf('%s ',ids_names_notok{:})])
end
ids_names_ok = intersect(ids_full_list,ids_names);

if isempty(ids_names_ok)
  disp('no ids names available compatible with IDS_list')
  return
end

try
  %% Initialize the three
  shot_is_new = 1; % seems only create is ok even if does already exist
  mdsplustree0=getenv('MDSPLUS_TREE_BASE_0');
  [aa,bb]=unix(['ls ' mdsplustree0 '/*' num2str(shot) num2str(run,'%4.4d') '*.tree']);
  if aa==0
    shot_is_new = 0;
  end
  if shot_is_new
    idx  = imas_create('ids',shot,run,0,0); % 
  else
    idx  = imas_open('ids',shot,run); % 
  end

  %% Put the field
  for i=1:length(ids_names_ok)
    ids_to_get = ids_names_ok{i};
    if occurence <= 0
      ids_put(idx,ids_to_get,ids2put.(ids_to_get));
    else
      ids_put(idx,[ids_to_get '/' num2str(occurence)],ids2put.(ids_to_get));
    end
  end
  
  %% Close the file
  imas_close(idx)
catch ME
  disp('problems in putting data in database:')
  throw(ME)
  idx
  keyboard
end