diff --git a/deploy/packaging/debian/matlab.noarch b/deploy/packaging/debian/matlab.noarch index cf2b52d47d..be711e89f8 100644 --- a/deploy/packaging/debian/matlab.noarch +++ b/deploy/packaging/debian/matlab.noarch @@ -1,17 +1,24 @@ -./usr/local/mdsplus/matlab/MDSarg.m -./usr/local/mdsplus/matlab/MDSplusAPI.m -./usr/local/mdsplus/matlab/NATIVEvalue.m -./usr/local/mdsplus/matlab/mdsFromMatlab.m -./usr/local/mdsplus/matlab/mdsInfo.m -./usr/local/mdsplus/matlab/mdsToMatlab.m -./usr/local/mdsplus/matlab/mdsUseLegacy.m -./usr/local/mdsplus/matlab/mdsUsePython.m ./usr/local/mdsplus/matlab/mdsclose.m ./usr/local/mdsplus/matlab/mdsconnect.m ./usr/local/mdsplus/matlab/mdsdisconnect.m +./usr/local/mdsplus/matlab/mdsFromMatlab.m ./usr/local/mdsplus/matlab/mdsgetmsg.m +./usr/local/mdsplus/matlab/mdsInfo.m ./usr/local/mdsplus/matlab/mdsopen.m +./usr/local/mdsplus/matlab/MDSplusAPI.m ./usr/local/mdsplus/matlab/mdsput.m ./usr/local/mdsplus/matlab/mdstcl.m ./usr/local/mdsplus/matlab/mdstest.m +./usr/local/mdsplus/matlab/mdsToMatlab.m +./usr/local/mdsplus/matlab/mdsUsePython.m ./usr/local/mdsplus/matlab/mdsvalue.m +./usr/local/mdsplus/matlab/private/javaActivate.m +./usr/local/mdsplus/matlab/private/javaConnect.m +./usr/local/mdsplus/matlab/private/javaExecute.m +./usr/local/mdsplus/matlab/private/javaFromMatlab.m +./usr/local/mdsplus/matlab/private/javaToMatlab.m +./usr/local/mdsplus/matlab/private/pythonActivate.m +./usr/local/mdsplus/matlab/private/pythonConnect.m +./usr/local/mdsplus/matlab/private/pythonExecute.m +./usr/local/mdsplus/matlab/private/pythonFromMatlab.m +./usr/local/mdsplus/matlab/private/pythonToMatlab.m \ No newline at end of file diff --git a/deploy/packaging/redhat/matlab.noarch b/deploy/packaging/redhat/matlab.noarch index 564908cecf..d7b13e2571 100644 --- a/deploy/packaging/redhat/matlab.noarch +++ b/deploy/packaging/redhat/matlab.noarch @@ -1,18 +1,26 @@ ./usr/local/mdsplus/matlab -./usr/local/mdsplus/matlab/MDSarg.m -./usr/local/mdsplus/matlab/MDSplusAPI.m -./usr/local/mdsplus/matlab/NATIVEvalue.m -./usr/local/mdsplus/matlab/mdsFromMatlab.m -./usr/local/mdsplus/matlab/mdsInfo.m -./usr/local/mdsplus/matlab/mdsToMatlab.m -./usr/local/mdsplus/matlab/mdsUseLegacy.m -./usr/local/mdsplus/matlab/mdsUsePython.m ./usr/local/mdsplus/matlab/mdsclose.m ./usr/local/mdsplus/matlab/mdsconnect.m ./usr/local/mdsplus/matlab/mdsdisconnect.m +./usr/local/mdsplus/matlab/mdsFromMatlab.m ./usr/local/mdsplus/matlab/mdsgetmsg.m +./usr/local/mdsplus/matlab/mdsInfo.m ./usr/local/mdsplus/matlab/mdsopen.m +./usr/local/mdsplus/matlab/MDSplusAPI.m ./usr/local/mdsplus/matlab/mdsput.m ./usr/local/mdsplus/matlab/mdstcl.m ./usr/local/mdsplus/matlab/mdstest.m +./usr/local/mdsplus/matlab/mdsToMatlab.m +./usr/local/mdsplus/matlab/mdsUsePython.m ./usr/local/mdsplus/matlab/mdsvalue.m +./usr/local/mdsplus/matlab/private +./usr/local/mdsplus/matlab/private/javaActivate.m +./usr/local/mdsplus/matlab/private/javaConnect.m +./usr/local/mdsplus/matlab/private/javaExecute.m +./usr/local/mdsplus/matlab/private/javaFromMatlab.m +./usr/local/mdsplus/matlab/private/javaToMatlab.m +./usr/local/mdsplus/matlab/private/pythonActivate.m +./usr/local/mdsplus/matlab/private/pythonConnect.m +./usr/local/mdsplus/matlab/private/pythonExecute.m +./usr/local/mdsplus/matlab/private/pythonFromMatlab.m +./usr/local/mdsplus/matlab/private/pythonToMatlab.m \ No newline at end of file diff --git a/matlab/MDSarg.m b/matlab/MDSarg.m deleted file mode 100644 index 3ae4ff8945..0000000000 --- a/matlab/MDSarg.m +++ /dev/null @@ -1,8 +0,0 @@ -function obj = MDSarg( value ) -% MDSARG deprecated in favour of mdsFromMatlab. -% This function is deprecated. please use mdsFromMatlab instead - warning('MDSplus:Legacy',... - 'The MDSarg function is deprecated. Please use mdsFromMatlab instead') - obj = mdsFromMatlab(value); -end - diff --git a/matlab/MDSplusAPI.m b/matlab/MDSplusAPI.m index d5314cff6d..376530be1b 100644 --- a/matlab/MDSplusAPI.m +++ b/matlab/MDSplusAPI.m @@ -1,13 +1,18 @@ classdef MDSplusAPI < handle % MDSplusAPI new API core for MDSplus -% % e.g.: read signal from remote tree on mds-data-1 using thick client -% setenv('default_tree_path', 'mds-data-1::') -% mdsip = MDSplusAPI(); -% mdsip.open('mytree', 7357); -% sig = mdsip.tree.getNode('.signals:channel43').getRecord(); -% dat = sig.toFloatArray(); -% dim = sig.getDimension().toFloatArray(); -% plot(dim, dat) +% e.g.: +% - read signal from remote tree on mds-data-1 using thick client +% setenv('default_tree_path', 'mds-data-1::') +% mdsip = MDSplusAPI(); +% mdsip.open('mytree', 7357); +% sig = mdsip.tree.getNode('.signals:channel43').getRecord(); +% dat = sig.toFloatArray(); +% dim = sig.getDimension().toFloatArray(); +% plot(dim, dat) +% - using thin client connection to mds-data-1 +% mdsip = MDSplusAPI('mds-data-1') +% ... + properties(SetAccess = private) con; % java object of the connection api; % java object of the api @@ -25,7 +30,7 @@ % NULL convenient way to create Descriptor[] % arr = mdsip.toArray(mdsip.compile('1UQ')); arr = javaArray('mds.data.descriptor.Descriptor', nargin); - for i = 1:nargin + for i = 1 : nargin arr(i) = varargin{i}; end end @@ -93,7 +98,7 @@ function close(self) end function obj = evaluate(self, obj) % EVALUATE evaluate mdsplus object - % obj = mdsip.evaluate(obj_in); + % obj = mdsip.evaluate(obj_in); data = self.api.tdiEvaluate(self.tree, obj); obj = data.getData(); end diff --git a/matlab/NATIVEvalue.m b/matlab/NATIVEvalue.m deleted file mode 100644 index df62839271..0000000000 --- a/matlab/NATIVEvalue.m +++ /dev/null @@ -1,7 +0,0 @@ -function obj = NATIVEvalue( value ) -% NATIVEVALUE deprecated in favour of mdsToMatlab -% This function is deprecated. please use mdsToMatlab instead - warning('MDSplus:Legacy',... - 'The NATIVEvalue function is deprecated. Please use mdsToMatlab instead') - obj = mdsToMatlab(value); -end diff --git a/matlab/mdsFromMatlab.m b/matlab/mdsFromMatlab.m index 92c9f2f511..f48dbef355 100755 --- a/matlab/mdsFromMatlab.m +++ b/matlab/mdsFromMatlab.m @@ -4,85 +4,12 @@ % argument. % % syntax: -% mdsresult=mdsFromMatlab(matlab-native-object) +% mdsresult = mdsFromMatlab(matlab_native_object) % info = mdsInfo(); if info.usePython - result = matlab2python(value, info); + result = pythonFromMatlab(value, info); else - result = matlab2java(value, info); - end -end - -function result = matlab2python(value, info) - dtype=class(value); - switch dtype - case 'char' - result = value; - otherwise - switch dtype - case 'single' - dtype='float32'; - case 'double' - dtype='float64'; - case 'cell' - dtype='str'; - end - if info.ispy2 && contains(dtype,'int64') - warning('MDSplus:python2_int64','%s\n',... - 'Loss of precision: python2 does not support int64 properly. Try python3 instead.',... - 'You can disable this warning with "warning(''off'', ''MDSplus:python2_int64'')"') - value = double(value); - end - if isscalar(value) - f=str2func(strcat('py.numpy.',dtype)); - result=f(value); - else - sz = size(value); - result = py.numpy.array(reshape(value,int32(1),prod(int32(sz))),dtype); - if not(numel(sz) == 2 && sz(2) == 1) - result = py.numpy.resize(result, flip(int32(sz))); - result = py.numpy.ascontiguousarray(result); - end - end - end -end - -function result = matlab2java(value, ~) - switch class(value) - case 'double' - javaclass = 'MDSplus.Float64'; - case 'single' - javaclass = 'MDSplus.Float32'; - case 'int64' - javaclass = 'MDSplus.Int64'; - case 'uint64' - javaclass = 'MDSplus.Uint64'; - case 'int32' - javaclass = 'MDSplus.Int32'; - case 'uint32' - javaclass = 'MDSplus.Uint32'; - case 'int16' - javaclass = 'MDSplus.Int16'; - case 'uint16' - javaclass = 'MDSplus.Uint16'; - case 'int8' - javaclass = 'MDSplus.Int8'; - case 'uint8' - javaclass = 'MDSplus.Uint8'; - case 'cell' - javaclass = 'MDSplus.String'; - case 'char' - result = javaObject('MDSplus.String',value); - return - otherwise - result = value; - return - end - sz = size(value); - if isequal(sz, [1,1]) - result = javaObject(javaclass,value); - else - result = javaObject(strcat(javaclass,'Array'),reshape(value,[],1),sz); + result = javaFromMatlab(value, info); end end diff --git a/matlab/mdsInfo.m b/matlab/mdsInfo.m index dff6335e94..efbebbcfc9 100644 --- a/matlab/mdsInfo.m +++ b/matlab/mdsInfo.m @@ -1,111 +1,54 @@ function info = mdsInfo(varargin) % MDSINFO used internally by other functions to retrieve configuration info - global MDSINFO - if ~isempty(MDSINFO) - info = MDSINFO; - else - l = getenv('MDSplus_legacy_behavior'); - if strcmp(l,'yes') - MDSplus_legacy_behavior = true; - else - MDSplus_legacy_behavior = false; - end - info.isConnected = false; - info.connection = []; - info.connectedHost = ''; - info.usePython = false; - info.isPythonConnection = false; - info.useLegacy = MDSplus_legacy_behavior; - MDSINFO = info; - end - if nargin > 0 - MDSINFO.usePython = logical(varargin{1}); - end - err = {true,true}; - if MDSINFO.usePython - err{1} = setPython(); - if ~islogical(err{1}) - MDSINFO.usePython = false; - err{2} = setJavaMds(); - end - else - err{2} = setJavaMds(); - if ~islogical(err{2}) - MDSINFO.usePython = true; - err{1} = setPython(); - end +global MDSINFO +if ~isempty(MDSINFO) + info = MDSINFO; +else + info.isConnected = false; + info.connection = []; + info.connectedHost = ''; + info.usePython = false; + info.isPythonConnection = false; + MDSINFO = info; +end +if nargin > 0 + MDSINFO.usePython = logical(varargin{1}); +end +err = {true, true}; +if MDSINFO.usePython + try + err{1} = pythonActivate(); + catch e + err{1} = e; end - ok = cellfun(@islogical,err); - if ~all(ok) - if ~any(ok) - disp('Unable to connect to MDSplus using either a java bridge or a python bridge') - disp(strcat(' Java error: ',err{2}.message)) - disp(strcat(' Python error: ',err{1}.message)) - info = []; - MDSINFO = info; - elseif ~ok(2) - disp('Unable to connect to MDSplus using java bridge, using python bridge instead') - disp(strcat(' Java error: ',err{2}.message)) - else % ~ok(1) - disp('Unable to connect to MDSplus using python bridge, using python bridge instead') - disp(strcat(' Python error: ',err{1}.message)) - end + if ~islogical(err{1}) + MDSINFO.usePython = false; + err{2} = javaActivate(); end -end - -function err = setPython() - global MDSINFO - persistent cache - if ~isempty(cache) - err = cache; - else +else + err{2} = javaActivate(); + if ~islogical(err{2}) + MDSINFO.usePython = true; try - MDSINFO.ispy2 = logical(py.MDSplus.version.ispy2); - err = false; - cache = err; - catch err + err{1} = pythonActivate(); + catch e + err{1} = e; end end end - -function err = setJavaMds() - persistent cache - if ~isempty(cache) - err = cache; - else - isOctave = (exist ('OCTAVE_VERSION', 'builtin') > 0); - try - MDSPLUS_DIR = getenv('MDSPLUS_DIR'); - if ismac - lib = strcat(MDSPLUS_DIR,'/lib/libJavaMds.dylib'); - elseif isunix - lib = strcat(MDSPLUS_DIR,'/lib/libJavaMds.so'); - elseif ispc - lib = 'C:\WINDOWS\SYSTEM32\JavaMds.dll'; - if ~isfile(lib) - if strcmp(computer('arch'),'win64') - lib = fullfile(MDSPLUS_DIR,'bin64','JavaMds.dll'); - else - lib = fullfile(MDSPLUS_DIR,'bin32','JavaMds.dll'); - end - end - end - if not(isOctave) - java.lang.System.setProperty('JavaMdsLib',lib); - end - p=javaclasspath('-all'); - matches = strfind(p,'mdsobjects.jar'); - if not(any(vertcat(matches{:}))) - javaaddpath(fullfile(MDSPLUS_DIR,'java','classes','mdsobjects.jar')) - end - if isOctave - javaObject('MDSplus.Int32',1); - else - MDSplus.Data.execute('1',javaArray('MDSplus.Data',1)); - end - err = false; - cache = err; - catch err - end +ok = cellfun(@islogical, err); +if ~all(ok) + if ~any(ok) + disp('Unable to connect to MDSplus using either a java bridge or a python bridge') + disp(strcat(' Java error: ', err{2}.message)) + disp(strcat(' Python error: ', err{1}.message)) + info = []; + MDSINFO = info; + elseif ~ok(2) + disp('Unable to connect to MDSplus using java bridge, using python bridge instead') + disp(strcat(' Java error: ', err{2}.message)) + else % ~ok(1) + disp('Unable to connect to MDSplus using python bridge, using python bridge instead') + disp(strcat(' Python error: ', err{1}.message)) end -end \ No newline at end of file +end diff --git a/matlab/mdsToMatlab.m b/matlab/mdsToMatlab.m index ae848c2372..5981a0cb50 100755 --- a/matlab/mdsToMatlab.m +++ b/matlab/mdsToMatlab.m @@ -8,138 +8,8 @@ % answer = mdsToMatlab(mdsnode.getData) info = mdsInfo(); if info.usePython - result = python2matlab(mdsthing, info); + result = pythonToMatlab(mdsthing, info); else - result = java2matlab(mdsthing, info); - end -end - -function result = python2matlab(value, info) - if strncmp(class(value),'py.MDSplus', 10) - value = value.data(); - end - if ~strncmp(class(value),'py.numpy', 8) - result = value; - else - dtype = char(value.dtype.name); - if contains(dtype,'str') || contains(dtype,'bytes') - if value.ndim == 0 - result = char(value); - else - strs = value.flatten.tolist(); - result{length(strs)} = ''; - for idx = 1 : length(strs) - result{idx} = char(py.MDSplus.version.tostr(strs{idx})); - end - shape = flip(int64(py.array.array('i', value.shape))); - if numel(shape) == 1 - shape = [shape, int64(1)]; - end - result = reshape(result, shape); - end - else - switch dtype - case 'float32' - dtype='single'; - case 'float64' - dtype='double'; - end - f=str2func(dtype); - dtype = value.dtype.char; - if info.ispy2 && contains('Qq',char(dtype)) - warning('MDSplus:python2_int64','%s\n',... - 'Loss of precision: python2 does not support int64 properly. Try python3 instead.',... - 'You can disable this warning with "warning(''off'', ''MDSplus:python2_int64'')"') - dtype='d'; - end - if value.ndim > 1 - shape = int64(py.array.array('i',value.shape)); - if ~py.numpy.isfortran(value) - shape = flip(shape); - end - value = py.numpy.asfortranarray(value); - value = py.array.array(dtype,value.flat); - result = reshape(f(value),shape); - else - if value.ndim > 0 - result=reshape(f(py.array.array(dtype,value)),[int64(value.shape{1}),1]); - else - result=f(py.array.array(dtype,{value})); - end - end - end - end -end - -function result = java2matlab(mdsthing, info) - if ~strncmp(class(mdsthing),'MDSplus',7) - result = mdsthing; - else - if numel(mdsthing.getShape()) > 0 - shape = mdsthing.getShape; - if numel(shape) == 1 - shape = [shape, 1]; - else - shape = shape'; - end - switch class(mdsthing) - case 'MDSplus.Int64Array' - result = reshape(mdsthing.getLongArray, shape); - case 'MDSplus.Uint64Array' - result = reshape(typecast(mdsthing.getLongArray, 'uint64'), shape); - case 'MDSplus.Int32Array' - result = reshape(mdsthing.getIntArray, shape); - case 'MDSplus.Uint32Array' - result = reshape(typecast(mdsthing.getIntArray, 'uint32'), shape); - case 'MDSplus.Int16Array' - result = reshape(mdsthing.getShortArray, shape); - case 'MDSplus.Uint16Array' - result = reshape(typecast(mdsthing.getShortArray, 'uint16'), shape); - case 'MDSplus.Int8Array' - result = reshape(mdsthing.getByteArray, shape); - case 'MDSplus.Uint8Array' - result = reshape(typecast(mdsthing.getByteArray, 'uint8'), shape); - case 'MDSplus.Float64Array' - result = double(reshape(mdsthing.getDoubleArray, shape)); - case 'MDSplus.Float32Array' - result = reshape(mdsthing.getFloatArray, shape); - case 'MDSplus.StringArray' - result = reshape(cellstr(char(mdsthing.getStringArray)),shape); - otherwise - throw(MException('MDSplus:mdsToMatlab', 'class %s not supported by mdsToMatlab function\n', class(mdsthing))); - end - else - switch class(mdsthing) - case 'MDSplus.Int64' - result = mdsthing.getLongArray(); - case 'MDSplus.Uint64' - result = typecast(mdsthing.getLongArray(), 'uint64'); - case 'MDSplus.Int32' - result = int32(mdsthing.getIntArray()); - case 'MDSplus.Uint32' - result = typecast(mdsthing.getIntArray(), 'uint32'); - case 'MDSplus.Int16' - result = mdsthing.getShortArray(); - case 'MDSplus.Uint16' - result = typecast(mdsthing.getShortArray(), 'uint16'); - case 'MDSplus.Int8' - result = mdsthing.getByteArray(); - case 'MDSplus.Uint8' - result = typecast(mdsthing.getByteArray(), 'uint8'); - case 'MDSplus.Float64' - result = mdsthing.getDoubleArray(); - case 'MDSplus.Float32' - result = mdsthing.getFloatArray(); - case 'MDSplus.String' - result = char(mdsthing.getString()); - otherwise - throw(MException('MDSplus:mdsToMatlab', 'class %s not supported by mdsToMatlab function\n', class(mdsthing))); - end - end - if info.useLegacy - if ~ischar(result) && ~iscell(result) - result = double(result); - end - end + result = javaToMatlab(mdsthing); end end diff --git a/matlab/mdsUseLegacy.m b/matlab/mdsUseLegacy.m deleted file mode 100644 index afa5996266..0000000000 --- a/matlab/mdsUseLegacy.m +++ /dev/null @@ -1,29 +0,0 @@ -function mdsUseLegacy( varargin ) -% MDSUSELEGACY enable (or disable) the use of the legacy mode. -% This function is can be used to enable legacy mode. -% In legacy mode floating point values will always be returned as doubles. -% Also the warnings about the deprecated functions will be disabled. -% Legacy mode will be enabled if mdsUseLegacy is called with no arguments. -% If you want to turn off legacy mode use mdsUseLegacy(false). -% This is not a permanent setting and only applies to the current -% invocation of matlab. - mdsInfo(); - global MDSINFO - if nargin == 0 - MDSINFO.useLegacy = true; - else - MDSINFO.useLegacy = varargin{1}; - end - if MDSINFO.useLegacy - fprintf('%s\n',... - 'Legacy mode enabled. Floating point values will be returned',... - 'as doubles and MDSarg and NATIVEvalue will not display',... - 'deprecation warnings. It is recommended that you modify',... - 'your code to not depend on this legacy mode as it will',... - 'eventually be deprecated.') - warning('off', 'MDSplus:Legacy') - else - warning('on', 'MDSplus:Legacy') - end -end - diff --git a/matlab/mdsclose.m b/matlab/mdsclose.m index 02d9e31947..c253c99b93 100755 --- a/matlab/mdsclose.m +++ b/matlab/mdsclose.m @@ -1,8 +1,8 @@ function [ status ] = mdsclose() -% MDSCLOSE - closes currently active tree -% +% MDSCLOSE - closes currently active tree +% % This routine will close the tree at the top of the open tree stack. -% +% % Previous incarnations also disconnected thin-client (mdsconnect) connections. % THAT IS NO LONGER DONE % @@ -12,11 +12,10 @@ try info.connection.get('TreeClose()'); catch err - status=0; + status = 0; err.message end else status = mdsvalue('TreeClose()'); end end - diff --git a/matlab/mdsconnect.m b/matlab/mdsconnect.m index 1a04ef3698..723104098e 100755 --- a/matlab/mdsconnect.m +++ b/matlab/mdsconnect.m @@ -13,7 +13,7 @@ if MDSINFO.isConnected && strcmp(MDSINFO.connectedHost, host) && MDSINFO.usePython == MDSINFO.isPythonConnection status = 1; else - if strcmpi(host,'LOCAL') == 1 + if strcmpi(host, 'LOCAL') == 1 MDSINFO.isConnected = false; MDSINFO.connection = []; MDSINFO.connectedHost = host; @@ -21,14 +21,13 @@ else try if MDSINFO.usePython - py_MDSplus_Connection=str2func('py.MDSplus.Connection'); - MDSINFO.connection=py_MDSplus_Connection(host); + MDSINFO.connection = pythonConnect(host); else - MDSINFO.connection=javaObject('MDSplus.Connection',host); + MDSINFO.connection = javaConnect(host); end - MDSINFO.isPythonConnection=MDSINFO.usePython; - MDSINFO.connectedHost=host; - MDSINFO.isConnected=true; + MDSINFO.isPythonConnection = MDSINFO.usePython; + MDSINFO.connectedHost = host; + MDSINFO.isConnected = true; status = 1; catch status = -1; @@ -36,4 +35,3 @@ end end end - diff --git a/matlab/mdsgetmsg.m b/matlab/mdsgetmsg.m index 7f47f42728..16f892792d 100755 --- a/matlab/mdsgetmsg.m +++ b/matlab/mdsgetmsg.m @@ -1,4 +1,4 @@ -function msg = mdsgetmsg(id,throwerror,except) +function msg = mdsgetmsg(id, throwerror, except) % MDSGETMSG used to message for status code % msg = mdsgetmsg(id, [throwerror, except]) % id : message id @@ -6,29 +6,28 @@ % except : exception list of ids : [] % % translates the tdi message ID into the tdi message MSG -% - If THROWERROR is true, an error with the tdi message is thrown, +% -If THROWERROR is true, an error with the tdi message is thrown, % unless the ID is a member of the EXCEPT list. -% - If THROWERROR is false, no error will be thrown, +% -If THROWERROR is false, no error will be thrown, % unless the ID is a member of the EXCEPT list. - narginchk(1,3) - if nargin<3 - except = []; - if nargin<2 - throwerror = false; - end +narginchk(1, 3) +if nargin<3 + except = []; + if nargin<2 + throwerror = false; end +end - if ischar(id) - error(id); - else - try - msg = mdsvalue('_msg = REPEAT(0,255);MdsShr->MdsGetMsgDsc(val($),descr(_msg));TRIM(_msg)',id); - catch - msg = sprintf('message not found: 0x%08x',id); - end - if xor(throwerror,ismember(id, except)) - error(msg) - end +if ischar(id) + error(id); +else + try + msg = mdsvalue('_=REPEAT(0,255);MdsShr->MdsGetMsgDsc(val($),descr(_));TRIM(_)', id); + catch + msg = sprintf('message not found: 0x%08x', id); + end + if xor(throwerror, ismember(id, except)) + error(msg) end end \ No newline at end of file diff --git a/matlab/mdsopen.m b/matlab/mdsopen.m index df4f17a349..13b0c2e797 100755 --- a/matlab/mdsopen.m +++ b/matlab/mdsopen.m @@ -1,34 +1,33 @@ -function [ shoto,status ] = mdsopen( expt, shot ) -% MDSOPEN opend connect to a remote mdsplus data server. +function [ shoto, status ] = mdsopen( expt, shot ) +% MDSOPEN opend connect to a remote mdsplus data server. % This routine will invoke a treeopen(expt, shot) % expt may contain information about a remote server 'server::expt' status = 1; shoto = 'Failed'; - idx=strfind(expt, '::'); + idx = strfind(expt, '::'); if ~isempty(idx) - host = expt(1:idx-1); - ltree = expt(idx+2:end); + host = expt(1 : idx - 1); + ltree = expt(idx + 2 : end); status = mdsconnect(host); else - ltree=expt; + ltree = expt; end - if mod(status,2) - info=mdsInfo(); + if mod(status, 2) + info = mdsInfo(); if info.isConnected try - info.connection.openTree(ltree,int32(shot)); + info.connection.openTree(ltree, int32(shot)); shoto = mdsToMatlab(info.connection.get('$shot')); catch err - status=0; - shoto=err.message; + status = 0; + shoto = err.message; end else - status = mdsvalue('TreeOpen($,$)',ltree,int32(shot)); - if mod(status,2) + status = mdsvalue('TreeOpen($, $)', ltree, int32(shot)); + if mod(status, 2) shoto = mdsvalue('$shot'); end end end end - diff --git a/matlab/mdsput.m b/matlab/mdsput.m index c8af1bd7cb..5d03c05d55 100755 --- a/matlab/mdsput.m +++ b/matlab/mdsput.m @@ -1,9 +1,5 @@ function [ status ] = mdsput( node, expression, varargin) % MDSPUT put data into MDSplus tree node % This routine invokes treeput(node, expression, ...) -info = mdsInfo(); -if nargin == 2 && info.useLegacy - status = mdsput(node,'$',expression); -else - status = mdsvalue(sprintf('treeput($,$%s)',repmat(',$',1,nargin-2)),node,expression,varargin{:}); -end +status = mdsvalue(sprintf('treeput($, $%s)', repmat(', $', 1, nargin - 2)), node, expression, varargin{:}); + diff --git a/matlab/mdstcl.m b/matlab/mdstcl.m index ede1f8796f..650e9b5547 100755 --- a/matlab/mdstcl.m +++ b/matlab/mdstcl.m @@ -9,7 +9,7 @@ function mdstcl(command) % prompting for, and executing TCL commands, and outputting the responses, % until 'exit' is entered. - if (exist('command','var')) + if (exist('command', 'var')) mdsvalue('tcl($, _response)', command); response = char(mdsvalue('_response')); if (~isempty(response)) @@ -19,10 +19,10 @@ function mdstcl(command) end while 1 - tclcmd = input('TCL> ','s'); + tclcmd = input('TCL> ', 's'); if (isempty(tclcmd)) continue; - elseif (any(strcmpi(tclcmd,{'ex','exi','exit'}))) + elseif (any(strcmpi(tclcmd, {'ex', 'exi', 'exit'}))) return; else mdsvalue('tcl($, _response)', tclcmd); diff --git a/matlab/mdstest.m b/matlab/mdstest.m index 312a9d629a..673d56a10c 100644 --- a/matlab/mdstest.m +++ b/matlab/mdstest.m @@ -1,5 +1,5 @@ function result = mdstest(varargin) -% MDSTEST test the MDSplus - MATLAB API (types and shapes) +% MDSTEST test the MDSplus-MATLAB API (types and shapes) % This routine tests the various functions in the MDSplus % Matlab/Octave interface. % mdstest() % tests current bridge @@ -7,63 +7,59 @@ % mdstest(1) % tests python bridge mdsInfo(varargin{:}); % update MDSINFO - result=mdscheck('1BU','uint8',[1,1]); - result=result && mdscheck('1WU','uint16',[1,1]); - result=result && mdscheck('1LU','uint32',[1,1]); - result=result && mdscheck('100000000000QU','uint64',[1,1]); - result=result && mdscheck('1.','single',[1,1]); - result=result && mdscheck('1D0','double',[1,1]); - result=result && mdscheck('"string test"','char',[1,11]); - result=result && mdscheck('BYTE_UNSIGNED(1 : 100)','uint8',[100,1]); - result=result && mdscheck('WORD_UNSIGNED(1 : 100)','uint16',[100,1]); - result=result && mdscheck('LONG_UNSIGNED(1 : 100)','uint32',[100,1]); - result=result && mdscheck('BYTE(1 : 100)','int8',[100,1]); - result=result && mdscheck('WORD(1 : 100)','int16',[100,1]); - result=result && mdscheck('1 : 100','int32',[100,1]); - result=result && mdscheck('QUADWORD(1 : 100)','int64',[100,1]); - result=result && mdscheck('set_range(10,2,5,1 : 100)','int32',[10,2,5]); - result=result && mdscheck('FS_FLOAT(1 : 100)','single',[100,1]); - result=result && mdscheck('FT_FLOAT(1 : 100)','double',[100,1]); - result=result && mdscheck('set_range(10,2,5,FT_FLOAT(1 : 100))','double',[10,2,5]); - result=result && mdscheck('$ : $','int32',[100,1],int32(1),int32(100)); - result=result && mdscheck('$ == $','uint8',[1,1],1,2); - result=result && mdscheck('QUADWORD_UNSIGNED(1 : 100)','uint64',[100,1]); - result=result && mdscheck('["a","b","c","d"]','cell',[4,1]); - result=result && mdscheck('set_range(2,3,["a","b","c long string","d","e","f"])','cell',[2,3]); + result = mdscheck('1BU', 'uint8', [1, 1]); + result = result && mdscheck('1WU', 'uint16', [1, 1]); + result = result && mdscheck('1LU', 'uint32', [1, 1]); + result = result && mdscheck('100000000000QU', 'uint64', [1, 1]); + result = result && mdscheck('1.', 'single', [1, 1]); + result = result && mdscheck('1D0', 'double', [1, 1]); + result = result && mdscheck('"string test"', 'char', [1, 11]); + result = result && mdscheck('BYTE_UNSIGNED(1:100)', 'uint8', [100, 1]); + result = result && mdscheck('WORD_UNSIGNED(1:100)', 'uint16', [100, 1]); + result = result && mdscheck('LONG_UNSIGNED(1:100)', 'uint32', [100, 1]); + result = result && mdscheck('BYTE(1:100)', 'int8', [100, 1]); + result = result && mdscheck('WORD(1:100)', 'int16', [100, 1]); + result = result && mdscheck('1:100', 'int32', [100, 1]); + result = result && mdscheck('QUADWORD(1:100)', 'int64', [100, 1]); + result = result && mdscheck('set_range(10,2,5,1:100)', 'int32', [10, 2, 5]); + result = result && mdscheck('FS_FLOAT(1:100)', 'single', [100, 1]); + result = result && mdscheck('FT_FLOAT(1:100)', 'double', [100, 1]); + result = result && mdscheck('set_range(10,2,5,FT_FLOAT(1:100))', 'double', [10, 2, 5]); + result = result && mdscheck('$ : $', 'int32', [100, 1], int32(1), int32(100)); + result = result && mdscheck('$ == $', 'uint8', [1, 1], 1, 2); + result = result && mdscheck('QUADWORD_UNSIGNED(1:100)', 'uint64', [100, 1]); + result = result && mdscheck('["a","b","c","d"]', 'cell', [4, 1]); + result = result && mdscheck('set_range(2,3,["a","b","c long string","d","e","f"])', 'cell', [2, 3]); end -function result = mdscheck(exp,result_class,result_size,varargin) - global MDSINFO - if MDSINFO.useLegacy && ~strcmp(result_class,'char') && ~strcmp(result_class,'cell') - result_class = 'double'; - end - x = mdsvalue(exp,varargin{:}); - if ~strcmp(class(x),result_class) - fprintf('Error %s: expected class %s got %s\n',exp,result_class,class(x)) +function result = mdscheck(exp, result_class, result_size, varargin) + x = mdsvalue(exp, varargin{:}); + if ~strcmp(class(x), result_class) + fprintf('Error %s: expected class %s got %s\n', exp, result_class, class(x)) result = 0; return end try - if ~all(size(x)==result_size) - fprintf('Error %s: expected size %s got %s\n',exp,mat2str(result_size),mat2str(size(x))) + if ~all(size(x) == result_size) + fprintf('Error %s: expected size %s got %s\n', exp, mat2str(result_size), mat2str(size(x))) result = 0; return end catch - fprintf('Error %s: expected size %s got %s\n',exp,mat2str(result_size),mat2str(size(x))) + fprintf('Error %s: expected size %s got %s\n', exp, mat2str(result_size), mat2str(size(x))) result = 0; return end - y=mdsvalue('$',x); - if isa(x,'cell') + y = mdsvalue('$', x); + if isa(x, 'cell') try - result = strcmp(x,y); + result = strcmp(x, y); result = all(result(:)); if ~result - display(strcat('Error testing ',exp,' : string array does not match')) + display(strcat('Error testing "', exp, '": string array does not match')) end catch err - display(strcat('Error testing ',exp,' : ',err.message)) + display(strcat('Error testing "', exp, '": ', err.message)) rethrow(err) end else @@ -71,12 +67,12 @@ result = x==y; result = all(result(:)); if ~result - fprintf('Error %s: input != output\n',exp) + fprintf('Error %s: input != output\n', exp) display(x); display(y); end catch err - display(strcat('Error testing ',exp,' : ',err.message)) + display(strcat('Error testing "', exp, '": ', err.message)) rethrow(err) end end diff --git a/matlab/mdsvalue.m b/matlab/mdsvalue.m index f7df095678..d8f5ca3dbb 100755 --- a/matlab/mdsvalue.m +++ b/matlab/mdsvalue.m @@ -1,56 +1,53 @@ -function [ result, status ] = mdsvalue( expression, varargin) +function [ result, status ] = mdsvalue( expression, varargin ) % MDSVALUE evaluate an expression -% [ result, status ] = mdsvalue( expression, varargin) - info = mdsInfo(); - n = nargin-1; - if info.usePython - args=cell(n,1); +% [ result, status ] = mdsvalue( expression, varargin ) +info = mdsInfo(); +n = nargin - 1; +if info.usePython + args = cell(n, 1); +else + if info.isConnected + extra = 0; else - if info.isConnected - extra=0; - else - extra=1; - end - args = javaArray('MDSplus.Data',max(1,n+extra)); + extra = 1; end - for k = 1: n - argin=varargin(k); - if iscell(argin{1}) - argout = mdsFromMatlab(argin{1}); - else - argout = mdsFromMatlab(cell2mat(argin)); - end - if info.usePython - args{k} = argout; - else - args(k) = argout; - end + args = javaArray('MDSplus.Data', max(1, n + extra)); +end +for k = 1 : n + argin = varargin(k); + if iscell(argin{1}) + argout = mdsFromMatlab(argin{1}); + else + argout = mdsFromMatlab(cell2mat(argin)); + end + if info.usePython + args{k} = argout; + else + args(k) = argout; end +end - try - if info.isConnected - if n > 0 - if info.usePython - result = info.connection.get(expression,args{:}); - else - result = info.connection.get(expression,args); - end - else - result = info.connection.get(expression); - end - else +try + if info.isConnected + if n > 0 if info.usePython - result = py.MDSplus.EXECUTE(expression,args{:}); - result.evaluate(); + result = info.connection.get(expression, args{:}); else - dobj=javaObject('MDSplus.Data'); - result = dobj.execute(expression, args); + result = info.connection.get(expression, args); end + else + result = info.connection.get(expression); + end + else + if info.usePython + result = pythonExecute(expression, args); + else + result = javaExecute(expression, args); end - status=1; - catch err - status=0; - result=err.message; end - result=mdsToMatlab(result); + status = 1; +catch err + status = 0; + result = err.message; end +result = mdsToMatlab(result); diff --git a/matlab/private/javaActivate.m b/matlab/private/javaActivate.m new file mode 100644 index 0000000000..ac58e8a5af --- /dev/null +++ b/matlab/private/javaActivate.m @@ -0,0 +1,40 @@ +function err = javaActivate() +persistent cache +if ~isempty(cache) + err = cache; +else + isOctave = (exist ('OCTAVE_VERSION', 'builtin') > 0); + try + MDSPLUS_DIR = getenv('MDSPLUS_DIR'); + if ismac + lib = strcat(MDSPLUS_DIR, '/lib/libJavaMds.dylib'); + elseif isunix + lib = strcat(MDSPLUS_DIR, '/lib/libJavaMds.so'); + elseif ispc + lib = 'C:\WINDOWS\SYSTEM32\JavaMds.dll'; + if ~isfile(lib) + if strcmp(computer('arch'), 'win64') + lib = fullfile(MDSPLUS_DIR, 'bin64', 'JavaMds.dll'); + else + lib = fullfile(MDSPLUS_DIR, 'bin32', 'JavaMds.dll'); + end + end + end + if not(isOctave) + java.lang.System.setProperty('JavaMdsLib', lib); + end + p = javaclasspath('-all'); + matches = strfind(p, 'mdsobjects.jar'); + if not(any(vertcat(matches{:}))) + javaaddpath(fullfile(MDSPLUS_DIR, 'java', 'classes', 'mdsobjects.jar')) + end + if isOctave + javaObject('MDSplus.Int32', 1); + else + MDSplus.Data.execute('1', javaArray('MDSplus.Data', 1)); + end + err = false; + cache = err; + catch err + end +end \ No newline at end of file diff --git a/matlab/private/javaConnect.m b/matlab/private/javaConnect.m new file mode 100644 index 0000000000..d1185575cd --- /dev/null +++ b/matlab/private/javaConnect.m @@ -0,0 +1,2 @@ +function connection = javaConnect( host ) +connection = javaObject('MDSplus.Connection', host); diff --git a/matlab/private/javaExecute.m b/matlab/private/javaExecute.m new file mode 100644 index 0000000000..78515280cc --- /dev/null +++ b/matlab/private/javaExecute.m @@ -0,0 +1,3 @@ +function result = javaExecute(expression, args) +dobj = javaObject('MDSplus.Data'); +result = dobj.execute(expression, args); diff --git a/matlab/private/javaFromMatlab.m b/matlab/private/javaFromMatlab.m new file mode 100644 index 0000000000..1261c4aaad --- /dev/null +++ b/matlab/private/javaFromMatlab.m @@ -0,0 +1,37 @@ +function result = javaFromMatlab(value, ~) +switch class(value) + case 'double' + javaclass = 'MDSplus.Float64'; + case 'single' + javaclass = 'MDSplus.Float32'; + case 'int64' + javaclass = 'MDSplus.Int64'; + case 'uint64' + javaclass = 'MDSplus.Uint64'; + case 'int32' + javaclass = 'MDSplus.Int32'; + case 'uint32' + javaclass = 'MDSplus.Uint32'; + case 'int16' + javaclass = 'MDSplus.Int16'; + case 'uint16' + javaclass = 'MDSplus.Uint16'; + case 'int8' + javaclass = 'MDSplus.Int8'; + case 'uint8' + javaclass = 'MDSplus.Uint8'; + case 'cell' + javaclass = 'MDSplus.String'; + case 'char' + result = javaObject('MDSplus.String', value); + return + otherwise + result = value; + return +end +sz = size(value); +if isequal(sz, [1, 1]) + result = javaObject(javaclass, value); +else + result = javaObject(strcat(javaclass, 'Array'), reshape(value, [], 1), sz); +end diff --git a/matlab/private/javaToMatlab.m b/matlab/private/javaToMatlab.m new file mode 100644 index 0000000000..041f7e0e3a --- /dev/null +++ b/matlab/private/javaToMatlab.m @@ -0,0 +1,66 @@ +function result = javaToMatlab(mdsthing) +if ~strncmp(class(mdsthing), 'MDSplus', 7) + result = mdsthing; +else + if numel(mdsthing.getShape()) > 0 + shape = mdsthing.getShape; + if numel(shape) == 1 + shape = [shape, 1]; + else + shape = shape'; + end + switch class(mdsthing) + case 'MDSplus.Int64Array' + result = reshape(mdsthing.getLongArray, shape); + case 'MDSplus.Uint64Array' + result = reshape(typecast(mdsthing.getLongArray, 'uint64'), shape); + case 'MDSplus.Int32Array' + result = reshape(mdsthing.getIntArray, shape); + case 'MDSplus.Uint32Array' + result = reshape(typecast(mdsthing.getIntArray, 'uint32'), shape); + case 'MDSplus.Int16Array' + result = reshape(mdsthing.getShortArray, shape); + case 'MDSplus.Uint16Array' + result = reshape(typecast(mdsthing.getShortArray, 'uint16'), shape); + case 'MDSplus.Int8Array' + result = reshape(mdsthing.getByteArray, shape); + case 'MDSplus.Uint8Array' + result = reshape(typecast(mdsthing.getByteArray, 'uint8'), shape); + case 'MDSplus.Float64Array' + result = double(reshape(mdsthing.getDoubleArray, shape)); + case 'MDSplus.Float32Array' + result = reshape(mdsthing.getFloatArray, shape); + case 'MDSplus.StringArray' + result = reshape(cellstr(char(mdsthing.getStringArray)), shape); + otherwise + throw(MException('MDSplus:mdsToMatlab', 'class %s not supported by mdsToMatlab function\n', class(mdsthing))); + end + else + switch class(mdsthing) + case 'MDSplus.Int64' + result = mdsthing.getLongArray(); + case 'MDSplus.Uint64' + result = typecast(mdsthing.getLongArray(), 'uint64'); + case 'MDSplus.Int32' + result = int32(mdsthing.getIntArray()); + case 'MDSplus.Uint32' + result = typecast(mdsthing.getIntArray(), 'uint32'); + case 'MDSplus.Int16' + result = mdsthing.getShortArray(); + case 'MDSplus.Uint16' + result = typecast(mdsthing.getShortArray(), 'uint16'); + case 'MDSplus.Int8' + result = mdsthing.getByteArray(); + case 'MDSplus.Uint8' + result = typecast(mdsthing.getByteArray(), 'uint8'); + case 'MDSplus.Float64' + result = mdsthing.getDoubleArray(); + case 'MDSplus.Float32' + result = mdsthing.getFloatArray(); + case 'MDSplus.String' + result = char(mdsthing.getString()); + otherwise + throw(MException('MDSplus:mdsToMatlab', 'class %s not supported by mdsToMatlab function\n', class(mdsthing))); + end + end +end diff --git a/matlab/private/pythonActivate.m b/matlab/private/pythonActivate.m new file mode 100644 index 0000000000..d1e70cae8d --- /dev/null +++ b/matlab/private/pythonActivate.m @@ -0,0 +1,13 @@ +function err = pythonActivate() +global MDSINFO +persistent cache +if ~isempty(cache) + err = cache; +else + try + MDSINFO.ispy2 = logical(py.MDSplus.version.ispy2); + err = false; + cache = err; + catch err + end +end diff --git a/matlab/private/pythonConnect.m b/matlab/private/pythonConnect.m new file mode 100644 index 0000000000..ccd56318f8 --- /dev/null +++ b/matlab/private/pythonConnect.m @@ -0,0 +1,3 @@ +function connection = pythonConnect( host ) +py_MDSplus_Connection = str2func('py.MDSplus.Connection'); +connection = py_MDSplus_Connection(host); diff --git a/matlab/private/pythonExecute.m b/matlab/private/pythonExecute.m new file mode 100644 index 0000000000..ec9b11275f --- /dev/null +++ b/matlab/private/pythonExecute.m @@ -0,0 +1,3 @@ +function result = pythonExecute(expression, args) +result = py.MDSplus.EXECUTE(expression, args{:}); +result.evaluate(); diff --git a/matlab/private/pythonFromMatlab.m b/matlab/private/pythonFromMatlab.m new file mode 100644 index 0000000000..630dfec249 --- /dev/null +++ b/matlab/private/pythonFromMatlab.m @@ -0,0 +1,32 @@ +function result = pythonFromMatlab(value, info) +dtype = class(value); +switch dtype + case 'char' + result = value; + otherwise + switch dtype + case 'single' + dtype = 'float32'; + case 'double' + dtype = 'float64'; + case 'cell' + dtype = 'str'; + end + if info.ispy2 && contains(dtype, 'int64') + warning('MDSplus:python2_int64', '%s\n', ... + 'Loss of precision: python2 does not support int64 properly. Try python3 instead.', ... + 'You can disable this warning with "warning(''off'', ''MDSplus:python2_int64'')"') + value = double(value); + end + if isscalar(value) + f = str2func(strcat('py.numpy.', dtype)); + result = f(value); + else + sz = size(value); + result = py.numpy.array(reshape(value, int32(1), prod(int32(sz))), dtype); + if not(numel(sz) == 2 && sz(2) == 1) + result = py.numpy.resize(result, flip(int32(sz))); + result = py.numpy.ascontiguousarray(result); + end + end +end diff --git a/matlab/private/pythonToMatlab.m b/matlab/private/pythonToMatlab.m new file mode 100644 index 0000000000..0808fe1b50 --- /dev/null +++ b/matlab/private/pythonToMatlab.m @@ -0,0 +1,55 @@ +function result = pythonToMatlab(value, info) +if strncmp(class(value), 'py.MDSplus', 10) + value = value.data(); +end +if ~strncmp(class(value), 'py.numpy', 8) + result = value; +else + dtype = char(value.dtype.name); + if contains(dtype, 'str') || contains(dtype, 'bytes') + if value.ndim == 0 + result = char(value); + else + strs = value.flatten.tolist(); + result{length(strs)} = ''; + for idx = 1 : length(strs) + result{idx} = char(py.MDSplus.version.tostr(strs{idx})); + end + shape = flip(int64(py.array.array('i', value.shape))); + if numel(shape) == 1 + shape = [shape, int64(1)]; + end + result = reshape(result, shape); + end + else + switch dtype + case 'float32' + dtype = 'single'; + case 'float64' + dtype = 'double'; + end + f = str2func(dtype); + dtype = value.dtype.char; + if info.ispy2 && contains('Qq', char(dtype)) + warning('MDSplus:python2_int64', '%s\n', ... + 'Loss of precision: python2 does not support int64 properly. Try python3 instead.', ... + 'You can disable this warning with "warning(''off'', ''MDSplus:python2_int64'')"') + dtype = 'd'; + end + if value.ndim > 1 + shape = int64(py.array.array('i', value.shape)); + if ~py.numpy.isfortran(value) + shape = flip(shape); + end + value = py.numpy.asfortranarray(value); + value = py.array.array(dtype, value.flat); + result = reshape(f(value), shape); + else + if value.ndim > 0 + result = reshape(f(py.array.array(dtype, value)), [int64(value.shape{1}), 1]); + else + result = f(py.array.array(dtype, {value})); + end + end + end +end \ No newline at end of file