<gh_stars>0
'''

script for constructing HOD catalogs for the Quijote LHC simulations
with a forward model of the BOSS LOWZ SGC sample


'''
import os, sys 
import numpy as np 
from simbig import halos as Halos
from simbig import galaxies as Galaxies
from simbig import forwardmodel as FM

np.random.seed(1)

typ = sys.argv[1]
i0  = int(sys.argv[2]) 
i1  = int(sys.argv[3])


dat_dir = '/tigress/chhahn/simbig/cmass/'


def sample_HOD(): 
    ''' sample HOD value based on priors set by Parejko+(2013)
    '''
    _hod_fid = Galaxies.thetahod_literature('reid2014_cmass')
    hod_fid = np.array([_hod_fid['logMmin'], _hod_fid['sigma_logM'], _hod_fid['logM0'], _hod_fid['logM1'], _hod_fid['alpha']])

    dhod = 2*np.array([0.029, 0.06, 0.13, 0.06, 0.18])

    _hod = hod_fid + dhod * np.random.uniform(-0.1, 0.1, size=(5))

    return {'logMmin': _hod[0], 'sigma_logM': _hod[1], 'logM0': _hod[2], 'logM1': _hod[3], 'alpha': _hod[4]}


def standard_mocks(i0, i1): 
    ''' construct standard test mocks 
    '''
    for i_fid in range(i0, i1+1): 
        print('Fiducial %i' % i_fid)
        # read in halo catalog
        halos = Halos.Quijote_fiducial_HR(i_fid, z=0.5)
        for i_hod in range(5): 
            print('  HOD %i' % i_hod)
            fgal = os.path.join(dat_dir, 'tests', 'hod.quijote_fid%i.z0p5.cmass_sgc.%i.bf' % (i_fid, i_hod))
            fhod = fgal.replace('.bf', '.npy')

            if not os.path.isfile(fhod) or not os.path.isdir(fgal): 
                # sample HOD parameters
                theta_hod = sample_HOD()
                np.save(fhod, theta_hod)
                # populate with HOD
                hod = Galaxies.hodGalaxies(halos, theta_hod, seed=0)
                # apply forward model (footprint, veto mask fiber collisions)
                gals = FM.BOSS(hod, sample='cmass-south', seed=0, veto=True, fiber_collision=True, silent=True)
                # save to file 
                gals.save(fgal)
    return None 


def assemblybias_mocks(i0, i1): 
    ''' construct assembly bias test mocks 
    '''
    theta_fid = Galaxies.thetahod_literature('reid2014_cmass')
    abiases = np.array([-1. , -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

    for i_fid in range(i0, i1+1): 
        print('Fiducial %i' % i_fid)
        halos = Halos.Quijote_fiducial_HR_Rockstar(i_fid, z=0.5)

        _Z07AB = Galaxies.AssembiasZheng07Model()
        Z07AB = _Z07AB.to_halotools(halos.cosmo, 
                halos.attrs['redshift'], 
                halos.attrs['mdef'], 
                sec_haloprop_key='halo_nfw_conc')

        for i_ab, abias in enumerate(abiases):
            # HOD parameters with assembly bias 
            _theta_ab = theta_fid.copy()
            _theta_ab['mean_occupation_centrals_assembias_param1'] = abias
            _theta_ab['mean_occupation_satellites_assembias_param1'] = abias

            # populate with HOD
            hod_ab = halos.populate(Z07AB, seed=0, **_theta_ab)

            gals_ab = FM.BOSS(hod_ab, sample='cmass-south', seed=0, veto=True, fiber_collision=True, silent=True)
            fgal = os.path.join(dat_dir, 'tests',
                    'hod.quijote_fid_hr%i.z0p5.cmass_sgc.abias%i.bf' % (i_fid, i_ab))
            gals_ab.save(fgal)
    return None 

if typ == 'standard': 
    standard_mocks(i0, i1)
elif typ == 'assemblybias': 
    assemblybias_mocks(i0, i1)

'''

script for constructing HOD catalogs for the Quijote LHC simulations
with a forward model of the BOSS LOWZ SGC sample


'''
import os, sys 
import numpy as np 
from simbig import halos as Halos
from simbig import galaxies as Galaxies
from simbig import forwardmodel as FM

np.random.seed(1)

typ = sys.argv[1]
i0  = int(sys.argv[2]) 
i1  = int(sys.argv[3])


dat_dir = '/tigress/chhahn/simbig/cmass/'


def sample_HOD(): 
    ''' sample HOD value based on priors set by Parejko+(2013)
    '''
    _hod_fid = Galaxies.thetahod_literature('reid2014_cmass')
    hod_fid = np.array([_hod_fid['logMmin'], _hod_fid['sigma_logM'], _hod_fid['logM0'], _hod_fid['logM1'], _hod_fid['alpha']])

    dhod = 2*np.array([0.029, 0.06, 0.13, 0.06, 0.18])

    _hod = hod_fid + dhod * np.random.uniform(-0.1, 0.1, size=(5))

    return {'logMmin': _hod[0], 'sigma_logM': _hod[1], 'logM0': _hod[2], 'logM1': _hod[3], 'alpha': _hod[4]}


def standard_mocks(i0, i1): 
    ''' construct standard test mocks 
    '''
    for i_fid in range(i0, i1+1): 
        print('Fiducial %i' % i_fid)
        # read in halo catalog
        halos = Halos.Quijote_fiducial_HR(i_fid, z=0.5)
        for i_hod in range(5): 
            print('  HOD %i' % i_hod)
            fgal = os.path.join(dat_dir, 'tests', 'hod.quijote_fid%i.z0p5.cmass_sgc.%i.bf' % (i_fid, i_hod))
            fhod = fgal.replace('.bf', '.npy')

            if not os.path.isfile(fhod) or not os.path.isdir(fgal): 
                # sample HOD parameters
                theta_hod = sample_HOD()
                np.save(fhod, theta_hod)
                # populate with HOD
                hod = Galaxies.hodGalaxies(halos, theta_hod, seed=0)
                # apply forward model (footprint, veto mask fiber collisions)
                gals = FM.BOSS(hod, sample='cmass-south', seed=0, veto=True, fiber_collision=True, silent=True)
                # save to file 
                gals.save(fgal)
    return None 


def assemblybias_mocks(i0, i1): 
    ''' construct assembly bias test mocks 
    '''
    theta_fid = Galaxies.thetahod_literature('reid2014_cmass')
    abiases = np.array([-1. , -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

    for i_fid in range(i0, i1+1): 
        print('Fiducial %i' % i_fid)
        halos = Halos.Quijote_fiducial_HR_Rockstar(i_fid, z=0.5)

        _Z07AB = Galaxies.AssembiasZheng07Model()
        Z07AB = _Z07AB.to_halotools(halos.cosmo, 
                halos.attrs['redshift'], 
                halos.attrs['mdef'], 
                sec_haloprop_key='halo_nfw_conc')

        for i_ab, abias in enumerate(abiases):
            # HOD parameters with assembly bias 
            _theta_ab = theta_fid.copy()
            _theta_ab['mean_occupation_centrals_assembias_param1'] = abias
            _theta_ab['mean_occupation_satellites_assembias_param1'] = abias

            # populate with HOD
            hod_ab = halos.populate(Z07AB, seed=0, **_theta_ab)

            gals_ab = FM.BOSS(hod_ab, sample='cmass-south', seed=0, veto=True, fiber_collision=True, silent=True)
            fgal = os.path.join(dat_dir, 'tests',
                    'hod.quijote_fid_hr%i.z0p5.cmass_sgc.abias%i.bf' % (i_fid, i_ab))
            gals_ab.save(fgal)
    return None 

if typ == 'standard': 
    standard_mocks(i0, i1)
elif typ == 'assemblybias': 
    assemblybias_mocks(i0, i1)
script for constructing HOD catalogs for the Quijote LHC simulations with a forward model of the BOSS LOWZ SGC sample sample HOD value based on priors set by Parejko+(2013) construct standard test mocks # read in halo catalog # sample HOD parameters # populate with HOD # apply forward model (footprint, veto mask fiber collisions) # save to file construct assembly bias test mocks # HOD parameters with assembly bias # populate with HOD