1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; write an IDL function, compile it and execute it. |
---|
5 | ; useful to avoid the use of execute |
---|
6 | ; |
---|
7 | ; @param command {in}{required} {type=scalar string} |
---|
8 | ; defining the result to be given back by the function. |
---|
9 | ; (see examples) |
---|
10 | ; |
---|
11 | ; @keyword FILENAMEIN {in} {default=for_createfunc.pro} |
---|
12 | ; name of the function to be created. |
---|
13 | ; |
---|
14 | ; @keyword KWDLIST {in} {type=vector string} |
---|
15 | ; to specify a list of keywords that must be included in the |
---|
16 | ; function definition. |
---|
17 | ; |
---|
18 | ; Warning: the string must start with a ',' |
---|
19 | ; for example: KWDLIST=', TOTO=toto' |
---|
20 | ; |
---|
21 | ; @keyword _EXTRA |
---|
22 | ; Used to pass keywords to the created function. |
---|
23 | ; |
---|
24 | ; @restrictions |
---|
25 | ; - arguments can be given only through keywords; |
---|
26 | ; - ends the function name with '.pro' if needed. |
---|
27 | ; |
---|
28 | ; @examples |
---|
29 | ; |
---|
30 | ; IDL> print, createfunc('3*2', FILENAMEIN='test') |
---|
31 | ; IDL> print, createfunc('3*two', FILENAMEIN='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 | FUNCTION createfunc, command, FILENAMEIN=filenamein $ |
---|
43 | , KWDLIST=kwdlist, _EXTRA=ex |
---|
44 | ; |
---|
45 | compile_opt idl2, hidden, strictarrsubs |
---|
46 | ; |
---|
47 | usage="result=createfunc(command, FILENAMEIN=filenamein , KWDLIST=kwdlist, _EXTRA=ex)" |
---|
48 | ; |
---|
49 | IF n_elements(command) NE 1 THEN BEGIN |
---|
50 | dummy = report(['Input parameter command empty', $ |
---|
51 | 'Usage : ' + usage]) |
---|
52 | stop |
---|
53 | ENDIF |
---|
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 | ; is dirname in !path? |
---|
75 | cd, current = here |
---|
76 | pathlist = [here, strsplit(!path, path_sep(/search_path),/extract)] |
---|
77 | inpath = total((file_search(dirname, /fully_qualify_path))[0] EQ pathlist) |
---|
78 | IF inpath EQ 0 THEN !path = dirname + path_sep(/search_path) + !path |
---|
79 | ; update the list of .pro and .sav in !PATH |
---|
80 | path_cache, /rebuild |
---|
81 | ; do we really use shortfilename? |
---|
82 | list = find(shortfilename, /onlypro, /firstfound) |
---|
83 | IF list[0] NE filename THEN BEGIN ; it is ok if filename is the first one |
---|
84 | dummy = report(['Several files ' + shortfilename + ' are found in the !path and' $ |
---|
85 | , list[0] + ' we be used instead of', filename, 'We stop...'], /simple) |
---|
86 | stop |
---|
87 | ENDIF |
---|
88 | ; compile it |
---|
89 | resolve_routine, shortfilename, /is_function |
---|
90 | ; execute it |
---|
91 | res = call_function(shortfilename, _EXTRA = ex) |
---|
92 | ; |
---|
93 | return, res |
---|
94 | end |
---|