Blame view

src/idl/dustem_brute_force_fit.pro 6.25 KB
f8c3f95f   Jean-Philippe Bernard   added limits and ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FUNCTION dustem_brute_force_fit,sed $
							   ,table_name $
							   ,filters $
							   ,normalize=normalize $
							   ,params_hit=params_hit $
							   ,params_uncertainties=params_uncertainties $
							   ,params_min=params_min $
							   ,params_max=params_max $
							   ,fact=fact $
							   ,chi2=chi2 $
							   ,rchi2=rchi2 $
							   ,best_sed=best_sed $
							   ,best_grid_sed=best_grid_sed $
							   ,show_sed=show_sed $
							   ,nostop=nostop $
							   ,reset=reset
6ff7d98f   Jean-Philippe Bernard   First commit
17

dbf82b16   Jean-Philippe Bernard   chaaged the plot
18
19
20
21
22
23
24
25
;+
; NAME:
;    dustem_brute_force_fit
; PURPOSE:
;    does brut force fit from a pre-computed model grid
; CATEGORY:
;    DustEM
; CALLING SEQUENCE:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
26
;    res=dustem_brute_force_fit(sed,table_name,filters[,/normalize][,fact=][,chi2=][,rchi2=][rchi2][best_sed=][,/show_sed][,/nostop][,/reset])
dbf82b16   Jean-Philippe Bernard   chaaged the plot
27
; INPUTS:
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
28
;    sed  = sed to be fit. Should contain all filters provided to the routine trhough input variable filter
f8c3f95f   Jean-Philippe Bernard   added limits and ...
29
30
;    table_name = name of the fits file to be used as a grid (see dustem_make_sed_table.pro for creation)
;    filters    = filters to be used in the grid table (only used if !dustem_grid does not exist or if /reset)
dbf82b16   Jean-Philippe Bernard   chaaged the plot
31
; OPTIONAL INPUT PARAMETERS:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
32
;    None
dbf82b16   Jean-Philippe Bernard   chaaged the plot
33
; OUTPUTS:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
34
;    res = best values of the free parameters. Not that linear parameters to the sed should be multiplied by fact.
dbf82b16   Jean-Philippe Bernard   chaaged the plot
35
; OPTIONAL OUTPUT PARAMETERS:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
36
;    fact = best scaling factor. Such that best_sed*fact is for NH=1.e20 H/cm2 (as in the grid). IF sed is already normalized to NH=1e20 H/cm2, no need to set /normalize
b4dbd43c   Jean-Philippe Bernard   chaaged the plot
37
38
;    chi2 = chi2 of the best fit
;    rchi2 = reduced chi2 of the best fit
f8c3f95f   Jean-Philippe Bernard   added limits and ...
39
40
41
42
43
44
;    best_grid_sed = best SED found in the grid
;    best_sed = best fit SED matching input SED (such that best_sed=best_grid_sed*fact)
;    params_hit= 1 if parameter hits the top, -1 if hits the bottom, 0 otherwise
;    params_uncertainties = 90% confidence level uncertainty on parameters
;    params_min = 90% lower limit of parameters
;    params_max = 90% upper limit of parameters
dbf82b16   Jean-Philippe Bernard   chaaged the plot
45
46
; ACCEPTED KEY-WORDS:
;    help                  = if set, print this help
f8c3f95f   Jean-Philippe Bernard   added limits and ...
47
48
49
;    normalize = if set, the best match is done based on the shape of the SEDs only, instead of their absolute values. fact will be computed.
;    show_sed = if set, plot the seds
;    nostop   = if set, do not stop
dbf82b16   Jean-Philippe Bernard   chaaged the plot
50
51
52
53
54
55
56
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    None
; RESTRICTIONS:
;    The DustEMWrap IDL code must be installed
; PROCEDURE:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
57
;    Scans the grid SEDs to find the best fit (lowest chi2)
dbf82b16   Jean-Philippe Bernard   chaaged the plot
58
; EXAMPLES
f8c3f95f   Jean-Philippe Bernard   added limits and ...
59
;    
dbf82b16   Jean-Philippe Bernard   chaaged the plot
60
; MODIFICATION HISTORY:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
61
;    Written by JPB Jan 2024
dbf82b16   Jean-Philippe Bernard   chaaged the plot
62
63
64
65
66
;    Evolution details on the DustEMWrap gitlab.
;    See http://dustemwrap.irap.omp.eu/ for FAQ and help.  
;-

IF keyword_set(help) THEN BEGIN
f8c3f95f   Jean-Philippe Bernard   added limits and ...
67
68
  doc_library,'dustem_brute_force_fit'
  params=0.
dbf82b16   Jean-Philippe Bernard   chaaged the plot
69
70
71
  goto,the_end
ENDIF

6ff7d98f   Jean-Philippe Bernard   First commit
72
;stop
88f84272   Jean-Philippe Bernard   changed
73
;sed must be normalized to NH=1e20 H/cm2
6ff7d98f   Jean-Philippe Bernard   First commit
74

f8c3f95f   Jean-Philippe Bernard   added limits and ...
75
;==== test for the existence of a loaded grid and initialize if needed.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
76
;==== only filters contained in the filters variable are kept in the grid, and the sum of grid seds are recomputed accordingly
6ff7d98f   Jean-Philippe Bernard   First commit
77
defsysv,'!dustem_grid',0,exist=exist
f8c3f95f   Jean-Philippe Bernard   added limits and ...
78
IF exist EQ 0 or keyword_set(reset)THEN BEGIN
6ff7d98f   Jean-Philippe Bernard   First commit
79
80
81
    dustem_define_grid,table_name,filters
ENDIF

6ff7d98f   Jean-Philippe Bernard   First commit
82
83
84
85
seds=!dustem_grid.seds
Nseds=!dustem_grid.Nsed
Nfilters=!dustem_grid.Nfilters
Nparams=!dustem_grid.Nparams
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
86
87
88
89
90
table_params=!dustem_grid.params
table_pmins=!dustem_grid.pmin_values
table_pmaxs=!dustem_grid.pmax_values
;dustem_grid={Nsed:Nsed,Nfilters:Nfilters,Nparams:Ntparams,filters:filters,params:table_params,seds:st,pmin_values:table_pmins,pmax_values:table_pmaxs}

6ff7d98f   Jean-Philippe Bernard   First commit
91
chi2s=dblarr(Nseds)
f8c3f95f   Jean-Philippe Bernard   added limits and ...
92
93
94
grid_seds=dblarr(Nseds,Nfilters)
total_seds=dblarr(Nseds)
grid_param_values=dblarr(Nseds,Nparams)
6ff7d98f   Jean-Philippe Bernard   First commit
95
96
97
98
99
100

;=== compute sed grid Array
FOR i=0L,Nseds-1 DO BEGIN
	FOR j=0L,Nfilters-1 DO BEGIN
		grid_seds[i,j]=seds[i].(j+Nparams)
	ENDFOR
f8c3f95f   Jean-Philippe Bernard   added limits and ...
101
	total_seds[i]=seds[i].total
6ff7d98f   Jean-Philippe Bernard   First commit
102
103
104
105
106
107
108
109
110
ENDFOR
;=== compute the parameter values Array
FOR i=0L,Nseds-1 DO BEGIN
	FOR j=0L,Nparams-1 DO BEGIN
		grid_param_values[i,j]=seds[i].(j)
	ENDFOR
ENDFOR
;stop

f8c3f95f   Jean-Philippe Bernard   added limits and ...
111
;=== loop over grid lines to compute chi2s
6ff7d98f   Jean-Philippe Bernard   First commit
112
113
114
115
116
117
;we should actually store the total of grid sed for each grid sed into the grid fits file (!)
;we could also store the total of observed seds in the observed sed save fits file (!)
facts=dblarr(Nseds)
facts[*]=1.d0
IF keyword_set(normalize) THEN BEGIN
	FOR i=0L,Nseds-1 DO BEGIN
f8c3f95f   Jean-Philippe Bernard   added limits and ...
118
119
		;facts[i]=total(sed.stokesI)/total(grid_seds[i,*])
		facts[i]=total(sed.stokesI)/total_seds[i]
6ff7d98f   Jean-Philippe Bernard   First commit
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
		this_sed=sed.stokesI/facts[i]
		this_var=sed.sigmaII/facts[i]^2
	    ;chi2s[i]=total((sed.stokesI-grid_seds[i,*])^2/sed.sigmaII)
	    chi2s[i]=total((this_sed-grid_seds[i,*])^2/this_var)
	ENDFOR
ENDIF ELSE BEGIN
	FOR i=0L,Nseds-1 DO BEGIN
	    chi2s[i]=total((sed.stokesI-grid_seds[i,*])^2/sed.sigmaII)
	ENDFOR
ENDELSE
;stop

chi2=min(chi2s)
ind=where(chi2s EQ chi2,count)

f8c3f95f   Jean-Philippe Bernard   added limits and ...
135
136
params=grid_param_values[ind[0],*]  ;these are the best fit parameters
fact=facts[ind[0]]   ; This is the corresponding scaling factor
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
137
Nfreedom=Nfilters-Nparams-1   ;This is the degree of freedom
f8c3f95f   Jean-Philippe Bernard   added limits and ...
138
139
140
141
rchi2=chi2/Nfreedom        ;This is the reduced chi2
best_grid_sed=reform(grid_seds[ind[0],*])   ;This is the best SED fround in the grid.
best_sed=best_grid_sed*fact  ;This is the best scaled grid SED matching the input SED.

4176fb1e   Jean-Philippe Bernard   fixed a bug
142
;==== should check bumping to table edges there
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
143
144
145
146
147
params_hit=intarr(Nparams)
FOR i=0L,Nparams-1 DO BEGIN
  IF params[i] EQ table_pmins[i] THEN params_hit[i]=-1
  IF params[i] EQ table_pmaxs[i] THEN params_hit[i]=1
ENDFOR
4176fb1e   Jean-Philippe Bernard   fixed a bug
148

f8c3f95f   Jean-Philippe Bernard   added limits and ...
149
;==== should compute parameter uncertainties there
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
150
;stop
f8c3f95f   Jean-Philippe Bernard   added limits and ...
151
conf_level=90./100.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
152
153
154
155
156
157
158
159
160
;print,Nfreedom,conf_level
IF Nfreedom NE 0. THEN BEGIN
	dchi2=delta_chi2(Nfreedom,conf_level)
ENDIF ELSE BEGIN
	message,'Nfreedom = 0. Beware of meaningless fit',/continue
	stop
	message,'seting Nfreedom = 1. Beware of meaningless fit',/continue
  dchi2=delta_chi2(1.,conf_level)
ENDELSE
f8c3f95f   Jean-Philippe Bernard   added limits and ...
161
ind=where(chi2s LE chi2+dchi2,count)
2d100dd8   Jean-Philippe Bernard   fixed another sma...
162
163
params_min=fltarr(Nparams)
params_max=fltarr(Nparams)
f8c3f95f   Jean-Philippe Bernard   added limits and ...
164
165
166
167
168
FOR i=0L,Nparams-1 DO BEGIN
	params_min[i]=min(params[ind,i])
	params_max[i]=max(params[ind,i])
ENDFOR
params_uncertainties=params_max-params_min
e62a03b3   Jean-Philippe Bernard   chaaged the plot
169

6ff7d98f   Jean-Philippe Bernard   First commit
170
171
172
IF keyword_set(show_sed) THEN BEGIN
	xtit='Wavelength [mic]'
	ytit='SED (model=red,data=blue)'
e62a03b3   Jean-Philippe Bernard   chaaged the plot
173
	;should also plot uncertainties
ad631268   Jean-Philippe Bernard   chaaged the plot
174
175
176
177
	minv=la_min([sed.stokesI,best_sed])
	maxv=la_max([sed.stokesI,best_sed])
	cgplot,sed.wave,sed.stokesI,psym='Filled Circle',color='blue',/ylog,/ysty,xtit=xtit,ytit=ytit,/xlog,yrange=[minv,maxv]
	cgoplot,sed.wave,best_sed,psym='Filled Square',color='red'
b4dbd43c   Jean-Philippe Bernard   chaaged the plot
178
	if not keyword_set(nostop) THEN stop
6ff7d98f   Jean-Philippe Bernard   First commit
179
180
ENDIF

f12d8052   Jean-Philippe Bernard   chaaged the plot
181
182
the_end:

6ff7d98f   Jean-Philippe Bernard   First commit
183
184
RETURN,params
END