OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
getl1rec.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "l1.h"
4 #include "l12_parms.h"
5 #include "l12_proto.h"
6 
7 l1qstr l1que;
8 
9 /* --------------------------------------------------------- */
10 /* free_l1q() - free memory allocated to the l1 queue */
11 
12 /* --------------------------------------------------------- */
13 void free_l1q() {
14  int32_t i;
15  int32_t nq = l1que.nq;
16 
17  if (nq > 0)
18  for (i = 0; i < nq; i++)
19  free_l1(&(l1que.r[i]));
20 }
21 
22 
23 /* --------------------------------------------------------- */
24 /* alloc_l1q() - allocates a structure to hold nq level-1b */
25 /* records */
26 
27 /* --------------------------------------------------------- */
28 int32_t alloc_l1q(int32_t nq, filehandle *l1file, l1qstr *l1que) {
29  int32_t i;
30 
31  if (nq <= 0)
32  nq = 1;
33 
34  if (nq > NQMAX) {
35  printf("Queue size limit of %d exceeded: %d\n", NQMAX, nq);
36  return (0);
37  }
38 
39  /* force the que size to an odd number and init the center scan num */
40  l1que->nq = (nq / 2)*2 + 1;
41  l1que->cscan = -1;
42 
43  for (i = 0; i < nq; i++) {
44  if (alloc_l1(l1file, &(l1que->r[i])) == 0) {
45  fprintf(stderr,
46  "-E- %s line %d: Memory allocation failure at L1B scan %d.\n",
47  __FILE__, __LINE__, i);
48  return (0);
49  }
50  }
51 
52 
53  return (nq);
54 }
55 
56 
57 /* ---------------------------------------------------------------- */
58 /* Read one or more level 1 records from the file pointed to by the */
59 /* input file handle, as required to produce a queue of l1 records */
60 /* centered on the input scan number. */
61 
62 /* ---------------------------------------------------------------- */
63 int loadl1q(filehandle *l1file, int32_t iscan, int32_t dscan) {
64  int32_t nq = l1que.nq;
65  int32_t i, iq, recnum;
66  int32_t fscan = iscan - nq / 2;
67  int32_t lscan = iscan + nq / 2;
68  l1str tmp;
69 
70  if (iscan < 0 || iscan > l1file->nscan)
71  return (EXIT_FAILURE);
72  /* */
73  /* If the current queue center is not the preceeding scan, then */
74  /* this is the first call or we are reading out of sequence, so */
75  /* we will just load the entire queue from the input file. */
76  /* Otherwise, we just need to shift the queue and read a scan. */
77  /* */
78  if (l1que.cscan == -1 || l1que.cscan != iscan - dscan) {
79 
80  for (i = fscan; i <= lscan; i++) {
81  iq = i - fscan;
82  recnum = MIN(MAX(MAX(i, l1_input->sline - 1), 0), l1file->nscan - 1);
83  if (readl1(l1file, recnum, &l1que.r[iq]) != 0)
84  return (EXIT_FAILURE);
85  if (loadl1(l1file, &l1que.r[iq]) != 0)
86  return (EXIT_FAILURE);
87  }
88 
89  } else {
90 
91  /* */
92  /* We really just re-arrange the record data pointers */
93  /* */
94  memcpy(&tmp, &l1que.r[0], sizeof (l1str));
95  for (iq = 1; iq < nq; iq++)
96  memcpy(&l1que.r[iq - 1], &l1que.r[iq], sizeof (l1str));
97  memcpy(&l1que.r[nq - 1], &tmp, sizeof (l1str));
98 
99  /* */
100  /* Now read the next scan into the top of the queue */
101  /* */
102  recnum = MIN(MAX(MAX(iscan + nq / 2, l1_input->sline - 1), 0), l1file->nscan - 1);
103  if (readl1(l1file, recnum, &l1que.r[nq - 1]) != 0)
104  return (EXIT_FAILURE);
105  if (loadl1(l1file, &l1que.r[nq - 1]) != 0)
106  return (EXIT_FAILURE);
107  }
108 
109  /* We now have a queue of nq L1B records, centered on iscan */
110  l1que.cscan = iscan;
111 
112  return (EXIT_SUCCESS);
113 }
114 
115 
116 /* ---------------------------------------------------------------- */
117 
118 /* ---------------------------------------------------------------- */
119 int getl1rec(int32_t iscan, int32_t dscan, l1str *l1rec) {
120  static int32_t npix = -1;
121  static int32_t nq = -1;
122  l1str *crec;
123  int32_t ip;
124  filehandle* l1file = l1rec->l1file;
125 
126  if (nq == -1)
127  nq = MAX(input->fctl.nscan, NQMIN);
128 
129  crec = &l1que.r[nq / 2];
130 
131  /* */
132  /* if the queue size is inconsistent with the filter scan count */
133  /* then this must be the first time through, so allocate the q. */
134  /* */
135  if (l1que.nq != nq || npix != l1file->npix) {
136 
137  npix = l1file->npix;
138 
139  /* Free any previously allocated queue */
140  free_l1q();
141 
142  /* Allocate a fresh queue */
143  if (alloc_l1q(nq, l1file, &l1que) == 0) {
144  printf("-E- %s: Unable to allocate L1B record queue.\n",
145  __FILE__);
146  return (EXIT_FAILURE);
147  }
148  }
149 
150  /* Ensure that the queue is centered on iscan */
151  if (loadl1q(l1file, iscan, dscan) != 0) {
152  printf("-E- %s %d: Error reading %s at scan %d.\n",
153  __FILE__, __LINE__, l1file->name, iscan);
154  return (EXIT_FAILURE);
155  }
156 
157  /* Now make a copy of the center queue record. */
158  cpl1rec(l1rec, crec);
159 
160  /* And replace data with smoothed values, if desired. */
161  if (input->fctl.nfilt > 0) {
162  filter(&input->fctl, &l1que, l1rec, dscan);
163  setflagbits_l1(0, l1rec, -1);
164  setflagbits_l1(1, l1rec, -1);
165  for (ip = 0; ip < npix; ip++)
166  l1_mask_set(l1rec, ip);
167  }
168 
169  /* Reset scan-specific private data */
171 
172  return (EXIT_SUCCESS);
173 }
int32 l1file(int32 sdfid, int32 *nsamp, int32 *nscans, int16 *dtynum)
Definition: l1stat_chk.c:586
#define MAX(A, B)
Definition: swl0_utils.h:26
#define MIN(x, y)
Definition: rice.h:169
int readl1(filehandle *l1file, int32_t recnum, l1str *l1rec)
Definition: l1_io.c:400
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
read l1rec
#define NQMIN
Definition: l12_parms.h:11
#define NQMAX
Definition: l12_parms.h:12
void cpl1rec(l1str *dest, l1str *src)
Definition: cpl1rec.c:6
int32_t alloc_l1q(int32_t nq, filehandle *l1file, l1qstr *l1que)
Definition: getl1rec.c:28
void l1_mask_set(l1str *l1rec, int32_t ip)
Definition: setflags_l1.c:3
instr * input
l1qstr l1que
Definition: getl1rec.c:7
int loadl1q(filehandle *l1file, int32_t iscan, int32_t dscan)
Definition: getl1rec.c:63
data_t tmp
Definition: decode_rs.h:74
read recnum
l1_input_t * l1_input
Definition: l1_options.c:9
INT32 getl1rec(INT16 sceneFrameNum, swl0scene *scene, swl0ctl *l0ctl, input_sType navinp[], navblk_sType navblk[], tilt_states_sType *tiltblk, swl1rec l1rec[])
Definition: getl1rec.c:45
void filter(fctlstr *fctl, l1qstr *l1que, l1str *l1rec, int32_t dscan)
Definition: filter.c:1396
void free_l1(l1str *l1rec)
Definition: alloc_l1.c:6
int32_t iscan
int32_t alloc_l1(filehandle *l1file, l1str *l1rec)
Definition: alloc_l1.c:15
void setflagbits_l1(int level, l1str *l1rec, int32_t ipix)
Definition: setflags_l1.c:129
void free_l1q()
Definition: getl1rec.c:13
int loadl1(filehandle *l1file, l1str *l1rec)
Definition: loadl1.c:201
int i
Definition: decode_rs.h:71
int npix
Definition: get_cmp.c:27
void seawater_set(l1str *l1rec)
Definition: seawater_get.c:8