Viewing contents of file '../idllib/contrib/tappin/graffer/gr_get_bin.pro'
function Gr_get_bin, pdefs, fname, no_set=no_set, no_warn=no_warn, $
                     resave=resave

;+
; GR_GET_BIN
;	Get an BINARY graffer dataset from a file
;
; Usage:
;	iopen = gr_get_bin(pdefs[, f])
;
; Return Value:
;	iopen	int	1 If file was opened; 0 if not.
;
; Argument:
;	pdefs	struct	in/out	The graffer data structure.
;	f	string	input	The file to read.
; Keywords:
;	no_set	input	If set, then don't try to set up the widget
;			values (because the widgets aren't there)
;	resave	input	If set, then save the file again
;			(i.e. autosave file)
;	no_warn	input	If set, then don't print a warning message if
;			file is not opened sucessfully.
;
; History:
;	Original: (from GR_GET_ASC) 15/1/96; SJT
;	Drop CDF support: 10/2/97; SJT
;	Fix bad tag problem (I hope): 19/5/97; SJT
;	Add REM(arks) field: 23/6/97; SJT
;-

on_ioerror, nofile

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

rs = bytarr(7)                  ; Read the file header "record"
readu, ilu, rs
file_v = gr_int_rd(ilu, 2)

dir = gr_str_rd(ilu)
name = gr_str_rd(ilu)
time = gr_str_rd(ilu)


dflag = 0b
tflag = 0b

tag = '   '

while (not eof(ilu)) do begin
    
    readu, ilu, tag
    
    case (tag) of
        
                                ; The G keys are general graffer keys
                                ; GT - plot title
                                ; GS - Plot subtitle
                                ; GC - Annotation charcter size
                                ; GA - line thickness for AXES
                                ; GP - Positions of corners
                                ; GR - Aspect of plot.
        
        'GT ': pdefs.title = gr_str_rd(ilu)
        'GS ': pdefs.subtitle = gr_str_rd(ilu)
        'GC ': pdefs.charsize = gr_flt_rd(ilu, 1)
        'GA ': pdefs.axthick = gr_int_rd(ilu, 1)
        'GP ': pdefs.position = gr_flt_rd(ilu, 4)
        'GR ': pdefs.aspect = gr_flt_rd(ilu, 2)
        
                                ; The X and Y keys are items relating
                                ; to the X and Y axes respectively
                                ; XR, YR - axis range
                                ; XL, YL - axis log/linear
                                ; XSI, YSI - axis style (the IDL STYLE key)
                                ; XSE, YSE - extra style items
                                ; XSG, YSG - Grid linestyle (IDL linesyle+1)
                                ; XST (YST) - Time labelling options
                                ; XSZ (YSZ) - Time zero.
                                ; XT, YT - Axis label.

        'XR ': pdefs.xrange = gr_flt_rd(ilu, 2)
        'XL ': pdefs.xtype = gr_int_rd(ilu, 1)
        'XSI': pdefs.xsty.idl = gr_int_rd(ilu, 1)
        'XSE': pdefs.xsty.extra = gr_int_rd(ilu, 1)
        'XSG': pdefs.xsty.grid = gr_int_rd(ilu, 1)
        'XST': pdefs.xsty.time = gr_int_rd(ilu, 1)
        'XSZ': pdefs.xsty.tzero = gr_int_rd(ilu, 1)
        'XT ': pdefs.xtitle = gr_str_rd(ilu)
        
        'YR ': pdefs.yrange = gr_flt_rd(ilu, 2)
        'YL ': pdefs.ytype = gr_int_rd(ilu, 1)
        'YSI': pdefs.ysty.idl = gr_int_rd(ilu, 1)
        'YSE': pdefs.ysty.extra = gr_int_rd(ilu, 1)
        'YSG': pdefs.ysty.grid = gr_int_rd(ilu, 1)
        'YST': pdefs.ysty.time = gr_int_rd(ilu, 1)
        'YSZ': pdefs.ysty.tzero = gr_int_rd(ilu, 1)
        'YT ': pdefs.ytitle = gr_str_rd(ilu)
        
                                ; ZT - specifies the colour table to
                                ;      be used by the image format for
                                ;      displaying 2-D data
                                ; ZG - The gamma value for same.
        
        'ZT ': pdefs.ctable = gr_int_rd(ilu, 1)
        'ZG ': pdefs.gamma = gr_flt_rd(ilu, 1)
        
                                ; DN - total number of datasets in the
                                ;      file. This MUST come before any
                                ;      datasets are defined.
                                ; DC - The currently selected dataset
                                ;      (N.B. This is ZERO-based while
                                ;      the dataset index showing via
                                ;      the GRAFFER interface is
                                ;      ONE-based)
        
        'DN ': begin
            pdefs.nsets = gr_int_rd(ilu, 1)
            nds = pdefs.nsets > 1
            data = replicate({graff_data}, nds)
            dflag = 1b
        end
        'DC ': pdefs.cset = gr_int_rd(ilu, 1)
        
                                ; TN - The total number of text
                                ;      strings in the file. This must
                                ;      come before any strings are
                                ;      actually defined.
        
        'TN ': begin
            pdefs.ntext = gr_int_rd(ilu, 1)
            ntext = pdefs.ntext > 1
            text = replicate({graff_text}, ntext)
            tflag = 1b
        end
        
                                ; DS - Start the definition of a
                                ;      dataset.
        
        'DS ': begin
            rset = gr_int_rd(ilu, 1)
            gr_bin_ds, data, rset, ilu, pdefs.ids.message, file_v
        end
        
                                ; TS - start the definition of a text
                                ;      string 
                                ; TTS - start the definition of the
                                ;       text template (current default
                                ;       text options).
        
        'TS ': begin
            tset = gr_int_rd(ilu, 1)
            gr_bin_txt, text, tset, ilu, pdefs.ids.message 
        end
        
        'TTS': begin
            topts = {graff_text}
            gr_bin_txt, topts, 0, ilu, pdefs.ids.message, /template
            pdefs.text_options = topts
        end
        
                                ; The H options refer to the options
                                ; for genration PostScript hardcopy
                                ; files.
                                ; HC - Colour or monchrome
                                ; HE - Eps or normal
                                ; HO - landscape or portrait
                                ;      (Orientation)
                                ; HP - Paper size (A4 or letter)
                                ; HT - whether to put a timestapm on
                                ;      the plot.
                                ; HS - size x & y in cm.
                                ; HD - Page offset in cm.
                                ; HAB - The spooling command (up to
                                ;       the filename)
                                ; HAA - Any part of the spooling
                                ;       command which follows the
                                ;       filename. 
                                ; HF - Font family.
                                ; HWS - Font weight and slant (bit 0 is
                                ;       on for bold, bit 1 for
                                ;       oblique/italic) 
        
        'HC ': pdefs.hardset.colour = gr_byt_rd(ilu, 1)
        'HE ': pdefs.hardset.eps = gr_byt_rd(ilu, 1)
        'HO ': pdefs.hardset.orient = gr_byt_rd(ilu, 1)
        'HP ': pdefs.hardset.psize = gr_byt_rd(ilu, 1)
        'HT ': pdefs.hardset.timestamp = gr_byt_rd(ilu, 1)
        'HS ': pdefs.hardset.size = gr_flt_rd(ilu, 2)
        'HD ': pdefs.hardset.off = gr_flt_rd(ilu, 2)

        'HAB': pdefs.hardset.action(0) = gr_str_rd(ilu)
        'HAA': pdefs.hardset.action(1) = gr_str_rd(ilu)
 
        'HF ': pdefs.hardset.font.family = $
          gr_int_rd(ilu, 1)
        'HWS': pdefs.hardset.font.wg_sl = $
          gr_int_rd(ilu, 1)
        
                                ; The K tags relate to the plotting of
                                ; a key on the plot.
                                ; KU - Plot a key
                                ; KX - X coordinates of the corners
                                ; KY - Y coordinates of the corners
                                ; KN - System they are given in.
                                ; KC - How many columns
                                ; KF - Frame?
                                ; KT - Title of key
                                ; KNL - Number of items in key (only
                                ;       used in an ascii save
                                ; KL - The indices of the datasets to
                                ;      display
                                ; KP - Whether to plot 1 or 2 points.
        
        'KU ': pdefs.key.use = gr_byt_rd(ilu, 1)
        'KX ': pdefs.key.x = gr_flt_rd(ilu, 2)
        'KY ': pdefs.key.y = gr_flt_rd(ilu, 2)
        'KN ': pdefs.key.norm = gr_int_rd(ilu, 1)
        'KC ': pdefs.key.cols = gr_int_rd(ilu, 1)
        'KF ': pdefs.key.frame = gr_byt_rd(ilu, 1)
        'KP ': pdefs.key.one_point = gr_byt_rd(ilu, 1)
        'KT ': pdefs.key.title = gr_str_rd(ilu)
        'KL ': begin
            nl = gr_lon_rd(ilu, 1)
            list = gr_lon_rd(ilu, nl)
            handle_value, pdefs.key.list, list, /set, /no_copy
        end
        
                                ; REM - Remarks attached to the file
        
        'REM': begin
            remarks = gr_str_rd(ilu)
            handle_value, pdefs.remarks, remarks, /set, /no_copy
        end
        
                                ; This probably means that the file is
                                ; corrupted. 
        
        Else: begin
            graff_msg, pdefs.ids.message, "Unknown tag: " + $
              tag + "Ignoring."
            point_lun, -ilu, ipos 
            point_lun, ilu, ipos-2 ; Move back 2 bytes
        end
    endcase
    
New_line:
    
endwhile

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


free_lun, ilu

pdefs.chflag = 0                ; Clear changes flag

if (not keyword_set(no_set)) then begin
    graff_set_vals, pdefs
    vm = total(file_v ne pdefs.version)
    if (vm ne 0.) then graff_msg, pdefs.ids.message, $
      ['File and program versions differ', $
       'File: '+string(file_v, format = "(I2,'.',I2.2)")+ $
       '  Program: '+string(pdefs.version, format = "(I2,'.',I2.2)")]
endif

if (keyword_set(resave)) then begin
    gr_bin_save, pdefs
    graff_msg, pdefs.ids.message, "Resaving"
endif

return, 1

Nofile:
if (not keyword_set(no_warn)) then  $
  graff_msg, pdefs.ids.message, ['Failed to open file: '+dir+f, $
                                 !Err_string]

return, 0

end