Blame view

src/idl/dustem_check_data.pro 21.1 KB
04efbaa5   Ilyes Choubani   Correcting set_da...
1
PRO dustem_check_data,data_sed,data_ext,sed,ext,polext,polsed,polfrac,psi_em,qsed,used,qext,uext,psi_ext,fpolext
09e61d64   Ilyes Choubani   cleaned set_data ...
2

04efbaa5   Ilyes Choubani   Correcting set_da...
3
4
;+
; NAME:
edeb9236   Annie Hughes   updated doc help
5
6
;    dustem_check_data
;
04efbaa5   Ilyes Choubani   Correcting set_da...
7
; PURPOSE:
edeb9236   Annie Hughes   updated doc help
8
9
;    Verify that the input data conforms to what DustEMWrap requires
;
04efbaa5   Ilyes Choubani   Correcting set_da...
10
; CATEGORY:
edeb9236   Annie Hughes   updated doc help
11
12
;    DustEMWrap, Mid-Level, Distributed, Initialization
;
04efbaa5   Ilyes Choubani   Correcting set_da...
13
14
; CALLING SEQUENCE:
;    dustem_check_data,data_sed,data_ext,sed,ext,polext,polsed,polfrac,psi_em,qsed,used,qext,uext,psi_ext,fpolext,[/help]
edeb9236   Annie Hughes   updated doc help
15
;
04efbaa5   Ilyes Choubani   Correcting set_da...
16
17
18
; INPUTS:
;    data_sed : Emission dustemwrap structure
;    data_ext : Extinction dustemwrap structure
edeb9236   Annie Hughes   updated doc help
19
;
04efbaa5   Ilyes Choubani   Correcting set_da...
20
21
; OPTIONAL INPUT PARAMETERS:
;    None
edeb9236   Annie Hughes   updated doc help
22
;
04efbaa5   Ilyes Choubani   Correcting set_da...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
; OUTPUTS:
;    ## adapting the data structures to the format used by the fitting algorithm 
;    sed : emission formatted structure 
;    ext : extinction formatted structure
;    polext : polarized extinction formatted structure
;    polsed : polarized emission formatted structure 
;    polfrac : fractional polarized emission formatted structure
;    psi_em  : emission polarization angle formatted structure 
;    qsed : stokesQ emission formatted structure
;    used : stokesU emission formatted structure
;    qext : stokesQ extinction formatted structure
;    uext : stokesU extinction formatted structure
;    psi_ext : extinction polarization angle formatted structure
;    fpolext : fractional extinction polarization formatted structure
edeb9236   Annie Hughes   updated doc help
37
;
04efbaa5   Ilyes Choubani   Correcting set_da...
38
39
; OPTIONAL OUTPUT PARAMETERS:
;    None
edeb9236   Annie Hughes   updated doc help
40
;
04efbaa5   Ilyes Choubani   Correcting set_da...
41
42
; ACCEPTED KEY-WORDS:
;    help      = If set print this help
edeb9236   Annie Hughes   updated doc help
43
;
04efbaa5   Ilyes Choubani   Correcting set_da...
44
45
; COMMON BLOCKS:
;    None
edeb9236   Annie Hughes   updated doc help
46
;
04efbaa5   Ilyes Choubani   Correcting set_da...
47
; SIDE EFFECTS:
edeb9236   Annie Hughes   updated doc help
48
;
04efbaa5   Ilyes Choubani   Correcting set_da...
49
; RESTRICTIONS:
edeb9236   Annie Hughes   updated doc help
50
51
;    The DustEMWrap IDL code must be installed
;
04efbaa5   Ilyes Choubani   Correcting set_da...
52
53
; PROCEDURE:
;    None
edeb9236   Annie Hughes   updated doc help
54
;
04efbaa5   Ilyes Choubani   Correcting set_da...
55
56
; EXAMPLES
;    dustem_check_data,data_sed,data_ext,sed,ext,polext,polsed,polfrac,psi_em,qsed,used,qext,uext,psi_ext,fpolext,[/help]
edeb9236   Annie Hughes   updated doc help
57
58
59
60
61
;
; MODIFICATION HISTORY:
;    Written I Choubani 2022
;    Evolution details on the DustEMWrap gitlab.
;    See http://dustemwrap.irap.omp.eu/ for FAQ and help.   
04efbaa5   Ilyes Choubani   Correcting set_da...
62
63
64
65
66
67
68
69
;-


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

006bb639   Jean-Philippe Bernard   improved erro mes...
70

09e61d64   Ilyes Choubani   cleaned set_data ...
71
;CHECKING THE FORMAT OF THE SED STRUCTURE
3ff9cac3   Ilyes Choubani   general update
72
73
IF KEYWORD_SET(data_sed) THEN BEGIN
            
09e61d64   Ilyes Choubani   cleaned set_data ...
74
    ans=''
3ff9cac3   Ilyes Choubani   general update
75
    wavs=data_sed.wave
09e61d64   Ilyes Choubani   cleaned set_data ...
76
    ;=== Impose central wavelengths for photometric channels
3ff9cac3   Ilyes Choubani   general update
77
    ind=where(data_sed.filter NE 'SPECTRUM',count)
09e61d64   Ilyes Choubani   cleaned set_data ...
78
    IF count NE 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
79
        wavs(ind)=dustem_filter2wav(data_sed(ind).filter)
09e61d64   Ilyes Choubani   cleaned set_data ...
80
    ENDIF
3ff9cac3   Ilyes Choubani   general update
81
    ;=== Initializing observation structure
3ff9cac3   Ilyes Choubani   general update
82
    obs_EM={instru_names:data_sed.instru,filt_names:data_sed.filter,wav:wavs, values:data_sed.StokesI,sigma:sqrt(abs(data_sed.sigmaII))} 
09e61d64   Ilyes Choubani   cleaned set_data ...
83
    ;INITIALIZING OUTPUT STRUCTURES
3ff9cac3   Ilyes Choubani   general update
84
    sed = obs_EM
3ff9cac3   Ilyes Choubani   general update
85
86
    qsed = sed
    used = sed
3ff9cac3   Ilyes Choubani   general update
87
    
09e61d64   Ilyes Choubani   cleaned set_data ...
88
    ;Test if there is StokesI data. If not move to polsed testing.
3ff9cac3   Ilyes Choubani   general update
89
    ind = where(data_sed.StokesI NE la_undef() and finite(data_sed.StokesI) EQ 1 and data_sed.sigmaII NE la_undef() and finite(data_sed.sigmaII) EQ 1,N_sed) ;Testing if the data and errors are set
09e61d64   Ilyes Choubani   cleaned set_data ...
90
    IF N_sed EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
91
        sed = ptr_new()
abebbdf3   Ilyes Choubani   General update: A...
92
        goto, qzone
09e61d64   Ilyes Choubani   cleaned set_data ...
93
    ENDIF ELSE BEGIN 
006bb639   Jean-Philippe Bernard   improved erro mes...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        ind = where(finite(data_sed.StokesI) EQ 0 or finite(data_sed.sigmaII) EQ 0, countnoki) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
        IF countnoki NE 0 THEN BEGIN
            message, "SED format isn't met. NaN value(s) detected in StokesI data.",/continue
            message, 'Without automatic format fitering you cannot proceed.',/continue
            ;stop
            read, ans, prompt='Would you like automatic format filtering? (Y/N):'
            IF strupcase(ans) EQ 'N' THEN BEGIN
                message,'You have chosen not to use automatic filtering. Please adjust the SED format.',/continue
                message, 'Aborting ...'
            ENDIF
        ENDIF 
        IF strupcase(ans) EQ 'Y' THEN message, 'You have chosen automatic format filtering. Removal of undefined data points...',/continue $
        ELSE message, 'Data format is met. Removal of undefined data points...',/continue
        tdaerri=where((data_sed.sigmaII EQ la_undef() and data_sed.StokesI NE la_undef()) or (data_sed.sigmaII NE la_undef() and data_sed.StokesI EQ la_undef()),ctdaerri)
        IF ctdaerri NE 0 THEN BEGIN
            message, 'Data and errors do not match. Undefinded data detected. Aborting...',/continue
09e61d64   Ilyes Choubani   cleaned set_data ...
110
        ENDIF
006bb639   Jean-Philippe Bernard   improved erro mes...
111
112
113
114
115
116
117
118
119
        testi=where(data_sed.sigmaII NE 0. and finite(data_sed.sigmaII) EQ 1 and data_sed.sigmaII NE la_undef() and data_sed.StokesI NE la_undef() and finite(data_sed.StokesI) EQ 1,ctesti)                
        IF ctesti NE 0 THEN BEGIN 
            instru_names = sed.instru_names(testi)
            filt_names = sed.filt_names(testi)
            wav = sed.wav(testi)
            values = sed.values(testi)
            sigma = sed.sigma(testi)
            sed={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
        ENDIF    
09e61d64   Ilyes Choubani   cleaned set_data ...
120
    ENDELSE
09e61d64   Ilyes Choubani   cleaned set_data ...
121
122
    qzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
123
124
    qsed.values=data_sed.StokesQ
    qsed.sigma=sqrt(abs(data_sed.sigmaQQ))
09e61d64   Ilyes Choubani   cleaned set_data ...
125
    ;Testig if there is StokesQ data. If not move to used data
3ff9cac3   Ilyes Choubani   general update
126
    ind = where(data_sed.StokesQ NE la_undef() and finite(data_sed.StokesQ) EQ 1 and data_sed.sigmaQQ NE la_undef() and finite(data_sed.sigmaQQ) EQ 1,NQ)
09e61d64   Ilyes Choubani   cleaned set_data ...
127
    IF NQ EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
128
        qsed = ptr_new()
09e61d64   Ilyes Choubani   cleaned set_data ...
129
130
        goto, uzone
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
131
    ind = where(finite(data_sed.StokesQ) EQ 0 or finite(data_sed.sigmaQQ) EQ 0, countnokq) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
09e61d64   Ilyes Choubani   cleaned set_data ...
132
133
134
135
136
137
    IF countnokq NE 0 THEN BEGIN 
        message, "SED format isn't met. NaN value(s) detected in StokesQ data.",/continue
        message, 'Without automatic format fitering you cannot proceed.',/continue
        read, ans, prompt='Would you like automatic format filtering? (Y/N):' 
        IF strupcase(ans) EQ 'N' THEN BEGIN
            message,'You have chosen not to use automatic filtering. Please adjust the SED format.',/continue
266ae799   Ilyes Choubani   General update
138
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
139
140
141
142
        ENDIF
    ENDIF
    IF strupcase(ans) EQ 'Y' THEN message, 'You have chosen automatic format filtering. Removal of undefined data points...',/continue $
    ELSE message, 'Data format is met. Removal of undefined data points...',/continue
3ff9cac3   Ilyes Choubani   general update
143
    tdaerrq=where((data_sed.sigmaQQ EQ la_undef() and data_sed.StokesQ NE la_undef()) or (data_sed.sigmaQQ NE la_undef() and data_sed.StokesQ EQ la_undef()),ctdaerrq)
006bb639   Jean-Philippe Bernard   improved erro mes...
144
145
146
147
148
149
    IF ctdaerrq NE 0 THEN BEGIN
        message, 'Data and errors in Stokes Q do not match. Undefinded data detected for these filters:',/continue
        print,data_sed[tdaerrq].filter
        ;message, 'Data and errors do not match. Undefinded data detected. Aborting...',/continue
        stop
    ENDIF
3ff9cac3   Ilyes Choubani   general update
150
    testq=where(data_sed.sigmaQQ NE 0. and finite(data_sed.sigmaQQ) EQ 1 and data_sed.sigmaQQ NE la_undef() and data_sed.StokesQ NE la_undef() and finite(data_sed.StokesQ) EQ 1,ctestq)                
09e61d64   Ilyes Choubani   cleaned set_data ...
151
    IF ctestq NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
152
153
154
155
156
157
        instru_names = qsed.instru_names(testq)
        filt_names = qsed.filt_names(testq)
        wav = qsed.wav(testq)
        values = qsed.values(testq)
        sigma = qsed.sigma(testq)
        qsed={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
09e61d64   Ilyes Choubani   cleaned set_data ...
158
159
160
161
    ENDIF
    ENDELSE
    uzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
162
163
    used.values=data_sed.StokesU
    used.sigma=sqrt(abs(data_sed.sigmaUU))
09e61d64   Ilyes Choubani   cleaned set_data ...
164
    ;Testig if there is StokesU data. If not return empty value and end loop.
3ff9cac3   Ilyes Choubani   general update
165
    ind = where(data_sed.StokesU NE la_undef() and finite(data_sed.StokesU) EQ 1 and data_sed.sigmaUU NE la_undef() and finite(data_sed.sigmaUU) EQ 1,NU)
09e61d64   Ilyes Choubani   cleaned set_data ...
166
    IF NU EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
167
        used = ptr_new()
abebbdf3   Ilyes Choubani   General update: A...
168
        goto, psiemzone
09e61d64   Ilyes Choubani   cleaned set_data ...
169
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
170
    ind = where(finite(data_sed.StokesU) EQ 0 or finite(data_sed.sigmaUU) EQ 0, countnoku) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
09e61d64   Ilyes Choubani   cleaned set_data ...
171
172
173
174
175
176
    IF countnoku NE 0 THEN BEGIN 
        message, "SED format isn't met. NaN value(s) detected in StokesU data.",/continue
        message, 'Without automatic format fitering you cannot proceed.',/continue
        read, ans, prompt='Would you like automatic format filtering? (Y/N):' 
        IF strupcase(ans) EQ 'N' THEN BEGIN
            message,'You have chosen not to use automatic filtering. Please adjust the SED format.',/continue
266ae799   Ilyes Choubani   General update
177
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
178
179
180
181
        ENDIF
    ENDIF  
    IF strupcase(ans) EQ 'Y' THEN message, 'You have chosen automatic format filtering. Removal of undefined data points...',/continue $
    ELSE message, 'Data format is met. Removal of undefined data points...',/continue
3ff9cac3   Ilyes Choubani   general update
182
    tdaerru=where((data_sed.sigmaUU EQ la_undef() and data_sed.StokesU NE la_undef()) or (data_sed.sigmaUU NE la_undef() and data_sed.StokesU EQ la_undef()),ctdaerru)
006bb639   Jean-Philippe Bernard   improved erro mes...
183
184
185
186
187
    IF ctdaerru NE 0 THEN BEGIN
        message, 'Data and errors in Stokes U do not match. Undefinded data detected for these filters:',/continue
        print,data_sed[tdaerru].filter
        stop
    ENDIF
3ff9cac3   Ilyes Choubani   general update
188
    testu=where(data_sed.sigmaUU NE 0. and finite(data_sed.sigmaUU) EQ 1 and data_sed.sigmaUU NE la_undef() and data_sed.StokesU NE la_undef() and finite(data_sed.StokesU) EQ 1,ctestu)                
09e61d64   Ilyes Choubani   cleaned set_data ...
189
    IF ctestu NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
190
191
192
193
194
195
        instru_names = used.instru_names(testu)
        filt_names = used.filt_names(testu)
        wav = used.wav(testu)
        values = used.values(testu)
        sigma = used.sigma(testu)
        used={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
09e61d64   Ilyes Choubani   cleaned set_data ...
196
197
    ENDIF
    ENDELSE
abebbdf3   Ilyes Choubani   General update: A...
198
199
    psiemzone:
    
303de20d   Ilyes Choubani   updating check_data
200
    if nu eq 0 and nq eq 0 then goto, end_sed
abebbdf3   Ilyes Choubani   General update: A...
201
202
    degtorad = !pi/180
    
303de20d   Ilyes Choubani   updating check_data
203
    ;Generation of POLFRAC,POLSED and PSI data by default   
abebbdf3   Ilyes Choubani   General update: A...
204
    ;Testing on the presence of covariances otherwise an error is returned
303de20d   Ilyes Choubani   updating check_data
205
    ;The testing happens here because covariances are needed for the error estimation of P,p and PSI.   
abebbdf3   Ilyes Choubani   General update: A...
206
207
     
    tstcovu = where((data_sed(testu).sigmaIU) EQ la_undef() or (data_sed(testu).sigmaQU) EQ la_undef(), ctcovu)
006bb639   Jean-Philippe Bernard   improved erro mes...
208
209
210
211
212
213
    if ctcovu NE 0  then BEGIN
        ;message, 'Covariance data is missing in StokesU data. Data, errors and covariances do not match. Aborting...',/continue
        message, 'IU or QU Covariance data is missing in StokesU data. Data, errors and covariances do not match for the following filters.',/continue
        print,data_sed[testu[tstcovu]].filter
        stop
    ENDIF
abebbdf3   Ilyes Choubani   General update: A...
214
    tstcovq = where((data_sed(testq).sigmaIQ) EQ la_undef() or (data_sed(testq).sigmaQU) EQ la_undef(), ctcovq)
006bb639   Jean-Philippe Bernard   improved erro mes...
215
216
217
218
219
    if ctcovq NE 0  then BEGIN
        message, 'IQ or QU Covariance data is missing in StokesQ data. Data, errors and covariances do not match for the following filters.',/continue
        print,data_sed[testq[tstcovq]].filter
        stop
    ENDIF
abebbdf3   Ilyes Choubani   General update: A...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    ;Matching both filter and spectrum points 
    
    match2,used.wav,qsed.wav,qpts,upts
    
    ;removing -1 values from the indices (qpts & upts)
    indnotq = where(qpts eq -1, cnotq)
    if cnotq ne 0 then remove, indnotq, qpts
    indnotu = where(upts eq -1, cnotu)
    if cnotu ne 0 then remove, indnotu, upts
    
    match2, sed.wav, qsed.wav(qpts), qtmp, sedpts
    indnoti = where(sedpts eq -1, cnoti)
    if cnoti ne 0 then remove, indnoti, sedpts
    
    instru_names = used.instru_names(upts)
    filt_names = used.filt_names(upts)
    wav = used.wav(upts)
    values = 0.5*atan(used.values(upts),qsed.values(qpts))/degtorad
    polar_variance_iqu2ippsi,sed.values(sedpts),qsed.values(qpts),used.values(upts),(data_sed(testi).sigmaII)(sedpts),(data_sed(testq).sigmaQQ)(qpts),(data_sed(testu).sigmaUU)(upts),(data_sed(testi).sigmaIQ)(sedpts),(data_sed(testi).sigmaIU)(sedpts),(data_sed(testi).sigmaQU)(sedpts),variance_smallp,variance_psi,variance_largep
    sigma = sqrt(variance_psi)
    psi_em={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
    
abebbdf3   Ilyes Choubani   General update: A...
242
243
244
    polsed = psi_em
    polsed.values = sqrt(used.values(upts)^2+qsed.values(qpts)^2) 
    polsed.sigma = sqrt(variance_largep)
abebbdf3   Ilyes Choubani   General update: A...
245
246
247
248
249
250
    
    polfrac = psi_em
    polfrac.values = polsed.values/sed.values(sedpts) 
    polfrac.sigma = sqrt(variance_smallp)
    
    goto, end_sed
09e61d64   Ilyes Choubani   cleaned set_data ...
251
    end_sed:
303de20d   Ilyes Choubani   updating check_data
252
    
09e61d64   Ilyes Choubani   cleaned set_data ...
253
254
255
ENDIF


abebbdf3   Ilyes Choubani   General update: A...
256
257
258
;EXTINCTION HASN'T BEEN TREATED YET 


67bd858a   Ilyes Choubani   Replication of th...
259
260
;SMALLL ISSUE .. POLEXTFRAC is not present. Let's create it for completness

3ff9cac3   Ilyes Choubani   general update
261
262
IF KEYWORD_SET(data_ext) THEN begin
    
09e61d64   Ilyes Choubani   cleaned set_data ...
263
    ans=''
3ff9cac3   Ilyes Choubani   general update
264
    wavs=data_ext.wave
09e61d64   Ilyes Choubani   cleaned set_data ...
265
    ;=== Impose central wavelengths for photometric channels
3ff9cac3   Ilyes Choubani   general update
266
    ind=where(data_ext.filter NE 'SPECTRUM',count)
09e61d64   Ilyes Choubani   cleaned set_data ...
267
    IF count NE 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
268
        wavs(ind)=dustem_filter2wav(data_ext(ind).filter)
09e61d64   Ilyes Choubani   cleaned set_data ...
269
270
    ENDIF
    ;=== define observations structure 
3ff9cac3   Ilyes Choubani   general update
271
    obs_EXT={instru_names:data_ext.instru,filt_names:data_ext.filter,wav:wavs, values:data_ext.EXT_I,sigma:sqrt(abs(data_ext.sigextII))} 
09e61d64   Ilyes Choubani   cleaned set_data ...
272
    ;INITIALIZING OUTPUT STRUCTURES
3ff9cac3   Ilyes Choubani   general update
273
    ext = obs_EXT
3ff9cac3   Ilyes Choubani   general update
274
275
    qext = ext
    uext = ext
3ff9cac3   Ilyes Choubani   general update
276
277
    
    ind = where(data_ext.EXT_I NE la_undef() and finite(data_ext.EXT_I) EQ 1 and data_ext.sigextII NE la_undef() and finite(data_ext.sigextII) EQ 1,N_ext) ;Testing if the data and errors are set
09e61d64   Ilyes Choubani   cleaned set_data ...
278
    IF N_ext EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
279
        ext = ptr_new()
303de20d   Ilyes Choubani   updating check_data
280
        goto, qexzone
09e61d64   Ilyes Choubani   cleaned set_data ...
281
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
282
    ind = where(finite(data_ext.EXT_I) EQ 0 or finite(data_ext.sigextII) EQ 0, countnokex) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
09e61d64   Ilyes Choubani   cleaned set_data ...
283
284
285
286
287
288
289
    IF countnokex NE 0 THEN BEGIN 
        message, "SED format isn't met. NaN value(s) detected in EXT_I data.",/continue
        message, 'Without automatic format fitering you cannot proceed.',/continue
        ans=''
        read, ans, prompt='Would you like automatic format filtering? (Y/N):' 
        IF strupcase(ans) EQ 'N' THEN BEGIN
            message,'You have chosen not to use automatic filtering. Please adjust the SED format.',/continue
266ae799   Ilyes Choubani   General update
290
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
291
292
293
294
        ENDIF
    ENDIF      
    IF strupcase(ans) EQ 'Y' THEN message, 'You have chosen automatic format filtering. Removal of undefined data points...',/continue $ 
    ELSE message, 'Data format is met. Removal of undefined data points...',/continue
3ff9cac3   Ilyes Choubani   general update
295
296
297
    tdaerrex=where((data_ext.sigextII EQ la_undef() and data_ext.EXT_I NE la_undef()) or (data_ext.sigextII NE la_undef() and data_ext.EXT_I EQ la_undef()),ctdaerrex)
    IF ctdaerrex NE 0 THEN message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    testex=where(data_ext.sigextII NE 0. and finite(data_ext.sigextII) EQ 1 and data_ext.sigextII NE la_undef() and data_ext.EXT_I NE la_undef() and finite(data_ext.EXT_I) EQ 1,ctestex)                
09e61d64   Ilyes Choubani   cleaned set_data ...
298
    IF ctestex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
299
300
301
302
303
304
        instru_names = ext.instru_names(testex)
        filt_names = ext.filt_names(testex)
        wav = ext.wav(testex)
        values = ext.values(testex)
        sigma = ext.sigma(testex)
        ext={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
09e61d64   Ilyes Choubani   cleaned set_data ...
305
306
    ENDIF 
    ENDELSE
303de20d   Ilyes Choubani   updating check_data
307
    
09e61d64   Ilyes Choubani   cleaned set_data ...
308
309
    qexzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
310
311
    qext.values=data_ext.EXT_Q
    qext.sigma=sqrt(abs(data_ext.sigextQQ))
09e61d64   Ilyes Choubani   cleaned set_data ...
312
    ;Testig if there is EXT_P data. If not move to EXT_U testing
3ff9cac3   Ilyes Choubani   general update
313
    ind = where(data_ext.EXT_Q NE la_undef() and finite(data_ext.EXT_Q) EQ 1 and data_ext.sigextQQ NE la_undef() and finite(data_ext.sigextQQ) EQ 1,NQ_ext) ;Testing if the data and errors are set
9ee42e40   Ilyes Choubani   fixed small bugs
314
    ;dustem_set_data.prostop   
09e61d64   Ilyes Choubani   cleaned set_data ...
315
    IF NQ_ext EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
316
        qext = ptr_new()
09e61d64   Ilyes Choubani   cleaned set_data ...
317
318
        goto, uexzone
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
319
    ind = where(finite(data_ext.EXT_Q) EQ 0 or finite(data_ext.sigextQQ) EQ 0, countnokqex) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
09e61d64   Ilyes Choubani   cleaned set_data ...
320
    IF countnokqex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
321
        message, "SED format isn't met. NaN value(s) detected in EXT_Q data.",/continue
09e61d64   Ilyes Choubani   cleaned set_data ...
322
323
324
325
326
        message, 'Without automatic format fitering you cannot proceed.',/continue
        ans=''
        read, ans, prompt='Would you like automatic format filtering? (Y/N):' 
        IF strupcase(ans) EQ 'N' THEN BEGIN
            message,'You have chosen not to use automatic filtering. Please adjust the SED format.',/continue
266ae799   Ilyes Choubani   General update
327
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
328
329
330
331
        ENDIF
    ENDIF
    IF strupcase(ans) EQ 'Y' THEN message, 'You have chosen automatic format filtering. Removal of undefined data points...',/continue $
    ELSE message, 'Data format is met. Removal of undefined data points...',/continue
3ff9cac3   Ilyes Choubani   general update
332
333
334
    tdaerrqex=where((data_ext.sigextQQ EQ la_undef() and data_ext.EXT_Q NE la_undef()) or (data_ext.sigextQQ NE la_undef() and data_ext.EXT_Q EQ la_undef()),ctdaerrqex)
    IF ctdaerrqex NE 0 THEN message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    testqex=where(data_ext.sigextQQ NE 0. and finite(data_ext.sigextQQ) EQ 1 and data_ext.sigextQQ NE la_undef() and data_ext.EXT_Q NE la_undef() and finite(data_ext.EXT_Q) EQ 1,ctestqex)                
09e61d64   Ilyes Choubani   cleaned set_data ...
335
    IF ctestqex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
336
337
338
339
340
341
        instru_names = qext.instru_names(testqex)
        filt_names = qext.filt_names(testqex)
        wav = qext.wav(testqex)
        values = qext.values(testqex)
        sigma = qext.sigma(testqex)
        qext={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
09e61d64   Ilyes Choubani   cleaned set_data ...
342
343
    ENDIF 
    ENDELSE
303de20d   Ilyes Choubani   updating check_data
344
    
09e61d64   Ilyes Choubani   cleaned set_data ...
345
346
    uexzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
347
348
    uext.values=data_ext.EXT_U
    uext.sigma=sqrt(abs(data_ext.sigextUU))
09e61d64   Ilyes Choubani   cleaned set_data ...
349
    ;Testig if there is EXT_P data. If not return empty value and end loop.
3ff9cac3   Ilyes Choubani   general update
350
    ind = where(data_ext.EXT_U NE la_undef() and finite(data_ext.EXT_U) EQ 1 and data_ext.sigextUU NE la_undef() and finite(data_ext.sigextUU) EQ 1,NU_ext) ;Testing if the data and errors are set
09e61d64   Ilyes Choubani   cleaned set_data ...
351
    IF NU_ext EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
352
        uext = ptr_new()
303de20d   Ilyes Choubani   updating check_data
353
        goto, psiexzone
09e61d64   Ilyes Choubani   cleaned set_data ...
354
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
355
    ind = where(finite(data_ext.EXT_U) EQ 0 or finite(data_ext.sigextUU) EQ 0, countnokuex) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
09e61d64   Ilyes Choubani   cleaned set_data ...
356
    IF countnokuex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
357
        message, "SED format isn't met. NaN value(s) detected in EXT_U data.",/continue
09e61d64   Ilyes Choubani   cleaned set_data ...
358
359
360
361
362
        message, 'Without automatic format fitering you cannot proceed.',/continue
        ans=''
        read, ans, prompt='Would you like automatic format filtering? (Y/N):' 
        IF strupcase(ans) EQ 'N' THEN BEGIN
            message,'You have chosen not to use automatic filtering. Please adjust the SED format.',/continue
266ae799   Ilyes Choubani   General update
363
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
364
365
366
367
        ENDIF
    ENDIF
    IF strupcase(ans) EQ 'Y' THEN message, 'You have chosen automatic format filtering. Removal of undefined data points...',/continue $
    ELSE message, 'Data format is met. Removal of undefined data points...',/continue
3ff9cac3   Ilyes Choubani   general update
368
369
370
    tdaerruex=where((data_ext.sigextUU EQ la_undef() and data_ext.EXT_U NE la_undef()) or (data_ext.sigextUU NE la_undef() and data_ext.EXT_U EQ la_undef()),ctdaerruex)
    IF ctdaerruex NE 0 THEN message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    testuex=where(data_ext.sigextUU NE 0. and finite(data_ext.sigextUU) EQ 1 and data_ext.sigextUU NE la_undef() and data_ext.EXT_U NE la_undef() and finite(data_ext.EXT_U) EQ 1,ctestuex)                
09e61d64   Ilyes Choubani   cleaned set_data ...
371
    IF ctestuex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
372
373
374
375
376
377
        instru_names = uext.instru_names(testuex)
        filt_names = uext.filt_names(testuex)
        wav = uext.wav(testuex)
        values = uext.values(testuex)
        sigma = uext.sigma(testuex)
        uext={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
09e61d64   Ilyes Choubani   cleaned set_data ...
378
379
    ENDIF 
    ENDELSE
303de20d   Ilyes Choubani   updating check_data
380
381
382
    
    psiexzone: ;Starting the analogy here. 
    
04efbaa5   Ilyes Choubani   Correcting set_da...
383
    if NU_EXT eq 0 and NQ_EXT eq 0 then goto, end_ext
303de20d   Ilyes Choubani   updating check_data
384
385
386
387
388
389
    degtorad = !pi/180
    
    ;Generation of POLEXT and PSI_EXT data by default   
    ;Testing on the presence of covariances otherwise an error is returned
    ;The testing happens here because covariances are needed for the error estimation of EXT_P,PSI_EXT.   
    
0e955322   Ilyes Choubani   fixing small typo...
390
    tstcovuex = where((data_ext(testuex).sigextIU) EQ la_undef() or (data_ext(testuex).sigextQU) EQ la_undef(), ctcovuex)
303de20d   Ilyes Choubani   updating check_data
391
392
393
394
395
396
397
398
399
400
401
402
403
    if ctcovuex NE 0  then message, 'Covariance data is missing in the extinction data pertaining to the StokesU parameter. Data, errors and covariances do not match. Aborting...'
    tstcovqex = where((data_ext(testqex).sigextIQ) EQ la_undef() or (data_ext(testqex).sigextQU) EQ la_undef(), ctcovqex)
    if ctcovqex NE 0  then message, 'Covariance data is missing in the extinction pertaining to the StokesQ parameter. Data, errors and covariances do not match. Aborting...'
    
    ;Matching both filter and spectrum points 
    match2,uext.wav,qext.wav,qexpts,uexpts
    
    ;removing -1 values from the indices (qpts & upts)
    indnotqex = where(qexpts eq -1, cnotqex)
    if cnotqex ne 0 then remove, indnotqex, qexpts
    indnotuex = where(uexpts eq -1, cnotuex)
    if cnotuex ne 0 then remove, indnotuex, uexpts
    
0e955322   Ilyes Choubani   fixing small typo...
404
    match2, ext.wav, qext.wav(qexpts), qextmp, extpts
303de20d   Ilyes Choubani   updating check_data
405
406
407
408
409
410
411
    indnotx = where(extpts eq -1, cnotx)
    if cnotx ne 0 then remove, indnotx, extpts
    
    instru_names = uext.instru_names(uexpts)
    filt_names = uext.filt_names(uexpts)
    wav = uext.wav(uexpts)
    values = 0.5*atan(uext.values(uexpts),qext.values(qexpts))/degtorad
0e955322   Ilyes Choubani   fixing small typo...
412
    polar_variance_iqu2ippsi,ext.values(extpts),qext.values(qexpts),uext.values(uexpts),(data_ext(testex).sigextII)(extpts),(data_ext(testqex).sigextQQ)(qexpts),(data_ext(testuex).sigextUU)(uexpts),(data_ext(testex).sigextIQ)(extpts),(data_ext(testex).sigextIU)(extpts),(data_ext(testex).sigextQU)(extpts),variance_smallp,variance_psi,variance_largep
303de20d   Ilyes Choubani   updating check_data
413
    sigma = sqrt(variance_psi)
5a2643a4   Ilyes Choubani   cleaning/improvin...
414
    psi_ext={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
07372e26   Ilyes Choubani   Corrected huge pl...
415
416


303de20d   Ilyes Choubani   updating check_data
417
    polext = psi_ext
0e955322   Ilyes Choubani   fixing small typo...
418
    polext.values = sqrt(uext.values(uexpts)^2+qext.values(qexpts)^2) 
303de20d   Ilyes Choubani   updating check_data
419
    polext.sigma = sqrt(variance_largep)
67bd858a   Ilyes Choubani   Replication of th...
420
421
422
423
424
    
    fpolext = psi_ext
    fpolext.values = polext.values/ext.values(extpts) 
    fpolext.sigma = sqrt(variance_smallp)
    
303de20d   Ilyes Choubani   updating check_data
425
426
    goto, end_ext
    
67bd858a   Ilyes Choubani   Replication of th...
427
    
09e61d64   Ilyes Choubani   cleaned set_data ...
428
429
    end_ext:
ENDIF
04efbaa5   Ilyes Choubani   Correcting set_da...
430
431
432

the_end:

09e61d64   Ilyes Choubani   cleaned set_data ...
433
434

END