pro wrt_gen,filename,data, $
                  success=success,text=text,replace=replace,header=header
;
;+
; NAME:
; 	WRT_GEN
;
; PURPOSE:
;	Write SXT generic file 
;
; CALLING SEQUENCE:
;	WRT_GEN, filename, data [,TEXT=TEXT, SUCCESS, /REPLACE] 
;
; INPUTS:
;	DATA - data to save (simple structure, n-dimen array, or scaler)
;	FILENAME - string containing generic file name
;
; OPTIONAL KEYWORD PARAMETERS:
; 	TEXT -    (Input) Generic file text description
;	REPLACE - (Input) If set , forces overwriting of existing file
;	HEADER -  (Output) Returns ystem imposed header (version/creattion date)
;	SUCCESS - (Output) Returns success code
;
; FILE I/O:
;  	If succesful, named file is created or updated (overwritten) 
;
; COMMON BLOCKS;
;	NONE
;
; RESTRICTIONS:
;	Version 00 does not support nested structures for DATA
;
; MODIFICATION HISTORY:
;	Version 0 - SLF, 3/5/91
;		    slf, 19-dec-1992 changed variable name to function call
;			             variable/function conflict (file_exit)
;				     force text to be scaler string
;-
;
success=0			 	; assume the worst	
on_error,2				; return to caller on error
;
header='{dummy,version:0L, creation:string(replicate(32b,24))}'
header=make_str(header)
status=execute('header.creation = systime()')
;
;
; slf, 19-dec
if n_elements(text) eq 0 then text='' else text=arr2str(text,'\\')
;
; slf, 19-dec-1992
; file_exist = findfile(filename) ; removed, call file_exist.pro instead
if ((1-file_exist(filename,/dir)) or keyword_set(replace)) then begin  
   openw,unit,filename,/get_lun
   writeu,unit,header
   writeu,unit,long(strlen(text)),text
   ident=size(data)
   if ident(n_elements(ident)-2) eq 8 then begin
      writeu,unit,long(n_tags(data))
      for i=0,n_tags(data)-1 do writeu,unit,size(data.(i)) 
   endif else begin
      writeu,unit,long(1),size(data)
   endelse
   writeu,unit,data
   success=1				
   free_lun,unit
endif
;
;
return
end

