Blame view

src/idl/dustem_mpfit_data.pro 5.16 KB
9ccf7615   Jean-Philippe Bernard   modified to fit u...
1
FUNCTION dustem_mpfit_data,tol=tol,Nitermax=Nitermax,gtol=gtol,function_name=function_name,xtol=xtol,_extra=_extra
427f1205   Jean-Michel Glorian   version 4.2 merged
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

;+
; NAME:
;    dustem_mpfit_data
; PURPOSE:
;    call MPFIT to fit observational datas using ponderated reduced chi2
;    between extinction, emission, and polarisation as defined in !fit_rchi2_weight
; CATEGORY:
;    Dustem
; CALLING SEQUENCE:
;    res=dustem_mpfit_data(tol=tol,Nitermax=Nitermax,gtol=gtol) 
; INPUTS:
;    None
; OPTIONAL INPUT PARAMETERS:
; OUTPUTS:
;    None
; OPTIONAL OUTPUT PARAMETERS:
; ACCEPTED KEY-WORDS:
;    help      = If set, print this help
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    None
; RESTRICTIONS:
;    The dustem idl wrapper must be installed
; PROCEDURE:
;    None
; EXAMPLES
;
; MODIFICATION HISTORY:
;    Written by V. Guillet (2012)
;    see evolution details on the dustem cvs maintained at CESR
;    Contact J.-Ph. Bernard (Jean-Philippe.Bernard@cesr.fr) in case of problems.
;-

9eb53891   Jean-Philippe Bernard   modified call to ...
37
38
;stop

427f1205   Jean-Michel Glorian   version 4.2 merged
39
40
41
42
43
44
45
46
47
48
49
50
51
52
IF keyword_set(mode) THEN BEGIN
  use_mode=strupcase(mode)
ENDIF ELSE BEGIN
  use_mode='COMPIEGNE_ETAL2010'    ;Default is last dustem model
ENDELSE


disp_str='====================================================='
IF not keyword_set(function_name) THEN BEGIN
  func_name='dustem_mpfit_run'
ENDIF ELSE BEGIN
  func_name=function_name
ENDELSE

9ccf7615   Jean-Philippe Bernard   modified to fit u...
53
;functargs={plot_it:1}
427f1205   Jean-Michel Glorian   version 4.2 merged
54
55
56
57
58
59
60

use_tol=1.e-14   ;Default tolerance value
use_Nitermax=3 ;Default maximum number of iterations

IF keyword_set(tol) THEN use_tol=tol
IF keyword_set(Nitermax) THEN use_Nitermax=Nitermax

b74c4452   Ilyes Choubani   Treating extincti...
61
; Build array concatenating observational data
1355825c   Ilyes Choubani   General update
62
63
64
65

if !run_pol then begin
    
    ind_data_tag=dustem_data_tag(count=count_data_tag)
0068116a   Ilyes Choubani   General update + ...
66
    tagnames=tag_names(*!dustem_data)
96a72c8b   Ilyes Choubani   seperating xrange...
67
    testpol = tagnames EQ 'POLSED' or tagnames EQ 'POLEXT' or tagnames EQ 'POLFRAC' or tagnames EQ 'PSI_EM' or tagnames EQ 'PSI_EXT' or tagnames EQ 'FPOLEXT'
1355825c   Ilyes Choubani   General update
68
69
70
    data_ind=where(~testpol,ctestpol)
    if ctestpol ne 0 then begin
        match,ind_data_tag,data_ind,ind1,ind2 ;not using match2 because when matching indices, if the first is 0 it doesn't count which is wrong.
4da8e20e   Ilyes Choubani   other procs that ...
71
        ;we also only need the points that match.
1355825c   Ilyes Choubani   General update
72
73
74
75
76
        ind_data_tag = ind_data_tag[ind1]
        count_data_tag = n_elements(ind_data_tag) 
    endif
endif else ind_data_tag=dustem_data_tag(count=count_data_tag)

427f1205   Jean-Michel Glorian   version 4.2 merged
77
78

fields=['wav','values','sigma']
b5ccb706   Jean-Philippe Bernard   improved to fit p...
79
FOR j=0,n_elements(fields)-1 DO BEGIN
427f1205   Jean-Michel Glorian   version 4.2 merged
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

	instruc = fields(j)+' = ([0'	; we add 0 first to allow for ',' after it

	for i=0, count_data_tag-1 do begin $	; Add each data set

		; Adapt sigma to the reduced chi2 ponderation defined in !fit_rchi2_weight between sed, ext, polext and polsed
		; Total_reduced_chi2_driving_mpfit  = sum_i ( chi2_i * (weight_i/n_i) )
		;	n 	: the total number of observational variables in the data
		;	n_i 	: the number of obs. variable in data i 
		;	w_i	: the weight for data i from !fit_rchi2_weight for the ponderation of reduced chi2
		; ex : 	w_sed = 0.8, w_ext = 0.2 , i.e. rchi2_sed counts 4 times as much as rchi2_ext
		;	sed : 314 pts,	ext : 52 pts, i.e. 6 times much more constraints in sed than in ext
		; sigma_sed *= sqrt(314./366/0.8) = 1.03
		; sigma_ext *= sqrt(52./366./0.2) = 0.84
		; By increasing sigma by a factor w, we diminish the weight of the corresponding point by a factor w^2
		if count_data_tag gt 1 and j eq 2 then begin ; sigma ponderation = sqrt(ni/wi)
			if !fit_rchi2_weight.(i) gt 0 then begin
				weight = sqrt(1./!fit_rchi2_weight.(i) ) 
			endif else begin
				weight = 1.d99
			endelse
		endif else begin
			weight = 1.
		endelse
		;print,"WEIGHT",i,j,weight,n_elements((*!dustem_data.(i)).sigma),"/",n_elements(wav)

0068116a   Ilyes Choubani   General update + ...
106
		instruc += ', ((*(*!dustem_data).(' + strtrim(ind_data_tag(i),2) + ')).'+fields(j)+')*'+strtrim(weight,2)
427f1205   Jean-Michel Glorian   version 4.2 merged
107
108
109
110
111
		;print,"INSTRUC",instruc

	endfor
	instruc += '])(1:*)'	; from 1 to exclude first 0
	;print,'-> INSTRUC: ',instruc
6730c3f8   Jean-Philippe Bernard   modified to be co...
112
    toto=execute(instruc)	; Variables wav, values & sigma are created
36e8b879   Jean-Michel Glorian   update from deborah
113

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

9eb53891   Jean-Philippe Bernard   modified call to ...
116
117
;stop

9331f1cd   Jean-Philippe Bernard   introduced use of...
118
nocatch=!dustem_nocatch
427f1205   Jean-Michel Glorian   version 4.2 merged
119
120
121
p_min = dustem_mpfitfun(func_name,wav,values,sigma, $
                 parinfo=(*!dustem_parinfo), perror=perror, yfit=yfit, $
                 bestnorm=bestnorm, dof=dof,quiet=quiet, status=status, xtol=xtol, niter=niter_completed, ftol=use_tol, $
9331f1cd   Jean-Philippe Bernard   introduced use of...
122
                 maxiter=use_Nitermax,gtol=gtol,errmsg=errmsg,functargs=_extra,nocatch=nocatch)
9eb53891   Jean-Philippe Bernard   modified call to ...
123
124

;stop                 
427f1205   Jean-Michel Glorian   version 4.2 merged
125
126
127
message,errmsg,/info
message,'mpfitfun Status='+strtrim(status,2),/info
p_dim=p_min*(*(*!dustem_fit).param_init_values)
4da8e20e   Ilyes Choubani   other procs that ...
128
!dustem_end = 1
427f1205   Jean-Michel Glorian   version 4.2 merged
129

427f1205   Jean-Michel Glorian   version 4.2 merged
130
131
;PRINT RESULTS
;print,'<°)))><  <°)))><  <°)))><  <°)))><  <°)))><  <°)))><  <°)))><  <°)))><'
7cc6c3de   Jean-Philippe Bernard   added and removed...
132
133
message, 'Final parameters best values : ',/continue
print, p_dim
4da8e20e   Ilyes Choubani   other procs that ...
134
135
136


;Readying this part to be erased.
427f1205   Jean-Michel Glorian   version 4.2 merged
137
;=== store best fit values in syst variables
427f1205   Jean-Michel Glorian   version 4.2 merged
138

4da8e20e   Ilyes Choubani   other procs that ...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
;THESE TWO are already set in mpfit_run if correctly recalled.

; (*!dustem_fit).chi2=bestnorm
; (*!dustem_fit).rchi2=bestnorm/dof

;SADLY these are only valid after the fitting procedure has ended. Therefore, they will be placed in dustem_mpfit_run. 
;It seems to be an adequate level. 


; p_er_dim=perror*(*(*!dustem_fit).param_init_values)
; (*!dustem_fit).current_param_values=ptr_new(p_dim)
; (*!dustem_fit).current_param_errors=ptr_new(p_er_dim)



f11d3d62   Jean-Philippe Bernard   added a comment a...
154
;JPB: What if _extra.xr does not exist then ?
96a72c8b   Ilyes Choubani   seperating xrange...
155
dustemwrap_plot,p_dim,stp,xr=_extra.xr,/xstyle,yr=_extra.yr,/ysty,/ylog,/xlog,title=_extra.title
7cc6c3de   Jean-Philippe Bernard   added and removed...
156

427f1205   Jean-Michel Glorian   version 4.2 merged
157
158
159
RETURN, p_dim

END