% Matlab implementation of getSig()
% 	Gets 1D/2D signals from the MDS+ DataSignals HDF5 cache.
% 
% This uses Matlab's built in HDF5 reading but has to decompress the 
% files to a seperate copy first.
%
% These global variables should be set:
%	mdsCache	path to the DataSignals MDS+ HDF5 data cache.
%	h5repackCmd	path and command to the HDF5 util 'h5repack'
%	
%
% optional:
%	sigFetchCmd		Commmand to execute to fetch a signal (usually the DataSignals.utils.mdsFetch utility)
%
%
function [data,t,x]=getSig(machine, pulse, path)
	global mdsCache h5repackCmd sigFetchCmd;
	clear('rnk');
	clear('data');
	clear('coord1');
	clear('coord2');
	clear('t');
	clear('x');

	if length(mdsCache) <= 0 | length(h5repackCmd) <= 0;
		error('Must set mdsCache and h5repackCmd variables before getSig()');
	end;

	sigFile = lower(path);
	sigFile = strrep(sigFile,'<','_lt_');
	sigFile = strrep(sigFile,'>','_gt_');
	sigFile = sprintf('%s/%s/%i/%s.hdf',mdsCache,machine,pulse,sigFile);

	disp(sigFile);
	if exist(sigFile,'file') <= 0;
		if exist('sigFetchCmd') <= 0 | length(sigFetchCmd) <= 0; 
			data = []; t=[]; x=[];
			disp(strcat('No signal ',path,' and no sigFetchCmd\n'));
			return;
		end;
		%mdsFetchPath = strrep(path,'<','\<');
		%mdsFetchPath = strrep(mdsFetchPath,'>','\>');
		cmd = sprintf('%s %s %i \"%s\" %s',sigFetchCmd,machine,pulse, path, mdsCache);
		ret=system(cmd);
		if ret > 0;
			disp(sprintf('Signal fetch command %s returned %i\n', cmd, ret));
		end;

		if exist(sigFile,'file') <= 0;
			% still no file, fetch failed so give up
			data=[]; t=[]; x=[];
			return;
		end;
	end


	tempFile = strcat(tempdir(),'/lastSignal-decompressed.hdf');
    if exist(tempFile, 'file') > 0;
        delete(tempFile);
    end;
	cmd = sprintf('%s -i %s -o %s -f NONE',h5repackCmd,sigFile,tempFile);
	ret=system(cmd);
	if ret > 0;
		disp(sprintf('HDF5 Repack command %s returned %i\n', cmd, ret));
	end;

	% get the HDF5 info structure and deal with datasets we know about as we find them
	sigInfo=hdf5info(tempFile);
	for i=1:length(sigInfo.GroupHierarchy.Datasets);
		dset = sigInfo.GroupHierarchy.Datasets(i);
		if strcmpi(dset.Name, '/data') > 0; 
			rnk = length(dset.Dims);
			data = hdf5read(dset);
		elseif strcmpi(dset.Name, '/coord0') > 0;
			coord0 = hdf5read(dset);
		elseif strcmpi(dset.Name, '/coord1') > 0;
			coord1 = hdf5read(dset);
		end
	end;

	if exist('data') == 0 | length(data) < 1;
		data = []; t=[]; x=[];
		disp(strcat('Signal ',path,' exists but has no data\n'));
		return;
	end;

	% now some hacks as per the scilab code because some PPFs are
	% up the wrong way (i.e 1D data stored as a 2D PPF with one row/column)
	if rnk > 1; % 2D PPF
		if exist('coord0') > 0;
			x = coord0;
		else
			x = [];
			disp('No coords0 in 2D signal (x)\n');
		end;
		if exist('coord1') > 0;
			t = coord1';
		else
			t = [];
			disp('No coords1 in 2D signal (t)\n');
		end;
		data = data';
	else
		if exist('coord0') > 0;
			t = coord0';
		else
			t = [];
			disp('No coords0 in 1D signal (t)\n');
		end;
		x = [];
		data = data'; % and get data the right way up
	end;

	if sum(data) == 0;
		disp(sprintf('*** WARNING: ALL THE DATA IN SIGNAL %s is 0. ***', path));
	end;

	
% endfunction
