Blame view

src/idl/dustem_check_data.pro 20.9 KB
3ff9cac3   Ilyes Choubani   general update
1
FUNCTION dustem_check_data,data_sed=data_sed,data_ext=data_ext,sed=sed,ext=ext,polext=polext,polsed=polsed,polfrac=polfrac,psi_em=psi_em,qsed=qsed,used=used,qext=qext,uext=uext,psi_ext=psi_ext
09e61d64   Ilyes Choubani   cleaned set_data ...
2
3


18e4331f   Ilyes Choubani   general update (f...
4
;NOTA BENE: No testing is done on the covariances. These will be used when debiasing the large P.       
06fe3845   Ilyes Choubani   general update
5
6


09e61d64   Ilyes Choubani   cleaned set_data ...
7
;CHECKING THE FORMAT OF THE SED STRUCTURE
3ff9cac3   Ilyes Choubani   general update
8
9
IF KEYWORD_SET(data_sed) THEN BEGIN
            
09e61d64   Ilyes Choubani   cleaned set_data ...
10
    ans=''
3ff9cac3   Ilyes Choubani   general update
11
    wavs=data_sed.wave
09e61d64   Ilyes Choubani   cleaned set_data ...
12
    ;=== Impose central wavelengths for photometric channels
3ff9cac3   Ilyes Choubani   general update
13
    ind=where(data_sed.filter NE 'SPECTRUM',count)
09e61d64   Ilyes Choubani   cleaned set_data ...
14
    IF count NE 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
15
        wavs(ind)=dustem_filter2wav(data_sed(ind).filter)
09e61d64   Ilyes Choubani   cleaned set_data ...
16
    ENDIF
3ff9cac3   Ilyes Choubani   general update
17
18
19
    ;=== Initializing observation structure
    ;it doesn't matter that data.StokesI is used, further test will take care of what is necessary. 
    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 ...
20
    ;INITIALIZING OUTPUT STRUCTURES
3ff9cac3   Ilyes Choubani   general update
21
    sed = obs_EM
3ff9cac3   Ilyes Choubani   general update
22
23
    qsed = sed
    used = sed
3ff9cac3   Ilyes Choubani   general update
24
    
09e61d64   Ilyes Choubani   cleaned set_data ...
25
    ;Test if there is StokesI data. If not move to polsed testing.
3ff9cac3   Ilyes Choubani   general update
26
    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 ...
27
    IF N_sed EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
28
        sed = ptr_new()
abebbdf3   Ilyes Choubani   General update: A...
29
        goto, qzone
09e61d64   Ilyes Choubani   cleaned set_data ...
30
    ENDIF ELSE BEGIN 
3ff9cac3   Ilyes Choubani   general update
31
    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 
09e61d64   Ilyes Choubani   cleaned set_data ...
32
33
34
35
36
37
    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
        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
38
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
39
40
41
42
        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
43
44
45
    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 message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    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)                
09e61d64   Ilyes Choubani   cleaned set_data ...
46
    IF ctesti NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
47
48
49
50
51
52
        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}
09e61d64   Ilyes Choubani   cleaned set_data ...
53
54
    ENDIF    
    ENDELSE
09e61d64   Ilyes Choubani   cleaned set_data ...
55
56
    qzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
57
58
    qsed.values=data_sed.StokesQ
    qsed.sigma=sqrt(abs(data_sed.sigmaQQ))
09e61d64   Ilyes Choubani   cleaned set_data ...
59
    ;Testig if there is StokesQ data. If not move to used data
3ff9cac3   Ilyes Choubani   general update
60
    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 ...
61
    IF NQ EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
62
        qsed = ptr_new()
09e61d64   Ilyes Choubani   cleaned set_data ...
63
64
        goto, uzone
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
65
    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 ...
66
67
68
69
70
71
    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
72
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
73
74
75
76
        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
77
78
79
    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)
    IF ctdaerrq NE 0 THEN message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    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 ...
80
    IF ctestq NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
81
82
83
84
85
86
        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 ...
87
88
89
90
    ENDIF
    ENDELSE
    uzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
91
92
    used.values=data_sed.StokesU
    used.sigma=sqrt(abs(data_sed.sigmaUU))
09e61d64   Ilyes Choubani   cleaned set_data ...
93
    ;Testig if there is StokesU data. If not return empty value and end loop.
3ff9cac3   Ilyes Choubani   general update
94
    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 ...
95
    IF NU EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
96
        used = ptr_new()
abebbdf3   Ilyes Choubani   General update: A...
97
        goto, psiemzone
09e61d64   Ilyes Choubani   cleaned set_data ...
98
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
99
    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 ...
100
101
102
103
104
105
    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
106
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
107
108
109
110
        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
111
112
113
    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)
    IF ctdaerru NE 0 THEN message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    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 ...
114
    IF ctestu NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
115
116
117
118
119
120
        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 ...
121
122
    ENDIF
    ENDELSE
abebbdf3   Ilyes Choubani   General update: A...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    psiemzone:
    
    degtorad = !pi/180
    
    ;INSTEAD OF USING THE DATA THAT IS IN THE XCAT FILE WE WILL CODE IT ANEW.  
    
    ;Testing on the presence of covariances otherwise an error is returned
    ;The testing happens here because covariances are needed for P,p and PSI.   
     
    tstcovu = where((data_sed(testu).sigmaIU) EQ la_undef() or (data_sed(testu).sigmaQU) EQ la_undef(), ctcovu)
    if ctcovu NE 0 and ctestu EQ 0 then message, 'Covariance data is missing in StokesU data. Data, errors and covariances do not match. Aborting...'
    tstcovq = where((data_sed(testq).sigmaIQ) EQ la_undef() or (data_sed(testq).sigmaQU) EQ la_undef(), ctcovq)
    if ctcovq NE 0 and ctestq EQ 0 then message, 'Covariance data is missing in StokesQ data. Data, errors and covariances do not match. Aborting...'
    
    ;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}
    
    goto, pzone ; Not forgetting that the variances for polfrac and polsed were already computed. 
        
    pzone:
    
    polsed = psi_em
    polsed.values = sqrt(used.values(upts)^2+qsed.values(qpts)^2) 
    polsed.sigma = sqrt(variance_largep)
    goto, fpzone
    
    fpzone:
    
    polfrac = psi_em
    polfrac.values = polsed.values/sed.values(sedpts) 
    polfrac.sigma = sqrt(variance_smallp)
    
    goto, end_sed
09e61d64   Ilyes Choubani   cleaned set_data ...
175
176
177
178
    end_sed:
ENDIF


abebbdf3   Ilyes Choubani   General update: A...
179
180
181
182

;EXTINCTION HASN'T BEEN TREATED YET 


3ff9cac3   Ilyes Choubani   general update
183
184
IF KEYWORD_SET(data_ext) THEN begin
    
09e61d64   Ilyes Choubani   cleaned set_data ...
185
    ans=''
3ff9cac3   Ilyes Choubani   general update
186
    wavs=data_ext.wave
09e61d64   Ilyes Choubani   cleaned set_data ...
187
    ;=== Impose central wavelengths for photometric channels
3ff9cac3   Ilyes Choubani   general update
188
    ind=where(data_ext.filter NE 'SPECTRUM',count)
09e61d64   Ilyes Choubani   cleaned set_data ...
189
    IF count NE 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
190
        wavs(ind)=dustem_filter2wav(data_ext(ind).filter)
09e61d64   Ilyes Choubani   cleaned set_data ...
191
192
    ENDIF
    ;=== define observations structure 
3ff9cac3   Ilyes Choubani   general update
193
    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 ...
194
    ;INITIALIZING OUTPUT STRUCTURES
3ff9cac3   Ilyes Choubani   general update
195
196
197
198
199
200
201
    ext = obs_EXT
    polext = ext
    qext = ext
    uext = ext
    psi_ext = ext
    
    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 ...
202
    IF N_ext EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
203
        ext = ptr_new()
09e61d64   Ilyes Choubani   cleaned set_data ...
204
205
        goto, pexzone
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
206
    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 ...
207
208
209
210
211
212
213
    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
214
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
215
216
217
218
        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
219
220
221
    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 ...
222
    IF ctestex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
223
224
225
226
227
228
        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 ...
229
230
231
232
    ENDIF 
    ENDELSE
    pexzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
233
234
    polext.values=data_ext.EXT_P
    polext.sigma=sqrt(abs(data_ext.sigextP))
09e61d64   Ilyes Choubani   cleaned set_data ...
235
    ;Testig if there is EXT_P data. If not move to EXT_Q testing
3ff9cac3   Ilyes Choubani   general update
236
    ind = where(data_ext.EXT_P NE la_undef() and finite(data_ext.EXT_P) EQ 1 and data_ext.sigextP NE la_undef() and finite(data_ext.sigextP) EQ 1,NP_ext) ;Testing if the data and errors are set
09e61d64   Ilyes Choubani   cleaned set_data ...
237
    IF NP_ext EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
238
        polext = ptr_new()
09e61d64   Ilyes Choubani   cleaned set_data ...
239
240
        goto, qexzone
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
241
    ind = where(finite(data_ext.EXT_P) EQ 0 or finite(data_ext.sigextP) EQ 0, countnokpex) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
09e61d64   Ilyes Choubani   cleaned set_data ...
242
    IF countnokpex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
243
        message, "SED format isn't met. NaN value(s) detected in EXT_P data.",/continue
09e61d64   Ilyes Choubani   cleaned set_data ...
244
245
246
247
248
        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
249
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
250
251
252
253
        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
254
255
256
    tdaerrpex=where((data_ext.sigextP EQ la_undef() and data_ext.EXT_P NE la_undef()) or (data_ext.sigextP NE la_undef() and data_ext.EXT_P EQ la_undef()),ctdaerrpex)
    IF ctdaerrpex NE 0 THEN message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    testpex=where(data_ext.sigextP NE 0. and finite(data_ext.sigextP) EQ 1 and data_ext.sigextP NE la_undef() and data_ext.EXT_P NE la_undef() and finite(data_ext.EXT_P) EQ 1,ctestpex)                
09e61d64   Ilyes Choubani   cleaned set_data ...
257
    IF ctestpex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
        instru_names = polext.instru_names(testpex)
        filt_names = polext.filt_names(testpex)
        wav = polext.wav(testpex)
        values = polext.values(testpex)
        sigma = polext.sigma(testpex)
        polext={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
    ENDIF 
    ENDELSE
    psiexzone:
    ans=''
    psi_ext.values=data_ext.psi
    psi_ext.sigma=sqrt(abs(data_ext.sigextpsi))
    ;Testig if there is EXT_P data. If not move to EXT_Q testing
    ind = where(data_ext.psi NE la_undef() and finite(data_ext.psi) EQ 1 and data_ext.sigextpsi NE la_undef() and finite(data_ext.sigextpsi) EQ 1,NPSIEXT) ;Testing if the data and errors are set
    IF NPSIEXT EQ 0 THEN BEGIN
        psi_ext = ptr_new()
        goto, qexzone
    ENDIF ELSE BEGIN
    ind = where(finite(data_ext.psi) EQ 0 or finite(data_ext.sigextpsi) EQ 0, countnokpsiex) ;Format is not okay if there are nan values but the wrapper recognizes the la_undef() value 
    IF countnokpsiex NE 0 THEN BEGIN 
        message, "SED format isn't met. NaN value(s) detected in PSI_EXT 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
            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
    tdaerrpsiex=where((data_ext.sigextpsi EQ la_undef() and data_ext.psi NE la_undef()) or (data_ext.sigextpsi NE la_undef() and data_ext.psi EQ la_undef()),ctdaerrpsiex)
    IF ctdaerrpsiex NE 0 THEN message, 'Data and errors do not match. Undefinded data detected. Aborting...'
    testpsiex=where(data_ext.sigextpsi NE 0. and finite(data_ext.sigextpsi) EQ 1 and data_ext.sigextpsi NE la_undef() and data_ext.psi NE la_undef() and finite(data_ext.psi) EQ 1,ctestpsiex)                
    IF ctestpsiex NE 0 THEN BEGIN 
        instru_names = psi_ext.instru_names(testpsiex)
        filt_names = psi_ext.filt_names(testpsiex)
        wav = psi_ext.wav(testpsiex)
        values = psi_ext.values(testpsiex)
        sigma = psi_ext.sigma(testpsiex)
        psi_ext={instru_names:instru_names,filt_names:filt_names,wav:wav,values:values,sigma:sigma}
09e61d64   Ilyes Choubani   cleaned set_data ...
299
300
301
302
    ENDIF 
    ENDELSE
    qexzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
303
304
    qext.values=data_ext.EXT_Q
    qext.sigma=sqrt(abs(data_ext.sigextQQ))
09e61d64   Ilyes Choubani   cleaned set_data ...
305
    ;Testig if there is EXT_P data. If not move to EXT_U testing
3ff9cac3   Ilyes Choubani   general update
306
    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
307
    ;dustem_set_data.prostop   
09e61d64   Ilyes Choubani   cleaned set_data ...
308
    IF NQ_ext EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
309
        qext = ptr_new()
09e61d64   Ilyes Choubani   cleaned set_data ...
310
311
        goto, uexzone
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
312
    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 ...
313
    IF countnokqex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
314
        message, "SED format isn't met. NaN value(s) detected in EXT_Q data.",/continue
09e61d64   Ilyes Choubani   cleaned set_data ...
315
316
317
318
319
        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
320
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
321
322
323
324
        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
325
326
327
    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 ...
328
    IF ctestqex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
329
330
331
332
333
334
        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 ...
335
336
337
338
    ENDIF 
    ENDELSE
    uexzone:
    ans=''
3ff9cac3   Ilyes Choubani   general update
339
340
    uext.values=data_ext.EXT_U
    uext.sigma=sqrt(abs(data_ext.sigextUU))
09e61d64   Ilyes Choubani   cleaned set_data ...
341
    ;Testig if there is EXT_P data. If not return empty value and end loop.
3ff9cac3   Ilyes Choubani   general update
342
    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 ...
343
    IF NU_ext EQ 0 THEN BEGIN
3ff9cac3   Ilyes Choubani   general update
344
        uext = ptr_new()
09e61d64   Ilyes Choubani   cleaned set_data ...
345
346
        goto, end_ext
    ENDIF ELSE BEGIN
3ff9cac3   Ilyes Choubani   general update
347
    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 ...
348
    IF countnokuex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
349
        message, "SED format isn't met. NaN value(s) detected in EXT_U data.",/continue
09e61d64   Ilyes Choubani   cleaned set_data ...
350
351
352
353
354
        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
355
            message, 'Aborting ...'
09e61d64   Ilyes Choubani   cleaned set_data ...
356
357
358
359
        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
360
361
362
    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 ...
363
    IF ctestuex NE 0 THEN BEGIN 
3ff9cac3   Ilyes Choubani   general update
364
365
366
367
368
369
        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 ...
370
371
372
373
    ENDIF 
    ENDELSE
    end_ext:
ENDIF
9ee42e40   Ilyes Choubani   fixed small bugs
374
;stop
3ff9cac3   Ilyes Choubani   general update
375
376
RETURN, 0.

09e61d64   Ilyes Choubani   cleaned set_data ...
377
378

END