Skip to content
Snippets Groups Projects
Commit 86405792 authored by Bruce Flynn's avatar Bruce Flynn
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
.eggs
build
dist
*.egg-info
*.sw?
\ No newline at end of file
include .version
from datetime import datetime
from jpssl0util import split, dump
from jpssl0util.timecode import cds_stamp
def cmd_split():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--minutes', type=int, default=6)
parser.add_argument('--output-format', default='%y%j%H%M%S001.PDS')
parser.add_argument('filepath')
args = parser.parse_args()
for time, blob in split.split(args.filepath, args.minutes):
stamp = datetime.utcfromtimestamp(time)
filepath = stamp.strftime(args.output_format)
print("writing %s" % filepath)
open(filepath, 'wb', buffering=0).write(blob)
def cmd_dump():
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('pds')
args = parser.parse_args()
def handler(h1, h2):
print(h1)
if h2:
stamp = cds_stamp(h2.day, h2.milliseconds, h2.microseconds)
print("\t%s : %s" % (stamp, h2))
stats = dump.dump(args.pds, handler=handler)
print("Packet Count: {:d}".format(stats['packet_count']))
print("Group Count: {:d}".format(stats['group_count']))
apids = sorted(stats['apid_counts'])
for apid in apids:
print("APID {:d} Count: {:d}".format(apid, stats['apid_counts'][apid]))
#!/usr/bin/env python
from collections import defaultdict
from edos import ccsds
from jpssl0util.timecode import cds_stamp
def dump(filepath, handler=None):
pktcnt = 0
groupcnt = 0
apidcnts = defaultdict(lambda: 0)
for h1, h2 in ccsds.walk(filepath):
pktcnt += 1
if h1.sequence_grouping == ccsds.GROUP_FIRST:
groupcnt += 1
apidcnts[h1.apid] += 1
handler(h1, h2)
return dict(
packet_count = pktcnt,
group_count = groupcnt,
apid_counts = apidcnts
)
\ No newline at end of file
#!/usr/bin/env python
"""
TODO:
- Deal with leapseconds, use grain?
"""
from io import open
from cStringIO import StringIO
from datetime import datetime, timedelta
from edos import ccsds
from jpssl0util.timecode import cds_stamp, unixtime
def first_pkt_in_scan(h1):
return h1.sequence_grouping == ccsds.GROUP_FIRST
def standalone_pkt(h1):
return h1.sequence_grouping == ccsds.GROUP_STANDALONE
def time_to_roll(time, minutes):
return time - time % (minutes * 60)
def split(filepath, minutes):
"""Split a VIIRS L0 PDS file into files based on their scan time mod the
number of minutes provided.
A single output file is buffered in memory until it is written.
"""
with open(filepath, 'rb') as datain:
buf = StringIO() # buffer for a single data file until it is written
cur_bucket = 0 # cur time bucket of size 'minutes'
cursor = 0 # cur pos in file
headers = ccsds.walk(filepath)
for h1, h2 in headers:
if first_pkt_in_scan(h1) or standalone_pkt(h1):
stamp = cds_stamp(h2.day, h2.millisecods, h2.microseconds)
hdrtime = unixtime(stamp)
pkt_bucket = hdrtime - hdrtime % (minutes * 60)
if cur_bucket == 0:
cur_bucket = pkt_bucket
if cursor != 0 and pkt_bucket > cur_bucket:
yield cur_bucket, buf.getvalue()
buf = StringIO()
cur_bucket = pkt_bucket
datain.seek(cursor)
# data_length_minus1 + 1 == len(h2) + len(data)
read_size = ccsds.c.sizeof(h1) + h1.data_length_minus1 + 1
buf.write(datain.read(read_size))
cursor += read_size
yield cur_bucket, buf.getvalue()
\ No newline at end of file
from datetime import datetime, timedelta
JPSS_EPOCH = datetime(1958, 1, 1)
UNIX_EPOCH = datetime(1970, 1, 1)
def unixtime(dt):
if dt > UNIX_EPOCH:
return (dt - UNIX_EPOCH).total_seconds()
return (UNIX_EPOCH - dt).total_seconds()
def cds_stamp(days, millis, micros, epoch=JPSS_EPOCH):
return JPSS_EPOCH + timedelta(days=days, microseconds=1000 * millis + micros)
\ No newline at end of file
setup.py 0 → 100644
from setuptools import setup, find_packages
setup(
name='JpssL0Util',
version=':pygittools:',
pygitmeta=True,
packages=find_packages(),
setup_requires=[
'PyGitTools>=0.1.2'
],
install_requires=[
'edos'
],
entry_points="""
[console_scripts]
jpssl0split = jpssl0util.cli:cmd_split
jpssl0dump = jpssl0util.cli:cmd_dump
"""
)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment