;+ ; ; @file_comments ; write an IDL function, compile it and execute it. ; useful to avoid the use of execute ; ; @param command {in}{required} {type=scalar string} ; defining the result to be given back by the function. ; (see examples) ; ; @keyword FILENAMEIN {in} {default=for_createfunc.pro} ; name of the function to be created. ; ; @keyword KWDLIST {in} {type=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' ; ; @keyword _EXTRA ; Used to pass keywords to the created function. ; ; @restrictions ; - arguments can be given only through keywords; ; - ends the function name with '.pro' if needed. ; ; @examples ; IDL> print, createfunc('3*2', FILENAMEIN='test') ; IDL> print, createfunc('3*two', FILENAMEIN='test' $ ; IDL> , KWDLIST='two = two', TWO=2) ; ; @history ; Sebastien Masson (smasson\@lodyc.jussieu.fr) ; May 2005 ; ; @version ; $Id$ ; ;- ; FUNCTION createfunc, command, FILENAMEIN = filenamein $ , KWDLIST = kwdlist, _EXTRA = ex ; compile_opt idl2, hidden, strictarrsubs ; usage="result=createfunc(command, FILENAMEIN=filenamein , KWDLIST=kwdlist, _EXTRA=ex)" ; IF n_elements(command) NE 1 THEN BEGIN dummy = report(['Input parameter command empty', $ 'Usage : ' + usage]) stop ENDIF ; 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'] ; is dirname in !path? cd, current = here pathlist = [here, strsplit(!path, path_sep(/search_path),/extract)] inpath = total((file_search(dirname, /fully_qualify_path))[0] EQ pathlist) IF inpath EQ 0 THEN !path = dirname + path_sep(/search_path) + !path ; update the list of .pro and .sav in !PATH path_cache, /rebuild ; do we really use shortfilename? list = find(shortfilename, /onlypro, /firstfound) IF list[0] NE filename THEN BEGIN ; it is ok if filename is the first one dummy = report(['Several files ' + shortfilename + ' are found in the !path and' $ , list[0] + ' we be used instead of', filename, 'We stop...'], /simple) stop ENDIF ; compile it resolve_routine, shortfilename, /is_function ; execute it res = call_function(shortfilename, _EXTRA = ex) ; return, res end