Blame view

LabTools/IRAP/JPB/make_phangs_isrf_classes.pro 9.29 KB
de766928   Jean-Philippe Bernard   improved
1
PRO make_phangs_isrf_classes,bidon,source_name=source_name,help=help,resolution_filter=resolution_filter,save=save
8b65a68c   Jean-Philippe Bernard   First commit
2
3
4
5
6
7
8
9
10
11
12

;+
; NAME:
;       make_phangs_isrf_classes
; CALLING SEQUENCE:
;       make_phangs_isrf_classes[,/save]
; PURPOSE:
;       generates the ISRF classes used in fiting PHANGs data
; INPUTS:
;       None
; OPTIONAL KEYWORDS:
66b0e1d4   Jean-Philippe Bernard   improved
13
;       source_name   = source name. Default = ngc0628
de766928   Jean-Philippe Bernard   improved
14
;       save          = if set, saves the classes into a .fits file
66b0e1d4   Jean-Philippe Bernard   improved
15
;       help          = if set, print this help
8b65a68c   Jean-Philippe Bernard   First commit
16
17
18
19
20
21
22
; OUTPUTS:
;	    None
; OPTIONAL INPUT:
;       None
; OPTIONAL OUTPUT:
;       None
; PROCEDURE AND SUBROUTINE USED
66b0e1d4   Jean-Philippe Bernard   improved
23
24
25
26
;       Uses the following files:
;       *_isrf_min_prediction.sav
;       *_ref_header.sav
;       *_astrosat_seds_muse_pixels.sav
8b65a68c   Jean-Philippe Bernard   First commit
27
; SIDE EFFECTS:
66b0e1d4   Jean-Philippe Bernard   improved
28
29
30
31
;       Produces the following files:
;       isrf_classes_one_ratio_on_*.fits
;       *_isrf_classes_map.fits
;       *_isrf_classes_voronoi.sav
8b65a68c   Jean-Philippe Bernard   First commit
32
33
34
35
36
37
38
39
40
41
42
43
44
; EXAMPLE:
;       make_phangs_isrf_classes,/save
; MODIFICATION HISTORY:
;       written by Jean-Philippe Bernard
;-

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

win=0L

adaf437f   Jean-Philippe Bernard   improved path
45
46
47
;data_dir='/Volumes/PILOT_FLIGHT1/PHANGS-JWST/DR1/'
data_dir=!phangs_data_dir+'/ISRF/WORK/'

8b65a68c   Jean-Philippe Bernard   First commit
48
49
50
51
52
53
use_model='DBP90'    ;Example with default keywords uses the DBP90 model
use_polarization=0   ; initialize Dustemwrap in no polarization mode 

;== INITIALISE DUSTEM
dustem_init,model=use_model,polarization=use_polarization,show_plots=show_plots

82beee16   Jean-Philippe Bernard   improved
54
55
use_source_name='ngc0628'
IF keyword_set(source_name) THEN use_source_name=source_name
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
56

9b0b6d7e   Jean-Philippe Bernard   finished implemen...
57
58
59
60
61
reso_str=''
IF keyword_set(resolution_filter) THEN BEGIN
    reso_str='_'+resolution_filter
ENDIF

8b65a68c   Jean-Philippe Bernard   First commit
62
;==== restore needed data
9b0b6d7e   Jean-Philippe Bernard   finished implemen...
63
;CAUTION: Here ISRFs has a different shape wrt voronoi bins when reso_str is set vs not set ...
de766928   Jean-Philippe Bernard   improved
64
65
66
67
68
69
70
file=data_dir+use_source_name+'_isrf_min_prediction'+reso_str+'.sav'
st_info=file_info(file)
IF st_info.exists NE 1 THEN BEGIN
  message,'Could not find '+file,/continue
  stop
ENDIF
restore,file,/verb
8b65a68c   Jean-Philippe Bernard   First commit
71
72
73
74
;% RESTORE: Restored variable: ISRFS.
;% RESTORE: Restored variable: OBJECT_DISTANCE.
;% RESTORE: Restored variable: OBJECT_THICKNESS.
;% RESTORE: Restored variable: SOURCE_NAME.
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
75
76
77

;=== The following is restored only for variables HREF and ALL_SEDS_INDICES
;=== in order to generate the fits class map fo that galaxy
de766928   Jean-Philippe Bernard   improved
78
79
80
81
82
83
84
file=data_dir+use_source_name+'_ref_header'+reso_str+'.sav'
st_info=file_info(file)
IF st_info.exists NE 1 THEN BEGIN
  message,'Could not find '+file,/continue
  stop
ENDIF
restore,file,/verb
8b65a68c   Jean-Philippe Bernard   First commit
85
;% RESTORE: Restored variable: HREF.
82beee16   Jean-Philippe Bernard   improved
86
87
;=== The following is restored only for variables HREF and ALL_SEDS_INDICES
;=== in order to generate the fits class map fo that galaxy
de766928   Jean-Philippe Bernard   improved
88
89
90
91
92
93
94
file=data_dir+use_source_name+'_seds_indices'+reso_str+'.sav'
st_info=file_info(file)
IF st_info.exists NE 1 THEN BEGIN
  message,'Could not find '+file,/continue
  stop
ENDIF
restore,file,/verb
9b0b6d7e   Jean-Philippe Bernard   finished implemen...
95
96
;% RESTORE: Restored variable: ALL_SEDS_INDICES.
;restore,data_dir+use_source_name+'_astrosat_seds_muse_pixels.sav',/verb
82beee16   Jean-Philippe Bernard   improved
97
;% RESTORE: Restored variable: ALL_SEDS.
8b65a68c   Jean-Philippe Bernard   First commit
98
;% RESTORE: Restored variable: ALL_SEDS_INDICES.
8b65a68c   Jean-Philippe Bernard   First commit
99

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
100
class_map=lonarr(sxpar(href,'NAXIS1'),sxpar(href,'NAXIS2'))
8b65a68c   Jean-Philippe Bernard   First commit
101
102
;stop

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
103
;=== Defines ISRF classes, based on a given galaxy
8b65a68c   Jean-Philippe Bernard   First commit
104

cc81fcdc   Jean-Philippe Bernard   improved
105
lambir=dustem_get_wavelengths(isrf_wavelengths=isrf_wavelengths)
8b65a68c   Jean-Philippe Bernard   First commit
106

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
107
Nlamb=(size(ISRFS))[1]
8b65a68c   Jean-Philippe Bernard   First commit
108
109
Nvor=(size(ISRFS))[2]

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
110
111
112
;Define normalized ISRFs
n_ISRFS=fltarr(Nlamb,Nvor)     ;ISRFs normalized to 1 micron
n_wavs=fltarr(Nlamb,Nvor)
cc81fcdc   Jean-Philippe Bernard   improved
113
ind=where(isrf_wavelengths GT 1.)
8b65a68c   Jean-Philippe Bernard   First commit
114
115
116
indd=ind[0]
FOR vid=0L,Nvor-1 DO BEGIN
  n_ISRFS[*,vid]=ISRFS[*,vid]/ISRFS[indd,vid]
cc81fcdc   Jean-Philippe Bernard   improved
117
  n_wavs[*,vid]=isrf_wavelengths
8b65a68c   Jean-Philippe Bernard   First commit
118
119
ENDFOR

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
120
121
122
;=== Plot 2D histogram of normalized ISRFs in NGC0628
x=reform(n_wavs,Nlamb*Nvor)
y=reform(n_ISRFS,Nlamb*Nvor)
8b65a68c   Jean-Philippe Bernard   First commit
123
124
125
126
127
128
129
130
131
132
133
134
135
136

;jpbloadct
loadct,13
Nx=200 & Ny=200
Nx=100 & Ny=100
xmin=-1.5 & xmax=2.
ymin=-5 & ymax=1.
xtit='log10(wav [mic])'
ytit='log10(ISRF/ISRF(1 mic))'
range=[-1,5]

window,win & win=win+1
plot_bin_correl,alog10(x),alog10(y),Nx,Ny,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,xtit=xtit,ytit=ytit,range=range;,/xlog ;,/ylog

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
137
138
;=== compute the median ISRFS
;med_isrf=la_median(n_ISRFS,dim=-1)
8b65a68c   Jean-Philippe Bernard   First commit
139
140
141
142

;refs_wavs=[0.11]      ;This is set to be just before the Lymann break
reference_wavelength=1.  ;all ISRFs are normalized to 1 mic
refs_wavs=[0.15]      ;This is set to be just before the Lymann break
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
143
;ref_ratios=interpol(med_isrf,isrf_wavelengths,refs_wavs)  ;actually not used
8b65a68c   Jean-Philippe Bernard   First commit
144
145
;These are the ratios which will be used to classify ISRFs

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
146
147
;==== compute ISRF ration for each Voronoi bin
isrf_ratio_values=fltarr(Nvor)
e2fb44a5   Jean-Philippe Bernard   improved
148
vor_class=lonarr(Nvor)         ;This is the class of each voronoi bin
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
149
150
151
152
153
154
155
156
157
158
159
160
161
FOR i=0L,Nvor-1 DO BEGIN
  isrf_ratio_values[i]=interpol(n_ISRFS[*,i],isrf_wavelengths,refs_wavs[0])
ENDFOR

undef_array=fltarr(Nlamb)
undef_array[*]=la_undef()

one_class={number:0L $                                  ;class number
           ,name:'' $                                   ;class name
           ,ISRF_wavelengths:undef_array $              ;isrf wavelengths
           ,ISRF_template:undef_array $                 ;isrf template, normalized at reference_wavelength
           ,flux_ratio:0. $                             ;flux ratio between flux_ratio_wav and reference_wavelength
           ,flux_ratio_wav:0. $                         ;flux ratio wavelength
8b65a68c   Jean-Philippe Bernard   First commit
162
           ,reference_wavelength:reference_wavelength $ ;ISRF normalisation wavelength
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
163
164
165
166
167
168
169
170
171
172
           ,flux_ratio_min:0. $                         ;minimum flux ratio to belong to this class
           ,flux_ratio_max:0. $                         ;maximum flux ratio to belong to this class           
           }
;           ,flux_ref:0. $                               ;
;           ,Nvor:0L  $                                  ;number of Voronoi bins in the class
;           ,voronoi_members:ptr_new() $ ;voronoi pixel members of that class
;           ,voronoi_chi2:ptr_new()}    ;chi2 between ref ISRF and voronoi member's ISRF

class_ratio_range=[1.e-5,10.]    ;reange of flux ratios used in the classification
Nclasses=30L                     ;Number of classes used for classification
8b65a68c   Jean-Philippe Bernard   First commit
173
ratios=range_gen(Nclasses,class_ratio_range,/log,/get_minmax,min_values=ratios_min,max_values=ratios_max)
8b65a68c   Jean-Philippe Bernard   First commit
174
175
classes=replicate(one_class,Nclasses)

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
176
177
;====== fill in class values
Nvor_class=lonarr(Nclasses)
8b65a68c   Jean-Philippe Bernard   First commit
178
179
180
181
182
classes.number=lindgen(Nclasses)
classes.flux_ratio=ratios
classes.flux_ratio_wav=refs_wavs[0]
classes.flux_ratio_min=ratios_min
classes.flux_ratio_max=ratios_max
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
183
median_isrf=fltarr(Nlamb)
8b65a68c   Jean-Philippe Bernard   First commit
184
FOR i=0L,Nclasses-1 DO BEGIN
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
185
186
  ind=where(isrf_ratio_values GE ratios_min[i] AND isrf_ratio_values LT ratios_max[i],count)
  IF count NE 0 THEN  BEGIN
e2fb44a5   Jean-Philippe Bernard   improved
187
    vor_class[ind]=i             ;This is the class of each voronoi bin
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
188
189
190
191
192
193
194
    FOR j=0L,Nlamb-1 DO BEGIN
      median_isrf[j]=la_median(n_ISRFs[j,ind])
    ENDFOR
  	classes[i].ISRF_template=median_isrf
    classes[i].ISRF_wavelengths=isrf_wavelengths
    Nvor_class[i]=count
    FOR j=0L,count-1 DO BEGIN
9b0b6d7e   Jean-Philippe Bernard   finished implemen...
195
196
197
      IF ptr_valid(ALL_SEDS_INDICES[ind[j]]) THEN BEGIN
        class_map[*ALL_SEDS_INDICES[ind[j]]]=i
      ENDIF
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
198
199
    ENDFOR
  ENDIF
8b65a68c   Jean-Philippe Bernard   First commit
200
ENDFOR
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
201
;stop
8b65a68c   Jean-Philippe Bernard   First commit
202
203
204
205
206
207
208
209
210
211

;=== Do a class (class 0) with a Mathis field
class0=one_class
class0.number=0L
class0.name='Mathis'
class0.reference_wavelength=1.
class0.flux_ratio_wav=refs_wavs[0]
file=!dustem_soft_dir+'/data/ISRF_MATHIS.DAT'
;file='/Users/jpb/Soft_Libraries/dustem_fortran/data/ISRF.DAT'
readcol,file,Mathis_wavs,Mathis_ISRF
cc81fcdc   Jean-Philippe Bernard   improved
212
213
Mathis_ISRF=interpol(Mathis_ISRF,Mathis_wavs,isrf_wavelengths)
n_Mathis_ISRF=Mathis_ISRF/interpol(Mathis_ISRF,isrf_wavelengths,class0.reference_wavelength)
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
214
215
216
;class0.ISRF_template=ptr_new(n_Mathis_ISRF)
class0.ISRF_template=n_Mathis_ISRF
class0.ISRF_wavelengths=isrf_wavelengths
cc81fcdc   Jean-Philippe Bernard   improved
217
ratio=interpol(n_Mathis_ISRF,isrf_wavelengths,class0.flux_ratio_wav)
8b65a68c   Jean-Philippe Bernard   First commit
218
219
220
class0.flux_ratio=ratio
class0.flux_ratio_min=ratio*(1.-0.1)  ;arbitrary +-10% around ratio
class0.flux_ratio_max=ratio*(1.+0.1)  ;arbitrary +-10% around ratio
cc81fcdc   Jean-Philippe Bernard   improved
221
Mathis_1mic=interpol(Mathis_ISRF,isrf_wavelengths,1.0)
8b65a68c   Jean-Philippe Bernard   First commit
222
223
224
message,'Mathis (G0=1) has 4*pi*Inu='+strtrim(Mathis_1mic,2)+' ergs/s/cm2/Hz',/continue
message,'Mathis (G0=1) has Inu(0.15 mic)/Inu(1 mic)='+strtrim(ratio,2)+' ',/continue

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
;=== Do a plot of ISRF classes
window,win & win=win+1
colors=range_gen(Nclasses,[0,255])
yrange=[1.e-5,10]
xrange=[1.e-2,1.e4]
xtit='Wavlenghts [mic]'
ytit='Normalized ISRF (Inu)'
cgplot,class0.ISRF_wavelengths,class0.ISRF_template,/xlog,/ylog,yrange=yrange,xrange=xrange,xtit=xtit,ytit=ytit
FOR i=0L,Nclasses-1 DO BEGIN
  oplot,classes[i].ISRF_wavelengths,classes[i].ISRF_template,color=colors[i]
ENDFOR

;=== plot an histogram of the classes:
window,win & win=win+1
xtit='ISRF class #'
ytit='Number of Voronoi bins'
cgplot,Nvor_class,psym=10,xtit=xtit,ytit=ytit

;=== plot the class map:
cleanplot
window,win & win=win+1
obp=[1.1,0.,1.15,1]
imrange=[0,30]
image_cont20,class_map,href,/square,/silent,image_color_table='jpbloadct',axis_color_table=1,off_bar=obp,imrange=imrange

8b65a68c   Jean-Philippe Bernard   First commit
250
251
;stop

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
252
;=== save a fits file
de766928   Jean-Philippe Bernard   improved
253
IF keyword_set(save) THEN BEGIN
9b0b6d7e   Jean-Philippe Bernard   finished implemen...
254
  file_save=data_dir+'isrf_classes_one_ratio_on_'+use_source_name+reso_str+'.fits'
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
255
256
257
258
259
  ;message,'Will save '+file_save,/continue
  ;message,'.c to proceed ',/continue
  ;stop
  mwrfits,class0,file_save,/create
  mwrfits,classes,file_save
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
260
261
  message,'Saved '+file_save,/continue

9b0b6d7e   Jean-Philippe Bernard   finished implemen...
262
  file_save=data_dir+use_source_name+'_isrf_classes_map'+reso_str+'.fits'
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
263
  writefits,file_save,class_map,href
e2fb44a5   Jean-Philippe Bernard   improved
264
  message,'Saved '+file_save,/continue
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
265

9b0b6d7e   Jean-Philippe Bernard   finished implemen...
266
  file_save=data_dir+use_source_name+'_isrf_classes_voronoi'+reso_str+'.sav'
e2fb44a5   Jean-Philippe Bernard   improved
267
  save,vor_class,file=file_save
ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
268
269
  message,'Saved '+file_save,/continue

8b65a68c   Jean-Philippe Bernard   First commit
270
271
ENDIF

ccfa6760   Jean-Philippe Bernard   corrected ISRF cl...
272
273
274



8b65a68c   Jean-Philippe Bernard   First commit
275
276
277
the_end:

END