Blame view

src/idl/dustem_brute_force_fit.pro 6.84 KB
f8c3f95f   Jean-Philippe Bernard   added limits and ...
1
FUNCTION dustem_brute_force_fit,sed $
849fd436   Jean-Philippe Bernard   improved
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
							   							 ,table_name $
							   							 ,filters $
							   							 ,fixed_parameters_description=fixed_parameters_description $
							   							 ,fixed_parameters_values=fixed_parameters_values $
							   							 ,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
19

dbf82b16   Jean-Philippe Bernard   chaaged the plot
20
21
22
23
24
25
26
27
;+
; 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 ...
28
;    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
29
; INPUTS:
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
30
;    sed  = sed to be fit. Should contain all filters provided to the routine trhough input variable filter
f8c3f95f   Jean-Philippe Bernard   added limits and ...
31
32
;    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
33
; OPTIONAL INPUT PARAMETERS:
849fd436   Jean-Philippe Bernard   improved
34
35
;    fixed_parameters_description = if set, describes parameters of the table that should be considered fixed.
;    fixed_parameters_values      = values of parameters of the table that should be considered fixed.
dbf82b16   Jean-Philippe Bernard   chaaged the plot
36
; OUTPUTS:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
37
;    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
38
; OPTIONAL OUTPUT PARAMETERS:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
39
;    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
40
41
;    chi2 = chi2 of the best fit
;    rchi2 = reduced chi2 of the best fit
f8c3f95f   Jean-Philippe Bernard   added limits and ...
42
43
44
45
46
47
;    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
48
49
; ACCEPTED KEY-WORDS:
;    help                  = if set, print this help
f8c3f95f   Jean-Philippe Bernard   added limits and ...
50
51
52
;    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
53
54
55
56
57
58
59
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    None
; RESTRICTIONS:
;    The DustEMWrap IDL code must be installed
; PROCEDURE:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
60
;    Scans the grid SEDs to find the best fit (lowest chi2)
dbf82b16   Jean-Philippe Bernard   chaaged the plot
61
; EXAMPLES
f8c3f95f   Jean-Philippe Bernard   added limits and ...
62
;    
dbf82b16   Jean-Philippe Bernard   chaaged the plot
63
; MODIFICATION HISTORY:
f8c3f95f   Jean-Philippe Bernard   added limits and ...
64
;    Written by JPB Jan 2024
dbf82b16   Jean-Philippe Bernard   chaaged the plot
65
66
67
68
69
;    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 ...
70
71
  doc_library,'dustem_brute_force_fit'
  params=0.
dbf82b16   Jean-Philippe Bernard   chaaged the plot
72
73
74
  goto,the_end
ENDIF

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

f8c3f95f   Jean-Philippe Bernard   added limits and ...
78
;==== test for the existence of a loaded grid and initialize if needed.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
79
;==== 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
80
defsysv,'!dustem_grid',0,exist=exist
f8c3f95f   Jean-Philippe Bernard   added limits and ...
81
IF exist EQ 0 or keyword_set(reset)THEN BEGIN
6ff7d98f   Jean-Philippe Bernard   First commit
82
83
84
    dustem_define_grid,table_name,filters
ENDIF

849fd436   Jean-Philippe Bernard   improved
85
;=== Should marginalize table for fixed parameters, if any
6ff7d98f   Jean-Philippe Bernard   First commit
86
87
88
89
seds=!dustem_grid.seds
Nseds=!dustem_grid.Nsed
Nfilters=!dustem_grid.Nfilters
Nparams=!dustem_grid.Nparams
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
90
91
92
table_params=!dustem_grid.params
table_pmins=!dustem_grid.pmin_values
table_pmaxs=!dustem_grid.pmax_values
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
93

849fd436   Jean-Philippe Bernard   improved
94
95
96
97
98
IF keyword_set(fixed_parameters_description) THEN BEGIN
	dustem_marginalize_grid,fixed_parameters_description,fixed_parameters_values,seds,Nseds,Nfilters,Nparams,table_params,table_pmins,table_pmaxs
ENDIF

;=== These are the full arrays needed for calculations
6ff7d98f   Jean-Philippe Bernard   First commit
99
chi2s=dblarr(Nseds)
f8c3f95f   Jean-Philippe Bernard   added limits and ...
100
101
102
grid_seds=dblarr(Nseds,Nfilters)
total_seds=dblarr(Nseds)
grid_param_values=dblarr(Nseds,Nparams)
6ff7d98f   Jean-Philippe Bernard   First commit
103
104
105
106
107
108

;=== 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 ...
109
	total_seds[i]=seds[i].total
6ff7d98f   Jean-Philippe Bernard   First commit
110
111
112
113
114
115
116
117
118
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 ...
119
;=== loop over grid lines to compute chi2s
849fd436   Jean-Philippe Bernard   improved
120
;we do actually store the total of grid sed for each grid sed into the grid fits file
6ff7d98f   Jean-Philippe Bernard   First commit
121
122
123
124
125
;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 ...
126
127
		;facts[i]=total(sed.stokesI)/total(grid_seds[i,*])
		facts[i]=total(sed.stokesI)/total_seds[i]
6ff7d98f   Jean-Philippe Bernard   First commit
128
129
		this_sed=sed.stokesI/facts[i]
		this_var=sed.sigmaII/facts[i]^2
849fd436   Jean-Philippe Bernard   improved
130
131
	  ;chi2s[i]=total((sed.stokesI-grid_seds[i,*])^2/sed.sigmaII)
	  chi2s[i]=total((this_sed-grid_seds[i,*])^2/this_var)
6ff7d98f   Jean-Philippe Bernard   First commit
132
133
134
	ENDFOR
ENDIF ELSE BEGIN
	FOR i=0L,Nseds-1 DO BEGIN
849fd436   Jean-Philippe Bernard   improved
135
	   chi2s[i]=total((sed.stokesI-grid_seds[i,*])^2/sed.sigmaII)
6ff7d98f   Jean-Philippe Bernard   First commit
136
137
138
139
140
141
142
	ENDFOR
ENDELSE
;stop

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

f8c3f95f   Jean-Philippe Bernard   added limits and ...
143
144
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...
145
Nfreedom=Nfilters-Nparams-1   ;This is the degree of freedom
f8c3f95f   Jean-Philippe Bernard   added limits and ...
146
147
148
149
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.

849fd436   Jean-Philippe Bernard   improved
150
;==== Check bumping to table edges.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
151
152
153
154
155
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
156

849fd436   Jean-Philippe Bernard   improved
157
;==== Compute parameter uncertainties
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
158
;stop
f8c3f95f   Jean-Philippe Bernard   added limits and ...
159
conf_level=90./100.
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
160
161
162
163
164
165
166
167
168
;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 ...
169
ind=where(chi2s LE chi2+dchi2,count)
2d100dd8   Jean-Philippe Bernard   fixed another sma...
170
171
params_min=fltarr(Nparams)
params_max=fltarr(Nparams)
f8c3f95f   Jean-Philippe Bernard   added limits and ...
172
173
174
175
176
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
177

6ff7d98f   Jean-Philippe Bernard   First commit
178
179
180
IF keyword_set(show_sed) THEN BEGIN
	xtit='Wavelength [mic]'
	ytit='SED (model=red,data=blue)'
e62a03b3   Jean-Philippe Bernard   chaaged the plot
181
	;should also plot uncertainties
ad631268   Jean-Philippe Bernard   chaaged the plot
182
183
184
185
	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
186
	if not keyword_set(nostop) THEN stop
6ff7d98f   Jean-Philippe Bernard   First commit
187
188
ENDIF

f12d8052   Jean-Philippe Bernard   chaaged the plot
189
190
the_end:

6ff7d98f   Jean-Philippe Bernard   First commit
191
192
RETURN,params
END