NASA Logo
Ocean Color Science Software

ocssw V2022
l0info_hkt.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 from telemetry import ccsdspy
3 from io import BytesIO
4 from telemetry.timestamp import *
5 from telemetry.PacketUtils import readFileHeader
6 
7 __version__ = '3.0.0_2024-02-26'
8 
9 
10 
11 def get_timestamp(all_packets, is_stime=True):
12  '''
13  Given a CCSDSPY packet list, get the start or end time
14  from the packets
15 
16  PARAMETERS:
17  all_packets : List of bytes
18  is_stime : True for start time, False for end time
19 
20  RETURNS:
21  datetime
22 
23  '''
24  # reading backwards or forward range object
25  range_order = range(len(all_packets)) if is_stime else reversed(range(len(all_packets)))
26 
27  time = None
28  for i in range_order:
29  packet = all_packets[i]
30  data = packet[6:]
31  header = ccsdspy.utils.read_primary_headers(BytesIO(packet))
32 
33  for k, v in header.items():
34  header[k] = v[0] # remove outer array
35 
36  #check for invalid header
37  if header['CCSDS_PACKET_LENGTH'] > 16378:
38  continue # go to next packet
39 
40  # check for truncated data
41  if len(data) < header['CCSDS_PACKET_LENGTH'] + 1:
42  break # done with this file
43 
44  if (header['CCSDS_SECONDARY_FLAG'] == 1
45  and data[0] > 112 # after 2017-07-18T05:49:15Z
46  and data[0] < 192): # before 2060-01-28T16:50:35Z
47  timestamp = readTimestamp(data)
48  time = tai58_as_datetime(timestamp)
49  break # only need the first valid time for forward and backwards reading
50 
51  return time.strftime('%Y-%m-%dT%H:%M:%S.%f')
52 
53 
54 
55 def l0info_hkt(args, fh, output):
56  # procedure to get start and end times from PACE S-band HKT data file name
57  print("Running l0info_hkt (version: %s) \n" % __version__)
58 
59  if args.verbose:
60  print("Reading PACE S-band data file.")
61 
62  status = 104
63  try:
64  # read the CFE header and advance the reading pointer if it exists
65  headerInfo = readFileHeader(fh)
66 
67  # if not a PACE file, raise exception
68  if (headerInfo["SCID"] != b"PACE"):
69  print('Not a valid PACE L0 file')
70  status = 102 # invalid instrument
71  raise Exception
72 
73  # get all the packets
74  all_packets = ccsdspy.utils.split_packet_bytes(fh, include_primary_header=True)
75 
76  # get the start and stop times
77  stime = get_timestamp(all_packets, True)
78  etime = get_timestamp(all_packets, False)
79 
80  print("start_time=%s" % stime)
81  print("stop_time=%s" % etime)
82 
83  if output:
84  output.write("start_time=%s\n" % stime)
85  output.write("stop_time=%s\n" % etime)
86 
87  except Exception as e:
88  return status
89 
90 
91  # status 0 if no issues
92  return 0
def read_primary_headers(file)
Definition: utils.py:109
def split_packet_bytes(file, include_primary_header=True)
Definition: utils.py:84
def get_timestamp(all_packets, is_stime=True)
Definition: l0info_hkt.py:11
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38
def readTimestamp(data)
Definition: PacketUtils.py:19
def l0info_hkt(args, fh, output)
Definition: l0info_hkt.py:55
def tai58_as_datetime(tai58)
Definition: PacketUtils.py:24