<commit_before>""" Top-level module to build or re-build the JSON files for
FRB host galaxies"""

from pkg_resources import resource_filename
import os
import sys
import warnings

from IPython import embed

import numpy as np

from astropy.coordinates import SkyCoord
from astropy import units
from astropy.table import Table
from astropy.coordinates import match_coordinates_sky

from frb.frb import FRB
from frb.galaxies import frbgalaxy, defs
from frb.galaxies import photom as frbphotom
try:
    from frb.galaxies import ppxf
except:
    print('WARNING:  ppxf not installed')
from frb.galaxies import nebular
from frb.surveys import des
from frb.surveys import sdss
from frb.surveys import wise
from frb.surveys import panstarrs
from frb.surveys import catalog_utils

try:
    import extinction
except ImportError:
    print("extinction package not loaded.  Extinction corrections will fail")

try:
    from frb.galaxies import cigale
except ModuleNotFoundError:
    warnings.warn("You haven't installed pcigale and won't be able to do that analysis")

try:
    from frb.galaxies import eazy as frbeazy
except:
    warnings.warn("NOT READY FOR EAZY!")

from linetools.spectra.xspectrum1d import XSpectrum1D

db_path = os.getenv('FRB_GDB')
if db_path is None:
    print("Warning, you need to set $FRB_GDB to build hosts")
    #embed(header='You need to set $FRB_GDB')

ebv_method = 'SandF'

def build_host_121102(build_photom=False, build_cigale=False, use_orig=False):
    """
    Generate the JSON file for FRB 121102

    Writes to 121102/FRB121102_host.json

    The majority of data comes from Tendulkar et al. 2017

    reff from Bassa+17 https://ui.adsabs.harvard.edu/abs/2017ApJ...843L...8B/abstract


    Args:
        build_photom (bool, optional): Generate the photometry file in the Galaxy_DB

    """
    #FRB_coord = SkyCoord('05h31m58.6980s +33d8m52.671s', frame='icrs') # Updated coords from Bassa+17
    # Eyeball Tendulkar+17 PA
    # UPDATE THIS!
    #gal_coord = FRB_coord.directional_offset_by(-45 * units.deg, 286e-3 * units.arcsec)
    gal_coord = SkyCoord('05h31m58.6980s +33d8m52.671s', frame='icrs') # Updated coords from Bassa+17
    
    # Instantiate
    frb121102 = FRB.by_name('FRB121102')
    host121102 = frbgalaxy.FRBHost(gal_coord.ra.value, gal_coord.dec.value, frb121102)

    # Redshift
    host121102.set_z(0.19273, 'spec', err=0.00008)

    # Photometry
    EBV = nebular.get_ebv(gal_coord, definition=ebv_method)['meanValue']  #
    print("EBV={} for the host {}".format(EBV, host121102.name))

    # photom_file = os.path.join(db_path, 'Repeater', 'Tendulkar2017', 'tendulkar2017_photom.ascii')
    photom_file = os.path.join(db_path, 'Repeater', 'Bassa2017', 'bassa2017_photom.ascii')
    if build_photom:
        photom = Table()
        photom['Name'] = ['HG121102']
        photom['ra'] = [host121102.coord.ra.value]
        photom['dec'] = host121102.coord.dec.value
        #
        # GMOS-N photometry from Bassa+2017, different than Tendulkar+2017, see Bassa+17 for details (AB)
        photom['GMOS_N_r'] = 25.46
        photom['GMOS_N_r_err'] = 0.14
        photom['GMOS_N_i'] = 24.75
        photom['GMOS_N_i_err'] = 0.09
        photom['GMOS_N_z'] = 24.30
        photom['GMOS_N_z_err'] = 0.13
        photom['GMOS_N_g'] = 25.85
        photom['GMOS_N_g_err'] = 0.12

        # HST from Bassa+2017 (AB)
        photom['WFC3_F110W'] = 23.675
        photom['WFC3_F110W_err'] = 0.012
        photom['WFC3_F160W'] = 23.31
        photom['WFC3_F160W_err'] = 0.03

        # Spitzer from Bassa (micro-Jy)
        mag_3p6, err_mag_3p6 = catalog_utils.mag_from_flux(flux=1.03e-3*units.mJy,
                                                           flux_err=0.19e-3*units.mJy)  # in micro-Jy
        photom['Spitzer_3.6'] = mag_3p6
        photom['Spitzer_3.6_err'] = err_mag_3p6
        photom['Spitzer_4.5'] = catalog_utils.mag_from_flux(0.9e-3*units.mJy/2.)[0]   # upper limit (6sigma/2 = ~3sigma) in micro-Jy
        photom['Spitzer_4.5_err'] = 999.  # the flux is un upper limit, note it is 3sigma (estimated by dividing the 6sigma/2)

        # Write
        photom = frbphotom.merge_photom_tables(photom, photom_file)
        photom.write(photom_file, format=frbphotom.table_format, overwrite=True)
    # Read
    photom = Table.read(photom_file, format=frbphotom.table_format)
    # Dust correction
    frbphotom.correct_photom_table(photom, EBV, 'HG121102')
    # Parse
    host121102.parse_photom(photom, EBV=EBV)

    # CIGALE
    cigale_file = os.path.join(db_path, 'Repeater', 'Bassa2017', 'HG121102_CIGALE.fits')
    sfh_file = cigale_file.replace('CIGALE', 'CIGALE_SFH')
    if build_cigale:
        # Run
        cigale.host_run(host121102, cigale_file=cigale_file)

    host121102.parse_cigale(cigale_file, sfh_file=sfh_file)

    # Nebular lines
    neb_lines = {}
    neb_lines['Halpha'] = 0.652e-16
    neb_lines['Halpha_err'] = 0.009e-16
    neb_lines['Halpha_Al'] = 0.622
    #
    neb_lines['Hbeta'] = 0.118e-16
    neb_lines['Hbeta_err'] = 0.011e-16
    neb_lines['Hbeta_Al'] = 0.941
    #
    neb_lines['[OIII] 5007'] = 0.575e-16
    neb_lines['[OIII] 5007_err'] = 0.011e-16
    neb_lines['[OIII] 5007_Al'] = 0.911
    #
    neb_lines['[NII] 6584'] = 0.030e-16  # * units.erg/units.cm**2/units.s      # Upper limit
    neb_lines['[NII] 6584_err'] = -999.  # * units.erg/units.cm**2/units.s      # Upper limit
    neb_lines['[NII] 6584_Al'] = 0.619

    AV = 2.42

    # Extinction correct
    for key in neb_lines.keys():
        if '_err' in key:
            continue
        if 'Al' in key:
            continue
        # Ingest
        host121102.neb_lines[key] = neb_lines[key] * 10 ** (neb_lines[key + '_Al'] * AV / 2.5)
        if neb_lines[key + '_err'] > 0:
            host121102.neb_lines[key + '_err'] = neb_lines[key + '_err'] * 10 ** (neb_lines[key + '_Al'] * AV / 2.5)
        else:
            host121102.neb_lines[key + '_err'] = neb_lines[key + '_err']

    # Vette
    for key in host121102.neb_lines.keys():
        if '_err' in key:
            continue
        assert key in defs.valid_neb_lines

    # Morphology : Bassa+2017 half-light
    host121102.morphology['reff_ang'] = 0.20   # arcsec
    host121102.morphology['reff_ang_err'] = 0.01
    # Other
    host121102.morphology['n'] = 2.2
    host121102.morphology['n_err'] = 1.5
    #
    host121102.morphology['b/a'] = 0.25
    host121102.morphology['b/a_err'] = 0.13

    # Derived quantities
    if use_orig:
        host121102.derived['M_r'] = -17.0  # AB; Tendulkar+17
        host121102.derived['M_r_err'] = 0.2  # Estimated by JXP
        host121102.derived['SFR_nebular'] = 0.23  # MSun/yr; Tendulkar+17
        host121102.derived['Mstar'] = 5.5e7  # Msun; Tendulkar+17
        host121102.derived['Mstar_err'] = 1.5e7  # Msun; Tendulkar+17
    host121102.derived['Z_spec'] = -0.16  # Tendulkar+17 on a scale with Solar O/H = 8.86
    host121102.derived['Z_spec_err'] = -999.  # Tendulkar+17

    # References<commit_msg>updated build hosts for 121102<commit_after>""" Top-level module to build or re-build the JSON files for
FRB host galaxies"""

from pkg_resources import resource_filename
import os
import sys
import warnings

from IPython import embed

import numpy as np

from astropy.coordinates import SkyCoord
from astropy import units
from astropy.table import Table
from astropy.coordinates import match_coordinates_sky

from frb.frb import FRB
from frb.galaxies import frbgalaxy, defs
from frb.galaxies import photom as frbphotom
try:
    from frb.galaxies import ppxf
except:
    print('WARNING:  ppxf not installed')
from frb.galaxies import nebular
from frb.surveys import des
from frb.surveys import sdss
from frb.surveys import wise
from frb.surveys import panstarrs
from frb.surveys import catalog_utils

try:
    import extinction
except ImportError:
    print("extinction package not loaded.  Extinction corrections will fail")

try:
    from frb.galaxies import cigale
except ModuleNotFoundError:
    warnings.warn("You haven't installed pcigale and won't be able to do that analysis")

try:
    from frb.galaxies import eazy as frbeazy
except:
    warnings.warn("NOT READY FOR EAZY!")

from linetools.spectra.xspectrum1d import XSpectrum1D

db_path = os.getenv('FRB_GDB')
if db_path is None:
    print("Warning, you need to set $FRB_GDB to build hosts")
    #embed(header='You need to set $FRB_GDB')

ebv_method = 'SandF'

def build_host_121102(build_photom=False, build_cigale=False, use_orig=False):
    """
    Generate the JSON file for FRB 121102

    Writes to 121102/FRB121102_host.json

    The majority of data comes from Tendulkar et al. 2017

    reff from Bassa+17 https://ui.adsabs.harvard.edu/abs/2017ApJ...843L...8B/abstract


    Args:
        build_photom (bool, optional): Generate the photometry file in the Galaxy_DB

    """
    #FRB_coord = SkyCoord('05h31m58.6980s +33d8m52.671s', frame='icrs') # Updated coords from Bassa+17
    # Eyeball Tendulkar+17 PA
    # UPDATE THIS!
    #gal_coord = FRB_coord.directional_offset_by(-45 * units.deg, 286e-3 * units.arcsec)
    gal_coord = SkyCoord('05h31m58.6980s +33d8m52.671s', frame='icrs') # Updated coords from Bassa+17
    
    # Instantiate
    frb121102 = FRB.by_name('FRB121102')
    host121102 = frbgalaxy.FRBHost(gal_coord.ra.value, gal_coord.dec.value, frb121102)

    # Redshift
    host121102.set_z(0.19273, 'spec', err=0.00008)

    # Photometry
    EBV = nebular.get_ebv(gal_coord, definition=ebv_method)['meanValue']  #
    print("EBV={} for the host {}".format(EBV, host121102.name))

    # photom_file = os.path.join(db_path, 'Repeater', 'Tendulkar2017', 'tendulkar2017_photom.ascii')
    photom_file = os.path.join(db_path, 'Repeater', 'Bassa2017', 'bassa2017_photom.ascii')
    if build_photom:
        photom = Table()
        photom['Name'] = ['HG121102']
        photom['ra'] = [host121102.coord.ra.value]
        photom['dec'] = host121102.coord.dec.value
        #
        # GMOS-N photometry from Bassa+2017, different than Tendulkar+2017, see Bassa+17 for details (AB)
        photom['GMOS_N_r'] = 25.46
        photom['GMOS_N_r_err'] = 0.14
        photom['GMOS_N_i'] = 24.75
        photom['GMOS_N_i_err'] = 0.09
        photom['GMOS_N_z'] = 24.30
        photom['GMOS_N_z_err'] = 0.13
        photom['GMOS_N_g'] = 25.85
        photom['GMOS_N_g_err'] = 0.12

        # HST from Bassa+2017 (AB)
        photom['WFC3_F110W'] = 23.675
        photom['WFC3_F110W_err'] = 0.012
        photom['WFC3_F160W'] = 23.31
        photom['WFC3_F160W_err'] = 0.03

        # Spitzer from Bassa (micro-Jy)
        mag_3p6, err_mag_3p6 = catalog_utils.mag_from_flux(flux=1.03e-3*units.mJy,
                                                           flux_err=0.19e-3*units.mJy)  # in micro-Jy
        photom['Spitzer_3.6'] = mag_3p6
        photom['Spitzer_3.6_err'] = err_mag_3p6
        photom['Spitzer_4.5'] = catalog_utils.mag_from_flux(0.9e-3*units.mJy/2.)[0]   # upper limit (6sigma/2 = ~3sigma) in micro-Jy
        photom['Spitzer_4.5_err'] = 999.  # the flux is un upper limit, note it is 3sigma (estimated by dividing the 6sigma/2)

        # Write
        photom = frbphotom.merge_photom_tables(photom, photom_file)
        photom.write(photom_file, format=frbphotom.table_format, overwrite=True)
    # Read
    photom = Table.read(photom_file, format=frbphotom.table_format)
    # Dust correction
    frbphotom.correct_photom_table(photom, EBV, 'HG121102')
    # Parse
    host121102.parse_photom(photom, EBV=EBV)


    # CIGALE
    cigale_file = os.path.join(db_path, 'Repeater', 'Bassa2017', 'HG121102_CIGALE.fits')
    sfh_file = cigale_file.replace('CIGALE', 'CIGALE_SFH')
    if build_cigale:
        # Run
        cigale.host_run(host121102, cigale_file=cigale_file)

    host121102.parse_cigale(cigale_file, sfh_file=sfh_file)

    # Nebular lines
    neb_lines = {}
    neb_lines['Halpha'] = 0.652e-16
    neb_lines['Halpha_err'] = 0.009e-16
    neb_lines['Halpha_Al'] = 0.622
    #
    neb_lines['Hbeta'] = 0.118e-16
    neb_lines['Hbeta_err'] = 0.011e-16
    neb_lines['Hbeta_Al'] = 0.941
    #
    neb_lines['[OIII] 5007'] = 0.575e-16
    neb_lines['[OIII] 5007_err'] = 0.011e-16
    neb_lines['[OIII] 5007_Al'] = 0.911
    #
    neb_lines['[NII] 6584'] = 0.030e-16  # * units.erg/units.cm**2/units.s      # Upper limit
    neb_lines['[NII] 6584_err'] = -999.  # * units.erg/units.cm**2/units.s      # Upper limit
    neb_lines['[NII] 6584_Al'] = 0.619

    AV = 2.42

    # Extinction correct
    for key in neb_lines.keys():
        if '_err' in key:
            continue
        if 'Al' in key:
            continue
        # Ingest
        host121102.neb_lines[key] = neb_lines[key] * 10 ** (neb_lines[key + '_Al'] * AV / 2.5)
        if neb_lines[key + '_err'] > 0:
            host121102.neb_lines[key + '_err'] = neb_lines[key + '_err'] * 10 ** (neb_lines[key + '_Al'] * AV / 2.5)
        else:
            host121102.neb_lines[key + '_err'] = neb_lines[key + '_err']

    # AV
    host121102.calc_nebular_AV('Ha/Hb', min_AV=0.)

    # SFR
    host121102.calc_nebular_SFR('Ha')

    # Vette
    for key in host121102.neb_lines.keys():
        if '_err' in key:
            continue
        assert key in defs.valid_neb_lines

    # Morphology : Bassa+2017 half-light
    host121102.morphology['reff_ang'] = 0.20   # arcsec
    host121102.morphology['reff_ang_err'] = 0.01
    host121102.morphology['reff_kpc'] = 0.66   # kpc
    host121102.morphology['reff_kpc_err'] = 0.03
    # Other
    host121102.morphology['n'] = 2.2
    host121102.morphology['n_err'] = 1.5
    #
    host121102.morphology['b/a'] = 0.25
    host121102.morphology['b/a_err'] = 0.13

    # Derived quantities
    if use_orig:
        host121102.derived['M_r'] = -17.0  # AB; Tendulkar+17
        host121102.derived['M_r_err'] = 0.2  # Estimated by JXP
        host121102.derived['SFR_nebular'] = 0.15  # MSun/yr from new dust-corrected H-alpha; (0.23 from Tendulkar+17)
        host121102.derived['Mstar'] = 5.5e7  # Msun; Tendulkar+17
        host121102.derived['Mstar_err'] = 1.5e7  # Msun; Tendulkar+17
    host121102.derived['Z_spec'] = -0.16  # Tendulkar+17 on a scale with Solar O/H = 8.86
    host121102.derived['Z_spec_err'] = -999.  # Tendulkar+17

    # References
{'fraction_non_alphanumeric': 0.12129304286718201, 'fraction_numerical': 0.0832044975404076, 'mean_word_length': 3.031444759206799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4006382', 'n_tokens_mistral': 6350, 'n_tokens_neox': 5438, 'n_words': 1099}