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 Original line Diff line number Diff line
@@ -10,8 +10,8 @@ cpism_refdata/
*.egg-info
*.egg-info
example/example_output
example/example_output


refdata/starmodel
refdata
refdata/target_model



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


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


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

        expt = expt_set
        expt = expt_set
        if expt_set == 0:
        if expt_set == 0:
            expt = 0.001
            expt = 0.001
+47 −27
Original line number Original line 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.dirname(os.path.dirname(__file__))
config_aim = os.path.join(config_aim, 'data/refdata_path.yaml')
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:
    with open(config_aim, 'r') as f:
        cpism_refdata = yaml.load(f, Loader=yaml.FullLoader)
        refdata_list = 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()


    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 = {}
config['cpism_refdata'] = cpism_refdata
config['cpism_refdata'] = cpism_refdata
@@ -121,15 +141,15 @@ def which_focalplane(band):
    ValueError
    ValueError
        If the band is not in ['f565', 'f661', 'f743', 'f883', 'f940', 'f1265', 'f1425', 'f1542', 'wfs']
        If the band is not in ['f565', 'f661', 'f743', 'f883', 'f940', 'f1265', 'f1425', 'f1542', 'wfs']
    """
    """
    band = band.lower()
    # band = band.lower()
    if band in ['f565', 'f661', 'f743', 'f883']:
    # 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'
    return 'vis'
    if band in ['f940', 'f1265', 'f1425', 'f1542']:
    # raise ValueError(f"未知的波段{band}")
        return 'nir'
    if band in ['wfs']:
        return 'wfs'

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


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

def set_up_logger(log_dir):
    if not os.path.exists(log_dir):
    if not os.path.exists(log_dir):
        os.makedirs(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):
def check_and_update_fits_header(header):
    """
    """
@@ -140,7 +141,6 @@ def obsid_parser(
        '01': 'SCI',
        '01': 'SCI',
        '02': 'DSF',
        '02': 'DSF',
        '10': 'CALS',
        '10': 'CALS',
        '00': 'TEMP'
    }
    }
    obstype = obstype_dict.get(obsid[1:3], 'DEFT')
    obstype = obstype_dict.get(obsid[1:3], 'DEFT')
    return obstype
    return obstype
@@ -196,7 +196,7 @@ def primary_hdu(


    obsid = obs_info['obsid']
    obsid = obs_info['obsid']


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


    exp_end = obs_info['EXPEND']
    exp_end = obs_info['EXPEND']
@@ -283,7 +283,7 @@ def primary_hdu(
    cabend = gnc_info.get('CABEND', exp_end.isoformat(timespec='seconds'))
    cabend = gnc_info.get('CABEND', exp_end.isoformat(timespec='seconds'))
    cabend = iso_time(cabend)
    cabend = iso_time(cabend)
    cabend_mjd = datetime_obj_to_mjd(datetime.fromisoformat(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['SUNANGL1'] = gnc_info.get('SUNANGL1', header['SUNANGL0'])
    header['MOONANG1'] = gnc_info.get('MOONANG1', header['MOONANG0'])
    header['MOONANG1'] = gnc_info.get('MOONANG1', header['MOONANG0'])
    header['TEL_ALT1'] = gnc_info.get('TEL_ALT1', header['TEL_ALT0'])
    header['TEL_ALT1'] = gnc_info.get('TEL_ALT1', header['TEL_ALT0'])
@@ -318,7 +318,7 @@ def primary_hdu(
        return 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.
    Generate the header for a single frame.


@@ -466,7 +466,7 @@ def frame_header(obs_info, index, primary_header, camera_dict={}):
    return header
    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.
    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']
    shift = obs_info['shift']
    header['shift'] = f"x:{shift[0]},y:{shift[1]}"
    header['shift'] = f"x:{shift[0]},y:{shift[1]}"


    if output_folder is None:

        fullname = f"{tmp_folder_path}/{filename}"
    fullname = os.path.join(output_folder, filename)
    else:
    print(fullname)
        fullname = f"{output_folder}/{filename}"
    if not os.path.exists(output_folder):
        if os.path.exists(output_folder) is False:
        os.makedirs(output_folder)
        os.makedirs(output_folder)
        log.debug(f"Output folder {output_folder} is created.")
        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
    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.
    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()
        frame_hdu.add_checksum()
        hdu_list.append(frame_hdu)
        hdu_list.append(frame_hdu)


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


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


import numpy as np
import numpy as np
from .target import spectrum_generator, target_file_load
from .target import spectrum_generator, target_file_load
@@ -123,7 +124,8 @@ def vis_observation(
    params['EXPSTART'] = expt_start_iso.isoformat()
    params['EXPSTART'] = expt_start_iso.isoformat()
    params['EXPEND'] = expt_end_iso.isoformat()
    params['EXPEND'] = expt_end_iso.isoformat()
    params['frame_info'] = all_frame_info
    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:
    if prograss_bar:
        pg_bar.close()
        pg_bar.close()
        print(f'  Done [{time.time() - start_time:.1f}s]                             ')
        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):
    for ind_target, file in enumerate(file_list):
        try:
            with open(file, 'r') as fid:
            with open(file, 'r') as fid:
                obs_info = yaml.load(fid, Loader=yaml.FullLoader)
                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', {})
            gnc_info = obs_info.get('gnc_info', {})
            time = obs_info.get('time', 0)
            time = obs_info.get('time', 0)
            emgain = obs_info.get('emgain', None)
            emgain = obs_info.get('emgain', None)
        
            time = relative_time(time)
            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
        ind_camera = 0
        for camera_name, camera in zip(all_camera_name, all_camera):
        for camera_name, camera in zip(all_camera_name, all_camera):
            try:
            ind_camera += 1
            ind_camera += 1
            ind_run = ind_target * len(all_camera) + ind_camera
            ind_run = ind_target * len(all_camera) + ind_camera
            all_run = len(all_camera) * len(file_list)
            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}"
            info_text = f"({ind_run}/{all_run}) obsid[{obsid}] with {camera_name}"
                log.info(info_text)


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


            try:
                vis_observation(
                vis_observation(
                    target,
                    target,
                    skybg,
                    skybg,
@@ -307,7 +314,7 @@ def observation_simulation_from_config(obs_file, config_file):
                    csst_format=csst_format,
                    csst_format=csst_format,
                    prograss_bar=True)
                    prograss_bar=True)
            except Exception as e:
            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):
def main(argv=None):
    parser = argparse.ArgumentParser(description='Cpic obsevation image simulation')
    parser = argparse.ArgumentParser(description='Cpic obsevation image simulation')
@@ -315,7 +322,7 @@ def main(argv=None):


    subparsers = parser.add_subparsers(help='type of runs')
    subparsers = parser.add_subparsers(help='type of runs')
    parser_quickrun = subparsers.add_parser('quickrun', help='a quick observation with no configration file')
    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('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('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')
    parser_quickrun.add_argument('nframe', type=int, help='number of frames')
@@ -361,8 +368,8 @@ def main(argv=None):
    args.func(args)
    args.func(args)
    
    


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




#     target_example = {
#     target_example = {
Loading