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