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