[69] | 1 | ;+ |
---|
[231] | 2 | ; |
---|
[128] | 3 | ; @file_comments |
---|
[136] | 4 | ; write an IDL function, compile it and execute it. |
---|
| 5 | ; useful to avoid the use of execute |
---|
[69] | 6 | ; |
---|
[242] | 7 | ; @param command {in}{required} {type=scalar string} |
---|
| 8 | ; defining the result to be given back by the function. |
---|
[136] | 9 | ; (see examples) |
---|
[69] | 10 | ; |
---|
[128] | 11 | ; @keyword FILENAMEIN {in} {default=for_createfunc.pro} |
---|
[121] | 12 | ; name of the function to be created. |
---|
[118] | 13 | ; |
---|
[242] | 14 | ; @keyword KWDLIST {in} {type=vector string} |
---|
| 15 | ; to specify a list of keywords that must be included in the |
---|
[136] | 16 | ; function definition. |
---|
| 17 | ; Warning: the string must start with a ',' |
---|
[327] | 18 | ; for example: KWDLIST=', TOTO=toto' |
---|
[69] | 19 | ; |
---|
[136] | 20 | ; @keyword _EXTRA |
---|
[231] | 21 | ; Used to pass keywords to the created function. |
---|
[69] | 22 | ; |
---|
[128] | 23 | ; @restrictions |
---|
[118] | 24 | ; - arguments can be given only through keywords; |
---|
| 25 | ; - ends the function name with '.pro' if needed. |
---|
[69] | 26 | ; |
---|
[118] | 27 | ; @examples |
---|
[69] | 28 | ; |
---|
[371] | 29 | ; IDL> print, createfunc('3*2', FILENAMEIN='test') |
---|
| 30 | ; IDL> print, createfunc('3*two', FILENAMEIN='test' $ |
---|
| 31 | ; IDL> , KWDLIST='two = two', TWO=2) |
---|
| 32 | ; |
---|
[224] | 33 | ; @history |
---|
| 34 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[74] | 35 | ; May 2005 |
---|
[118] | 36 | ; |
---|
[224] | 37 | ; @version |
---|
| 38 | ; $Id$ |
---|
[118] | 39 | ; |
---|
[69] | 40 | ;- |
---|
[327] | 41 | FUNCTION createfunc, command, FILENAMEIN=filenamein $ |
---|
| 42 | , KWDLIST=kwdlist, _EXTRA=ex |
---|
[69] | 43 | ; |
---|
| 44 | compile_opt idl2, hidden, strictarrsubs |
---|
| 45 | ; |
---|
[269] | 46 | usage="result=createfunc(command, FILENAMEIN=filenamein , KWDLIST=kwdlist, _EXTRA=ex)" |
---|
| 47 | ; |
---|
| 48 | IF n_elements(command) NE 1 THEN BEGIN |
---|
| 49 | dummy = report(['Input parameter command empty', $ |
---|
| 50 | 'Usage : ' + usage]) |
---|
| 51 | stop |
---|
| 52 | ENDIF |
---|
[69] | 53 | ; define filename if needed |
---|
| 54 | if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $ |
---|
[118] | 55 | ELSE filename = filenamein |
---|
[69] | 56 | ; get the name of the function (not the name of the file containing the function) |
---|
| 57 | shortfilename = file_basename(filename, '.pro') |
---|
| 58 | ; check if the directory exists |
---|
| 59 | dirname = isadirectory(file_dirname(filename) $ |
---|
| 60 | , title = 'Redefine '+shortfilename+'.pro directory') |
---|
| 61 | IF size(dirname, /type) NE 7 THEN return, -1 |
---|
| 62 | ; |
---|
| 63 | filename = dirname + shortfilename + '.pro' |
---|
| 64 | ; create the file |
---|
| 65 | if NOT keyword_set(kwdlist) then kwdlist = '' |
---|
[118] | 66 | kwdlist = kwdlist + ', _EXTRA = ex' |
---|
[74] | 67 | IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' + kwdlist |
---|
[69] | 68 | putfile, filename, ['function ' + shortfilename + kwdlist $ |
---|
| 69 | , 'compile_opt idl2, hidden, strictarrsubs' $ |
---|
| 70 | , 'res = ' + command $ |
---|
| 71 | , 'return, res' $ |
---|
| 72 | , 'end'] |
---|
[229] | 73 | ; is dirname in !path? |
---|
| 74 | cd, current = here |
---|
| 75 | pathlist = [here, strsplit(!path, path_sep(/search_path),/extract)] |
---|
[243] | 76 | inpath = total((file_search(dirname, /fully_qualify_path))[0] EQ pathlist) |
---|
| 77 | IF inpath EQ 0 THEN !path = dirname + path_sep(/search_path) + !path |
---|
[229] | 78 | ; update the list of .pro and .sav in !PATH |
---|
| 79 | path_cache, /rebuild |
---|
[239] | 80 | ; do we really use shortfilename? |
---|
| 81 | list = find(shortfilename, /onlypro, /firstfound) |
---|
| 82 | IF list[0] NE filename THEN BEGIN ; it is ok if filename is the first one |
---|
| 83 | dummy = report(['Several files ' + shortfilename + ' are found in the !path and' $ |
---|
| 84 | , list[0] + ' we be used instead of', filename, 'We stop...'], /simple) |
---|
| 85 | stop |
---|
| 86 | ENDIF |
---|
[69] | 87 | ; compile it |
---|
| 88 | resolve_routine, shortfilename, /is_function |
---|
| 89 | ; execute it |
---|
[118] | 90 | res = call_function(shortfilename, _EXTRA = ex) |
---|
[69] | 91 | ; |
---|
| 92 | return, res |
---|
| 93 | end |
---|