function [ids_from_tcv,varargout] = tcv2ids2database(shot,run_out,varargin);
% 
% [ids_from_tcv,varargout] = tcv2ids2database(shot,run_out,varargin)
% 
% script to get TCV data as ids and then write them on the database
%
% Uses function tcv2ids and ids2database
% Assumes you connected to the right port like: mdsconnect('localhost:5555'); (if not tries that one)
%
% 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)
%             {'equilibrium', 'magnetics', 'tf', 'pf_active','wall','core_profiles'} or a subset
% 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
%                'delta_with_lower': as 'delta' but lower also set
%
% 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 = [];

% mdsconnect('localhost:5555');
aa=mdsvalue('1+2');
if isempty(aa) || aa ~= 3
  mdsconnect('localhost:5555');
  aa=mdsvalue('1+2');
  if isempty(aa) || aa ~= 3
    error('problem with mdsconnect?');
  end
end

% 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_out', [], @(x) (isnumeric(x) && isscalar(x) && (x == round(x)))); % integer
p.addOptional('occurence', 0, @(x) (isnumeric(x) && isscalar(x) && (x == round(x)))); % integer
p.addOptional('ids_names', {'equilibrium', 'magnetics', 'pf_active','wall', 'tf','core_profiles'}, @(x) isempty(x) | iscell(x)); % char or cell array
p.addOptional('error_bar', 'delta', @(x) isempty(x) | ischar(x) ); % char or cell array

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

if nargin == 1
  p.parse(shot);
  params = p.Results;
elseif nargin == 2
  p.parse(shot,run_out);
  params = p.Results;
elseif nargin > 2
  p.parse(shot,run_out,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.']));
  if ~isfield(params, 'shot') || isnan(params.shot)
    warning('No shot entered');
    return
  end
  p.parse(params.shot,rmfield(params,'shot'));
  params = p.Results;
end

ids_names_ok = params.ids_names;
for i=1:length(params.ids_names)
  ij = strcmp(params.ids_names{i},defaults_tcv2ids2database.ids_names);
  if ~any(ij)
    disp(['ids name: ' params.ids_names{i} ' is not available yet, ask O. Sauter'])
    ids_names_ok = setdiff(ids_names_ok,params.ids_names{i});
  end
end
params.ids_names = ids_names_ok;
params_tcv2ids2database = params;
params_not_in_tcv2ids = {'run_out','occurence'};
params_tcv2ids = rmfield(params_tcv2ids2database,params_not_in_tcv2ids);
[ids_from_tcv,idsok] = tcv2ids(shot,params_tcv2ids);
ids_from_tcv.params_tcv2ids2database = params_tcv2ids2database;
ids_from_tcv.params_tcv2ids2database
%% can plot with: [plotids_H] = plotids([],idsok.ids{1},idsok.ids{2});
if isfield(ids_from_tcv,'tf')
  % avoid present problem of not saving data if ids_from_tcv.tf.field_map{1}.time is not set
  if length(ids_from_tcv.tf.field_map)>0 && abs(ids_from_tcv.tf.field_map{1}.time+9.e40)<1e-5
    ids_from_tcv.tf.field_map{1}.time = -8.9000e+40;
    disp(['WARNING: ids_from_tcv.tf.field_map{1}.time changed to ' num2str(ids_from_tcv.tf.field_map{1}.time) ' to avoid writing error']);
  end
end
%%

[ids_put_status] = ids2database(shot,run_out,ids_from_tcv.params_tcv2ids2database.occurence,ids_from_tcv);

ids_from_tcv.ids_put_status = ids_put_status;

% [plotids_H] = plotids([],idsok.ids);

% tic;[TTTe,idse]=evalc('ids_get(expIdx, ''equilibrium'')');toc
% tic;[TTTm,idsm]=evalc('ids_get(expIdx, ''magnetics'')');toc
% tic;[TTTp,idsp]=evalc('ids_get(expIdx, ''pf_active'')');toc
% tic;[TTTt,idst]=evalc('ids_get(expIdx, ''tf'')');toc
% tic;[TTTw,idsw]=evalc('ids_get(expIdx, ''wall'')');toc
