import os
from glob import glob
from qsdriver import make_maps, combine_ccds, combine_fits

"""
Explanation: Create Imaging Templates with QuickSip
To create templates for imaging quantities, we first combine the ccd annotated files. Then, we run QuickSip (Boris Lesitedt). The QuickSip code is designed for the Dark Energy Survey (DES). Therefore, we use the modified version by Marc Manera, Ashley Ross, Hee-Jong Seo et al.
End of explanation
"""


#--- inputs 
name = 'dr9pub' # don't use an underline "_"
nside = 256
bands = ['r', 'g', 'z']

in_dir = '/home/mehdi/data/templates/ccds/dr9/'
out_dir = '/home/mehdi/data/templates/dr9/'
out_name = 'ccds-annotated-combined-dr9-test.fits'
output_ccd = os.path.join(out_dir, out_name)

ccds = glob(f'{in_dir}ccds-annotated-*-cut.fits')
ccds

combine_ccds(ccds, output_ccd)

"""
Explanation: 1. Combine CCD files
End of explanation
"""


input_ccd = output_ccd
make_maps(input_ccd, nside, bands, name, out_dir)

"""
Explanation: 2. Run QuickSip
End of explanation
"""


#--- inputs 
name = 'dr9pub' # don't use an underline "_"
nside = 256
in_dir = '/home/mehdi/data/templates/dr9/'
out_path = f'/home/mehdi/data/templates/dr9/dr9pub_nside{nside}.h5'

inputs = os.path.join(in_dir, name, f'nside{nside}_oversamp1', f'{name}*.fits.gz')
input_maps = glob(inputs)

input_maps

df = combine_fits(input_maps, 256, write_to=out_path)

df.shape # (12*256*256=786,432, # of maps)

df.dropna().head() # index represent HEALPix index in ring ordering

"""
Explanation: 3. Combine QuickSip outputs into a hdf5 file (Pandas Dataframe)
End of explanation
"""


%matplotlib inline
import matplotlib.pyplot as plt
import healpy as hp
import numpy as np

hp.mollview(df['mjd_mean_g'], rot=-85, title='MJD Mean g-band')

"""
Explanation: 4. Visualization
End of explanation
"""


