Blame view

src/idl/dustem_make_sed_table.pro 9.96 KB
2b6fd350   Jean-Philippe Bernard   added pre-computi...
1
2
3
4
5
6
7
8
9
10
11
PRO dustem_make_sed_table,model, $
											    parameters_description, $
											    iv_min, $
											    iv_max, $
											    iv_Nvalues, $
						  						fpd=fpd, $
						  						fiv=fiv, $
						  						filename=filename, $
						  						filters=filters, $
						  						log=log, $
						  						show_seds=show_seds, $
2b6fd350   Jean-Philippe Bernard   added pre-computi...
12
						  						help=help
eb263d61   Jean-Philippe Bernard   commented
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

;+
; NAME:
;    dustem_make_sed_table
; PURPOSE:
;    makes an SED lookup table using dustemwrap for a given dust model and free parameters
; CATEGORY:
;    Dustem
; CALLING SEQUENCE:
;    dustem_make_sed_table,model,parameters_description,parvalues_min,parvalues_max,parvalues_Nvalues, $
;                     	   [,fpd=][,fiv=][,filename=][,filters=][,log=][,/show_seds]
; INPUTS:
;    model                  : dustemwrap model name to be used
;    parameters_description : dustemwrap parameter description array
;    parvalues_min          : minimum values for each parameter
;    parvalues_max          : maximum values for each parameter
2b6fd350   Jean-Philippe Bernard   added pre-computi...
29
;    parvalues_Nvalues      : number of parameter values for each parameter
eb263d61   Jean-Philippe Bernard   commented
30
31
32
33
; OPTIONAL INPUT PARAMETERS:
;    filters                : name of dustemwrap filters to be included in the SED calculations (default = IRAS filters)
;    fpd                    : fixed parameter description
;    fiv                    : fixed parameter values
eb263d61   Jean-Philippe Bernard   commented
34
35
36
37
38
39
40
41
;    filename               : output fits file name for the table (default ='/tmp/dustem_seds.fits')
;    log                    : array indicating if a given parameter is to be sampled in linear (0) or log scale (1)
;    show_seds              : if set, plots SEDs as they are computed.
; OUTPUTS:
;    None
; OPTIONAL OUTPUT PARAMETERS:
;    None
; ACCEPTED KEY-WORDS:
eb263d61   Jean-Philippe Bernard   commented
42
43
44
45
46
47
48
49
50
51
52
;    help      = If set, print this help
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    A file is written
; RESTRICTIONS:
;    The DustEM fortran code must be installed
;    The DustEMWrap IDL code must be installed
; PROCEDURE:
;    None
; EXAMPLES
2b6fd350   Jean-Philippe Bernard   added pre-computi...
53
54
55
;    dustem_make_sed_table,'DBP90',['(*!dustem_params).G0','(*!dustem_params).grains(0).mdust_o_mh'], $
;																[0.1,1.e-3],[100.,1.e-1],[10,3],filename='/tmp/IRAS_GO.fits',log=[1,0],/show_seds, $
;                              	filters=[dustem_instru2filters('IRAC'),dustem_instru2filters('SPIRE')]
eb263d61   Jean-Philippe Bernard   commented
56
57
58
59
60
61
62
63
64
65
; MODIFICATION HISTORY:
;    Written by J.-Ph. Bernard (2023)
;    Evolution details on the DustEMWrap gitlab.
;    See http://dustemwrap.irap.omp.eu/ for FAQ and help.  
;-

IF keyword_set(help) THEN BEGIN
  doc_library,'dustem_make_sed_table'
  goto,the_end
ENDIF
6ff7d98f   Jean-Philippe Bernard   First commit
66

6ff7d98f   Jean-Philippe Bernard   First commit
67
68
69
70
71
Nparams=n_elements(parameters_description)

;== INITIALISE DUSTEM
use_polarization=0
dustem_init,model=model,pol=use_polarisation
0297394d   Jean-Philippe Bernard   test
72
!quiet=1
f28cb4f4   Jean-Philippe Bernard   improved
73
74
;!dustem_verbose=1
;!dustem_show_plot=1
6ff7d98f   Jean-Philippe Bernard   First commit
75
76
!dustem_which='RELEASE'

429a6075   Jean-Philippe Bernard   improved
77
;=== define default filters
eb263d61   Jean-Philippe Bernard   commented
78
use_filters=[dustem_instru2filters('NIRCAM'),dustem_instru2filters('MIRI'),dustem_instru2filters('IRAS'),dustem_instru2filters('PACS'),dustem_instru2filters('SPIRE'),dustem_instru2filters('HFI')]
6ff7d98f   Jean-Philippe Bernard   First commit
79
IF keyword_set(filters) THEN use_filters=filters
eb263d61   Jean-Philippe Bernard   commented
80
81
82
wavs=dustem_filter2wav(use_filters)
order=sort(wavs)
use_filters=use_filters[order]
6ff7d98f   Jean-Philippe Bernard   First commit
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Nfilters=n_elements(use_filters)

;==== compute all combinations of model parameter values
parameter_values=dustem_param_range2param_values(iv_min,iv_max,iv_Nvalues,Nc=Nc,log=log)

;=== initialize filters for the SED
Nfilt=n_elements(use_filters)
;stop
sed=dustem_initialize_sed(Nfilt)

sed.filter=use_filters
sed.wave=dustem_filter2wav(use_filters)
sed.instru=dustem_filter2instru(use_filters)

;=== initialize IQU and associated errors to avoid problems when checking SED in dustem_set_data.pro
sed[*].StokesI=1.e-10
sed.StokesQ=sed.StokesI/100.
sed.StokesU=sed.StokesI/100.
sed.SigmaII=sed.StokesI/100.
sed.SigmaQQ=sed.StokesI/100.
sed.SigmaUU=sed.StokesI/100.
sed.SigmaIQ=sed.StokesI/100.
sed.SigmaIU=sed.StokesI/100.
sed.SigmaQU=sed.StokesI/100.

;=== initialize wavelengths for the EXTINCTION
use_next=100 ; number of extinction measurements
use_wave=10^(alog10(0.01)+alog10(50)*findgen(use_next)/float(use_next)) ; wavelengths at which extinction is defined
if keyword_set(next) then use_next=next
if keyword_set(wave) then $
   use_wave=range_gen(use_next,[wave[0],wave[1]],/log)

ext=dustem_initialize_ext(use_next)
ext.instru='EXTINCTION'
ext.filter='SPECTRUM'
ext.wave=use_wave

;=== initialize IQU and associated errors to avoid problems when checking EXT in dustem_set_data.pro
ext[*].EXT_I=1.e-10
ext.EXT_Q=ext.EXT_I/100.
ext.EXT_U=ext.EXT_I/100.
ext.SIGEXTII=ext.EXT_I/1000.
ext.SIGEXTQQ=ext.EXT_I/1000.
ext.SIGEXTUU=ext.EXT_I/1000.
ext.SIGEXTIQ=ext.EXT_I/1000.
ext.SIGEXTIU=ext.EXT_I/1000.
ext.SIGEXTQU=ext.EXT_I/1000.

dustem_set_data,m_fit=sed,m_show=sed,x_fit=ext,x_show=ext

;=== initialize at least one parameter as 'free'
;=== this is only to keep dustemwrap happy in its management of variables
;=== no fitting is going to be done
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
136
137
138
;pd = ['(*!dustem_params).gas.G0'] ; G0
;pval = [-1.e-20]
pd=parameters_description
6ff7d98f   Jean-Philippe Bernard   First commit
139
140
141
142
143

;=== declare some other parameters as fixed
;=== this shows how to add any non-dust plugins to the observed spectra/SED
;=== for the dust-model parameters, it is not necessary, but allows the
;=== user to (i) change default values and (2) see the adopted values in the graphical output windows
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
144
;fpd=parameters_description
6ff7d98f   Jean-Philippe Bernard   First commit
145
146
147
seds=ptrarr(Nc)
yrange=[1.e-5,1.e5]
colors=long(range_gen(Nc,[0,255]))
14c90b64   Jean-Philippe Bernard   removed loadct co...
148
IF keyword_set(show_seds) THEN loadct,13
6ff7d98f   Jean-Philippe Bernard   First commit
149
150
151
FOR i=0L,Nc-1 DO BEGIN
	;dustem_init,model=model,pol=use_polarisation
	;dustem_set_data,m_fit=sed,m_show=sed,x_fit=ext,x_show=ext
0297394d   Jean-Philippe Bernard   test
152
  dummy=0 ;otherwise model is not recomputed !!
6ff7d98f   Jean-Philippe Bernard   First commit
153

25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
154
155
	;fpval = *parameter_values[i]
  pval = *parameter_values[i]
f28cb4f4   Jean-Philippe Bernard   improved
156
157
	message,'= computing model '+strtrim(i,2)+'/'+strtrim(Nc,2)+' ('+strtrim(1.*i/Nc*100.)+'%)',/continue
	;print,'parameter_values=',pval
6ff7d98f   Jean-Philippe Bernard   First commit
158
159
    
	;=== initialise all this information into dustemwrap's brain
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
160
161
162
	;stop
	;dustem_init_params,model,pd,pval,fpd=fpd,fiv=fpval,pol=use_polarisation
	dustem_init_params,model,pd,pval,fpd=fpd,fiv=fiv,pol=use_polarisation
6ff7d98f   Jean-Philippe Bernard   First commit
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

	;=== compute the predicted SED and extinction curve for the dust-model
	;=== and any plugins 
	dustem_Ised=dustem_compute_sed(pval,st=dummy)
	dustem_Qsed=dustem_Ised*0.
	dustem_Used=dustem_Ised*0.
	; uncomment following lines if running a polarisation model and you
	;want predictions of QU
	;toto=dustem_compute_stokes(pval,st=dummy) ;this procedure also allows for the extraction of the spectra
	;dustem_qsed = toto[0]
	;dustem_used = toto[1]

    ;This is for extinction. Not used at the moment.
	;dustem_iext=dustem_compute_ext(pval,st=dummy)
	;dustem_Qext=dustem_Iext*0.
	;dustem_Uext=dustem_Iext*0.
	; uncomment following lines if running a polarisation model and you
	;want predictions of QU
	;toto=dustem_compute_stokext(pval,st=dummy) ;this procedure also allows for the extraction of the spectra
	;dustem_qext = toto[0]
	;dustem_uext = toto[1]

	;== Now we have a predicted SED (I), we fill up a structure that we
	;== will write to the sed_outfile
	;== if you 
	sed.stokesI=dustem_Ised
	sed.stokesQ=dustem_Qsed
	sed.stokesU=dustem_Used
	;=== set uncertainties for I,Q,U to 0.
	sed.sigmaII=0.
	sed.sigmaQQ=0.
	sed.sigmaUU=0.
	;=== set covariances to 0.
	sed.sigmaIQ=0.
	sed.sigmaIU=0.
	sed.sigmaQU=0.

	;==== fill in dependent columns of the SED.
	sed=dustem_fill_sed_dependent_columns(sed)
	seds[i]=ptr_new(sed)
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
203
  IF keyword_set(show_seds) THEN BEGIN
6ff7d98f   Jean-Philippe Bernard   First commit
204
	    IF i EQ 0 THEN BEGIN
25370043   Jean-Philippe Bernard   src/idl/dustem_ac...
205
206
207
		    tit='Grid SEDs model '+model
		    xtit='Wavelength [mic]'
		    ytit='SED [MJy/sr for NH=1.e20 H/cm^2]'
eb263d61   Jean-Philippe Bernard   commented
208
	    	cgplot,(*seds[i]).wave,(*seds[i]).stokesI,psym=-4,/ylog,yr=yrange,xtit=xtit,ytit=ytit,tit=tit,/xlog
6ff7d98f   Jean-Philippe Bernard   First commit
209
	    ENDIF ELSE BEGIN
eb263d61   Jean-Philippe Bernard   commented
210
	    	cgoplot,(*seds[i]).wave,(*seds[i]).stokesI,psym=-4,color=colors[i]
6ff7d98f   Jean-Philippe Bernard   First commit
211
212
213
214
215
216
217
218
219
220
221
222
	    ENDELSE
	ENDIF
	;stop
ENDFOR
;stop

str='one_sed={'
FOR i=0L,Nparams-1 DO BEGIN
  str=str+'P'+strtrim(i+1,2)+':0.d0,'
ENDFOR
FOR i=0L,Nfilters-1 DO BEGIN
	;istr=strtrim(i+1,2)
eb263d61   Jean-Philippe Bernard   commented
223
	istr=strtrim(use_filters[i],2)
6ff7d98f   Jean-Philippe Bernard   First commit
224
225
	str=str+'I'+istr+':0.d0'
	IF use_polarization EQ 1 THEN str=str+',Q'+istr+':0.d0,U'+istr+':0.d0'
2b6fd350   Jean-Philippe Bernard   added pre-computi...
226
227
	;IF i NE Nfilters-1 THEN str=str+',' ELSE str=str+'}'
	str=str+','
6ff7d98f   Jean-Philippe Bernard   First commit
228
ENDFOR
2b6fd350   Jean-Philippe Bernard   added pre-computi...
229
str=str+'total:0.d0}'
6ff7d98f   Jean-Philippe Bernard   First commit
230
231
232
233
234
235
236
237
238
239
toto=execute(str)

;stop
Noffset=1
IF use_polarization EQ 1 THEN Noffset=3
seds_array=replicate(one_sed,Nc)
FOR i=0L,Nc-1 DO BEGIN
	FOR j=0L,Nparams-1 DO BEGIN
		seds_array[i].(j)=(*parameter_values[i])[j]
	ENDFOR
2b6fd350   Jean-Philippe Bernard   added pre-computi...
240
	sum=0.d0
6ff7d98f   Jean-Philippe Bernard   First commit
241
242
243
	FOR j=0L,Nfilters-1 DO BEGIN
		pos=Nparams+j*Noffset
		seds_array[i].(pos)=((*seds[i]).stokesI)[j]
2b6fd350   Jean-Philippe Bernard   added pre-computi...
244
		sum=sum+((*seds[i]).stokesI)[j] ;======== compute the sum of SEDs
6ff7d98f   Jean-Philippe Bernard   First commit
245
	ENDFOR
2b6fd350   Jean-Philippe Bernard   added pre-computi...
246
	seds_array[i].total=sum
6ff7d98f   Jean-Philippe Bernard   First commit
247
248
249
ENDFOR

;======== save SED tables
3b79f061   Jean-Philippe Bernard   improved
250
251
252
;use_sed_outfile='/tmp/dustem_seds.xcat'
;write_xcat,seds_array,use_sed_outfile
;message,'Wrote '+use_sed_outfile,/continue
6ff7d98f   Jean-Philippe Bernard   First commit
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

fits_file='/tmp/dustem_seds.fits'
IF keyword_set(filename) THEN fits_file=filename

mwrfits,seds_array,fits_file,header,/create

st=mrdfits(fits_file,0,h)
st1=mrdfits(fits_file,1,h1)

sxaddpar,h1,'MODEL',model,'dustemwrap model used',after='TFORM'+strtrim(Nparams+Nfilters,2)
sxaddpar,h1,'POLAR',use_polarization,'dustemwrap polarization ?',after='MODEL'
FOR j=0L,Nparams-1 DO BEGIN
	value=sxpar(h1,'TTYPE'+strtrim(j+1,2))
	sxaddpar,h1,'TTYPE'+strtrim(j+1,2),value,'dustemwrap '+parameters_description[j]
	sxaddpar,h1,'PNAME'+strtrim(j+1,2),parameters_description[j],'dustemwrap param name'
2b6fd350   Jean-Philippe Bernard   added pre-computi...
268
269
270
271
272
273
	;iv_min,iv_max,iv_Nvalues,Nc=Nc,log=log
	sxaddpar,h1,'PMIN'+strtrim(j+1,2),iv_min[j],'minimum param value'
	sxaddpar,h1,'PMAX'+strtrim(j+1,2),iv_max[j],'maximum param value'
	sxaddpar,h1,'PNVAL'+strtrim(j+1,2),iv_Nvalues[j],'Number of param values'
	sxaddpar,h1,'PLOG'+strtrim(j+1,2),log[j],'1 if log scale sampling'
	last_pname='PLOG'+strtrim(j+1,2)
6ff7d98f   Jean-Philippe Bernard   First commit
274
275
276
277
278
279
280
281
282
283
284
285
	IF j EQ 0 THEN first_pname='PNAME'+strtrim(j+1,2)
ENDFOR
j0=j
sxaddpar,h1,'COMMENT','**** Dustemwrap PARAMETERS ***',before=first_pname
sxaddpar,h1,'COMMENT','**** Dustemwrap FILTERS ***',after=last_pname
FOR j=0L,Nfilters-1 DO BEGIN
	jj=j+j0
	value=sxpar(h1,'TTYPE'+strtrim(jj+1,2))
	sxaddpar,h1,'TTYPE'+strtrim(jj+1,2),value,'Intensity in '+use_filters[j]
	sxaddpar,h1,'FNAME'+strtrim(j+1,2),use_filters[j],'dustemwrap filter name'
ENDFOR

2b6fd350   Jean-Philippe Bernard   added pre-computi...
286
287
;stop

6ff7d98f   Jean-Philippe Bernard   First commit
288
289
290
291
292
293
294
295
mwrfits,seds_array,fits_file,h1,/create
message,'Wrote '+fits_file,/continue

toto=mrdfits(fits_file,1,header)

;hprint,header

;stop
eb263d61   Jean-Philippe Bernard   commented
296
the_end:
6ff7d98f   Jean-Philippe Bernard   First commit
297
298

END