Last active
March 19, 2021 05:33
-
-
Save boyank/d9640e9c4dc3877012b8fb4dc9f6c053 to your computer and use it in GitHub Desktop.
Example of reading NASA JUNO JADE CALIBRATED SCIENCE DATA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Example of reading NASA JUNO JADE CALIBRATED SCIENCE DATA | |
https://pds-ppi.igpp.ucla.edu/search/view/?f=yes&id=pds://PPI/JNO-J_SW-JAD-3-CALIBRATED-V1.0/DATA/2018/2018091/ELECTRONS/JAD_L30_LRS_ELC_ANY_CNT_2018091_V03&o=1 | |
https://stackoverflow.com/a/66687113/4046632 | |
""" | |
import struct | |
from functools import reduce | |
from operator import mul | |
from collections import namedtuple | |
__author__ = "Boyan Kolev, https://stackoverflow.com/users/4046632/buran" | |
with open('JAD_L30_LRS_ELC_ANY_CNT_2018091_V03.LBL') as f: | |
rjws = [line.strip('\n/* ') for line in f if line.startswith('/* RJW')] | |
# create the format string for struct | |
rjws = rjws[2:] # exclude first 2 RJW comments related to file itself | |
names = [] | |
FMT = '=' | |
print(f'Number of objects: {len(rjws)}') | |
for idx, rjw in enumerate(rjws): | |
_, name, fmt, num_dim, *dims = rjw.split(', ') | |
fstr = f'{reduce(mul, map(int, dims))}{fmt}' | |
FMT = f'{FMT} {fstr}' | |
names.append(name) | |
print(f'{idx}:{name}, {fstr}') | |
FMT = FMT.replace('c', 's') # for conveninece treat 21c as s char[] | |
print(f"Format string: {repr(FMT)}") | |
# parse DAT file | |
s = struct.Struct(FMT) | |
print(f'Struct size:{s.size}') | |
with open('JAD_L30_LRS_ELC_ANY_CNT_2018091_V03.DAT', 'rb') as f: | |
n = 0 | |
while True: # in python3.8+ this loop can be simplified with walrus operator | |
chunk = f.read(s.size) | |
if not chunk: | |
break | |
data = s.unpack_from(chunk) | |
# process data further, e.g. split data in 2D containers where appropriate | |
n += 1 | |
print(f'Number of records: {n}') | |
# make a named tuple to represent first 10 fields | |
# for nice display. This basic use of namedtuple works only | |
# for first 23 objects, which have single item. | |
num_fields = 10 | |
Record = namedtuple('Record', names[:num_fields]) | |
record = Record(*data[:num_fields]) | |
print('\n----------------------\n') | |
print(f'First {num_fields} fields of the last record.') | |
print(record) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output