| 1 |
"""Sentinel-1 Instrument Source Packets (ISP) decoding example.""" |
| 2 |
|
| 3 |
import enum |
| 4 |
import logging |
| 5 |
import datetime |
| 6 |
|
| 7 |
import bpack |
| 8 |
import bpack.bs |
| 9 |
from bpack import T |
| 10 |
|
| 11 |
BITS = bpack.EBaseUnits.BITS |
| 12 |
BE = bpack.EByteOrder.BE |
| 13 |
|
| 14 |
|
| 15 |
SYNK_MARKER = 0x352EF853 |
| 16 |
FREF = 37.53472224 |
| 17 |
|
| 18 |
|
| 19 |
class SyncMarkerException(RuntimeError): |
| 20 |
pass |
| 21 |
|
| 22 |
|
| 23 |
class EEccNumber(enum.IntEnum): |
| 24 |
# TODO: check "NOT_SET" |
| 25 |
NOT_SET = 0 # CONTINGENCY: RESERVED FOR GROUND TESTING OR MODE UPGRADING |
| 26 |
S1 = 1 |
| 27 |
S2 = 2 |
| 28 |
S3 = 3 |
| 29 |
S4 = 4 |
| 30 |
S5_N = 5 |
| 31 |
S6 = 6 |
| 32 |
IW = 8 |
| 33 |
WM = 9 |
| 34 |
S5_S = 10 |
| 35 |
S1_NO_ICAL = 11 |
| 36 |
S2_NO_ICAL = 12 |
| 37 |
S3_NO_ICAL = 13 |
| 38 |
S4_NO_ICAL = 14 |
| 39 |
RFC = 15 |
| 40 |
TEST = 16 |
| 41 |
EN_S3 = 17 |
| 42 |
AN_S1 = 18 |
| 43 |
AN_S2 = 19 |
| 44 |
AN_S3 = 20 |
| 45 |
AN_S4 = 21 |
| 46 |
AN_S5_N = 22 |
| 47 |
AN_S5_S = 23 |
| 48 |
AN_S6 = 24 |
| 49 |
S5_N_NO_ICAL = 25 |
| 50 |
S5_S_NO_ICAL = 26 |
| 51 |
S6_NO_ICAL = 27 |
| 52 |
EN_S3_NO_ICAL = 31 |
| 53 |
EN = 32 |
| 54 |
AN_S1_NO_ICAL = 33 |
| 55 |
AN_S3_NO_ICAL = 34 |
| 56 |
AN_S6_NO_ICAL = 35 |
| 57 |
NC_S1 = 37 |
| 58 |
NC_S2 = 38 |
| 59 |
NC_S3 = 39 |
| 60 |
NC_S4 = 40 |
| 61 |
NC_S5_N = 41 |
| 62 |
NC_S5_S = 42 |
| 63 |
NC_S6 = 43 |
| 64 |
NC_EW = 44 |
| 65 |
NC_IW = 45 |
| 66 |
NC_WM = 46 |
| 67 |
|
| 68 |
|
| 69 |
class ETestMode(enum.IntEnum): |
| 70 |
DEFAULT = 0 |
| 71 |
CONTINGENCY_RXM_FULLY_OPERATIONAL = 4 # 100 |
| 72 |
CONTINGENCY_RXM_FULLY_BYPASSED = 5 # 101 |
| 73 |
OPER = 6 # 110 |
| 74 |
BYPASS = 7 # 111 |
| 75 |
|
| 76 |
|
| 77 |
class ERxChannelId(enum.IntEnum): |
| 78 |
V = 0 |
| 79 |
H = 1 |
| 80 |
|
| 81 |
|
| 82 |
class EBaqMode(enum.IntEnum): |
| 83 |
BYPASS = 0 |
| 84 |
BAQ3 = 3 |
| 85 |
BAQ4 = 4 |
| 86 |
BAQ5 = 5 |
| 87 |
FDBAQ_MODE_0 = 12 |
| 88 |
FDBAQ_MODE_1 = 13 |
| 89 |
FDBAQ_MODE_2 = 14 |
| 90 |
|
| 91 |
|
| 92 |
class ERangeDecimation(enum.IntEnum): |
| 93 |
X3_ON_4 = 0 |
| 94 |
X2_ON_3 = 1 |
| 95 |
X5_ON_9 = 3 |
| 96 |
X4_ON_9 = 4 |
| 97 |
X3_ON_8 = 5 |
| 98 |
X1_ON_3 = 6 |
| 99 |
X1_ON_6 = 7 |
| 100 |
X3_ON_7 = 8 |
| 101 |
X5_ON_16 = 9 |
| 102 |
X3_ON_26 = 10 |
| 103 |
X4_ON_11 = 11 |
| 104 |
|
| 105 |
|
| 106 |
@bpack.bs.decoder |
| 107 |
@bpack.descriptor(baseunits=BITS, byteorder=BE) |
| 108 |
class PacketPrimaryHeader: |
| 109 |
version: T["u3"] = 0 |
| 110 |
packet_type: T["u1"] = 0 |
| 111 |
secondary_header_flag: bool = True |
| 112 |
pid: T["u7"] = 0 |
| 113 |
pcat: T["u4"] = 0 |
| 114 |
sequence_flags: T["u2"] = 0 |
| 115 |
sequence_counter: T["u14"] = 0 |
| 116 |
packet_data_length: T["u16"] = 0 |
| 117 |
|
| 118 |
|
| 119 |
@bpack.bs.decoder |
| 120 |
@bpack.descriptor(baseunits=BITS, byteorder=BE) |
| 121 |
class DatationService: |
| 122 |
coarse_time: T["u32"] = 0 |
| 123 |
fine_time: T["u16"] = 0 |
| 124 |
|
| 125 |
|
| 126 |
@bpack.bs.decoder |
| 127 |
@bpack.descriptor(baseunits=BITS, byteorder=BE) |
| 128 |
class FixedAncillaryDataService: |
| 129 |
sync_marker: T["u32"] = SYNK_MARKER |
| 130 |
data_take_id: T["u32"] = 0 |
| 131 |
ecc_num: EEccNumber = bpack.field(size=8, default=EEccNumber.NOT_SET) |
| 132 |
# n. 1 bit n/a |
| 133 |
test_mode: ETestMode = bpack.field( |
| 134 |
size=3, offset=73, default=ETestMode.DEFAULT |
| 135 |
) |
| 136 |
rx_channel_id: ERxChannelId = bpack.field(size=4, default=ERxChannelId.V) |
| 137 |
instrument_configuration_id: T["u32"] = 0 # NOTE: the data type is TBD |
| 138 |
|
| 139 |
|
| 140 |
@bpack.bs.decoder |
| 141 |
@bpack.descriptor(baseunits=BITS, byteorder=BE) |
| 142 |
class SubCommunicationAncillaryDataService: |
| 143 |
word_index: T["u8"] = 0 |
| 144 |
word_data: T["u16"] = 0 |
| 145 |
|
| 146 |
|
| 147 |
@bpack.bs.decoder |
| 148 |
@bpack.descriptor(baseunits=BITS, byteorder=BE) |
| 149 |
class CounterService: |
| 150 |
space_packet_count: T["u32"] = 0 |
| 151 |
pri_count: T["u32"] = 0 |
| 152 |
|
| 153 |
|
| 154 |
@bpack.bs.decoder |
| 155 |
@bpack.descriptor(baseunits=BITS, byteorder=BE) |
| 156 |
class RadarConfigurationSupportService: |
| 157 |
error_flag: bool = False |
| 158 |
baq_mode: EBaqMode = bpack.field(size=5, offset=3, default=EBaqMode.BYPASS) |
| 159 |
baq_block_len: T["u8"] = 0 |
| 160 |
# n. 8 bits padding |
| 161 |
range_decimation: ERangeDecimation = bpack.field( |
| 162 |
size=8, offset=24, default=0 |
| 163 |
) |
| 164 |
rx_gain: T["u8"] = 0 |
| 165 |
tx_ramp_rate: T["u16"] = 0 |
| 166 |
tx_pulse_start_freq: T["u16"] = 0 |
| 167 |
tx_pulse_length: T["u24"] = 0 |
| 168 |
# n. 3 bits pad |
| 169 |
rank: T["u5"] = bpack.field(offset=99, default=0) |
| 170 |
pri: T["u24"] = 0 |
| 171 |
swst: T["u24"] = 0 |
| 172 |
swl: T["u24"] = 0 |
| 173 |
sas_sbb_message: T["S24"] = 0 # TODO: replace with SAS sub-record |
| 174 |
ses_sbb_message: T["S24"] = 0 # TODO: replace with SES sub-record |
| 175 |
|
| 176 |
|
| 177 |
@bpack.bs.decoder |
| 178 |
@bpack.descriptor(baseunits=BITS, byteorder=BE, size=24) |
| 179 |
class RadarSampleCountService: |
| 180 |
number_of_quads: T["u16"] = 0 |
| 181 |
# n. 8 bits pad |
| 182 |
|
| 183 |
|
| 184 |
@bpack.bs.decoder |
| 185 |
@bpack.descriptor(baseunits=BITS, byteorder=BE) |
| 186 |
class PacketSecondaryHeader: |
| 187 |
datation_service: DatationService |
| 188 |
fixed_ancillary_data_service: FixedAncillaryDataService |
| 189 |
subcom_ancillary_data_service: SubCommunicationAncillaryDataService |
| 190 |
counters_service: CounterService |
| 191 |
radar_configuration_support_service: RadarConfigurationSupportService |
| 192 |
radar_sample_count_service: RadarSampleCountService |
| 193 |
|
| 194 |
|
| 195 |
def sequential_stream_decoder(filename, maxcount=None): |
| 196 |
"""Decode packet headers and store them into a pandas data-frame.""" |
| 197 |
import tqdm |
| 198 |
import pandas as pd |
| 199 |
|
| 200 |
log = logging.getLogger(__name__) |
| 201 |
log.info(f'start decoding: "{filename}"') |
| 202 |
t0 = datetime.datetime.now() |
| 203 |
|
| 204 |
primary_header_size = bpack.calcsize( |
| 205 |
PacketPrimaryHeader, bpack.EBaseUnits.BYTES |
| 206 |
) |
| 207 |
secondary_header_size = bpack.calcsize( |
| 208 |
PacketSecondaryHeader, bpack.EBaseUnits.BYTES |
| 209 |
) |
| 210 |
records = [] |
| 211 |
packet_counter = 0 |
| 212 |
pbar = tqdm.tqdm(unit=" packets", desc="decoded") |
| 213 |
with open(filename, "rb") as fd, pbar: |
| 214 |
while fd: |
| 215 |
# primary header |
| 216 |
data = fd.read(primary_header_size) |
| 217 |
if len(data) == 0 or (maxcount and len(records) > maxcount): |
| 218 |
break |
| 219 |
|
| 220 |
# type - PacketPrimaryHeader |
| 221 |
primary_header = PacketPrimaryHeader.frombytes(data) |
| 222 |
# print(primary_header) |
| 223 |
|
| 224 |
assert primary_header.version == 0 |
| 225 |
assert primary_header.packet_type == 0 |
| 226 |
assert primary_header.sequence_flags == 3 |
| 227 |
# assert primary_header.sequence_counter == packet_counter % 2**14 |
| 228 |
|
| 229 |
# secondary header |
| 230 |
assert primary_header.secondary_header_flag |
| 231 |
data_field_size = primary_header.packet_data_length + 1 |
| 232 |
data = fd.read(data_field_size) |
| 233 |
|
| 234 |
# type - PacketSecondaryHeader |
| 235 |
secondary_header = PacketSecondaryHeader.frombytes( |
| 236 |
data[:secondary_header_size] |
| 237 |
) |
| 238 |
# print(secondary_header) |
| 239 |
|
| 240 |
sync = secondary_header.fixed_ancillary_data_service.sync_marker |
| 241 |
if sync != SYNK_MARKER: |
| 242 |
raise SyncMarkerException( |
| 243 |
f"packat count: {packet_counter + 1}" |
| 244 |
) |
| 245 |
|
| 246 |
radar_cfg = secondary_header.radar_configuration_support_service |
| 247 |
assert radar_cfg.error_flag is False |
| 248 |
# baq_block_len = 8 * (radar_cfg.baq_block_len + 1) |
| 249 |
# assert baq_block_len == 256, ( |
| 250 |
# f'baq_block_len: {radar_cfg.baq_block_len}, ' |
| 251 |
# f'baq_mode: {radar_cfg.baq_mode}' |
| 252 |
# ) |
| 253 |
|
| 254 |
counters_service = secondary_header.counters_service |
| 255 |
assert packet_counter == counters_service.space_packet_count |
| 256 |
packet_counter += 1 |
| 257 |
|
| 258 |
# update the dataframe |
| 259 |
r = bpack.asdict(primary_header) |
| 260 |
r.update(bpack.asdict(secondary_header.datation_service)) |
| 261 |
r.update( |
| 262 |
bpack.asdict(secondary_header.fixed_ancillary_data_service) |
| 263 |
) |
| 264 |
r.update( |
| 265 |
bpack.asdict(secondary_header.subcom_ancillary_data_service) |
| 266 |
) |
| 267 |
r.update(bpack.asdict(secondary_header.counters_service)) |
| 268 |
r.update( |
| 269 |
bpack.asdict( |
| 270 |
secondary_header.radar_configuration_support_service |
| 271 |
) |
| 272 |
) |
| 273 |
r.update(bpack.asdict(secondary_header.radar_sample_count_service)) |
| 274 |
records.append(r) |
| 275 |
|
| 276 |
# user data |
| 277 |
# TBW |
| 278 |
|
| 279 |
pbar.update() |
| 280 |
|
| 281 |
elapsed = datetime.datetime.now() - t0 |
| 282 |
log.info(f"decoding complete (elapsed time: {elapsed})") |
| 283 |
|
| 284 |
return pd.DataFrame(records) |
| 285 |
|
| 286 |
|
| 287 |
if __name__ == "__main__": |
| 288 |
import os |
| 289 |
import sys |
| 290 |
|
| 291 |
logging.basicConfig( |
| 292 |
level=logging.DEBUG, format="%(asctime)s %(levelname)s: %(message)s" |
| 293 |
) |
| 294 |
|
| 295 |
filename = ( |
| 296 |
"S1B_S3_RAW__0SDV_20210530T130904_20210530T130929_027134_033DC1_1AF2.SAFE/" |
| 297 |
"s1b-s3-raw-s-vv-20210530t130904-20210530t130929-027134-033dc1.dat" |
| 298 |
) |
| 299 |
if not os.path.exists(filename): |
| 300 |
sys.exit( |
| 301 |
"""ERROR: sample product not available. |
| 302 |
|
| 303 |
You can download it as follows (scihub.copernicus.eu authentication needed): |
| 304 |
|
| 305 |
$ sentinelsat --name 'S1B_S3_RAW__0SDV_20200615T162409_20200615T162435_022046_029D76*' --download |
| 306 |
""" |
| 307 |
) |
| 308 |
|
| 309 |
df = sequential_stream_decoder(filename) # , maxcount=10) |
| 310 |
# print() |
| 311 |
# print(df.head()) |
| 312 |
|
| 313 |
""" |
| 314 |
$ env PYTHONPATH=.. python3 s1isp.py |
| 315 |
2021-06-03 08:56:31,783 INFO: start decoding: "S1B_S3_RAW__0SDV_20210530T130904_20210530T130929_027134_033DC1_1AF2.SAFE/s1b-s3-raw-s-vv-20210530t130904-20210530t130929-027134-033dc1.dat" |
| 316 |
decoded: 48941 packets [00:19, 2506.83 packets/s] |
| 317 |
2021-06-03 08:56:51,320 INFO: decoding complete (elapsed time: 0:00:19.537022) |
| 318 |
""" |