OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
process_a_scan.c
Go to the documentation of this file.
1 #include "PGS_IO.h"
2 #include "PGS_SMF.h"
3 #include "PGS_TYPES.h"
4 #include "PGS_MODIS_35005.h"
5 #include "SC_scan.h"
6 #include "PH_pkt_hdr.h"
7 #include "PD_pkt_data.h"
8 #include "MD_metadata.h"
9 #include "EN_eng_data.h"
10 #include "FP_failed_pkt_queue.h"
11 #include "L1A_prototype.h"
12 
13 PGSt_SMF_status process_a_scan (
14  int *scan_number,
15  PGSt_IO_L0_Packet *pkt,
16  PGSt_double *scan_rate,
17  PGSt_double *scan_time,
18  SC_SCAN_DATA_t *L1A_scan,
19  MD_SCAN_MET_t *scan_meta,
20  EN_VDATA_TYPE_t *eng_data,
21  FP_QUEUE_t *failed_pkts,
22  PH_PACKET_HEADER_t *pkt_header,
23  SC_PIXEL_QUALITY_DATA_t *scan_pixel,
24  PGSt_IO_L0_VirtualDataSet *L0_file
25 )
26 /*
27 !C************************************************************************
28 
29 !Description:
30  This routine drives the accumulation of scans of MODIS data. It calls
31  process_a_packet in a loop until it gets the first packet of a new scan.
32  As each packet is received, the contents are placed into the
33  appropriate part of the scan data structure, and the scan metadata and
34  pixel quality data are updated. If a data dropout is detected, then
35  the routine updates the scan metadata and may return if the dropout
36  extends past the end of the next scan. When the next scan is detected,
37  the routine returns the scan data structure, scan metadata, pixel
38  quality data, and the start time of the next scan.
39 
40 !Input Parameters:
41  PGSt_double *scan_rate ** Scan rate **
42 
43 
44 !Output Parameters:
45  SC_SCAN_DATA_t L1A_scan ** Scan data structure **
46 
47  MD_SCAN_MET_t *scan_meta ** Scan Level Metadata **
48 
49  FP_QUEUE_t *failed_pkts ** Discarded packets **
50 
51  SC_PIXEL_QUALITY_DATA_t *scan_pixel ** Pixel Quality Data **
52 
53 
54 !Input/Output Parameters:
55  EN_VDATA_TYPE_t *eng_data ** Eng Data **
56 
57  PGSt_double *scan_time ** Start time for the **
58  ** scan **
59 
60  PGSt_IO_L0_VirtualDataSet * L0_file ** The L0 file that **
61  ** contains the packets **
62 
63  PGSt_IO_L0_Packet *pkt ** Packet of data **
64 
65  PH_PACKET_HEADER_t *pkt_header ** Unpacked packet **
66  ** header **
67 
68  int *scan_number ** Scan number **
69 
70 Return Values:
71  MODIS_E_CHECKSUM_NOT_VALID
72  MODIS_E_INV_PKT_TIME
73  MODIS_E_SCANCNT_NOT_VALID
74  MODIS_F_PKT_READ_FAILED
75  MODIS_M_PKT_NOT_IN_SCAN
76  MODIS_S_SUCCESS
77  MODIS_W_NO_MORE_PACKETS
78 
79 Externally Defined:
80  EN_VDATA_TYPE_t (EN_eng_data.h)
81  FP_QUEUE_t (FP_failed_pkt_queue.h)
82  MD_SCAN_MET_t (MD_metadata.h)
83  MODIS_E_CHECKSUM_NOT_VALID (PGS_MODIS_35005.h)
84  MODIS_E_INV_PKT_TIME (PGS_MODIS_35005.h)
85  MODIS_E_SCANCNT_NOT_VALID (PGS_MODIS_35005.h)
86  MODIS_F_PKT_READ_FAILED (PGS_MODIS-35005.h)
87  MODIS_M_PKT_NOT_IN_SCAN (PGS_MODIS_35005.h)
88  MODIS_S_SUCCESS (PGS_MODIS_35005.h)
89  MODIS_W_NO_MORE_PACKETS (PGS_MODIS-35005.h)
90  PGS_FALSE (PGS_SMF.h)
91  PGS_TRUE (PGS_SMF.h)
92  PGSt_double (PGS_TYPES.h)
93  PGSt_IO_L0_Packet (PGS_IO.h)
94  PGSt_IO_L0_VirtualDataSet (PGS_SMF.h)
95  PGSt_SMF_boolean (PGS_SMF.h)
96  PGSt_SMF_status (PGS_SMF.h)
97  PH_MOD_SOURCE_ID_TYPE_FLAG_CAL (PH_pkt_hdr.h)
98  PH_PACKET_HEADER_t (PH_pkt_hdr.h)
99  PH_PRI_SEQUENCE_FIRST_PKT_IN_GROUP (PH_pkt_hdr.h)
100  PH_PRI_SEQUENCE_ONLY_PKT_IN_GROUP (PH_pkt_hdr.h)
101  PH_PRI_SEQUENCE_SECOND_PKT_IN_GROUP (PH_pkt_hdr.h)
102  PH_MOD_SOURCE_ID_CAL_TYPE_BLACKBODY_SOURCE (PH_pkt_hdr.h)
103  PH_MOD_SOURCE_ID_CAL_TYPE_SOLAR_DIFFUSER_SOURCE (PH_pkt_hdr.h)
104  PH_MOD_SOURCE_ID_CAL_TYPE_SPACE_SOURCE (PH_pkt_hdr.h)
105  PH_MOD_SOURCE_ID_CAL_TYPE_SRCA_CAL_SOURCE (PH_pkt_hdr.h)
106  PH_SEC_PKT_TYPE_DAY_GROUP (PH_pkt_hdr.h)
107  PH_SEC_PKT_TYPE_ENG1_GROUP (PH_pkt_hdr.h)
108  PH_SEC_PKT_TYPE_ENG2_GROUP (PH_pkt_hdr.h)
109  PH_SEC_PKT_TYPE_NIGHT_GROUP (PH_pkt_hdr.h)
110  SC_PIXEL_QUALITY_DATA_t (SC_scan.h)
111  SC_SCAN_DATA_t (SC_scan.h)
112 
113 Called By:
114  process_a_granule
115 
116 Routines Called:
117  initialize_scan
118  unpack_packet_contents
119  check_checksum
120  put_pkt_cont_in_scan
121  accumulate_failed_packets
122  update_pixel_qual_data
123  update_scan_metadata
124  process_eng_packet
125  process_a_packet
126  finalize_scan_metadata
127  finalize_pixel_qual_data
128  compute_SD_start_time
129  log_fmt_msg
130  packet_of_scan
131 
132 !Revision History:
133 $Log: process_a_scan.c,v $
134 Revision 6.1 2014/11/13 19:05:06 jkuyper
135 Simplified error message to reduce log file size.
136 
137 Revision 4.3 2003/04/07 17:32:11 kuyper
138 Fixed some comments.
139 
140 Revision 4.2 2003/03/10 23:19:29 kuyper
141 Cleanup, standardization for ease of reading.
142 
143 James Kuyper Jr. (kuyper@saicmodis.com)
144 
145 Revision 2.4 2001/01/05
146 John Seaton (seaton@ltpmail.gsfc.nasa.gov)
147 Added code to correctly count packets in a scan
148 
149 Revision 2.3 2000/07/05
150 John Seaton/SAIC/GSC (seaton@ltpmail.gsfc.nasa.gov)
151 Added code to fix split scan problem. DDTS MODx101733
152 
153 Revision 2.2 1997/09/05 16:45 EDT
154 Timi Adelekan/SAIC/GSC (adelekan@ltpmail.gsfc.nasa.gov)
155 Originated Code.
156 
157 Revision 2.1 1997/08/27
158 Tom Johnson (johnson@ltpmail.gsfc.nasa.gov)
159 Incorporate PDL walkthru comments
160 
161 Revision 2.0 1997/07/24 11:55 EDT
162 Timi Adelekan/SAIC/GSC (adelekan@ltpmail.gsfc.nasa.gov)
163 Created Module for version 2 from guideline of version 1
164 code.
165 
166 Revision 1.0 1997/06/18 16:40 EDT
167 Timi Adelekan/SAIC/GSC (adelekan@ltpmail.gsfc.nasa.gov)
168 Baseline from Version 1.
169 
170 !Team-unique Header:
171  This software is developed by the MODIS Science
172  Data Support Team (SDST) for the National Aeronautics
173  and Space Administration (NASA), Goddard Space Flight
174  Center (GSFC), under contract NAS5-32373.
175 
176 !References and Credits:
177  None
178 
179 !Design Notes:
180 
181  This routine assumes that the data contained in the input parameters
182  have been validated before this routine is executed. No validation of
183  data is performed in this routine.
184 
185 !END*************************************************************************
186 */
187 
188 {
189  /*************************************************************************/
190  /* */
191  /* Define and Initialize Local Variables */
192  /* */
193  /*************************************************************************/
194  /* */
195  /* initialize Status to MODIS_S_SUCCESS */
196  /* */
197  /* Set previous_scan_count to -1 */
198  /* */
199  /*************************************************************************/
200 
201  #define FIRSTPKT 0
202  #define SECONDPKT 1
203  #define MAX_CALIB_FRAMES 64
204  #define MAX_EARTH_FRAMES 1400
205 
206 
207  SC_SCAN_PROC_STATE_t scan_proc_state[5];
208 
209  char *routine = "process_a_scan";
210 
211  PGSt_SMF_status tempStatus = MODIS_S_SUCCESS;
212  /* SMF-style message returned by function */
213 
214  static PGSt_SMF_status Status = MODIS_S_SUCCESS;
215  /* SMF-style message returned by function */
216 
217  PGSt_SMF_boolean current_scan; /* current scan being processed */
218 
219  PGSt_double next_scan_start_time = 0.0;
220  /* Start time scan after current scan */
221  /* being processed */
222 
223  uint16 pkt_contents[PD_NUM_ELMTS_IN_DATA_FIELD_DAY_PKT +
225  /* unpacked packet data array */
226 
227  int16 qual_value = 0; /* The pixel quality value to be written */
228  /* to the pixel quality array */
229 
230  int16 num_packets=0; /* Number of packets read from L0 file */
231 
232  int8 previous_scan_count = -1;
233 
234  char msg[300];
235 
236  int i;
237 
238  int8 pktnum;
239 
240  /*************************************************************************/
241  /* */
242  /* define scan count arrays */
243  /* */
244  /*************************************************************************/
245 
246  int calib_cnt_SD[2][MAX_CALIB_FRAMES];
247  int calib_cnt_SRCA[2][MAX_CALIB_FRAMES];
248  int calib_cnt_BB[2][MAX_CALIB_FRAMES];
249  int calib_cnt_SV[2][MAX_CALIB_FRAMES];
250  int earth_cnt_EV[2][MAX_EARTH_FRAMES];
251  int eng_cnt_ENG[2][2];
252 
253  /*************************************************************************/
254  /* */
255  /* initialize all count arrays (eng, cal and Earth view) to zero */
256  /* */
257  /*************************************************************************/
258 
259  memset(calib_cnt_SD, 0, sizeof(calib_cnt_SD));
260  memset(calib_cnt_SRCA, 0, sizeof(calib_cnt_SRCA));
261  memset(calib_cnt_BB, 0, sizeof(calib_cnt_BB));
262  memset(calib_cnt_SV, 0, sizeof(calib_cnt_SV));
263  memset(earth_cnt_EV, 0, sizeof(earth_cnt_EV));
264  memset(eng_cnt_ENG, 0, sizeof(eng_cnt_ENG));
265 
266  initialize_scan (L1A_scan, scan_pixel, scan_meta);
267 
268  scan_meta->scan_num = ++*scan_number;
269 
270  unpack_packet_contents (pkt, pkt_header, pkt_contents);
271 
272  Status = check_checksum (*pkt_header, pkt_contents);
273 
274  if (Status != MODIS_S_SUCCESS) {
275  sprintf(msg, "Scan Number %d: Packet Number %d",
276  scan_meta->scan_num, num_packets);
278  }
279 
280  next_scan_start_time = *scan_time + *scan_rate;
281  current_scan = PGS_TRUE;
282 
283  do {
284  if (Status == MODIS_S_SUCCESS)
285  {
286  put_pkt_cont_in_scan (*pkt_header, pkt, pkt_contents, L1A_scan);
287  if ((pkt_header->pkt_type == PH_SEC_PKT_TYPE_ENG1_GROUP) ||
288  (pkt_header->pkt_type == PH_SEC_PKT_TYPE_ENG2_GROUP))
289  process_eng_packet (eng_data, *scan_number, pkt_header, pkt);
290 
291  /* To figure out if the packet is the first or second or only packet
292  * in the group. The default case or a '0' case is not needed. These
293  * checks happen when the packet header is verified.
294  */
295  switch (pkt_header->sequence_flag) {
296  case PH_PRI_SEQUENCE_FIRST_PKT_IN_GROUP: /* FALLTHROUGH */
298  pktnum = FIRSTPKT; break;
300  pktnum = SECONDPKT; break;
301  }
302 
304  {
305  switch (pkt_header->cal_type) {
307  calib_cnt_SD[pktnum][pkt_header->cal_frame_cnt -1] = 1;
308  break;
310  calib_cnt_SRCA[pktnum][pkt_header->cal_frame_cnt -1] = 1;
311  break;
313  calib_cnt_BB[pktnum][pkt_header->cal_frame_cnt -1] = 1;
314  break;
316  calib_cnt_SV[pktnum][pkt_header->cal_frame_cnt -1] = 1;
317  break;
318  /* No default case needed, only a 2 bit field */
319  }
320  }
321  else
322  {
323  switch (pkt_header->pkt_type)
324  {
325  case PH_SEC_PKT_TYPE_DAY_GROUP: /* FALLTHROUGH */
327  earth_cnt_EV[pktnum][pkt_header->earth_frame_cnt-1] = 1;
328  break;
330  eng_cnt_ENG[pktnum][FIRSTPKT] = 1;
331  break;
333  eng_cnt_ENG[pktnum][SECONDPKT] = 1;
334  break;
335  /* No default case needed, only a 2 bit field */
336  }
337  }
338  }
339  else
340  {
341  tempStatus = accumulate_failed_packets (pkt, *failed_pkts);
342  if (tempStatus != MODIS_S_SUCCESS)
343  log_fmt_msg (tempStatus, routine,
344  "Unable to add discarded packet to queue");
345  }
346 
347  num_packets++;
348 
349  update_scan_metadata (*pkt_header, Status, scan_meta, &qual_value);
350  update_pixel_qual_data (*pkt_header, qual_value, scan_pixel);
351  Status = process_a_packet (L0_file, pkt, pkt_header, pkt_contents);
352 
353  if (Status == MODIS_E_CHECKSUM_NOT_VALID)
354  {
355  sprintf(msg, "Scan Number = %d: Packet Number %d",
356  scan_meta->scan_num, num_packets);
358  }
359 
360  if (Status==MODIS_W_NO_MORE_PACKETS ||
361  Status==MODIS_F_PKT_READ_FAILED)
362  {
363  current_scan = PGS_FALSE;
364  log_fmt_msg (Status, routine,
365  "Unable to continue packet processing");
366  }
367  else
368  {
369  tempStatus = packet_of_scan(pkt_header, next_scan_start_time,
370  &previous_scan_count, scan_proc_state, L0_file);
371 
372  switch (tempStatus)
373  {
374  case MODIS_S_SUCCESS :
375  previous_scan_count = pkt_header->scan_cnt;
376  break;
377 
379  previous_scan_count = pkt_header->scan_cnt;
380  current_scan = FALSE;
381  break;
382 
384  if (Status != MODIS_E_CHECKSUM_NOT_VALID)
385  Status = tempStatus;
387  "%d: invalid scan count:%d", num_packets,
388  pkt_header->scan_cnt);
389  break;
390 
391  case MODIS_E_INV_PKT_TIME :
392  previous_scan_count = pkt_header->scan_cnt;
393  if (Status != MODIS_E_CHECKSUM_NOT_VALID)
394  Status = tempStatus;
395  sprintf(msg, "\nScan Number = %d: Packet Number %d",
396  scan_meta->scan_num,num_packets);
398  break;
399  }
400  }
401  } while (current_scan == PGS_TRUE); /* End_while */
402 
403  num_packets = 0;
404  num_packets = eng_cnt_ENG[FIRSTPKT][0] + eng_cnt_ENG[FIRSTPKT][1] +
405  eng_cnt_ENG[SECONDPKT][0] + eng_cnt_ENG[SECONDPKT][1];
406  for(i=0; i<MAX_CALIB_FRAMES; i++) {
407  num_packets += calib_cnt_SD[FIRSTPKT][i] +
408  calib_cnt_SD[SECONDPKT][i] +
409  calib_cnt_SRCA[FIRSTPKT][i] +
410  calib_cnt_SRCA[SECONDPKT][i] +
411  calib_cnt_BB[FIRSTPKT][i] +
412  calib_cnt_BB[SECONDPKT][i] +
413  calib_cnt_SV[FIRSTPKT][i] +
414  calib_cnt_SV[SECONDPKT][i];
415  }
416  for(i=0;i<MAX_EARTH_FRAMES; i++)
417  num_packets += earth_cnt_EV[FIRSTPKT][i] + earth_cnt_EV[SECONDPKT][i];
418 
419  finalize_scan_metadata (scan_meta, num_packets);
420  finalize_pixel_qual_data (scan_pixel, scan_meta);
421 
422  if (pkt_header->pkt_TAI_time > 0.0)
423  compute_SD_start_time (pkt_header, scan_time);
424 
425  return (Status);
426 
427 } /* End process_a_scan.c */
#define PD_NUM_ELMTS_IN_DATA_FIELD_DAY_PKT
Definition: PD_pkt_data.h:79
integer, parameter int16
Definition: cubeio.f90:3
void initialize_scan(SC_SCAN_DATA_t *L1A_scan, SC_PIXEL_QUALITY_DATA_t *scan_pixel, MD_SCAN_MET_t *scan_meta)
void put_pkt_cont_in_scan(PH_PACKET_HEADER_t pkt_header, PGSt_IO_L0_Packet *pkt, uint16 *pkt_contents, SC_SCAN_DATA_t *L1A_scan)
void compute_SD_start_time(PH_PACKET_HEADER_t *pkt_header, PGSt_double *SD_start_time)
#define MAX_EARTH_FRAMES
void unpack_packet_contents(PGSt_IO_L0_Packet *pkt, PH_PACKET_HEADER_t *pkt_header, uint16 *pkt_contents)
#define FALSE
Definition: rice.h:164
#define PH_PRI_SEQUENCE_SECOND_PKT_IN_GROUP
Definition: PH_pkt_hdr.h:128
#define MODIS_E_CHECKSUM_NOT_VALID
void finalize_pixel_qual_data(SC_PIXEL_QUALITY_DATA_t *scan_pixel, MD_SCAN_MET_t *scan_meta)
#define MAX_CALIB_FRAMES
#define PH_MOD_SOURCE_ID_CAL_TYPE_BLACKBODY_SOURCE
Definition: PH_pkt_hdr.h:202
#define PH_SEC_PKT_TYPE_ENG2_GROUP
Definition: PH_pkt_hdr.h:164
#define PH_MOD_SOURCE_ID_CAL_TYPE_SOLAR_DIFFUSER_SOURCE
Definition: PH_pkt_hdr.h:200
void update_pixel_qual_data(PH_PACKET_HEADER_t pkt_header, int16 qual_value, SC_PIXEL_QUALITY_DATA_t *scan_pix)
#define MODIS_E_SCANCNT_NOT_VALID
#define PH_MOD_SOURCE_ID_CAL_TYPE_SPACE_SOURCE
Definition: PH_pkt_hdr.h:203
PGSt_SMF_status process_a_packet(PGSt_IO_L0_VirtualDataSet *L0_file, PGSt_IO_L0_Packet *pkt, PH_PACKET_HEADER_t *packet_header, uint16 *packet_cont)
#define PH_NUM_12BIT_WORDS_IN_HEADER
Definition: PH_pkt_hdr.h:78
void log_fmt_msg(PGSt_SMF_status code, const char *routine, const char *msg_fmt,...)
Definition: log_fmt_msg.c:6
PGSt_SMF_status process_a_scan(int *scan_number, PGSt_IO_L0_Packet *pkt, PGSt_double *scan_rate, PGSt_double *scan_time, SC_SCAN_DATA_t *L1A_scan, MD_SCAN_MET_t *scan_meta, EN_VDATA_TYPE_t *eng_data, FP_QUEUE_t *failed_pkts, PH_PACKET_HEADER_t *pkt_header, SC_PIXEL_QUALITY_DATA_t *scan_pixel, PGSt_IO_L0_VirtualDataSet *L0_file)
#define PH_SEC_PKT_TYPE_NIGHT_GROUP
Definition: PH_pkt_hdr.h:162
#define FIRSTPKT
PGSt_SMF_status check_checksum(PH_PACKET_HEADER_t pkt_header, uint16 *pkt_contents)
Definition: check_checksum.c:9
#define MODIS_E_INV_PKT_TIME
PGSt_double pkt_TAI_time
Definition: PH_pkt_hdr.h:251
#define PH_MOD_SOURCE_ID_TYPE_FLAG_CAL
Definition: PH_pkt_hdr.h:189
#define PH_PRI_SEQUENCE_FIRST_PKT_IN_GROUP
Definition: PH_pkt_hdr.h:127
#define PH_PRI_SEQUENCE_ONLY_PKT_IN_GROUP
Definition: PH_pkt_hdr.h:129
#define PH_SEC_PKT_TYPE_DAY_GROUP
Definition: PH_pkt_hdr.h:161
PGSt_SMF_status packet_of_scan(PH_PACKET_HEADER_t *pkt_header, PGSt_double next_scan_start_time, int8 *previous_scan_count, SC_SCAN_PROC_STATE_t scan_proc_state[5], PGSt_IO_L0_VirtualDataSet *L0_file)
#define SECONDPKT
#define PH_SEC_PKT_TYPE_ENG1_GROUP
Definition: PH_pkt_hdr.h:163
void update_scan_metadata(PH_PACKET_HEADER_t packet_header, PGSt_SMF_status packetStatus, MD_SCAN_MET_t *scan_meta, int16 *qual_value)
#define PH_MOD_SOURCE_ID_CAL_TYPE_SRCA_CAL_SOURCE
Definition: PH_pkt_hdr.h:201
#define MODIS_S_SUCCESS
#define MODIS_M_PKT_NOT_IN_SCAN
#define MODIS_W_NO_MORE_PACKETS
void finalize_scan_metadata(MD_SCAN_MET_t *scan_meta, int16 num_packets)
#define MODIS_F_PKT_READ_FAILED
string msg
Definition: mapgen.py:227
void process_eng_packet(EN_VDATA_TYPE_t *eng_data, int scan_number, PH_PACKET_HEADER_t *pkt_header, PGSt_IO_L0_Packet *pkt)
int i
Definition: decode_rs.h:71
PGSt_SMF_status accumulate_failed_packets(PGSt_IO_L0_Packet *pkt, FP_QUEUE_t failed_pkts)