Пример #1
0
    def setUp(self):
        cube = imgs[0].with_suffix(".TestHiCal_TestNeedCNstatsfile.cub")
        self.statsfile = cube.with_suffix(".stats")
        self.output = Path("dummy.tab")

        isis.hi2isis(imgs[0], to=cube)
        isis.cubenorm(cube, stats=self.statsfile)
        cube.unlink()
Пример #2
0
def EDR_Stats(
    img: os.PathLike,
    out_path: os.PathLike,
    gainsinfo: dict,
    histmin=0.01,
    histmax=99.99,
    keep=False,
) -> dict:
    logger.info(f"EDR_Stats start: {img}")
    try:
        logger.info("The LUT for this file is: " + str(check_lut(img)))
    except KeyError as err:
        logger.error("The LUT header area is either corrupted or has a gap.")
        raise err

    # Convert to .cub
    isis.hi2isis(img, to=out_path)

    histat_complete = isis.histat(
        out_path,
        useoffsets=True,
        leftimage=0,
        rightimage=1,
        leftcalbuffer=3,
        rightcalbuffer=1,
        leftcaldark=3,
        rightcaldark=1,
        leftbuffer=3,
        rightbuffer=1,
        leftdark=3,
        rightdark=1,
    )
    histats = parse_histat(histat_complete.stdout)

    # Get some info from the new cube:
    histats["PRODUCT_ID"] = isis.getkey_k(out_path, "Archive", "ProductId")
    histats["IMAGE_LINES"] = int(
        isis.getkey_k(out_path, "Dimensions", "Lines")
    )
    histats["LINE_SAMPLES"] = int(
        isis.getkey_k(out_path, "Dimensions", "Samples")
    )
    histats["BINNING"] = int(isis.getkey_k(out_path, "Instrument", "Summing"))

    histats["STD_DN_LEVELS"] = get_dncnt(out_path, histmin, histmax, keep=keep)
    histats["IMAGE_SIGNAL_TO_NOISE_RATIO"] = calc_snr(
        out_path, gainsinfo, histats
    )
    histats["GAP_PIXELS_PERCENT"] = (
        histats["GAP_PIXELS"]
        / (int(histats["IMAGE_LINES"]) * int(histats["LINE_SAMPLES"]))
    ) * 100.0

    tdi_bin_check(out_path, histats)
    lut_check(out_path, histats)

    logger.info(f"EDR_Stats done: {out_path}")
    return histats
Пример #3
0
    def test_stats_k_file(self):
        cub = Path('test_stats_k.cub')
        isis.hi2isis(HiRISE_img, to=cub)

        d = isis.stats_k(cub)
        self.assertEqual(d['TotalPixels'], '2048000')

        cub.unlink()
        with contextlib.suppress(FileNotFoundError):
            Path('print.prt').unlink()
Пример #4
0
    def setUp(self):
        named_cube = img.with_suffix(".cub")
        isis.hi2isis(img, to=named_cube)
        noname_cube = named_cube.with_name("noname.cub")
        shutil.copyfile(named_cube, noname_cube)

        self.paths = (
            (named_cube, "PSP_010502_2090", "PSP_010502_2090_RED5_0"),
            (noname_cube, "PSP_010502_2090", "PSP_010502_2090_RED5_0"),
            (
                Path("PSP_010502_2090_RED5.fake"),
                "PSP_010502_2090",
                "PSP_010502_2090_RED5",
            ),
        )
Пример #5
0
def hi2isis_k(*args, **kwargs):
    '''Creates a default name for the to= cube.

       If the FROM file has the name ``foo.img``, then the output will
       be ``foo.cub``.
    '''
    from_path = Path()
    if len(args) > 0 and not str(args[0]).endswith('__'):
        from_path = Path(args[0])
    else:
        for (k, v) in kwargs.items():
            if 'from_' == k:
                from_path = Path(v)
    if 'to_' not in kwargs:
        kwargs.setdefault('to', from_path.with_suffix('.cub'))
    return (isis.hi2isis(*args, **kwargs))
Пример #6
0
def hi2isis_k(*args, **kwargs):
    """Creates a default name for the to= cube.

    If the FROM file has the name ``foo.img``, then the output will
    be ``foo.cub``.
    """
    if not any(k.startswith('t') for k in kwargs.keys()):
        from_path = Path()
        if len(args) > 0 and not str(args[0]).endswith('__'):
            from_path = Path(args[0])
        else:
            for (k, v) in kwargs.items():
                if k.startswith('f'):
                    from_path = Path(v)
                    break

        kwargs.setdefault('to', from_path.with_suffix('.cub'))

    return (isis.hi2isis(*args, **kwargs))
Пример #7
0
 def setUp(self):
     self.cube = Path('test_hist.cub')
     isis.hi2isis(HiRISE_img, to=self.cube)
Пример #8
0
 def setUp(self):
     self.cub = Path('test_getkey_k.cub')
     isis.hi2isis(HiRISE_img, to=self.cub)
Пример #9
0
def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "-o", "--output", required=False, default=".mdr.iof.cub"
    )
    parser.add_argument("-e", "--edr", required=True)
    parser.add_argument(
        "-c",
        "--conf",
        required=False,
        type=argparse.FileType('r'),
        default=pkg_resources.resource_stream(
            __name__,
            'data/hical.pipelines.conf'
        ),
    )
    parser.add_argument("mdr", metavar="MDR_file")
    parser.add_argument(
        "-l",
        "--log",
        required=False,
        default="WARNING",
        help="The log level to show for this program, can "
        "be a named log level or a numerical level.",
    )
    parser.add_argument(
        "-k",
        "--keep",
        required=False,
        default=False,
        action="store_true",
        help="Normally, the program will clean up any "
        "intermediary files, but if this option is given, it "
        "won't.",
    )

    args = parser.parse_args()

    util.set_logger(args.verbose, args.logfile, args.log)

    edr_path = Path(args.edr)
    mdr_path = Path(args.mdr)

    to_del = isis.PathSet()

    h2i_path = to_del.add(edr_path.with_suffix(".hi2isis.cub"))

    out_path = util.path_w_suffix(args.output, edr_path)

    # The first thing Alan's program did was to crop the image down to only the
    # 'imaging' parts.  We're not doing that so the resultant file has a
    # geometry similar to what comes out of ISIS hical.

    # Convert the EDR to a cube file
    isis.hi2isis(edr_path, to=h2i_path)

    # Convert Alan's MDR to a cube file
    mdr_cub_path = to_del.add(mdr_path.with_suffix(".alan.cub"))
    logger.info(f"Running gdal_translate {mdr_path} -of ISIS3 {mdr_cub_path}")
    gdal.Translate(str(mdr_cub_path), str(mdr_path), format="ISIS3")

    h2i_s = int(isis.getkey_k(h2i_path, "Dimensions", "Samples"))
    h2i_l = int(isis.getkey_k(h2i_path, "Dimensions", "Lines"))
    mdr_s = int(isis.getkey_k(mdr_cub_path, "Dimensions", "Samples"))
    mdr_l = int(isis.getkey_k(mdr_cub_path, "Dimensions", "Lines"))

    if h2i_s != mdr_s:
        label = pvl.load(str(h2i_path))
        hirise_cal_info = get_one(
            label, "Table", "HiRISE Calibration Ancillary"
        )

        buffer_pixels = get_one(hirise_cal_info, "Field", "BufferPixels")[
            "Size"
        ]
        dark_pixels = get_one(hirise_cal_info, "Field", "DarkPixels")["Size"]
        rev_mask_tdi_lines = hirise_cal_info["Records"]

        if h2i_s + buffer_pixels + dark_pixels == mdr_s:
            logger.info(
                f"The file {mdr_cub_path} has "
                f"{buffer_pixels + dark_pixels} more sample pixels "
                f"than {h2i_path}, assuming those are dark and "
                "buffer pixels and will crop accordingly."
            )
            if h2i_l + rev_mask_tdi_lines != mdr_l:
                logger.critical(
                    'Even assuming this is a "full" channel '
                    "image, this has the wrong number of lines. "
                    f"{mdr_cub_path} should have "
                    f"{h2i_l + rev_mask_tdi_lines}, but "
                    f"has {mdr_l} lines. Exiting"
                )
                sys.exit()
            else:
                crop_path = to_del.add(mdr_cub_path.with_suffix(".crop.cub"))
                # We want to start with the next pixel (+1) after the cal
                # pixels.
                isis.crop(
                    mdr_cub_path,
                    to=crop_path,
                    sample=buffer_pixels + 1,
                    nsamples=h2i_s,
                    line=rev_mask_tdi_lines + 1,
                )
                mdr_cub_path = crop_path
                mdr_l = int(isis.getkey_k(mdr_cub_path, "Dimensions", "Lines"))

        else:
            logger.critical(
                f"The number of samples in {h2i_path} ({h2i_s}) "
                f"and {mdr_cub_path} ({mdr_s}) are different. "
                "Exiting."
            )
            sys.exit()

    if h2i_l != mdr_l:
        logger.critical(
            f"The number of lines in {h2i_path} ({h2i_l}) "
            f"and {mdr_cub_path} ({mdr_l}) are different. "
            "Exiting."
        )
        sys.exit()

    # Convert the EDR to the right bit type for post-HiCal Pipeline:
    h2i_16b_p = to_del.add(h2i_path.with_suffix(".16bit.cub"))
    isis.bit2bit(
        h2i_path,
        to=h2i_16b_p,
        bit="16bit",
        clip="minmax",
        minval=0,
        maxval=1.5,
    )
    shutil.copyfile(h2i_16b_p, out_path)

    # If it is a channel 1 file, Alan mirrored it so that he could process
    # the two channels in an identical way (which we also took advantage
    # of above if the buffer and dark pixels were included), so we need to
    # mirror it back.
    cid = hirise.get_ChannelID_fromfile(h2i_16b_p)
    if cid.channel == "1":
        mirror_path = to_del.add(mdr_cub_path.with_suffix(".mirror.cub"))
        isis.mirror(mdr_cub_path, to=mirror_path)
        mdr_cub_path = mirror_path

    # Is the MDR in DN or I/F?
    maximum_pxl = float(
        pvl.loads(isis.stats(mdr_cub_path).stdout)["Results"]["Maximum"]
    )
    if maximum_pxl < 1.5:
        logger.info("MDR is already in I/F units.")
        mdr_16b_p = to_del.add(mdr_cub_path.with_suffix(".16bit.cub"))
        isis.bit2bit(
            mdr_cub_path,
            to=mdr_16b_p,
            bit="16bit",
            clip="minmax",
            minval=0,
            maxval=1.5,
        )
        isis.handmos(mdr_16b_p, mosaic=out_path)
    else:
        logger.info("MDR is in DN units and will be converted to I/F.")

        fpa_t = statistics.mean(
            [
                float(
                    isis.getkey_k(
                        h2i_16b_p, "Instrument", "FpaPositiveYTemperature"
                    )
                ),
                float(
                    isis.getkey_k(
                        h2i_16b_p, "Instrument", "FpaNegativeYTemperature"
                    )
                ),
            ]
        )
        print(f"fpa_t {fpa_t}")

        conf = pvl.load(args.conf)

        tdg = t_dep_gain(get_one(conf["Hical"], "Profile", cid.ccdname), fpa_t)
        suncorr = solar_correction()
        sclk = isis.getkey_k(
            h2i_16b_p, "Instrument", "SpacecraftClockStartCount"
        )
        target = isis.getkey_k(h2i_16b_p, "Instrument", "TargetName")
        suncorr = solar_correction(sunDistanceAU(sclk, target))
        sed = float(
            isis.getkey_k(h2i_16b_p, "Instrument", "LineExposureDuration")
        )
        zbin = get_one(conf["Hical"], "Profile", "GainUnitConversion")[
            "GainUnitConversionBinFactor"
        ]

        # The 'ziof' name is from the ISIS HiCal/GainUnitConversion.h, it is a
        # divisor in the calibration equation.
        print(f"zbin {zbin}")
        print(f"tdg {tdg}")
        print(f"sed {sed}")
        print(f"suncorr {suncorr}")
        ziof = zbin * tdg * sed * 1e-6 * suncorr
        eqn = f"\(F1 / {ziof})"  # noqa W605

        mdriof_p = to_del.add(mdr_cub_path.with_suffix(".iof.cub"))
        to_s = "{}+SignedWord+{}:{}".format(mdriof_p, 0, 1.5)
        isis.fx(f1=mdr_cub_path, to=to_s, equ=eqn)

        isis.handmos(mdriof_p, mosaic=out_path)

    if not args.keep:
        to_del.unlink()
Пример #10
0
 def setUp(self):
     self.cube = Path('test_Histogram.cub')
     self.histfile = Path('test_Histogram.hist')
     isis.hi2isis(img, to=self.cube)
     isis.hist(self.cube, to=self.histfile)
Пример #11
0
 def setUp(self):
     self.cube = imgs[0].with_suffix(".TestHiCal_TestNeedISISCube.cub")
     isis.hi2isis(imgs[0], to=self.cube)
     self.pid = hirise.get_ChannelID_fromfile(self.cube)
     self.binning = int(isis.getkey_k(self.cube, "Instrument", "Summing"))
Пример #12
0
 def setUp(self):
     self.cube = Path('test_cubenormfile.cub')
     self.statsfile = Path('test_cubenormfile.stats')
     isis.hi2isis(img, to=self.cube)
     isis.cubenorm(self.cube, stats=self.statsfile)
Пример #13
0
 def setUp(self):
     self.hicube = img.with_suffix(".TestNeedHiCube.cub")
     isis.hi2isis(img, to=self.hicube)