;+ ; Name: spex_change_summ_comp ; ; Purpose: Modify spex_summ structure containing summary of fit results to add or remove fit components ; ; Input Keywords: ; summ - input summ structure, will contain modified summ structure on exit ; comp_remove - name of components to remove, string scalar or array, no default ; param_ind_remove - indices of function corresponding to comp_remove components that we will remove. If not supplied, we figure ; them out here. ; comp_ind_remove - indices of function components in comp_remove components that we will remove. If not supplied, we figure ; them out here. ; comp_add - name of components to add, string scalar or array, no default ; nodup - if set, then don't add a component in comp_add if it's already in the function ; ; Output Keywords: ; summ - input and output, see above ; status - 0 / 1 means we did not / did make deletions or additions in summ structure ; ; Written: 26-Aug-2013, Kim. Previously this functionality was inside the spex_fit::check_func method, but wanted to be ; able to call it outside of that. ; ; Modifications: ;- pro spex_change_summ_comp, summ=summ, $ comp_remove=comp_remove, param_ind_remove=param_ind_remove, comp_ind_remove=comp_ind_remove, $ comp_add=comp_add, nodup=nodup, status=status checkvar, nodup, 0 did_removes = 0 did_adds = 0 func = summ.spex_summ_fit_function func_arr = str2arr(func,'+') new_func_arr = func_arr starting_params = summ.spex_summ_starting_params params = summ.spex_summ_params sigmas = summ.spex_summ_sigmas minima = summ.spex_summ_minima maxima = summ.spex_summ_maxima free_mask = summ.spex_summ_free_mask func_spectrum = summ.spex_summ_func_spectrum func_model = summ.spex_summ_func_model if exist(comp_remove) then begin if ~exist(comp_ind_remove) then begin comp_ind_remove = where_arr(func_arr, comp_remove, count) if count eq 0 then begin message, /cont, 'Component(s) to remove were not found in function.' goto, comp_add_section endif endif did_removes = 1 if ~exist(param_ind_remove) then begin nrem = n_elements(comp_remove) for i=0,nrem-1 do begin relem = fit_function_query(func, comp=comp_remove[i], /param_elem) if relem[0] ne -1 then param_ind_remove = append_arr(param_ind_remove, relem) endfor endif ; remove indices from arrays dimensioned by number of parameters for deleted components ind = indgen(n_elements(params[*,0])) remove, param_ind_remove, ind ; ind will have indices we want to keep starting_params = starting_params[ind,*] params = params[ind,*] sigmas = sigmas[ind,*] minima = minima[ind,*] maxima = maxima[ind,*] free_mask = free_mask[ind,*] ; remove indices from arrays dimensioned by number of components for deleted components ind = indgen(n_elements(func_spectrum[*,0])) remove, comp_ind_remove, ind ; ind will have indices we want to keep new_func_arr = func_arr[ind] func_spectrum = func_spectrum[ind,*] func_model = func_model[ind,*] endif comp_add_section: if exist(comp_add) then begin ; add values for each new component at end of current arrays for each row (time interval) for i=0,n_elements(comp_add)-1 do begin if nodup then begin ind = where(func_arr eq comp_add[i], count) if count gt 0 then continue ; skip adding this component since it's already there, and nodup is set endif did_adds = 1 new_func_arr = [new_func_arr, comp_add[i]] def = fit_comp_defaults(comp_add[i]) ; for all functions except drm, set 1st param to 0. so will not contribute not_drm = strpos(comp_add[i],'drm') eq -1 ;and strpos(comp_add[i],'pileup') eq -1 if not_drm then def.fit_comp_params[0] = 0. ; index will be element # to insert into for each row index = n_elements(params[*,0]) starting_params = array_insert(starting_params, def.fit_comp_params, index) params = array_insert(params, def.fit_comp_params, index) sigmas = array_insert(sigmas, 0.*def.fit_comp_params, index) minima = array_insert(minima, def.fit_comp_minima, index) maxima = array_insert(maxima, def.fit_comp_maxima, index) free_mask = array_insert(free_mask, def.fit_comp_free_mask, index) ; put new spectrum and model strings after all current ones in each row (time interval) index = n_elements(func_spectrum[*,0]) func_spectrum = array_insert(func_spectrum, def.fit_comp_spectrum, index) func_model = array_insert(func_model, def.fit_comp_model, index) endfor endif if did_adds + did_removes gt 0 then begin new_func = arr2str(new_func_arr, '+') summ = rep_tag_value(summ, new_func, 'spex_summ_fit_function') summ = rep_tag_value(summ, starting_params, 'spex_summ_starting_params') summ = rep_tag_value(summ, params, 'spex_summ_params') summ = rep_tag_value(summ, sigmas, 'spex_summ_sigmas') summ = rep_tag_value(summ, minima, 'spex_summ_minima') summ = rep_tag_value(summ, maxima, 'spex_summ_maxima') summ = rep_tag_value(summ, free_mask, 'spex_summ_free_mask') summ = rep_tag_value(summ, func_spectrum, 'spex_summ_func_spectrum') summ = rep_tag_value(summ, func_model, 'spex_summ_func_model') message, /cont, 'Finished removing and/or inserting array elements for changed fit function.' status = 1 endif else begin message, /cont, 'No additions or deletions made in summ structure.' status = 0 endelse end