Skip to content

Commit

Permalink
Merge branch 'activ_dev' of https://github.com/exoAtmospheres/ForMoSA
Browse files Browse the repository at this point in the history
…into activ_dev
  • Loading branch information
Allan Denis committed Oct 28, 2024
2 parents 757f5dc + 0ab3bbe commit 877b569
Show file tree
Hide file tree
Showing 72 changed files with 6,003 additions and 998 deletions.
434 changes: 179 additions & 255 deletions ForMoSA/adapt/adapt_grid.py

Large diffs are not rendered by default.

107 changes: 27 additions & 80 deletions ForMoSA/adapt/adapt_obs_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from adapt.extraction_functions import extract_observation
from adapt.adapt_grid import adapt_grid
from main_utilities import diag_mat
import glob
# ----------------------------------------------------------------------------------------------------------------------

Expand All @@ -33,10 +32,15 @@ def launch_adapt(global_params, justobs='no'):
res_mod_nativ = attr['res']
ds.close()

# Check if the spectrum is Nyquist-sampled, else set the resolution to R = wav / 2 Deltawav
dwav = np.abs(wav_mod_nativ - np.roll(wav_mod_nativ, 1))
dwav[0] = dwav[1]
res_Nyquist = wav_mod_nativ / (2 * dwav)
res_mod_nativ[np.where(res_mod_nativ > res_Nyquist)] = res_Nyquist[np.where(res_mod_nativ > res_Nyquist)]

# Extract the data from the observation files
main_obs_path = global_params.main_observation_path


for indobs, obs in enumerate(sorted(glob.glob(main_obs_path))):

global_params.observation_path = obs
Expand All @@ -48,93 +52,37 @@ def launch_adapt(global_params, justobs='no'):
print(obs_name + ' will have a R=' + global_params.continuum_sub[indobs] + ' continuum removed using a '
+ global_params.wav_for_continuum[indobs] + ' wavelength range')
print()
obs_spectro, obs_photo, obs_spectro_ins, obs_photo_ins, obs_opt = extract_observation(global_params, wav_mod_nativ, res_mod_nativ, 'yes',
obs_spectro, obs_photo, obs_photo_ins, obs_opt = extract_observation(global_params, wav_mod_nativ, res_mod_nativ, 'yes',
obs_name=obs_name, indobs=indobs)
else:
obs_spectro, obs_photo, obs_spectro_ins, obs_photo_ins, obs_opt = extract_observation(global_params, wav_mod_nativ, res_mod_nativ,
obs_spectro, obs_photo, obs_photo_ins, obs_opt = extract_observation(global_params, wav_mod_nativ, res_mod_nativ,
obs_name=obs_name, indobs=indobs)


# Merging of each sub-spectrum and interpolating the grid
for c, cut in enumerate(obs_spectro):

if len(cut[0]) > 0:
# Interpolate the resolution onto the wavelength of the data
ind_mod_obs = np.where((wav_mod_nativ <= cut[0][-1]) & (wav_mod_nativ > cut[0][0]))
wav_mod_cut = wav_mod_nativ[ind_mod_obs]
res_mod_cut = res_mod_nativ[ind_mod_obs]
interp_mod_to_obs = interp1d(wav_mod_cut, res_mod_cut, fill_value='extrapolate')
res_mod_cut = interp_mod_to_obs(cut[0])

if c == 0:
wav_obs_extract = obs_spectro[c][0]
flx_obs_extract = obs_spectro[c][1]
err_obs_extract = obs_spectro[c][2]
res_obs_extract = obs_spectro[c][3]
cov_obs_extract = obs_opt[c][0]
transm_obs_extract = obs_opt[c][1]
star_flx_obs_extract = obs_opt[c][2]
system_obs_extract = obs_opt[c][3]
# Save the interpolated resolution of the grid
res_mod_obs_merge = [res_mod_cut]

else:
wav_obs_extract = np.concatenate((wav_obs_extract, obs_spectro[c][0]))
flx_obs_extract = np.concatenate((flx_obs_extract, obs_spectro[c][1]))
err_obs_extract = np.concatenate((err_obs_extract, obs_spectro[c][2]))
res_obs_extract = np.concatenate((res_obs_extract, obs_spectro[c][3]))
if len(cov_obs_extract) != 0:
cov_obs_extract = diag_mat([cov_obs_extract, obs_opt[c][0]])
if len(transm_obs_extract) != 0:
transm_obs_extract = np.concatenate((transm_obs_extract, obs_opt[c][1]))
if len(star_flx_obs_extract) != 0:
star_flx_obs_extract = np.concatenate((star_flx_obs_extract, obs_opt[c][2]), axis=0)
if len(system_obs_extract) != 0:
system_obs_extract = np.concatenate((system_obs_extract, obs_opt[c][3]), axis=0)
# Save the interpolated resolution of the grid
res_mod_obs_merge.append(res_mod_cut)



# Compute the inverse of the merged covariance matrix (note: inv(C1, C2) = (in(C1), in(C2)) if C1 and C2 are block matrix on the diagonal)
# if necessary
if len(cov_obs_extract) != 0:
inv_cov_obs_extract = np.linalg.inv(cov_obs_extract)
else:
inv_cov_obs_extract = np.asarray([])

# Check-ups and warnings for negative values in the diagonal of the covariance matrix
if len(cov_obs_extract) != 0 and any(np.diag(cov_obs_extract) < 0):
print()
print("WARNING: Negative value(s) is(are) present on the diagonal of the covariance matrix.")
print("Operation aborted.")
print()
exit()

else:
wav_obs_extract, flx_obs_extract, err_obs_extract, res_obs_extract = [], [], [], []
inv_cov_obs_extract, transm_obs_extract, star_flx_obs_extract, system_obs_extract = [], [], [], []
res_mod_obs_merge = res_mod_nativ

# Compile everything and changing data type to object to allow for different array sizes
obs_spectro_merge = np.asarray([wav_obs_extract, flx_obs_extract, err_obs_extract, res_obs_extract])
obs_spectro = np.asarray(obs_spectro, dtype=object)
obs_spectro_ins = np.asarray(obs_spectro_ins, dtype=object)
obs_photo = np.asarray(obs_photo, dtype=object)
obs_photo_ins = np.asarray(obs_photo_ins, dtype=object)
obs_opt_merge = np.asarray([inv_cov_obs_extract, transm_obs_extract, star_flx_obs_extract, system_obs_extract], dtype=object)



# Interpolate the resolution onto the wavelength of the data
if len(obs_spectro[0]) != 0:
mask_mod_obs = (wav_mod_nativ <= obs_spectro[0][-1]) & (wav_mod_nativ > obs_spectro[0][0])
wav_mod_cut = wav_mod_nativ[mask_mod_obs]
res_mod_cut = res_mod_nativ[mask_mod_obs]
interp_mod_to_obs = interp1d(wav_mod_cut, res_mod_cut, fill_value='extrapolate')
res_mod_obs = interp_mod_to_obs(obs_spectro[0])
else:
res_mod_obs = np.asarray([])

# Check-ups and warnings for negative values in the diagonal of the covariance matrix
if len(obs_opt[0]) != 0 and any(np.diag(obs_opt[0]) < 0):
print()
print("WARNING: Negative value(s) is(are) present on the diagonal of the covariance matrix.")
print("Operation aborted.")
print()
exit()

# Save the new data spectrum
np.savez(os.path.join(global_params.result_path, f'spectrum_obs_{obs_name}.npz'),
obs_spectro_merge=obs_spectro_merge,
obs_spectro=obs_spectro,
obs_spectro_ins=obs_spectro_ins,
obs_photo=obs_photo,
obs_photo_ins=obs_photo_ins,
obs_opt_merge=obs_opt_merge) # Optional arrays kept separatly
obs_opt=obs_opt) # Optional arrays kept separatly

# Adaptation of the model grid
if justobs == 'no':
Expand All @@ -155,9 +103,8 @@ def launch_adapt(global_params, justobs='no'):
print('- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -')
print(f"-> Sarting the adaptation of {obs_name}")

adapt_grid(global_params, obs_spectro_merge[0], obs_photo[0], res_mod_obs_merge, obs_name=obs_name, indobs=indobs)
adapt_grid(global_params, res_mod_obs, obs_spectro[0], obs_spectro[3], obs_photo[0], obs_photo_ins, obs_name=obs_name, indobs=indobs)


# ----------------------------------------------------------------------------------------------------------------------


Expand Down
Loading

0 comments on commit 877b569

Please sign in to comment.