Commit 05c74432 authored by GZhao's avatar GZhao
Browse files

update test_io and test_optics

parent 11de1d29
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -10,8 +10,8 @@ cpism_refdata/
*.egg-info
example/example_output

refdata/starmodel
refdata/target_model
refdata


# Other files and folders
.settings/
@@ -31,6 +31,7 @@ docs/notebooks/image_files/_*
tests/.coverage
tests/htmlcov/
tests/*.xml
tests/test_output

# Executables
*.swf
+1 −0
Original line number Diff line number Diff line
@@ -689,6 +689,7 @@ class CpicVisEmccd(object):
    #     return img_line[:shape[0]*shape[1]].reshape(shape)

    def readout(self, image_focal, em_set, expt_set, image_cosmic_ray=False, emgain=None):

        expt = expt_set
        if expt_set == 0:
            expt = 0.001
+47 −27
Original line number Diff line number Diff line
@@ -6,28 +6,48 @@ import numpy as np

config_aim = os.path.dirname(os.path.dirname(__file__))
config_aim = os.path.join(config_aim, 'data/refdata_path.yaml')
config_set = False

def set_config(refdata_path=None):
    if refdata_path is None:
        print("input cpism refencence data folder")
        refdata_path = input()
        refdata_path = os.path.abspath(refdata_path)
    with open(config_aim, 'w') as f:
        yaml.dump(refdata_path, f)
    return refdata_path

# def set_config(refdata_path=None):
#     if refdata_path is None:
#         print("input cpism refencence data folder")
#         refdata_path = input()
#         refdata_path = os.path.abspath(refdata_path)
#     with open(config_aim, 'w') as f:
#         yaml.dump(refdata_path, f)
#     return refdata_path
    

# try:
#     with open(config_aim, 'r') as f:
#         cpism_refdata = yaml.load(f, Loader=yaml.FullLoader)
#     if not os.path.isdir(cpism_refdata):
#         raise FileNotFoundError('cpism refdata path not found')
#     config_set = True
# except FileNotFoundError:
#     warnings.warn(f'refdata not setup yet, set it before use')
#     cpism_refdata = set_config()

try:

def load_refdata_path(config_aim):
    with open(config_aim, 'r') as f:
        cpism_refdata = yaml.load(f, Loader=yaml.FullLoader)
    if not os.path.isdir(cpism_refdata):
        raise FileNotFoundError('cpism refdata path not found')
    config_set = True
except FileNotFoundError:
    warnings.warn(f'refdata not setup yet, set it before use')
    cpism_refdata = set_config()
        refdata_list = yaml.load(f, Loader=yaml.FullLoader)

    for refdata in refdata_list:
        if os.path.isdir(refdata):
            return refdata
        
    print("csst_cpic_sim refdata folder not found, please input cpism refencence data folder")
    refdata = input()
    refdata = os.path.abspath(refdata)
    if os.path.isdir(refdata):
        refdata_list.append(refdata)
    with open(config_aim, 'w') as f:
        yaml.dump(refdata_list, f)
    exit()
    

cpism_refdata = load_refdata_path(config_aim)

config = {}
config['cpism_refdata'] = cpism_refdata
@@ -121,15 +141,15 @@ def which_focalplane(band):
    ValueError
        If the band is not in ['f565', 'f661', 'f743', 'f883', 'f940', 'f1265', 'f1425', 'f1542', 'wfs']
    """
    band = band.lower()
    if band in ['f565', 'f661', 'f743', 'f883']:
    # band = band.lower()
    # if band in ['f565', 'f661', 'f743', 'f883']:
    #     return 'vis'
    # if band in ['f940', 'f1265', 'f1425', 'f1542']:
    #     return 'nir'
    # if band in ['wfs']:
    #     return 'wfs'
    return 'vis'
    if band in ['f940', 'f1265', 'f1425', 'f1542']:
        return 'nir'
    if band in ['wfs']:
        return 'wfs'

    raise ValueError(f"未知的波段{band}")
    # raise ValueError(f"未知的波段{band}")

def iso_time(time):
    if isinstance(time, str):
@@ -145,7 +165,7 @@ def relative_time(time):
    if isinstance(time, float):
        return time
    if isinstance(time, int):
        return float(int)
        return float(time)
    
    utc0 = config['utc0']
    time0 = datetime.timestamp(datetime.fromisoformat(utc0))
+17 −21
Original line number Diff line number Diff line
@@ -13,12 +13,13 @@ from .config import config, iso_time
default_output_dir = config['output']
log_level = config['log_level']
header_check = config['check_fits_header']
log_dir = config['log_dir']

def set_up_logger(log_dir):
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    return Logger(log_dir+'/cpism_pack.log', log_level).logger

log = Logger(log_dir+'/cpism_pack.log', log_level).logger

log = set_up_logger(config['log_dir'])

def check_and_update_fits_header(header):
    """
@@ -140,7 +141,6 @@ def obsid_parser(
        '01': 'SCI',
        '02': 'DSF',
        '10': 'CALS',
        '00': 'TEMP'
    }
    obstype = obstype_dict.get(obsid[1:3], 'DEFT')
    return obstype
@@ -196,7 +196,7 @@ def primary_hdu(

    obsid = obs_info['obsid']

    exp_start = obs_info.get('EXPSTART')
    exp_start = obs_info['EXPSTART']
    exp_start = datetime.fromisoformat(exp_start)

    exp_end = obs_info['EXPEND']
@@ -283,7 +283,7 @@ def primary_hdu(
    cabend = gnc_info.get('CABEND', exp_end.isoformat(timespec='seconds'))
    cabend = iso_time(cabend)
    cabend_mjd = datetime_obj_to_mjd(datetime.fromisoformat(cabend))
    header['CABEND'] = gnc_info.get('CABEDN', header['EXPEND'])
    header['CABEND'] = cabend_mjd
    header['SUNANGL1'] = gnc_info.get('SUNANGL1', header['SUNANGL0'])
    header['MOONANG1'] = gnc_info.get('MOONANG1', header['MOONANG0'])
    header['TEL_ALT1'] = gnc_info.get('TEL_ALT1', header['TEL_ALT0'])
@@ -318,7 +318,7 @@ def primary_hdu(
        return hdu


def frame_header(obs_info, index, primary_header, camera_dict={}):
def frame_header(obs_info, index, primary_header, camera_dict):
    """
    Generate the header for a single frame.

@@ -466,7 +466,7 @@ def frame_header(obs_info, index, primary_header, camera_dict={}):
    return header


def save_fits_simple(images, obs_info, output_folder=None):
def save_fits_simple(images, obs_info, output_folder='./'):
    """
    Save the image to a fits file with a simple header to TMP directory.

@@ -512,11 +512,10 @@ def save_fits_simple(images, obs_info, output_folder=None):
    shift = obs_info['shift']
    header['shift'] = f"x:{shift[0]},y:{shift[1]}"

    if output_folder is None:
        fullname = f"{tmp_folder_path}/{filename}"
    else:
        fullname = f"{output_folder}/{filename}"
        if os.path.exists(output_folder) is False:

    fullname = os.path.join(output_folder, filename)
    print(fullname)
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
        log.debug(f"Output folder {output_folder} is created.")

@@ -525,7 +524,7 @@ def save_fits_simple(images, obs_info, output_folder=None):
    return fullname


def save_fits(images, obs_info, gnc_info, camera_dict={}, csst_format=True, output_folder=None):
def save_fits(images, obs_info, gnc_info, camera_dict={}, csst_format=True, output_folder='./'):
    """
    Save the image to a fits file.

@@ -581,9 +580,6 @@ def save_fits(images, obs_info, gnc_info, camera_dict={}, csst_format=True, outp
        frame_hdu.add_checksum()
        hdu_list.append(frame_hdu)

    if output_folder is None:
        folder = f"{default_output_dir}/{folder}"
    else:
    folder = f"{output_folder}/{folder}"

    if not os.path.exists(folder):
+49 −42
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
import argparse, sys, tqdm, time, os, yaml
from glob import glob
from datetime import datetime
import traceback

import numpy as np
from .target import spectrum_generator, target_file_load
@@ -123,7 +124,8 @@ def vis_observation(
    params['EXPSTART'] = expt_start_iso.isoformat()
    params['EXPEND'] = expt_end_iso.isoformat()
    params['frame_info'] = all_frame_info
    save_fits(image_cube, params, gnc_info, camera_dict=camera.__dict__.copy(), csst_format=csst_format, output_folder=output)

    save_fits(image_cube, params, gnc_info, camera.__dict__.copy(), csst_format=csst_format, output_folder=output)
    if prograss_bar:
        pg_bar.close()
        print(f'  Done [{time.time() - start_time:.1f}s]                             ')
@@ -248,6 +250,7 @@ def observation_simulation_from_config(obs_file, config_file):


    for ind_target, file in enumerate(file_list):
        try:
            with open(file, 'r') as fid:
                obs_info = yaml.load(fid, Loader=yaml.FullLoader)

@@ -263,18 +266,21 @@ def observation_simulation_from_config(obs_file, config_file):
            gnc_info = obs_info.get('gnc_info', {})
            time = obs_info.get('time', 0)
            emgain = obs_info.get('emgain', None)
        
            time = relative_time(time)

        except Exception as e:
            log.error(f"{file} is not a valid yaml file.")
            log.error(f"Failed with {type(e).__name__}{e}.\n\n {traceback.format_exc()}")
            continue

        ind_camera = 0
        for camera_name, camera in zip(all_camera_name, all_camera):
            try:
            ind_camera += 1
            ind_run = ind_target * len(all_camera) + ind_camera
            all_run = len(all_camera) * len(file_list)
                info_text = f"({ind_run}/{all_run}) obsid[{obsid}]/{os.path.basename(file)[:-5]} with {camera_name}"
                log.info(info_text)
            info_text = f"({ind_run}/{all_run}) obsid[{obsid}] with {camera_name}"

            log.info(info_text)
            if time == 0:
                camera.time_syn(time, initial=True)
            else:
@@ -289,6 +295,7 @@ def observation_simulation_from_config(obs_file, config_file):
            else:
                output = output_folder

            try:
                vis_observation(
                    target,
                    skybg,
@@ -307,7 +314,7 @@ def observation_simulation_from_config(obs_file, config_file):
                    csst_format=csst_format,
                    prograss_bar=True)
            except Exception as e:
                raise(e)
                log.error(f"{info_text} failed with {type(e).__name__}{e}.\n\n {traceback.format_exc()}")

def main(argv=None):
    parser = argparse.ArgumentParser(description='Cpic obsevation image simulation')
@@ -315,7 +322,7 @@ def main(argv=None):

    subparsers = parser.add_subparsers(help='type of runs')
    parser_quickrun = subparsers.add_parser('quickrun', help='a quick observation with no configration file')
    parser_quickrun.add_argument('target_string', type=str, help='example: \*5.1/25.3(1.3,1.5)/22.1(2.3,-4.5)')
    parser_quickrun.add_argument('target_string', type=str, help='example: *5.1/25.3(1.3,1.5)/22.1(2.3,-4.5)')
    parser_quickrun.add_argument('expt', type=float, help='exposure time [ms]')
    parser_quickrun.add_argument('emgain', type=float, help='emgain or emgain set value if emgain_input is False')
    parser_quickrun.add_argument('nframe', type=int, help='number of frames')
@@ -361,8 +368,8 @@ def main(argv=None):
    args.func(args)
    

if __name__ == '__main__':  # pragma: no cover
    sys.exit(main())
# if __name__ == '__main__':  # pragma: no cover
#     sys.exit(main())


#     target_example = {
Loading