planetarypy / planetaryimage

Python PDS and Isis Cube file parser.
BSD 3-Clause "New" or "Revised" License
40 stars 20 forks source link

Can't save correctly with new data type #71

Open percurnicus opened 7 years ago

percurnicus commented 7 years ago

Im running into a problem with saving an image after I convert the data to a different dtype. I need to convert because I have to make changes to the data and I was having problems making the needed changes with uint8. I also had to make changes to the label and need those changes seen in the new file. Here is how to recreate the problem:

>>> from planetaryimage import PDS3Image
>>> import numpy as np
>>> old_im = PDS3Image.open('tests/mission_data/0615ML0026220010301833C00_DXXX.IMG')
>>> old_im.data
array([[[ 8,  5,  8, ...,  5,  7,  4],
        [ 2,  2,  5, ...,  4,  4,  4],
        [72, 55, 84, ..., 30, 41, 26],
        ...,
        [54, 34, 51, ..., 82, 62, 42],
        [71, 51, 71, ..., 79, 75, 59],
        [52, 34, 50, ..., 44, 64, 45]]], dtype=uint8)
>>> old_im.data = old_im.data.astype(np.int64)
>>> new_im = 'new_im.IMG'
>>> old_im.save(new_im)
>>> PDS3Image.open(new_im).data
array([[[ 576460752303423488,  360287970189639680,  576460752303423488,
         ...,  360287970189639680,  504403158265495552,  288230376151711744],
        [ 144115188075855872,  144115188075855872,  360287970189639680,
         ...,  288230376151711744,  288230376151711744,  288230376151711744],
        [5188146770730811392, 3963167672086036480, 6052837899185946624,
         ..., 2161727821137838080, 2954361355555045376, 1873497444986126336],
        ...,
        [3891110078048108544, 2449958197289549824, 3674937295934324736,
         ..., 5908722711110090752, 4467570830351532032, 3026418949592973312],
        [5116089176692883456, 3674937295934324736, 5116089176692883456,
         ..., 5692549928996306944, 5404319552844595200, 4251398048237748224],
        [3746994889972252672, 2449958197289549824, 3602879701896396800,
         ..., 3170534137668829184, 4611686018427387904, 3242591731706757120]]])

I think this is part of a larger bug because I don't get the same array when I just save it as is:

>>> from planetaryimage import PDS3Image
>>> old_im = PDS3Image.open('tests/mission_data/0615ML0026220010301833C00_DXXX.IMG')
>>> old_im.data
array([[[ 8,  5,  8, ...,  5,  7,  4],
        [ 2,  2,  5, ...,  4,  4,  4],
        [72, 55, 84, ..., 30, 41, 26],
        ...,
        [54, 34, 51, ..., 82, 62, 42],
        [71, 51, 71, ..., 79, 75, 59],
        [52, 34, 50, ..., 44, 64, 45]]], dtype=uint8)
>>> new_im = 'new_im.IMG'
>>> old_im.save(new_im)
>>> PDS3Image.open(new_im).data
array([[[ 32,  32,  32, ...,   7,   4,   4],
        [  5,   2,   5, ...,   2,   1,   4],
        [  5,   2,   2, ...,  44,  57,  40],
        ...,
        [118,  95, 107, ...,  56,  81,  54],
        [ 80,  55,  83, ...,  87, 105,  84],
        [104,  89,  95, ...,  71,  91,  45]]], dtype=uint8)

Looking at the test for save, it doesn't seem like the data is tested to be correct, only the surrounding parameters. I can write a test to expose this bug but I wanted to check if I was using save correctly before I did so.