1 | ;------------------------------------------------------------ |
---|
2 | ;------------------------------------------------------------ |
---|
3 | ;------------------------------------------------------------ |
---|
4 | ;+ |
---|
5 | ; NAME:createfunc |
---|
6 | ; |
---|
7 | ; PURPOSE: write an idl function, compile it and execute it. |
---|
8 | ; usefull to avoid the use of execute |
---|
9 | ; |
---|
10 | ; CATEGORY: |
---|
11 | ; |
---|
12 | ; CALLING SEQUENCE:res = createfunc(command) |
---|
13 | ; |
---|
14 | ; INPUTS: |
---|
15 | ; command: a scalar string defining the result to be byven back by the |
---|
16 | ; function. (see examples) |
---|
17 | ; |
---|
18 | ; KEYWORD PARAMETERS: |
---|
19 | ; |
---|
20 | ; FILENAMEIN: name of the funccedure to be created. |
---|
21 | ; 'for_createfunc.pro' by default |
---|
22 | ; |
---|
23 | ; KWDLIST: a vector string. to specify a list of keywords that |
---|
24 | ; must be included in the function definition. Warning: the string |
---|
25 | ; must start with a ',' for example: KWDLIST = ', TOTO = toto' |
---|
26 | ; |
---|
27 | ; _EXTRA: used to pass your keywords to the created function. |
---|
28 | ; |
---|
29 | ; OUTPUTS: none |
---|
30 | ; |
---|
31 | ; COMMON BLOCKS: none |
---|
32 | ; |
---|
33 | ; SIDE EFFECTS: ends the function name with '.pro' if needed |
---|
34 | ; |
---|
35 | ; RESTRICTIONS:arguments can be given only through keywords |
---|
36 | ; |
---|
37 | ; EXAMPLE: |
---|
38 | ; IDL> print, createfunc('3*2', filename='test') |
---|
39 | ; IDL> print, createfunc('3*two', filename = 'test' $ |
---|
40 | ; , kwdlist ='two = two', two = 2) |
---|
41 | ; |
---|
42 | ; MODIFICATION HISTORY:Sebastien Masson (smasson@lodyc.jussieu.fr) |
---|
43 | ; May 2005 |
---|
44 | ;- |
---|
45 | ;------------------------------------------------------------ |
---|
46 | ;------------------------------------------------------------ |
---|
47 | ;------------------------------------------------------------ |
---|
48 | FUNCTION createfunc, command, FILENAMEIN = filenamein $ |
---|
49 | , KWDLIST = kwdlist, _extra = ex |
---|
50 | ; |
---|
51 | compile_opt idl2, hidden, strictarrsubs |
---|
52 | ; |
---|
53 | IF n_elements(command) NE 1 THEN stop |
---|
54 | ; define filename if needed |
---|
55 | if NOT keyword_set(filenamein) then filename = 'for_createfunc.pro' $ |
---|
56 | ELSE filename = filenamein |
---|
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 = '' |
---|
67 | kwdlist = kwdlist + ', _extra = ex' |
---|
68 | IF strmid(kwdlist, 0, 1) NE ',' THEN kwdlist = ', ' + kwdlist |
---|
69 | putfile, filename, ['function ' + shortfilename + kwdlist $ |
---|
70 | , 'compile_opt idl2, hidden, strictarrsubs' $ |
---|
71 | , 'res = ' + command $ |
---|
72 | , 'return, res' $ |
---|
73 | , 'end'] |
---|
74 | ; go in dirname directory |
---|
75 | cd, dirname, current = old_dir |
---|
76 | ; compile it |
---|
77 | resolve_routine, shortfilename, /is_function |
---|
78 | cd, old_dir |
---|
79 | ; execute it |
---|
80 | res = call_function(shortfilename, _extra = ex) |
---|
81 | ; |
---|
82 | return, res |
---|
83 | end |
---|