Blame view

src/idl/dustem_fit_sed_polsed_readme.pro 7.55 KB
4535858e   Jean-Philippe Bernard   First commit ?
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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.
;-

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

dustem_define_la_common

;=== initialise dustem
;dustem_init,mode='COMPIEGNE_ETAL2010',/pol

;use_mode='G17_MODELA'

use_mode='G17_MODELC';'DBP90';'G17_MODELA';'THEMIS';;'USER_MODEL';'THEMIS';;'THEMIS';'G17_MODELA';'THEMIS';'NY_MODELA';'G17_MODELA';'DBP90';'USER_MODEL';'THEMIS';'NY_MODELA';'G17_MODELA';'DBP90';'COMPIEGNE_ETAL2010'
;,/pol
dustem_init,mode=use_mode,kwords=['logn-chrg-spin','plaw-pol','plaw-pol'],/pol;,kwords=['plaw-ed','logn','logn'];,/pol;kwords=['plaw-ed-chrg-spin'],/pol;,kwords=['plaw-ed'];kwords=['plaw-ed-chrg-spin'];,/pol;

!dustem_verbose=1
!dustem_show_plot=1
!dustem_nocatch=1
;help,!run_pol

;stop


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

;INITIALIZING IQU and associated errors to avoid problems when checking SED in dustem_set_data.pro
sed[*].StokesI=1.
sed.StokesQ=sed.StokesI/100.
sed.StokesU=sed.StokesI/100.
sed.SigmaII=sed.StokesI/100.
sed.SigmaQQ=sed.StokesI/100.
sed.SigmaUU=sed.StokesI/100.
sed.SigmaIQ=sed.StokesI/100.
sed.SigmaIU=sed.StokesI/100.
sed.SigmaQU=sed.StokesI/100.

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

;INITIALIZING THE !dustem_data tags that will be internally used 
;st=dustem_set_data(sed=sed,rchi2_weight=rchi2_weight,f_HI=f_HI)
2a47d796   Jean-Philippe Bernard   improved
92
dustem_set_data,sed
4535858e   Jean-Philippe Bernard   First commit ?
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

;st=dustem_set_data() ;this should be one of the tests


;=== Set which parameters you want to fit
pd = [ $
             '(*!dustem_params).G0', $      ;G0
             '(*!dustem_params).grains(0).mdust_o_mh',$  ;PAH0 mass fraction
             '(*!dustem_params).grains(1).mdust_o_mh',$  ;PAH0 mass fraction   
             '(*!dustem_params).grains(2).mdust_o_mh' $    ;PAH1 mass fraction
             ;'(*!dustem_params).grains(3).mdust_o_mh',$    ;amCBEx
             ;'dustem_plugin_synchrotron_2', $               ;Synchrotron amplitude
             ;'dustem_plugin_synchrotron_4', $               ;Synchrotron polarization angle
             ;'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' $
              ]                   

;p_truth=[1.,7.8000E-04,7.8000E-04,7.8000E-04,1.6500E-04,68.,4e-3,20]
;iv=p_truth+[0.,8.e-4,8.e-4,8.e-4,8.e-4,-10.,2e-4,10]      ;shifted from solution

;p_truth=[1.,7.8000E-04,7.8000E-04,7.8000E-04,1.6500E-04,5e-3,20.,68.,1.2,20]    
p_truth=[1.,7.8000E-04,7.8000E-04,7.8000E-04]
iv=p_truth+[0.,8.e-4,8.e-4,8.e-4]      ;shifted from solution

Npar=n_elements(pd)  
ulimed=replicate(0,Npar)
llimed=replicate(1,Npar)
llims=replicate(0,Npar)

;stop

dustem_init_parinfo,pd,iv,up_limited=ulimed,lo_limited=llimed,up_limits=ulims,lo_limits=llims;,tied=tied
;stop
dustem_init_plugins,pd

;Here fortran fails when pluggins are activated and/or type-keywods exist
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=abs(sed.StokesI*0.000001)

;sed.largeP=dustem_compute_polsed(p_truth,sstp)
;sed.sigma_largep=abs(sed.largeP*0.000001)

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=abs(sed.StokesQ*0.000001)

sed.StokesU=dustem_used
sed.sigmaUU=abs(sed.StokesU*0.000001)

message,'Fake polarization SED has been set',/continue
stop


;=== SET THE OBSERVATION STRUCTURE

ind=where(sed.wave GE 0.1)

st_bidon=dustem_set_data(m_fit=sed[ind],rchi2_weight=rchi2_weight,f_HI=f_HI) ; comment if second line is to be used

;=== RUN fit
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
;Nitermax=4           ;maximum number of iteration. This is the criterion which will stop the fit procedure

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)
title='CACA'    ;does not work if title is undefined ....
res=dustem_mpfit_data(tol=tol,xtol=xtol,Nitermax=Nitermax,xrange=xrange,/xstyle,yrange=yrange,/ysty,/ylog,/xlog,xtit='wavelength [mic]',ytit='Brightness []',title=title)
t2=systime(0,/sec)

stop

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

;help,(*!dustem_fit),/STRUCTURE

;=== SAVE FIT RESULTS
file_out='/tmp/DUSTEM_polsed_fit_example.sav'
dustem_save_system_variables,file_out
message,'Saved '+file_out,/continue

;stop

;======================================
;====You can exit IDL here and re-enter   -   THIS PART APPEARS TO BE FAULTY. NEEDS INVESTIGATION 
;======================================
file='/tmp/DUSTEM_polsed_fit_example.sav'
dustem_restore_system_variables,file

;=== Plot best fit
win=1
window,win & win=win+1
xrange=[1.,2.e4]
yrange=[1e-4,1.e3]

;=== 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'
  ps_file=!dustem_wrap_soft_dir+'/Docs/'+'Last_dustem_spinning_fit.ps';'/Docs/Figures/'+'Last_dustem_spinning_fit.ps'
  device,filename=ps_file,/color
ENDIF

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

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