[69] | 1 | ;------------------------------------------------------------ |
---|
| 2 | ;------------------------------------------------------------ |
---|
| 3 | ;------------------------------------------------------------ |
---|
| 4 | ;+ |
---|
[97] | 5 | ; @file_comments write an idl function, compile it and execute it. |
---|
[93] | 6 | ; usefull to avoid the use of execute |
---|
[69] | 7 | ; |
---|
[118] | 8 | ; @param command {in}{required} a scalar string defining the result to be |
---|
| 9 | ; given back by the function. (see examples) |
---|
[69] | 10 | ; |
---|
[93] | 11 | ; @keyword FILENAMEIN {in} name of the funccedure to be created. |
---|
[74] | 12 | ; 'for_createfunc.pro' by default |
---|
[118] | 13 | ; |
---|
[93] | 14 | ; @keyword KWDLIST {in} a vector string. to specify a list of keywords that |
---|
[74] | 15 | ; must be included in the function definition. Warning: the string |
---|
[69] | 16 | ; must start with a ',' for example: KWDLIST = ', TOTO = toto' |
---|
| 17 | ; |
---|
[93] | 18 | ; @keyword _EXTRA used to pass your keywords to the created function. |
---|
[69] | 19 | ; |
---|
[118] | 20 | ; @restrictions |
---|
| 21 | ; - arguments can be given only through keywords; |
---|
| 22 | ; - ends the function name with '.pro' if needed. |
---|
[69] | 23 | ; |
---|
[118] | 24 | ; @examples |
---|
[74] | 25 | ; IDL> print, createfunc('3*2', filename='test') |
---|
| 26 | ; IDL> print, createfunc('3*two', filename = 'test' $ |
---|
| 27 | ; , kwdlist ='two = two', two = 2) |
---|
[69] | 28 | ; |
---|
[93] | 29 | ; @history Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[74] | 30 | ; May 2005 |
---|
[118] | 31 | ; |
---|
| 32 | ; @version $Id$ |
---|
| 33 | ; |
---|
[69] | 34 | ;- |
---|
| 35 | ;------------------------------------------------------------ |
---|
| 36 | ;------------------------------------------------------------ |
---|
| 37 | ;------------------------------------------------------------ |
---|
| 38 | FUNCTION createfunc, command, FILENAMEIN = filenamein $ |
---|
[118] | 39 | , KWDLIST = kwdlist, _EXTRA = ex |
---|
[69] | 40 | ; |
---|
| 41 | compile_opt idl2, hidden, strictarrsubs |
---|
| 42 | ; |
---|
[74] | 43 | IF n_elements(command) NE 1 THEN stop |
---|
[69] | 44 | ; define filename if needed |
---|
| 45 | if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $ |
---|
[118] | 46 | ELSE filename = filenamein |
---|
[69] | 47 | ; get the name of the function (not the name of the file containing the function) |
---|
| 48 | shortfilename = file_basename(filename, '.pro') |
---|
| 49 | ; check if the directory exists |
---|
| 50 | dirname = isadirectory(file_dirname(filename) $ |
---|
| 51 | , title = 'Redefine '+shortfilename+'.pro directory') |
---|
| 52 | IF size(dirname, /type) NE 7 THEN return, -1 |
---|
| 53 | ; |
---|
| 54 | filename = dirname + shortfilename + '.pro' |
---|
| 55 | ; create the file |
---|
| 56 | if NOT keyword_set(kwdlist) then kwdlist = '' |
---|
[118] | 57 | kwdlist = kwdlist + ', _EXTRA = ex' |
---|
[74] | 58 | IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' + kwdlist |
---|
[69] | 59 | putfile, filename, ['function ' + shortfilename + kwdlist $ |
---|
| 60 | , 'compile_opt idl2, hidden, strictarrsubs' $ |
---|
| 61 | , 'res = ' + command $ |
---|
| 62 | , 'return, res' $ |
---|
| 63 | , 'end'] |
---|
| 64 | ; go in dirname directory |
---|
| 65 | cd, dirname, current = old_dir |
---|
| 66 | ; compile it |
---|
| 67 | resolve_routine, shortfilename, /is_function |
---|
| 68 | cd, old_dir |
---|
| 69 | ; execute it |
---|
[118] | 70 | res = call_function(shortfilename, _EXTRA = ex) |
---|
[69] | 71 | ; |
---|
| 72 | return, res |
---|
| 73 | end |
---|