diff --git a/doc/versionHistory.rst b/doc/versionHistory.rst index e27a256..def24b4 100644 --- a/doc/versionHistory.rst +++ b/doc/versionHistory.rst @@ -6,6 +6,12 @@ Version History ################## +------------- +0.10.1 +------------- + +* Replace all inst_name calls with CamType. + ------------- 0.10.0 ------------- diff --git a/python/lsst/ts/imsim/closed_loop_task.py b/python/lsst/ts/imsim/closed_loop_task.py index d4bd137..d33c12f 100644 --- a/python/lsst/ts/imsim/closed_loop_task.py +++ b/python/lsst/ts/imsim/closed_loop_task.py @@ -44,7 +44,7 @@ plot_fwhm_of_iters, ) from lsst.ts.ofc import OFC, OFCData -from lsst.ts.wep.utils import CamType, FilterType +from lsst.ts.wep.utils import CamType, FilterType, getCamNameFromCamType, getCamType from lsst.ts.wep.utils import getConfigDir as getWepConfigDir from lsst.ts.wep.utils import rotMatrix, runProgram @@ -75,7 +75,7 @@ def __init__(self) -> None: def config_sky_sim( self, - inst_name: str, + cam_type: CamType, obs_metadata: ObsMetadata, path_sky_file: str = "", star_mag: float = 15.0, @@ -89,8 +89,8 @@ def config_sky_sim( Parameters ---------- - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. obs_metadata : lsst.ts.imsim.ObsMetadata Observation metadata. path_sky_file : str, optional @@ -107,16 +107,16 @@ def config_sky_sim( """ self.sky_sim = SkySim() - self.sky_sim.set_camera(inst_name) + self.sky_sim.set_camera(cam_type) if path_sky_file == "": - self._set_sky_sim_based_on_opd_field_pos(inst_name, obs_metadata, star_mag) + self._set_sky_sim_based_on_opd_field_pos(cam_type, obs_metadata, star_mag) else: abs_sky_file_path = os.path.abspath(path_sky_file) self.sky_sim.add_star_by_file(abs_sky_file_path) def _set_sky_sim_based_on_opd_field_pos( self, - inst_name: str, + cam_type: CamType, obs_metadata: ObsMetadata, star_mag: float, ) -> None: @@ -126,8 +126,8 @@ def _set_sky_sim_based_on_opd_field_pos( Parameters ---------- - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. obs_metadata : lsst.ts.imsim.ObsMetadata object Observation metadata. star_mag : float @@ -146,10 +146,10 @@ def _set_sky_sim_based_on_opd_field_pos( ) opd_metr = OpdMetrology() - if inst_name in ["lsst", "lsstfam", "comcam"]: + if cam_type in [CamType.LsstCam, CamType.LsstFamCam, CamType.ComCam]: field_x, field_y = list(), list() - camera = get_camera(inst_name) - for name in self.get_sensor_name_list_of_fields(inst_name): + camera = get_camera(cam_type) + for name in self.get_sensor_name_list_of_fields(cam_type): detector = camera.get(name) x_rad, y_rad = detector.getCenter(FIELD_ANGLE) x_deg, y_deg = np.rad2deg(x_rad), np.rad2deg(y_rad) @@ -158,7 +158,7 @@ def _set_sky_sim_based_on_opd_field_pos( opd_metr.field_x = field_x opd_metr.field_y = field_y else: - raise ValueError(f"This instrument name ({inst_name}) is not supported.") + raise ValueError(f"This CamType ({cam_type}) is not supported.") star_id = 0 ra_in_deg_arr = np.array(opd_metr.field_x) @@ -183,18 +183,18 @@ def _set_sky_sim_based_on_opd_field_pos( ) star_id += 1 - def config_ofc_calc(self, inst_name: str) -> None: + def config_ofc_calc(self, cam_type: str) -> None: """Configure the OFC calculator. OFC: Optical feedback calculator. Parameters ---------- - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. """ - self.ofc_calc = OFC(OFCData(inst_name)) + self.ofc_calc = OFC(OFCData(getCamNameFromCamType(cam_type))) def map_filter_ref_to_g(self, filter_type_name: str) -> str: """Map the reference filter to the G filter. @@ -234,15 +234,15 @@ def check_boresight(self, boresight: list[float]) -> None: if dec < -90 or dec > 90: raise ValueError("The declination (Dec) should be in [-90, 90].") - def get_sensor_name_list_of_fields(self, inst_name: str) -> list[str]: + def get_sensor_name_list_of_fields(self, cam_type: CamType) -> list[str]: """Get the list of sensor name of fields. The list will be sorted based on the field index. Parameters ---------- - inst_name : enum 'InstName' in lsst.ts.ofc.Utility - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. Returns ------- @@ -255,9 +255,11 @@ def get_sensor_name_list_of_fields(self, inst_name: str) -> list[str]: This instrument name is not supported. """ - camera = get_camera(inst_name) + camera = get_camera(cam_type) detector_type = ( - DetectorType.WAVEFRONT if inst_name == "lsst" else DetectorType.SCIENCE + DetectorType.WAVEFRONT + if cam_type == CamType.LsstCam + else DetectorType.SCIENCE ) return [ detector.getName() @@ -265,15 +267,15 @@ def get_sensor_name_list_of_fields(self, inst_name: str) -> list[str]: if detector.getType() == detector_type ] - def get_sensor_id_list_of_fields(self, inst_name: str) -> list[int]: + def get_sensor_id_list_of_fields(self, cam_type: CamType) -> list[int]: """Get the list of sensor ids of fields. The list will be sorted based on the field index. Parameters ---------- - inst_name : enum 'InstName' in lsst.ts.ofc.Utility - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. Returns ------- @@ -286,10 +288,12 @@ def get_sensor_id_list_of_fields(self, inst_name: str) -> list[int]: This instrument name is not supported. """ - camera = get_camera(inst_name) + camera = get_camera(cam_type) detector_type = ( - DetectorType.WAVEFRONT if inst_name == "lsst" else DetectorType.SCIENCE + DetectorType.WAVEFRONT + if cam_type == CamType.LsstCam + else DetectorType.SCIENCE ) return [ detector.getId() @@ -317,36 +321,6 @@ def check_and_create_base_output_dir(self, base_output_dir: str) -> str: return output_dir - def get_cam_type_and_inst_name(self, inst: str) -> tuple[CamType, str]: - """Get the camera type and instrument name. - - Parameters - ---------- - inst : str - Instrument to use: currently only lsst. - - Returns - ------- - lsst.ts.wep.utils.enumUtils.CamType - Camera type. - str - Instrument name. - - Raises - ------ - ValueError - This instrument is not supported. - """ - - if inst == "lsst": - return CamType.LsstCam, "lsst" - elif inst == "lsstfam": - return CamType.LsstFamCam, "lsstfam" - elif inst == "comcam": - return CamType.ComCam, "comcam" - else: - raise ValueError(f"This instrument ({inst}) is not supported.") - def get_filter_type(self, filter_type_name: str) -> FilterType: """Get the filter type. @@ -386,7 +360,6 @@ def get_filter_type(self, filter_type_name: str) -> FilterType: def _run_sim( self, cam_type: CamType, - inst_name: str, obs_metadata: ObsMetadata, base_output_dir: str, butler_root_path: str, @@ -405,8 +378,6 @@ def _run_sim( ---------- cam_type : enum 'CamType' in lsst.ts.wep.utility Camera type. - inst_name : enum 'InstName' in lsst.ts.ofc.Utility - Instrument name. obs_metadata : lsst.ts.imsim.ObsMetadata object Observation metadata. base_output_dir : str @@ -443,8 +414,8 @@ def _run_sim( # If using wavefront sensors we measure one per pair # and the field if cam_type == CamType.LsstCam: - corner_sensor_name_list = self.get_sensor_name_list_of_fields(inst_name) - corner_sensor_id_list = self.get_sensor_id_list_of_fields(inst_name) + corner_sensor_name_list = self.get_sensor_name_list_of_fields(cam_type) + corner_sensor_id_list = self.get_sensor_id_list_of_fields(cam_type) ref_sensor_name_list = [] ref_sensor_id_list = [] for sens_name, sens_id in zip( @@ -454,8 +425,8 @@ def _run_sim( ref_sensor_name_list.append(sens_name) ref_sensor_id_list.append(sens_id) else: - ref_sensor_name_list = self.get_sensor_name_list_of_fields(inst_name) - ref_sensor_id_list = self.get_sensor_id_list_of_fields(inst_name) + ref_sensor_name_list = self.get_sensor_name_list_of_fields(cam_type) + ref_sensor_id_list = self.get_sensor_id_list_of_fields(cam_type) # Common file and directory names opd_zk_file_name = "opd.zer" @@ -499,7 +470,7 @@ def _run_sim( if cam_type == CamType.LsstCam: self._generate_images( obs_metadata, - inst_name=inst_name, + cam_type=cam_type, sky_seed=sky_seed, pert_seed=pert_seed, num_pro=num_pro, @@ -513,7 +484,7 @@ def _run_sim( obs_metadata.focus_z = focus_z self._generate_images( obs_metadata, - inst_name=inst_name, + cam_type=cam_type, sky_seed=sky_seed, pert_seed=pert_seed, num_pro=num_pro, @@ -524,7 +495,7 @@ def _run_sim( # Analyze the OPD data self.imsim_cmpt.analyze_opd_data( - inst_name, + cam_type, zk_file_name=opd_zk_file_name, rot_opd_in_deg=obs_metadata.rotator_angle, pssn_file_name=opd_pssn_file_name, @@ -535,7 +506,7 @@ def _run_sim( list_of_wf_err = self._calc_wf_err_from_img( obs_metadata, butler_root_path=butler_root_path, - inst_name=inst_name, + cam_type=cam_type, num_pro=num_pro, pipeline_file=pipeline_file, ) @@ -560,7 +531,7 @@ def _run_sim( self.imsim_cmpt.reorder_and_save_wf_err_file( list_of_wf_err, ref_sensor_name_list, - get_camera(inst_name), + get_camera(cam_type), zk_file_name=wfs_zk_file_name, ) @@ -617,7 +588,7 @@ def _run_sim( def _generate_images( self, obs_metadata: ObsMetadata, - inst_name: str, + cam_type: CamType, sky_seed: int = 42, pert_seed: int = 11, num_pro: int = 1, @@ -631,8 +602,8 @@ def _generate_images( ---------- obs_metadata : lsst.ts.imsim.ObsMetadata object Observation metadata. - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. sky_seed : int, optional Random seed for the sky background. (The default is 42.) @@ -655,12 +626,16 @@ def _generate_images( # Generate the images if imsim_config_pointer_file is None: - if inst_name == "lsst": - imsim_config_pointer_file = os.path.join( - get_config_dir(), "lsstCamDefaultPointer.yaml" - ) + if cam_type == CamType.LsstCam: + default_pointer = "lsstCamDefaultPointer.yaml" + elif cam_type == CamType.LsstFamCam: + default_pointer = "lsstFamCamDefaultPointer.yaml" + elif cam_type == CamType.ComCam: + default_pointer = "lsstComCamDefaultPointer.yaml" + imsim_config_pointer_file = os.path.join(get_config_dir(), default_pointer) + base_config_yaml = self.imsim_cmpt.assemble_config_yaml( - obs_metadata, imsim_config_pointer_file, inst_name + obs_metadata, imsim_config_pointer_file, cam_type ) inst_cat = self.imsim_cmpt.gen_instance_catalog(self.sky_sim) @@ -683,7 +658,7 @@ def _generate_images( {"type": "Kolmogorov", "fwhm": 0.7} ) - if inst_name == "lsst": + if cam_type == CamType.LsstCam: imsim_config_yaml = self.imsim_cmpt.add_sources_to_config( base_config_yaml, inst_cat_path, use_ccd_img=self.use_ccd_img ) @@ -694,7 +669,7 @@ def _generate_images( self.imsim_cmpt.write_yaml_and_run_imsim( imsim_config_path, imsim_config_yaml ) - elif inst_name in ["lsstfam", "comcam"]: + elif cam_type in [CamType.LsstFamCam, CamType.ComCam]: if self.use_ccd_img: # Run once for OPD imsim_opd_config_path = os.path.join( @@ -759,7 +734,7 @@ def _calc_wf_err_from_img( self, obs_metadata: ObsMetadata, butler_root_path: str, - inst_name: str, + cam_type: CamType, num_pro: int = 1, pipeline_file: str | None = None, filter_type_name: str = "", @@ -772,8 +747,8 @@ def _calc_wf_err_from_img( Observation metadata. butler_root_path : str Path to the butler repository. - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. num_pro : int, optional Number of processor to run DM pipeline. (the default is 1.) pipeline_file : str or None, optional @@ -790,12 +765,12 @@ def _calc_wf_err_from_img( """ # Ingest images into butler gen3 - self.ingest_data(butler_root_path=butler_root_path, inst_name=inst_name) + self.ingest_data(butler_root_path=butler_root_path, cam_type=cam_type) list_of_wf_err = self.run_wep( obs_metadata.seq_num, butler_root_path, - inst_name, + cam_type, num_pro=num_pro, pipeline_file=pipeline_file, filter_type_name=filter_type_name, @@ -807,7 +782,7 @@ def run_wep( self, seq_num: int, butler_root_path: str, - inst_name: str, + cam_type: CamType, num_pro: int = 1, pipeline_file: str | None = None, filter_type_name: str = "", @@ -820,8 +795,8 @@ def run_wep( Observation id. butler_root_path : str Path to the butler gen3 repos. - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. num_pro : int, optional Number of processor to run DM pipeline. (the default is 1.) pipeline_file : str or None, optional @@ -838,16 +813,14 @@ def run_wep( estimation pipeline for each sensor. """ - if inst_name in ["lsst", "lsstfam"]: + if cam_type in [CamType.LsstCam, CamType.LsstFamCam]: butler_inst_name = "Cam" - elif inst_name == "comcam": + elif cam_type == CamType.ComCam: butler_inst_name = "ComCam" if pipeline_file is None: - pipeline_yaml = f"{inst_name}Pipeline.yaml" + pipeline_yaml = f"{getCamNameFromCamType(cam_type)}Pipeline.yaml" pipeline_yaml_path = os.path.join(butler_root_path, pipeline_yaml) - self.write_wep_configuration( - inst_name, pipeline_yaml_path, filter_type_name - ) + self.write_wep_configuration(cam_type, pipeline_yaml_path, filter_type_name) else: pipeline_yaml_path = pipeline_file @@ -864,7 +837,7 @@ def run_wep( # The limit for seq_num is 5 digits, # set by the expectation that no more than 100K images # could be taken in a single day (i.e. no more than 1/sec). - if inst_name == "lsst": + if cam_type == CamType.LsstCam: runProgram( f"pipetask run -b {butler_root_path} " f"-i refcats,LSST{butler_inst_name}/raw/all,LSST{butler_inst_name}/calib/unbounded " @@ -872,7 +845,7 @@ def run_wep( f"--register-dataset-types --output-run ts_imsim_{seq_num} -p {pipeline_yaml_path} -d " f'"visit.seq_num IN ({seq_num})" -j {num_pro}' ) - elif inst_name == "lsstfam": + elif cam_type == CamType.LsstFamCam: runProgram( f"pipetask run -b {butler_root_path} " f"-i refcats,LSST{butler_inst_name}/raw/all,LSST{butler_inst_name}/calib/unbounded " @@ -880,7 +853,7 @@ def run_wep( f"--register-dataset-types --output-run ts_imsim_{seq_num} -p {pipeline_yaml_path} -d " f'"visit.seq_num IN ({seq_num-1}, {seq_num})" -j {num_pro}' ) - elif inst_name == "comcam": + elif cam_type == CamType.ComCam: runProgram( f"pipetask run -b {butler_root_path} " f"-i refcats,LSST{butler_inst_name}/raw/all,LSST{butler_inst_name}/calib/unbounded " @@ -931,14 +904,14 @@ def run_wep( return list_of_wf_err def write_wep_configuration( - self, inst_name: str, pipeline_yaml_path: str, filter_type_name: str + self, cam_type: CamType, pipeline_yaml_path: str, filter_type_name: str ) -> None: """Write wavefront estimation pipeline task configuration. Parameters ---------- - inst_name : str - Name of the instrument this configuration is intended for. + cam_type : lsst.ts.wep.utils.CamType + Camera type. pipeline_yaml_path : str Path where the pipeline task configuration yaml file should be saved. @@ -946,9 +919,9 @@ def write_wep_configuration( Filter type name: ref (or ''), u, g, r, i, z, or y. """ - if inst_name in ["lsst", "lsstfam"]: + if cam_type in [CamType.LsstCam, CamType.LsstFamCam]: butler_inst_name = "Cam" - elif inst_name == "comcam": + elif cam_type == CamType.ComCam: butler_inst_name = "ComCam" # Remap reference filter @@ -964,7 +937,7 @@ def write_wep_configuration( instrument: lsst.obs.lsst.Lsst{butler_inst_name} # Use imported instrument configuration imports: - - location: {getWepConfigDir()}/cwfs/instData/{inst_name}/instParamPipeConfig.yaml + - location: {getWepConfigDir()}/cwfs/instData/{getCamNameFromCamType(cam_type)}/instParamPipeConfig.yaml # Then we can specify each task in our pipeline by a name # and then specify the class name corresponding to that task tasks: @@ -1065,7 +1038,7 @@ def run_img( raw_seeing : float Raw seeing in arcsec. """ - cam_type, inst_name = self.get_cam_type_and_inst_name(inst) + cam_type = getCamType(inst) base_output_dir = self.check_and_create_base_output_dir(base_output_dir) if do_erase_dir_content: self.erase_directory_content(base_output_dir) @@ -1090,9 +1063,9 @@ def run_img( # Configure the components self.config_sky_sim( - inst_name, obs_metadata, path_sky_file=path_sky_file, star_mag=star_mag + cam_type, obs_metadata, path_sky_file=path_sky_file, star_mag=star_mag ) - self.config_ofc_calc(inst_name) + self.config_ofc_calc(cam_type) self.imsim_cmpt = ImsimCmpt() # If path_sky_file using default OPD positions write this to disk @@ -1106,7 +1079,7 @@ def run_img( butler_root_path = os.path.join(base_output_dir, "imsimData") if self.use_ccd_img: - self.generate_butler(butler_root_path, inst_name) + self.generate_butler(butler_root_path, cam_type) self.generate_ref_catalog( butler_root_path=butler_root_path, path_sky_file=path_sky_file, @@ -1115,7 +1088,6 @@ def run_img( self._run_sim( cam_type, - inst_name, obs_metadata, base_output_dir, butler_root_path, @@ -1129,24 +1101,26 @@ def run_img( turn_off_atmosphere=turn_off_atmosphere, ) - def generate_butler(self, butler_root_path: str, inst_name: str) -> None: + def generate_butler(self, butler_root_path: str, cam_type: CamType) -> None: """Generate butler gen3. Parameters ---------- butler_root_path: `str` Path to where the butler repository should be created. - inst_name: `str` - Name of the instrument. + cam_type : lsst.ts.wep.utils.CamType + Camera type. """ - self.log.info(f"Generating butler gen3 in {butler_root_path} for {inst_name}") + self.log.info( + f"Generating butler gen3 in {butler_root_path} for {cam_type.name}" + ) runProgram(f"butler create {butler_root_path}") - if inst_name in ["lsst", "lsstfam"]: + if cam_type in [CamType.LsstCam, CamType.LsstFamCam]: butler_inst_name = "Cam" - elif inst_name == "comcam": + elif cam_type == CamType.ComCam: butler_inst_name = "ComCam" self.log.debug(f"Registering Lsst{butler_inst_name}") @@ -1212,25 +1186,25 @@ def generate_ref_catalog( f"butler ingest-files -t direct {butler_root_path} cal_ref_cat refcats {sky_ecsv_file_name}" ) - def ingest_data(self, butler_root_path: str, inst_name: str) -> None: + def ingest_data(self, butler_root_path: str, cam_type: CamType) -> None: """Ingest data into a gen3 data Butler. Parameters ---------- butler_root_path : str Path to the butler repository. - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. """ output_img_dir = self.imsim_cmpt.output_img_dir files = " ".join(glob(os.path.join(output_img_dir, "amp*"))) - if inst_name in ["lsst", "lsstfam", "comcam"]: + if cam_type in [CamType.LsstCam, CamType.LsstFamCam, CamType.ComCam]: runProgram(f"butler ingest-raws {butler_root_path} {files}") - if inst_name in ["lsst", "lsstfam"]: + if cam_type in [CamType.LsstCam, CamType.LsstFamCam]: butler_inst_name = "Cam" - elif inst_name == "comcam": + elif cam_type == CamType.ComCam: butler_inst_name = "ComCam" runProgram( diff --git a/python/lsst/ts/imsim/imsim_cmpt.py b/python/lsst/ts/imsim/imsim_cmpt.py index ee94220..09df237 100644 --- a/python/lsst/ts/imsim/imsim_cmpt.py +++ b/python/lsst/ts/imsim/imsim_cmpt.py @@ -39,7 +39,7 @@ make_dir, ) from lsst.ts.ofc.ofc_data.base_ofc_data import BaseOFCData -from lsst.ts.wep.utils import runProgram +from lsst.ts.wep.utils import CamType, runProgram from scipy.ndimage import rotate @@ -130,7 +130,7 @@ def assemble_config_yaml( self, obs_metadata: ObsMetadata, config_pointer_file: str, - inst_name: str, + cam_type: CamType, required_modules_file: str | None = None, ) -> dict[str, Any]: """ @@ -142,8 +142,8 @@ def assemble_config_yaml( The location of the config pointer file that specifies the location of configurations files for each imsim component. - inst_name : str - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. required_modules_file : str or None Path to yaml file with required imsim modules. If None, then will use policy/requiredModulesDefault.yaml. @@ -197,7 +197,7 @@ def assemble_config_yaml( # Add additional header text full_config_text += self.add_config_header(obs_metadata) + "\n" # Add OPD - full_config_text += self.format_opd_text(obs_metadata, inst_name) + full_config_text += self.format_opd_text(obs_metadata, cam_type) # Assemble as yaml full_config_yaml = yaml.safe_load(full_config_text) @@ -240,7 +240,7 @@ def convert_obs_metadata_to_text(self, obs_metadata: ObsMetadata) -> str: return obs_variables_text - def format_opd_text(self, obs_metadata: ObsMetadata, inst_name: str) -> str: + def format_opd_text(self, obs_metadata: ObsMetadata, cam_type: CamType) -> str: """ Write out the OPD section of the config file. @@ -248,8 +248,8 @@ def format_opd_text(self, obs_metadata: ObsMetadata, inst_name: str) -> str: ---------- obs_metadata : lsst.ts.imsim.ObsMetadata ObsMetadata dataclass object with observation information. - inst_name: str - Name of the instrument + cam_type : lsst.ts.wep.utils.CamType + Camera type. Returns ------- @@ -267,7 +267,7 @@ def format_opd_text(self, obs_metadata: ObsMetadata, inst_name: str) -> str: opd_section_text += " fields:\n" # Get the locations for the OPD from OPD Metrology - self.opd_metr.set_wgt_and_field_xy_of_gq(inst_name) + self.opd_metr.set_wgt_and_field_xy_of_gq(cam_type) for thx, thy in zip(self.opd_metr.field_x, self.opd_metr.field_y): opd_section_text += f" - {{thx: {thx} deg, thy: {thy} deg}}\n" @@ -510,7 +510,7 @@ def generate_star( def analyze_opd_data( self, - inst_name: str, + cam_type: CamType, zk_file_name: str = "opd.zer", rot_opd_in_deg: float = 0.0, pssn_file_name: str = "PSSN.txt", @@ -525,8 +525,8 @@ def analyze_opd_data( Parameters ---------- - inst_name : `str` - Instrument name. + cam_type : lsst.ts.wep.utils.CamType + Camera type. zk_file_name : str, optional OPD in zk file name. (the default is "opd.zer".) rot_opd_in_deg : float, optional @@ -537,10 +537,10 @@ def analyze_opd_data( """ # Set the weighting ratio and field positions of OPD - if inst_name == "lsst": + if cam_type == CamType.LsstCam: self.opd_metr.set_default_lsst_wfs_gq() else: - self.opd_metr.set_wgt_and_field_xy_of_gq(inst_name) + self.opd_metr.set_wgt_and_field_xy_of_gq(cam_type) num_opd = len(self.opd_metr.field_x) self.opd_file_path = os.path.join(self.output_img_dir, "opd.fits") diff --git a/python/lsst/ts/imsim/opd_metrology.py b/python/lsst/ts/imsim/opd_metrology.py index 5c0e0cb..8dc37bc 100644 --- a/python/lsst/ts/imsim/opd_metrology.py +++ b/python/lsst/ts/imsim/opd_metrology.py @@ -27,7 +27,12 @@ from lsst.afw.cameraGeom import FIELD_ANGLE from lsst.ts.imsim.utils import calc_pssn, get_camera from lsst.ts.ofc.utils import get_config_dir as getConfigDirOfc -from lsst.ts.wep.utils import ZernikeAnnularFit, ZernikeEval +from lsst.ts.wep.utils import ( + CamType, + ZernikeAnnularFit, + ZernikeEval, + getCamNameFromCamType, +) class OpdMetrology: @@ -99,7 +104,7 @@ def get_default_lsst_wfs_gq(self) -> tuple[list[float], list[float], list[int]]: # These will be chosen at the center of the extra-intra # focal pairs of wavefront sensors. det_ids = [191, 195, 199, 203] - camera = get_camera("lsst") + camera = get_camera(CamType.LsstCam) field_x = [] field_y = [] det_map = camera.getIdMap() @@ -113,16 +118,16 @@ def get_default_lsst_wfs_gq(self) -> tuple[list[float], list[float], list[int]]: return field_x, field_y, det_ids - def set_wgt_and_field_xy_of_gq(self, inst_name: str) -> None: + def set_wgt_and_field_xy_of_gq(self, cam_type: CamType) -> None: """Set the GQ weighting ratio and field X, Y. GQ: Gaussian quadrature. Parameters ---------- - inst_name : `str` - Instrument name. - Valid options are 'lsst', 'lsstfam', or 'comcam'. + cam_type : lsst.ts.wep.utils.CamType + Camera type. + Valid CamTypes are LsstCam, LsstFamCam, ComCam. Raises ------ @@ -131,12 +136,14 @@ def set_wgt_and_field_xy_of_gq(self, inst_name: str) -> None: """ # Set camera and field ids for given instrument - if inst_name == "lsst": + if cam_type == CamType.LsstCam: self.set_default_lsst_wfs_gq() return weight_dir_path = getConfigDirOfc() / "image_quality_weights" - path_wgt_file = weight_dir_path / f"{inst_name}_weights.yaml" + path_wgt_file = ( + weight_dir_path / f"{getCamNameFromCamType(cam_type)}_weights.yaml" + ) if not path_wgt_file.exists(): raise RuntimeError( @@ -150,13 +157,13 @@ def set_wgt_and_field_xy_of_gq(self, inst_name: str) -> None: # Normalize weights self.wt = wgt_values / np.sum(wgt_values) - camera = get_camera(inst_name) - if inst_name == "lsstfam": + camera = get_camera(cam_type) + if cam_type == CamType.LsstFamCam: self.sensor_ids = np.arange(189) - elif inst_name == "comcam": + elif cam_type == CamType.ComCam: self.sensor_ids = np.arange(9) else: - raise ValueError(f"Instrument {inst_name} is not supported in OPD mode.") + raise ValueError(f"CamType {cam_type} is not supported in OPD mode.") field_x = [] field_y = [] diff --git a/python/lsst/ts/imsim/sky_sim.py b/python/lsst/ts/imsim/sky_sim.py index e83a096..edabf52 100644 --- a/python/lsst/ts/imsim/sky_sim.py +++ b/python/lsst/ts/imsim/sky_sim.py @@ -23,6 +23,7 @@ import numpy as np from lsst.ts.imsim.utils import get_camera +from lsst.ts.wep.utils import CamType class SkySim: @@ -44,16 +45,16 @@ def __init__(self) -> None: # Camera self._camera = None - def set_camera(self, inst_name: str) -> None: + def set_camera(self, cam_type: CamType) -> None: """Set the camera. Parameters ---------- - inst_name : `str` - Instrument name. Valid options are 'lsstfam' or 'lsst'. + cam_type : lsst.ts.wep.utils.CamType + Camera type. """ - self._camera = get_camera(inst_name) + self._camera = get_camera(cam_type) def add_star_by_ra_dec_in_deg( self, diff --git a/python/lsst/ts/imsim/utils/utility.py b/python/lsst/ts/imsim/utils/utility.py index 80761ff..4e4c3e1 100644 --- a/python/lsst/ts/imsim/utils/utility.py +++ b/python/lsst/ts/imsim/utils/utility.py @@ -35,6 +35,7 @@ import yaml from lsst.afw import cameraGeom from lsst.obs.lsst import LsstCam, LsstComCam +from lsst.ts.wep.utils import CamType from lsst.utils import getPackageDir from numpy import ndarray @@ -75,13 +76,13 @@ def get_config_dir() -> str: return os.path.join(get_policy_path(), "config") -def get_camera(inst_name: str) -> cameraGeom.Camera: +def get_camera(cam_type: CamType) -> cameraGeom.Camera: """Returns a lsst instrument for a given instrument name. Parameters ---------- - inst_name : `str` - Instrument name. Valid options are 'lsstfam' or 'lsst'. + cam_type : lsst.ts.wep.utils.CamType + Camera type. Returns ------- @@ -90,16 +91,16 @@ def get_camera(inst_name: str) -> cameraGeom.Camera: Raises ------ ValueError - If input `instName` is not valid. + If input `cam_type` is not valid. """ # Check the input - if (inst_name == "lsstfam") or (inst_name == "lsst"): + if cam_type in [CamType.LsstCam, CamType.LsstFamCam]: return LsstCam().getCamera() - elif inst_name == "comcam": + elif cam_type == CamType.ComCam: return LsstComCam().getCamera() else: raise ValueError( - f"This instrument name ({inst_name}) is not supported. Must be 'lsstfam', 'lsst', or 'comcam'." + f"This CamType ({cam_type}) is not supported. Must be LsstCam, LsstFamCam or ComCam." ) diff --git a/tests/test_closed_loop_task.py b/tests/test_closed_loop_task.py index b55f6ec..aa1e476 100644 --- a/tests/test_closed_loop_task.py +++ b/tests/test_closed_loop_task.py @@ -51,7 +51,7 @@ def test_config_sky_sim_with_error(self): ) def test_config_sky_sim_no_sky_file_lsst(self): - self.closed_loop_task.config_sky_sim("lsst", self.obs_metadata) + self.closed_loop_task.config_sky_sim(CamType.LsstCam, self.obs_metadata) sky_sim = self.closed_loop_task.sky_sim self.assertEqual(len(sky_sim.star_id), 8) @@ -61,7 +61,7 @@ def test_config_sky_sim_with_sky_file(self): get_module_path(), "tests", "testData", "sky", "wfsStar.txt" ) self.closed_loop_task.config_sky_sim( - "lsst", self.obs_metadata, path_sky_file=test_sky_file + CamType.LsstCam, self.obs_metadata, path_sky_file=test_sky_file ) sky_sim = self.closed_loop_task.sky_sim @@ -69,11 +69,11 @@ def test_config_sky_sim_with_sky_file(self): self.assertEqual(sky_sim.mag[0], 15) def test_config_ofc_calc(self): - inst_name = "lsst" - self.closed_loop_task.config_ofc_calc(inst_name) + cam_type = CamType.LsstCam + self.closed_loop_task.config_ofc_calc(cam_type) ofc_calc = self.closed_loop_task.ofc_calc - self.assertEqual(ofc_calc.ofc_data.name, inst_name) + self.assertEqual(ofc_calc.ofc_data.name, "lsst") def test_map_filter_ref_to_g(self): # test that the reference filter @@ -125,28 +125,6 @@ def test_get_filter_type_err(self): ValueError, self.closed_loop_task.get_filter_type, "noThisFilterType" ) - def test_get_cam_type_and_inst_name_lsst(self): - cam_type, inst_name = self.closed_loop_task.get_cam_type_and_inst_name("lsst") - self.assertEqual(cam_type, CamType.LsstCam) - self.assertEqual(inst_name, "lsst") - - def test_get_cam_type_and_inst_name_fam(self): - cam_type, inst_name = self.closed_loop_task.get_cam_type_and_inst_name( - "lsstfam" - ) - self.assertEqual(cam_type, CamType.LsstFamCam) - self.assertEqual(inst_name, "lsstfam") - - def test_get_cam_type_and_inst_name_comcam(self): - cam_type, inst_name = self.closed_loop_task.get_cam_type_and_inst_name("comcam") - self.assertEqual(cam_type, CamType.ComCam) - self.assertEqual(inst_name, "comcam") - - def test_get_cam_type_and_inst_name_err(self): - self.assertRaises( - ValueError, self.closed_loop_task.get_cam_type_and_inst_name, "noThisInst" - ) - def test_erase_directory_content(self): # Make the temporary directory temp_dir = os.path.join(self.test_dir.name, "tempDir") @@ -206,7 +184,9 @@ def test_set_img_parser(self): self.assertEqual(args.star_mag, 15.0) def test_get_sensor_name_list_of_field_lsst_wfs(self): - sensor_name_list = self.closed_loop_task.get_sensor_name_list_of_fields("lsst") + sensor_name_list = self.closed_loop_task.get_sensor_name_list_of_fields( + CamType.LsstCam + ) self.assertEqual(len(sensor_name_list), 8) sensor_name_list_ans = [ @@ -222,7 +202,9 @@ def test_get_sensor_name_list_of_field_lsst_wfs(self): self.assertCountEqual(sensor_name_list, sensor_name_list_ans) def test_get_sensor_id_list_of_fields(self): - sensor_id_list = self.closed_loop_task.get_sensor_id_list_of_fields("lsst") + sensor_id_list = self.closed_loop_task.get_sensor_id_list_of_fields( + CamType.LsstCam + ) self.assertEqual(len(sensor_id_list), 8) sensor_id_list_ans = [191, 192, 195, 196, 199, 200, 203, 204] diff --git a/tests/test_imsim_cmpt.py b/tests/test_imsim_cmpt.py index eaa8fca..6d3c920 100644 --- a/tests/test_imsim_cmpt.py +++ b/tests/test_imsim_cmpt.py @@ -34,6 +34,7 @@ get_module_path, get_zk_from_file, ) +from lsst.ts.wep.utils import CamType class TestImsimCmpt(unittest.TestCase): @@ -83,7 +84,7 @@ def tearDown(self): shutil.rmtree(self.output_dir) def _map_sensor_name_and_id(self, sensor_name_list): - camera = get_camera("lsst") + camera = get_camera(CamType.LsstCam) return dict( [ (camera[detector].getName(), camera[detector].getId()) @@ -108,7 +109,9 @@ def test_verify_pointer_file_raises_error(self): def test_assemble_config(self): full_config_yaml = self.imsim_cmpt.assemble_config_yaml( - self.obs_metadata_test, self.config_pointer_default_lsst_cam, "lsst" + self.obs_metadata_test, + self.config_pointer_default_lsst_cam, + CamType.LsstCam, ) self.full_test_yaml["output"]["dir"] = self.imsim_cmpt.output_img_dir self.assertDictEqual(full_config_yaml, self.full_test_yaml) @@ -124,7 +127,9 @@ def test_convert_obs_metadata_to_text(self): self.assertDictEqual(yaml.safe_load(obs_variables_text), default_vars) def test_format_opd_text_lsst_cam(self): - opd_text = self.imsim_cmpt.format_opd_text(self.obs_metadata_test, "lsst") + opd_text = self.imsim_cmpt.format_opd_text( + self.obs_metadata_test, CamType.LsstCam + ) self.assertTrue(opd_text.startswith(" opd:")) self.assertTrue( opd_text.endswith( @@ -253,7 +258,7 @@ def _analyze_lsst_cam_opd_data(self, rot_opd_in_deg=0.0): self.imsim_cmpt.output_img_dir, ) self.imsim_cmpt.analyze_opd_data( - "lsst", + CamType.LsstCam, zk_file_name=self.zk_file_name, rot_opd_in_deg=rot_opd_in_deg, pssn_file_name=self.pssn_file_name, @@ -343,7 +348,7 @@ def test_reorder_and_save_wf_err_file(self): sensor_id_list = list(map_sensor_name_and_id.values()) zk_file_name = "testZk.zer" - camera = get_camera("lsst") + camera = get_camera(CamType.LsstCam) self.imsim_cmpt.reorder_and_save_wf_err_file( list_of_wf_err, ref_sensor_name_list, camera, zk_file_name=zk_file_name ) diff --git a/tests/test_opd_metrology.py b/tests/test_opd_metrology.py index fffd832..9c0f8d3 100644 --- a/tests/test_opd_metrology.py +++ b/tests/test_opd_metrology.py @@ -28,6 +28,7 @@ from lsst.afw.cameraGeom import FIELD_ANGLE from lsst.ts.imsim import OpdMetrology from lsst.ts.imsim.utils.utility import get_module_path, get_zk_from_file +from lsst.ts.wep.utils import CamType class TestOpdMetrology(unittest.TestCase): @@ -50,7 +51,7 @@ def test_set_weighting_ratio(self): self.assertEqual(str(context.exception), "All weighting ratios should be >= 0.") def test_set_wgt_and_field_xy_of_gq_lsst_fam(self): - self.metr.set_wgt_and_field_xy_of_gq("lsstfam") + self.metr.set_wgt_and_field_xy_of_gq(CamType.LsstFamCam) field_x = self.metr.field_x self.assertEqual(len(field_x), 189) @@ -59,7 +60,7 @@ def test_set_wgt_and_field_xy_of_gq_lsst_fam(self): self.assertEqual(len(wgt), 189) def test_set_wgt_and_field_xy_of_gq_com_cam(self): - self.metr.set_wgt_and_field_xy_of_gq("comcam") + self.metr.set_wgt_and_field_xy_of_gq(CamType.ComCam) field_x = self.metr.field_x self.assertEqual(len(field_x), 9) @@ -69,7 +70,7 @@ def test_set_wgt_and_field_xy_of_gq_com_cam(self): def test_set_wgt_and_field_xy_of_gq_err(self): self.assertRaises( - RuntimeError, self.metr.set_wgt_and_field_xy_of_gq, "NoThisInstName" + ValueError, self.metr.set_wgt_and_field_xy_of_gq, "NoThisInstName" ) def test_set_default_lsst_wfs_gq(self): diff --git a/tests/test_sky_sim.py b/tests/test_sky_sim.py index 7cb83e9..e41f941 100644 --- a/tests/test_sky_sim.py +++ b/tests/test_sky_sim.py @@ -24,6 +24,7 @@ from lsst.ts.imsim import SkySim from lsst.ts.imsim.utils.utility import get_module_path +from lsst.ts.wep.utils import CamType class TestSkySim(unittest.TestCase): @@ -31,7 +32,7 @@ def setUp(self): self.sky_sim = SkySim() def test_set_camera(self): - self.sky_sim.set_camera("lsstfam") + self.sky_sim.set_camera(CamType.LsstFamCam) self.assertEqual(self.sky_sim._camera.getName(), "LSSTCam") def test_add_star_by_ra_dec_in_deg(self): diff --git a/tests/utils/test_utility.py b/tests/utils/test_utility.py index 6a80c3d..a675167 100644 --- a/tests/utils/test_utility.py +++ b/tests/utils/test_utility.py @@ -32,6 +32,7 @@ get_policy_path, get_zk_from_file, ) +from lsst.ts.wep.utils import CamType class TestUtility(unittest.TestCase): @@ -50,15 +51,15 @@ def test_get_config_dir(self): ) def test_get_camera(self): - lsst_fam_cam = get_camera("lsstfam") + lsst_fam_cam = get_camera(CamType.LsstFamCam) self.assertIsInstance(lsst_fam_cam, cameraGeom.Camera) self.assertEqual(lsst_fam_cam.getName(), LsstCam.getCamera().getName()) - lsst_cam = get_camera("lsst") + lsst_cam = get_camera(CamType.LsstCam) self.assertIsInstance(lsst_cam, cameraGeom.Camera) self.assertEqual(lsst_cam.getName(), LsstCam.getCamera().getName()) - com_cam = get_camera("comcam") + com_cam = get_camera(CamType.ComCam) self.assertIsInstance(com_cam, cameraGeom.Camera) self.assertEqual(com_cam.getName(), LsstComCam.getCamera().getName())