def _save(self, file_to_write, overwrite): 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 diff = 0 if len(pvl.dumps(self.label, cls=encoder)) != label_sz: diff = 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._sample_type[1]) stream.close()
def pvl_flavor( text, dialect, decenc: dict, filename, verbose=False ) -> tuple((bool, bool)): """Returns a two-tuple of booleans which indicate whether the *text* could be loaded and then encoded. The first boolean in the two-tuple indicates whether the *text* could be loaded with the given parser, grammar, and decoder. The second indicates whether the loaded PVL object could be encoded with the given encoder, grammar, and decoder. If the first element is False, the second will be None. """ loads = None encodes = None try: some_pvl = pvl.loads(text, **decenc) loads = True try: pvl.dumps(some_pvl, **decenc) encodes = True except (LexerError, ParseError, ValueError) as err: logging.error(f"{dialect} encode error {filename} {err}") encodes = False except (LexerError, ParseError) as err: logging.error(f"{dialect} load error {filename} {err}") loads = False return (loads, encodes)
def _save(self, file_to_write, overwrite): """Save PDS3Image object as PDS3 file. Parameters ---------- Overwrite: Use this keyword to save image with same filename. Examples -------- >>> from planetaryimage import PDS3Image >>> image = PDS3Image.open('tests/mission_data/2p129641989eth0361p2600r8m1.img') >>> image.save('temp.IMG') >>> 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']['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()
def _save(self, file_to_write, overwrite): """Save PDS3Image object as PDS3 file. Parameters ---------- 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']['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()
def test_special_strings(): module = pvl.PVLModule([ ('single_quote', "'"), ('double_quote', '"'), ('mixed_quotes', '"\''), ]) assert module == pvl.loads(pvl.dumps(module)) encoder = pvl.encoder.IsisCubeLabelEncoder assert module == pvl.loads(pvl.dumps(module, cls=encoder)) encoder = pvl.encoder.PDSLabelEncoder assert module == pvl.loads(pvl.dumps(module, cls=encoder))
def test_special_values(): module = pvl.PVLModule([ ('bool_true', True), ('bool_false', False), ('null', None), ]) assert module == pvl.loads(pvl.dumps(module)) encoder = pvl.encoder.IsisCubeLabelEncoder assert module == pvl.loads(pvl.dumps(module, cls=encoder)) encoder = pvl.encoder.PDSLabelEncoder assert module == pvl.loads(pvl.dumps(module, cls=encoder))
def _save(self, file_to_write, overwrite): """ Redefine _save() used in PDS3Image() so it does not overwrite the comment lines we add in with PDS3LabelEncoder() """ # 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) # with open(file_to_write, 'a+b') as stream: encoder = PDS3LabelEncoder 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']['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()
def _update_label(self, label, array): """Update PDS3 label for NumPy Array. It is called by '_create_label' to update label values such as, - ^IMAGE, RECORD_BYTES - STANDARD_DEVIATION - MAXIMUM, MINIMUM - MEDIAN, MEAN ** Currently lines are commented out because these values are not desired by users. Returns ------- Update label module for the NumPy array. Usage: self.label = self._update_label(label, array) """ maximum = float(np.max(array)) mean = float(np.mean(array)) median = float(np.median(array)) minimum = float(np.min(array)) stdev = float(np.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 return label
def Map2pvl(self): self.mapDICT['End_Group'] = 'Mapping' mappvl = pvl.dumps(self.mapDICT) self.mappvl = mappvl return mappvl
def Map2File(self, filename): self.mapDICT['End_Group'] = 'Mapping' tempPVL = pvl.dumps(self.mapDICT) file = open(filename, 'w') file.write(tempPVL) file.close()
def test_special_strings(): module = pvl.PVLModule([ ("single_quote", "'"), ("double_quote", '"'), # ('mixed_quotes', '"\''), # see above about escaped quotes. ]) assert module == pvl.loads( pvl.dumps(module, encoder=pvl.encoder.PVLEncoder()))
def create_pvl_header(self, version, headerstartbyte, networkid, targetname, description, username, buffer_header_size, points_bytes, creation_date, modified_date): """ Create the PVL header object Parameters ---------- version : int The current ISIS version to write, defaults to 2 headerstartbyte : int The seek offset that the protocol buffer header starts at networkid : str The name of the network targetname : str The name of the target, e.g. Moon description : str A description for the network. username : str The name of the user / application that created the control network buffer_header_size : int Total size of the header in bytes points_bytes : int The total number of bytes all points require Returns ------- : object An ISIS compliant PVL header object """ encoder = pvl.encoder.IsisCubeLabelEncoder header_bytes = buffer_header_size points_start_byte = HEADERSTARTBYTE + buffer_header_size header = pvl.PVLModule([( 'ProtoBuffer', ({ 'Core': { 'HeaderStartByte': headerstartbyte, 'HeaderBytes': header_bytes, 'PointsStartByte': points_start_byte, 'PointsBytes': points_bytes }, 'ControlNetworkInfo': pvl.PVLGroup([('NetworkId', networkid), ('TargetName', targetname), ('UserName', username), ('Created', creation_date), ('LastModified', modified_date), ('Description', description), ('NumberOfPoints', self.npoints), ('NumberOfMeasures', self.nmeasures), ('Version', version)]) }), )]) return pvl.dumps(header, cls=encoder)
def test_quoated_strings(): module = pvl.PVLModule([ ('int_like', "123"), ('float_like', '.2'), ('date', '1987-02-25'), ('time', '03:04:05'), ('datetime', '1987-02-25T03:04:05'), ('keyword', 'END'), ('restricted_chars', '&<>\'{},[]=!#()%";|'), ('restricted_seq', '/**/'), ]) assert module == pvl.loads(pvl.dumps(module)) encoder = pvl.encoder.IsisCubeLabelEncoder assert module == pvl.loads(pvl.dumps(module, cls=encoder)) encoder = pvl.encoder.PDSLabelEncoder assert module == pvl.loads(pvl.dumps(module, cls=encoder))
def Map2File(self, filename): """ Parameters ---------- filename """ self.mapDICT['End_Group'] = 'Mapping' tempPVL = pvl.dumps(self.mapDICT) with open(filename, 'wb') as f: f.write(tempPVL)
def test_dumps_PDS(self): s = '''A = b\r GROUP = staygroup\r C = d\r END_GROUP = staygroup\r OBJECT = obj\r D = e\r GROUP = f\r G = h\r END_GROUP = f\r END_OBJECT = obj\r END\r\n''' self.assertEqual(s, pvl.dumps(self.module))
def Map2pvl(self): """ Returns ---------- str mappvl """ self.mapDICT['End_Group'] = 'Mapping' mappvl = pvl.dumps(self.mapDICT) self.mappvl = mappvl return mappvl
def Map2pvl(self): """ Returns ---------- str mappvl """ self.mapDICT['End_Group'] = 'Mapping' mappvl = pvl.dumps(self.mapDICT, encoder=pvl.encoder.ISISEncoder()) self.mappvl = mappvl return mappvl
def test_dumps_PVL(self): s = '''a = b; BEGIN_GROUP = staygroup; c = d; END_GROUP = staygroup; BEGIN_GROUP = obj; d = e; BEGIN_GROUP = f; g = h; END_GROUP = f; END_GROUP = obj; END;''' self.assertEqual( s, pvl.dumps(self.module, encoder=pvl.encoder.PVLEncoder()))
def test_quoated_strings(): module = pvl.PVLModule([ ("int_like", "123"), ("float_like", ".2"), ("date", "1987-02-25"), ("time", "03:04:05"), ("datetime", "1987-02-25T03:04:05"), ("keyword", "END"), # both kinds of quotes aren't allowed: # ('restricted_chars', '&<>\'{},[]=!#()%";|'), ("restricted_chars", '&<>{},[]=!#()%";|'), ("restricted_seq", "/**/"), ]) assert module == pvl.loads( pvl.dumps(module, encoder=pvl.encoder.PVLEncoder()))
def test_special_values(): module = pvl.PVLModule([ ("bool_true", True), ("bool_false", False), ("null", None), ]) assert module == pvl.loads( pvl.dumps(module, encoder=pvl.encoder.PVLEncoder())) # IsisCubeLabelEncoder class is deprecated # encoder = pvl.encoder.IsisCubeLabelEncoder # assert module == pvl.loads(pvl.dumps(module, cls=encoder)) # This is now the default: # encoder = pvl.encoder.PDSLabelEncoder # But to compare with PVL written by the PDSLabelEncoder, the # labels must be uppercase: pds_module = pvl.PVLModule([ ("BOOL_TRUE", True), ("BOOL_FALSE", False), ("NULL", None), ]) assert pds_module == pvl.loads(pvl.dumps(module))
def test_quoated_strings(): module = pvl.PVLModule([ ('int_like', "123"), ('float_like', '.2'), ('date', '1987-02-25'), ('time', '03:04:05'), ('datetime', '1987-02-25T03:04:05'), ('keyword', 'END'), # both kinds of quotes aren't allowed: # ('restricted_chars', '&<>\'{},[]=!#()%";|'), ('restricted_chars', '&<>{},[]=!#()%";|'), ('restricted_seq', '/**/'), ]) assert module == pvl.loads( pvl.dumps(module, encoder=pvl.encoder.PVLEncoder()))
def test_dumps_ODL(self): s = '''A = b\r GROUP = staygroup\r C = d\r END_GROUP = staygroup\r GROUP = obj\r D = e\r GROUP = f\r G = h\r END_GROUP = f\r END_GROUP = obj\r END\r\n''' self.assertEqual( s, pvl.dumps(self.module, encoder=pvl.encoder.ODLEncoder()))
def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() if isinstance(obj, np.int64): return int(obj) if isinstance(obj, datetime.datetime): return obj.__str__() if isinstance(obj, bytes): return obj.decode("utf-8") if isinstance(obj, pvl.PVLModule): return pvl.dumps(obj) if isinstance(obj, set): return list(obj) if isinstance(obj, np.nan): return None return json.JSONEncoder.default(self, obj)
def create_camera(obj, url='http://pfeffer.wr.usgs.gov/v1/pds/', plugin_name='USGS_ASTRO_LINE_SCANNER_PLUGIN', model_name='USGS_ASTRO_LINE_SCANNER_SENSOR_MODEL'): data = data_from_cube(obj.metadata) data_serialized = {'label': pvl.dumps(data).decode()} r = requests.post(url, json=data_serialized).json() r['IKCODE'] = -1 # Get the ISD back and instantiate a local ISD for the image isd = csmapi.Isd.loads(r) # Create the plugin and camera as usual plugin = csmapi.Plugin.findPlugin(plugin_name) if plugin.canModelBeConstructedFromISD(isd, model_name): return plugin.constructModelFromISD(isd, model_name)
def _update_label(self, label, array): """Update PDS3 label for NumPy Array. It is called by '_create_label' to update label values such as, - ^IMAGE, RECORD_BYTES - STANDARD_DEVIATION - MAXIMUM, MINIMUM - MEDIAN, MEAN 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
def test_default_encoder(): for filename in PDS_COMPLIANT: label = pvl.load(filename) assert label == pvl.loads(pvl.dumps(label))
def test_pds_encoder(): for filename in PDS_COMPLIANT: label = pvl.load(filename) encoder = pvl.encoder.PDSLabelEncoder() assert label == pvl.loads(pvl.dumps(label, encoder=encoder))
async def _set_label(self, key: str, image: PDSImage) -> None: label = await image.label await super().set(f'{key}:label', pvl.dumps(label))
def test_unkown_value(): class UnknownType(object): pass with pytest.raises(TypeError): print(pvl.dumps({"foo": UnknownType()}))
def test_unkown_value(): class UnknownType(object): pass with pytest.raises(TypeError): pvl.dumps({'foo': UnknownType()})
async def download_label(request): label = await image.label body = pvl.dumps(label) return aiohttp.web.Response(body=body)
def test_pds_encoder(): for filename in PDS_LABELS: label = pvl.load(filename) encoder = pvl.encoder.PDSLabelEncoder assert label == pvl.loads(pvl.dumps(label, cls=encoder))
def test_default_encoder(): for filename in PDS_LABELS: label = pvl.load(filename) assert label == pvl.loads(pvl.dumps(label))
def create_pvl_header(self, cnet, version, headerstartbyte, networkid, targetname, description, username, buffer_header_size, points_bytes): """ Create the PVL header object Parameters ---------- cnet : object A control net object version : int The current ISIS version to write, defaults to 2 headerstartbyte : int The seek offset that the protocol buffer header starts at networkid : str The name of the network targetname : str The name of the target, e.g. Moon description : str A description for the network. username : str The name of the user / application that created the control network buffer_header_size : int Total size of the header in bytes points_bytes : int The total number of bytes all points require Returns ------- : object An ISIS compliant PVL header object """ encoder = pvl.encoder.IsisCubeLabelEncoder header_bytes = buffer_header_size points_start_byte = HEADERSTARTBYTE + buffer_header_size header = pvl.PVLModule([ ('ProtoBuffer', ({'Core':{'HeaderStartByte': headerstartbyte, 'HeaderBytes': header_bytes, 'PointsStartByte': points_start_byte, 'PointsBytes': points_bytes}, 'ControlNetworkInfo': pvl.PVLGroup([ ('NetworkId', networkid), ('TargetName', targetname), ('UserName', username), ('Created', cnet.creationdate), ('LastModified', cnet.modifieddate), ('Description', description), ('NumberOfPoints', cnet.n), ('NumberOfMeasures', cnet.m), ('Version', version) ]) }), ) ]) return pvl.dumps(header, cls=encoder)