Viewing contents of file '../idllib/contrib/tappin/graffer/gr_fun_read.pro'
function Gr_fun_read, pdefs

;+
; GR_FUN_READ
;	Get a function from a data file
;
; Usage:
;	ichange = gr_fun_read(pdefs)
;
; Return value:
;	ichange	int	1 if the DO button was used, 0 if cancel
;
; Argument:
;	pdefs	struct	in/out	The graffer plot structure.
;
; History:
;	Original (after gr_xy_read): 17/12/96; SJT
;	Made to function returning "cancel" state: 18/12/96; SJT
;-

junk = findfile(pdefs.ds_dir, count = num)
if (num ge 1) then path = pdefs.ds_dir $
else cd, current = path

widget_control, pdefs.ids.graffer, sensitive = 0
f = gr_pickfile(filter = '*.dat', title = 'Graffer function data', $
                /must, path = path, group = pdefs.ids.graffer, $
                get_path = newpath, pop = pdefs.popflag)
widget_control, pdefs.ids.graffer, sensitive = 1

if (f eq '') then return, 0

pdefs.ds_dir = newpath

on_ioerror, badfile

openr, ilu, /get, f
    
dv = ''
readf, ilu, dv

case strcompress(/remove, strupcase(dv)) of
    'Y': begin
        range = fltarr(2)
        nval = 0
        func = ''
        type = -1
    end
    'X': begin
        range = fltarr(2)
        nval = 0
        func = ''
        type = -2
    end
    'XY': begin
        range = fltarr(2)
        nval = 0
        func = strarr(2)
        type = -3
    end
    'Z': begin
        range = fltarr(2, 2)
        nval = intarr(2)
        func = ''
        type = -4
    end
    Else: begin
        free_lun, ilu
        graff_msg, pdefs.ids.message, ["Graffer function read failed:",  $
                                       "Unknown function type code"]
        return, 0
    end
endcase

readf, ilu, range
readf, ilu, nval
readf, ilu, func

free_lun, ilu

handle_value, pdefs.data, data, /no_copy

data(pdefs.cset).ndata = nval(0)
if (type eq -4) then data(pdefs.cset).ndata2 = nval(1)
data(pdefs.cset).type = type

if (type eq -1 or type eq -2) then xydata = {graff_funct,  $
                                             Range: range,  $
                                             Funct:func $
                                            } $
else if (type eq -3) then  xydata = {graff_pfunct, $
                                     Range: range, $
                                     Funct:func $
                                    } $
else  xydata = {graff_zfunct, $
                Range: range, $
                Funct:func $
               }

handle_value, data(pdefs.cset).xydata, xydata, /no_copy, /set

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

return, 1

Badfile:

graff_msg, pdefs.ids.message, ["Graffer function read failed:", $
                               !Err_string]

return, 0

end