Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pixel_type more explicit and extended #20

Merged
merged 3 commits into from
Jun 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions planetaryimage/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def __init__(self, stream, filename=None, memory_layout='DISK'):
#: A numpy array representing the image
self.data = self._parse_data(stream)

def __repr__(self):
return self.filename

@staticmethod
def get_nested_dict(item, key_list):
"""Get value from nested dictionary using a key list. """
Expand Down Expand Up @@ -185,10 +188,8 @@ def format(self):

@property
def byte_order(self):
"""Byte order in numpy format('>', '<', '=').
Defaults to msb.
"""
return '>'
"""Byte order in numpy format('>', '<', '=')."""
raise NotImplementedError

@property
def data_filename(self):
Expand Down
81 changes: 48 additions & 33 deletions planetaryimage/pds3image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ class PDS3Image(PlanetaryImage):

""" A PDS3 image reader. """

PIXEL_TYPES = {
'UnsignedByte': numpy.dtype('uint8'),
'SignedByte': numpy.dtype('int8'),
'UnsignedWord': numpy.dtype('uint16'),
'SignedWord': numpy.dtype('int16'),
'UnsignedInteger': numpy.dtype('uint32'),
'SignedInteger': numpy.dtype('int32'),
}
LSB_INTEGER_TYPES = ['LSB_INTEGER', 'PC_INTEGER', 'VAX_INTEGER']
LSB_UNSIGNED_INTEGER_TYPES = ['LSB_UNSIGNED_INTEGER', 'PC_UNSIGNED_INTEGER',
'VAX_UNSIGNED_INTEGER']
MSB_INTEGER_TYPES = ['MSB_INTEGER', 'MAC_INTEGER', 'SUN_INTEGER', 'INTEGER']
MSB_UNSIGNED_INTEGER_TYPES = ['MSB_UNSIGNED_INTEGER', 'UNSIGNED_INTEGER',
'MAC_UNSIGNED_INTEGER', 'SUN_UNSIGNED_INTEGER'
]
IEEE_REAL_TYPES = ['IEEE_REAL', 'MAC_REAL', 'SUN_REAL', 'REAL', 'FLOAT']
PC_REAL_TYPES = ['PC_REAL']

LABEL_MAPPING = {
'bands': ['IMAGE', 'BANDS'],
Expand Down Expand Up @@ -89,40 +90,54 @@ def format(self):

@property
def byte_order(self):
sample_type = self.get_nested_dict(
self.label, self.LABEL_MAPPING['sample_type'])
if "LSB" in sample_type:
return '<'
else:
return '>'
return self.data.dtype.byteorder

@property
def start_byte(self):
return self.parse_pointer(self.label['^IMAGE'], self.label['RECORD_BYTES'])[0]
return self.parse_pointer(
self.label['^IMAGE'], self.label['RECORD_BYTES']
)[0]

@property
def data_filename(self):
return self.parse_pointer(self.label['^IMAGE'], 0)[1]

@property
def dtype(self):
"""
Pixel data type overrides the implementation in PlanetaryImage
because PDS3 SAMPLE_TYPE expresses BOTH byte ordering and type.
Unlike ISIS CubeFile labels, which apparently express these as
separate label values.
"""
return self.pixel_type

@property
def pixel_type(self):
sample_type = self.get_nested_dict(
self.label, self.LABEL_MAPPING['sample_type'])
bits = self.get_nested_dict(self.label, self.LABEL_MAPPING['bits'])

if 'UNSIGNED' in sample_type:
if bits == 8:
return self.PIXEL_TYPES['UnsignedByte']
if bits == 16:
return self.PIXEL_TYPES['UnsignedWord']
if bits == 32:
return self.PIXEL_TYPES['UnsignedInteger']
else:
if bits == 8:
return self.PIXEL_TYPES['SignedByte']
if bits == 16:
return self.PIXEL_TYPES['SignedWord']
if bits == 32:
return self.PIXEL_TYPES['SignedInteger']
sample_type = self.label['IMAGE']['SAMPLE_TYPE']
bits = self.label['IMAGE']['SAMPLE_BITS']
sample_bytes = str(int(bits / 8)) # get bytes to match NumPy dtype expressions

if sample_type in self.LSB_INTEGER_TYPES:
return numpy.dtype('<i' + sample_bytes)

if sample_type in self.LSB_UNSIGNED_INTEGER_TYPES:
return numpy.dtype('<u' + sample_bytes)

if sample_type in self.MSB_INTEGER_TYPES:
return numpy.dtype('>i' + sample_bytes)

if sample_type in self.MSB_UNSIGNED_INTEGER_TYPES:
return numpy.dtype('>u' + sample_bytes)

# I am guessing byte order by process of elimination
if sample_type in self.IEEE_REAL_TYPES:
return numpy.dtype('>f' + sample_bytes)

# The byte order used here worked properly for the HiRISE product
# DTEEC_008520_2085_009232_2085_A01.IMG
# I'd like a little better understand of this.
if sample_type in self.PC_REAL_TYPES:
return numpy.dtype('<f' + sample_bytes)

raise TypeError
2 changes: 1 addition & 1 deletion tests/test_pds3file.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_pds3_1band_labels():
assert image.lines == 10
assert image.samples == 10
assert image.format == 'BAND_SEQUENTIAL'
assert image.pixel_type == numpy.dtype('int16')
assert image.pixel_type == numpy.dtype('>i2')
assert image.dtype == numpy.dtype('>i2')
assert image.start_byte == 640
assert image.shape == (1, 10, 10)
Expand Down