#!/usr/bin/env python3
# encoding: utf-8
# Author: Akke Viitanen
# Email: akke.viitanen@helsinki.fi
# Date: 2023-07-04 10:05:16

"""
Create configuration files for the different bands

Numbers are based on

    https://smtn-002.lsst.io/#
"""


import argparse
import glob
import math
import os
import random
import re
import subprocess
import sys
import time

import astropy as ap
import astropy.coordinates as c
import astropy.units as u
import fitsio
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp

zeropoint = {
    "u":   27.03,
    "g":   28.38,
    "r":   28.16,
    "i":   27.85,
    "z":   27.46,
    "y":   26.68,
}

zeropoint = {
    "u": 26.7899838658703,
    "g": 28.6422348533437,
    "r": 28.4762158877359,
    "i": 28.150403321047,
    "z": 27.7604119423889,
    "y": 26.9342790022924,
}

filter_lam_fwhm = {
    "u": (0.368483, 0.0516),
    "g": (0.480199, 0.146),
    "r": (0.623119, 0.1354),
    "i": (0.754169, 0.1246),
    "z": (0.869047, 0.1024),
    "y": (0.973638, 0.0892),
}

seeing = {
    "u": 0.92,
    "g": 0.87,
    "r": 0.83,
    "i": 0.80,
    "z": 0.78,
    "y": 0.76,
}

sky = {
    "u": 22.96,
    "g": 22.26,
    "r": 21.20,
    "i": 20.48,
    "z": 19.60,
    "y": 18.61,
}

psf = {
    "u": "660230_144_u_psf.fits",
    "g": "781365_170_g_psf.fits",
    "r": "755161_141_r_psf.fits",
    "i": "654496_187_i_psf.fits",
    "z": "707614_42_z_psf.fits",
    "y": "647542_93_y_psf.fits",
}


def get_conf(zeropoint, lam, seeing, sky, psf):
    return f"""\
IMAGE_TYPE         SKY
GAIN               1.0
WELL_CAPACITY      0
SATUR_LEVEL        1e9
READOUT_NOISE      12.7
#READOUT_NOISE      0.0
EXPOSURE_TIME      30.0
MAG_ZEROPOINT      {zeropoint}
PIXEL_SIZE         0.20
PSF_TYPE           FILE
PSF_NAME           {psf}
SEEING_FWHM        {seeing}
AUREOLE_RADIUS     0
PSF_OVERSAMP       1
M1_DIAMETER        8.40
M2_DIAMETER        3.42
WAVELENGTH         {lam}
#BACK_MAG           {sky}
BACK_MAG           99.00
STARCOUNT_ZP       0
VERBOSE_TYPE       FULL
NTHREADS           4"""

def main():
    for band in 'ugrizy':
        conf = get_conf(
            zeropoint[band],
            filter_lam_fwhm[band][0],
            seeing[band],
            sky[band],
            psf[band],
        )
        filename = f"{band}.conf"
        print("Writing", filename)
        print(conf, file=open(filename, 'w'))


if __name__ == "__main__":
    main()
