[2] | 1 | ;+ |
---|
[150] | 2 | ; @file_comments |
---|
| 3 | ; extract elements of a structure to constitute a new structure. |
---|
[2] | 4 | ; |
---|
[150] | 5 | ; @categories |
---|
[157] | 6 | ; Utilities |
---|
[2] | 7 | ; |
---|
[150] | 8 | ; @param STRU {in}{required} |
---|
| 9 | ; A structure |
---|
[2] | 10 | ; |
---|
[163] | 11 | ; @param LISTE {in}{required}{type=vector} |
---|
[226] | 12 | ; A vector of string including names of STRU to be deleted |
---|
[150] | 13 | ; (by default) or to be kept (if KEEP is activated). |
---|
[2] | 14 | ; |
---|
[150] | 15 | ; @keyword KEEP |
---|
| 16 | ; Specify that the given liste concern elements of STRU to be kept. |
---|
[2] | 17 | ; |
---|
[163] | 18 | ; @keyword DELETE |
---|
[150] | 19 | ; Specify that the given liste concern elements of STRU to be deleted. |
---|
| 20 | ; This keyword is activated by default. |
---|
[2] | 21 | ; |
---|
[150] | 22 | ; @returns |
---|
| 23 | ; A structure or -1 in case of problem |
---|
[2] | 24 | ; |
---|
[150] | 25 | ; @examples |
---|
[2] | 26 | ; |
---|
| 27 | ; IDL> extra=get_extra(/ok, year=1999, age_capitaine=35 ) |
---|
| 28 | ; IDL> help, extra,/struct |
---|
| 29 | ; ** Structure <83e66bc>, 3 tags, length=6, refs=1: |
---|
| 30 | ; AGE_CAPITAINE INT 35 |
---|
| 31 | ; OK INT 1 |
---|
| 32 | ; YEAR INT 1999 |
---|
| 33 | ; IDL> help, extractstru(extra,['ok','hhuihi','YEAR']),/stru |
---|
| 34 | ; ** Structure <831afac>, 1 tags, length=2, refs=1: |
---|
| 35 | ; AGE_CAPITAINE INT 35 |
---|
[150] | 36 | ; IDL> help, extractstru(extra,['ok','hhuihi','YEAR'],/keep),/stru |
---|
[2] | 37 | ; ** Structure <834bbc4>, 2 tags, length=4, refs=1: |
---|
| 38 | ; OK INT 1 |
---|
| 39 | ; YEAR INT 1999 |
---|
| 40 | ; |
---|
[150] | 41 | ; @history |
---|
[157] | 42 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[2] | 43 | ; 8/10/1999 |
---|
[150] | 44 | ; |
---|
| 45 | ; @version |
---|
| 46 | ; $Id$ |
---|
[226] | 47 | ; |
---|
[2] | 48 | ;- |
---|
[232] | 49 | ; |
---|
[150] | 50 | FUNCTION extractstru, stru, liste, KEEP = keep, DELETE = delete |
---|
[114] | 51 | ; |
---|
| 52 | compile_opt idl2, strictarrsubs |
---|
| 53 | ; |
---|
[2] | 54 | if size(stru, /type) NE 8 then return, -1 |
---|
| 55 | if size(liste, /type) NE 7 then return, -1 |
---|
[226] | 56 | ; checking for keep and vire keywords |
---|
[150] | 57 | keep = keyword_set(keep)*(1-keyword_set(delete)) |
---|
| 58 | delete = keyword_set(delete)*(1-keyword_set(keep)) +(keyword_set(delete) EQ keep) |
---|
[2] | 59 | ; |
---|
| 60 | tname = tag_names(stru) |
---|
| 61 | index = make_selection(tname, strupcase(liste), /only_valid, /quiet) |
---|
| 62 | ; |
---|
[150] | 63 | if keep then BEGIN ; We just keep the list |
---|
[2] | 64 | if index[0] EQ -1 then return, -1 |
---|
| 65 | if n_elements(index) EQ n_elements(tname) then return, stru |
---|
| 66 | res = create_struct(tname[index[0]], stru.(index[0])) |
---|
| 67 | if n_elements(index) GT 1 then for i = 1, n_elements(index)-1 do $ |
---|
| 68 | res = create_struct(res, tname[index[i]], stru.(index[i])) |
---|
[150] | 69 | ENDIF ELSE BEGIN ; We delete the list |
---|
[2] | 70 | if n_elements(index) EQ n_elements(tname) then return, -1 |
---|
| 71 | if index[0] EQ -1 then return, stru |
---|
[150] | 72 | ; We take the complementary one of index to obtain indexes we keep. |
---|
[2] | 73 | index = different(indgen(n_elements(tname)), index) |
---|
| 74 | res = create_struct(tname[index[0]], stru.(index[0])) |
---|
| 75 | if n_elements(index) GT 1 then for i = 1, n_elements(index)-1 do $ |
---|
| 76 | res = create_struct(res, tname[index[i]], stru.(index[i])) |
---|
[226] | 77 | ENDELSE |
---|
[2] | 78 | |
---|
| 79 | return, res |
---|
| 80 | end |
---|