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 void lt_agregat_ocis(l1str*l1rec) {
116  static int firstcall = 1;
117 
118  int i, j, ipb, k;
119  float *wave = l1rec->l1file->fwave;
120  int npix = l1rec->npix;
121  int nbands = l1rec->l1file->nbands;
122  float templt = 0;
123 
124  int ib325 = windex(325, wave, nbands); //5nm
125  int ib340 = windex(340, wave, nbands); //5nm
126  int ib665 = windex(665, wave, nbands); //10nm
127  int ib680 = windex(680, wave, nbands); //10nm
128  int ib750 = windex(750, wave, nbands); //10nm
129  int ib865 = windex(865, wave, nbands); //40nm
130 
131  static float *Lt_scan;
132 
133  if (firstcall) {
134  Lt_scan = (float*) malloc(npix * nbands * sizeof (float));
135  firstcall = 0;
136  }
137  memcpy(Lt_scan, l1rec->Lt, sizeof (float)*npix * nbands);
138 
139 
140  for (i = 0; i < npix; i++) {
141  ipb = i*nbands;
142  for (j = 0; j < nbands; j++) {
143  if (j == ib325 || j == ib340)
144  continue;
145  else if (j == ib865) {
146  templt = 0;
147  for (k = j - 4; k <= j + 4; k++)
148  templt += Lt_scan[ipb + k];
149  l1rec->Lt[ipb + j] = templt / 9.0;
150  } else if (j == ib665 || j == ib680 || j == ib750) {
151  l1rec->Lt[ipb + j] = (Lt_scan[ipb + j - 1] + Lt_scan[ipb + j]) / 2.0;
152  } else {
153  if (j ==0)
154  l1rec->Lt[ipb + j] = (2 * Lt_scan[ipb + j] + Lt_scan[ipb + j + 1]) / 3.0;
155  else if (j == nbands - 1)
156  l1rec->Lt[ipb + j] = (Lt_scan[ipb + j - 1] + 2 * Lt_scan[ipb + j]) / 3.0;
157  else
158  l1rec->Lt[ipb + j] = (Lt_scan[ipb + j - 1] + Lt_scan[ipb + j] + Lt_scan[ipb + j + 1]) / 3.0;
159  }
160  }
161  }
162 
163 }
164 
165 
166 /* ---------------------------------------------------------------- */
167 
168 /* ---------------------------------------------------------------- */
169 int getl1rec(int32_t iscan, int32_t dscan, l1str *l1rec) {
170  static int32_t npix = -1;
171  static int32_t nq = -1;
172  l1str *crec;
173  int32_t ip,iq;
174  filehandle* l1file = l1rec->l1file;
175  int nbands=l1file->nbands;
176 
177  if (nq == -1)
178  nq = MAX(input->fctl.nscan, NQMIN);
179 
180  crec = &l1que.r[nq / 2];
181 
182  /* */
183  /* if the queue size is inconsistent with the filter scan count */
184  /* then this must be the first time through, so allocate the q. */
185  /* */
186  if (l1que.nq != nq || npix != l1file->npix) {
187 
188  npix = l1file->npix;
189 
190  /* Free any previously allocated queue */
191  free_l1q();
192 
193  /* Allocate a fresh queue */
194  if (alloc_l1q(nq, l1file, &l1que) == 0) {
195  printf("-E- %s: Unable to allocate L1B record queue.\n",
196  __FILE__);
197  return (EXIT_FAILURE);
198  }
199 
200  for(iq=0;iq<nq;iq++)
201  l1que.r[iq].uncertainty=NULL;
202  if(l1rec->uncertainty){
203  for(iq=0;iq<nq;iq++){
204  l1que.r[iq].uncertainty= (uncertainty_t*) malloc(sizeof (uncertainty_t));
205  if (alloc_uncertainty(nbands, l1rec->uncertainty->nbands_ac, npix,l1que.r[iq].uncertainty) != 0) {
206  printf("-E- %s: Unable to allocate error record.\n",__FILE__);
207  exit(EXIT_FAILURE);
208  }
209  }
210  }
211  }
212 
213  /* Ensure that the queue is centered on iscan */
214  if (loadl1q(l1file, iscan, dscan) != 0) {
215  printf("-E- %s %d: Error reading %s at scan %d.\n",
216  __FILE__, __LINE__, l1file->name, iscan);
217  return (EXIT_FAILURE);
218  }
219 
220  /* Now make a copy of the center queue record. */
221  cpl1rec(l1rec, crec);
222 
223  /* And replace data with smoothed values, if desired. */
224  if (input->fctl.nfilt > 0) {
225  filter(&input->fctl, &l1que, l1rec, dscan);
226  setflagbits_l1(0, l1rec, -1);
227  setflagbits_l1(1, l1rec, -1);
228  for (ip = 0; ip < npix; ip++)
229  l1_mask_set(l1rec, ip);
230  }
231 
232  //lt_agregat_ocis(l1rec);
233 
234  if(l1rec->uncertainty){
235 
236  init_uncertainty(l1rec->uncertainty,1);
237 
238  /* calculate the error in Lt and Lr */
239  //set_error_input(l1rec);
240 
242 
243  }
244 
245  /* Reset scan-specific private data */
247 
248  return (EXIT_SUCCESS);
249 }
int32 l1file(int32 sdfid, int32 *nsamp, int32 *nscans, int16 *dtynum)
Definition: l1stat_chk.c:586
#define MAX(A, B)
Definition: swl0_utils.h:25
#define MIN(x, y)
Definition: rice.h:169
int readl1(filehandle *l1file, int32_t recnum, l1str *l1rec)
Definition: l1_io.c:439
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
int j
Definition: decode_rs.h:73
#define NULL
Definition: decode_rs.h:63
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
void lt_agregat_ocis(l1str *l1rec)
Definition: getl1rec.c:115
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 init_uncertainty(uncertainty_t *uncertainty, int ifscan)
Definition: uncertainty.c:177
void filter(fctlstr *fctl, l1qstr *l1que, l1str *l1rec, int32_t dscan)
Definition: filter.c:1398
void free_l1(l1str *l1rec)
Definition: alloc_l1.c:7
int32_t nbands
int32_t iscan
int windex(float wave, float twave[], int ntwave)
Definition: windex.c:73
float * get_uncertainty(l1str *l1rec)
int32_t alloc_l1(filehandle *l1file, l1str *l1rec)
Definition: alloc_l1.c:18
void setflagbits_l1(int level, l1str *l1rec, int32_t ipix)
Definition: setflags_l1.c:131
void free_l1q()
Definition: getl1rec.c:13
int loadl1(filehandle *l1file, l1str *l1rec)
Definition: loadl1.c:36
int alloc_uncertainty(int32_t nbands, int32_t nbands_ac, int32_t npix, uncertainty_t *uncertainty)
Definition: uncertainty.c:21
int i
Definition: decode_rs.h:71
int k
Definition: decode_rs.h:73
int npix
Definition: get_cmp.c:28
void seawater_set(l1str *l1rec)
Definition: seawater_get.c:8