dustem_mask_data.pro 11.1 KB
FUNCTION dustem_mask_data, data_struct,       $
                           filters,           $
                           data_set=data_set, $
                           default=default,   $
                           help=help

;+
; NAME:
;    dustem_mask_data
; PURPOSE:
;    Set dataset values to la_undef() for specified filter/spectrum data in an input DustEmWrap data structure.
; CATEGORY:
;    DUSTEM Wrapper
; CALLING SEQUENCE:
;    data_struct=dustem_mask_data(data_struct,filters,data_set=,/default)
; INPUTS:
;    data_struct : SED Data structure following the format explained in the DustermWrap Users' Guide
;
;    filters : Can be an array or list. In both cases it should contain the filter names as character strings. 
;    ie: filters=['SPIRE1','SPIRE2'] for an array or filters=list(['SPIRE2','SPIRE1','PACS2'],['HFI5','HFI4']) for a list.
;    When using a list, its shape (dimension) and the number of elements of the data_set array should be equal.
;    Each dimension is an array of filters to be removed from the corresponding data set in the data_set array.
;    ie: filters=list(['SPIRE2','SPIRE1','PACS2'],['LFI2','LFI3']) for datas_set=['STOKESI','STOKESQ']
; OPTIONAL INPUT PARAMETERS:
;    data_set : Array of character strings containing the different data sets to be filtered.
;    ie: ['STOKESI','STOKESQ','STOKESU'] or just ['STOKESQ'] for example.
; OUTPUTS:
;    data_struct 
; OPTIONAL OUTPUT PARAMETERS:
;    None
; ACCEPTED KEY-WORDS:
;    help                  = if set, print this help
;
;    default               = if set, remove targeted filter/spectrum point(s) from datasets that depend on the filtered dataset(s).
; EAMPLE
;    masked_structure = dustem_mask_data(data_struct,['IRAS1','IRAS2'],data_set=['STOKESI'])  
; COMMENTS
;    IC:
;    If a wavelength value is supplied instead of a filter name, the procedure masks for a spectrum point with that corresponding wavelength
;    If the dataset array isn't supplied, all the datasets in the input data structure will be considered.
;    If both filters and dataset are arrays, all mentioned filters will be masked from all mentioned datasets.
;    This procedure does not change the shape of the output structure partly because dustem_check_data.pro does.
; MODIFICATION HISTORY:
;    Written by IC 2022
;-


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

IF arg_present(data_struct) and arg_present(filters) 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(filters) EQ 'LIST' then ind0 = filters[*] else ind0=intarr(n_elements(filters))
    ;because types can be float or double but not long. 
    for i=0L,n_elements(filters)-1 do begin
        dim_i = n_elements(filters(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(filters)-1 do begin        
        dim_i=n_elements(filters(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((filters[i])[j]) then begin               
                ind=where(float((filters[i])[j]) EQ data_struct.wave,test0)
                if test0 ne 0 and data_struct[ind].filter EQ 'SPECTRUM' then arr_i[j] = ind else message, 'WAVELENGTH '+strtrim((filters[i])[j],2)+' does not correspond to any spectrum point in the supplied data strucure.'
            endif else begin  
                ind = where((filters[i])[j] EQ filters_dustem,test1) 
                if test1 eq 0 then message, "Filter "+strtrim((filters[i])[j],2)+" isn't in the DustEMWrap Filters' list"
                ind = where((filters[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((filters[i])[j],2)+' cannot be found in the supplied data structure.'              
            endelse
        endfor        
        ind0[i] = arr_i ;Indices of the filters to be removed in the data structure
    endfor
    
    If typename(ind0) EQ 'LIST' then indices = ind0[i] ELSE indices = ind0 
     
     ;If default isn't set, errors in dustem_check_data.pro can occur because of differences in tag values. A common behavior of dustem_check_data.pro
     ;example: filter removed for StokesI but not for SIGMAII
    IF keyword_set(default) THEN BEGIN
        
        ;Part pertainig to the /default keyword
        ;bunch of tests to locate datasets depending on STOKESI, STOKESQ, STOKESU, LARGEP, SMALLP and PSI     
        ;is it an emission or extinction structure?
        ind_m = where(tagnames EQ 'STOKESI', ctm)
        ind_x = where(tagnames EQ 'EXT_I', ctx)
        IF ctm NE 0 THEN BEGIN ;no counters are used in the following because it's a consequence of the test being valid.
            ;Getting the indices this way because it allows for an easy modification if/when tagnames in the xcat emission/extinction file change
            ind_set_StokesI =  fix([where(strmatch(tagnames,'STOKESI')), $
                                 where(strmatch(tagnames,'SIGMAII'))]) 
            ind_set_StokesQ = fix([where(strmatch(tagnames,'STOKESQ')),  $ 
                                where(strmatch(tagnames,'SIGMAQQ')),     $
                                where(strmatch(tagnames,'SIGMAIQ')) ])
            ind_set_StokesU = fix([where(strmatch(tagnames,'STOKESU')),  $ 
                                where(strmatch(tagnames,'SIGMAUU')),     $
                                where(strmatch(tagnames,'SIGMAIU')) ])
            ind_set_LargeP = fix([where(strmatch(tagnames,'LARGEP')),    $
                                where(strmatch(tagnames,'SIGMA_LARGEP'))])
            ind_set_smallp = fix([where(strmatch(tagnames,'SMALLP')),    $
                               where(strmatch(tagnames,'SIGMA_SMALLP'))])
            ind_set_psi = fix([where(strmatch(tagnames,'PSI')),          $
                            where(strmatch(tagnames,'SIGMA_PSI'))])                    
        ENDIF
        IF ctx NE 0 THEN BEGIN
            ind_set_EXT_I =  fix([where(strmatch(tagnames,'EXT_I')),          $ 
                               where(strmatch(tagnames,'SIGEXTII')) ]) 
            ind_set_EXT_Q = fix([where(strmatch(tagnames,'EXT_Q')),           $
                              where(strmatch(tagnames,'SIGEXTQQ')),           $
                              where(strmatch(tagnames,'SIGEXTIQ')) ])  
            ind_set_EXT_U = fix([where(strmatch(tagnames,'EXT_U')),           $
                              where(strmatch(tagnames,'SIGEXTUU')),           $
                              where(strmatch(tagnames,'SIGEXTIU')) ]) 
            ind_set_EXT_P = fix([where(strmatch(tagnames,'EXT_P')),           $
                              where(strmatch(tagnames,'SIGEXTP'))])
            ind_set_EXT_smallp = fix([where(strmatch(tagnames,'EXT_SMALLP')), $
                                   where(strmatch(tagnames,'SIGEXTSMALLP'))])
            ind_set_psi = fix([where(strmatch(tagnames,'PSI')),               $
                            where(strmatch(tagnames,'SIGEXTPSI'))])
        ENDIF
    ENDIF
   
    ;setting filters' data to la_undef()    
    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)
            if ctset ne 0 then begin
            
                ;the requires the default keyword to be set
                IF keyword_set(default)  THEN BEGIN    
                    ;EMISSION CASE
                    IF ctm NE 0 THEN BEGIN
                        ;testing data_set(i) against the above strings. 
                        test_StokesI = where(ind_set_StokesI EQ ind_set, ct_StokesI)
                        IF ct_StokesI NE 0 THEN data_struct(indices).(ind_set_StokesI) = la_undef()
                        test_StokesQ =  where(ind_set_StokesQ EQ ind_set, ct_StokesQ)
                        IF ct_StokesQ NE 0 THEN data_struct(indices).(ind_set_StokesQ) = la_undef()
                        test_StokesU = where(ind_set_StokesU EQ ind_set, ct_StokesU)
                        IF ct_StokesU NE 0 THEN data_struct(indices).(ind_set_StokesU) = la_undef()
                        test_LargeP = where(ind_set_LargeP EQ ind_set, ct_LargeP)
                        IF ct_LargeP NE 0 THEN data_struct(indices).(ind_set_LargeP) = la_undef()
                        test_smallp = where(ind_set_smallp EQ ind_set, ct_smallp)
                        IF ct_smallp NE 0 THEN data_struct(indices).(ind_set_smallp) = la_undef()
                        test_psi = where(ind_set_psi EQ ind_set, ct_psi)
                        IF ct_psi NE 0 THEN data_struct(indices).(ind_set_psi) = la_undef()
                    ENDIF     
                    ;EXTINCTION CASE
                    IF ctx NE 0 THEN BEGIN
                        ;testing data_set(i) against the above strings. 
                        test_EXT_I = where(ind_set_EXT_I EQ ind_set, ct_EXT_I)
                        IF ct_EXT_I NE 0 THEN data_struct(indices).(ind_set_EXT_I) = la_undef()
                        test_EXT_Q =  where(ind_set_EXT_Q EQ ind_set, ct_EXT_Q)
                        IF ct_EXT_Q NE 0 THEN data_struct(indices).(ind_set_EXT_Q) = la_undef()
                        test_EXT_U = where(ind_set_EXT_U EQ ind_set, ct_EXT_U)
                        IF ct_EXT_U NE 0 THEN data_struct(indices).(ind_set_EXT_U) = la_undef()
                        test_EXT_P = where(ind_set_EXT_P EQ ind_set, ct_EXT_P)
                        IF ct_EXT_P NE 0 THEN data_struct(indices).(ind_set_EXT_P) = la_undef()
                        test_EXT_smallp = where(ind_set_EXT_smallp EQ ind_set, ct_EXT_smallp)
                        IF ct_EXT_smallp NE 0 THEN data_struct(indices).(ind_set_EXT_smallp) = la_undef()
                        test_psi = where(ind_set_psi EQ ind_set, ct_psi)
                        IF ct_psi NE 0 THEN data_struct(indices).(ind_set_psi) = la_undef()
                    ENDIF         
                ENDIF ELSE data_struct(indices).(ind_set) = la_undef()            
            ENDIF
        ENDFOR
    ENDIF ELSE BEGIN ;If no datasets are supplied, all tag values are set to la_undef()
        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(indices).(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