function varargout = read_sds_temp(varargin)
% read_sds: read data from a scientific data set
% 
%      [data1,data2,...] = read_sds('filename','sds_name1','sds_name2',...)
%      or
%      [data1,data2,...] = read_sds(sd_id,'sds_name1','sds_name2',...)
%      or
%      [data1,data2,...] = read_sds(xxxxx,'sds_name1',range1,'sds_name2',range2,...)
%          where rangex = [start1 start2 ... end1 end2 ...]
%          where startx is the first index (base 1) of the first dimension
%              and endx is the last index of the first dimension, etc...
%              (Note, the order of the dimensions will be the way that they 
%                  show up in matlab, not the way they are listed in the HDF file.)

if isnumeric(varargin{1})
  sd_id = varargin{1};
else
  sd_id = hdfsd('start',varargin{1},'read');
end

y = 0;
x = 1;
while x<nargin
  x = x+1;
  sds_index = hdfsd('nametoindex',sd_id,varargin{x});
  if sds_index==-1
    attr_index = hdfsd('findattr',sd_id,varargin{x});
    y = y+1;
    if attr_index~=-1
      [attr_name,num_type,count,status] = hdfsd('attrinfo',sd_id,attr_index);
      [attr_data,status] = hdfsd('readattr',sd_id,attr_index);
      jpl = 1;
      if x<nargin
	if ischar(varargin{x+1})
	  if strcmp(varargin{x+1},'-nojpl')
	    jpl = 0;
	    x = x+1;
	  end
	end
      end
      if jpl
	ind=find(attr_data == char(10));
	if length(ind)>2
	  n_values=length(ind)-2;
	  varargout{y}=attr_data(ind(2)+1:ind(length(ind))-1);
	else
	  varargout{y} = attr_data;
	end
      else
	varargout{y} = attr_data;
      end
    end
  else
    sds_id = hdfsd('select',sd_id,sds_index);
    [name,rank,dimsizes,data_type,nattrs,status] = hdfsd('getinfo',sds_id);  
    try
      [cal,cal_err,offset,offset_err,data_type,status_cal] = hdfsd('getcal',sds_id);
    catch
      status_cal = -1;
    end
    start = zeros(1,rank);
    count = dimsizes;
    if x<nargin
      if ischar(varargin{x+1})
	if strcmp(varargin{x+1},'-nocal')
	  x = x+1;
	  status_cal = -1;
	  disp(['Calibration not performed on ' varargin{x-1}])
	end
      end
    end
    if x<nargin
      if isnumeric(varargin{x+1})
	x = x+1;
	start = varargin{x}(rank:-1:1)-1;
	count = varargin{x}(2*rank:-1:rank+1)-start;
      end
    end
    y = y+1;
    if status_cal == -1
      varargout{y} = double(hdfsd('readdata',sds_id,start,[],count));
    else
      varargout{y} = cal*double(hdfsd('readdata',sds_id,start,[],count))+offset;
    end    

    hdfsd('endaccess',sds_id);
  end  
end

if ~isnumeric(varargin{1})
  status = hdfsd('end',sd_id);
end