import unittest

import sys,os,math
from itertools import islice

import numpy as np

import yaml
from ObservationSim.Config import Config
from ObservationSim.Config.Config import config_dir
from ObservationSim.Instrument import Chip

from ObservationSim.PSF.PSFInterp import PSFInterp


def defineCCD(iccd, config_file):
    with open(config_file, "r") as stream:
        try:
            config = yaml.safe_load(stream)
            #for key, value in config.items():
            #    print (key + " : " + str(value))
        except yaml.YAMLError as exc:
            print(exc)
    path_dict = config_dir(config=config, work_dir=config['work_dir'], data_dir=config['data_dir'])
    chip = Chip(chipID=iccd, config=config)
    #chip = Chip(chipID=iccd, ccdEffCurve_dir=path_dict["ccd_dir"], CRdata_dir=path_dict["CRdata_dir"], normalize_dir=path_dict["normalize_dir"], sls_dir=path_dict['sls_dir'], config=config)
    return chip

def loadPSFSet(iccd):
    config_file = "/public/home/weichengliang/CSST_git/newVersion/CSST/config/config_C3.yaml"
    chip = defineCCD(iccd, config_file)
    print(chip.chipID)
    print(chip.cen_pix_x, chip.cen_pix_y)

    ipath = '/data/simudata/CSSOSDataProductsSims/data/csstPSFdata/psfCube'
    psfMat= PSFInterp(chip, npsf=900, PSF_data_file=ipath, PSF_data_prefix="")
    psfSet= psfMat._loadPSF(iccd, ipath, PSF_data_prefix="")

    twave = 0 #[0...3]
    tpsf  = 0 #[0...899]
    field_x= psfSet[twave][tpsf]['field_x']
    field_y= psfSet[twave][tpsf]['field_y']
    image_x= psfSet[twave][tpsf]['image_x']
    image_y= psfSet[twave][tpsf]['image_y']
    centroid_x= psfSet[twave][tpsf]['centroid_x']
    centroid_y= psfSet[twave][tpsf]['centroid_y']
    print("pos_info:", field_x, field_y, image_x, image_y, centroid_x, centroid_y)
    return psfSet



class PSFInterpModule_coverage(unittest.TestCase):
    def test_psfEll_(self):
        iccd = 1  #[1...30]
        psfSet = loadPSFSet(iccd)


if __name__ == '__main__':
    unittest.main()
    print('#####haha#####')



