dustem_grids_extend_parameter_range.pro 6.8 KB
PRO dustem_grids_extend_parameter_range,input_table_name,pd_add,Nv_add $
									   ,what_do_we_add=what_do_we_add $
									   ,show_seds=show_seds $
									   ,print_params=print_params $
									   ,force_grain_keywords=force_grain_keywords $
									   ,output_table_name=output_table_name

;input_table_name='/Volumes/PILOT_FLIGHT1/PHANGS//ISRF/GRIDS/DL07_logn_MuseISRF_JWST_adaptedG0_Ypah1added_Yvsgadded_4Phangs_noionis_isrfclass26.fits'
;force_grain_keywords=['logn','logn',replicate('?',3)]
;dustem_grids_extend_parameter_range,input_table_name,'(*!dustem_params).grains[1].MDUST_O_MH',2,force_grain_keywords=force_grain_keywords

	dustem_read_grid_table2arrays,input_table_name $
								  ,Ngrains,model,old_Ncomb,NFparams,INCSP,use_polar,use_double $
								  ,old_parameter_description,old_parameter_pmin,old_parameter_pmax,old_parameter_pnval,old_parameter_plog,old_fparameter_description,old_fparameter_value $
								  ,old_param_values,old_Nparams,old_seds,filters,Nfilters,wavs,Nwavs $
								  ,old_I_tot_vec,old_ext_tot_vec,old_I_grains_vec,old_ext_grains_vec,grain_keywords=old_grain_keywords
    ;stop
    ;print,Ngrains,model,old_Ncomb,NFparams
    ;print,old_parameter_description,old_parameter_pmin,old_parameter_pmax,old_parameter_pnval,old_parameter_plog

    ;=== decide which dustemwrap parameters are linear
    ind_non_linear=dustem_where_non_linear(old_parameter_description,count_non_linear,complement=ind_linear,Ncomplement=count_linear,non_linear_mask=non_linear_mask)
    
	use_what_do_we_add='larger'
	IF keyword_set(what_do_we_add) THEN use_what_do_we_add=strlowcase(what_do_we_add)

    ind_add=where(old_parameter_description EQ pd_add,count_add)
    param_is_non_linear=non_linear_mask[ind_add[0]]               ;This is 1 if the parameter to be added is non-linear

    IF count_add EQ 0 THEN BEGIN
    	message,'parameter description to be added not found in grid parameter description',/continue
    	stop
    ENDIF ELSE BEGIN
    	old_add_parameter_pmin=old_parameter_pmin[ind_add[0]]
    	old_add_parameter_pmax=old_parameter_pmax[ind_add[0]]
    	old_add_parameter_pnval=old_parameter_pnval[ind_add[0]]
    	old_add_parameter_plog=old_parameter_plog[ind_add[0]]
    	old_add_param_values=old_param_values.(ind_add[0])
    	order=sort(old_add_param_values)
    	old_add_param_values=old_add_param_values[order]
    	un=uniq(old_add_param_values)
    	old_add_param_values=old_add_param_values[un]
    ENDELSE
    add_parameter_values=fltarr(Nv_add)

    CASE use_what_do_we_add OF
    	'lower':BEGIN
    	   iv_max=old_add_parameter_pmin
    	   iv_Nvalues=Nv_add+1
    	   add_plog=old_add_parameter_plog
    	   IF add_plog EQ 1 THEN BEGIN
    	   	 use_values=alog10(old_add_param_values)
    	   ENDIF ELSE BEGIN
    	   	 use_values=old_add_param_values
    	   ENDELSE
    	   vvec=use_values-shift(use_values,1)
    	   dv=median(vvec)
    	   IF add_plog EQ 1 THEN BEGIN
    	   	 iv_min=10.^(alog10(iv_max)-dv*iv_Nvalues)
    	   ENDIF ELSE BEGIN
    	   	iv_min=iv_max-dv*iv_Nvalues
    	   ENDELSE
    	   ;iv_min=
    	   add_parameter_values_ptr=dustem_param_range2param_values([iv_min],[iv_max],[iv_Nvalues],Nc=Nc,log=[add_plog])
    	   ifrom=0L
    	   ito=Nv_add-1
           FOR i=ifrom,ito DO BEGIN
           	 add_parameter_values[i-ifrom]=*add_parameter_values_ptr[i]
           ENDFOR
    	   print,old_add_param_values
    	   print,add_parameter_values
    	   ;stop
    	END
    	'larger':BEGIN
    	   iv_min=old_add_parameter_pmax
    	   iv_Nvalues=Nv_add+1
    	   add_plog=old_add_parameter_plog
    	   IF add_plog EQ 1 THEN BEGIN
    	   	 use_values=alog10(old_add_param_values)
    	   ENDIF ELSE BEGIN
    	   	 use_values=old_add_param_values
    	   ENDELSE
    	   vvec=use_values-shift(use_values,1)
    	   dv=median(vvec)
    	   IF add_plog EQ 1 THEN BEGIN
    	   	 iv_max=10.^(alog10(iv_min)+dv*iv_Nvalues)
    	   ENDIF ELSE BEGIN
    	   	iv_max=iv_min+dv*iv_Nvalues
    	   ENDELSE
    	   ;iv_min=
    	   add_parameter_values_ptr=dustem_param_range2param_values([iv_min],[iv_max],[iv_Nvalues],Nc=Nc,log=[add_plog])
    	   ifrom=1L
    	   ito=Nv_add
           FOR i=ifrom,ito DO BEGIN
           	 add_parameter_values[i-ifrom]=*add_parameter_values_ptr[i]
           ENDFOR
    	   print,old_add_param_values
    	   print,add_parameter_values
    	   ;stop
    	END
    ENDCASE
    ;=== Compute the min and max values for parameter to be extended
    add_iv_min=add_parameter_values[0]
    add_iv_max=add_parameter_values[Nv_add-1]

    ;=== merge parameter range values
    add_model=model
    add_parameters_description=old_parameter_description
	add_iv_min=old_parameter_pmin
	add_iv_min[ind_add]=iv_min
	add_iv_max=old_parameter_pmax
	add_iv_max[ind_add]=iv_max
    add_iv_Nvalues=old_parameter_pnval
    add_iv_Nvalues[ind_add]=Nv_add
    add_plog=old_add_parameter_plog
	add_fpd=old_fparameter_description
	add_fiv=old_fparameter_value
	add_grain_keywords=old_grain_keywords
	IF keyword_set(force_grain_keywords) THEN BEGIN
		add_grain_keywords=force_grain_keywords
	ENDIF
    add_filters=filters
    add_plog=old_parameter_plog
    
    ;try_add_parameter_values=dustem_param_range2param_values(add_iv_min,add_iv_max,add_iv_Nvalues,Nc=Nc,log=add_plog)
    ;stop
    add_table_name='/tmp/add_grid.fits'
    ;=== decide which parameters are linear
    ;IF param_is_non_linear THEN BEGIN ;This is if the parameter to be added is non-linear
        ;== make table for non-linear parameters
    	message,'========================== Making added grid for non-linear parameters ...',/continue
    	dustem_make_sed_table,add_model $
    					     ,add_parameters_description[ind_non_linear] $
							 ,add_iv_min[ind_non_linear] $
						     ,add_iv_max[ind_non_linear] $
							 ,add_iv_Nvalues[ind_non_linear] $
						  	 ,fpd=add_fpd $
						  	 ,fiv=add_fiv $
						  	 ,grain_keywords=add_grain_keywords $
						  	 ,filename=add_table_name $
						  	 ,filters=add_filters $
						  	 ,plog=add_plog[ind_non_linear] $
						  	 ,show_seds=show_seds $
						  	 ,print_params=print_params $
						  	 ,help=help
	    ;== extend table for linear parameter
    	message,'========================== Making added grid for linear parameters ...',/continue
	    dustem_add_linear_params2grid,add_table_name $
	    							 ,add_parameters_description[ind_linear] $
	    							 ,add_iv_min[ind_linear] $
	    							 ,add_iv_max[ind_linear] $
	    							 ,add_iv_Nvalues[ind_linear] $
	    							 ,out_filename=add_table_name $
	    							 ,plog=add_plog[ind_linear] $
	    							 ,show_seds=show_seds
    ;ENDIF ELSE BEGIN

	;ENDELSE

    ;=== merge tables
    use_output_table_name='/tmp/add_ext_grid.fits'
    IF keyword_set(output_table_name) THEN use_output_table_name=output_table_name

    message,'========================== Merging grids ...',/continue
	dustem_merge_grids,input_table_name,add_table_name,output_table_name
	
	stop
						  				
END