Barycentric correction shifts not applying to spectrum - python code

22 Views Asked by At

I am attempting to apply a barycentric correction in this code to ensure that all the absorption are overlapping in the one plot. For some reason this is not happening. I adopted the code from matlab and converted it to python.

def shift_vel(wave, vel): 
    c = 299792458  # speed of light in m/s 
    shift = np.log(1 + vel / c) 
    wave = np.exp(np.log(wave) - shift) 
    return wave 

bcorr_fib4_all = np.random.rand(num_observations) 

# Initialize ALLwv with the appropriate dimensions 
ALLwv = np.zeros((num_orders, num_pixels, num_observations)) 
 
# Barycentric correction 
for s in range(ALLfib4spec.shape[2]):
    tWVout = ALLfib4specwave[:,:,s]
    for ord in range(tWVout.shape[0]):
        tWVout[ord,:] = shift_vel(tWVout[ord,:], bcorr_fib4_all[s]*-1)
    # dimensions are: [orders pixels observations]
    ALLwv[:,:,s] = tWVout


# Define order to plot 
order = 22
     
# Define observation to plot 
observation = 12 
     
# Extract wavelengths and intensities for the chosen order and observation 
Tw = np.transpose(ALLwv[order, :, :]) 
Tint = np.transpose(nALLfib4specbNc[order, :, :]) 

# Create the log step wavelength axis
c = 299792458
logstep = 1e-6
a = 8
velstep = (np.exp(a + logstep) - np.exp(a)) / np.exp(a) * c
lw = np.arange(np.log(np.min(Tw[:, 0]) + 2), np.log(np.max(Tw[:, 0]) - 2), logstep)


# Check for valid values in Tw[:, 1] 
min_value = np.min(Tw[:, 1]) 
max_value = np.max(Tw[:, 1]) 
 
# Check for invalid values in Tw[:, 1]
invalid_indices = np.where(Tw[:, 1] <= 0)[0]

if invalid_indices.size > 0:
    # Create a mask to exclude invalid observations
    valid_indices = np.setdiff1d(np.arange(Tw.shape[0]), invalid_indices)
    # Update Tw and Tint to include only valid observations
    Tw = Tw[valid_indices, :]
    Tint = Tint[valid_indices, :]
0

There are 0 best solutions below