Blame view

src/idl/dustem_define_grid_v1.pro 5.81 KB
4d392534   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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
PRO dustem_define_grid_v1,table_name $
										  ,filters=filters $
										  ,help=help $
					   					,table_params=table_params $
					   					,table_pmins=table_pmins $
					   					,table_pmaxs=table_pmaxs $
					   					,table_pnvals=table_pnvals $
					   					,table_plogs=table_plogs

;+
; NAME:
;    dustem_define_grid_v1
; PURPOSE:
;    loads an SED Grid table into !dustem_grid. This is for initial versions of grid tables. Used to convert them to new convention.
; CATEGORY:
;    Dustem
; CALLING SEQUENCE:
;    dustem_define_grid,table_name,filters[,table_params=][,table_pmins=][,table_pmaxs=][,table_pnvals=][,table_plogs=]
; INPUTS:
;    table_name             : fits file name containing the grid
;    filters                : name of dustemwrap filters to be included in the GRID
; OPTIONAL INPUT PARAMETERS:
;    NONE
; OUTPUTS:
;    None
; OPTIONAL OUTPUT PARAMETERS:
;    table_params= parameter names in the grid table
;    table_pmins= parameter min values in the grid table
;    table_pmaxs= parameter max values in the grid table
;    table_pnvals= parameter number of values in the grid table
;    table_plogs= 1=parameter values in log in the grid table, 0=linear
; ACCEPTED KEY-WORDS:
;    help      = If set, print this help
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    !dustem_grid is updated
; RESTRICTIONS:
;    The DustEM fortran code must be installed
;    The DustEMWrap IDL code must be installed
; PROCEDURE:
;    None
; EXAMPLES
;    dustem_make_sed_table,'DBP90',['(*!dustem_params).G0','(*!dustem_params).grains(0).mdust_o_mh'], $
;								[0.1,1.e-3],[100.,1.e-1],[10,3],filename='/tmp/IRAS_GO.fits',log=[1,0],/show_seds, $
;                              	filters=[dustem_instru2filters('IRAC'),dustem_instru2filters('SPIRE')]
;    dustem_define_grid,/tmp/IRAS_GO.fits',['IRAC3','SPIRE2']
;    help,!dustem_grid
; MODIFICATION HISTORY:
;    Written by J.-Ph. Bernard (2023)
;    Evolution details on the DustEMWrap gitlab.
;    See http://dustemwrap.irap.omp.eu/ for FAQ and help.  
;-

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

;=== read the grid table
message,'reading grid data from '+table_name,/continue
;stop
info=file_info(table_name)
IF info.exists THEN BEGIN
	st=mrdfits(table_name,1,h)
ENDIF ELSE BEGIN
	message,'grid data '+table_name+' not found',/continue
	stop
ENDELSE

;stop
;=== get filters and params names from grid table header
i=0L
table_filters=['']
fname='bidon'
count=1
WHILE count NE 0 DO BEGIN
	fname=sxpar(h,'FNAME'+strtrim(i+1,2),count=count)
  IF count NE 0 THEN table_filters=[table_filters,strtrim(fname,2)]
  i=i+1
ENDWHILE
table_filters=table_filters[1:*]
Ntfilters=n_elements(table_filters)

table_params=['']
table_pmins=[0.d0]
table_pmaxs=[0.d0]
table_pnvals=[0L]
table_plogs=[0]
count=1
i=0L
WHILE count NE 0 DO BEGIN
	pname=sxpar(h,'PNAME'+strtrim(i+1,2),count=count)
    IF count NE 0 THEN BEGIN
    	table_params=[table_params,strtrim(pname,2)]
    	pmin=sxpar(h,'PMIN'+strtrim(i+1,2),count=count)
    	pmax=sxpar(h,'PMAX'+strtrim(i+1,2),count=count)
    	pnval=sxpar(h,'PNVAL'+strtrim(i+1,2),count=count)
    	plog=sxpar(h,'PLOG'+strtrim(i+1,2),count=count)
    	table_pmins=[table_pmins,pmin]
    	table_pmaxs=[table_pmaxs,pmax]
    	table_pnvals=[table_pnvals,pnval]
    	table_plogs=[table_plogs,plog]
    ENDIF
    i=i+1
ENDWHILE
table_params=table_params[1:*]
table_pmins=table_pmins[1:*]
table_pmaxs=table_pmaxs[1:*]
table_pnvals=table_pnvals[1:*]
table_plogs=table_plogs[1:*]
Ntparams=n_elements(table_params)

;=== select only requested filters
;stop
use_filters=table_filters   ;default is to keep all filters
IF keyword_set(filters) THEN use_filters=filters

Nfilters=n_elements(use_filters)
keep_tfilters=intarr(Ntfilters)
tags=tag_names(st)
FOR i=0L,Nfilters-1 DO BEGIN
    ind=where(table_filters EQ use_filters[i],count)
    IF count NE 0 THEN keep_tfilters[ind]=1
ENDFOR

;stop

;totals=st.total
FOR i=0L,Ntfilters-1 DO BEGIN
	IF keep_tfilters[i] EQ 0 THEN BEGIN
		col_name=tags[i+Ntparams]
		st=structure_remove_column(st,col_name)
	ENDIF
ENDFOR

;separate structure into parameters and seds
st_seds=st
tags=tag_names(st)
FOR i=0L,Ntparams-1 DO BEGIN
	col_name=tags[i]
	st_seds=structure_remove_column(st_seds,col_name)
ENDFOR
;stop
st_seds=structure_remove_column(st_seds,'TOTAL')
st_params=st
FOR i=0L,Nfilters-1 DO BEGIN
	col_name=tags[i+Ntparams]
	st_params=structure_remove_column(st_params,col_name)
ENDFOR
st_params=structure_remove_column(st_params,'TOTAL')
sed_totals=st.(Nfilters+Ntparams-1)

;stop

;==== recompute totals
Nsed=n_elements(st)
;==== recompute totals (needed if some columns have been removed)
FOR i=0L,Nsed-1 DO BEGIN
	this_total=0.d0
	FOR j=0L,Nfilters-1 DO BEGIN
		;this_total=this_total+(st[i]).(j+Ntparams)
		this_total=this_total+st_seds[i].(j)
	ENDFOR
	;st[i].total=this_total
	sed_totals[i]=this_total
ENDFOR

;stop

dustem_grid={Nsed:Nsed $ ;
					  ,Nfilters:Nfilters $ ;
					  ,Nparams:Ntparams $  ;
					  ,filters:use_filters $   ;
					  ,params:table_params $;
					  ,st_seds:st_seds $          ;
					  ,st_params:st_params $      ;
					  ,sed_totals:sed_totals $     ;
;					  ,seds:st $                  ;meant to disapear
					  ,pmin_values:table_pmins $  ;
					  ,pmax_values:table_pmaxs $  ;
					  }

defsysv,'!dustem_grid',ptr_new(dustem_grid)
message,'Defined !dustem_grid',/continue

;help,!dustem_grid
;** Structure <9b80dc08>, 10 tags, length=3760784, data length=3760780, refs=2:
;   NSED            LONG             20000
;   NFILTERS        LONG                42
;   NPARAMS         LONG                 3
;   FILTERS         STRING    Array[42]
;   PARAMS          STRING    Array[3]
;   ST_SEDS         STRUCT    -> <Anonymous> Array[20000]
;   ST_PARAMS       STRUCT    -> <Anonymous> Array[20000]
;   SED_TOTALS      DOUBLE    Array[20000]
;   PMIN_VALUES     DOUBLE    Array[3]
;   PMAX_VALUES     DOUBLE    Array[3]

the_end:

END