Blame view

src/idl/dustem_write_fits_table.pro 11.5 KB
1cf58fd8   Jean-Philippe Bernard   First commit
1
2
3
4
5
PRO dustem_write_fits_table,filename=filename,help=help

;+
; NAME:
;    dustem_write_fits_table
fdf4c603   Annie Hughes   cosmetic changes ...
6
;
1cf58fd8   Jean-Philippe Bernard   First commit
7
; PURPOSE:
201cf7e2   Annie Hughes   minor changes to ...
8
;    Write a FITS table with content of current DustEMWrap system variables
fdf4c603   Annie Hughes   cosmetic changes ...
9
;
1cf58fd8   Jean-Philippe Bernard   First commit
10
; CATEGORY:
201cf7e2   Annie Hughes   minor changes to ...
11
;    DustEMWrap, Distributed, High-Level, User Convenience
fdf4c603   Annie Hughes   cosmetic changes ...
12
;
1cf58fd8   Jean-Philippe Bernard   First commit
13
14
; CALLING SEQUENCE:
;    dustem_write_fits_table,file[,/help]
fdf4c603   Annie Hughes   cosmetic changes ...
15
;
1cf58fd8   Jean-Philippe Bernard   First commit
16
; INPUTS:
201cf7e2   Annie Hughes   minor changes to ...
17
;    file      = File name to be written (default='./dustemwrap_results.fits')
fdf4c603   Annie Hughes   cosmetic changes ...
18
;
1cf58fd8   Jean-Philippe Bernard   First commit
19
20
; OPTIONAL INPUT PARAMETERS:
;    None
fdf4c603   Annie Hughes   cosmetic changes ...
21
;
1cf58fd8   Jean-Philippe Bernard   First commit
22
23
; OUTPUTS:
;    None
fdf4c603   Annie Hughes   cosmetic changes ...
24
;
1cf58fd8   Jean-Philippe Bernard   First commit
25
26
; OPTIONAL OUTPUT PARAMETERS:
;    None
fdf4c603   Annie Hughes   cosmetic changes ...
27
;
1cf58fd8   Jean-Philippe Bernard   First commit
28
29
; ACCEPTED KEY-WORDS:
;    help      = If set, print this help
fdf4c603   Annie Hughes   cosmetic changes ...
30
;
1cf58fd8   Jean-Philippe Bernard   First commit
31
32
; COMMON BLOCKS:
;    None
fdf4c603   Annie Hughes   cosmetic changes ...
33
;
1cf58fd8   Jean-Philippe Bernard   First commit
34
; SIDE EFFECTS:
201cf7e2   Annie Hughes   minor changes to ...
35
;    Output FITS file is written to disk.
fdf4c603   Annie Hughes   cosmetic changes ...
36
;
1cf58fd8   Jean-Philippe Bernard   First commit
37
; RESTRICTIONS:
201cf7e2   Annie Hughes   minor changes to ...
38
;    The DustEMWrap IDL code must be installed
fdf4c603   Annie Hughes   cosmetic changes ...
39
;
1cf58fd8   Jean-Philippe Bernard   First commit
40
41
; PROCEDURE:
;    None
fdf4c603   Annie Hughes   cosmetic changes ...
42
;
1cf58fd8   Jean-Philippe Bernard   First commit
43
44
45
; EXAMPLES
;    
; MODIFICATION HISTORY:
1cf2bf59   Annie Hughes   updated dustem_da...
46
47
48
;    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
49
50
;-

1cf58fd8   Jean-Philippe Bernard   First commit
51
52
53
54
55
56
57

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

;===== run dustem to get predicted spectrum and sed
8ddee2db   Jean-Philippe Bernard   coping for the fa...
58
59
60
61
62
IF ptr_valid((*!dustem_data).sed) THEN BEGIN
	stop
	dustem_predicted_sed=dustem_compute_sed(*(*!dustem_fit).current_param_values,out_st=dustem_spectra_st)
ENDIF ELSE BEGIN
ENDELSE
fdf4c603   Annie Hughes   cosmetic changes ...
63

3a8e4128   Jean-Philippe Bernard   fixed writing pol...
64
65
IF !run_pol THEN BEGIN
;This is needed to compute Q,U of both SED and spectra
af3a1896   Jean-Philippe Bernard   improved
66
   toto=dustem_compute_stokes(*(*!dustem_fit).current_param_values, $
f9065c74   Jean-Philippe Bernard   adapted for new k...
67
                                                 Q_spec=dustem_predicted_Q_spec,U_spec=dustem_predicted_U_spec,PSI_spec=dustem_predicted_PSI_spec, $
af3a1896   Jean-Philippe Bernard   improved
68
                                                 dustem_psi_em=dustem_psi_em)
f9065c74   Jean-Philippe Bernard   adapted for new k...
69
70
   	dustem_predicted_Qsed=toto[0]
    dustem_predicted_Used=toto[1]
fdf4c603   Annie Hughes   cosmetic changes ...
71
;Same in extinction, but does not work in polarization extinction was not fitted.
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
72
73
74
75
76
;   dustem_predicted_polext=dustem_compute_stokext(*(*!dustem_fit).current_param_values,bidon, $
;											  dustem_predicted_Qext,dustem_predicted_Uext, $
;											  dustem_predicted_Qext_spec,dustem_predicted_Uext_spec,dustem_predicted_PSI_ext, $
;											  dustem_psi_ext,out_st=out_st)
ENDIF
1cf58fd8   Jean-Philippe Bernard   First commit
77

72089d7a   Jean-Philippe Bernard   improved to save ...
78
79
used_model=!dustem_model

f2808c64   Jean-Philippe Bernard   improved
80
81
Ngrains=n_tags(dustem_spectra_st.sed)-2

1cf58fd8   Jean-Philippe Bernard   First commit
82
;===== define structures containing results
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
83
84
one_str_input_SED={FILTER:'',Wavelength:la_undef(4),I:la_undef(5),Q:la_undef(5),U:la_undef(5),varianceII:la_undef(5),varianceQQ:la_undef(5),varianceUU:la_undef(5)}
one_str_predicted_SED={FILTER:'',Wavelength:la_undef(4),I:la_undef(5),Q:la_undef(5),U:la_undef(5)}
f2808c64   Jean-Philippe Bernard   improved
85
86
;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
87

1cf2bf59   Annie Hughes   updated dustem_da...
88
Nsed=n_elements((*(*!dustem_data).sed).filt_names)
1cf58fd8   Jean-Philippe Bernard   First commit
89
str_input_SED=replicate(one_str_input_SED,Nsed)
1cf2bf59   Annie Hughes   updated dustem_da...
90
91
92
93
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
94
95
96

N_predicted_sed=n_elements(dustem_predicted_sed)
str_predicted_SED=replicate(one_str_predicted_SED,N_predicted_sed)
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
97
98
str_predicted_EXT=replicate(one_str_predicted_SED,N_predicted_sed)
fully_undefined_sed=str_predicted_SED
1cf2bf59   Annie Hughes   updated dustem_da...
99
100
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
fdf4c603   Annie Hughes   cosmetic changes ...
101
str_predicted_SED.I=dustem_predicted_sed    
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
102
103
104
105
106
107
108
109
110
111
112
113
;stop
IF !run_pol THEN BEGIN
	str_predicted_SED.Q=dustem_predicted_Qsed
	str_predicted_SED.U=dustem_predicted_Used
	;str_predicted_EXT.Q=dustem_predicted_Qext
	;str_predicted_EXT.U=dustem_predicted_Uext
ENDIF ELSE BEGIN
	str_predicted_SED.Q=fully_undefined_sed.Q
	str_predicted_SED.U=fully_undefined_sed.U
	;str_predicted_EXT.Q=fully_undefined_sed.Q
	;str_predicted_EXT.U=fully_undefined_sed.U
ENDELSE
1cf58fd8   Jean-Philippe Bernard   First commit
114
115

N_predicted_spectra=n_elements(dustem_spectra_st.sed)
1cf58fd8   Jean-Philippe Bernard   First commit
116
117
118
119
120
121
122
123

;===== 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 ...
124
parameters_func_values=*(*!dustem_fit).PARAM_FUNC
1cf58fd8   Jean-Philippe Bernard   First commit
125
126
127

Nparams=n_elements(parameters_desc)

f2808c64   Jean-Philippe Bernard   improved
128
129
130
131
132
;unit_input_sed=1
;unit_predicted_sed=2
;unit_predicted_spectra=3
;unit_predicted_extinctions=4

1cf58fd8   Jean-Philippe Bernard   First commit
133
;stop
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
134
135
136
137
138
139
;This is what will be saved
;help,str_input_SED,/str
;help,str_predicted_SED,/str
;help,dustem_spectra_st.sed,/str
;help,dustem_spectra_st.ext,/str

3b2e1c40   Annie Hughes   updated default f...
140
file='./dustemwrap_tmp.fits'
1cf58fd8   Jean-Philippe Bernard   First commit
141
;==== Write the observed SED in extension 1
f2808c64   Jean-Philippe Bernard   improved
142
unit=1L
1cf58fd8   Jean-Philippe Bernard   First commit
143
mwrfits,str_input_SED,file,/create
f2808c64   Jean-Philippe Bernard   improved
144
145
unit_input_sed=unit
unit=unit+1
1cf58fd8   Jean-Philippe Bernard   First commit
146
147
;==== add the predicted SED in extension 2
mwrfits,str_predicted_SED,file
f2808c64   Jean-Philippe Bernard   improved
148
149
unit_predicted_sed=unit
unit=unit+1
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
150
;==== add the predicted per-grain spectrum in extension 3
f2808c64   Jean-Philippe Bernard   improved
151
152
153
mwrfits,dustem_spectra_st.sed,file
unit_predicted_spectra=unit
unit=unit+1
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
154
;==== add the predicted per-grain extinction in extension 4
f2808c64   Jean-Philippe Bernard   improved
155
156
mwrfits,dustem_spectra_st.ext,file
unit_predicted_extinctions=unit
f2808c64   Jean-Philippe Bernard   improved
157
158
159

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

;===== read back extensions to get proper extension headers
3b2e1c40   Annie Hughes   updated default f...
162
file='./dustemwrap_tmp.fits'
f2808c64   Jean-Philippe Bernard   improved
163
164
165
166
167
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)
1cf58fd8   Jean-Philippe Bernard   First commit
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

;===== 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
189
sxaddpar,header_predicted_SED,'NGRAINS',Ngrains,'Number of grain species'
1cf58fd8   Jean-Philippe Bernard   First commit
190
191
;====== write dustem wrap parameter names
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap parameter names ======',after='RCHI2'
f2808c64   Jean-Philippe Bernard   improved
192
sxaddpar,header_predicted_SED,'NPARAMS',Nparams,' Number of DustemWrap free parameters'
1cf58fd8   Jean-Philippe Bernard   First commit
193
194
195
196
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 ...
197
198
199
200
201
202

;====== 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'

3a8e4128   Jean-Philippe Bernard   fixed writing pol...
203
204
205
206
sxaddpar,header_predicted_SED,'COMMENT','======= Dustemwrap dust model used in polarisation ? ======',after='COMMENT'
sxaddpar,header_predicted_SED,'POL',!run_pol,'1 if pol was used, 0 otherwise'
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='POL'

f2808c64   Jean-Philippe Bernard   improved
207
;====== write dustem wrap parameter best fit values
1cf58fd8   Jean-Philippe Bernard   First commit
208
209
210
211
212
213
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
214
215
216
217
218
;====== 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 ...
219
sxaddpar,header_predicted_SED,'COMMENT','===================================================',after='PARU'+strtrim(i,2)
f2808c64   Jean-Philippe Bernard   improved
220
221
222
223
224
225

;====== 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 ...
226
227
228
229
230
231
232
233
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
234

1cf58fd8   Jean-Philippe Bernard   First commit
235
;===== complement extension header for predicted spectrum total
fdf4c603   Annie Hughes   cosmetic changes ...
236
;sxaddpar,header_predicted_spectrum_tot,'TITLE','OUTPUT BEST FIT TOTAL SPECTRUM BY DUSTEMWRAP'
f2808c64   Jean-Philippe Bernard   improved
237
238
239
240
241
242
243
244
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
245

3a8e4128   Jean-Philippe Bernard   fixed writing pol...
246
247
248
249
;stop
;hprint,header_input_sed
;hprint,header_predicted_SED

1cf58fd8   Jean-Philippe Bernard   First commit
250
251
252
253
;write final file
IF keyword_set(filename) THEN BEGIN
	file=filename
ENDIF ELSE BEGIN
3b2e1c40   Annie Hughes   updated default f...
254
	file='./dustemwrap_results.fits'
1cf58fd8   Jean-Philippe Bernard   First commit
255
256
257
258
ENDELSE

;==== Write the observed SED in extension 1
mwrfits,str_input_SED,file,header_input_sed,/create
f2808c64   Jean-Philippe Bernard   improved
259
unit=unit+1
1cf58fd8   Jean-Philippe Bernard   First commit
260
261
262
;==== 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
263
264
265
266
267
268
269
270
271
272
273
;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
274
275
276

message,'Wrote '+file,/info

f2808c64   Jean-Philippe Bernard   improved
277
278
279
;stop
;hprint,header_input_sed
;hprint,header_predicted_SED
1cf58fd8   Jean-Philippe Bernard   First commit
280
281
282
283
284


the_end:

END