; $Id: dcs_write_asciioctavefile.pro,v 1.3 2005/02/14 17:50:14 azuah Exp $
; Written by J.R.D. Copley.
;************************************************************************************************
;###############################################################################
;
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used or if the code in this file is
;  included in another product.
;
;###############################################################################
pro dcs_write_asciiOctavefile,filename,datafile=datafile,compress=compressed
;************************************************************************************************
;
compile_opt strictarr
;
; This is a procedure that writes an ascii Octave file.
;
if (filename eq "") then stop
;
on_ioerror, no_file
get_lun,unit
if (!operating_system eq "Windows") then openw,unit,filename,/binary,/noautomode,compress=compressed
if (!operating_system eq "unix") then openw,unit,filename,compress=compressed
if (!operating_system eq "MacOS") then openw,unit,filename,compress=compressed
nvars=n_tags(datafile)
tagnames=tag_names(datafile)
;
for ivar=0,nvars-1 do begin
	name=tagnames[ivar]
	data=datafile.(ivar)
	nsize=size(data)
	ns0=nsize[0]
	nst=nsize[ns0+1]
	if (ns0 eq 0) then begin
		case nst of
			7: dt=5; string
			else: dt=1; scalar
		endcase
	endif
	if (ns0 eq 1) then begin
		case nst of
			7: dt=7; string array
			else: dt=2
		endcase
		if (dt eq 2) then begin & n1=nsize[1] & n2=1 & end
	endif
	if (ns0 eq 2) then begin
		case nst of
			7: dt=0
			else: dt=2
		endcase
		if (dt eq 2) then begin & n1=nsize[1] & n2=nsize[2] & end
	endif
;
	if (dt eq 0) then stop,"No good."
;
	printf,unit,"# name: "+name
	case dt of
		1: begin; write scalar
			printf,unit,"# type: scalar"
			printf,unit,data
		end
		2: begin; write matrix
			printf,unit,"# type: matrix"
			printf,unit,"# rows:"+strcompress(n1)
			printf,unit,"# columns:"+strcompress(n2)
			printf,unit,data
		end
		5: begin; write string
			nchars=strlen(data)
			printf,unit,"# type: string array"
			printf,unit,"# elements: 1"
			printf,unit,"# length:"+strcompress(nchars)
			printf,unit,data
		end
		7: begin; write string array
			printf,unit,"# type: string array"
			nstrgs=nsize[1]
			printf,unit,"# elements:"+strcompress(nstrgs)
			for j=0,nstrgs-1 do begin
				nchars=strlen(data[j])
				printf,unit,"# length:"+strcompress(nchars)
				printf,unit,data[j]
			endfor
		end
	endcase
endfor
;
free_lun,unit
return
;
no_file: begin
	free_lun,unit
	error="There is no file named "+filename
end
;
end
