Viewing contents of file '../idllib/contrib/tappin/graffer/gr_bin_save.pro'
pro Gr_bin_save, pdefs, auto=auto

;+
; GR_BIN_SAVE
;	Save a graffer dataset to a file in binary format
;
; Usage:
;	gr_bin_save, pdefs
;
; Argument:
;	pdefs	struct	input	The graffer data structure.
;
; Keyword:
;	auto		input	If set & non-zero, then this is an
;				autosave.
;				
; History:
;	Original: (from GR_SAVE_ASC) 14/1/96; SJT
;	Drop CDF support: 10/2/97; SJT
;	Made unique in 8.3: 11/2/97; SJT
;	Add REM(arks) field: 23/6/97; SJT
;-

if (keyword_set(auto)) then begin
    file = pdefs.dir+'#'+pdefs.name+'#'
    graff_msg, pdefs.ids.message, 'Autosaving'
endif else file = pdefs.dir+pdefs.name

on_ioerror, no_open
if (!version.os eq 'vms') then $
  openw, ilu, /get, file, /udf_block $ ; The UDF_BLOCK is for VMS ignored
                                ; by other systems.
else  openw, ilu, /get, file

on_ioerror, null

; Header & title information

dl = strlen(pdefs.dir)
nl = strlen(pdefs.name)
t = systime()
tl = strlen(t)

writeu, ilu, 'GRAFFER', pdefs.version, dl, pdefs.dir, nl, pdefs.name, $
  tl, t

gr_str_put, ilu, 'GT ', pdefs.title
gr_str_put, ilu, 'GS ', pdefs.subtitle
writeu, ilu, 'GC ', pdefs.charsize, 'GA ', pdefs.axthick
writeu, ilu, 'GP ', pdefs.position, 'GR ', pdefs.aspect

; X-axis information

writeu, ilu, 'XR ', pdefs.xrange, 'XL ', pdefs.xtype
writeu, ilu, 'XSI', pdefs.xsty.idl, 'XSE', pdefs.xsty.extra, $
  'XSG', pdefs.xsty.grid, 'XST', pdefs.xsty.time, $
  'XSZ', pdefs.xsty.tzero
gr_str_put, ilu, 'XT ', pdefs.xtitle

; Y-axis information

writeu, ilu, 'YR ', pdefs.yrange, 'YL ', pdefs.ytype
writeu, ilu, 'YSI', pdefs.ysty.idl, 'YSE', pdefs.ysty.extra, $
  'YSG', pdefs.ysty.grid, 'YST', pdefs.ysty.time, $
  'YSZ', pdefs.ysty.tzero
gr_str_put, ilu, 'YT ', pdefs.ytitle

; Colour table for displayed Z data
writeu, ilu, 'ZT ', pdefs.ctable, 'ZG ', pdefs.gamma

; Number of data sets.
writeu, ilu, 'DN ', pdefs.nsets, 'DC ', pdefs.cset

;	Output each dataset

handle_value, pdefs.data, data, /no_copy

for j = 0, (pdefs.nsets-1) > 0 do begin
    writeu, ilu, 'DS ', j
    gr_str_put, ilu, 'D  ', data(j).descript
    writeu, ilu, 'T  ', data(j).type, 'M  ', data(j).mode, 'N  ', $
      data(j).ndata, 'N2 ', data(j).ndata2
    
    writeu, ilu, 'J  ', data(j).pline, 'P  ', data(j).psym, 'S  ', $
      data(j).symsize, 'L  ', data(j).line, 'C  ', data(j).colour, $
      'W  ', data(j).thick, 'O  ', data(j).sort, 'K  ', $
      data(j).noclip, 'E  ', data(j).medit
    
    handle_value, data(j).xydata, xydata, /no_copy
    
    if (data(j).type lt 0) then begin ; Function
        writeu, ilu, 'R  ', xydata.range
        if (data(j).type eq -3) then begin
            gr_str_put, ilu, 'FX ', xydata.funct(0)
            gr_str_put, ilu, 'FY ', xydata.funct(1)
        endif else gr_str_put, ilu, 'F  ', xydata.funct
        
    endif else if (data(j).type eq 9) then begin ; 2-D data
        handle_value, xydata.x, x, /no_copy
        handle_value, xydata.y, y, /no_copy
        handle_value, xydata.z, z, /no_copy

        writeu, ilu, 'ZXS'
        writeu, ilu, x
        writeu, ilu, 'ZXE'
        writeu, ilu, 'ZYS'
        writeu, ilu, y
        writeu, ilu, 'ZYE'
        writeu, ilu, 'ZZS'
        writeu, ilu, z
        writeu, ilu, 'ZZE'
        
        handle_value, xydata.x, x, /no_copy, /set
        handle_value, xydata.y, y, /no_copy, /set
        handle_value, xydata.z, z, /no_copy, /set
        
    endif else begin            ; Ordinary data
        sxy = size(xydata)
        writeu, ilu, 'VS ', fix(sxy(1))
        writeu, ilu, xydata
        writeu, ilu, 'VE '
    endelse
    
                                ; We only need to save the 2-D
                                ; specific options for 2-D data (types
                                ; 9 & -4)
    
    if (data(j).type eq 9 or data(j).type eq -4) then begin
        writeu, ilu, 'ZF ', data(j).zopts.format
        if (data(j).zopts.format eq 0) then begin ; contour data
            writeu, ilu, 'ZNL', data(j).zopts.n_levels, 'ZNC', $
              data(j).zopts.n_cols, 'ZNS', data(j).zopts.n_sty, $
              'ZNT', data(j).zopts.n_thick, 'ZCF', $
              data(j).zopts.fill, 'ZLI', data(j).zopts.label
            
            if (data(j).zopts.n_levels gt 0) then $ ; Explicit levels
              writeu, ilu, 'ZL ', $
              data(j).zopts.levels(0:data(j).zopts.n_levels-1)
            writeu, ilu, 'ZC ', $
              data(j).zopts.colours(0:data(j).zopts.n_cols-1)
            writeu, ilu, 'ZS ', $
              data(j).zopts.style(0:data(j).zopts.n_sty-1)
            writeu, ilu, 'ZT ', $
              data(j).zopts.thick(0:data(j).zopts.n_thick-1)
        endif else if (data(j).zopts.format eq 1) then begin ; colour
                                ; picture
            writeu, ilu, 'ZR ', data(j).zopts.range, 'ZP ', $
              data(j).zopts.pxsize
        endif
    endif
    
    writeu, ilu, 'DE '
    
    handle_value, data(j).xydata, xydata, /set, /no_copy
endfor


handle_value, pdefs.data, data, /no_copy, /set

;	Note: don't save the widget ids.

writeu, ilu, 'TN ', pdefs.ntext ; Number of text items


handle_value, pdefs.text, text, /no_copy

for j = 0, (pdefs.ntext-1) > 0 do begin
    writeu, ilu, 'TS ', j
    gr_str_put, ilu, 'T  ', text(j).text
    writeu, ilu, 'X  ', text(j).x, 'Y  ', text(j).y, 'N  ', $
      text(j).norm
    writeu, ilu, 'C  ', text(j).colour, 'S  ', text(j).size, 'O  ', $
      text(j).orient, 'A  ', text(j).align, 'F  ', text(j).font, $
      'W  ', text(j).thick
    writeu, ilu, 'TE '
endfor

handle_value, pdefs.text, text, /no_copy, /set

;	The text template (no need to dump the text string or position
;	here as it 
;	must be the null string, only included to simplify coding)

writeu, ilu, 'TTS'
writeu, ilu, 'C  ', pdefs.text_options.colour, 'S  ', $
  pdefs.text_options.size, 'O  ', pdefs.text_options.orient, 'A  ', $
  pdefs.text_options.align, 'F  ', pdefs.text_options.font, 'W  ', $
  pdefs.text_options.thick, 'N  ', pdefs.text_options.norm
writeu, ilu, 'TTE'

;	Specify the key information

writeu, ilu, 'KU ', pdefs.key.use, 'KX ', pdefs.key.x, 'KY ', $
  pdefs.key.y, 'KN ', pdefs.key.norm, 'KC ', pdefs.key.cols, 'KF ', $
  pdefs.key.frame, 'KP ', pdefs.key.one_point
gr_str_put, ilu, 'KT ', pdefs.key.title

handle_value, pdefs.key.list, list, /no_copy
if (n_elements(list) ne 0) then $
  writeu, ilu, 'KL ', n_elements(list), list
handle_value, pdefs.key.list, list, /no_copy, /set

;	Any remarks associated with the file.

handle_value, pdefs.remarks, remarks, /no_copy
if (n_elements(remarks) ne 0) then $
  gr_str_put, ilu, 'REM', remarks
handle_value, pdefs.remarks, remarks, /no_copy, /set

                                ; The hardcopy options

writeu, ilu, 'HC ', pdefs.hardset.colour, 'HE ', pdefs.hardset.eps, $
  'HO ', pdefs.hardset.orient, 'HP ', pdefs.hardset.psize, 'HT ', $
  pdefs.hardset.timestamp
writeu, ilu, 'HS ', pdefs.hardset.size, 'HD ', pdefs.hardset.off

gr_str_put, ilu, 'HAB', pdefs.hardset.action(0)
gr_str_put, ilu, 'HAA', pdefs.hardset.action(1)

writeu, ilu, 'HF ', pdefs.hardset.font.family, 'HWS', $
  pdefs.hardset.font.wg_sl

free_lun, ilu

if (not keyword_set(auto)) then pdefs.chflag = 0b ; Clear change flag
pdefs.transient.changes = 0

return

No_open:
graff_msg, pdefs.ids.message, ["Failed to open save file:"+file, $
                               !Err_string]

end