Blame view

src/idl/dustem_mpfit_data.pro 5.32 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

;+
; NAME:
;    dustem_mpfit_data
362c0f7b   Annie Hughes   brought back the ...
6
;
427f1205   Jean-Michel Glorian   version 4.2 merged
7
8
9
; PURPOSE:
;    call MPFIT to fit observational datas using ponderated reduced chi2
;    between extinction, emission, and polarisation as defined in !fit_rchi2_weight
362c0f7b   Annie Hughes   brought back the ...
10
;
427f1205   Jean-Michel Glorian   version 4.2 merged
11
12
; CATEGORY:
;    Dustem
362c0f7b   Annie Hughes   brought back the ...
13
;
427f1205   Jean-Michel Glorian   version 4.2 merged
14
15
; CALLING SEQUENCE:
;    res=dustem_mpfit_data(tol=tol,Nitermax=Nitermax,gtol=gtol) 
362c0f7b   Annie Hughes   brought back the ...
16
;
427f1205   Jean-Michel Glorian   version 4.2 merged
17
18
; INPUTS:
;    None
362c0f7b   Annie Hughes   brought back the ...
19
;
427f1205   Jean-Michel Glorian   version 4.2 merged
20
; OPTIONAL INPUT PARAMETERS:
362c0f7b   Annie Hughes   brought back the ...
21
;
427f1205   Jean-Michel Glorian   version 4.2 merged
22
23
; OUTPUTS:
;    None
362c0f7b   Annie Hughes   brought back the ...
24
;
427f1205   Jean-Michel Glorian   version 4.2 merged
25
; OPTIONAL OUTPUT PARAMETERS:
362c0f7b   Annie Hughes   brought back the ...
26
;
427f1205   Jean-Michel Glorian   version 4.2 merged
27
28
; ACCEPTED KEY-WORDS:
;    help      = If set, print this help
362c0f7b   Annie Hughes   brought back the ...
29
;
427f1205   Jean-Michel Glorian   version 4.2 merged
30
31
; COMMON BLOCKS:
;    None
362c0f7b   Annie Hughes   brought back the ...
32
;
427f1205   Jean-Michel Glorian   version 4.2 merged
33
34
; SIDE EFFECTS:
;    None
362c0f7b   Annie Hughes   brought back the ...
35
;
427f1205   Jean-Michel Glorian   version 4.2 merged
36
; RESTRICTIONS:
4c644daa   Annie Hughes   fixed specificati...
37
38
;    The DustEM fortran code must be installed
;    The DustEMWrap IDL code must be installed
362c0f7b   Annie Hughes   brought back the ...
39
;
427f1205   Jean-Michel Glorian   version 4.2 merged
40
41
; PROCEDURE:
;    None
362c0f7b   Annie Hughes   brought back the ...
42
;
427f1205   Jean-Michel Glorian   version 4.2 merged
43
44
45
46
; EXAMPLES
;
; MODIFICATION HISTORY:
;    Written by V. Guillet (2012)
4c644daa   Annie Hughes   fixed specificati...
47
48
;    Evolution details on the DustEMWrap gitlab.
;    See http://dustemwrap.irap.omp.eu/ for FAQ and help.  
427f1205   Jean-Michel Glorian   version 4.2 merged
49
50
;-

362c0f7b   Annie Hughes   brought back the ...
51
52
53
54
IF keyword_set(help) THEN BEGIN
  doc_library,'dustem_mpfit_data'
  goto,the_end
ENDIF
427f1205   Jean-Michel Glorian   version 4.2 merged
55
56
57
58
59
60
61
62

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...
63
;functargs={plot_it:1}
427f1205   Jean-Michel Glorian   version 4.2 merged
64
65
66
67
68
69

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
8168169d   Annie Hughes   turned off plotti...
70
previous_show_plot = !dustem_show_plot
427f1205   Jean-Michel Glorian   version 4.2 merged
71

b74c4452   Ilyes Choubani   Treating extincti...
72
; Build array concatenating observational data
1355825c   Ilyes Choubani   General update
73
74
75
76

if !run_pol then begin
    
    ind_data_tag=dustem_data_tag(count=count_data_tag)
0068116a   Ilyes Choubani   General update + ...
77
    tagnames=tag_names(*!dustem_data)
96a72c8b   Ilyes Choubani   seperating xrange...
78
    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
79
80
81
    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 ...
82
        ;we also only need the points that match.
1355825c   Ilyes Choubani   General update
83
84
85
86
87
        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
88
89

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

	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)
d0f68bb6   Jean-Philippe Bernard   replaced system v...
107
108
			if (*!fit_rchi2_weight).(i) gt 0 then begin
				weight = sqrt(1./(*!fit_rchi2_weight).(i) ) 
427f1205   Jean-Michel Glorian   version 4.2 merged
109
110
111
112
113
114
115
116
			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 + ...
117
		instruc += ', ((*(*!dustem_data).(' + strtrim(ind_data_tag(i),2) + ')).'+fields(j)+')*'+strtrim(weight,2)
427f1205   Jean-Michel Glorian   version 4.2 merged
118
119
120
121
122
		;print,"INSTRUC",instruc

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

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

5eebdedb   Jean-Philippe Bernard   changed plugin st...
127
128
;IF tag_exist(_extra,'show_plot') THEN !dustem_show_plot = _extra.show_plot
;IF ~tag_exist(_extra,'show_plot') THEN !dustem_show_plot = 0
5b7cf393   Annie Hughes   removed stop
129

9331f1cd   Jean-Philippe Bernard   introduced use of...
130
nocatch=!dustem_nocatch
427f1205   Jean-Michel Glorian   version 4.2 merged
131
132
133
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...
134
                 maxiter=use_Nitermax,gtol=gtol,errmsg=errmsg,functargs=_extra,nocatch=nocatch)
9eb53891   Jean-Philippe Bernard   modified call to ...
135
136

;stop                 
2c22f184   Jean-Philippe Bernard   fixed bug when so...
137
138
;!dustem_iter.act=niter_completed

427f1205   Jean-Michel Glorian   version 4.2 merged
139
140
141
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 ...
142
!dustem_end = 1
427f1205   Jean-Michel Glorian   version 4.2 merged
143

427f1205   Jean-Michel Glorian   version 4.2 merged
144
;PRINT RESULTS
9ce86925   Annie Hughes   improved the fish
145
146
147
148
149
150
151
152
print,''
print,''
print,'<o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><'
print,'<o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><'
print,''
print,'FINISHED DUSTEMWRAP FITTING...'
print,''
print,'The final values of the free parameters (pd) are : '
7cc6c3de   Jean-Philippe Bernard   added and removed...
153
print, p_dim
9ce86925   Annie Hughes   improved the fish
154
155
156
157
158
print,''
print,'<o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><'
print,'<o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><  <o)))><'
print,''
print,''
4da8e20e   Ilyes Choubani   other procs that ...
159
160


8168169d   Annie Hughes   turned off plotti...
161
162
163
164
165
166
167
168
; we assume that the user knows if they want plots or not
IF !dustem_show_plot gt 0 then begin
   IF !dustem_noobj THEN BEGIN
      dustemwrap_plot_noobj,p_dim,st=stp,_extra=_extra
   ENDIF ELSE BEGIN
      dustemwrap_plot,p_dim,st=stp,_extra=_extra
   ENDELSE
end
7cc6c3de   Jean-Philippe Bernard   added and removed...
169

8168169d   Annie Hughes   turned off plotti...
170
!dustem_show_plot = previous_show_plot
2c22f184   Jean-Philippe Bernard   fixed bug when so...
171
the_end:
8168169d   Annie Hughes   turned off plotti...
172

427f1205   Jean-Michel Glorian   version 4.2 merged
173
174
175
RETURN, p_dim

END