OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l0info_spex.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # procedure to get start and end times from Level-0 fpacket files for SPEXone
4 # The times are extracted from the first fpacket of each image fpacket group.
5 # Reference: SPEXone TMTC Database, SPX1-TN-004
6 
7 __version__ = '1.3.0_2023-01-23'
8 
9 from l0info_utils import read_packet, read_apid, get_anc_packet_time, is_bad_packet
10 
11 def is_bad_apid(apid,fh):
12  if apid == -1:
13  return False
14  if (apid < 800) or (apid > 849):
15  print("Invalid packet header at byte %d."%fh.tell())
16  return True
17  else:
18  return False
19 
20 def l0info_spex(args, fh, output):
21  print("Running l0info_spex (version: %s) \n" % __version__)
22 
23  status = 0
24  if args.verbose:
25  print("Reading SPEXone science data.")
26 
27  # Skip CFE header
28  fh.seek(64)
29  bSPW = True
30 
31  # Find first science fpacket
32  apid = 0
33  packetLength = 8
34  firstp = 0
35  while ((apid != 848) or (not firstp)) and (packetLength > 7):
36  fpacket, packetLength = read_packet(fh, bSPW)
37  apid = read_apid(fpacket)
38  if is_bad_apid(apid,fh): return 104
39  if fpacket: firstp = (fpacket[2] & 64)>>6
40 
41  if is_bad_packet(packetLength,fh): return 104
42 
43  # If no science packets, rewind and get times from HKT packets
44  if (packetLength==0):
45  print("No science packets found in file.")
46  if output:
47  output.write("datatype=SPEX\n")
48  status = 110
49  fh.seek(64)
50  apid = 0
51  packetLength = 8
52  csec = 0
53  while ((apid != 800) or (csec<1000000000)) and (packetLength > 7):
54  fpacket, packetLength = read_packet(fh, bSPW)
55  apid = read_apid(fpacket)
56  csec = int.from_bytes(fpacket[6:10],'big')
57 
58  if is_bad_packet(packetLength,fh): return 104
59 
60  if fpacket:
61  mpacket = fpacket
62  try:
63  stime = get_anc_packet_time(mpacket[6:12],0,'msec')
64  print("start_time=%s" % stime.strftime('%Y-%m-%dT%H:%M:%S.%f'))
65  if output:
66  output.write("start_time=%s\n" % stime.strftime('%Y-%m-%dT%H:%M:%S.%f'))
67  except:
68  return 120
69 
70  while packetLength > 7:
71  fpacket, packetLength = read_packet(fh, bSPW)
72  apid = read_apid(fpacket)
73  if (apid == 800) and fpacket: mpacket = fpacket
74 
75  if is_bad_packet(packetLength,fh): return 104
76 
77  etime = get_anc_packet_time(mpacket[6:12],0,'msec')
78  else:
79  # Determine data type from packet fields
80  fmode = fpacket[125] # frame mode
81  omode = fpacket[130] # output mode
82  imrlen = int.from_bytes(fpacket[296:300],'big') # swap_endian(long(packet[296:299],0)); image record length
83  dtype = -1
84  if (fmode == 2) and (omode == 1) and (imrlen > 0): dtype = 1 # Science
85  if (fmode == 1) and (omode == 3) and (imrlen > 0): dtype = 2 # Diagnostic
86 
87  if dtype == 1:
88  # Science data
89  print("datatype=SPEX")
90  if output:
91  output.write("datatype=SPEX_SCI\n")
92  elif dtype == 2:
93  # Diagnostic data
94  print("datatype=SPEX_DIAG")
95  if output:
96  output.write("datatype=SPEX_DIAG\n")
97  else:
98  print("Invalid SPEXone data type")
99  status = 101
100 
101  # Get start time
102  try:
103  stime = get_anc_packet_time(fpacket[300:306],0,'msec')
104  print("start_time=%s" % stime.strftime('%Y-%m-%dT%H:%M:%S.%f'))
105  if output:
106  output.write("start_time=%s\n" % stime.strftime('%Y-%m-%dT%H:%M:%S.%f'))
107  except:
108  return 120
109 
110  # Read to end of file
111  epacket = fpacket
112  while packetLength > 0:
113  apid = 0
114  firstp = 0
115  while ((apid != 848) or (not firstp)) and (packetLength > 7):
116  fpacket, packetLength = read_packet(fh, bSPW)
117  apid = read_apid(fpacket)
118  if is_bad_apid(apid,fh): return 104
119  if fpacket: firstp = (fpacket[2] & 64)>>6
120 
121  if is_bad_packet(packetLength,fh): return 104
122 
123  if fpacket: epacket = fpacket
124 
125  etime = get_anc_packet_time(epacket[300:306],0,'msec')
126 
127  print("stop_time=%s" % etime.strftime('%Y-%m-%dT%H:%M:%S.%f'))
128  if output:
129  output.write("stop_time=%s\n" % etime.strftime('%Y-%m-%dT%H:%M:%S.%f'))
130 
131  return status
def read_apid(fpacket)
Definition: l0info_utils.py:93
int get_anc_packet_time(uint8_t *apacket, int32_t &iyear, int32_t &iday, double &stime)
Definition: common.cpp:97
int read_packet(FILE *infile, uint8_t packet[], int *len, long int *endfile)
Definition: read_packet.c:18
def is_bad_apid(apid, fh)
Definition: l0info_spex.py:11
def is_bad_packet(packetLength, fh)
def l0info_spex(args, fh, output)
Definition: l0info_spex.py:20