micappe / ppxf_examples

30 stars 9 forks source link

Problem Fitting PPAK/PPMAS data #2

Open aquiles300 opened 7 months ago

aquiles300 commented 7 months ago

Hello, I used ppxf for fitting my data, but it does not fit pretty good.

WhatsApp Image 2024-02-28 at 09 59 24

Figure_2

I followed instruction as ppxf_example_high_redshift.ipynb, but the fit is not correct, and I am not sure how to improve it. My data is getting from a PPAK/PMAS cube.

I would appretiate any help, please

spec.csv

from os import path
from urllib import request
import matplotlib.pyplot as plt
import numpy as np
from ppxf.ppxf import ppxf
import ppxf.ppxf_util as util
import ppxf.sps_util as lib

#STARLIGHT setting parameters
fnorm= 6.149655E-01 #from STARLIGHT for this particular spectrum
lnorm = [5075,5125]
start = [400, 150.]

# cinematic components for CALIFA sample: see Falcon-Barroso et al. 2017
R = 850 #For V500
#R = 1650 #For V1200
FWHM_gal = 6 # For V500
#FWHM_gal = 2.3 # For V1200
c = 299792.458  # speed of light in km/s
sigma_inst = c/(R * 2.355)

# Read a galaxy spectrum and define the wavelength range
data = np.loadtxt("spec.out",skiprows=1)
lam = data[:,0]
galaxy = fnorm*data[:,1]
galaxy = galaxy/np.median(galaxy)   # Normalize the spectrum to avoid numerical issues
noise = np.full_like(galaxy, 0.5)

# Read the list of filenames from the MILES library
#sps_name = 'emiles'
#sps_name = 'galaxev'
sps_name = 'fsps'
velscale = c*np.diff(np.log(lam[[0,-1]])/(lam.size-1))
velscale = velscale[0] # must be scalar
print(velscale)
FWHM_temp = 2.51 # template emiles
ppxf_dir = path.dirname(path.realpath(lib.__file__))
basename = f"spectra_{sps_name}_9.0.npz"
filename = path.join(ppxf_dir, 'sps_models', basename)
if not path.isfile(filename):
    url = "https://raw.githubusercontent.com/micappe/ppxf_data/main/" + basename
    request.urlretrieve(url, filename)

sps = lib.sps_lib(filename, velscale, norm_range=lnorm, age_range=[0.0, 13.5])
#esta parte es todavia un poco oscura para mi, no se por que hacen esto
reg_dim = sps.templates.shape[1:]
stars_templates = sps.templates.reshape(sps.templates.shape[0], -1)
#creating emission lines templates
lam_range_gal = [np.min(lam), np.max(lam)]
gas_templates, gas_names, line_wave = util.emission_lines(sps.ln_lam_temp, lam_range_gal, FWHM_gal, tie_balmer=1)
templates = np.column_stack([stars_templates, gas_templates])

# Setup pPXF parameters
n_stars = stars_templates.shape[1]
n_gas = len(gas_names)
component = [0]*n_stars + [1]*n_gas
gas_component = np.array(component) > 0
# Fit (V, si;g) moments=2 for both the stars and the gas
moments = [2, 2]
# Adopt the same starting value for both the stars and the gas components
start = [start, start]

# Start pPXF fit
pp = ppxf(templates, galaxy, noise, velscale, start,
          moments=moments, degree=-1, mdegree=-1, lam=lam, lam_temp=sps.lam_temp,
          reg_dim=reg_dim, component=component, gas_component=gas_component,
          reddening=0, gas_reddening=0, gas_names=gas_names)

plt.figure(figsize=(15, 5))
pp.plot()
plt.show()