#!/usr/bin/env python3
#
# Copyright (c) 2017 Weitian LI <weitian@aaronly.me>
# MIT License
#

"""
Tile the given slices to the required FoV size, scale down to the
wanted size (for faster simulation later).
"""

import sys
import argparse

import numpy as np
from astropy.io import fits
from astropy.cosmology import FlatLambdaCDM
import astropy.units as au
import scipy.ndimage

from z2freq import z2freq, freq2z


# Adopted cosmology
cosmo = FlatLambdaCDM(H0=71.0, Om0=0.27, Ob0=0.046)


def main():
    outfile_default = "{prefix}_f{freq:06.2f}_N{Nside}_fov{fov:.1f}.fits"

    parser = argparse.ArgumentParser(
        description="Tile slice to match FoV size and scale to required size")
    parser.add_argument("-C", "--clobber", dest="clobber",
                        action="store_true",
                        help="overwrite existing files")
    parser.add_argument("-F", "--fov", dest="fov", default=5.0, type=float,
                        help="required FoV [deg] of the output slice " +
                        "(default: 5.0 deg)")
    parser.add_argument("-N", "--n-side", dest="Nside", default=600, type=int,
                        help="required image size of output slice " +
                        "(default: 600)")
    parser.add_argument("-i", "--infile", dest="infile", required=True,
                        help="input slice")
    parser.add_argument("-o", "--outfile", dest="outfile",
                        default=outfile_default,
                        help="output tiled slice (default: %s)" %
                        outfile_default)
    parser.add_argument("-p", "--prefix", dest="prefix", required=True,
                        help="prefix for the output tiled slice")
    exgrp = parser.add_mutually_exclusive_group(required=True)
    exgrp.add_argument("-z", "--redshift-c", dest="zc", type=float,
                       help="central redshift of the selected cube")
    exgrp.add_argument("-f", "--freq-c", dest="fc", type=float,
                       help="central frequency [MHz] of the selected cube")
    args = parser.parse_args()

    if args.zc:
        zc = args.zc
        fc = z2freq(zc, print_=False)
    else:
        fc = args.fc
        zc = freq2z(fc, print_=False)

    with fits.open(args.infile) as f:
        img_in = f[0].data
        header = f[0].header
    freq = header["FREQ"]  # [MHz]
    Lside = header["LSIDE"]  # [Mpc]
    print("frequency = %.2f [MHz], Lside = %.1f [cMpc]" % (freq, Lside))
    DM = cosmo.comoving_distance(zc).value  # [Mpc]
    print("Comoving distance (@z=%.3f/f=%.2fMHz) = %.2f [Mpc]" % (zc, fc, DM))
    Nside = img_in.shape[0]
    fov_in = (Lside / DM) * au.rad.to(au.deg)  # [deg]
    Nup = int(np.ceil(args.fov / fov_in))
    print("Input FoV: %s [deg], Tiling repeats: %d" % (fov_in, Nup))
    img_tiled = np.tile(img_in, reps=(Nup, Nup))
    Nside2 = round(Nside * args.fov / fov_in)
    img2 = img_tiled[:Nside2, :Nside2]
    # Rescale to the output size
    img_out = scipy.ndimage.zoom(img2, zoom=args.Nside/Nside2, order=1)
    # Record information to header
    header["Z_C"] = (zc, "Central redshift")
    header["FREQ_C"] = (fc, "Frequency [MHz] w.r.t. to the redshift")
    header["FOV"] = (args.fov, "FoV [deg] of this slice")
    header["PixSize"] = (60.0*args.fov/args.Nside, "Pixel size [arcmin]")
    header.add_history(" ".join(sys.argv))
    hdu = fits.PrimaryHDU(data=img_out, header=header)
    outfile = args.outfile.format(prefix=args.prefix, freq=freq,
                                  Nside=args.Nside, fov=args.fov)
    try:
        hdu.writeto(outfile, overwrite=args.clobber)
    except TypeError:
        hdu.writeto(outfile, clobber=args.clobber)
    print("Tiled and scaled slice: %s" % outfile)


if __name__ == "__main__":
    main()

@@ -908,17 +908,18 @@
 default=
-5
+10
 .0, type
@@ -1038,15 +1038,18 @@
 lt: 
-5
+10
 .0 
+%5B
 deg
+%5D
 )%22)%0A
@@ -1112,17 +1112,18 @@
 default=
-6
+18
 00, type
@@ -1233,17 +1233,18 @@
 efault: 
-6
+18
 00)%22)%0A  
@@ -1629,29 +1629,33 @@
 refix%22, 
-required=True
+default=%22deltaTb%22
 ,%0A      
@@ -1707,24 +1707,74 @@
  tiled slice
+ %22 +%0A                        %22(default: 'deltaTb')
 %22)%0A    exgrp
@@ -3193,16 +3193,22 @@
 = (fc, %22
+%5BMHz%5D 
 Frequenc
@@ -3208,22 +3208,16 @@
 requency
- %5BMHz%5D
  w.r.t. 
@@ -3219,19 +3219,23 @@
 r.t. to 
-the
+central
  redshif
@@ -3251,17 +3251,17 @@
 eader%5B%22F
-O
+o
 V%22%5D = (a
@@ -3270,25 +3270,25 @@
 s.fov, %22
-FoV 
 %5Bdeg%5D
+ FoV
  of this
@@ -3321,17 +3321,19 @@
 ze%22%5D = (
-6
+360
 0.0*args
@@ -3350,16 +3350,25 @@
 Nside, %22
+%5Barcsec%5D 
 Pixel si
@@ -3373,17 +3373,8 @@
 size
- %5Barcmin%5D
 %22)%0A 

Update default arguments values & use [arcsec]