#!/usr/bin/env python

"""Wrapper script on desitarget.mock.targets_truth which is called by the
SLURM scripts generated by sprint-healpix-slurm.

This is a simplified version of select_mock_targets.

"""
from __future__ import print_function, division

import argparse
from time import time

import yaml
import numpy as np
import healpy as hp

from desitarget.mock.build import targets_truth
from desispec.log import get_logger
log = get_logger()

parser = argparse.ArgumentParser()
parser.add_argument('--config', '-c',default='input.yaml')
parser.add_argument('--output_dir', help='Output directory.', type=str, default='./')
parser.add_argument('--seed', '-s', help='Seed for random number generation', type=int, default=None)
parser.add_argument('--nproc', type=int, help='number of concurrent processes to use', default=4)
parser.add_argument('--nside', help='Resolution of the healpix pixels', type=int, default=64)
parser.add_argument('--healpixels', help='Integer list of healpix pixels (corresponding to nside) to process.', type=int, nargs='*', default=None)
args = parser.parse_args()

log.info('Reading configuration file {}'.format(args.config))
with open(args.config, 'r') as pfile:
    params = yaml.load(pfile)

t0 = time()
targets_truth(params, args.output_dir, seed=args.seed, nside=args.nside,
              nproc=args.nproc, verbose=True, clobber=True,
              healpixels=args.healpixels)
log.info('All done in {:.2f} minutes!'.format( (time() - t0) / 60 ))
