Blame view

src/idl/dustem_fit_sed_polsed_readme.pro 7.84 KB
b5ccb706   Jean-Philippe Bernard   improved to fit p...
1
2
3
4
5
6
7
8
9
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
PRO dustem_fit_sed_polsed_readme,postcript=postcript,mode=mode,help=help

;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:
;    dustem_fit_sed_polsed_readme
; 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
46
47
48
49
IF keyword_set(help) THEN BEGIN
  doc_library,'dustem_fit_sed_polsed_readme'
  goto,the_end
ENDIF

cf92aab3   Jean-Philippe Bernard   improved
50
51
dustem_define_la_common

b5ccb706   Jean-Philippe Bernard   improved to fit p...
52
53
54
;=== initialise dustem
;dustem_init,mode='COMPIEGNE_ETAL2010',/pol
use_mode='G17_MODELA'
4750086c   Ilyes Choubani   nouvelle philosph...
55

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

b5ccb706   Jean-Philippe Bernard   improved to fit p...
58
59
!dustem_verbose=1
!dustem_show_plot=1
b5ccb706   Jean-Philippe Bernard   improved to fit p...
60
61
62
63
64
65
66
67
68
69
70
71
help,!run_pol

;stop

;=== make a fake one out for given filters
;filters=['DIRBE1','DIRBE2','DIRBE3','DIRBE4','IRAS1','IRAS2','IRAS3','IRAS4','PACS3','SPIRE1','SPIRE2','SPIRE3','HFI2','HFI3','HFI4','HFI5','HFI6','LFI1','LFI2','LFI3']
filters=['IRAS1','IRAS2','IRAS3','IRAS4','PACS3','SPIRE1','SPIRE2','SPIRE3','HFI2','HFI3','HFI4','HFI5','HFI6','LFI1','LFI2','LFI3']
Nfilt=n_elements(filters)
sed=dustem_initialize_sed(Nfilt)
sed.filter=filters
sed.wave=dustem_filter2wav(filters)
sed.instru=dustem_filter2instru(filters)
f882e687   Ilyes Choubani   improved
72
polsed=sed ; comment if polext is to be used
452c334e   Ilyes Choubani   Implementation Of...
73

4750086c   Ilyes Choubani   nouvelle philosph...
74
;UNCOMMENT THIS BLOCK AFTER THE TESTING
4750086c   Ilyes Choubani   nouvelle philosph...
75
76
77
; ;stokes fake parameters - comment if stokes extinction parameters are to be used
; qsed=sed
; used=sed
452c334e   Ilyes Choubani   Implementation Of...
78

452c334e   Ilyes Choubani   Implementation Of...
79
st=dustem_set_data(ext=ext,polsed=polsed,polext=polext,sed=sed,polfrac=polfrac,rchi2_weight=rchi2_weight,f_HI=f_HI,qsed=qsed,used=used,qext=qext,uext=uext)
b5ccb706   Jean-Philippe Bernard   improved to fit p...
80

4750086c   Ilyes Choubani   nouvelle philosph...
81

b5ccb706   Jean-Philippe Bernard   improved to fit p...
82
;=== Set which parameters you want to fit
b5ccb706   Jean-Philippe Bernard   improved to fit p...
83
pd = [ $
7f2b2149   Jean-Philippe Bernard   a Ilyes
84
85
86
87
             '(*!dustem_params).G0', $      ;G0
             '(*!dustem_params).grains(0).mdust_o_mh',$  ;PAH0 mass fraction
             '(*!dustem_params).grains(1).mdust_o_mh',$     ;PAH1 mass fraction
             '(*!dustem_params).grains(2).mdust_o_mh',$    ;amCBEx
cbf75ef0   Ilyes Choubani   dustem_init_plugi...
88
             'dustem_plugin_synchrotron_2' $               ;Synchrotron amplitude
7f2b2149   Jean-Philippe Bernard   a Ilyes
89
90
91
92
93
94
95
;             'dustem_create_stokes_1',$ ;Polarization angle psi_ref_dust
;             'dustem_create_stokes_2',$;Polarization angle psi_ref_synch
;             'dustem_create_stokes_3'$;Polarization fraction of synchrotron spectrum
             ;'dustem_create_stext_1'$;Polarization angle in extinction PSI_ext
              ]                   

p_truth=[1.,7.8000E-04,7.8000E-04,1.6500E-04,5e-3]    
4750086c   Ilyes Choubani   nouvelle philosph...
96
; ;p_truth=[1.,7.8000E-04,7.8000E-04,1.6500E-04,25]    
452c334e   Ilyes Choubani   Implementation Of...
97
98


4750086c   Ilyes Choubani   nouvelle philosph...
99
100
101
102
103
104
105
106
107
;UNCOMMENT THIS BLOCK AFTER THE TESTING
;p_truth=[1.,7.8000E-04,7.8000E-04,2E-03,5e-3,70,20,0.2]

;TESTING THE ISRF PLUGIN

;p_truth=[1.,7.8000E-04,7.8000E-04,2E-03,2e-3,13.,3.,9.,2.]
;p_truth=[7.8000E-04,7.8000E-04,2E-03,2e-3,13.,3.];,9.,2.]
;p_truth=[7.8000E-04,7.8000E-04,2E-03,10.,8.]
;p_truth=[10.,8.]
452c334e   Ilyes Choubani   Implementation Of...
108

b5ccb706   Jean-Philippe Bernard   improved to fit p...
109
110

;=== set initial value of parameters you want to fit
4750086c   Ilyes Choubani   nouvelle philosph...
111
112
113
iv=p_truth   ;exact solution


452c334e   Ilyes Choubani   Implementation Of...
114

4750086c   Ilyes Choubani   nouvelle philosph...
115
;UNCOMMENT THIS BLOCK AFTER THE TESTING
452c334e   Ilyes Choubani   Implementation Of...
116

7f2b2149   Jean-Philippe Bernard   a Ilyes
117
 iv=p_truth+[0.,3.e-5,1.e-5,-2.e-5,5e-2,5,5,0.2];shifted from solution
4750086c   Ilyes Choubani   nouvelle philosph...
118
; ;iv=p_truth+[0.,3.e-5,1.e-5,-2.e-5,5];shifted from solution
452c334e   Ilyes Choubani   Implementation Of...
119

452c334e   Ilyes Choubani   Implementation Of...
120

4750086c   Ilyes Choubani   nouvelle philosph...
121
122
123
124
125
;TESTING THE ISRF PLUGIN

Npar=n_elements(pd)  
ulimed=replicate(0,Npar)
llimed=replicate(1,Npar)
7f2b2149   Jean-Philippe Bernard   a Ilyes
126
llims=replicate(0,Npar)
b5ccb706   Jean-Philippe Bernard   improved to fit p...
127
128
129
130

;== 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

452c334e   Ilyes Choubani   Implementation Of...
131
132
;dustem_init_fixed_params,fpd,fiv
    
b5ccb706   Jean-Philippe Bernard   improved to fit p...
133
134
135
136
;(*!dustem_params).grains[0].TYPE_KEYWORDS='logn-chrg-spin'
;(*!dustem_params).grains[1].TYPE_KEYWORDS='logn-chrg-spin'
;(*!dustem_params).grains[4].TYPE_KEYWORDS='pol-plaw-ed'

4750086c   Ilyes Choubani   nouvelle philosph...
137
138
139

dustem_init_plugins, pd

b5ccb706   Jean-Philippe Bernard   improved to fit p...
140
141
142
143
help,*!dustem_fit

help,!run_pol

b5ccb706   Jean-Philippe Bernard   improved to fit p...
144
145
146
sed.spec=dustem_compute_sed(p_truth,sst,_extra=extra)   ;sst is not set on purpose, for dustem_compute_sed to compute the sed using fortran code
sed.error=(sed.spec*0.1);>0.1

4750086c   Ilyes Choubani   nouvelle philosph...
147
148
149
150
;UNCOMMENT THIS BLOCK AFTER THE TESTING
;polsed.spec=dustem_compute_polsed(p_truth,sstp,_extra=extra,dustem_qsed,dustem_used)   ;sst is not set on purpose, for dustem_compute_sed to compute the sed using fortran code

polsed.spec=dustem_compute_polsed(p_truth,sstp,_extra=extra)
f882e687   Ilyes Choubani   improved
151

b5ccb706   Jean-Philippe Bernard   improved to fit p...
152
polsed.error=(polsed.spec*0.1)
b5ccb706   Jean-Philippe Bernard   improved to fit p...
153

452c334e   Ilyes Choubani   Implementation Of...
154

f882e687   Ilyes Choubani   improved
155
156
157
158
; ;creating fake polext
;   
; polext.spec=dustem_compute_polext(p_truth,ssttp,dustem_qext,dustem_uext)
; polext.error=(polext.spec*0.1)
452c334e   Ilyes Choubani   Implementation Of...
159
  
452c334e   Ilyes Choubani   Implementation Of...
160
161


4750086c   Ilyes Choubani   nouvelle philosph...
162
;UNCOMMENT THIS BLOCK AFTER THE TESTING
452c334e   Ilyes Choubani   Implementation Of...
163

4750086c   Ilyes Choubani   nouvelle philosph...
164
165
166
167
168
169
170
; ;SET qsed and used data - comment if extinction polarization data is to be used
; qsed.spec=dustem_qsed
; qsed.error=qsed.spec*0.1
; 
; 
; used.spec=dustem_used
; used.error=used.spec*0.1
452c334e   Ilyes Choubani   Implementation Of...
171
172


f882e687   Ilyes Choubani   improved
173
; ;SET qext and uext data - comment if polarizarion data is to be used
452c334e   Ilyes Choubani   Implementation Of...
174
175
176
177
178
179
; qext.spec=dustem_qext
; qext.error=qext.spec*0.1
; 
; 
; uext.spec=dustem_uext
; uext.error=uext.spec*0.1
f882e687   Ilyes Choubani   improved
180
; ; 
452c334e   Ilyes Choubani   Implementation Of...
181

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

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

4750086c   Ilyes Choubani   nouvelle philosph...
186
187
188
189
190
191
192
193
194
195
196
;UNCOMMENT THIS BLOCK AFTER THE TESTING
; st=dustem_set_data(polsed=polsed[ind],sed=sed[ind],qsed=qsed[ind],used=used[ind]) ; comment if second line is to be used
; ;st=dustem_set_data(sed=sed[ind],polext=polext[ind],qext=qext[ind],uext=uext[ind])


st=dustem_set_data(polsed=polsed[ind],sed=sed[ind])





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

b5ccb706   Jean-Philippe Bernard   improved to fit p...
198
;=== RUN fit
4750086c   Ilyes Choubani   nouvelle philosph...
199
200
201
202
203
204
tol=1.e-16
xtol=1.e-16
Nitermax=30; just an initialization
;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...
205
;Nitermax=4           ;maximum number of iteration. This is the criterion which will stop the fit procedure
b5ccb706   Jean-Philippe Bernard   improved to fit p...
206
207
208
209
210
211
212
213
214
215
216
217

xrange=[1.,2.e4]
yrange=[1e-4,1.e3]

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...
218
;help,(*!dustem_fit),/STRUCTURE
b5ccb706   Jean-Philippe Bernard   improved to fit p...
219
220

;=== SAVE FIT RESULTS
66aff855   Jean-Philippe Bernard   modified to impro...
221
file_out='/tmp/DUSTEM_polsed_fit_example.sav'
b5ccb706   Jean-Philippe Bernard   improved to fit p...
222
223
224
225
226
227
dustem_save_system_variables,file_out
message,'Saved '+file_out,/continue

stop

;======================================
f882e687   Ilyes Choubani   improved
228
;====You can exit IDL here and re-enter   -   THIS PART APPEARS TO BE FAULTY. NEEDS INVESTIGATION 
b5ccb706   Jean-Philippe Bernard   improved to fit p...
229
;======================================
66aff855   Jean-Philippe Bernard   modified to impro...
230
file='/tmp/DUSTEM_polsed_fit_example.sav'
b5ccb706   Jean-Philippe Bernard   improved to fit p...
231
232
233
dustem_restore_system_variables,file

;=== Plot best fit
b5ccb706   Jean-Philippe Bernard   improved to fit p...
234
235
236
237
238
win=1
window,win & win=win+1
xrange=[1.,2.e4]
yrange=[1e-4,1.e3]

b5ccb706   Jean-Philippe Bernard   improved to fit p...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
;=== 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
254
  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...
255
256
  device,filename=ps_file,/color
ENDIF
b5ccb706   Jean-Philippe Bernard   improved to fit p...
257

66aff855   Jean-Philippe Bernard   modified to impro...
258
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...
259

b5ccb706   Jean-Philippe Bernard   improved to fit p...
260
261
262
263
264
265
266
267
268
269
270
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...