OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
read_viirs_scan_packets.c
Go to the documentation of this file.
1 // Ported from IDL procedure to read all of the packets for a VIIRS scan
2 // into a packet buffer
3 
4 // Arguments
5 //
6 // Name Type I/O Description
7 // ---- ---- --- -----------
8 // infile FILE I Input file pointer
9 // epacket byte(9318) I/O First packet already read from
10 // file
11 // len_packet int O packet size
12 // pbuffer byte(32000,513) O Packet buffer array
13 // npkts int O Number of packets stored in buffer
14 // endfile long int O File Length (bytes); value set to 0 at the end of file
15 // Liang Hong, July 22, 2015
16 
17 #include <string.h>
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <timeutils.h>
21 #include "l0qc_viirs.h"
22 
23 int read_viirs_scan_packets(FILE *infile, uint8_t epacket[], int *len_packet, uint8_t pbuffer[], int *npkts, long int *endfile) {
24  // Get VIIRS scan number and start time from first packet
25 
26  int32_t iyear, iday;
27  int i;
28  double stime;
29  uint8_t cctime[8];
30  memcpy(cctime, &epacket[6], 8);
31  ccsds_to_yds(cctime, &iyear, &iday, &stime);
32  int32_t jd1 = jday(iyear, 1, iday);
33 
34  long scnum;
35  if (epacket[1] == 58) {
36  //scnum = swap_endian(long(epacket(48:51),0));
37  scnum = (epacket[48] << 24) + (epacket[49] << 16) + (epacket[50] << 8) + epacket[51];
38  } else {
39  //scnum = swap_endian(long(epacket(34:37),0));
40  scnum = (epacket[34] << 24) + (epacket[35] << 16) + (epacket[36] << 8) + epacket[37];
41  }
42 
43  // Store the engineering packet in the buffer
44  for (i = 0; i<*len_packet; i++) pbuffer[i + 0 * 32000] = epacket[i];
45  *npkts = 1;
46 
47  // Read all of the packets with this start time and scan number
48  // and store in the packet buffer
49 
50  // Read the next packet
51  uint8_t packet[30000];
52  read_packet(infile, packet, len_packet, endfile);
53  if (*endfile == 0) return 0;
54 
55  // Check for missing first packet of a group
56  int first = (packet[0] & 8) / 8;
57  while (!first) {
58  for (i = 0; i<*len_packet; i++) pbuffer[i + *npkts * 32000] = packet[i];
59  *npkts = *npkts + 1;
60  read_packet(infile, packet, len_packet, endfile);
61  if (*endfile == 0) return 0;
62  first = (packet[0] & 8) / 8;
63  }
64 
65  // Check scan number and start time
66 
67  //scn = swap_endian(long(packet(34:37),0))
68  long scn;
69  scn = (packet[34] << 24) + (packet[35] << 16) + (packet[36] << 8) + packet[37];
70  memcpy(cctime, &packet[6], 8);
71  double stm;
72  ccsds_to_yds(cctime, &iyear, &iday, &stm);
73  stm = stm + 864 * (jday(iyear, 1, iday) - jd1);
74 
75  int apd, apid, npkg;
76  double usec, sec;
77  int16_t iyr, ih, mn, mm, dd;
78  while ((scn <= scnum) && (stm <= stime)) {
79  // Check for packet out of order
80  if ((scn != scnum) || (stm != stime)) {
81  usec = yds2unix(iyear, iday, stm);
82  unix2ymdhms(usec, &iyr, &mm, &dd, &ih, &mn, &sec);
83  printf("Packet out of order at %02d:%02d:%02d\n", ih, mn, (int) sec);
84  // Find next packet group
85  first = 0;
86  while (!first) {
87  read_packet(infile, packet, len_packet, endfile);
88  if (*endfile == 0) return 0;
89  first = (packet[0] & 8) / 8;
90  }
91  } else {
92  // Get APID and number of packets in group and load packet into buffer
93  apid = (packet[0] % 8)*256 + packet[1];
94  npkg = packet[14];
95  for (i = 0; i<*len_packet; i++) pbuffer[i + *npkts * 32000] = packet[i];
96  *npkts = *npkts + 1;
97 
98  // Read remaining packets in group
99  int ipkg = 0;
100  while (ipkg <= npkg) {
101  read_packet(infile, packet, len_packet, endfile);
102  if (*endfile == 0) return 0;
103  apd = (packet[0] % 8)*256 + packet[1];
104 
105  // Check APID// if not a match, missing packets
106  if (apd == apid) {
107  for (i = 0; i<*len_packet; i++) pbuffer[i + *npkts * 32000] = packet[i];
108  *npkts = *npkts + 1;
109  ipkg++;
110  } else {
111  ipkg = npkg + 1;
112  }
113  }
114 
115  // Check for another first packet of a group
116  first = (packet[0] & 8) / 8;
117  while (!first) {
118  for (i = 0; i<*len_packet; i++) pbuffer[i + *npkts * 32000] = packet[i];
119  *npkts = *npkts + 1;
120  read_packet(infile, packet, len_packet, endfile);
121  if (*endfile == 0) return 0;
122  first = (packet[0] & 8) / 8;
123  }
124  }
125  scn = (packet[34] << 24) + (packet[35] << 16) + (packet[36] << 8) + packet[37];
126 
127  memcpy(cctime, &packet[6], 8);
128  ccsds_to_yds(cctime, &iyear, &iday, &stm);
129 
130  stm = stm + 864 * (jday(iyear, 1, iday) - jd1);
131  }
132 
133  // Load last packet read into engineering packet for next scan
134  memcpy(epacket, &packet, *len_packet);
135 
136  return 0;
137 }
138 
139 
140 
141 
142 
143 
144 
double yds2unix(int16_t year, int16_t day, double secs)
Definition: yds2unix.c:7
int32_t jday(int16_t i, int16_t j, int16_t k)
Definition: jday.c:4
int ccsds_to_yds(uint8_t *cctime, int32_t *iyear, int32_t *iday, double *sec)
Definition: ccsds_to_yds.c:5
int read_packet(FILE *infile, uint8_t packet[], int *len, long int *endfile)
Definition: read_packet.c:18
subroutine stm
Definition: 6sm1.f:6698
int read_viirs_scan_packets(FILE *infile, uint8_t epacket[], int *len_packet, uint8_t pbuffer[], int *npkts, long int *endfile)
int32_t ih
Definition: atrem_corl1.h:161
void unix2ymdhms(double usec, int16_t *year, int16_t *mon, int16_t *day, int16_t *hour, int16_t *min, double *sec)
Definition: unix2ymdhms.c:8
int i
Definition: decode_rs.h:71
int32_t iyr
Definition: atrem_corl1.h:161