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