Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Returns
-------
Update label module for the NumPy array.
Usage: self.label = self._update_label(label, array)
"""
maximum = float(numpy.max(array))
mean = float(numpy.mean(array))
median = float(numpy.median(array))
minimum = float(numpy.min(array))
stdev = float(numpy.std(array, ddof=1))
encoder = pvl.encoder.PDSLabelEncoder
serial_label = pvl.dumps(label, cls=encoder)
label_sz = len(serial_label)
image_pointer = int(label_sz / label['RECORD_BYTES']) + 1
label['^IMAGE'] = image_pointer + 1
label['LABEL_RECORDS'] = image_pointer
label['IMAGE']['MEAN'] = mean
label['IMAGE']['MAXIMUM'] = maximum
label['IMAGE']['MEDIAN'] = median
label['IMAGE']['MINIMUM'] = minimum
label['IMAGE']['STANDARD_DEVIATION'] = stdev
return label
----------
filename: Set filename for the pds image to be saved.
Overwrite: Use this keyword to save image with same filename.
Usage: image.save('temp.IMG', overwrite=True)
"""
if overwrite:
file_to_write = self.filename
elif os.path.isfile(file_to_write):
msg = 'File ' + file_to_write + ' already exists !\n' + \
'Call save() with "overwrite = True" to overwrite the file.'
raise IOError(msg)
encoder = pvl.encoder.PDSLabelEncoder
serial_label = pvl.dumps(self.label, cls=encoder)
label_sz = len(serial_label)
image_pointer = int(label_sz / self.label['RECORD_BYTES']) + 1
self.label['^IMAGE'] = image_pointer + 1
if self._sample_bytes != self.label['IMAGE']['SAMPLE_BITS'] * 8:
self.label['IMAGE']['SAMPLE_BITS'] = self.data.itemsize * 8
sample_type_to_save = self.DTYPES[self._sample_type[0] + self.dtype.kind]
self.label['IMAGE']['SAMPLE_TYPE'] = sample_type_to_save
if len(self.data.shape) == 3:
self.label['IMAGE']['BANDS'] = self.data.shape[0]
self.label['IMAGE']['LINES'] = self.data.shape[1]
self.label['IMAGE']['LINE_SAMPLES'] = self.data.shape[2]
else:
self.label['IMAGE']['BANDS'] = 1
self.label['IMAGE']['SAMPLE_BITS'] = self.data.itemsize * 8
sample_type_to_save = self.DTYPES[self._sample_type[0] + self.dtype.kind]
self.label['IMAGE']['SAMPLE_TYPE'] = sample_type_to_save
if len(self.data.shape) == 3:
self.label['IMAGE']['BANDS'] = self.data.shape[0]
self.label['IMAGE']['LINES'] = self.data.shape[1]
self.label['IMAGE']['LINE_SAMPLES'] = self.data.shape[2]
else:
self.label['IMAGE']['BANDS'] = 1
self.label['IMAGE']['LINES'] = self.data.shape[0]
self.label['IMAGE']['LINE_SAMPLES'] = self.data.shape[1]
diff = 0
if len(pvl.dumps(self.label, cls=encoder)) != label_sz:
diff = abs(label_sz - len(pvl.dumps(self.label, cls=encoder)))
pvl.dump(self.label, file_to_write, cls=encoder)
offset = image_pointer * self.label['RECORD_BYTES'] - label_sz
stream = open(file_to_write, 'a')
for i in range(0, offset+diff):
stream.write(" ")
if (self._bands > 1 and self._format != 'BAND_SEQUENTIAL'):
raise NotImplementedError
else:
self.data.tofile(stream, format='%' + self.dtype.kind)
stream.close()