Blame view

src/idl/dustem_mpfit_run.pro 15 KB
427f1205   Jean-Michel Glorian   version 4.2 merged
1
2
3
4
FUNCTION dustem_mpfit_run,x,p_min,_EXTRA=extra

;+
; NAME:
36e8b879   Jean-Michel Glorian   update from deborah
5
;    dustem_mpfit_run_vg
427f1205   Jean-Michel Glorian   version 4.2 merged
6
7
8
9
10
; PURPOSE:
;    function used to fit dustem model with dustem_mpfit_sed
; CATEGORY:
;    Dustem
; CALLING SEQUENCE:
36e8b879   Jean-Michel Glorian   update from deborah
11
;    res=dustem_mpfit_run_vg(x,params[,_EXTRA=extra][,cc_sed=cc_sed][,/help])
427f1205   Jean-Michel Glorian   version 4.2 merged
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
; INPUTS:
;    x         = necessary for interface but unused
;    params    = Dustem Model parameters normalized to initial values
; OPTIONAL INPUT PARAMETERS:
;    cc_sed    = SED color correction factors
;    _extra    = extra parameters for the plot routine
; OUTPUTS:
;    None
; OPTIONAL OUTPUT PARAMETERS:
;    None
; ACCEPTED KEY-WORDS:
;    help      = If set, print this help
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    SED and model are plotted if !dustem_show_plot
; RESTRICTIONS:
;    The dustem idl wrapper must be installed
; PROCEDURE:
;    None
; EXAMPLES
36e8b879   Jean-Michel Glorian   update from deborah
33
;    res=dustem_mpfit_run_vg(x,params[,_EXTRA=extra][,cc_sed=cc_sed][,/help])
427f1205   Jean-Michel Glorian   version 4.2 merged
34
35
36
37
38
39
40
41
42
; MODIFICATION HISTORY:
;    Adapted to extinction and polarization by Vincent Guillet (IAS)
;    Written by J.-Ph. Bernard
;    see evolution details on the dustem cvs maintained at CESR
;    Contact J.-Ph. Bernard (Jean-Philippe.Bernard@cesr.fr) in case of problems.
;-

;note that x is not used.

36e8b879   Jean-Michel Glorian   update from deborah
43
44
;COMMON MPFIT_ERROR, ERROR_CODE

427f1205   Jean-Michel Glorian   version 4.2 merged
45
IF keyword_set(help) THEN BEGIN
36e8b879   Jean-Michel Glorian   update from deborah
46
  doc_library,'dustem_mpfit_run_vg'
427f1205   Jean-Michel Glorian   version 4.2 merged
47
48
49
  sed=0.
  goto,the_end
ENDIF
427f1205   Jean-Michel Glorian   version 4.2 merged
50
51

p_dim=p_min*(*(*!dustem_fit).param_init_values)
36e8b879   Jean-Michel Glorian   update from deborah
52
;Run dustem with as an interface to mpfitfun
427f1205   Jean-Michel Glorian   version 4.2 merged
53

427f1205   Jean-Michel Glorian   version 4.2 merged
54
IF !dustem_idl_continuum THEN cont=dustem_create_continuum()
b5ccb706   Jean-Philippe Bernard   improved to fit p...
55
56
IF !dustem_idl_freefree THEN freefree=dustem_create_freefree()
IF !dustem_idl_synchrotron THEN synchrotron=dustem_create_synchrotron()
427f1205   Jean-Michel Glorian   version 4.2 merged
57
58
59
60
61
62

;CALL THE DUSTEM_CREATE FUNCTIONS
;Actually, res is never used
;THIS line below was added by NF, but removed by JPB on Dec 5 2009
;dustem_set_params,p_dim         ;NF on October 2009 (params have to be updated before calling some functions !)
;dustem_create_functions,p_dim/(*!IDO),cont=cont,res=res
b5ccb706   Jean-Philippe Bernard   improved to fit p...
63
dustem_create_functions,p_dim/(*(*!dustem_fit).param_init_values),cont=cont,freefree=freefree,synchrotron=synchrotron,res=res
427f1205   Jean-Michel Glorian   version 4.2 merged
64
65
66
67
68
69

;if ISRF has been modified and IONFRAC has not been modified, then call dustem_create_ionfrac
;NF on October 2009 : handling of DUSTEM_CREATE_IONFRAC has been changed
;this call only if IONFRACPAH master-keyword in GRAIN.DAT
;Removed for now (JPB). Use of ionfrac should be fully revisited.
IF !run_ionfrac gt 0. THEN toto = dustem_create_ionfrac()
427f1205   Jean-Michel Glorian   version 4.2 merged
70
71
;RUN THE MODEL AND GET THE SPECTRUM
st = dustem_run(p_dim)
36e8b879   Jean-Michel Glorian   update from deborah
72
st_model=dustem_read_all(!dustem_soft_dir)
427f1205   Jean-Michel Glorian   version 4.2 merged
73
74
75
76

chi2_sed      = 0  
chi2_ext      = 0  
chi2_polext   = 0 
36e8b879   Jean-Michel Glorian   update from deborah
77
78
chi2_polsed   = 0 
chi2_polfrac   = 0 
427f1205   Jean-Michel Glorian   version 4.2 merged
79
80
81
rchi2_sed     = 0
rchi2_ext     = 0
rchi2_polext  = 0
36e8b879   Jean-Michel Glorian   update from deborah
82
83
rchi2_polsed  = 0
rchi2_polfrac  = 0
427f1205   Jean-Michel Glorian   version 4.2 merged
84
85
86
wrchi2_sed    = 0
wrchi2_ext    = 0
wrchi2_polext = 0
36e8b879   Jean-Michel Glorian   update from deborah
87
88
wrchi2_polsed = 0
wrchi2_polfrac = 0
427f1205   Jean-Michel Glorian   version 4.2 merged
89
90
91
n_sed         = 0
n_ext         = 0
n_polext      = 0
36e8b879   Jean-Michel Glorian   update from deborah
92
93
94
95
n_polsed      = 0
n_polfrac      = 0

win = 0
427f1205   Jean-Michel Glorian   version 4.2 merged
96
97
98

tagnames=tag_names(!dustem_data)
dustem_out = [0]
36e8b879   Jean-Michel Glorian   update from deborah
99
100
101
ind_data_tag=dustem_data_tag(count=count_data_tag)

;if exist('./noplot') then !dustem_show_plot = 0 else if !dustem_show_plot eq 0 then !dustem_show_plot = 1
427f1205   Jean-Michel Glorian   version 4.2 merged
102

b5ccb706   Jean-Philippe Bernard   improved to fit p...
103
FOR i_tag=0,count_data_tag-1 DO BEGIN
427f1205   Jean-Michel Glorian   version 4.2 merged
104

6730c3f8   Jean-Philippe Bernard   modified to be co...
105
106
;stop

36e8b879   Jean-Michel Glorian   update from deborah
107
108
109
;print,i_tag,ind_data_tag(i_tag)
;print,tagnames(ind_data_tag(i_tag))
    CASE tagnames(ind_data_tag(i_tag)) OF
427f1205   Jean-Michel Glorian   version 4.2 merged
110

b5ccb706   Jean-Philippe Bernard   improved to fit p...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		'SED'	: BEGIN

	        dustem_sed = dustem_compute_sed(p_dim,st,cont=cont,freefree=freefree,synchrotron=synchrotron)
	        chi2_sed   = total(((*!dustem_data.sed).values-dustem_sed)^2/(*!dustem_data.sed).sigma^2)
			n_sed = n_elements((*!dustem_data.sed).values)
	       	rchi2_sed  = chi2_sed / n_sed
	       	wrchi2_sed  = rchi2_sed * !fit_rchi2_weight.sed
	       	message,"chi2 SED = "+strtrim(chi2_sed,2)+"  rchi2 SED = "+strtrim(rchi2_sed,2),/continue;," weighted rchi2 SED=",wrchi2_sed
			;print,'wl ',(*!dustem_data.sed).wav
			;print,"Delta SED per wl: ",(*!dustem_data.sed).values-dustem_sed
			;print,"error per wl: ",(*!dustem_data.sed).sigma
			;print,"chi2 per wl: ",((*!dustem_data.sed).values-dustem_sed)^2/(*!dustem_data.sed).sigma^2
	        alpha = total(((*!dustem_data.sed).values*dustem_sed)/(*!dustem_data.sed).sigma^2) $
	              		/ total(((*!dustem_data.sed).values)^2/(*!dustem_data.sed).sigma^2)
	        beta = total(((*!dustem_data.sed).values*dustem_sed)/(*!dustem_data.sed).sigma^2) $
	             		/ total(dustem_sed^2/(*!dustem_data.sed).sigma^2)
427f1205   Jean-Michel Glorian   version 4.2 merged
127
		
b5ccb706   Jean-Philippe Bernard   improved to fit p...
128
	        print,"Best SED chi2 obtained if f_HI = ",alpha, " or if we multiply model by ",beta
36e8b879   Jean-Michel Glorian   update from deborah
129

b5ccb706   Jean-Philippe Bernard   improved to fit p...
130
131
132
133
134
135
	        	;dustem_plot_nuinu_em,st,dustem_sed,cont,/print_radiance,p_dim=p_dim
	               
			; Plot if needed
	        IF !dustem_show_plot NE 0 THEN BEGIN
	            window,0
	            dustem_plot_fit_sed,st,dustem_sed,cont,freefree,synchrotron,_extra=extra,res=p_min*(*(*!dustem_fit).param_init_values),chi2=(*!dustem_fit).chi2,rchi2=(*!dustem_fit).rchi2
b5ccb706   Jean-Philippe Bernard   improved to fit p...
136
137
138
		 	ENDIF

	        dustem_out = [ dustem_out, dustem_sed ]
36e8b879   Jean-Michel Glorian   update from deborah
139
140
141

		END

b5ccb706   Jean-Philippe Bernard   improved to fit p...
142
143
144
145
146
147
148
149
150
		'EXT' 	: BEGIN 

			dustem_ext = interpol(st.ext.ext_tot,st.ext.wav,(*!dustem_data.ext).wav) 
	        chi2_ext   = total(((*!dustem_data.ext).values-dustem_ext)^2/(*!dustem_data.ext).sigma^2)
			n_ext = n_elements((*!dustem_data.ext).values)
			rchi2_ext  = chi2_ext / n_ext
	        	wrchi2_ext  = rchi2_ext * !fit_rchi2_weight.ext
	        	print,"chi2 EXT = ",chi2_ext,"  rchi2 EXT = ",rchi2_ext;," weighted rchi2 EXT=",wrchi2_ext
	        	;print,"chi2 per wl: ",((*!dustem_data.ext).values-dustem_ext)^2/(*!dustem_data.ext).sigma^2
36e8b879   Jean-Michel Glorian   update from deborah
151

b5ccb706   Jean-Philippe Bernard   improved to fit p...
152
153
			; Plot if needed
			IF !dustem_show_plot NE 0 THEN BEGIN
36e8b879   Jean-Michel Glorian   update from deborah
154

b5ccb706   Jean-Philippe Bernard   improved to fit p...
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
				win+=1
	                	;dustem_plot_ext,st,yr=[1.e-6,1.e0],/ysty,xr=[0.5,40],/xsty,win=win
				xr=[0.3,40]
				yr=[1.e-26,1.e-21]
				dustem_plot_ext,st,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,mergePAH=mergePAH,/donotclose,multi=1
				dustem_plot_ext,st,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,mergePAH=mergePAH,multi=2
		
				win+=1
	                	;dustem_plot_ext,st,/UV,yr=[0,2.5],/ysty,xr=[0,10],/xsty,win=win
				xr=[0.2,10]
				yr=[0,3e-21]
				dustem_plot_ext,st,/UV,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,mergePAH=mergePAH,/donotclose,multi=1
				dustem_plot_ext,st,/UV,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,mergePAH=mergePAH,multi=2

			ENDIF
			
			dustem_plot_ext,st,/print_Rv

		 	dustem_out = [ dustem_out, dustem_ext ]

		 	END

		'POLEXT': BEGIN

			IF !dustem_show_plot NE 0 and !run_circ then begin
	                   win+=1
	                 
				dustem_plot_circ,st,win=win,xr=[0.1,5],aligned=st_model.align.grains.aligned,yr=[-0.03,0.03]
			endif

			IF !run_lin then begin

				dustem_polext = interpol(st.polext.ext_tot,st.polext.wav,(*!dustem_data.polext).wav) 
	        		chi2_polext   = total(((*!dustem_data.polext).values-dustem_polext)^2/(*!dustem_data.polext).sigma^2)
				n_polext = n_elements((*!dustem_data.polext).values)
	        		rchi2_polext  = chi2_polext / n_polext
	        		wrchi2_polext  = rchi2_polext * !fit_rchi2_weight.polext
	        		print,"chi2 POLEXT = ",chi2_polext,"  rchi2 POLEXT = ",rchi2_polext;," weighted rchi2 POLEXT=",wrchi2_polext
	        		;print,"chi2 per wl: ",((*!dustem_data.polext).values-dustem_polext)^2/(*!dustem_data.polext).sigma^2

	        		; Fit in (pmax,lmax,K)
	        		x=st.polext.wav
	        		logx=alog(x)
	        		logy=alog(st.polext.ext_tot)
	        		;j=where(st.polext.ext_tot gt 0 and x gt 0.2 and x lt 1)

	        		; Whittet+1992 (Table 1)
	        		Uband = 0.36
	        		Bband = 0.43
	        		Vband = 0.55
	        		Rband = 0.63
	        		Iband = 0.78
	        		Jband = 1.21
	        		Hband = 1.64
	        		Kband = 2.04
	        		bands = [Uband,Bband,Vband,Rband,Iband,Jband,Hband]
	        		nbands = n_elements(bands)
	        		jband = indgen(nbands)*0
	        		for k=0,nbands-1 do begin
	                		xx=min(abs(st.polext.wav-bands(k)),j)
	                		jband(k) = j
	        		endfor
	        		;j=where(st.polext.ext_tot gt 0 and x gt 0.2 and x lt 1)
	        		res = poly_fit(logx(jband),logy(jband),2)
			
	        		print
	        		print,"Serkowski Fit with free K over ",string(st.polext(jband).wav,format='(100F5.2)')
	        		print,"-------------------------"
	        		K    = -res(2)
	        		lmax = exp(res(1)/(2*K))
	        		pmax = exp(res(0)+K*alog(lmax)^2)
	        		print,"* lmax = ",lmax," micron"
	        		print,"* K    = ",K
	        		print,"* pmax = ",pmax," for NH=1e21"
	        		print
			
				; Plot if needed
				IF !dustem_show_plot NE 0 THEN BEGIN

	       				; Polarization fraction in the UV-IR
	                           win+=1
	                           
	       				dustem_plot_polar,st,aligned=st_model.align.grains.aligned,yr=[0,0.15],/ysty,xr=[0.1,4],/xsty,win=win
					
	   				; Polarisation curve (Serkowski)
					win+=1
					xr=[0.1,50]
					yr=[5e-25,5e-23]
					dustem_plot_polar,st,aligned=st_model.align.grains.aligned,/UV,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,cont=cont,freefree=freefree,synchrotron=synchrotron,/donotclose,multi=1
					dustem_plot_polar,st,aligned=st_model.align.grains.aligned,/UV,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,cont=cont,freefree=freefree,synchrotron=synchrotron,multi=2
		
					win+=1
					dustem_plot_fpol,win=win,xr=[10,1000],yr=[0,1.1]
					
	                                
	            ENDIF
36e8b879   Jean-Michel Glorian   update from deborah
251

b5ccb706   Jean-Philippe Bernard   improved to fit p...
252
253
254
				;dustem_plot_polar,st,/print_ratio,cont=cont
	                     
				dustem_out = [ dustem_out, dustem_polext ]
36e8b879   Jean-Michel Glorian   update from deborah
255

b5ccb706   Jean-Philippe Bernard   improved to fit p...
256
			ENDIF
36e8b879   Jean-Michel Glorian   update from deborah
257

b5ccb706   Jean-Philippe Bernard   improved to fit p...
258
			END
427f1205   Jean-Michel Glorian   version 4.2 merged
259

b5ccb706   Jean-Philippe Bernard   improved to fit p...
260
		'POLFRAC': BEGIN
427f1205   Jean-Michel Glorian   version 4.2 merged
261

b5ccb706   Jean-Philippe Bernard   improved to fit p...
262
			IF !run_lin then begin
427f1205   Jean-Michel Glorian   version 4.2 merged
263

b5ccb706   Jean-Philippe Bernard   improved to fit p...
264
265
266
267
268
269
270
271
272
	            dustem_polsed = dustem_compute_polsed(p_dim,st.polsed,cont=cont,freefree=freefree,synchrotron=synchrotron)
				dustem_sed_tmp = dustem_compute_sed(p_dim,st,cont=cont,freefree=freefree,synchrotron=synchrotron)
				dustem_sed = dustem_polsed
				ind_filt=where((*!dustem_data.polsed).filt_names NE 'SPECTRUM',count_filt)
				for i = 0, count_filt-1 do begin
					j=where((*!dustem_data.polsed).filt_names(ind_filt(i)) eq (*!dustem_data.sed).filt_names,count)
					if count eq 1 then dustem_sed(i) = dustem_sed_tmp(j(0))
				endfor 
				dustem_polfrac = dustem_polsed/dustem_sed
427f1205   Jean-Michel Glorian   version 4.2 merged
273

b5ccb706   Jean-Philippe Bernard   improved to fit p...
274
275
276
			; We normalize at 353GHz
			;x=min(((*!dustem_data.polfrac).wav-850)^2,j353)
			;dustem_polfrac = dustem_polfrac/dustem_polfrac(j353)*(*!dustem_data.polfrac).values(j353)
36e8b879   Jean-Michel Glorian   update from deborah
277

b5ccb706   Jean-Philippe Bernard   improved to fit p...
278
279
280
281
282
283
	        	chi2_polfrac   = total(((*!dustem_data.polfrac).values-dustem_polfrac)^2/(*!dustem_data.polfrac).sigma^2)
				n_polfrac = n_elements((*!dustem_data.polfrac).values)
	        	rchi2_polfrac  = chi2_polfrac / n_polfrac
	        	wrchi2_polfrac  = rchi2_polfrac * !fit_rchi2_weight.polfrac
	        	print,"chi2 POLFRAC = ",chi2_polfrac,"  rchi2 POLFRAC = ",rchi2_polfrac;," weighted rchi2 POLFRAC=",wrchi2_polfrac
	        	;print,"chi2 per wl: ",((*!dustem_data.polfrac).values-dustem_polfrac)^2/(*!dustem_data.polfrac).sigma^2
36e8b879   Jean-Michel Glorian   update from deborah
284

b5ccb706   Jean-Philippe Bernard   improved to fit p...
285
	            dustem_out = [ dustem_out, dustem_polfrac ]
36e8b879   Jean-Michel Glorian   update from deborah
286

b5ccb706   Jean-Philippe Bernard   improved to fit p...
287
288
			; Plot if needed
				IF !dustem_show_plot NE 0 THEN BEGIN
36e8b879   Jean-Michel Glorian   update from deborah
289

b5ccb706   Jean-Philippe Bernard   improved to fit p...
290
291
292
				; Also show prediction for polarized P/I
					win+=1
	       			dustem_plot_polar,st,aligned=st_model.align.grains.aligned,/Pfrac,win=win,cont=cont,freefree=freefree,synchrotron=synchrotron,yr=[0,0.25],/ysty,xr=[10,5d3]
36e8b879   Jean-Michel Glorian   update from deborah
293

b5ccb706   Jean-Philippe Bernard   improved to fit p...
294
		 		ENDIF
36e8b879   Jean-Michel Glorian   update from deborah
295

b5ccb706   Jean-Philippe Bernard   improved to fit p...
296
		 	ENDIF
36e8b879   Jean-Michel Glorian   update from deborah
297

b5ccb706   Jean-Philippe Bernard   improved to fit p...
298
			END
36e8b879   Jean-Michel Glorian   update from deborah
299

b5ccb706   Jean-Philippe Bernard   improved to fit p...
300
		'POLSED': BEGIN
b5ccb706   Jean-Philippe Bernard   improved to fit p...
301
302
			IF !run_lin THEN BEGIN

66aff855   Jean-Philippe Bernard   modified to impro...
303
	        	dustem_polsed = dustem_compute_polsed(p_dim,st,cont=cont,freefree=freefree,synchrotron=synchrotron)
b5ccb706   Jean-Philippe Bernard   improved to fit p...
304
305
306
307
308
309
310
311
312
	        	chi2_polsed   = total(((*!dustem_data.polsed).values-dustem_polsed)^2/(*!dustem_data.polsed).sigma^2)
				n_polsed = n_elements((*!dustem_data.polsed).values)
	        	rchi2_polsed  = chi2_polsed / n_polsed
	        	wrchi2_polsed  = rchi2_polsed * !fit_rchi2_weight.polsed
	        	message,"chi2 POLSED = "+strtrim(chi2_polsed,2)+"  rchi2 POLSED = "+strtrim(rchi2_polsed,2),/continue;," weighted rchi2 POLSED=",wrchi2_polsed
	        	print,"chi2 per wl: ",((*!dustem_data.polsed).values-dustem_polsed)^2/(*!dustem_data.polsed).sigma^2
	        	;print,"SNR per wl: ",(*!dustem_data.polsed).values/(*!dustem_data.polsed).sigma

	            dustem_out = [ dustem_out, dustem_polsed ]
b5ccb706   Jean-Philippe Bernard   improved to fit p...
313
314
				; Plot if needed
				IF !dustem_show_plot NE 0 THEN BEGIN
b5ccb706   Jean-Philippe Bernard   improved to fit p...
315
316
317
318
319
					win+=1
					xr=[10,5e3]
					yr=[1e-34,1e-28]
					dustem_plot_polar,st,aligned=st_model.align.grains.aligned,/SED,ps=filename,win=win,cont=cont,freefree=freefree,synchrotron=synchrotron,xr=xr,yr=yr,/xsty,/donotclose,multi=1
					dustem_plot_polar,st,aligned=st_model.align.grains.aligned,/SED,ps=filename,win=win,cont=cont,freefree=freefree,synchrotron=synchrotron,xr=xr,yr=yr,/xsty,multi=2
b5ccb706   Jean-Philippe Bernard   improved to fit p...
320
				ENDIF
b5ccb706   Jean-Philippe Bernard   improved to fit p...
321
			ENDIF
427f1205   Jean-Michel Glorian   version 4.2 merged
322
323
		END

b5ccb706   Jean-Philippe Bernard   improved to fit p...
324
	    ELSE :	stop
427f1205   Jean-Michel Glorian   version 4.2 merged
325
326
327

    ENDCASE

b5ccb706   Jean-Philippe Bernard   improved to fit p...
328
ENDFOR
427f1205   Jean-Michel Glorian   version 4.2 merged
329

36e8b879   Jean-Michel Glorian   update from deborah
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
DOF = n_elements(p_min)
total_chi2 = 0
total_rchi2 = 0
total_n = 0
if !fit_rchi2_weight.ext ne 0 then begin
	total_n += n_ext
	total_chi2 +=  chi2_ext
	total_rchi2 += rchi2_ext
endif
if !fit_rchi2_weight.sed ne 0 then begin
	total_n += n_sed
	total_chi2 += chi2_sed
	total_rchi2 += rchi2_sed
endif
if !run_pol then begin
if !fit_rchi2_weight.polext ne 0 then begin
	total_n += n_polext
	total_chi2 += chi2_polext
	total_rchi2 += rchi2_polext
endif
if !fit_rchi2_weight.polsed ne 0 then begin
	total_n += n_polsed
	total_chi2 += chi2_polsed
	total_rchi2 += rchi2_polsed
endif
if !fit_rchi2_weight.polfrac ne 0 then begin
	total_n += n_polfrac
	total_chi2 += chi2_polfrac
	total_rchi2 += rchi2_polfrac
endif
endif

total_wchi2 = chi2_ext * !fit_rchi2_weight.ext $
            + chi2_sed * !fit_rchi2_weight.sed 
if !run_pol then begin
	total_wchi2 	+= chi2_polext  * !fit_rchi2_weight.polext $
             		 + chi2_polsed  * !fit_rchi2_weight.polsed $
             		 + chi2_polfrac * !fit_rchi2_weight.polfrac 
endif

9ccf7615   Jean-Philippe Bernard   modified to fit u...
370
message,'======================================================================',/continue
36e8b879   Jean-Michel Glorian   update from deborah
371
372
373
374
375
376
377
378
379
380
print,"Total weighted chi2 driving mpfitfun = ", total_wchi2
print,"Nb of data points = ", total_n
print,"DOF = ", DOF
print,"Reduced chi2 EXT/SED/POLEXT/POLSED/POLFRAC",rchi2_ext,rchi2_sed,rchi2_polext,rchi2_polsed,rchi2_polfrac
print,"Weighted reduced chi2 EXT/SED/POLEXT/POLSED/POLFRAC",wrchi2_ext,wrchi2_sed,wrchi2_polext,wrchi2_polsed,wrchi2_polfrac
if !run_pol then 	print,"Weights",!fit_rchi2_weight.ext,!fit_rchi2_weight.sed,!fit_rchi2_weight.polext,!fit_rchi2_weight.polsed,!fit_rchi2_weight.polfrac $ 
else 			print,"Weights",!fit_rchi2_weight.ext,!fit_rchi2_weight.sed
print,"Mean weighted reduced chi2 = ", total_wchi2/(total_n-DOF)
print,"Unweighted reduced Total chi2 = ", total_chi2/(total_n-DOF)
print,"Unweighted mean reduced chi2 = ", total_rchi2/4*total_n/(total_n-DOF)
9ccf7615   Jean-Philippe Bernard   modified to fit u...
381
message,'======================================================================',/continue
36e8b879   Jean-Michel Glorian   update from deborah
382
383
384
385
386
387
388
389
390

;print,"Total reduced chi2 = ", (wrchi2_sed+wrchi2_ext+wrchi2_polext+wrchi2_polsed+wrchi2_polfrac)/(n_sed+n_ext+n_polext+n_polsed+n_polfrac-DOF)
; Reduced chi2 is like if there were the same nb of data points for each curve
;print,"Mean reduced chi2 = ", (wrchi2_sed+wrchi2_ext+wrchi2_polext+wrchi2_polsed+wrchi2_polfrac) * total_n/(total_n-DOF)

; Plot size distribution
win+=1
xr=[0.3,5e3]
yr=[0.1,100]
9ccf7615   Jean-Philippe Bernard   modified to fit u...
391
;IF !dustem_show_plot NE 0 THEN dustem_plot_sdist,xr=xr,yr=yr,ps=filename,win=win;,/donotclose
427f1205   Jean-Michel Glorian   version 4.2 merged
392
393
394
395

; Suppress the artificial [0] at the beginning 
dustem_out = dustem_out[1:*]

9ccf7615   Jean-Philippe Bernard   modified to fit u...
396
397
398
399
400
401
402
;IF defined(extra) THEN BEGIN
;  IF keyword_set(extra.plot_it) THEN BEGIN
;;    message,'params='+strtrim(p_min,2),/info,/continue
;;    stop
;    print,'dustem_mpfit_run_vg: Current parameters values:',p_dim
;  ENDIF
;ENDIF
427f1205   Jean-Michel Glorian   version 4.2 merged
403
404
405
406
407
408

the_end:

RETURN,dustem_out

END