Blame view

src/idl/dustem_write_fits_table.pro 10.5 KB
1cf58fd8   Jean-Philippe Bernard   First commit
1
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
PRO dustem_write_fits_table,filename=filename,help=help

;+
; NAME:
;    dustem_write_fits_table
; PURPOSE:
;    Write a fits table with content of current Dustem system variables
; CATEGORY:
;    Dustem
; CALLING SEQUENCE:
;    dustem_write_fits_table,file[,/help]
; INPUTS:
;    file      = File name
; OPTIONAL INPUT PARAMETERS:
;    None
; OUTPUTS:
;    None
; OPTIONAL OUTPUT PARAMETERS:
;    None
; ACCEPTED KEY-WORDS:
;    help      = If set, print this help
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    File is stored.
; RESTRICTIONS:
;    The dustem idl wrapper must be installed
; PROCEDURE:
;    None
; EXAMPLES
;    
; MODIFICATION HISTORY:
1cf2bf59   Annie Hughes   updated dustem_da...
33
34
35
;    Written by J.-Ph. Bernard July 2022
;    Evolution details on the DustEMWrap gitlab.
;    See http://dustemwrap.irap.omp.eu/ for FAQ and help.  
1cf58fd8   Jean-Philippe Bernard   First commit
36
37
;-

1cf58fd8   Jean-Philippe Bernard   First commit
38
39
40
41
42
43
44
45
46
47
48
49

IF keyword_set(help) THEN BEGIN
  doc_library,'dustem_write_fits_table'
  goto,the_end
ENDIF

;===== run dustem to get predicted spectrum and sed
IF not keyword_set(function_name) THEN BEGIN 
  dustem_predicted_sed=dustem_compute_sed(*(*!dustem_fit).current_param_values,out_st=dustem_spectra_st)
ENDIF ELSE BEGIN
ENDELSE

72089d7a   Jean-Philippe Bernard   improved to save ...
50
51
used_model=!dustem_model

f2808c64   Jean-Philippe Bernard   improved
52
53
Ngrains=n_tags(dustem_spectra_st.sed)-2

1cf58fd8   Jean-Philippe Bernard   First commit
54
55
56
;===== define structures containing results
one_str_input_SED={FILTER:'',Wavelength:0.,I:0.,Q:0.,U:0.,varianceII:0.,varianceQQ:0.,varianceUU:0.}
one_str_predicted_SED={FILTER:'',Wavelength:0.,I:0.,Q:0.,U:0.}
f2808c64   Jean-Philippe Bernard   improved
57
58
;one_str_predicted_spectrum_tot={Wavelength:0.,I:0.,Q:0.,U:0.}
;one_str_predicted_spectrum_grain1={Wavelength:0.,I:0.,Q:0.,U:0.}
1cf58fd8   Jean-Philippe Bernard   First commit
59

1cf2bf59   Annie Hughes   updated dustem_da...
60
Nsed=n_elements((*(*!dustem_data).sed).filt_names)
1cf58fd8   Jean-Philippe Bernard   First commit
61
str_input_SED=replicate(one_str_input_SED,Nsed)
1cf2bf59   Annie Hughes   updated dustem_da...
62
63
64
65
str_input_SED.FILTER=(*(*!dustem_data).sed).filt_names
str_input_SED.Wavelength=(*(*!dustem_data).sed).wav
str_input_SED.I=(*(*!dustem_data).sed).values
str_input_SED.varianceII=(*(*!dustem_data).sed).sigma   ;should it be squared ??
1cf58fd8   Jean-Philippe Bernard   First commit
66
67
68

N_predicted_sed=n_elements(dustem_predicted_sed)
str_predicted_SED=replicate(one_str_predicted_SED,N_predicted_sed)
1cf2bf59   Annie Hughes   updated dustem_da...
69
70
str_predicted_SED.FILTER=(*(*!dustem_data).sed).filt_names    ;taken from input SED
str_predicted_SED.Wavelength=(*(*!dustem_data).sed).wav       ;taken from input SED
1cf58fd8   Jean-Philippe Bernard   First commit
71
72
73
74
str_predicted_SED.I=dustem_predicted_sed    ;but where is Q,U ??
;str_predicted_SED.Q=
;str_predicted_SED.U=

f2808c64   Jean-Philippe Bernard   improved
75
76
77
78
;str_predicted_spectrum_grains=ptrarr(Ngrains)
;FOR i=0L,Ngrains-1 DO BEGIN
;	str_predicted_spectrum_grains[i]=ptr_new(dustem_spectra_st.sed)
;ENDFOR
1cf58fd8   Jean-Philippe Bernard   First commit
79
80

N_predicted_spectra=n_elements(dustem_spectra_st.sed)
f2808c64   Jean-Philippe Bernard   improved
81
82
83
;str_predicted_spectrum_tot=replicate(one_str_predicted_spectrum_tot,N_predicted_spectra)
;str_predicted_spectrum_tot.Wavelength=dustem_spectra_st.SED.wav
;str_predicted_spectrum_tot.I=dustem_spectra_st.SED.em_tot
1cf58fd8   Jean-Philippe Bernard   First commit
84
85
86
87
88
89
90
91
92
93
;str_predicted_spectrum_tot.Q=dustem_spectra_st.polsed         ;not exactly, should have Q,U
;str_predicted_spectrum_tot.U=dustem_spectra_st.polsed         ;not exactly, should have Q,U

;===== Fill in header with parameter values
parameters_desc=*(*!dustem_fit).PARAM_DESCS
parameters_init_values=*(*!dustem_fit).PARAM_INIT_VALUES
best_parameters=*(*!dustem_fit).CURRENT_PARAM_VALUES
best_parameters_uncertainties=*(*!dustem_fit).CURRENT_PARAM_ERRORS
best_fit_chi2=(*!dustem_fit).CHI2
best_fit_rchi2=(*!dustem_fit).RCHI2
72089d7a   Jean-Philippe Bernard   improved to save ...
94
parameters_func_values=*(*!dustem_fit).PARAM_FUNC
1cf58fd8   Jean-Philippe Bernard   First commit
95
96
97

Nparams=n_elements(parameters_desc)

f2808c64   Jean-Philippe Bernard   improved
98
99
100
101
102
;unit_input_sed=1
;unit_predicted_sed=2
;unit_predicted_spectra=3
;unit_predicted_extinctions=4

1cf58fd8   Jean-Philippe Bernard   First commit
103
;stop
3b2e1c40   Annie Hughes   updated default f...
104
file='./dustemwrap_tmp.fits'
1cf58fd8   Jean-Philippe Bernard   First commit
105
;==== Write the observed SED in extension 1
f2808c64   Jean-Philippe Bernard   improved
106
unit=1L
1cf58fd8   Jean-Philippe Bernard   First commit
107
mwrfits,str_input_SED,file,/create
f2808c64   Jean-Philippe Bernard   improved
108
109
unit_input_sed=unit
unit=unit+1
1cf58fd8   Jean-Philippe Bernard   First commit
110
111
;==== add the predicted SED in extension 2
mwrfits,str_predicted_SED,file
f2808c64   Jean-Philippe Bernard   improved
112
113
unit_predicted_sed=unit
unit=unit+1
1cf58fd8   Jean-Philippe Bernard   First commit
114
;==== add the predicted total spectrum in extension 3
f2808c64   Jean-Philippe Bernard   improved
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
;mwrfits,str_predicted_spectrum_tot,file
;==== add the predicted per-grain spectrum in extension 4
;stop
mwrfits,dustem_spectra_st.sed,file
unit_predicted_spectra=unit
unit=unit+1
;==== add the predicted per-grain extinction in extension 5
mwrfits,dustem_spectra_st.ext,file
unit_predicted_extinctions=unit
;FOR i=0L,Ngrains-1 DO BEGIN
;	mwrfits,*str_predicted_spectrum_grains[i],file
;ENDFOR

;print,unit_input_sed,unit_predicted_sed,unit_predicted_spectra,unit_predicted_extinctions
;stop
1cf58fd8   Jean-Philippe Bernard   First commit
130
131

;===== read back extensions to get proper extension headers
3b2e1c40   Annie Hughes   updated default f...
132
file='./dustemwrap_tmp.fits'
f2808c64   Jean-Philippe Bernard   improved
133
134
135
136
137
138
139
140
141
142
143
144
145
sst_input_sed=mrdfits(file,unit_input_sed,header_input_sed)
sst_predicted_SED=mrdfits(file,unit_predicted_sed,header_predicted_SED)
;sst_predicted_spectrum_tot=mrdfits(file,3,header_predicted_spectrum_tot)
dustem_spectra_st.sed=mrdfits(file,unit_predicted_spectra,header_predicted_spectra)
dustem_spectra_st.ext=mrdfits(file,unit_predicted_extinctions,header_predicted_extinctions)
;FOR i=0L,Ngrains-1 DO BEGIN
;	str_predicted_spectrum_grains[i]=ptr_new(dustem_spectra_st.sed.(i+1))
;ENDFOR
;headers_predicted_spectrum_grain=ptrarr(Ngrains)
;FOR i=0L,Ngrains-1 DO BEGIN
;	*str_predicted_spectrum_grains[i]=mrdfits(file,3+i+1,hh)
;	headers_predicted_spectrum_grain[i]=ptr_new(hh)
;ENDFOR
1cf58fd8   Jean-Philippe Bernard   First commit
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

;===== complement extension header for input SED
sxaddpar,header_input_sed,'TITLE','OBSERVED INPUT SED TO DUSTEMWRAP',before='COMMENT'
sxaddpar,header_input_sed,'TTYPE1','FILTER','Dustemwrap Filter name'
sxaddpar,header_input_sed,'TTYPE2','WAVELENGTH','Dustemwrap Filter reference wavelength [microns]'
sxaddpar,header_input_sed,'TTYPE3','I','Observed Total Stokes I Intensity [??]'
sxaddpar,header_input_sed,'TTYPE4','Q','Observed Stokes Q Intensity [??]'
sxaddpar,header_input_sed,'TTYPE5','U','Observed Stokes U Intensity [??]'
sxaddpar,header_input_sed,'TTYPE6','VARIANCEII','Observed Total Stokes I Variance [??]'
sxaddpar,header_input_sed,'TTYPE7','VARIANCEQQ','Observed Stokes Q Variance [??]'
sxaddpar,header_input_sed,'TTYPE8','VARIANCEUU','Observed Stokes U Variance [??]'

;===== complement extension header for predicted SED
sxaddpar,header_predicted_SED,'TITLE','OUTPUT BEST FIT SED BY DUSTEMWRAP'
sxaddpar,header_predicted_SED,'TTYPE1','FILTER','Dustemwrap Filter name'
sxaddpar,header_predicted_SED,'TTYPE2','WAVELENGTH','Dustemwrap Filter reference wavelength [microns]'
sxaddpar,header_predicted_SED,'TTYPE3','I','Observed Total Stokes I Intensity [??]'
sxaddpar,header_predicted_SED,'TTYPE4','Q','Observed Stokes Q Intensity [??]'
sxaddpar,header_predicted_SED,'TTYPE5','U','Observed Stokes U Intensity [??]'
sxaddpar,header_predicted_SED,'CHI2',best_fit_chi2,'Dustemwrap best fit chi2 [-]'
sxaddpar,header_predicted_SED,'RCHI2',best_fit_rchi2,'Dustemwrap best fit reduced chi2 [-]'
f2808c64   Jean-Philippe Bernard   improved
167
sxaddpar,header_predicted_SED,'NGRAINS',Ngrains,'Number of grain species'
1cf58fd8   Jean-Philippe Bernard   First commit
168
169
;====== write dustem wrap parameter names
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap parameter names ======',after='RCHI2'
f2808c64   Jean-Philippe Bernard   improved
170
sxaddpar,header_predicted_SED,'NPARAMS',Nparams,' Number of DustemWrap free parameters'
1cf58fd8   Jean-Philippe Bernard   First commit
171
172
173
174
FOR i=0L,Nparams-1 DO BEGIN
	sxaddpar,header_predicted_SED,'PARN'+strtrim(i+1,2),parameters_desc[i],' DustemWrap parameter name'
ENDFOR
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='PARN'+strtrim(i,2)
72089d7a   Jean-Philippe Bernard   improved to save ...
175
176
177
178
179
180

;====== write dustem wrap parameter best fit values
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap dust model used ======',after='COMMENT'
sxaddpar,header_predicted_SED,'MODEL',used_model,'Dust model used'
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='MODEL'

f2808c64   Jean-Philippe Bernard   improved
181
;====== write dustem wrap parameter best fit values
1cf58fd8   Jean-Philippe Bernard   First commit
182
183
184
185
186
187
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap parameter best fit values ======',after='COMMENT'
FOR i=0L,Nparams-1 DO BEGIN
	sxaddpar,header_predicted_SED,'PARV'+strtrim(i+1,2),best_parameters[i],'Param. '+parameters_desc[i]
ENDFOR
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='PARV'+strtrim(i,2)

f2808c64   Jean-Philippe Bernard   improved
188
189
190
191
192
;====== write dustem wrap parameter best fit values uncertainties
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap best fit parameter uncertainties ======',after='COMMENT'
FOR i=0L,Nparams-1 DO BEGIN
	sxaddpar,header_predicted_SED,'PARU'+strtrim(i+1,2),best_parameters_uncertainties[i],'Param. '+parameters_desc[i]
ENDFOR
72089d7a   Jean-Philippe Bernard   improved to save ...
193
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='PARU'+strtrim(i,2)
f2808c64   Jean-Philippe Bernard   improved
194
195
196
197
198
199

;====== write dustem wrap parameter initial fit values
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap parameter initial fit values ======',after='COMMENT'
FOR i=0L,Nparams-1 DO BEGIN
	sxaddpar,header_predicted_SED,'PARI'+strtrim(i+1,2),parameters_init_values[i],'Param. '+parameters_desc[i]
ENDFOR
72089d7a   Jean-Philippe Bernard   improved to save ...
200
201
202
203
204
205
206
207
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='PARI'+strtrim(i,2)

;====== write dustem wrap parameter func values (not sure exactly what this is)
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap parameter func values ======',after='COMMENT'
FOR i=0L,Nparams-1 DO BEGIN
	sxaddpar,header_predicted_SED,'PARF'+strtrim(i+1,2),parameters_func_values[i],'Param. '+parameters_desc[i]
ENDFOR
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='PARF'+strtrim(i,2)
f2808c64   Jean-Philippe Bernard   improved
208

1cf58fd8   Jean-Philippe Bernard   First commit
209
210
;===== complement extension header for predicted spectrum total
sxaddpar,header_predicted_spectrum_tot,'TITLE','OUTPUT BEST FIT TOTAL SPECTRUM BY DUSTEMWRAP'
f2808c64   Jean-Philippe Bernard   improved
211
212
213
214
215
216
217
218
sxaddpar,header_predicted_spectra,'TITLE','OUTPUT BEST FIT PER-GRAIN AND TOTAL EMISSION SPECTRA BY DUSTEMWRAP'
sxaddpar,header_predicted_extinctions,'TITLE','OUTPUT BEST FIT PER-GRAIN AND TOTAL EXTINCTION BY DUSTEMWRAP'

;FOR i=0L,Ngrains-1 DO BEGIN
;	hh=*headers_predicted_spectrum_grain[i]
;	sxaddpar,hh,'TITLE','OUTPUT BEST FIT SPECTRUM BY DUSTEMWRAP FOR GRAIN '+strtrim(i+1,2)
;	*headers_predicted_spectrum_grain[i]=hh
;ENDFOR
1cf58fd8   Jean-Philippe Bernard   First commit
219
220
221
222
223

;write final file
IF keyword_set(filename) THEN BEGIN
	file=filename
ENDIF ELSE BEGIN
3b2e1c40   Annie Hughes   updated default f...
224
	file='./dustemwrap_results.fits'
1cf58fd8   Jean-Philippe Bernard   First commit
225
226
227
228
ENDELSE

;==== Write the observed SED in extension 1
mwrfits,str_input_SED,file,header_input_sed,/create
f2808c64   Jean-Philippe Bernard   improved
229
unit=unit+1
1cf58fd8   Jean-Philippe Bernard   First commit
230
231
232
;==== add the predicted SED in extension 2
mwrfits,str_predicted_SED,file,header_predicted_SED
;==== add the predicted total spectrum in extension 3
f2808c64   Jean-Philippe Bernard   improved
233
234
235
236
237
238
239
240
241
242
243
;mwrfits,str_predicted_spectrum_tot,file,header_predicted_spectrum_tot
;==== add the predicted spectrum for each grain type in extension 4 - 4+Ngrains
;FOR i=0L,Ngrains-1 DO BEGIN
;	mwrfits,*str_predicted_spectrum_grains[i],file,*headers_predicted_spectrum_grain[i]
;ENDFOR
;==== add the predicted per-grain spectrum in extension 3
;stop
mwrfits,dustem_spectra_st.sed,file,header_predicted_spectra
;==== add the predicted per-grain extinction in extension 4
mwrfits,dustem_spectra_st.ext,file,header_predicted_extinctions

1cf58fd8   Jean-Philippe Bernard   First commit
244
245
246

message,'Wrote '+file,/info

f2808c64   Jean-Philippe Bernard   improved
247
248
249
;stop
;hprint,header_input_sed
;hprint,header_predicted_SED
1cf58fd8   Jean-Philippe Bernard   First commit
250
251
252
253
254


the_end:

END