Blame view

src/idl/dustem_fit_sed_qsed_used_readme.pro 8.56 KB
607060e5   Ilyes Choubani   test version
1
PRO dustem_fit_sed_qsed_used_readme,postcript=postcript,mode=mode,help=help
b5ccb706   Jean-Philippe Bernard   improved to fit p...
2
3
4
5
6
7
8

;This Readme describes how to fit total intensity and polarization SEDs jointly
;Obviously, the dustem package must have been installed succesfully
;see DustemWrap documentation for install instructions.

;+
; NAME:
607060e5   Ilyes Choubani   test version
9
;    dustem_fit_sed_qsed_used_readme
b5ccb706   Jean-Philippe Bernard   improved to fit p...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
; PURPOSE:
;    This is an example of how to fit total intensity and polarization SEDs jointly
;    It is meant to be an example to follow when writing your own
;    programs using the dustem IDL wrapper.
; CATEGORY:
;    Dustem
; CALLING SEQUENCE:
;    dustem_fit_sed_polsed_readme,postcript=postcript,mode=mode,help=help
; INPUTS:
;    None
; OPTIONAL INPUT PARAMETERS:
;    None
; OUTPUTS:
;    None
; OPTIONAL OUTPUT PARAMETERS:
;    None
; ACCEPTED KEY-WORDS:
;    postcript = if set plot is done in DUSTEM/Docs/Figures/Last_dustem_fit.ps
;    help      = If set print this help
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    None
; RESTRICTIONS:
;    The dustem idl wrapper must be installed
; PROCEDURE:
;    None
; EXAMPLES
;    dustem_fit_sed_polsed_readme
; MODIFICATION HISTORY:
;    Written by J.P. Bernard June 29th 2011
;    see evolution details on the dustem cvs maintained at CESR
;    Contact J.-Ph. Bernard (Jean-Philippe.Bernard@cesr.fr) in case of problems.
;-

b5ccb706   Jean-Philippe Bernard   improved to fit p...
45
IF keyword_set(help) THEN BEGIN
607060e5   Ilyes Choubani   test version
46
  doc_library,'dustem_fit_sed_qsed_used_readme'
b5ccb706   Jean-Philippe Bernard   improved to fit p...
47
48
49
  goto,the_end
ENDIF

607060e5   Ilyes Choubani   test version
50

cf92aab3   Jean-Philippe Bernard   improved
51
52
dustem_define_la_common

b5ccb706   Jean-Philippe Bernard   improved to fit p...
53
54
;=== initialise dustem
;dustem_init,mode='COMPIEGNE_ETAL2010',/pol
d06cb418   Ilyes Choubani   Added PILOT filter
55
use_mode='THEMIS';'G17_MODELA' ; try with model D 
4750086c   Ilyes Choubani   nouvelle philosph...
56

b5ccb706   Jean-Philippe Bernard   improved to fit p...
57
dustem_init,mode=use_mode,/pol
66aff855   Jean-Philippe Bernard   modified to impro...
58

b5ccb706   Jean-Philippe Bernard   improved to fit p...
59
60
!dustem_verbose=1
!dustem_show_plot=1
b5ccb706   Jean-Philippe Bernard   improved to fit p...
61
62
63
64
help,!run_pol

;stop

607060e5   Ilyes Choubani   test version
65
66
67
!dustem_verbose=1
!dustem_show_plot=1

4fd64cbb   Ilyes Choubani   dustem_fit_sed_po...
68

b5ccb706   Jean-Philippe Bernard   improved to fit p...
69
;filters=['DIRBE1','DIRBE2','DIRBE3','DIRBE4','IRAS1','IRAS2','IRAS3','IRAS4','PACS3','SPIRE1','SPIRE2','SPIRE3','HFI2','HFI3','HFI4','HFI5','HFI6','LFI1','LFI2','LFI3']
d06cb418   Ilyes Choubani   Added PILOT filter
70
filters=['IRAS1','IRAS2','IRAS3','IRAS4','PACS3','PILOT1','SPIRE1','SPIRE2','SPIRE3','HFI2','HFI3','HFI4','HFI5','HFI6','LFI1','LFI2','LFI3']
b5ccb706   Jean-Philippe Bernard   improved to fit p...
71
72
73
74
75
Nfilt=n_elements(filters)
sed=dustem_initialize_sed(Nfilt)
sed.filter=filters
sed.wave=dustem_filter2wav(filters)
sed.instru=dustem_filter2instru(filters)
b5ccb706   Jean-Philippe Bernard   improved to fit p...
76

607060e5   Ilyes Choubani   test version
77
78
79


;INITIALIZING IQU SEDS and associated errors
4fd64cbb   Ilyes Choubani   dustem_fit_sed_po...
80
sed.StokesI=sed.wave*0
d06cb418   Ilyes Choubani   Added PILOT filter
81
82
sed.StokesQ=sed.wave*0
sed.StokesU=sed.wave*0
4fd64cbb   Ilyes Choubani   dustem_fit_sed_po...
83
sed.sigmaII=sed.wave*0
d06cb418   Ilyes Choubani   Added PILOT filter
84
85
sed.sigmaQQ=sed.wave*0
sed.sigmaUU=sed.wave*0
ba82c5cf   Ilyes Choubani   improved with mod...
86
87


4fd64cbb   Ilyes Choubani   dustem_fit_sed_po...
88
89
90
91

;INITIALIZING THE !dustem_data tags that will be internally used 
st=dustem_set_data(sed=sed,rchi2_weight=rchi2_weight,f_HI=f_HI)

4fd64cbb   Ilyes Choubani   dustem_fit_sed_po...
92

4750086c   Ilyes Choubani   nouvelle philosph...
93

b5ccb706   Jean-Philippe Bernard   improved to fit p...
94
;=== Set which parameters you want to fit
b5ccb706   Jean-Philippe Bernard   improved to fit p...
95
pd = [ $
7f2b2149   Jean-Philippe Bernard   a Ilyes
96
             '(*!dustem_params).G0', $      ;G0
607060e5   Ilyes Choubani   test version
97
98
99
             '(*!dustem_params).grains(0).mdust_o_mh',$  
             '(*!dustem_params).grains(1).mdust_o_mh',$     
             ;'(*!dustem_params).grains(1).mdust_o_mh',$    
d06cb418   Ilyes Choubani   Added PILOT filter
100
             '(*!dustem_params).grains(2).mdust_o_mh',$    
ba82c5cf   Ilyes Choubani   improved with mod...
101
102
             ;'dustem_plugin_synchrotron_2', $               ;Synchrotron amplitude
             ;'dustem_plugin_synchrotron_4', $               ;Synchrotron polarization angle
d06cb418   Ilyes Choubani   Added PILOT filter
103
104
105
             'dustem_plugin_modify_dust_polarization_2',$ ;Polarization angle applied to the Fortran dust model via a core plugin
             'dustem_plugin_modify_spinning_polarization_1', $
             'dustem_plugin_modify_spinning_polarization_2' $
7f2b2149   Jean-Philippe Bernard   a Ilyes
106
107
              ]                   

d06cb418   Ilyes Choubani   Added PILOT filter
108
109
110
111
112
113
114
115
;p_truth=[1.,7.8000E-04,7.8000E-04,1.6500E-04,5e-3,20.,68.,0.03,30.]    
;p_truth=[1.,7.8000E-04,7.8000E-04,1.6500E-04,68.,0.03,30.]
;p_truth=[1.,7.8000E-04,7.8000E-04,1.6500E-04];,0.01,30.]



p_truth=[1.,0.17E-03,0.63E-03,0.51E-03,68.,0.03,30.]

607060e5   Ilyes Choubani   test version
116

607060e5   Ilyes Choubani   test version
117
118

;p_truth=[68.]
4750086c   Ilyes Choubani   nouvelle philosph...
119

452c334e   Ilyes Choubani   Implementation Of...
120

607060e5   Ilyes Choubani   test version
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
; fpd = [ $
;              '(*!dustem_params).G0', $      ;G0
;              '(*!dustem_params).grains(0).mdust_o_mh',$  
;              '(*!dustem_params).grains(1).mdust_o_mh',$     
;              '(*!dustem_params).grains(2).mdust_o_mh'$    
; ;              '(*!dustem_params).grains(3).mdust_o_mh',$    
; ;              'dustem_plugin_modify_dust_polarization_2', $ ;Polarization angle applied to the Fortran dust model via a core plugin
; ;              'dustem_plugin_modify_spinning_polarization_1' $
;                ]                   
; ; 
; fiv=[1.,7.8000E-04,7.8000E-04,1.6500E-04];,68.,0.01]


fixed=[1,1,1,1,0];0,1,1]
 
;dustem_init_parinfo,pd,p_truth;,fixed=fixed

;dustem_init_fixed_params,fpd,fiv

d06cb418   Ilyes Choubani   Added PILOT filter
140
141
iv=p_truth+[0.,5.000E-04,5.000E-04,5.000E-03,-10.,1.00e-2,10.]      ;shifted from solution ,10.

607060e5   Ilyes Choubani   test version
142

d06cb418   Ilyes Choubani   Added PILOT filter
143
;iv=p_truth+[0.,5.000E-04,5.000E-04,5.000E-04];,5.e-4,1.]      ;shifted from solution ,10.
607060e5   Ilyes Choubani   test version
144

4750086c   Ilyes Choubani   nouvelle philosph...
145
146

Npar=n_elements(pd)  
b5ccb706   Jean-Philippe Bernard   improved to fit p...
147

607060e5   Ilyes Choubani   test version
148
ulimed=replicate(0,Npar);[0]
7d2d66e7   Ilyes Choubani   replacing polsed,...
149

607060e5   Ilyes Choubani   test version
150
151
;ulimed=[0,0,0,0,0,1,1,1]
;ulims=[0.,0.,0.,0.,0.,100.,1.,100.]
b5ccb706   Jean-Philippe Bernard   improved to fit p...
152

607060e5   Ilyes Choubani   test version
153
154
llimed=replicate(1,Npar);[1]
llims=replicate(0,Npar);[10]
4750086c   Ilyes Choubani   nouvelle philosph...
155

607060e5   Ilyes Choubani   test version
156
dustem_init_parinfo,pd,iv,up_limited=ulimed,lo_limited=llimed,up_limits=ulims,lo_limits=llims;,fixed=fixed
b5ccb706   Jean-Philippe Bernard   improved to fit p...
157

607060e5   Ilyes Choubani   test version
158
;dustem_init_fixed_params,fpd,fiv
b5ccb706   Jean-Philippe Bernard   improved to fit p...
159

607060e5   Ilyes Choubani   test version
160
dustem_init_plugins,pd;, fpd=fpd
83b3ddee   Jean-Philippe Bernard   modified with Ily...
161

83b3ddee   Jean-Philippe Bernard   modified with Ily...
162

b5ccb706   Jean-Philippe Bernard   improved to fit p...
163

607060e5   Ilyes Choubani   test version
164
sed.StokesI=dustem_compute_sed(p_truth,ste)   ;sst is not set on purpose, for dustem_compute_sed to compute the sed using fortran code
d06cb418   Ilyes Choubani   Added PILOT filter
165
sed.sigmaII=double((sed.StokesI*0.3)^2)
607060e5   Ilyes Choubani   test version
166

d06cb418   Ilyes Choubani   Added PILOT filter
167
toto=dustem_compute_stokes(p_truth,stee,dustem_qsed,dustem_used) ;this procedure also allows for the extraction of the spectra
607060e5   Ilyes Choubani   test version
168

d06cb418   Ilyes Choubani   Added PILOT filter
169
170
171
172
sed.StokesQ=dustem_qsed
sed.sigmaQQ=(double((sed.StokesQ*0.03)^2)) > 1d-40
;tes=where(finite(sed.sigmaQQ) eq 0,coun)
;if coun ne 0 then sed.sigmaQQ(tes)=0.
607060e5   Ilyes Choubani   test version
173

d06cb418   Ilyes Choubani   Added PILOT filter
174
175
176
177
sed.StokesU=dustem_used
sed.sigmaUU=(double((sed.StokesU*0.03)^2)) > 1d-40
;tes=where(finite(sed.sigmaUU) eq 0,coun)
;if coun ne 0 then sed.sigmaUU(tes)=0.
607060e5   Ilyes Choubani   test version
178
179
180
181
182


;======================================================================================================================


4750086c   Ilyes Choubani   nouvelle philosph...
183

607060e5   Ilyes Choubani   test version
184
185
; llimed=[1];replicate(1,Npar)
; llims=[1e-12];replicate(0,Npar)
f882e687   Ilyes Choubani   improved
186

607060e5   Ilyes Choubani   test version
187
188
189
190
191
192
;== SET THE FITTED PARAMETERS (now done in dustem_init_parinfo)

;dustem_init_parinfo,pd,iv,up_limited=ulimed,lo_limited=llimed,up_limits=ulims,lo_limits=llims;,fixed=fixed


;dustem_init_plugins,pd,fpd=fpd
b5ccb706   Jean-Philippe Bernard   improved to fit p...
193

452c334e   Ilyes Choubani   Implementation Of...
194

452c334e   Ilyes Choubani   Implementation Of...
195

607060e5   Ilyes Choubani   test version
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
;(*!dustem_params).grains[0].TYPE_KEYWORDS='logn-chrg-spin'
;(*!dustem_params).grains[1].TYPE_KEYWORDS='logn-chrg-spin'


; 
; sed.StokesI=dustem_compute_sed(p_truth,ssti)   ;sst is not set on purpose, for dustem_compute_sed to compute the sed using fortran code
; sed.sigmaII=(sed.StokesI*0.3)^2
; 
; 
; toto=dustem_compute_stokes(p_truth,sstqu,dustem_qsed,dustem_used) ;this procedure also allows for the extraction of the spectra
; 
; sed.StokesQ=dustem_qsed
; sed.sigmaQQ=(double(sed.StokesQ)*0.4^2) > 1d-50
; 
; 
; sed.StokesU=dustem_used
; sed.sigmaUU=(double(sed.StokesU)*0.4^2) > 1d-50
452c334e   Ilyes Choubani   Implementation Of...
213

452c334e   Ilyes Choubani   Implementation Of...
214

b5ccb706   Jean-Philippe Bernard   improved to fit p...
215
216
;=== SET THE OBSERVATION STRUCTURE

b5ccb706   Jean-Philippe Bernard   improved to fit p...
217
218
ind=where(sed.wave GE 0.1)

4fd64cbb   Ilyes Choubani   dustem_fit_sed_po...
219
220
st_bidon=dustem_set_data(sed=sed[ind],rchi2_weight=rchi2_weight,f_HI=f_HI) ; comment if second line is to be used

b5ccb706   Jean-Philippe Bernard   improved to fit p...
221
;=== RUN fit
607060e5   Ilyes Choubani   test version
222
223
224
225
226
tol=1.e-40
xtol=1.e-40
Nitermax=23; just an initialization


4750086c   Ilyes Choubani   nouvelle philosph...
227
228
229
;for i=0L,n_tags(!dustem_data)-1 do begin
;if ptr_valid(!dustem_data.(i)) then Nitermax+=1
;endfor
452c334e   Ilyes Choubani   Implementation Of...
230
;Nitermax=4           ;maximum number of iteration. This is the criterion which will stop the fit procedure
b5ccb706   Jean-Philippe Bernard   improved to fit p...
231
232

xrange=[1.,2.e4]
607060e5   Ilyes Choubani   test version
233
yrange=[1e-7,1.e3]
b5ccb706   Jean-Philippe Bernard   improved to fit p...
234
235
236
237
238
239
240
241
242

loadct,13
;!y.range=[1e-8,100]              ;This is to ajust plot range from outside the routine
t1=systime(0,/sec)
res=dustem_mpfit_data(tol=tol,xtol=xtol,Nitermax=Nitermax,xrange=xrange,/xstyle,yrange=yrange,/ysty,/ylog,/xlog,xtit='wavelength [mic]',ytit='Brightness []')
t2=systime(0,/sec)

print,(res-p_truth)/p_truth*100.

66aff855   Jean-Philippe Bernard   modified to impro...
243
;help,(*!dustem_fit),/STRUCTURE
b5ccb706   Jean-Philippe Bernard   improved to fit p...
244
245

;=== SAVE FIT RESULTS
66aff855   Jean-Philippe Bernard   modified to impro...
246
file_out='/tmp/DUSTEM_polsed_fit_example.sav'
b5ccb706   Jean-Philippe Bernard   improved to fit p...
247
248
249
dustem_save_system_variables,file_out
message,'Saved '+file_out,/continue

83b3ddee   Jean-Philippe Bernard   modified with Ily...
250
;stop
b5ccb706   Jean-Philippe Bernard   improved to fit p...
251
252

;======================================
f882e687   Ilyes Choubani   improved
253
;====You can exit IDL here and re-enter   -   THIS PART APPEARS TO BE FAULTY. NEEDS INVESTIGATION 
b5ccb706   Jean-Philippe Bernard   improved to fit p...
254
;======================================
66aff855   Jean-Philippe Bernard   modified to impro...
255
file='/tmp/DUSTEM_polsed_fit_example.sav'
b5ccb706   Jean-Philippe Bernard   improved to fit p...
256
257
258
dustem_restore_system_variables,file

;=== Plot best fit
b5ccb706   Jean-Philippe Bernard   improved to fit p...
259
260
261
262
263
win=1
window,win & win=win+1
xrange=[1.,2.e4]
yrange=[1e-4,1.e3]

b5ccb706   Jean-Philippe Bernard   improved to fit p...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
;=== recover best fit values
res=*(*!dustem_fit).current_param_values
chi2=(*!dustem_fit).chi2
rchi2=(*!dustem_fit).rchi2
;errors=*(*!dustem_fit).current_param_errors
errors=(*(*!dustem_fit).current_param_errors)*(*(*!dustem_fit).param_init_values)

;=== Plot best fit
tit='DUSTEM polarization dust Example (Saved)'
ytit=textoidl('I_\nu (MJy/sr) for N_H=10^{20} H/cm^2')
xtit=textoidl('\lambda (\mum)')

loadct,13
IF keyword_set(postcript) THEN BEGIN
  set_plot,'PS'
f882e687   Ilyes Choubani   improved
279
  ps_file=!dustem_wrap_soft_dir+'/Docs/'+'Last_dustem_spinning_fit.ps';'/Docs/Figures/'+'Last_dustem_spinning_fit.ps'
b5ccb706   Jean-Philippe Bernard   improved to fit p...
280
281
  device,filename=ps_file,/color
ENDIF
b5ccb706   Jean-Philippe Bernard   improved to fit p...
282

66aff855   Jean-Philippe Bernard   modified to impro...
283
dustem_sed_plot,*(*!dustem_fit).current_param_values,ytit=ytit,xtit=xtit,title=tit,yr=yrange,xr=xrange,/ysty,/xsty,res=res,errors=errors,chi2=chi2,rchi2=rchi2,/xlog,/ylog,/pol
b5ccb706   Jean-Philippe Bernard   improved to fit p...
284

b5ccb706   Jean-Philippe Bernard   improved to fit p...
285
286
287
288
289
290
291
292
293
294
295
IF keyword_set(postcript) THEN BEGIN
  device,/close
  set_plot,'X'
  message,'wrote '+ps_file,/info
ENDIF

print,'dustem_mpfit_sed executed in ',t2-t1,' sec'

the_end:

END
452c334e   Ilyes Choubani   Implementation Of...