Example #1
0
    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",
                ),
            ],
        )
Example #2
0
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
Example #3
0
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)