Blame view

src/idl/dustem_mpfit_run.pro 23.4 KB
266ae799   Ilyes Choubani   General update
1
FUNCTION dustem_mpfit_run,x,p_min,_EXTRA=_extra
427f1205   Jean-Michel Glorian   version 4.2 merged
2
3
4

;+
; 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])
4750086c   Ilyes Choubani   nouvelle philosph...
34
;  ODIFICATION HISTORY:
427f1205   Jean-Michel Glorian   version 4.2 merged
35
36
37
38
39
40
41
42
;    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.

427f1205   Jean-Michel Glorian   version 4.2 merged
43
IF keyword_set(help) THEN BEGIN
9be94157   Jean-Philippe Bernard   improved
44
  doc_library,'dustem_mpfit_run'
427f1205   Jean-Michel Glorian   version 4.2 merged
45
46
47
  sed=0.
  goto,the_end
ENDIF
427f1205   Jean-Michel Glorian   version 4.2 merged
48
49

p_dim=p_min*(*(*!dustem_fit).param_init_values)
427f1205   Jean-Michel Glorian   version 4.2 merged
50

427f1205   Jean-Michel Glorian   version 4.2 merged
51
IF !run_ionfrac gt 0. THEN toto = dustem_create_ionfrac()
452c334e   Ilyes Choubani   Implementation Of...
52

427f1205   Jean-Michel Glorian   version 4.2 merged
53
;RUN THE MODEL AND GET THE SPECTRUM
45da29c6   Ilyes Choubani   updating mpfit_run
54
55
56
57
58
59
60
61
62
63

stop
dustem_activate_plugins,p_dim/(*(*!dustem_fit).param_init_values),st ;st is an output...

stop

st1 = dustem_run(p_dim) 
    
stop

36e8b879   Jean-Michel Glorian   update from deborah
64
st_model=dustem_read_all(!dustem_soft_dir)
427f1205   Jean-Michel Glorian   version 4.2 merged
65

45da29c6   Ilyes Choubani   updating mpfit_run
66

427f1205   Jean-Michel Glorian   version 4.2 merged
67
68
chi2_sed      = 0  
chi2_ext      = 0  
452c334e   Ilyes Choubani   Implementation Of...
69
70
71
72
chi2_qsed      =0
chi2_used      =0 
chi2_qext      =0
chi2_uext      =0 
18e4331f   Ilyes Choubani   general update (f...
73

427f1205   Jean-Michel Glorian   version 4.2 merged
74
75
rchi2_sed     = 0
rchi2_ext     = 0
452c334e   Ilyes Choubani   Implementation Of...
76
77
78
79
rchi2_qsed     =0
rchi2_used     =0
rchi2_qext     =0
rchi2_uext     =0
18e4331f   Ilyes Choubani   general update (f...
80

427f1205   Jean-Michel Glorian   version 4.2 merged
81
82
wrchi2_sed    = 0
wrchi2_ext    = 0
452c334e   Ilyes Choubani   Implementation Of...
83
84
85
86
wrchi2_qsed    =0
wrich2_used    =0
wrchi2_qext    =0
wrich2_uext    =0
18e4331f   Ilyes Choubani   general update (f...
87

427f1205   Jean-Michel Glorian   version 4.2 merged
88
89
n_sed         = 0
n_ext         = 0
452c334e   Ilyes Choubani   Implementation Of...
90
91
92
93
n_qsed         =0
n_used         =0
n_qext         =0
n_uext         =0
36e8b879   Jean-Michel Glorian   update from deborah
94
95

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
ind_data_tag=dustem_data_tag(count=count_data_tag)

45da29c6   Ilyes Choubani   updating mpfit_run
101
;stop
b5ccb706   Jean-Philippe Bernard   improved to fit p...
102
FOR i_tag=0,count_data_tag-1 DO BEGIN
427f1205   Jean-Michel Glorian   version 4.2 merged
103

36e8b879   Jean-Michel Glorian   update from deborah
104
    CASE tagnames(ind_data_tag(i_tag)) OF
266ae799   Ilyes Choubani   General update
105
        
b5ccb706   Jean-Philippe Bernard   improved to fit p...
106
		'SED'	: BEGIN
45da29c6   Ilyes Choubani   updating mpfit_run
107
    		
3ff9cac3   Ilyes Choubani   general update
108
	        dustem_sed = dustem_compute_sed(p_dim,st)
b5ccb706   Jean-Philippe Bernard   improved to fit p...
109
110
111
112
113
114
115
116
117
118
119
120
121
	        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
122
		
b5ccb706   Jean-Philippe Bernard   improved to fit p...
123
	        print,"Best SED chi2 obtained if f_HI = ",alpha, " or if we multiply model by ",beta
759a527d   Ilyes Choubani   general update
124
125
	        (*!dustem_fit).chi2=chi2_sed
	        (*!dustem_fit).rchi2=rchi2_sed
1355825c   Ilyes Choubani   General update
126
127
              
	        if isa(!dustem_data.sed) then dustem_out = [ dustem_out, dustem_sed ]
266ae799   Ilyes Choubani   General update
128
            
36e8b879   Jean-Michel Glorian   update from deborah
129
130
		END

1355825c   Ilyes Choubani   General update
131
		'EXT' 	: BEGIN ;STILL HAVEN'T DONE THE EXTINCTION PART
452c334e   Ilyes Choubani   Implementation Of...
132
133
    		
    		;ADDING PLUGIN TO SPECTRUM----------------
759a527d   Ilyes Choubani   general update
134
    		IF ptr_valid(*!dustem_plugin) THEN BEGIN
452c334e   Ilyes Choubani   Implementation Of...
135
    		for i=0L,n_tags(*!dustem_scope)-1 do begin
4fd64cbb   Ilyes Choubani   dustem_fit_sed_po...
136
            if total(strsplit(*(*!dustem_scope).(i),'+',/extract) eq 'ADD_EXT') then st.ext.ext_tot+=(*(*!dustem_plugin).(i))
452c334e   Ilyes Choubani   Implementation Of...
137
            endfor
759a527d   Ilyes Choubani   general update
138
            endif
452c334e   Ilyes Choubani   Implementation Of...
139
140
        	;------------------------------------------
        	
b5ccb706   Jean-Philippe Bernard   improved to fit p...
141
			dustem_ext = interpol(st.ext.ext_tot,st.ext.wav,(*!dustem_data.ext).wav) 
452c334e   Ilyes Choubani   Implementation Of...
142
            
b5ccb706   Jean-Philippe Bernard   improved to fit p...
143
144
145
146
147
148
	        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
149

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

b5ccb706   Jean-Philippe Bernard   improved to fit p...
153
154
155
156
157
158
				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
452c334e   Ilyes Choubani   Implementation Of...
159
        		
b5ccb706   Jean-Philippe Bernard   improved to fit p...
160
161
162
163
164
165
166
167
168
				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
			
759a527d   Ilyes Choubani   general update
169
			;dustem_plot_ext,st,/print_Rv ;commented this recently
b5ccb706   Jean-Philippe Bernard   improved to fit p...
170
171
172
173
174
175
176

		 	dustem_out = [ dustem_out, dustem_ext ]

		 	END

		'POLEXT': BEGIN

759a527d   Ilyes Choubani   general update
177
178
			
			;---------------the procedure within this block has not been updated for a vert long time
b5ccb706   Jean-Philippe Bernard   improved to fit p...
179
180
181
182
183
			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
759a527d   Ilyes Choubani   general update
184
185
186
			;--------------------------------------------------------------------------------------
			
			
b5ccb706   Jean-Philippe Bernard   improved to fit p...
187
188

			IF !run_lin then begin
759a527d   Ilyes Choubani   general update
189
                    ;compute_polext needs updating. It's been months. 
452c334e   Ilyes Choubani   Implementation Of...
190
                    dustem_polext = dustem_compute_polext(p_dim,st,dustem_qext,dustem_uext,Q_ext,U_ext)
b5ccb706   Jean-Philippe Bernard   improved to fit p...
191
	        		chi2_polext   = total(((*!dustem_data.polext).values-dustem_polext)^2/(*!dustem_data.polext).sigma^2)
18e4331f   Ilyes Choubani   general update (f...
192
193
194
195
;     				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
b5ccb706   Jean-Philippe Bernard   improved to fit p...
196
197
198
199
200
201
	        		;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)
4750086c   Ilyes Choubani   nouvelle philosph...
202
	        		
b5ccb706   Jean-Philippe Bernard   improved to fit p...
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
	        		; 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

452c334e   Ilyes Choubani   Implementation Of...
236
237
238
239
240
;  	       			; Polarization fraction in the UV-IR
;  	                win+=1
; 	                           
;  	       			dustem_plot_polar,st,p_dim,aligned=st_model.align.grains.aligned,yr=[0,0.15],/ysty,xr=[0.1,50],/xsty,win=win;multi=0,ps=filename
; ; 					
b5ccb706   Jean-Philippe Bernard   improved to fit p...
241
	   				; Polarisation curve (Serkowski)
452c334e   Ilyes Choubani   Implementation Of...
242
243
244
245
 					win+=1
 					;xr=[0.3,1e4]
 					xr=[1e-4,3.5]
 					yr=[1e-30,5e-24]
b7f936fc   Ilyes Choubani   to JP: initial fi...
246
247
248
249
250
 					
 					dustem_plot_polext,st,p_dim,dustem_polext,aligned=st_model.align.grains.aligned,win=win
 					
;  					dustem_plot_polar,st,p_dim,aligned=st_model.align.grains.aligned,/UV,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,/donotclose,multi=1
;  					dustem_plot_polar,st,p_dim,aligned=st_model.align.grains.aligned,/UV,yr=yr,/ysty,xr=xr,/xsty,ps=filename,win=win,multi=2
b5ccb706   Jean-Philippe Bernard   improved to fit p...
251
		
452c334e   Ilyes Choubani   Implementation Of...
252
253
254
;  					win+=1
;  					dustem_plot_fpol,win=win,xr=[10,1000],yr=[0,1.1]
;  					
b5ccb706   Jean-Philippe Bernard   improved to fit p...
255
256
	                                
	            ENDIF
36e8b879   Jean-Michel Glorian   update from deborah
257

b5ccb706   Jean-Philippe Bernard   improved to fit p...
258
259
260
				;dustem_plot_polar,st,/print_ratio,cont=cont
	                     
				dustem_out = [ dustem_out, dustem_polext ]
36e8b879   Jean-Michel Glorian   update from deborah
261

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

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

45da29c6   Ilyes Choubani   updating mpfit_run
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        'QSED': BEGIN 
        		    
		        toto = dustem_compute_stokes(p_dim,st,dustem_qsed,dustem_used,Q_spec,U_spec) 
		       
		        ;taking care of the color correction for spass ; SHOULD NOT LEAVE THIS ON
		        ;dustem_qsed(-1)=dustem_qsed(-2)
; 		        testspass1 = ((*!dustem_data.qsed).filt_names)(-1) eq 'SPASS1' 
;  			    if testspass1 then dustem_qsed(-1)=dustem_qsed(-2)
;  			    
			    chi2_qsed   =total(((*!dustem_data.qsed).values-dustem_qsed)^2/(*!dustem_data.qsed).sigma^2) 
			    n_qsed = n_elements((*!dustem_data.qsed).values)
			    rchi2_qsed  = chi2_qsed / n_qsed
	        	wrchi2_qsed  = rchi2_qsed * !fit_rchi2_weight.qsed 
	            if isa(!dustem_data.qsed) then dustem_out = [ dustem_out, dustem_qsed ] ; classic command
	            message,"chi2 QSED = "+strtrim(chi2_qsed,2)+"  rchi2 QSED = "+strtrim(rchi2_qsed,2),/continue;," weighted rchi2 POLSED=",wrchi2_polsed
	        	print,"chi2 per wl: ",((*!dustem_data.qsed).values-dustem_qsed)^2/(*!dustem_data.qsed).sigma^2
		        
		    END   
1355825c   Ilyes Choubani   General update
284

45da29c6   Ilyes Choubani   updating mpfit_run
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
        'USED': BEGIN
        		    ;THIS IMPLIES THAT QSED HAS BEEN RAN BEFORE 
        		    ;toto = dustem_compute_stokes(p_dim,st,dustem_qsed,dustem_used,Q_spec,U_spec) 
 			    chi2_used   =total(((*!dustem_data.used).values-dustem_used)^2/(*!dustem_data.used).sigma^2) 
 			    n_used = n_elements((*!dustem_data.used).values)
 			    rchi2_used  = chi2_used / n_used
  	        	wrchi2_used  = rchi2_used * !fit_rchi2_weight.used
 	            if isa(!dustem_data.used) then dustem_out = [ dustem_out, dustem_used] ; classic command
 	            message,"chi2 USED = "+strtrim(chi2_used,2)+"  rchi2 USED = "+strtrim(rchi2_used,2),/continue;," weighted rchi2 POLSED=",wrchi2_polsed
  	        	print,"chi2 per wl: ",((*!dustem_data.used).values-dustem_used)^2/(*!dustem_data.used).sigma^2
                
  		END
  		
        'POLSED': BEGIN
		    
		          ;PLUGIN IS ADDED INSIDE DUSTEM_COMPUTE_SED
 			
             	  IF !run_pol and !run_lin THEN BEGIN
  
            	       	dustem_polsed = dustem_compute_polsed(p_dim,st);dustem_compute_polsed(p_dim,st,cont=cont,freefree=freefree,synchrotron=synchrotron)
 	        
         		  ENDIF
		END
    	
    	
    	'POLFRAC': BEGIN 
      		
1355825c   Ilyes Choubani   General update
312
      		if !run_lin then begin
45da29c6   Ilyes Choubani   updating mpfit_run
313
      		
1355825c   Ilyes Choubani   General update
314
                  if ~isa(dustem_sed) then dustem_sed = dustem_compute_sed(p_dim,st) 
45da29c6   Ilyes Choubani   updating mpfit_run
315
                  if ~isa(dustem_polsed) then dustem_polsed = dustem_compute_polsed(p_dim,st) ; this quantity hasn't been computed yet that is why this line is present here.      
1355825c   Ilyes Choubani   General update
316
317
                  ind_filt=where((*!dustem_data.polfrac).filt_names NE 'SPECTRUM',count_filt) ;locating the filters of polfrac=smallp in SED xcat        
                  ind_fspec=where((*!dustem_data.polfrac).filt_names EQ 'SPECTRUM',count_fspec) ;locating the spectrum points in polfrac data. Later testing will have to be on the wavelengths    
3ff9cac3   Ilyes Choubani   general update
318
;                 
1355825c   Ilyes Choubani   General update
319
320
321
322
323
324
325
326
327
328
329
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
;                 ;dustem_sed and dustem_polsed might not have the same length thus this block.
;                 
                  If n_elements(dustem_sed) ne n_elements(dustem_polsed) then begin                            
                      if n_elements(dustem_sed) gt n_elements(dustem_polsed) then begin 
                          dustem_polsed_x = dustem_polsed
                          dustem_sed_x = dustem_polsed ;meaning dustem_sed needs to be modified
                          ;matching the filter data points
                          IF count_filt ne 0 then begin
                              for i=0L,count_filt-1 do begin    
                                  j=where((*!dustem_data.sed).filt_names EQ (*!dustem_data.polfrac).filt_names(ind_filt(i)),testfilt)
                                  if testfilt ne 0 then dustem_sed_x(ind_filt(i)) = dustem_sed(j(0))                                                 
                              endfor                                        
                          endif
                          ;matching the spectrum data points    
                          IF count_fspec ne 0 then begin
                              for i=0L,count_fspec-1 do begin
                                  j=where((*!dustem_data.sed).wav EQ (*!dustem_data.polfrac).wav(ind_fspec(i)),testspec)
                                  if testspec ne 0 then dustem_sed_x((ind_fspec(i))) = dustem_sed(j(0))                                    
                             endfor
                        endif     
                    endif ELSE begin
                        
                        dustem_polsed_x = dustem_sed
                        dustem_sed_x = dustem_sed ;meaning dustem_polsed needs to be modified
                        ;matching the filter data points
                        IF count_filt ne 0 then begin
                            for i=0L,count_filt-1 do begin    
                                j=where((*!dustem_data.sed).filt_names EQ (*!dustem_data.polfrac).filt_names(ind_filt(i)),testfilt)
                                if testfilt ne 0 then dustem_polsed_x(ind_filt(i)) = dustem_polsed(j(0))                                                 
                            endfor                                        
                        endif
                        ;matching the spectrum data points    
                        IF count_fspec ne 0 then begin
                            for i=0L,count_fspec-1 do begin
                                j=where((*!dustem_data.sed).wav EQ (*!dustem_data.polfrac).wav(ind_fspec(i)),testspec)
                                if testspec ne 0 then dustem_polsed_x((ind_fspec(i))) = dustem_polsed(j(0))                                    
                            endfor
                        endif
                    
                    ENDELSE
                endif ELSE BEGIN
                    dustem_polsed_x = dustem_polsed 
                    dustem_sed_x = dustem_sed
                ENDELSE
                
                dustem_polfrac = dustem_polsed_x/dustem_sed_x 
                	       
            ENDIF
45da29c6   Ilyes Choubani   updating mpfit_run
367
            
1355825c   Ilyes Choubani   General update
368
369
            ;stop
 			END
452c334e   Ilyes Choubani   Implementation Of...
370

452c334e   Ilyes Choubani   Implementation Of...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
 		
  		'QEXT': BEGIN ; Q - EXTINCTION CROSS SECTION
                   
 			    chi2_qext   =total(((*!dustem_data.qext).values-dustem_qext)^2/(*!dustem_data.qext).sigma^2) 
   			    n_qext = n_elements((*!dustem_data.qext).values)
   			    rchi2_qext  = chi2_qext / n_qext
  	        	wrchi2_qext  = rchi2_qext * !fit_rchi2_weight.qext
   	            dustem_out = [ dustem_out, dustem_qext] ; classic command
   	            message,"chi2 QEXT = "+strtrim(chi2_qext,2)+"  rchi2 QEXT = "+strtrim(rchi2_qext,2),/continue;," weighted rchi2 POLSED=",wrchi2_polsed
  	        	print,"chi2 per wl: ",((*!dustem_data.qext).values-dustem_qext)^2/(*!dustem_data.qext).sigma^2
                if !dustem_show_plot ne 0 then begin
	            
	            win+=1
                window  ,win ,title='DUSTEM WRAP (QEXT)'	                 
                titqex='Q-polarized Cross-section'
                ytitqex=textoidl('\sigma_{Q}/N_H (cm^2/H)')
                xtit=textoidl('\lambda^{-1} (\mum^{-1})')
    	        ;xr=[0.3,1e4]
    	        xr=[1e-4,3.5]
    	          
                yr=[1e-34,5e-24]
                indqex=where(Q_ext lt 0, countqex)
                ylog=1
                if countqex ne 0 then begin
                ylog=0
                yr=[-9e-27,5e-27]  
                endif 
                normpol = st.polext.ext_tot * 0. + 1.d21  
                cgplot,1/st.polext.wav,Q_ext,xtit='',ytit=ytitqex,tit=titqex,ylog=ylog,/xlog,xr=xr,yr=yr,/ys,/xs,position=[0.17,0.35,0.93,0.93],xtickformat='(A1)',color='black'
                cgoplot,1/st.polext.wav,Q_ext/normpol,color='black'
                cgoplot,1/(*!dustem_data.qext).wav,(*!dustem_data.qext).values/normpol,color='Indian Red',psym=16,symsize=1,thick=2
                err_bar,1/(*!dustem_data.qext).wav,(*!dustem_data.qext).values/normpol,yrms=3.*((*!dustem_data.qext).sigma)/2/normpol,color=cgColor('Indian Red')
                cgoplot,1/(*!dustem_data.qext).wav,dustem_qext/normpol,psym=6,color='red',symsize=2
                
                
                
                cgplot,1/st.polsed.wav,Q_ext/Q_ext,xtit=xtit,ytit='Normalized',tit='',/xlog,xr=xr,/ys,/xs,yr=[0,2],ylog=0,position=[0.17,0.14,0.93,0.35],/noerase,yticks=2,ymino=5,xticklen=0.1    
                cgoplot,1/st.polsed.wav,Q_ext/Q_ext,color='black'
                cgoplot,1/(*!dustem_data.qext).wav,(*!dustem_data.qext).values/dustem_qext,color='Indian Red',psym=16,symsize=1,thick=2
                err_bar,1/(*!dustem_data.qext).wav,(*!dustem_data.qext).values/dustem_qext,yrms=3.*((*!dustem_data.qext).sigma)/2.,color=cgColor('Indian Red')
                    
            	            endif                  
     	           
  		END
;  		
  		'UEXT': BEGIN
  
 			    
 			    chi2_uext   =total(((*!dustem_data.uext).values-dustem_uext)^2/(*!dustem_data.uext).sigma^2) 
   			    n_uext = n_elements((*!dustem_data.uext).values)
   			    rchi2_uext  = chi2_uext / n_uext
  	        	wrchi2_uext  = rchi2_uext * !fit_rchi2_weight.uext
   	            dustem_out = [ dustem_out, dustem_uext] ; classic command
   	            message,"chi2 UEXT = "+strtrim(chi2_uext,2)+"  rchi2 UEXT = "+strtrim(rchi2_uext,2),/continue;," weighted rchi2 POLSED=",wrchi2_polsed
  	        	print,"chi2 per wl: ",((*!dustem_data.uext).values-dustem_uext)^2/(*!dustem_data.uext).sigma^2
                if !dustem_show_plot ne 0 then begin
	            
	            win+=1
                window  ,win ,title='DUSTEM WRAP (UEXT)'	                 
                tituex='U-polarized Cross-section'
                ytituex=textoidl('\sigma_{U}/N_H (cm^2/H)')
                xtit=textoidl('\lambda^{-1} (\mum^{-1})')
    	        ;xr=[0.3,1e4]
    	        xr=[1e-4,3.5]
    	          
                yr=[1e-34,5e-24]
                induex=where(U_ext lt 0, countuex)
                ylog=1
                if countuex ne 0 then begin
                ylog=0
                yr=[-9e-27,5e-27]  
                endif 
                normpol = st.polext.ext_tot * 0. + 1.d21  
                cgplot,1/st.polext.wav,U_ext,xtit='',ytit=ytituex,tit=tituex,ylog=ylog,/xlog,xr=xr,yr=yr,/ys,/xs,position=[0.17,0.35,0.93,0.93],xtickformat='(A1)',color='black'
                cgoplot,1/st.polext.wav,U_ext/normpol,color='black'
                cgoplot,1/(*!dustem_data.uext).wav,(*!dustem_data.uext).values/normpol,color='Steel Blue',psym=16,symsize=1,thick=2
                err_bar,1/(*!dustem_data.uext).wav,(*!dustem_data.uext).values/normpol,yrms=3.*((*!dustem_data.uext).sigma)/2/normpol,color=cgColor('Steel Blue')
                cgoplot,1/(*!dustem_data.uext).wav,dustem_uext/normpol,psym=6,color='red',symsize=2
                
                
                
                cgplot,1/st.polsed.wav,U_ext/U_ext,xtit=xtit,ytit='Normalized',tit='',/xlog,xr=xr,/ys,/xs,yr=[0,2],ylog=0,position=[0.17,0.14,0.93,0.35],/noerase,yticks=2,ymino=5,xticklen=0.1    
                cgoplot,1/st.polsed.wav,U_ext/U_ext,color='black'
                cgoplot,1/(*!dustem_data.uext).wav,(*!dustem_data.uext).values/dustem_uext,color='Steel Blue',psym=16,symsize=1,thick=2
                err_bar,1/(*!dustem_data.uext).wav,(*!dustem_data.uext).values/dustem_uext,yrms=3.*((*!dustem_data.uext).sigma)/2/normpol,color=cgColor('Steel Blue')
                    
            	            endif                  
     	        
                
                  
 	           
  		END
 		
18e4331f   Ilyes Choubani   general update (f...
464
	    ELSE :;IF tagnames(ind_data_tag(i_tag)) NE 'POLFRAC' THEN stop 
427f1205   Jean-Michel Glorian   version 4.2 merged
465
466
    ENDCASE

b5ccb706   Jean-Philippe Bernard   improved to fit p...
467
ENDFOR
45da29c6   Ilyes Choubani   updating mpfit_run
468
dustemwrap_plot,p_dim,st,dustem_sed,dustem_qsed,dustem_used,dustem_polsed,dustem_polfrac,_extra=_extra
427f1205   Jean-Michel Glorian   version 4.2 merged
469

36e8b879   Jean-Michel Glorian   update from deborah
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
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
1355825c   Ilyes Choubani   General update
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505

    if !fit_rchi2_weight.qsed ne 0 then begin
    	total_n += n_qsed
    	total_chi2 += chi2_qsed
    	total_rchi2 += rchi2_qsed
    endif
    if !fit_rchi2_weight.used ne 0 then begin
    	total_n += n_used
    	total_chi2 += chi2_used
    	total_rchi2 += rchi2_used
    endif
    if !fit_rchi2_weight.qext ne 0 then begin
    	total_n += n_qext
    	total_chi2 += chi2_qext
    	total_rchi2 += rchi2_qext
    endif
    if !fit_rchi2_weight.uext ne 0 then begin
    	total_n += n_uext
    	total_chi2 += chi2_uext
    	total_rchi2 += rchi2_uext
    endif
452c334e   Ilyes Choubani   Implementation Of...
506
507
508

endif

36e8b879   Jean-Michel Glorian   update from deborah
509
510
511
512

total_wchi2 = chi2_ext * !fit_rchi2_weight.ext $
            + chi2_sed * !fit_rchi2_weight.sed 
if !run_pol then begin
18e4331f   Ilyes Choubani   general update (f...
513
	total_wchi2 	+=  $;chi2_polext  * !fit_rchi2_weight.polext $
3ff9cac3   Ilyes Choubani   general update
514
515
;              		 + chi2_polsed  * !fit_rchi2_weight.polsed $
;              		 + chi2_polfrac * !fit_rchi2_weight.polfrac $
452c334e   Ilyes Choubani   Implementation Of...
516
517
518
519
             		 + chi2_qsed * !fit_rchi2_weight.qsed $
             		 + chi2_used * !fit_rchi2_weight.used $
             		 + chi2_qext * !fit_rchi2_weight.qext $
             		 + chi2_uext * !fit_rchi2_weight.uext  
36e8b879   Jean-Michel Glorian   update from deborah
520
521
endif

9ccf7615   Jean-Philippe Bernard   modified to fit u...
522
message,'======================================================================',/continue
36e8b879   Jean-Michel Glorian   update from deborah
523
524
525
print,"Total weighted chi2 driving mpfitfun = ", total_wchi2
print,"Nb of data points = ", total_n
print,"DOF = ", DOF
18e4331f   Ilyes Choubani   general update (f...
526
527
528
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 $ 
36e8b879   Jean-Michel Glorian   update from deborah
529
530
531
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)
452c334e   Ilyes Choubani   Implementation Of...
532
print,"Unweighted mean reduced chi2 = ", total_rchi2/4*total_n/(total_n-DOF);total_rchi2/4*total_n/(total_n-DOF) original
9ccf7615   Jean-Philippe Bernard   modified to fit u...
533
message,'======================================================================',/continue
36e8b879   Jean-Michel Glorian   update from deborah
534
535
536
537
538
539
540
541
542

;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...
543
;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
544
545
546
547

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

9ccf7615   Jean-Philippe Bernard   modified to fit u...
548
549
550
551
552
553
554
;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
555
556
557
558
559
560

the_end:

RETURN,dustem_out

END