function [stat]=hdfsd_write(SDw,jset,dim_lims,data);
%
% Call [stat]=hdfsd_write(SDw,jset,dim_lims,data);
%
% SDw.sds_id(jset)	= ID of dataset (from hdfsd_write_open);
% dim_lims	= Cell array of length ndim of {[jy1,jy2], [jx1,jx2], [jz1,jz2] ...};
%			= (Set to [] if data represents all of data to be output);
% data		= Data to be output;
%
% Use: 	hdfsd_write_open 	to open HDF file
% Use: 	hdfsd_write 		to write data sets to SDw.
% Use: 	hdfsd_write_close 	to close SDw finally.
%
%
% 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.
SDw.SD_id = hdfsd('start',SDw.hdf_file,'write');
%[ndatasets,nglobal_attr,status] = hdfsd('fileinfo',SDw.SD_id);
%
dims=size(data);
ndim=SDw.ndim(jset);
if isempty(dim_lims),
	for jdim=1:ndim, dim_lims{jdim}=[]; end
end
stat=-1;
%
if length(dim_lims)<ndim, disp('Bad length for dim_lims.'); return; end
stride=ones(1,ndim); start=zeros(1,ndim); edge=zeros(1,ndim);
for jdim=1:ndim,
	jd=ndim-jdim+1;
	dlims=dim_lims{jdim};
	if isempty(dlims), dlims=[1,SDw.dims{jset}(jdim)]; end
	if length(dlims(:))~=2, 
		disp(['Illegal dim_lims for dimension ',int2str(jdim),': ',num2str(dlims(:)')]);
	end
	start(jd)=dlims(1)-1;
	edge(jd)=dlims(2) - dlims(1) + 1;
end
%
ss=single(data);
%%%'hdfsd_write parms: ',SDw.sds_id(jset),start,stride,edge
stat=hdfsd('writedata',SDw.sds_id(jset),start,stride,edge,ss);
if stat<0, disp('Error writing data to data set.'); end
%
% TEMP: see if closing after each write is OK:
status=hdfsd_write_close(SDw);
