;------------------------------------------------------------ ;------------------------------------------------------------ ;------------------------------------------------------------ ;+ ; NAME:createfunc ; ; PURPOSE: write an idl function, compile it and execute it. ; usefull to avoid the use of execute ; ; CATEGORY: ; ; CALLING SEQUENCE:res = createfunc(command) ; ; INPUTS: ; command: a scalar string defining the result to be byven back by the ; function. (see examples) ; ; KEYWORD PARAMETERS: ; ; FILENAMEIN: name of the funccedure to be created. ; 'for_createfunc.pro' by default ; ; KWDLIST: a vector string. to specify a list of keywords that ; must be included in the function definition. Warning: the string ; must start with a ',' for example: KWDLIST = ', TOTO = toto' ; ; _EXTRA: used to pass your keywords to the created function. ; ; OUTPUTS: none ; ; COMMON BLOCKS: none ; ; SIDE EFFECTS: ends the function name with '.pro' if needed ; ; RESTRICTIONS:arguments can be given only through keywords ; ; EXAMPLE: ; IDL> print, createfunc('3*2', filename='test') ; IDL> print, createfunc('3*two', filename = 'test' $ ; , kwdlist ='two = two', two = 2) ; ; MODIFICATION HISTORY:Sebastien Masson (smasson@lodyc.jussieu.fr) ; May 2005 ;- ;------------------------------------------------------------ ;------------------------------------------------------------ ;------------------------------------------------------------ FUNCTION createfunc, command, FILENAMEIN = filenamein $ , KWDLIST = kwdlist, _extra = ex ; compile_opt idl2, hidden, strictarrsubs ; IF n_elements(command) NE 1 THEN stop ; define filename if needed if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $ ELSE filename = filenamein ; get the name of the function (not the name of the file containing the function) shortfilename = file_basename(filename, '.pro') ; check if the directory exists dirname = isadirectory(file_dirname(filename) $ , title = 'Redefine '+shortfilename+'.pro directory') IF size(dirname, /type) NE 7 THEN return, -1 ; filename = dirname + shortfilename + '.pro' ; create the file if NOT keyword_set(kwdlist) then kwdlist = '' kwdlist = kwdlist + ', _extra = ex' IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' + kwdlist putfile, filename, ['function ' + shortfilename + kwdlist $ , 'compile_opt idl2, hidden, strictarrsubs' $ , 'res = ' + command $ , 'return, res' $ , 'end'] ; go in dirname directory cd, dirname, current = old_dir ; compile it resolve_routine, shortfilename, /is_function cd, old_dir ; execute it res = call_function(shortfilename, _extra = ex) ; return, res end