;+ ; @file_comments ; extract elements of a structure to constitute a new structure. ; ; @categories ; Utilities ; ; @param STRU {in}{required} ; A structure ; ; @param LISTE {in}{required}{type=vector} ; A vector of string including names of STRU to be deleted ; (by default) or to be kept (if KEEP is activated). ; ; @keyword KEEP ; Specify that the given liste concern elements of STRU to be kept. ; ; @keyword DELETE ; Specify that the given liste concern elements of STRU to be deleted. ; This keyword is activated by default. ; ; @returns ; A structure or -1 in case of problem ; ; @examples ; ; IDL> extra=get_extra(/ok, year=1999, age_capitaine=35 ) ; IDL> help, extra,/struct ; ** Structure <83e66bc>, 3 tags, length=6, refs=1: ; AGE_CAPITAINE INT 35 ; OK INT 1 ; YEAR INT 1999 ; IDL> help, extractstru(extra,['ok','hhuihi','YEAR']),/stru ; ** Structure <831afac>, 1 tags, length=2, refs=1: ; AGE_CAPITAINE INT 35 ; IDL> help, extractstru(extra,['ok','hhuihi','YEAR'],/keep),/stru ; ** Structure <834bbc4>, 2 tags, length=4, refs=1: ; OK INT 1 ; YEAR INT 1999 ; ; @history ; Sebastien Masson (smasson\@lodyc.jussieu.fr) ; 8/10/1999 ; ; @version ; $Id$ ; ;- FUNCTION extractstru, stru, liste, KEEP=keep, DELETE=delete ; compile_opt idl2, strictarrsubs ; if size(stru, /type) NE 8 then return, -1 if size(liste, /type) NE 7 then return, -1 ; checking for keep and vire keywords keep = keyword_set(keep)*(1-keyword_set(delete)) delete = keyword_set(delete)*(1-keyword_set(keep)) +(keyword_set(delete) EQ keep) ; tname = tag_names(stru) index = make_selection(tname, strupcase(liste), /only_valid, /quiet) ; if keep then BEGIN ; We just keep the list if index[0] EQ -1 then return, -1 if n_elements(index) EQ n_elements(tname) then return, stru res = create_struct(tname[index[0]], stru.(index[0])) if n_elements(index) GT 1 then for i = 1, n_elements(index)-1 do $ res = create_struct(res, tname[index[i]], stru.(index[i])) ENDIF ELSE BEGIN ; We delete the list if n_elements(index) EQ n_elements(tname) then return, -1 if index[0] EQ -1 then return, stru ; We take the complementary one of index to obtain indexes we keep. index = different(indgen(n_elements(tname)), index) res = create_struct(tname[index[0]], stru.(index[0])) if n_elements(index) GT 1 then for i = 1, n_elements(index)-1 do $ res = create_struct(res, tname[index[i]], stru.(index[i])) ENDELSE return, res end