from astropy.io import fits
from astropy.table import Table
from astropy.time import Time
import astropy.units as u
import os
import numpy as np
from srttools.io import (
    mkdir_p,
    locations,
    read_data_fitszilla,
    get_chan_columns,
    classify_chan_columns,
)
from srttools.utils import scantype, force_move_file, minmax, median_diff
from srttools.fit import detrend_spectroscopic_data
import warnings
from astropy import log


def default_scan_info_table():
    return Table(
        names=[
            "scan_id",
            "start",
            "stop",
            "ra_min",
            "ra_max",
            "ra_d",
            "dec_min",
            "dec_max",
            "dec_d",
            "az_min",
            "az_max",
            "az_d",
            "el_min",
            "el_max",
            "el_d",
            "glon_min",
            "glon_max",
            "glon_d",
            "glat_min",
            "glat_max",
            "glat_d",
            "is_skydip",
            "kind",
            "direction",
        ],
        dtype=[
            int,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            float,
            bool,
            "S10",
            "S5",
        ],
    )


def get_subscan_info(subscan):
    info = default_scan_info_table()
    scan_id = subscan.meta["SubScanID"]
    start, stop = minmax(subscan["time"])
    ramin, ramax = minmax(subscan["ra"])