dustem_mask_data.pro 5.77 KB
FUNCTION dustem_mask_data,data_struct,omit=omit,data_set=data_set,help=help
;+
; NAME:
;    dustem_mask_data
; PURPOSE:
;    Removing filters from a data structure.
; CATEGORY:
;    DUSTEM Wrapper
; CALLING SEQUENCE:
;    data_struct=dustem_mask_data(data_struct,omit=,data_set=)
; INPUTS:
;    data_struct=read_xcat(file,/silent). Data structure formatted as outlined in the DustermWrap Users' Guide
;    data_set : Array of character strings containing the different data sets to be filtered.
;    ie: ['SED'],['QSED'],'USED'] or just ['QSED'] for example. The use of ['STOKESI','STOKESQ','STOKESU'] instead is equivalent.
;    omit : Can be an array or list. In both cases it should contain the filter names as character strings. 
;    ie: omit=['SPIRE1','SPIRE2'] for an array or omit=list(['SPIRE2','SPIRE1','PACS2'],['HFI5','HFI4']) for a list.
;    When using a list, its shape (dimension) should be the number of elements of the data_set array.
;    ie: Each dimension is an array of filters to be removed from the corresponding data set in the data_set array.
; OPTIONAL INPUT PARAMETERS:
;    None
; OUTPUTS:
;    data_struct 
; OPTIONAL OUTPUT PARAMETERS:
;    None
; ACCEPTED KEY-WORDS:
;    help                  = if set, prints this help
; COMMENTS
;   Both list and array entries for omit and hide keywords are allowed.
; MODIFICATION HISTORY:
;    Written by IC 
;-


IF keyword_set(help) THEN BEGIN
  doc_library,'dustem_filter_data'
  goto,the_end
ENDIF

IF keyword_set(data_struct) and keyword_set(omit) THEN BEGIN

    ;Array of filters present in dustem
    filters_dustem=[((*!dustem_filters).(0).filter_names)]
    
    for i=1L,n_elements(tag_names(*!dustem_filters))-1 do begin
        filters_dustem=[filters_dustem,((*!dustem_filters).(i).filter_names)] 
    endfor 
    
    ;Array of filters present in data_struct
    filters_data = data_struct.filter
    
    ;Tag names present in data_struct (to be able to test on the different data sets)
    tagnames = tag_names(data_struct)
   
    
    if typename(omit) EQ 'LIST' then ind0 = omit[*] else ind0=intarr(n_elements(omit)) ; this is a better condition because it is also valid for when the user wants to omit 'spectrum' (non-filter) data points.
    ;because types can be float or double but not long. 
    
    for i=0L,n_elements(omit)-1 do begin
        dim_i = n_elements(omit(i)) ;getting the dimension of each element of the list/array
        ind0[i] = intarr(dim_i) + la_undef() ;initializing to la_undef()     
    endfor
    
    ;Gathering the indices of the filters to remove.        
    
    for i=0L,n_elements(omit)-1 do begin
        
        dim_i=n_elements(omit(i)) ;Getting the dimension of each element of the list

        arr_i=intarr(dim_i) + la_undef() ;initializing to la_undef()
        
        for j=0L,dim_i-1 do begin
            
            ;testing whether the element corresponds to a specific wavelength. 
            ;accepts both numbers/strings
            
            if valid_num((omit[i])[j]) then begin
                
                ind=where(float((omit[i])[j]) EQ data_struct.wave,test0)
                if test0 ne 0 then arr_i[j] = ind else message, 'WAVELENGTH '+strtrim((omit[i])[j],2)+' does not correspond to any spectrum point in the supplied data strucure.'
            endif else begin
                
                ind = where((omit[i])[j] EQ filters_dustem,test1) ;Test if the filter is in the wrapper's database
                if test1 eq 0 then message, 'Filter '+strtrim((omit[i])[j],2)+' cannot be found in the DustemWrap database.'
                
                ind = where((omit[i])[j] EQ filters_data,test2) ;Test if the filter in in the data itself
                if test2 ne 0 then arr_i[j] = ind else message, 'Filter '+strtrim((omit[i])[j],2)+' cannot be found in the data supplied.'  
            
            endelse
               
        endfor
        ind0[i] = arr_i ;Indices of the filters to be removed in the data structure
    endfor

        eq_tags = ['STOKESI','EXT_I','EXT_P','','','','','STOKESQ','STOKESU','EXT_Q','EXT_U']
        errvec = ['SIGMAII','SIGEXTI','SIGEXTP','','','','','SIGMAQQ','SIGMAUU','SIGEXTQ','SIGEXTU']
    
    IF keyword_set(data_set) then begin
        for i=0L,n_elements(data_set)-1 do begin 
            ind_set = where(tagnames EQ data_set(i),ctset)
            ;here using eq_tags and errvec for another purpose
            ;because I need to locate the associated errors
            if ctset ne 0 then begin
                inderr = where(eq_tags EQ data_set(i)) ;no need for counters this is supposedly true at all times plus the error message is below in the next test
                ind_errset = where(tagnames EQ errvec[inderr[0]])
            endif else begin
               ind_set1 = where(tag_names(!dustem_data) EQ data_set(i),ctset)
               if ctset eq 0 then message, 'Aborting. The supplied data set is not found in the DustemWrap database.'
               ind_set = where(tagnames EQ eq_tags(ind_set1[0]))
               ind_errset = where(tagnames EQ errvec(ind_set1[0]))
            endelse
            
            if typename(ind0) EQ 'LIST' then begin 
                data_struct(ind0[i]).(ind_set) = la_undef()
                data_struct(ind0[i]).(ind_errset) = la_undef() 
            endif else begin
                data_struct(ind0).(ind_set) = la_undef()
                data_struct(ind0).(ind_errset) = la_undef()  
            endelse
             
        endfor
    endif else begin
        ind_tags = where(tagnames ne 'INSTRU' and tagnames ne 'FILTER' and tagnames ne 'WAVE')
        for j=0L,n_elements(ind_tags)-1 do begin
            data_struct(ind0).(ind_tags(j)) = la_undef()               
        endfor 
    endelse

    
ENDIF ELSE message, 'Cannot proceed. Keyword(s) missing. Refer to help for keyword options.'

return, data_struct

the_end:


END