def test_is_polar(self, mock_getkey, m_copy, m_spice, m_cam, m_pvl, m_path): c1 = hnp.Cube("dummy/PSP_010502_2090_RED4.HiStitch.balance.cub") c2 = hnp.Cube("dummy/PSP_010502_2090_RED3.HiStitch.balance.cub") self.assertFalse(hnp.is_polar([c1, c2], 87, "tt")) self.assertTrue(hnp.is_polar([c1, c2], 60, "tt"))
def setUp(self, getkey_k): self.r4 = hnp.Cube("dummy/PSP_010502_2090_RED4.HiStitch.balance.cub") self.r3 = hnp.Cube("dummy/PSP_010502_2090_RED3.HiStitch.balance.cub") self.r5 = hnp.Cube("dummy/PSP_010502_2090_RED5.HiStitch.balance.cub") self.i1 = hnp.Cube("dummy/PSP_010502_2090_IR10.HiStitch.balance.cub") self.sets = dict( CCDs="RED3,RED4,RED5,BG12", Alt_1_CCDs="RED4,RED5,RED6,IR11", Alt_2_CCDs="RED3,RED4,RED5,IR10", Alt_3_CCDs="IR10,RED4,RED5,BG12", )
def test_handmos_side(self, mock_getkey, m_hand): c2 = hnp.Cube("dummy/PSP_010502_2090_RED2.HiStitch.balance.cub") c3 = hnp.Cube("dummy/PSP_010502_2090_RED3.HiStitch.balance.cub") c4 = hnp.Cube("dummy/PSP_010502_2090_RED4.HiStitch.balance.cub") c5 = hnp.Cube("dummy/PSP_010502_2090_RED5.HiStitch.balance.cub") c6 = hnp.Cube("dummy/PSP_010502_2090_RED6.HiStitch.balance.cub") cubes = (c2, c3, c4, c5, c6) for i, c in enumerate(cubes): cubes[i].next_path = c.path cubes[i].line_offset = 10 cubes[i].samp_offset = 10 hnp.handmos_side(cubes, c4, "dummy/mosaic.cub", True) hnp.handmos_side(cubes, c4, "dummy/mosaic.cub", False) self.assertEqual( m_hand.call_args_list, [ call( c3.path, mosaic="dummy/mosaic.cub", outband=1, outline=11, outsample=11, priority="ontop", ), call( c2.path, mosaic="dummy/mosaic.cub", outband=1, outline=21, outsample=21, priority="ontop", ), call( c5.path, mosaic="dummy/mosaic.cub", outband=1, outline=-9, outsample=-9, priority="beneath", ), call( c6.path, mosaic="dummy/mosaic.cub", outband=1, outline=-19, outsample=-19, priority="beneath", ), ], )
def precision(obsid, conf_dir: Path, parent: Path, hijack=False, keep=False): if hijack: bal_cubs = get_cubes(f"{obsid}*balance.cub", parent) # HiJACK - starts with balance cubes, needs all the colors # takes all color balance.cubs # creates a whole slow of files in HiJACK/ HiJACK.HiJACK(bal_cubs, conf_dir, outdir=(parent / "HiJACK"), plot=False, keep=keep) else: red_bal_cubs = get_cubes(f"{obsid}_RED*balance.cub", parent) # HiNoProj - alternate to HiccdStitch, starts with balance cubes # takes only REDS: *RED*balance.cub # creates PSP_010502_2090_RED.NOPROJ.cub HiNoProj.HiNoProj(red_bal_cubs, pvl.load(conf_dir / "HiNoProj.conf"), keep=keep) return
def HiJACK( cube_paths: list, conf_dir: os.PathLike, outdir=Path("./HiJACK"), base_ccd_number=5, plot=True, keep=False, ): cubes = list(map(hnp.Cube, cube_paths)) cubes.sort() logger.info(f"HiJACK start: {', '.join(map(str, cubes))}") base_cube = check(cubes, base_ccd_number) temp_token = datetime.now().strftime("HiJACK-%y%m%d%H%M%S") outdir_p = Path(outdir) outdir_p.mkdir(exist_ok=True) to_del = isis.PathSet() # Pre-HiJACK for c in cubes: c.next_path = to_del.add( (outdir_p / c.path.name).with_suffix( f".{temp_token}.prehijack.cub" ) ) flat_path = outdir_p / "{}.{}.{}".format( cubes[0].get_obsid(), temp_token, "RED5-RED4_prehijack.flat.tab" ) match_red(cubes, base_cube, flat_path) # jitter_path = outdir_p / (str(cubes[0].get_obsid()) + "_jitter_cpp.txt") jitter_path = outdir_p / (str(cubes[0].get_obsid()) + "_jitter_py.txt") if not jitter_path.exists(): resolvejitter_conf = Path(conf_dir) / "ResolveJitter.conf" ResolveJitter( cubes, pvl.load(str(resolvejitter_conf)), jitter_path, temp_token, keep=keep, ) # SmearStats just updates the db, not needed. # HiJACK proper: hijack_conf = pvl.load(str(Path(conf_dir) / "HiJACK.conf")) hnp.conf_check(hijack_conf["HiJACK"]) polar = False if hijack_conf["HiJACK"]["Shape"] == "USER": polar = hnp.is_polar(cubes, hijack_conf["Pole_Tolerance"], temp_token) for c in cubes: hnp.copy_and_spice( c.next_path, to_del.add(c.path.with_suffix(f".{temp_token}.spiced.cub")), hijack_conf["HiJACK"], polar, ) # Run hijitter inlist = list() outlist = list() for c in cubes: c.next_path = ( (outdir_p / c.path.name) .with_suffix("") .with_suffix(".dejittered.cub") ) inlist.append(c.path) outlist.append(c.next_path) jitterck_p = outdir_p / (str(cubes[0].get_obsid()) + ".jittery.bc") hijitregdef_p = ( Path(os.environ["ISIS3DATA"]) / "mro/calibration/hijitreg.p1745.s3070.def" ) # inlist_p = to_del.add(isis.fromlist.make(inlist, # (outdir_p / # (str(cubes[0].get_obsid()) + # '_hijitter.inlst')))) # outlist_p = to_del.add(isis.fromlist.make(outlist, # inlist_p.with_suffix('.outlst'))) with isis.fromlist.temp(inlist) as inlist_f: with isis.fromlist.temp(outlist) as outlist_f: isis.hijitter( fromlist=inlist_f, jitter=jitter_path, regdef=hijitregdef_p, tolist=outlist_f, jitterck=jitterck_p, ) # Mosaic dejittered cubs # Remember hijitter makes all the individual cubes the size of the # entire image. With the image data in the appropriate space for the ccd. out_root = outdir_p / str(cubes[0].get_obsid()) # All REDs red_p = out_root.with_name(out_root.name + "_RED.NOPROJ.cub") red_5 = list( filter(lambda x: x.ccdname == "RED" and x.ccdnumber == "5", cubes) )[0] # shutil.copyfile(base_cube.next_path, red_p) shutil.copyfile(red_5.next_path, red_p) logger.info( "Original Perl hard codes this file copy from RED5, even if " "another cube is selected as the base_ccd." ) (red_cubes, red_flat_files) = hnp.add_offsets( list(filter(lambda x: x.ccdname == "RED", cubes)), 5, temp_token, keep=True, ) hnp.handmos_side(red_cubes, red_5, red_p, left=True) hnp.handmos_side(red_cubes, red_5, red_p, left=False) hnp.fix_labels( red_cubes, red_p, red_5, "{}_RED".format(str(red_cubes[0].get_obsid())) ) # Center RED for color center_red_p = out_root.with_name(out_root.name + "_RED4-5.NOPROJ.cub") center_red_cubes = list( filter(lambda x: x.ccdnumber == "4" or x.ccdnumber == "5", red_cubes) ) shutil.copyfile(center_red_cubes[1].next_path, center_red_p) mosaic_dejittered( center_red_cubes, center_red_p, "{}_RED4-5".format(str(center_red_cubes[0].get_obsid())), ) ir_p = out_root.with_name(out_root.name + "_IR.NOPROJ.cub") (ir_cubes, _) = hnp.add_offsets( list(filter(lambda x: x.ccdname == "IR", cubes)), 5, temp_token, keep=keep, ) shutil.copyfile(ir_cubes[1].next_path, ir_p) mosaic_dejittered( ir_cubes, ir_p, "{}_IR".format(str(ir_cubes[0].get_obsid())) ) # BG next bg_p = out_root.with_name(out_root.name + "_BG.NOPROJ.cub") (bg_cubes, _) = hnp.add_offsets( list(filter(lambda x: x.ccdname == "BG", cubes)), 5, temp_token, keep=keep, ) shutil.copyfile(bg_cubes[1].next_path, bg_p) mosaic_dejittered( bg_cubes, bg_p, "{}_BG".format(str(bg_cubes[0].get_obsid())) ) # Create color product irb_p = out_root.with_name(out_root.name + "_IRB.NOPROJ.cub") with isis.fromlist.temp([ir_p, center_red_p, bg_p]) as f: isis.cubeit(fromlist=f, to=irb_p, proplab=center_red_p) if plot: # Make plot of before and after flat.tab results dejit_flat = list( filter(lambda x: x.match("*RED5-RED4*"), red_flat_files) )[0] plot_flats(flat_path, dejit_flat) if not keep: flat_path.unlink() red_flat_files.unlink() to_del.unlink() logger.info(f"HiJACK done") return
def mosaic_dejittered(cubes: list, out_p: os.PathLike, prodid: str): if len(cubes) != 2: raise ValueError("Input cube list did not have 2 cubes.") hnp.handmos_side(cubes, cubes[1], out_p, left=True) hnp.fix_labels(cubes, out_p, str(cubes[1]), prodid)
def test_conf_check(self): self.assertIsNone(hnp.conf_check(conf["HiNoProj"]))
def test_init(self, mock_getkey): c = hnp.Cube("dummy/PSP_010502_2090_RED5_0") self.assertIsNone(c.next_path)
def test_HiNoProj( self, mock_getkey, m_spice, m_sfit, m_noproj, m_hijitreg, m_copy, m_handside, m_edit, ): c2 = Path("dummy/PSP_010502_2090_RED2.HiStitch.balance.cub") c3 = Path("dummy/PSP_010502_2090_RED3.HiStitch.balance.cub") c4 = Path("dummy/PSP_010502_2090_RED4.HiStitch.balance.cub") c5 = Path("dummy/PSP_010502_2090_RED5.HiStitch.balance.cub") c6 = Path("dummy/PSP_010502_2090_RED6.HiStitch.balance.cub") cubes = (c2, c3, c4, c5, c6) conf = {"HiNoProj": {"Shape": "SYSTEM"}} hnp.HiNoProj(cubes, conf, "dummy/mosaic.cub", 4, keep=True) self.assertEqual(len(cubes), m_spice.call_count) self.assertEqual(len(cubes), m_sfit.call_count) self.assertEqual(len(cubes), m_noproj.call_count) self.assertEqual(len(cubes) - 1, m_hijitreg.call_count) self.assertEqual(len(cubes) + 1, m_copy.call_count) self.assertEqual(2, m_handside.call_count) self.assertEqual( m_edit.call_args_list, [ call( Path("dummy/mosaic.cub"), grpname="Instrument", keyword="ImageJitterCorrected", option="addkey", value=0, ), call( Path("dummy/mosaic.cub"), grpname="Archive", keyword="ProductId", option="modkey", value="PSP_010502_2090_RED", ), call( Path("dummy/mosaic.cub"), grpname="Instrument", keyword="MatchedCube", option="modkey", value="PSP_010502_2090_RED4", ), call( Path("dummy/mosaic.cub"), grpname="Archive", keyword="SourceProductId", option="ADDKEY", value="(PSP_010502_2090_RED4_0, " "PSP_010502_2090_RED4_1, PSP_010502_2090_RED4_0, " "PSP_010502_2090_RED4_1, PSP_010502_2090_RED4_0, " "PSP_010502_2090_RED4_1, PSP_010502_2090_RED4_0, " "PSP_010502_2090_RED4_1, PSP_010502_2090_RED4_0, " "PSP_010502_2090_RED4_1)", ), ], )