Coverage for /home/runner/work/torchcvnn/torchcvnn/src/torchcvnn/datasets/alos2/trailer_file.py: 0%
23 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-13 08:53 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-13 08:53 +0000
1# MIT License
3# Copyright (c) 2024 Jeremy Fix
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
23# Standard imports
24import struct
26# External imports
27import numpy as np
29# Local imports
30from . import parse_utils
32descriptor_format = [
33 ("file_id", 48, 16, "A", None),
34 ("number_low_resolution_image_records", 490, 6, "I", None),
35 ("number_of_pixels", 504, 6, "I", None),
36 ("number_of_lines", 510, 6, "I", None),
37 ("image_record_length", 496, 8, "I", None),
38]
39descriptor_record_length = 720
42def parse_image_data(fh, number_pixels, number_lines):
43 data_bytes = fh.read(number_pixels * number_lines * 2)
44 data_array = struct.unpack(">" + ("H" * number_pixels * number_lines), data_bytes)
45 data = np.array(data_array, dtype=np.ushort).reshape(number_pixels, number_lines)
46 return data
49class TrailerFile:
50 r"""
51 Processing of the SAR trailer file.
52 """
54 def __init__(self, filepath):
55 self.descriptor_records = {}
56 with open(filepath, "rb") as fh:
57 fh_offset = 0
58 fh_offset = parse_utils.parse_from_format(
59 fh,
60 self.descriptor_records,
61 descriptor_format,
62 1,
63 descriptor_record_length,
64 fh_offset,
65 )
67 # Move the reading head to the beginning of the buffer
68 fh.seek(fh_offset)
69 num_pixels = self.descriptor_records["number_of_pixels"]
70 num_lines = self.descriptor_records["number_of_lines"]
71 self.image_data = parse_image_data(fh, num_pixels, num_lines)
73 def __repr__(self):
74 descriptor_txt = parse_utils.format_dictionary(self.descriptor_records, 1)
75 return f"""
76Descriptor :
77{descriptor_txt}
78 """