Blame view

LabTools/IRAP/JPB/check_phangs_ssps_against_uv_fast.pro 12 KB
0ed148cc   Jean-Philippe Bernard   improved
1
PRO check_phangs_ssps_against_uv_fast,show_map=show_map,from_restore=from_restore
d6343d56   Jean-Philippe Bernard   First commit
2
3
4

;check_phangs_ssps_against_uv_fast

49c5df35   Jean-Philippe Bernard   improved
5
6
7
8
9
;===== This computes a map of prediction by dustem_plugin_phangs_stellar_continuum
;===== constrained on JWST NIR data to astrosat data in the UV for NGC0628.
;===== uses the plugin in a mode where the fortran is not run, but yet, the output files of the fortran are read at each pixel,
;===== which is not efficient (supposed to run in 46hrs .... See  check_phangs_ssps_against_uv_fast for an alternative use.

d6343d56   Jean-Philippe Bernard   First commit
10
window,0,xsize=900,ysize=1000
49c5df35   Jean-Philippe Bernard   improved
11
12
13
window,1,xsize=700,ysize=700

dustem_define_la_common
d6343d56   Jean-Philippe Bernard   First commit
14
15
16
17
obp=[1.1,0,1.15,1]
source_name='ngc0628'
data_dir='/Volumes/PILOT_FLIGHT1/PHANGS-JWST/DR1/'

0ed148cc   Jean-Philippe Bernard   improved
18
19
IF keyword_set(from_restore) THEN goto,from_restore

d6343d56   Jean-Philippe Bernard   First commit
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
;dustem_init,show_plots=show_plots
;needed only for NHCO
restore,data_dir+'ngc0628_jwst_images.sav',/verb
;needed for stellar parameters and voronoi bin info
restore,data_dir+'ngc0628_muse_images.sav',/verb
;restore,data_dir+'ngc0628_muse_NH.sav',/verb
;restore,data_dir+'ngc0628_astrosat_data.sav',/verb

;stop

;=== restore SEDs in voronoi bins
restore,data_dir+'ngc0628_jwst_seds_muse_pixels.sav',/verb
jwst_seds=all_seds
jwst_filters=(*jwst_seds[0]).filter
restore,data_dir+'ngc0628_muse_seds_muse_pixels.sav',/verb
muse_seds=all_seds
muse_filters=(*muse_seds[0]).filter
restore,data_dir+'ngc0628_astrosat_seds_muse_pixels.sav',/verb
astrosat_seds=all_seds
astrosat_filters=(*astrosat_seds[0]).filter
49c5df35   Jean-Philippe Bernard   improved
40
all_filters=[astrosat_filters,muse_filters,jwst_filters]
d6343d56   Jean-Philippe Bernard   First commit
41
42

Nvor=max(voronoi_id)
49c5df35   Jean-Philippe Bernard   improved
43
Nfilters=n_elements(jwst_filters)+n_elements(astrosat_filters)+n_elements(muse_filters)
d6343d56   Jean-Philippe Bernard   First commit
44
45
46

use_NHmap=NHCO       ;used NH map in 1.e21 (from CO)

d6343d56   Jean-Philippe Bernard   First commit
47
48
49
50
use_model='DBP90'    ;Example with default keywords uses the DBP90 model
;use_model='MC10'    ;Example with default keywords uses the DBP90 model
;use_model='DL07'    ;Example with default keywords uses the DBP90 model
use_polarization=0   ; initialize Dustemwrap in no polarization mode 
49c5df35   Jean-Philippe Bernard   improved
51
52
53
54
55
56
57
58
59
60
61
;use_window=2          ; default graphics window number to use for plotting the results
;use_verbose=0
;use_Nitermax=5        ; maximum number of iterations for the fit
;parameters to fit
;pd = [ $
;     '(*!dustem_params).G0', $                           ;G0
;     '(*!dustem_params).grains(0).mdust_o_mh',$          ;PAH0 mass fraction
;     '(*!dustem_params).grains(1).mdust_o_mh',$          ;PAH1 mass fraction
;     'dustem_plugin_phangs_stellar_continuum_1']         ;stellar continuum amplitude
;iv =   [1.6, [2.2e-3, 2.2e-3],5.e-5] 
;Npar=n_elements(pd)
d6343d56   Jean-Philippe Bernard   First commit
62
63

pd_to_update=['dustem_plugin_phangs_stellar_continuum_1','(*!dustem_params).grains(0).mdust_o_mh']
0ed148cc   Jean-Philippe Bernard   improved
64
65
66
;pd_filter_names=['NIRCAM11','MIRI2']
;pd_filter_names=['SDSS4','MIRI2']       ;This normalizes SSPs to Muse filter SDSS4 at ~0.8 mic
pd_filter_names=['NIRCAM11','MIRI2']       ;This normalizes SSPs to NIRCAM12 filter  at ~2 mic 
49c5df35   Jean-Philippe Bernard   improved
67
68
69
70
pd_filter_wav=dustem_filter2wav(pd_filter_names)
pd_filter_index=lonarr(n_elements(pd_filter_names))
FOR i=0L,n_elements(pd_filter_names)-1 DO pd_filter_index[i]=where(all_filters EQ pd_filter_names[i])

d6343d56   Jean-Philippe Bernard   First commit
71
72
73
74
75

;== INITIALISE DUSTEM
dustem_init,model=use_model,polarization=use_polarization,show_plots=show_plots
;stop
!quiet=1
d6343d56   Jean-Philippe Bernard   First commit
76
77
78
79
80

;voronoi bins for test. Last series are randomly picked
one_sed=(*jwst_seds[0])[0]
Nfilters=n_elements(astrosat_filters)+n_elements(muse_filters)+n_elements(filters)
seds=replicate(one_sed,Nfilters)
4c500bde   Jean-Philippe Bernard   updated with new ...
81
;diff_seds=fltarr(Nvor,Nfilters)
49c5df35   Jean-Philippe Bernard   improved
82
83
actual_seds=fltarr(Nvor,Nfilters)
predicted_seds=fltarr(Nvor,Nfilters)
0ed148cc   Jean-Philippe Bernard   improved
84
85
all_predicted_specs=ptrarr(Nvor)
all_predicted_specs_normalized=ptrarr(Nvor)
49c5df35   Jean-Philippe Bernard   improved
86
87
;predicted_maps=fltarr(sxpar(href,'NAXIS1'),sxpar(href,'NAXIS2'),Nfilters) ;predicted maps
;actual_maps=fltarr(sxpar(href,'NAXIS1'),sxpar(href,'NAXIS2'),Nfilters)    ;actual maps
d6343d56   Jean-Philippe Bernard   First commit
88
diff_maps=fltarr(sxpar(href,'NAXIS1'),sxpar(href,'NAXIS2'),Nfilters)   ;difference maps
49c5df35   Jean-Philippe Bernard   improved
89
90
;predicted_maps[*]=la_undef()
;actual_maps[*]=la_undef()
d6343d56   Jean-Philippe Bernard   First commit
91
92
93
94
diff_maps[*]=la_undef()
;imrange=[-1.,1.]   ;MJy/sr
imrange=[-0.3,0.2]   ;MJy/sr

0ed148cc   Jean-Philippe Bernard   improved
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;===== Compute seds (pointer)
all_filters=[astrosat_filters,jwst_filters,muse_filters]
Nfilters=n_elements(all_filters)
seds_ptr=ptrarr(Nvor)
nperc=5./100.
FOR ii=0L,Nvor-1 DO BEGIN
	seds_ptr[ii]=ptr_new([*astrosat_seds[ii],*muse_seds[ii],*jwst_seds[ii]])
    ;=== check for 0 variances
	ind=where((*seds_ptr[ii]).sigmaII EQ 0,count)
    IF count NE 0 THEN BEGIN
    	this_sed=*seds_ptr[ii]
    	Ivalues=((*seds_ptr[ii]).stokesI)[ind]
    	sigmaii_values=(Ivalues*nperc)^2
    	this_sed[ind].sigmaii=sigmaii_values
    	seds_ptr[ii]=ptr_new(this_sed)
    ENDIF
ENDFOR

49c5df35   Jean-Philippe Bernard   improved
113
114
t1=systime(0,/sec)
first_vid=0L
0ed148cc   Jean-Philippe Bernard   improved
115
;first_vid=12640L   ;for tests
49c5df35   Jean-Philippe Bernard   improved
116
117
;do_normalize=1
do_normalize=0
d6343d56   Jean-Philippe Bernard   First commit
118
lambir=dustem_get_wavelengths()
49c5df35   Jean-Philippe Bernard   improved
119
120
;show_each=10
show_each=100
d6343d56   Jean-Philippe Bernard   First commit
121

49c5df35   Jean-Philippe Bernard   improved
122
123
124
NHvor=fltarr(Nvor)
FOR vid=first_vid,Nvor-1 DO BEGIN
	IF vid mod show_each EQ 0 THEN BEGIN
d6343d56   Jean-Philippe Bernard   First commit
125
126
127
128
129
		t2=systime(0,/sec)
		delta_t_hr=(t2-t1)/60.^2 ;elapsed time [hr]
		frac_perc=vid/Nvor*100
		delta_t_remain=delta_t_hr/frac_perc*100.-delta_t_hr  ;remaining time [hr]
		message,'done '+strtrim(frac_perc)+' % in '+strtrim(delta_t_hr,2)+' hr remaining '+strtrim(delta_t_remain,2)+' hr',/continue
d6343d56   Jean-Philippe Bernard   First commit
130
	ENDIF
d6343d56   Jean-Philippe Bernard   First commit
131
132
	;===== get the SSP weights for the given voronoi bins
	weights=phangs_binid2weights(st_muse_weights,vid,st_templates,age_values,metalicity_values,reddening=reddening)
0ed148cc   Jean-Philippe Bernard   improved
133
	sed=*seds_ptr[vid]
49c5df35   Jean-Philippe Bernard   improved
134
	;===== get combined Astro_sat+Muse+JWST SED for that voronoi bin
49c5df35   Jean-Philippe Bernard   improved
135
136
	ind=where(sed.stokesI EQ la_undef(),count)
	IF count NE 0 THEN goto,do_the_next
d6343d56   Jean-Philippe Bernard   First commit
137
	;===== set the fixed parameters and initial values
0ed148cc   Jean-Philippe Bernard   improved
138
139
140
	amplitude=1.
	reddening=0.     ;This sets Muse reddening to 0. Will be contsrained later
	fpd=phangs_stellar_continuum_plugin_weight2params(weights,parameter_values=fiv,redenning=reddening,/force_include_reddening,amplitude=amplitude,/force_include_amplitude)
49c5df35   Jean-Philippe Bernard   improved
141
142
143
144
145
146
   NH_value=la_mul(la_mean(use_NHmap[*all_seds_indices[vid]]),10.) ;NH value in 1.e20 H/cm2
   NHvor[vid]=NH_value
	IF do_normalize EQ 1 THEN BEGIN
		sed.stokesI=sed.stokesI/NH_value   ;normalize SED to 1.e20 H/cm2
		;sed.sigmaII=sed.sigmaII/NH_value^2   ;normalize SED to 1.e20 H/cm2
	ENDIF
0ed148cc   Jean-Philippe Bernard   improved
147
148
	;print,fpd,fiv
	;stop
49c5df35   Jean-Philippe Bernard   improved
149
150
151
152
153
154
155
   Nparams=n_elements(fiv)
   key=intarr(Nparams)
   FOR i=0L,Nparams-1 DO BEGIN
	    toto=dustem_parameter_description2type(fpd[i],string_name=string_name,key=one_key)
	    key[i]=one_key
 	ENDFOR
   spec=dustem_plugin_phangs_stellar_continuum(key=key,val=fiv)
0ed148cc   Jean-Philippe Bernard   improved
156
   ;sed_ref=interpol(spec[*,0],lambir,pd_filter_wav[0])
49c5df35   Jean-Philippe Bernard   improved
157
   dustem_predicted_sed=interpol(spec[*,0],lambir,sed.wave)
0ed148cc   Jean-Philippe Bernard   improved
158
   ;Those are reference SEDs used for normalization
49c5df35   Jean-Philippe Bernard   improved
159
160
   sed_ref=dustem_predicted_sed[pd_filter_index[0]]
   sed_goal=(sed.stokesI)[pd_filter_index[0]]
0ed148cc   Jean-Philippe Bernard   improved
161
   ;Normalize
49c5df35   Jean-Philippe Bernard   improved
162
   dustem_predicted_sed=dustem_predicted_sed/sed_ref*sed_goal
4c500bde   Jean-Philippe Bernard   updated with new ...
163
164
   all_predicted_specs[vid]=ptr_new(spec[*,0])
   all_predicted_specs_normalized[vid]=ptr_new(spec[*,0]/sed_ref*sed_goal)
49c5df35   Jean-Philippe Bernard   improved
165
166
167
168
169
	IF vid mod show_each EQ 0 THEN BEGIN
		wset,1
		cgplot,sed.wave,sed.stokesI,/xlog,/ylog,psym=-4
		cgoplot,sed.wave,dustem_predicted_sed,color='red'
 	ENDIF
49c5df35   Jean-Philippe Bernard   improved
170
171
   actual_seds[vid,*]=sed.stokesI
   predicted_seds[vid,*]=dustem_predicted_sed
49c5df35   Jean-Philippe Bernard   improved
172
173
174
	do_the_next:
ENDFOR

0ed148cc   Jean-Philippe Bernard   improved
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;stop

;compute E(B-V) needed to bring prediction to observed astrosat flux
first_vid=0L
ebv=fltarr(Nvor)
astrosat_filters=dustem_filter_names2filters(['F148W','F154W','F169M','F172M','N219M'])
wave_refs=dustem_filter2wav(astrosat_filters)*1.e4 ;AA
which_ebv_filter=1
FOR vid=first_vid,Nvor-1 DO BEGIN
	;flux_ratio=predicted_seds[vid,which_ebv_filter]/actual_seds[vid,which_ebv_filter]  ;This uses only one astrosat filter to derive E(B-V)
	flux_ratio=la_mean(predicted_seds[vid,0:4])/la_mean(actual_seds[vid,0:4]) ;This is an attempt to increase SNR on derived E(B-V)
   ebv[vid]=calz_guess_ebv(wave_refs[which_ebv_filter],flux_ratio,R_v=R_v)
ENDFOR

49c5df35   Jean-Philippe Bernard   improved
189
190
stop

0ed148cc   Jean-Philippe Bernard   improved
191
192
193
194
195
save,actual_seds,predicted_seds,href,all_seds_indices,do_normalize,all_filters,NHvor,ebv,file=data_dir+'ngc0628_astrosat_voronoi_prediction_fast.sav',/verb

from_restore:

message,'see plot_phangs_ssps_against_uv_fast instead'
49c5df35   Jean-Philippe Bernard   improved
196
197
198

restore,data_dir+'ngc0628_astrosat_minus_prediction_fast.sav',/verb
restore,data_dir+'ngc0628_astrosat_voronoi_prediction_fast.sav',/verb
0ed148cc   Jean-Philippe Bernard   improved
199
200
201
;needed only for NHCO
restore,data_dir+'ngc0628_jwst_images.sav',/verb
use_NHmap=NHCO
49c5df35   Jean-Philippe Bernard   improved
202

0ed148cc   Jean-Philippe Bernard   improved
203
;stop
49c5df35   Jean-Philippe Bernard   improved
204

0ed148cc   Jean-Philippe Bernard   improved
205
;===== plot histograms
49c5df35   Jean-Philippe Bernard   improved
206
207
208
209
210
211
212
213
214
215
216
217
218
219

window,0,xsize=800,ysize=800
!p.multi=[0,1,2]

xtit='(data-model) [MJy/sr]'
IF do_normalize THEN xtit='(data-model) normalized to 1e21 H/cm2 [MJy/sr]'
NHthreshold=0. & tit=''
;NHthreshold=0.1 & tit='NH > 1e20 H/cm2'
;NHthreshold=0.5 & tit='NH > 5e20 H/cm2'
;NHthreshold=1. & tit='NH > 1e21 H/cm2'
;NHthreshold=5. & tit='NH > 5e21 H/cm2'
;NHthreshold=10. & tit='NH > 1e22 H/cm2'
;NHthreshold=50. & tit='NH > 5e22 H/cm2'
;vmin=-0.1 & vmax=0.1 & Nbins=50
0ed148cc   Jean-Philippe Bernard   improved
220
vmin=-1. & vmax=1. & Nbins=51
49c5df35   Jean-Philippe Bernard   improved
221
222
223
224
225
;vmin=-5. & vmax=5. & Nbins=100
;vmin=-10. & vmax=10. & Nbins=100
;im=diff_maps[*,*,0]
ims=actual_seds-predicted_seds

0ed148cc   Jean-Philippe Bernard   improved
226
im=ims[*,0]
49c5df35   Jean-Philippe Bernard   improved
227
228
229
230
231
ind=where(im NE la_undef())
xtit='(data-model) normalized to 1e21 H/cm2 [MJy/sr]'
res=histogram(im[ind],locations=x,min=vmin,max=vmax,Nbins=Nbins)
yrange=[max([min(res),1.]),1.e4]
colors=['violet','blue','yellow','brown','red']
0ed148cc   Jean-Philippe Bernard   improved
232
cgplot,x,res,/ylog,xtit=xtit,ytit='N',psym=10,yrange=yrange,/ysty,color='violet',tit=tit,/nodata
49c5df35   Jean-Philippe Bernard   improved
233
234
235
FOR k=0L,4 DO BEGIN
	im=ims[*,k]
	ind=where(im NE la_undef())
0ed148cc   Jean-Philippe Bernard   improved
236
	res1=histogram(im[ind],locations=x1,min=vmin,max=vmax,Nbins=Nbins)
49c5df35   Jean-Philippe Bernard   improved
237
   cgoplot,x1,res1,color=colors[k],psym=10
d6343d56   Jean-Philippe Bernard   First commit
238
ENDFOR
49c5df35   Jean-Philippe Bernard   improved
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
im=ims[*,0]
;ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
ind=where(im NE la_undef())
res0=histogram(im[ind],locations=x0,min=vmin,max=vmax,Nbins=Nbins)
cgplot,x0,res0,/ylog,xtit='(data-model) normalized to 1e21 H/cm2 [MJy/sr]',ytit='N',psym=10,yrange=[max([min(res0),1.]),1.e4],/ysty,color='violet',tit=tit
im=ims[*,1]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res1=histogram(im[ind],locations=x1,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x1,res1,color='blue',psym=10
im=ims[*,2]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res2=histogram(im[ind],locations=x2,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x2,res2,color='yellow',psym=10
im=ims[*,3]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res3=histogram(im[ind],locations=x3,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x3,res3,color='brown',psym=10
im=ims[*,4]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res4=histogram(im[ind],locations=x4,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x4,res4,color='red',psym=10

;same in percent
window,0,xsize=800,ysize=800
!p.multi=[0,1,2]

xtit='(data-model) [%]'
IF do_normalize THEN xtit='(data-model) [%]'
NHthreshold=0. & tit=''
;NHthreshold=0.1 & tit='NH > 1e20 H/cm2'
;NHthreshold=0.5 & tit='NH > 5e20 H/cm2'
;NHthreshold=1. & tit='NH > 1e21 H/cm2'
;NHthreshold=5. & tit='NH > 5e21 H/cm2'
;NHthreshold=10. & tit='NH > 1e22 H/cm2'
;NHthreshold=50. & tit='NH > 5e22 H/cm2'
;vmin=-0.1 & vmax=0.1 & Nbins=50
;vmin=-1. & vmax=1. & Nbins=50
;vmin=-5. & vmax=5. & Nbins=100
vmin=-500. & vmax=500. & Nbins=100
;im=diff_maps[*,*,0]
ims=(actual_seds-predicted_seds)/predicted_seds*100.
;ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
im=ims[*,1]
ind=where(im NE la_undef())
res0=histogram(im[ind],locations=x0,min=vmin,max=vmax,Nbins=Nbins)
cgplot,x0,res0,/ylog,xtit='(data-model) normalized to 1e21 H/cm2 [MJy/sr]',ytit='N',psym=10,yrange=[max([min(res0),1.]),1.e7],/ysty,color='violet',tit=tit
im=ims[*,1]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res1=histogram(im[ind],locations=x1,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x1,res1,color='blue',psym=10
im=ims[*,2]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res2=histogram(im[ind],locations=x2,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x2,res2,color='yellow',psym=10
im=ims[*,3]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res3=histogram(im[ind],locations=x3,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x3,res3,color='brown',psym=10
im=ims[*,4]
ind=where(im NE la_undef() AND use_NHmap GT NHthreshold)
res4=histogram(im[ind],locations=x4,min=vmin,max=vmax,Nbins=Nbins)
cgoplot,x4,res4,color='red',psym=10


49c5df35   Jean-Philippe Bernard   improved
303
304
cgplot,use_NHmap,diff_maps[*,*,0],/xlog,yr=[-1.,1.],/ysty,psym=3,xr=[1.e-3,1.e2],xtit='NH/1e21',ytit='(data-model) normalized to 1e21 H/cm2 [MJy/sr]'
cgplot,use_NHmap,diff_maps[*,*,0],/xlog,yr=[-10.,10.],/ysty,psym=3,xr=[1.e-3,1.e2],xtit='NH/1e21',ytit='(data-model) normalized to 1e21 H/cm2 [MJy/sr]'
d6343d56   Jean-Philippe Bernard   First commit
305
306
307
308
309
310

message,'========== Finished computing difference SEDs',/continue
stop


END