function [SD,status]=hdfsd_open(filename)
%   call [SD,status]=hdfsd_open(filename)
%
% Opens a 3-D HDFv4.1r3 scientific dataset.
% 
% SD.hdf_file		= Name of HDF file;
% SD.nset			= Number of data sets in file;
% SD.data_name		= Cell array containing data labels of each data set;
% SD.rank			= Cell array containing ranks of each data set;
% SD.dim_sizes		= Cell array containing dimensions of each data set;
% SD.scales			= Cell array containing scales of each data set;
% SD.dim_names		= Cell array containing dimension labels for each data set;
% SD.data_sds_id	= Cell array containing identifiers for each data set;
%  
% Calls hdfsd('end',sd_id) at the end.
% See also hdfsd_read, which uses output SD to re-open the HDF file and read section from it.
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
if exist(filename)~=2,
	filename=[filename,'.hdf'];
	if exist(filename)~=2,
		disp(['HDF file not found (nor with .hdf suffix): ',filename(1:end-4)]); 
		status=-1; 
		return
	end
end
%
SD.hdf_file=filename;
SD.nset=0;
SD.data_name{1}='';
SD.rank{1}=0;
SD.dim_sizes{1}=0;
SD.scales{1}{1}=0;
SD.dim_names{1}{1}='';
SD.data_sds_id{1}=0;
%%SD.dim_ids{1}=0;
%
access_mode='read';
status=0;
sd_id = hdfsd('start',filename,access_mode);
[ndatasets,nglobal_attr,tstatus] = hdfsd('fileinfo',sd_id);
status=status+tstatus;
if nglobal_attr>0,
    SD.ATTR.name=cell(nglobal_attr,1); 
    SD.ATTR.data_type=cell(nglobal_attr,1); 
    SD.ATTR.data=cell(nglobal_attr,1); 
end
for jattr=0:nglobal_attr-1,
    j=jattr+1;
    [name,data,count,tstatus] = hdfsd('attrinfo',sd_id,jattr);
    SD.ATTR.name{j}=name; SD.ATTR.data_type{j}=data;
    status=status+tstatus;
    [SD.ATTR.data{j},tstatus] = hdfsd('readattr',sd_id,jattr);
    status=status+tstatus;
end
dimcnt=0; off=0;
for jset=0:ndatasets-1,
	sds_id = hdfsd('select',sd_id,jset);
	[dname,rank,dim_sizes,data_type,nattrs,status] = hdfsd('getinfo',sds_id);
	[label,unit,format,coordsys,status] = hdfsd('getdatastrs',sds_id,64);
	status=status+tstatus;
	isadim = hdfsd('iscoordvar',sds_id);
	if isadim==0,
		SD.nset=SD.nset+1;
		SD.data_sds_id{SD.nset}=sds_id;
		SD.data_name{SD.nset}=label;
		SD.rank{SD.nset}=double(rank);
		SD.dim_sizes{SD.nset}=double(dim_sizes(end:-1:1));
		dimcnt=0;
	else
		dimcnt=dimcnt+1;
		if SD.nset==0, off=1, end
%%		SD.dim_sds_ids{SD.nset+off}(dimcnt)=sds_id;
	end
end
%SD.dim_names='';
%scales=zeros(max(SD.dim_sizes),rank);
for jset=1:SD.nset,
	for i=1:SD.rank{jset},
		jmat=SD.rank{jset}-i+1;
%%		SD.dim_ids{jset}(jmat) = hdfsd('getdimid',SD.data_sds_id{jset},i-1);
%%		[dim_name,dim_count,dim_data_type,dim_nattrs,status] = ...
%%			hdfsd('diminfo',SD.dim_ids{jset}(jmat));
%%		[scale,status] = hdfsd('getdimscale',SD.dim_ids{jset}(jmat));
		dim_id = hdfsd('getdimid',SD.data_sds_id{jset},i-1);
		[dim_name,dim_count,dim_data_type,dim_nattrs,status] = ...
			hdfsd('diminfo',dim_id);
		[label,unit,format,status] = hdfsd('getdimstrs',dim_id);
		status=status+tstatus;
		SD.dim_names{jset}{jmat}=label;
		[scale,status] = hdfsd('getdimscale',dim_id);
		status=status+tstatus;
		SD.scales{jset}{jmat}=double(scale);
% sanity check
%		if dim_count ~= SD.dim_sizes{jset}(jmat)
%			disp(['dim_count ~= SD.dim_sizes(jmat)'])
%			dim_count,SD.dim_sizes{jset}(jmat),i
%		end
	end
end
%
status = hdfsd('end',sd_id);
