1 | ;------------------------------------------------------------ |
---|
2 | ;------------------------------------------------------------ |
---|
3 | ;------------------------------------------------------------ |
---|
4 | ;+ |
---|
5 | ; @file_comments 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 |
---|
9 | ; given back by the function. (see examples) |
---|
10 | ; |
---|
11 | ; @keyword FILENAMEIN {in} name of the funccedure to be created. |
---|
12 | ; 'for_createfunc.pro' by default |
---|
13 | ; |
---|
14 | ; @keyword KWDLIST {in} a vector string. to specify a list of keywords that |
---|
15 | ; must be included in the function definition. Warning: the string |
---|
16 | ; must start with a ',' for example: KWDLIST = ', TOTO = toto' |
---|
17 | ; |
---|
18 | ; @keyword _EXTRA used to pass your keywords to the created function. |
---|
19 | ; |
---|
20 | ; @restrictions |
---|
21 | ; - arguments can be given only through keywords; |
---|
22 | ; - ends the function name with '.pro' if needed. |
---|
23 | ; |
---|
24 | ; @examples |
---|
25 | ; IDL> print, createfunc('3*2', filename='test') |
---|
26 | ; IDL> print, createfunc('3*two', filename = 'test' $ |
---|
27 | ; , kwdlist ='two = two', two = 2) |
---|
28 | ; |
---|
29 | ; @history Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
30 | ; May 2005 |
---|
31 | ; |
---|
32 | ; @version $Id$ |
---|
33 | ; |
---|
34 | ;- |
---|
35 | ;------------------------------------------------------------ |
---|
36 | ;------------------------------------------------------------ |
---|
37 | ;------------------------------------------------------------ |
---|
38 | FUNCTION createfunc, command, FILENAMEIN = filenamein $ |
---|
39 | , KWDLIST = kwdlist, _EXTRA = ex |
---|
40 | ; |
---|
41 | compile_opt idl2, hidden, strictarrsubs |
---|
42 | ; |
---|
43 | IF n_elements(command) NE 1 THEN stop |
---|
44 | ; define filename if needed |
---|
45 | if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $ |
---|
46 | ELSE filename = filenamein |
---|
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 = '' |
---|
57 | kwdlist = kwdlist + ', _EXTRA = ex' |
---|
58 | IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' + kwdlist |
---|
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 |
---|
70 | res = call_function(shortfilename, _EXTRA = ex) |
---|
71 | ; |
---|
72 | return, res |
---|
73 | end |
---|