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