Blame view

src/idl/dustem_write_fits_table.pro 18.3 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:
4e29f522   Jean-Philippe Bernard   fixed issue when ...
17
;    filename   = 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.
17f02a5a   Jean-Philippe Bernard   updated
36
;    All dustem system variables are reinitialized
52f17b34   Annie Hughes   fixes for reading...
37
;
1cf58fd8   Jean-Philippe Bernard   First commit
38
; RESTRICTIONS:
201cf7e2   Annie Hughes   minor changes to ...
39
;    The DustEMWrap IDL code must be installed
52f17b34   Annie Hughes   fixes for reading...
40
;
1cf58fd8   Jean-Philippe Bernard   First commit
41
42
; PROCEDURE:
;    None
52f17b34   Annie Hughes   fixes for reading...
43
;
1cf58fd8   Jean-Philippe Bernard   First commit
44
; EXAMPLES
52f17b34   Annie Hughes   fixes for reading...
45
;
1cf58fd8   Jean-Philippe Bernard   First commit
46
; MODIFICATION HISTORY:
1cf2bf59   Annie Hughes   updated dustem_da...
47
48
49
;    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
50
51
;-

1cf58fd8   Jean-Philippe Bernard   First commit
52
53
54
55
56
IF keyword_set(help) THEN BEGIN
  doc_library,'dustem_write_fits_table'
  goto,the_end
ENDIF

762cbe48   Jean-Philippe Bernard   modifed to cope f...
57
58
59
60
61
62
63
64
used_model=!dustem_model

emission_sed_present=ptr_valid((*!dustem_data).sed)
extinction_sed_present=ptr_valid((*!dustem_data).ext)
;dustem_activate_plugins,p_dim/(*(*!dustem_fit).param_init_values),st=st  
;fact = 1.e4*(*!dustem_HCD)/(4.*!pi)/(3.e8/1.e-6/st.sed.wav)*1.e20/1.e7 ;this is correct despite the way it is presented (1.e4/1e.7*1e.20=1.e17 which is the factor to convert from ergs to Mega Janskys)
;SED_spec = st.sed.em_tot * fact

b7dca888   Jean-Philippe Bernard   improved to recov...
65
66
;stop

1cf58fd8   Jean-Philippe Bernard   First commit
67
;===== run dustem to get predicted spectrum and sed
762cbe48   Jean-Philippe Bernard   modifed to cope f...
68
IF emission_sed_present THEN BEGIN
a78ee4de   Annie Hughes   unknown
69
70
   dustem_predicted_sed=dustem_compute_sed(*(*!dustem_fit).current_param_values,st=dustem_st)
   
762cbe48   Jean-Philippe Bernard   modifed to cope f...
71
72
73
74
75
76
77
78
	IF !run_pol THEN BEGIN
	;This is needed to compute Q,U of both SED and spectra
	   dustem_predicted_polsed=dustem_compute_stokes(*(*!dustem_fit).current_param_values, $
	                                                 Q_spec=dustem_predicted_Q_spec,U_spec=dustem_predicted_U_spec,PSI_spec=dustem_predicted_PSI_spec, $
	                                                 dustem_psi_em=dustem_psi_em)
	   dustem_predicted_Qsed=dustem_predicted_polsed[0]
	   dustem_predicted_Used=dustem_predicted_polsed[1]
	ENDIF
b7dca888   Jean-Philippe Bernard   improved to recov...
79
80
81
82
83
84
85
86
    str_predicted_SED=dustem_make_fits_predicted_sed(*!dustem_data,dustem_predicted_sed $
    												,str_input_sed=str_input_sed $
    												,dustem_predicted_Qsed=dustem_predicted_Qsed $
    												,dustem_predicted_Used=dustem_predicted_Used)
    str_predicted_shown_SED=dustem_make_fits_predicted_sed(*!dustem_show,dustem_predicted_sed $
    												,str_input_sed=str_shown_sed $
    												,dustem_predicted_Qsed=dustem_predicted_Qsed $
    												,dustem_predicted_Used=dustem_predicted_Used)
3a8e4128   Jean-Philippe Bernard   fixed writing pol...
87
ENDIF
1cf58fd8   Jean-Philippe Bernard   First commit
88

762cbe48   Jean-Philippe Bernard   modifed to cope f...
89
90
91
92
93
94
IF extinction_sed_present THEN BEGIN
	;stop
	dustem_predicted_ext=dustem_compute_ext(*(*!dustem_fit).current_param_values,st=dustem_st)
	IF !run_pol THEN BEGIN
	;This is needed to compute Q,U of both SED and spectra
	   dustem_predicted_polext=dustem_compute_stokext((*!dustem_fit).current_param_values,st=dustem_st)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
95
96
97
	   dustem_predicted_Qext=dustem_predicted_polext[0]
	   dustem_predicted_Uext=dustem_predicted_polext[1]
	ENDIF
b7dca888   Jean-Philippe Bernard   improved to recov...
98
99
100
101
102
103
104
105
    str_predicted_EXT=dustem_make_fits_predicted_ext(*!dustem_data,dustem_predicted_ext $
    												,dustem_predicted_Qext=dustem_predicted_Qext $
    												,dustem_predicted_Uext=dustem_predicted_Uext $
    												,str_input_ext=str_input_ext)
    str_predicted_shown_EXT=dustem_make_fits_predicted_ext(*!dustem_show,dustem_predicted_ext $
    												,dustem_predicted_Qext=dustem_predicted_Qext $
    												,dustem_predicted_Uext=dustem_predicted_Uext $    	
    												,str_input_ext=str_shown_ext)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
106
107
108
109
ENDIF

Ngrains=n_tags(dustem_st.sed)-2

1cf58fd8   Jean-Philippe Bernard   First commit
110
111
112
113
;===== 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
b183ec5a   Annie Hughes   allowed for writi...
114
115
best_parameters_uncertainties=best_parameters*0.0 ; in case you are saving output without running a fit
IF ptr_valid((*!dustem_fit).current_param_errors) THEN best_parameters_uncertainties=*(*!dustem_fit).CURRENT_PARAM_ERRORS
1cf58fd8   Jean-Philippe Bernard   First commit
116
117
best_fit_chi2=(*!dustem_fit).CHI2
best_fit_rchi2=(*!dustem_fit).RCHI2
72089d7a   Jean-Philippe Bernard   improved to save ...
118
parameters_func_values=*(*!dustem_fit).PARAM_FUNC
f53f5e45   Jean-Philippe Bernard   not sure what
119
120
121
122
123
124
125
126
127
IF ptr_valid((*!dustem_fit).fixed_param_descs) THEN BEGIN
	there_are_fixed_params=1
	fixed_parameters_desc=*(*!dustem_fit).fixed_param_descs
	fixed_parameters_values=*(*!dustem_fit).fixed_param_init_values
	Nfixedparams=n_elements(fixed_parameters_desc)
ENDIF ELSE BEGIN
	there_are_fixed_params=0
	Nfixedparams=0
ENDELSE
1cf58fd8   Jean-Philippe Bernard   First commit
128
129
130

Nparams=n_elements(parameters_desc)

3b2e1c40   Annie Hughes   updated default f...
131
file='./dustemwrap_tmp.fits'
1cf58fd8   Jean-Philippe Bernard   First commit
132
;==== Write the observed SED in extension 1
f2808c64   Jean-Philippe Bernard   improved
133
unit=1L
762cbe48   Jean-Philippe Bernard   modifed to cope f...
134
135
136
137
138
139
140
141
142
143
144
;Yes, toto does not exist. This is just to write a dummy input to allow for a general header
mwrfits,toto,file,/create
IF emission_sed_present THEN BEGIN
	;==== add the input emission SED
	mwrfits,str_input_SED,file
	unit_input_sed=unit
	unit=unit+1
	;==== add the predicted emission SED
	mwrfits,str_predicted_SED,file
	unit_predicted_sed=unit
	unit=unit+1
17f02a5a   Jean-Philippe Bernard   updated
145
146
147
148
	;==== add the shown emission SED
	mwrfits,str_shown_SED,file
	unit_shown_sed=unit
	unit=unit+1
762cbe48   Jean-Philippe Bernard   modifed to cope f...
149
150
151
ENDIF ELSE BEGIN
	unit_input_sed=-1
	unit_predicted_sed=-1
17f02a5a   Jean-Philippe Bernard   updated
152
	unit_shown_sed=-1
762cbe48   Jean-Philippe Bernard   modifed to cope f...
153
154
155
156
157
158
ENDELSE
IF extinction_sed_present THEN BEGIN
	;stop
	mwrfits,str_input_EXT,file
	unit_input_ext=unit
	unit=unit+1
17f02a5a   Jean-Philippe Bernard   updated
159
	;==== add the predicted extinction SED
762cbe48   Jean-Philippe Bernard   modifed to cope f...
160
161
162
	mwrfits,str_predicted_EXT,file
	unit_predicted_ext=unit
	unit=unit+1
17f02a5a   Jean-Philippe Bernard   updated
163
164
165
166
	;==== add the shown extinction SED
	mwrfits,str_shown_EXT,file
	unit_shown_ext=unit
	unit=unit+1
762cbe48   Jean-Philippe Bernard   modifed to cope f...
167
168
169
ENDIF ELSE BEGIN
	unit_input_ext=-1
	unit_predicted_ext=-1
17f02a5a   Jean-Philippe Bernard   updated
170
	unit_shown_ext=-1
762cbe48   Jean-Philippe Bernard   modifed to cope f...
171
172
173
ENDELSE
;==== add the predicted per-grain emission spectrum
mwrfits,dustem_st.sed,file
f2808c64   Jean-Philippe Bernard   improved
174
175
unit_predicted_spectra=unit
unit=unit+1
762cbe48   Jean-Philippe Bernard   modifed to cope f...
176
177
;==== add the predicted per-grain extinction spectrum
mwrfits,dustem_st.ext,file
f2808c64   Jean-Philippe Bernard   improved
178
unit_predicted_extinctions=unit
762cbe48   Jean-Philippe Bernard   modifed to cope f...
179
180
181
182
183
184
185
186
187
188
189
190
unit=unit+1
IF !run_pol EQ 1 THEN BEGIN
	mwrfits,dustem_st.polsed,file
	unit_predicted_spectra_pol=unit
	unit=unit+1
	mwrfits,dustem_st.polext,file
	unit_predicted_extinctions_pol=unit
	unit=unit+1
ENDIF ELSE BEGIN
	unit_predicted_spectra_pol=-1
	unit_predicted_extinctions_pol=-1
ENDELSE
8f83f26b   Jean-Philippe Bernard   added saving plug...
191
192
193
194
195
196
197
198
199
200
201
202
;==== add the pluggins spectra
plugin_tags=tag_names(*!dustem_plugin)
ind=where(plugin_tags NE 'NONE',Nplugins)
IF Nplugins NE 0 THEN BEGIN
	unit_plugins=lonarr(Nplugins)
	str_plugins=strarr(Nplugins)
	FOR i=0L,Nplugins-1 DO BEGIN
		mwrfits,*((*!dustem_plugin).(i).spec),file
		unit_plugins[i]=unit
		str_plugins[i]=plugin_tags[i]
		unit=unit+1
	ENDFOR
d3d05d43   Jean-Philippe Bernard   dealt with case o...
203
204
205
ENDIF ELSE BEGIN  ;This is the case where there are no plugins involved
	unit_plugins=[-1]
	str_plugins=['']
8f83f26b   Jean-Philippe Bernard   added saving plug...
206
ENDELSE
f2808c64   Jean-Philippe Bernard   improved
207

17f02a5a   Jean-Philippe Bernard   updated
208
209
extension_numbers=[unit_input_sed,unit_predicted_sed,unit_shown_sed, $
				   unit_input_ext,unit_predicted_ext,unit_shown_ext, $
762cbe48   Jean-Philippe Bernard   modifed to cope f...
210
211
				   unit_predicted_spectra,unit_predicted_extinctions, $
				   unit_predicted_spectra_pol,unit_predicted_extinctions_pol]
17f02a5a   Jean-Philippe Bernard   updated
212
213
extensions_strs=['INPUT_EMISSION_SED','DUSTEM_PREDICTED_SED','DUSTEM_SHOWN_SED', $
				 'INPUT_EXTINCTION_SED','DUSTEM_PREDICTED_EXTINCTION_SED','DUSTEM_SHOWN_EXTINCTION_SED', $
762cbe48   Jean-Philippe Bernard   modifed to cope f...
214
215
216
				 'DUSTEM_PREDICTED_EMISSION_SPECTRA','DUSTEM_PREDICTED_EXTINCTION_SPECTRA', $
				 'DUSTEM_PREDICTED_EMISSION_SPECTRA_POL','DUSTEM_PREDICTED_EXTINCTION_SPECTRA_POL']

8f83f26b   Jean-Philippe Bernard   added saving plug...
217
218
219
extension_numbers=[extension_numbers,unit_plugins]
extensions_strs=[extensions_strs,'PLUGIN_'+str_plugins]

762cbe48   Jean-Philippe Bernard   modifed to cope f...
220
221
222
223
224
225
226
ind=where(extension_numbers NE -1,count)
extension_numbers=extension_numbers[ind]
extensions_strs=extensions_strs[ind]
order=sort(extension_numbers)
extension_numbers=extension_numbers[order]
extensions_strs=extensions_strs[order]
Nextensions=n_elements(extension_numbers)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
227
228

;===== read back fits file to get proper extension headers
3b2e1c40   Annie Hughes   updated default f...
229
file='./dustemwrap_tmp.fits'
762cbe48   Jean-Philippe Bernard   modifed to cope f...
230
toto=mrdfits(file,0,hdr)      ;this would be to get the main header
762cbe48   Jean-Philippe Bernard   modifed to cope f...
231
232
IF unit_input_sed NE -1 THEN sst_input_sed=mrdfits(file,unit_input_sed,header_input_sed)
IF unit_predicted_sed NE -1 THEN sst_predicted_SED=mrdfits(file,unit_predicted_sed,header_predicted_SED)
17f02a5a   Jean-Philippe Bernard   updated
233
IF unit_shown_sed NE -1 THEN sst_shown_SED=mrdfits(file,unit_shown_sed,header_shown_SED)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
234
235
IF unit_input_ext NE -1 THEN sst_input_ext=mrdfits(file,unit_input_ext,header_input_ext)
IF unit_predicted_ext NE -1 THEN sst_predicted_ext=mrdfits(file,unit_predicted_ext,header_predicted_ext)
17f02a5a   Jean-Philippe Bernard   updated
236
IF unit_shown_ext NE -1 THEN sst_shown_ext=mrdfits(file,unit_shown_ext,header_shown_ext)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
237
238
239
240
241
242
dustem_st.sed=mrdfits(file,unit_predicted_spectra,header_predicted_spectra)
dustem_st.ext=mrdfits(file,unit_predicted_extinctions,header_predicted_extinctions)
IF !run_pol EQ 1 THEN BEGIN
	dustem_st.polsed=mrdfits(file,unit_predicted_spectra_pol,header_predicted_spectra)
	dustem_st.polext=mrdfits(file,unit_predicted_extinctions_pol,header_predicted_extinctions)
ENDIF
8f83f26b   Jean-Philippe Bernard   added saving plug...
243
244
245
246
247
248
249
IF Nplugins NE 0 THEN BEGIN
	header_plugins=ptrarr(Nplugins)
	FOR i=0L,Nplugins-1 DO BEGIN
		a=mrdfits(file,unit_plugins[i],header_plugin)
		header_plugins[i]=ptr_new(header_plugin)
	ENDFOR
ENDIF
762cbe48   Jean-Philippe Bernard   modifed to cope f...
250
251
252
253
;===== write content of extensions in main header
FOR i=0L,Nextensions-1 DO BEGIN
	sxaddpar,hdr,'EXT'+strtrim(i+1,2),extensions_strs[i],'fits file extension'
ENDFOR
1cf58fd8   Jean-Philippe Bernard   First commit
254

762cbe48   Jean-Philippe Bernard   modifed to cope f...
255
256
257
258
259
260
261
262
;====== write dustem wrap parameter best fit values
sxaddpar,hdr,'COMMENT','======= Dustemwrap dust model used ======',after='COMMENT'
sxaddpar,hdr,'MODEL',used_model,'Dust model used'
sxaddpar,hdr,'WRAP_V',!dustem_version.version,'Dustwrap version used'
sxaddpar,hdr,'COMMENT','===================================================',after='MODEL'
sxaddpar,hdr,'CHI2',best_fit_chi2,'Dustemwrap best fit chi2 [-]'
sxaddpar,hdr,'RCHI2',best_fit_rchi2,'Dustemwrap best fit reduced chi2 [-]'
sxaddpar,hdr,'NGRAINS',Ngrains,'Number of grain species'
8f83f26b   Jean-Philippe Bernard   added saving plug...
263
264
265
266
267
sxaddpar,hdr,'COMMENT','======= Dustemwrap plugins names ======',after='NGRAINS'
sxaddpar,hdr,'NPLUG',Nplugins,'Number of plugins involved'
FOR i=0L,Nplugins-1 DO BEGIN
	sxaddpar,hdr,'PLUG'+strtrim(i+1,2),str_plugins[i],'PLUGIN name'
ENDFOR
1cf58fd8   Jean-Philippe Bernard   First commit
268
;====== write dustem wrap parameter names
8f83f26b   Jean-Philippe Bernard   added saving plug...
269
sxaddpar,hdr,'COMMENT','======= Dustemwrap parameter names ======',after='PLUG'+strtrim(i+1,2)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
270
sxaddpar,hdr,'NPARAMS',Nparams,' Number of DustemWrap free parameters'
1cf58fd8   Jean-Philippe Bernard   First commit
271
FOR i=0L,Nparams-1 DO BEGIN
762cbe48   Jean-Philippe Bernard   modifed to cope f...
272
	sxaddpar,hdr,'PARN'+strtrim(i+1,2),parameters_desc[i],' DustemWrap parameter name'
1cf58fd8   Jean-Philippe Bernard   First commit
273
ENDFOR
762cbe48   Jean-Philippe Bernard   modifed to cope f...
274
275
276
277
sxaddpar,hdr,'COMMENT','===================================================',after='PARN'+strtrim(i,2)
sxaddpar,hdr,'COMMENT','======= Dustemwrap dust model used in polarisation ? ======',after='COMMENT'
sxaddpar,hdr,'POL',!run_pol,'1 if pol was used, 0 otherwise'
sxaddpar,hdr,'COMMENT','===================================================',after='POL'
f2808c64   Jean-Philippe Bernard   improved
278
;====== write dustem wrap parameter best fit values
762cbe48   Jean-Philippe Bernard   modifed to cope f...
279
sxaddpar,hdr,'COMMENT','======= Dustemwrap parameter best fit values ======',after='COMMENT'
1cf58fd8   Jean-Philippe Bernard   First commit
280
FOR i=0L,Nparams-1 DO BEGIN
762cbe48   Jean-Philippe Bernard   modifed to cope f...
281
	sxaddpar,hdr,'PARV'+strtrim(i+1,2),best_parameters[i],'Param. '+parameters_desc[i]
1cf58fd8   Jean-Philippe Bernard   First commit
282
ENDFOR
762cbe48   Jean-Philippe Bernard   modifed to cope f...
283
sxaddpar,hdr,'COMMENT','===================================================',after='PARV'+strtrim(i,2)
1cf58fd8   Jean-Philippe Bernard   First commit
284

f2808c64   Jean-Philippe Bernard   improved
285
;====== write dustem wrap parameter best fit values uncertainties
762cbe48   Jean-Philippe Bernard   modifed to cope f...
286
sxaddpar,hdr,'COMMENT','======= Dustemwrap best fit parameter uncertainties ======',after='COMMENT'
f2808c64   Jean-Philippe Bernard   improved
287
FOR i=0L,Nparams-1 DO BEGIN
762cbe48   Jean-Philippe Bernard   modifed to cope f...
288
	sxaddpar,hdr,'PARU'+strtrim(i+1,2),best_parameters_uncertainties[i],'Param. '+parameters_desc[i]
f2808c64   Jean-Philippe Bernard   improved
289
ENDFOR
762cbe48   Jean-Philippe Bernard   modifed to cope f...
290
sxaddpar,hdr,'COMMENT','===================================================',after='PARU'+strtrim(i,2)
f2808c64   Jean-Philippe Bernard   improved
291
292

;====== write dustem wrap parameter initial fit values
762cbe48   Jean-Philippe Bernard   modifed to cope f...
293
sxaddpar,hdr,'COMMENT','======= Dustemwrap parameter initial fit values ======',after='COMMENT'
f2808c64   Jean-Philippe Bernard   improved
294
FOR i=0L,Nparams-1 DO BEGIN
762cbe48   Jean-Philippe Bernard   modifed to cope f...
295
	sxaddpar,hdr,'PARI'+strtrim(i+1,2),parameters_init_values[i],'Param. '+parameters_desc[i]
f2808c64   Jean-Philippe Bernard   improved
296
ENDFOR
762cbe48   Jean-Philippe Bernard   modifed to cope f...
297
sxaddpar,hdr,'COMMENT','===================================================',after='PARI'+strtrim(i,2)
72089d7a   Jean-Philippe Bernard   improved to save ...
298
299

;====== write dustem wrap parameter func values (not sure exactly what this is)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
300
sxaddpar,hdr,'COMMENT','======= Dustemwrap parameter func values ======',after='COMMENT'
72089d7a   Jean-Philippe Bernard   improved to save ...
301
FOR i=0L,Nparams-1 DO BEGIN
762cbe48   Jean-Philippe Bernard   modifed to cope f...
302
	sxaddpar,hdr,'PARF'+strtrim(i+1,2),parameters_func_values[i],'Param. '+parameters_desc[i]
72089d7a   Jean-Philippe Bernard   improved to save ...
303
ENDFOR
762cbe48   Jean-Philippe Bernard   modifed to cope f...
304
sxaddpar,hdr,'COMMENT','===================================================',after='PARF'+strtrim(i,2)
f53f5e45   Jean-Philippe Bernard   not sure what
305
306
307
308
309
310
311
312
313
sxaddpar,hdr,'NFPARAMS',Nfixedparams,' Number of DustemWrap fixed parameters'
FOR i=0L,Nfixedparams-1 DO BEGIN
	sxaddpar,hdr,'FPARN'+strtrim(i+1,2),fixed_parameters_desc[i],' Fixed DustemWrap parameter name'
ENDFOR
sxaddpar,hdr,'COMMENT','======= Dustemwrap fixed parameter values ======',after='FPARN'+strtrim(i,2)
FOR i=0L,Nfixedparams-1 DO BEGIN
	sxaddpar,hdr,'FPARV'+strtrim(i+1,2),fixed_parameters_values[i],'Param. '+fixed_parameters_desc[i]
ENDFOR
sxaddpar,hdr,'COMMENT','===================================================',after='FPARV'+strtrim(i,2)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
314

8f83f26b   Jean-Philippe Bernard   added saving plug...
315

3196a378   Ilyes Choubani   Complementing fit...
316
IF unit_input_sed NE -1 THEN BEGIN
b7dca888   Jean-Philippe Bernard   improved to recov...
317

3196a378   Ilyes Choubani   Complementing fit...
318
319
320
321
322
323
324
325
326
327
    ;===== 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','STOKESI','Observed Total Stokes I Intensity [??]'
    sxaddpar,header_input_sed,'TTYPE4','STOKESQ','Observed Stokes Q Intensity [??]'
    sxaddpar,header_input_sed,'TTYPE5','STOKESU','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 [??]'
52f17b34   Annie Hughes   fixes for reading...
328
329
    sxaddpar,header_input_sed,'NH_SED',*!dustem_hcd,'Column density [H/cm2]'
;    sxaddpar,header_input_sed,'NH',*!dustem_hcd,'Column density [H/cm2]'
3196a378   Ilyes Choubani   Complementing fit...
330
331
332
333
334
335
336
337
    
    ;===== 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','STOKESI','Observed Total Stokes I Intensity [??]'
    sxaddpar,header_predicted_SED,'TTYPE4','STOKESQ','Observed Stokes Q Intensity [??]'
    sxaddpar,header_predicted_SED,'TTYPE5','STOKESU','Observed Stokes U Intensity [??]'
52f17b34   Annie Hughes   fixes for reading...
338
339
340
341
;ENDIF ELSE BEGIN;IC: hesitated between this and 'IF unit_input_ext NE -1 THEN BEGIN'. This is under the assumtion that we always fit something.

 END
IF unit_input_ext NE -1 THEN BEGIN
762cbe48   Jean-Philippe Bernard   modifed to cope f...
342

3196a378   Ilyes Choubani   Complementing fit...
343
344
345
346
347
348
349
350
351
352
    ;===== complement extension header for input extinction SED
    sxaddpar,header_input_ext,'TITLE','OBSERVED INPUT EXTINCTION SED TO DUSTEMWRAP',before='COMMENT'
    sxaddpar,header_input_ext,'TTYPE1','FILTER','Dustemwrap Filter name'
    sxaddpar,header_input_ext,'TTYPE2','WAVELENGTH','Dustemwrap Filter reference wavelength [microns]'
    sxaddpar,header_input_ext,'TTYPE3','STOKESI_EXT','Observed Total Stokes I optical depth []'
    sxaddpar,header_input_ext,'TTYPE4','STOKESQ_EXT','Observed Stokes Q optical depth []'
    sxaddpar,header_input_ext,'TTYPE5','STOKESU_EXT','Observed Stokes U optical depth []'
    sxaddpar,header_input_ext,'TTYPE6','VARIANCEII_EXT','Observed Total Stokes I optical depth Variance [??]'
    sxaddpar,header_input_ext,'TTYPE7','VARIANCEQQ_EXT','Observed Stokes Q optical depth Variance [??]'
    sxaddpar,header_input_ext,'TTYPE8','VARIANCEUU_EXT','Observed Stokes U optical depth Variance [??]'
52f17b34   Annie Hughes   fixes for reading...
353
354
    sxaddpar,header_input_ext,'NH_EXT',*!dustem_hcd,'Column density [H/cm2]'
;    sxaddpar,header_input_ext,'NH',*!dustem_hcd,'Column density [H/cm2]'
3196a378   Ilyes Choubani   Complementing fit...
355
356
357
358
359
360
361
362
363
    
    ;===== complement extension header for predicted extinction SED
    sxaddpar,header_predicted_EXT,'TITLE','OUTPUT BEST FIT EXTINCTION SED BY DUSTEMWRAP'
    sxaddpar,header_predicted_EXT,'TTYPE1','FILTER','Dustemwrap Filter name'
    sxaddpar,header_predicted_EXT,'TTYPE2','WAVELENGTH','Dustemwrap Filter reference wavelength [microns]'
    sxaddpar,header_predicted_EXT,'TTYPE3','STOKESI_EXT','Observed Total Stokes I optical depth []'
    sxaddpar,header_predicted_EXT,'TTYPE4','STOKESQ_EXT','Observed Stokes Q optical depth []'
    sxaddpar,header_predicted_EXT,'TTYPE5','STOKESU_EXT','Observed Stokes U optical depth []'

52f17b34   Annie Hughes   fixes for reading...
364
END
f2808c64   Jean-Philippe Bernard   improved
365

1cf58fd8   Jean-Philippe Bernard   First commit
366
;===== complement extension header for predicted spectrum total
f2808c64   Jean-Philippe Bernard   improved
367
368
369
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'

8f83f26b   Jean-Philippe Bernard   added saving plug...
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
;===== complement plugin headers
FOR i=0L,Nplugins-1 DO BEGIN
	header=*header_plugins[i]
	sxaddpar,header,'PLUGIN',str_plugins[i],'Plugin name'
	sxaddpar,header,'SCOPE',*((*!dustem_plugin).(i).scope),'Plugin scope'
	tags=*((*!dustem_plugin).(i).paramtag)
	FOR j=0L,n_elements(tags)-1 DO BEGIN
		sxaddpar,header,'TAGN'+strtrim(j+1,2),(*((*!dustem_plugin).(i).paramtag))[j],'Plugin parameter tag'
	ENDFOR
	header_plugins[i]=ptr_new(header)
ENDFOR

;hprint,*header_plugins[0]
;stop

762cbe48   Jean-Philippe Bernard   modifed to cope f...
385
;============== write final fits file
1cf58fd8   Jean-Philippe Bernard   First commit
386
387
388
IF keyword_set(filename) THEN BEGIN
	file=filename
ENDIF ELSE BEGIN
3b2e1c40   Annie Hughes   updated default f...
389
	file='./dustemwrap_results.fits'
1cf58fd8   Jean-Philippe Bernard   First commit
390
391
ENDELSE

762cbe48   Jean-Philippe Bernard   modifed to cope f...
392
393
394
;==== Write the observed SED (if any)
mwrfits,0,file,hdr,create=1
IF unit_input_sed NE -1 THEN BEGIN
17f02a5a   Jean-Philippe Bernard   updated
395
	mwrfits,str_input_SED,file,header_input_SED
762cbe48   Jean-Philippe Bernard   modifed to cope f...
396
397
ENDIF
;==== add the predicted SED (if any)
17f02a5a   Jean-Philippe Bernard   updated
398
IF unit_predicted_sed NE -1 THEN BEGIN
762cbe48   Jean-Philippe Bernard   modifed to cope f...
399
400
	mwrfits,str_predicted_SED,file,header_predicted_SED
ENDIF
17f02a5a   Jean-Philippe Bernard   updated
401
402
403
404
405
;==== add the shown SED (if any)
IF unit_shown_sed NE -1 THEN BEGIN
	mwrfits,str_shown_SED,file,header_shown_SED
ENDIF
;==== Write the observed extinction SED (if any)
762cbe48   Jean-Philippe Bernard   modifed to cope f...
406
IF unit_input_ext NE -1 THEN BEGIN
17f02a5a   Jean-Philippe Bernard   updated
407
	mwrfits,str_input_EXT,file,header_input_EXT
762cbe48   Jean-Philippe Bernard   modifed to cope f...
408
409
410
ENDIF
;==== add the predicted SED (if any)
IF unit_predicted_ext NE -1 THEN BEGIN
17f02a5a   Jean-Philippe Bernard   updated
411
412
413
414
415
	mwrfits,str_predicted_EXT,file,header_predicted_EXT
ENDIF
;==== add the show extinction SED (if any)
IF unit_shown_ext NE -1 THEN BEGIN
	mwrfits,str_shown_EXT,file,header_shown_EXT
762cbe48   Jean-Philippe Bernard   modifed to cope f...
416
417
418
419
420
421
422
423
424
ENDIF
;==== add the predicted per-grain emission spectra
mwrfits,dustem_st.sed,file,header_predicted_spectra
;==== add the predicted per-grain extinction
mwrfits,dustem_st.ext,file,header_predicted_extinctions
IF !run_pol EQ 1 THEN BEGIN
	mwrfits,dustem_st.polsed,file
	mwrfits,dustem_st.polext,file
ENDIF
8f83f26b   Jean-Philippe Bernard   added saving plug...
425
426
427
FOR i=0L,Nplugins-1 DO BEGIN
  mwrfits,*((*!dustem_plugin).(i).spec),file,*header_plugins[i]
ENDFOR
1cf58fd8   Jean-Philippe Bernard   First commit
428

762cbe48   Jean-Philippe Bernard   modifed to cope f...
429
message,'================================================',/info
1cf58fd8   Jean-Philippe Bernard   First commit
430
message,'Wrote '+file,/info
52f17b34   Annie Hughes   fixes for reading...
431
message,'File extensions are: ',/info
762cbe48   Jean-Philippe Bernard   modifed to cope f...
432
FOR i=0L,Nextensions-1 DO BEGIN
52f17b34   Annie Hughes   fixes for reading...
433
	message,'extension '+strtrim(extension_numbers[i],2)+' : '+strtrim(extensions_strs[i],2),/info
762cbe48   Jean-Philippe Bernard   modifed to cope f...
434
435
ENDFOR
message,'================================================',/info
f2808c64   Jean-Philippe Bernard   improved
436
;stop
1cf58fd8   Jean-Philippe Bernard   First commit
437
438
439
440
441


the_end:

END