|
| 1 | +from os.path import exists, join |
| 2 | +from os import symlink |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from astropy.io import fits |
| 6 | +from astropy.table import Table |
| 7 | + |
| 8 | +from ..logging import log_stdout |
| 9 | +from .. import line_name |
| 10 | +from ..column_names import filename_dict |
| 11 | + |
| 12 | + |
| 13 | +def main(infile: str, log=None): |
| 14 | + """ |
| 15 | + Import previous DEEP2 Ly et al. (2015) dataset and export |
| 16 | + an astropy Table called bin_fit.tbl |
| 17 | +
|
| 18 | + :param infile: Input filename |
| 19 | + :param log: LogClass or logging object |
| 20 | + """ |
| 21 | + if log is None: |
| 22 | + log = log_stdout() |
| 23 | + |
| 24 | + log.info(f"Reading : {infile}") |
| 25 | + orig_tab = fits.getdata(infile) |
| 26 | + |
| 27 | + gauss_cols = [str0 + '_Flux_Gaussian' for str0 in line_name] |
| 28 | + obs_cols = [str0 + '_Flux_Observed' for str0 in line_name] |
| 29 | + flux_rms_cols = [str0 + '_RMS' for str0 in line_name] |
| 30 | + sigma_cols = [str0 + '_Sigma' for str0 in line_name] |
| 31 | + snr_cols = [str0 + '_S/N' for str0 in line_name] |
| 32 | + |
| 33 | + # Column name in orig_table corresponding to gauss_cols |
| 34 | + prefixes = ['OII', 'HD', 'HG', 'OIIIA', 'HB', 'OIIIB', 'OIIIR'] |
| 35 | + orig_gauss_cols = [str0 + '_FLUX_MOD' for str0 in prefixes] |
| 36 | + orig_obs_cols = [str0 + '_FLUX_DATA' for str0 in prefixes] |
| 37 | + orig_sigma_cols = [str0 + '_SIGMA' for str0 in prefixes] |
| 38 | + orig_snr_cols = [str0 + '_SNR' for str0 in prefixes] |
| 39 | + |
| 40 | + reformatted_tab = Table() |
| 41 | + reformatted_tab['bin_ID'] = 1 + np.arange(len(orig_tab)) |
| 42 | + |
| 43 | + for ii in range(len(gauss_cols)): |
| 44 | + flux = orig_tab[orig_gauss_cols[ii]] |
| 45 | + rms = flux / orig_tab[orig_snr_cols[ii]] |
| 46 | + # Widths are in observed frame, shift to rest-frame |
| 47 | + gauss_sig = orig_tab[orig_sigma_cols[ii]] / (1+orig_tab['ZSPEC']) |
| 48 | + reformatted_tab[gauss_cols[ii]] = flux |
| 49 | + reformatted_tab[obs_cols[ii]] = orig_tab[orig_obs_cols[ii]] |
| 50 | + reformatted_tab[flux_rms_cols[ii]] = rms |
| 51 | + if 'OIII_4363' in sigma_cols[ii]: |
| 52 | + reformatted_tab[sigma_cols[ii]] = gauss_sig |
| 53 | + reformatted_tab[snr_cols[ii]] = orig_tab[orig_snr_cols[ii]] |
| 54 | + |
| 55 | + out_dir = "tests_data/DEEP2_Ly2015" |
| 56 | + output_table = "DEEP2_Ly2015_fluxes.tbl" |
| 57 | + out_file = join(out_dir, output_table) |
| 58 | + log.info(f"Writing: {out_file}") |
| 59 | + reformatted_tab.write(out_file, format='ascii.fixed_width_two_line', |
| 60 | + overwrite=True) |
| 61 | + |
| 62 | + symlink_file = join(out_dir, filename_dict['bin_fit']) |
| 63 | + if not exists(symlink_file): |
| 64 | + symlink(output_table, symlink_file) |
| 65 | + log.info(f"Symlink created to : {output_table}") |
| 66 | + else: |
| 67 | + log.info(f"Symlink exists: {symlink_file}") |
| 68 | + |
| 69 | + # Write bin_info file to use with valid_table module |
| 70 | + info_tab = Table() |
| 71 | + info_tab['bin_ID'] = 1 + np.arange(len(orig_tab)) |
| 72 | + info_tab['N_stack'] = np.repeat(1, len(orig_tab)) |
| 73 | + info_file = join(out_dir, filename_dict['bin_info']) |
| 74 | + log.info(f"Writing: {info_file}") |
| 75 | + info_tab.write(info_file, format='ascii.fixed_width_two_line', |
| 76 | + overwrite=True) |
0 commit comments