Viewing contents of file '../idllib/contrib/tappin/graffer/gr_get_asc.pro'
function Gr_get_asc, pdefs, fname, no_set=no_set, no_warn=no_warn

;+
; GR_GET_ASC
;	Get an ASCII graffer dataset from a file
;
; Usage:
;	iopen = gr_get_asc(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)
;	recover input	If set, then try to recover the file from the
;			autosave file
;	no_warn	input	If set, then don't print a warning message if
;			file is not opened sucessfully.
;
; History:
;	Original: 16/8/95; SJT
;	Remove file argument (use pdefs.name): 17/8/95; SJT
;	Add facilities for string-type data and restore filename: 18/8/95; SJT
;	Change to a function returning 0 or 1 & add NO_WARN key:
;	11/6/96; SJT
;	Make this the ASCII only version GR_GET_ASC and make GRAFF_GET
;	a wrapper: 14/1/96; SJT
;	Drop CDF support: 10/2/97; SJT
;	Add REM(arks) field: 23/6/97; SJT
;-

on_ioerror, nofile

openr, ilu, /get, fname

rs = ''
readf, ilu, rs                  ; read the overall header

vp = strpos(rs, 'V')+1
file_v = float(strmid(rs, vp, 5))

if (file_v lt 1.0) then begin
    graff_msg, pdefs.ids.message, "Version 0.xx files are no longer " + $
      "supported."
    free_lun, ilu
    return, 0
endif else if (file_v lt 2.0) then begin
    gr_get_v1, pdefs, ilu, file_v ; Read an old fixed layout (V1) file.
    gr_resave, pdefs
endif else begin
    
    dflag = 0b
    tflag = 0b
    
    inline = ''
    
    while (not eof(ilu)) do begin
        
        readf, ilu, inline
        tag_val = str_sep(inline, ':')
        
        for itag = 0, n_elements(tag_val) - 2, 2 do begin
            case (tag_val(itag)) 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': begin
                    pdefs.title = gr_str_val(inline, 'GT')
                    goto, new_line
                end
                'GS': begin
                    pdefs.subtitle = gr_str_val(inline, 'GS')
                    goto, new_line
                end
                'GC': pdefs.charsize = gr_flt_val(tag_val(itag+1), 1)
                'GA': pdefs.axthick = gr_int_val(tag_val(itag+1), 1)
                'GP': pdefs.position = gr_flt_val(tag_val(itag+1), 4)
                'GR': pdefs.aspect = gr_flt_val(tag_val(itag+1), 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_val(tag_val(itag+1), 2)
                'XL': pdefs.xtype = gr_int_val(tag_val(itag+1), 1)
                'XSI': pdefs.xsty.idl = gr_int_val(tag_val(itag+1), 1)
                'XSE': pdefs.xsty.extra = gr_int_val(tag_val(itag+1), 1)
                'XSG': pdefs.xsty.grid = gr_int_val(tag_val(itag+1), 1)
                'XST': pdefs.xsty.time = gr_int_val(tag_val(itag+1), 1)
                'XSZ': pdefs.xsty.tzero = gr_int_val(tag_val(itag+1), 1)
                'XT': begin
                    pdefs.xtitle = gr_str_val(inline, 'XT')
                    goto, new_line
                end
                
                'YR': pdefs.yrange = gr_flt_val(tag_val(itag+1), 2)
                'YL': pdefs.ytype = gr_int_val(tag_val(itag+1), 1)
                'YSI': pdefs.ysty.idl = gr_int_val(tag_val(itag+1), 1)
                'YSE': pdefs.ysty.extra = gr_int_val(tag_val(itag+1), 1)
                'YSG': pdefs.ysty.grid = gr_int_val(tag_val(itag+1), 1)
                'YST': pdefs.ysty.time = gr_int_val(tag_val(itag+1), 1)
                'YSZ': pdefs.ysty.tzero = gr_int_val(tag_val(itag+1), 1)
                'YT': begin
                    pdefs.ytitle = gr_str_val(inline, 'YT')
                    goto, new_line
                end
                
                                ; 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_val(tag_val(itag+1), 1)
                'ZG': pdefs.gamma = gr_flt_val(tag_val(itag+1), 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
                    if (dflag) then  $
                      graff_msg, pdefs.ids.message, $
                      ["Datasets have already been defined", $
                       "these will be destroyed."]
                    
                    pdefs.nsets = gr_int_val(tag_val(itag+1), 1)
                    nds = pdefs.nsets > 1
                    data = replicate({graff_data}, nds)
                    dflag = 1b
                end
                'DC': pdefs.cset = gr_int_val(tag_val(itag+1), 1)
                
                                ; TN - The total number of text
                                ;      strings in the file. This must
                                ;      come before any strings are
                                ;      actually defined.
                
                'TN': begin
                    if (tflag) then  $
                      graff_msg, pdefs.ids.message, $
                      ["Text strings have already been defined", $
                       "these will be destroyed."]
                    
                    pdefs.ntext = gr_int_val(tag_val(itag+1), 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_val(tag_val(itag+1), 1)
                    if (not dflag) then begin
                        graff_msg, pdefs.ids.message, $
                          ["Dataset read before number of sets defined", $
                           "may be destroyed if number of sets found later"]
                        data = replicate({graff_data}, rset)
                        dflag = 1b
                        pdefs.nsets = rset+1
                    endif else if (rset ge pdefs.nsets) then begin
                        data = [data, replicate({graff_data}, $
                                                rset-pdefs.nsets+1)]
                    endif
                    gr_get_ds, data, rset, ilu, pdefs.ids.message, $
                      file_v
                    goto, new_line
                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_val(tag_val(itag+1), 1)
                    if (not tflag) then begin
                        graff_msg, pdefs.ids.message, $
                          ["Text string read before number of sets defined", $
                           "may be destroyed if number of strings found later"]
                        text = replicate({graff_text}, tset)
                        tflag = 1b
                        pdefs.ntext = tset+1
                    endif else if (tset ge pdefs.ntext) then begin
                        text = [text, replicate({graff_text}, $
                                                tset-pdefs.ntext+1)]
                    endif
                    gr_get_txt, text, tset, ilu, pdefs.ids.message 
                    goto, new_line
                end
                
                'TTS': begin
                    topts = {graff_text}
                    gr_get_txt, topts, 0, ilu, pdefs.ids.message, /template
                    pdefs.text_options = topts
                    goto, new_line
                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_val(tag_val(itag+1), 1)
                'HE': pdefs.hardset.eps = gr_byt_val(tag_val(itag+1), 1)
                'HO': pdefs.hardset.orient = gr_byt_val(tag_val(itag+1), 1)
                'HP': pdefs.hardset.psize = gr_byt_val(tag_val(itag+1), 1)
                'HT': pdefs.hardset.timestamp = gr_byt_val(tag_val(itag+1), 1)
                'HS' :pdefs.hardset.size = gr_flt_val(tag_val(itag+1), 2)
                'HD' :pdefs.hardset.off = gr_flt_val(tag_val(itag+1), 2)

                'HAB': begin
                    pdefs.hardset.action(0) = gr_str_val(inline, 'HAB')
                    goto, new_line
                end
                'HAA': begin
                    pdefs.hardset.action(1) = gr_str_val(inline, 'HAA')
                    goto, new_line
                end

                'HF': pdefs.hardset.font.family = $
                  gr_int_val(tag_val(itag+1), 1)
                'HWS': pdefs.hardset.font.wg_sl = $
                  gr_int_val(tag_val(itag+1), 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_val(tag_val(itag+1), 1)
                'KX': pdefs.key.x = gr_flt_val(tag_val(itag+1), 2)
                'KY': pdefs.key.y = gr_flt_val(tag_val(itag+1), 2)
                'KN': pdefs.key.norm = gr_int_val(tag_val(itag+1), 1)
                'KC': pdefs.key.cols = gr_int_val(tag_val(itag+1), 1)
                'KF': pdefs.key.frame = gr_byt_val(tag_val(itag+1), 1)
                'KP': pdefs.key.one_point = gr_byt_val(tag_val(itag+1), 1)
                'KT': begin
                    pdefs.key.title = gr_str_val(inline, 'KT')
                    goto, new_line
                end
                'KLN': n_key_list = gr_lon_val(tag_val(itag+1), 1)
                'KL': begin
                    list = gr_lon_val(tag_val(itag+1), n_key_list)
                    handle_value, pdefs.key.list, list, /set, /no_copy
                end
        
                                ; REM - Remarks attached to the file
        
                'REM': begin
                    nrem = gr_lon_val(tag_val(itag+1), 1)
                    remarks = strarr(nrem)
                    for j = 0, nrem-1 do begin
                        readf, ilu, inline
                        remarks(j) = inline
                    endfor
                    handle_value, pdefs.remarks, remarks, /set, $
                      /no_copy
                end
                
                                ; This probably means that the file is
                                ; corrupted. 
                
                Else: graff_msg, pdefs.ids.message, "Unknown tag: " + $
                  tag_val(itag) + "Ignoring."
            endcase
        endfor
        
New_line:
        
    endwhile

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

endelse

free_lun, ilu

pdefs.chflag = 0                ; Clear changes flag

if (not keyword_set(no_set)) then begin
    graff_set_vals, pdefs
    pv = total(pdefs.version*[1., .01])
    if (pv ne file_v) then graff_msg, pdefs.ids.message, $
      ['File and program versions differ', $
       'File: '+string(file_v, format = "(f5.2)")+ $
       '  Program: '+string(pdefs.version, format = "(I2,'.',I2.2)")]
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