dustem_define_grid.pro 5.04 KB
PRO dustem_define_grid,table_name $
										  ,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
; PURPOSE:
;    loads an SED Grid table into !dustem_grid
; 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'
  goto,the_end
ENDIF

;=== read the grid table
message,'reading grid data from '+table_name,/continue
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
Nfilters=n_elements(filters)
keep_tfilters=intarr(Ntfilters)
tags=tag_names(st)
FOR i=0L,Nfilters-1 DO BEGIN
    ind=where(table_filters EQ 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
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: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',dustem_grid
message,'Defined !dustem_grid',/continue

the_end:

END