Blame view

src/idl_retired/dustem_fit_sed_extsed_readme.pro 6.67 KB
0e608856   Jean-Philippe Bernard   improved
1
PRO dustem_fit_sed_extsed_readme,postscript=postscript,model=model,help=help,itermax=itermax
5b76b765   Jean-Philippe Bernard   First commit
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

;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
0e608856   Jean-Philippe Bernard   improved
38
;    dustem_fit_sed_extsed_readme,/postscript
5b76b765   Jean-Philippe Bernard   First commit
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
; 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_extsed_readme'
  goto,the_end
ENDIF

;=== initialise dustem
;dustem_init,mode='COMPIEGNE_ETAL2010',/pol
use_model='COMPIEGNE_ETAL2010'
IF keyword_set(model) THEN use_model=model

dustem_init,mode=use_model

!dustem_verbose=1
!dustem_show_plot=1

0e608856   Jean-Philippe Bernard   improved
61
62
63
64
65
IF keyword_set(postscript) THEN BEGIN
  ps_dir=!dustem_dat+'/Docs/Figures/'
  force_mkdir,ps_dir
ENDIF

5b76b765   Jean-Philippe Bernard   First commit
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
92
93
94
;stop

;==== read emission SED

dir=!dustem_wrap_soft_dir+'/Data/SEDs/'
file=dir+'Gal_composite_spectrum.xcat'
;file=dir+'Mathis1990_DISM.xcat'

;file=dir+'Gal_composite_spectrum_wAKARI.xcat'
;stop
spec=read_xcat(file,/silent)
ind=where(spec.error EQ 0.,count)
IF count NE 0 THEN spec(ind).error=0.2*spec(ind).spec
ind=where(spec.instru EQ 'FIRAS',count)
IF count NE 0 THEN spec(ind).error=0.2*spec(ind).spec

;==== read extinction SED

dir=!dustem_wrap_soft_dir+'/Data/EXTs/'
file=dir+'Mathis1990_DISM.xcat'
extsed=read_xcat(file,/silent)
ind=where(extsed.error EQ 0.,count)
IF count NE 0 THEN extsed[ind].error=0.2*spec(ind).spec


st=dustem_set_data(ext=extsed,sed=spec,rchi2_weight=rchi2_weight,f_HI=f_HI)

;=== Set which parameters you want to fit
pd = [ $
cbf75ef0   Ilyes Choubani   dustem_init_plugi...
95
             'dustem_plugin_continuum_2',$
5b76b765   Jean-Philippe Bernard   First commit
96
97
98
99
100
101
             '(*!dustem_params).gas.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
              ]                   

f882e687   Ilyes Choubani   improved
102
p_truth=[0.002,1.,7.8000E-04,7.8000E-04,1.6500E-04]    
5b76b765   Jean-Philippe Bernard   First commit
103
104
105

;=== set initial value of parameters you want to fit
;iv=p_truth   ;exact solution
f882e687   Ilyes Choubani   improved
106
iv=p_truth+[0.01,0.,3.e-5,1.e-5,-2.e-5]    ;shifted from solution
452c334e   Ilyes Choubani   Implementation Of...
107

f882e687   Ilyes Choubani   improved
108
109
110
ulimed=[0, 0 ,0   , 0   ,0   ]
llimed=[1 ,1 ,1   , 1   ,1   ]
llims =[0. ,0., 0.  ,0. ,0.  ]
452c334e   Ilyes Choubani   Implementation Of...
111
112
113
114
115
116


; 
dustem_init_plugins, pd  ;will have to be uncommented for this procedure to work. 


5b76b765   Jean-Philippe Bernard   First commit
117

5b76b765   Jean-Philippe Bernard   First commit
118
119
120
121

;== 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...
122
123
124



5b76b765   Jean-Philippe Bernard   First commit
125
;=== RUN fit
f882e687   Ilyes Choubani   improved
126
127
128
tol=1.e-12
xtol=1.e-12
Nitermax=3   ;maximum number of iteration. This is the criterium which will stop the fit procedure
37b5d8a7   Jean-Philippe Bernard   fixed to adapt gr...
129
IF keyword_set(itermax) THEN Nitermax=itermax
5b76b765   Jean-Philippe Bernard   First commit
130
131
132
133
134
135
136
137
138
139
140
141

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)

yr=[1e-4,10]
xr=[1,1.e5]
legend_xpos=0.6
legend_ypos=0.8
666cba76   Jean-Philippe Bernard   cleaned the code
142
tit='DUSTEMWrapper Intensity SED (Running)'
5b76b765   Jean-Philippe Bernard   First commit
143
144
ytit=textoidl('I_\nu (MJy/sr) for N_H=10^{20} H/cm^2')
xtit=textoidl('\lambda (\mum)')
0e608856   Jean-Philippe Bernard   improved
145
res=dustem_mpfit_data(tol=tol,xtol=xtol,Nitermax=Nitermax,xrange=xrange,/xstyle,yrange=yrange,/ysty,/ylog,/xlog,xtit=xtit,ytit=ytit,legend_xpos=legend_xpos,legend_ypos=legend_ypos,title=tit)
5b76b765   Jean-Philippe Bernard   First commit
146
147
148
149
150

t2=systime(0,/sec)

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

5b76b765   Jean-Philippe Bernard   First commit
151
152
153
154
155
;=== SAVE FIT RESULTS
file_out='/tmp/DUSTEM_sedextsed_fit_example.sav'
dustem_save_system_variables,file_out
message,'Saved '+file_out,/continue

5b76b765   Jean-Philippe Bernard   First commit
156
157
158
159
160
161
;======================================
;====You can exit IDL here and re-enter
;======================================
file='/tmp/DUSTEM_sedextsed_fit_example.sav'
dustem_restore_system_variables,file

0e608856   Jean-Philippe Bernard   improved
162
163
;=== Plot best fit intensity SED
win=0L
5b76b765   Jean-Philippe Bernard   First commit
164
window,win & win=win+1
0e608856   Jean-Philippe Bernard   improved
165
cleanplot
5b76b765   Jean-Philippe Bernard   First commit
166
167
xrange=[1.,2.e4]
yrange=[1e-4,1.e3]
5b76b765   Jean-Philippe Bernard   First commit
168
169
170
res=*(*!dustem_fit).current_param_values
chi2=(*!dustem_fit).chi2
rchi2=(*!dustem_fit).rchi2
5b76b765   Jean-Philippe Bernard   First commit
171
172
errors=(*(*!dustem_fit).current_param_errors)*(*(*!dustem_fit).param_init_values)

666cba76   Jean-Philippe Bernard   cleaned the code
173
tit='DUSTEMWrapper polarization dust Example (Saved)'
5b76b765   Jean-Philippe Bernard   First commit
174
175
176
ytit=textoidl('I_\nu (MJy/sr) for N_H=10^{20} H/cm^2')
xtit=textoidl('\lambda (\mum)')

0e608856   Jean-Philippe Bernard   improved
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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,legend_xpos=legend_xpos,legend_ypos=legend_ypos

loadct,13
IF keyword_set(postscript) THEN BEGIN
  previous_device=!d.name
  set_plot,'PS'
  !p.charsize=0.8
  ps_file=ps_dir+'Last_dustem_sed_fit.ps'
  device,filename=ps_file,/color

  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,legend_xpos=legend_xpos,legend_ypos=legend_ypos

  device,/close
  set_plot,previous_device
  message,'wrote '+ps_file,/info
ENDIF

;stop
;=== Plot best fit Extinction SED
5b76b765   Jean-Philippe Bernard   First commit
196
loadct,13
0e608856   Jean-Philippe Bernard   improved
197
198
199
200
201
202
203
204
window,win & win=win+1
cleanplot
xr=[0.3,40]
yr=[1.e-26,1.e-21]
dustem_extsed_plot,*(*!dustem_fit).current_param_values,pol=pol,xr=xr,yr=yr,/xsty,/ysty

IF keyword_set(postscript) THEN BEGIN
  previous_device=!d.name
5b76b765   Jean-Philippe Bernard   First commit
205
  set_plot,'PS'
0e608856   Jean-Philippe Bernard   improved
206
207
  !p.charsize=0.8
  ps_file=ps_dir+'Last_dustem_ext_ir_fit.ps'
5b76b765   Jean-Philippe Bernard   First commit
208
  device,filename=ps_file,/color
0e608856   Jean-Philippe Bernard   improved
209
210
211
212
213
214

  dustem_extsed_plot,*(*!dustem_fit).current_param_values,pol=pol,xr=xr,yr=yr,/xsty,/ysty

  device,/close
  set_plot,previous_device
  message,'wrote '+ps_file,/info
5b76b765   Jean-Philippe Bernard   First commit
215
216
ENDIF

0e608856   Jean-Philippe Bernard   improved
217
218
219
220
221
222
223
224
;stop

loadct,13
window,win & win=win+1
cleanplot
xr=[0.2,10]
yr=[0,3e-21]
dustem_extsed_plot,*(*!dustem_fit).current_param_values,pol=pol,xr=xr,yr=yr,/xsty,/ysty,/UV
5b76b765   Jean-Philippe Bernard   First commit
225

0e608856   Jean-Philippe Bernard   improved
226
227
228
229
230
231
232
233
234
IF keyword_set(postscript) THEN BEGIN
  previous_device=!d.name
  set_plot,'PS'
  !p.charsize=0.8
  ps_file=ps_dir+'Last_dustem_ext_uv_fit.ps'
  device,filename=ps_file,/color
  
  dustem_extsed_plot,*(*!dustem_fit).current_param_values,pol=pol,xr=xr,yr=yr,/xsty,/ysty,/UV
  
5b76b765   Jean-Philippe Bernard   First commit
235
  device,/close
0e608856   Jean-Philippe Bernard   improved
236
  set_plot,previous_device
5b76b765   Jean-Philippe Bernard   First commit
237
238
239
240
241
  message,'wrote '+ps_file,/info
ENDIF

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

0e608856   Jean-Philippe Bernard   improved
242
243
stop

5b76b765   Jean-Philippe Bernard   First commit
244
245
246
the_end:

END