OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
extr_bits.c
Go to the documentation of this file.
1 #include "PGS_MODIS_35005.h"
2 #include "PGS_SMF.h"
3 #include "hdfi.h"
4 #include "L1A_prototype.h"
5 
6 uint32 extr_bits (uint8 *a,
7  int start_bit,
8  int start_byte,
9  int num_bits )
10 
11 /*
12 !C************************************************************************
13 
14 !Description: This function will extract up to 32 bits from a uint8
15  (byte) array. This routine assumes that bits are numbered
16  from MSB to LSB in each byte, and that bit streams connect
17  from bit 7 in byte b to bit 0 in byte b + 1.
18 
19  The routine will not work for streams larger than 32 bits.
20  It won't crash in that case; it just won't return a valid
21  value.
22 
23 !Input Parameters:
24  int8 *a ** array from which bits are to be **
25  ** extracted **
26  int start_byte ** byte within array that contains **
27  ** the first bit to be extracted **
28  int start_bit ** bit within start_byte at witch **
29  ** extraction will start **
30  int num_bits ** number of bits to be extracted **
31 
32 !Output Parameters:
33  None
34 
35 Return Values:
36  Bits extracted from *a, or 0 (zero) if extract cannot
37  be performed
38 
39 Externally Defined:
40  PGSt_SMF_status (PGS_SMF.h)
41  MODIS_E_TOO_MANY_BITS (PGS_MODIS_35005.h)
42  MODIS_S_SUCCESS (PGS_MODIS_35005.h)
43  uint8 (hdfi.h)
44  uint32 (hdfi.h)
45  pkt_num (ext var to hold packet number)
46 
47 Called By:
48  unpack_primary_header
49  unpack_secondary_header
50  unpack_MODIS_header
51  unpack_packet_contents
52 
53 Routines Called:
54  log_fmt_msg
55 
56 !Revision History:
57  Revision 1.0 1997/09/15 14:35
58  Tom Johnson/GSC (johnson@ltpmail.gsfc.nasa.gov)
59  Developed the code for the module based on existing
60  code from beta version and version 1
61 
62 !Team-unique Header:
63  This software is developed by the MODIS Science
64  Data Support Team (SDST) for the National Aeronautics
65  and Space Administration (NASA), Goddard Space Flight
66  Center (GSFC), under contract NAS5-32373.
67 
68 !References and Credits:
69  None
70 
71 !Design Notes:
72  None
73 
74 !END***********************************************************************
75 */
76 
77 {
78  /***************************************************************************/
79  /* */
80  /* Declare and Initialize Local Variables */
81  /* */
82  /***************************************************************************/
83 
84  char msg[300]; /* amplifying message */
85  int i; /* loop counter */
86  uint32 ret; /* Variable holding extracted bits */
87  int end_bit; /* Calculated last bit to be extracted */
88  int end_byte; /* Calculated last byte from which bits */
89  /* will be extracted */
90 
91  char *routine = "extr_bits";
92 
93  /***************************************************************************/
94 
95 
96  /***************************************************************************/
97  /* */
98  /* Set routine to "extr_bits" (done during declaration) */
99  /* */
100  /***************************************************************************/
101 
102  /***************************************************************************/
103  /* */
104  /* Set ret to 0 */
105  /* Set end_bit to 0 */
106  /* Set end_byte to 0 */
107  /* */
108  /***************************************************************************/
109 
110  ret = 0;
111  end_bit = 0;
112  end_byte = 0;
113 
114  /***************************************************************************/
115  /* */
116  /* IF num_bits is greater than 32 */
117  /* THEN */
118  /* Set ret to 0 */
119  /* Set msg to "Number of bits to be extracted: <number> */
120  /* CALL log_fmt_msg to report that extraction can't be done */
121  /* INPUT: MODIS_E_TOO_MANY_BITS, routine, msg */
122  /* OUTPUT: None */
123  /* RETURN: None */
124  /* */
125  /***************************************************************************/
126 
127  if (num_bits > 32)
128  {
130  ret = 0;
131  sprintf (msg, "Number of bits to be extracted: %d", num_bits);
133  }
134 
135 
136  /***************************************************************************/
137  /* */
138  /* ELSE */
139  /* Set end_bit to start_bit + num_bits - 1 */
140  /* Set end_byte to start_byte + end_bit/8 */
141  /* Set end_bit to end_bit%8 */
142  /* */
143  /* Set ret to head_buff[start_byte] & (Oxff >> start_bit) */
144  /* */
145  /* FOR i from start_byte + 1 to end_byte - 1 */
146  /* Set ret to (ret << 8) | head_buff[i] */
147  /* ENDFOR */
148  /* */
149  /* IF start_byte is not equal to end_byte */
150  /* THEN */
151  /* Set ret to ((head_buff[end_byte] >> (7 - end_bit)) & Oxff) | */
152  /* (ret << (end_bit + 1)) */
153  /* ELSE */
154  /* Set ret to ret >> (7 - end_bit) */
155  /* ENDIF */
156  /* ENDIF */
157  /* */
158  /***************************************************************************/
159 
160  else
161  {
162  end_bit = ( start_bit + num_bits - 1 );
163  end_byte = ( start_byte + (end_bit / 8) );
164  end_bit = ( end_bit % 8 );
165 
166  /*******************************************************/
168  /*******************************************************/
174  /*******************************************************/
175  ret = a[start_byte] & (0xff >> start_bit);
176 
177  /*******************************************************/
182  /*******************************************************/
183  for (i = (start_byte + 1); i < end_byte; i++)
184  ret = (ret << 8) | a[i];
185 
186  /*******************************************************/
189  if (start_byte != end_byte)
194  ret = ((a[end_byte] >> (7 - end_bit)) & 0xff) |
195  (ret << (end_bit + 1));
196  else
199  ret = ret >> (7 - end_bit);
200  /*******************************************************/
201  }
202 
203 
204  /***************************************************************************/
205  /* */
206  /* RETURN ret */
207  /* */
208  /***************************************************************************/
209 
210  return (ret);
211 
212 } /* End extr_bits */
void log_fmt_msg(PGSt_SMF_status code, const char *routine, const char *msg_fmt,...)
Definition: log_fmt_msg.c:6
uint32 extr_bits(uint8 *a, int start_bit, int start_byte, int num_bits)
Definition: extr_bits.c:6
string msg
Definition: mapgen.py:227
#define MODIS_E_TOO_MANY_BITS
int i
Definition: decode_rs.h:71
PGE01 indicating that PGE02 PGE01 V6 for and PGE01 V2 for MOD03 were used to produce the granule By convention adopted in all MODIS Terra PGE02 code versions are The fourth digit of the PGE02 version denotes the LUT version used to produce the granule The source of the metadata environment variable ProcessingCenter was changed from a QA LUT value to the Process Configuration A sign used in error in the second order term was changed to a
Definition: HISTORY.txt:424