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