dustem_make_fits_predicted_sed.pro 5.23 KB
FUNCTION dustem_make_fits_predicted_sed ,syst_var $
									    ,dustem_predicted_sed $
									    ,str_input_sed=str_input_sed $
									    ,dustem_predicted_Qsed=dustem_predicted_Qsed $
									    ,dustem_predicted_Used=dustem_predicted_Used $
									    ,help=help

;+
; NAME:
;    dustem_make_fits_predicted_sed
; PURPOSE:
;    extracts dustemwrap predicted SED from dustem system variable syst_var, to be put into fits files
; CATEGORY:
;    DustEMWrap, Distributed, High-Level, User Convenience
; CALLING SEQUENCE:
;    str_predicted_SED=dustem_make_fits_predicted_sed(syst_var,dustem_predicted_sed[,str_input_sed=])
;    see dustem_write_fits_table.pro
; INPUTS:
;    syst_var  : dustem system variable (*!dustem_data or *!dustem_show)
;    what      = which tag of syst_var must be treated (can be sed, qsed, used, ...)
;    dustem_predicted_sed : dustemwrap predicted SED as output by dustem_compute_sed.pro
; OPTIONAL INPUT PARAMETERS:
;    dustem_predicted_Qsed : Stokes Q SED
;    dustem_predicted_Used : Stokes U SED
; OUTPUTS:
;    str_predicted_SED : dustemwrap predicted SED as used to store into fits file
; OPTIONAL OUTPUT PARAMETERS:
;    str_input_sed : dustem wrap input SED
; ACCEPTED KEY-WORDS:
;    help      = If set, print this help
; COMMON BLOCKS:
;    None
; SIDE EFFECTS:
;    None
; RESTRICTIONS:
;    The DustEMWrap IDL code must be installed
; PROCEDURE:
;    None
; EXAMPLES:
;    str_predicted_SED=dustem_make_fits_predicted_sed(*!dustem_data,dustem_predicted_sed,str_input_sed=str_input_sed)
; MODIFICATION HISTORY:
;    Written by J.-Ph. Bernard Dec 2022
;    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_make_fits_predicted_sed'
  str_predicted_SED=-1
  goto,the_end
ENDIF

;example:
;str_predicted_SED=dustem_make_fits_predicted_sed(*!dustem_data,str_input_sed=str_input_sed)

;===== define structures containing input and predicted SEDs
one_str_input_SED={FILTER:'',Wavelength:la_undef(4),STOKESI:la_undef(5),STOKESQ:la_undef(5),STOKESU:la_undef(5),varianceII:la_undef(5),varianceQQ:la_undef(5),varianceUU:la_undef(5)}
one_str_predicted_SED={FILTER:'',Wavelength:la_undef(4),STOKESI:la_undef(5),STOKESQ:la_undef(5),STOKESU:la_undef(5)}

;tags=tag_names(syst_var)
;ind_what=where(tags EQ strupcase(what),count_what)

;stop

Nsed=n_elements((*syst_var.sed).filt_names)
str_input_SED=replicate(one_str_input_SED,Nsed)
str_input_SED.FILTER=(*syst_var.sed).filt_names
str_input_SED.Wavelength=(*syst_var.sed).wav
str_input_SED.STOKESI=(*syst_var.sed).values
str_input_SED.varianceII=la_power((*syst_var.sed).sigma,2)   ;This is the variance

N_predicted_sed=n_elements(dustem_predicted_sed)
str_predicted_SED=replicate(one_str_predicted_SED,N_predicted_sed)
;str_predicted_EXT=replicate(one_str_predicted_SED,N_predicted_sed)
fully_undefined_sed=str_predicted_SED
str_predicted_SED.FILTER=(*syst_var.sed).filt_names    ;taken from input SED
str_predicted_SED.Wavelength=(*syst_var.sed).wav       ;taken from input SED
str_predicted_SED.STOKESI=dustem_predicted_sed    
;stop
IF !run_pol THEN BEGIN
	qsed=aos2soa(*syst_var.qsed)
	ind=where(qsed.filt_names NE 'SPECTRUM',count,complement=inds,Ncomplement=counts)
	IF count NE 0 THEN BEGIN
		qsed=qsed[ind]
		FOR i=0L,n_elements(qsed.wav)-1 DO BEGIN
			ind=where(str_input_SED.FILTER EQ qsed[i].filt_names,count)
			IF count NE 0 THEN BEGIN
				str_input_SED[ind[0]].STOKESQ=qsed[i].values
				str_input_SED[ind[0]].varianceQQ=la_power(qsed[i].sigma,2.)
			ENDIF
			str_predicted_SED[ind[0]].STOKESQ=dustem_predicted_Qsed[i]
		ENDFOR
	ENDIF
	qsed=aos2soa(*syst_var.qsed)
	IF counts NE 0 THEN BEGIN
		qsed=qsed[inds]
		ind2=where(str_predicted_SED.filter EQ 'SPECTRUM',count2)
		str_predicted_SED[ind2].STOKESQ=interpol(qsed.values,qsed.wav,str_predicted_SED[ind2].Wavelength)
		str_predicted_SED[ind2].varianceQQ=interpol(la_power(qsed.sigma,2.),qsed.wav,str_predicted_SED[ind2].Wavelength)
	ENDIF
	used=aos2soa(*syst_var.used)
	ind=where(used.filt_names NE 'SPECTRUM',count,complement=inds,Ncomplement=counts)
	IF count NE 0 THEN BEGIN
		used=used[ind]
		FOR i=0L,n_elements(used.wav)-1 DO BEGIN
			ind=where(str_input_SED.FILTER EQ used[i].filt_names,count)
			IF count NE 0 THEN BEGIN
				str_input_SED[ind[0]].STOKESU=used[i].values
				str_input_SED[ind[0]].varianceUU=la_power(used[i].sigma,2.)
			ENDIF
			str_predicted_SED[ind[0]].STOKESU=dustem_predicted_Used[i]
		ENDFOR
	ENDIF
	used=aos2soa(*syst_var.used)
	IF counts NE 0 THEN BEGIN
		used=used[inds]
		ind2=where(str_predicted_SED.filter EQ 'SPECTRUM',count2)
		str_predicted_SED[ind2].STOKESU=interpol(used.values,used.wav,str_predicted_SED[ind2].Wavelength)
		str_predicted_SED[ind2].varianceUU=interpol(la_power(used.sigma,2.),used.wav,str_predicted_SED[ind2].Wavelength)
	ENDIF
    ;stop
	;str_predicted_SED.Q=dustem_predicted_Qsed
	;str_predicted_SED.U=dustem_predicted_Used
	;str_predicted_SED.Q=interpol(dustem_predicted_Qsed,(*(*!dustem_data).qsed).wav,(*(*!dustem_data).sed).wav)
	;str_predicted_SED.U=interpol(dustem_predicted_Used,(*(*!dustem_data).used).wav,(*(*!dustem_data).sed).wav)
	;ENDFOR
ENDIF ELSE BEGIN
	str_predicted_SED.STOKESQ=fully_undefined_sed.STOKESQ
	str_predicted_SED.STOKESU=fully_undefined_sed.STOKESU
ENDELSE

the_end:

RETURN,str_predicted_SED

END