OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
viirs_straylt.c
Go to the documentation of this file.
1 #include "viirs_sim_sdr.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6 
7 int viirs_straylt(ctl_struc *ctl, in_rec_struc *in_rec, int stray_stop)
8 /*-----------------------------------------------------------------------------
9  Program: viirs_straylt.c
10 
11  Description: Add the VIIRS stray light artifact to the simulated
12  radiance field
13 
14  Arguments:
15  Type Name I/O Description
16  ---- ---- --- -----------
17  ctl_struc * ctl I input controls
18  in_rec_struc * in_rec I/O controls for input record reading
19  int stray_stop I if 1, remove storage used for stray
20  light processing - do at program end
21 
22  Modification history:
23 
24  W. Robinson, SAIC 3 June 2011 Original development
25 
26 ----------------------------------------------------------------------------*/ {
27  int ibnd, ilin, ipix, idet, lpsf, ppsf, pdat;
28  static int nbnd_s, npix, nlin, npix_s, nlin_s, lin_margin;
29  static vir_straylt_struc stlt;
30  static int stray_init = 0;
31  static float *bnd_store; /* storage for the unchanged radiance */
32  float *psf_bd; /* a pointer to convol array to use for a band, detector */
33  float sum_psf, sum_rad, psf_val;
34 
35  /*
36  * close down if stop switch is set
37  */
38  if (stray_stop == 1) {
39  free(stlt.psf);
40  free(bnd_store);
41  printf("%s: Shutting down the stray light artifact addition\n",
42  __FILE__);
43  } else {
44  /*
45  * do initialization
46  */
47  if (stray_init == 0) {
48  stray_init = 1;
49 
50  printf("%s, %d: Entering routine viirs_straylt\n", __FILE__, __LINE__);
51  printf("stray_tbl = %s\n", ctl->stray_tbl);
52  /*
53  * read in the psf = stray light convol array, and make a
54  * holding array for 1 band of a scan
55  */
56  if (viirs_straylt_rd(ctl->stray_tbl, &stlt) != 0)
57  return 1;
58 
59  nbnd_s = stlt.nbands; /* assume bands in psf go M1 - Mn bands */
60  nlin = in_rec->ndet_scan;
61  npix = in_rec->npix;
62  lin_margin = in_rec->margin[0];
63  npix_s = stlt.nsamp;
64  nlin_s = stlt.ndet;
65 
66  if ((bnd_store = (float *) malloc(nlin * npix * sizeof ( float)))
67  == NULL) {
68  printf(
69  "%s, %d: Unable to allocate band storage for stray light artifact\n",
70  __FILE__, __LINE__);
71  return 1;
72  }
73  }
74 
75  /*
76  * go through the bands, lines, and pixels of scan to apply stray light to
77  */
78  for (ibnd = 0; ibnd < nbnd_s; ibnd++) {
79  /*
80  * Keep a pristine copy of the original band's data
81  */
82  memcpy(bnd_store, in_rec->bnd_lt[ibnd], nlin * npix * sizeof (float));
83 
84  for (idet = 0; idet < nlin_s; idet++) {
85  ilin = idet + lin_margin; /* line in in_rec */
86  /*
87  * the convol array can now be selected from the table for
88  * this band and detector
89  */
90  psf_bd = stlt.psf + (npix_s * nlin_s) * (ibnd + nbnd_s * idet);
91 
92  for (ipix = 0; ipix < npix; ipix++) {
93  /*
94  * we are at the point of collecting the correction for a sample
95  * apply it only if that value is good
96  */
97  if (*(in_rec->bnd_q[ibnd] + ipix + npix * ilin) == 0) {
98  /*
99  * This is where the convolution begins, but only using
100  * good radiance values
101  */
102  sum_psf = 0.;
103  sum_rad = 0.;
104 
105  for (lpsf = 0; lpsf < nlin_s; lpsf++) {
106  for (ppsf = 0; ppsf < npix_s; ppsf++) {
107  /*
108  * compute te pixel of the radiance to apply convol to,
109  * account for the center sample of the psf array
110  */
111  pdat = ipix + ppsf - stlt.csamp;
112  if ((pdat >= 0) && (pdat < npix)) {
113  /*
114  * if the quality at this point is good, add the info
115  */
116  if (*(in_rec->bnd_q[ibnd] + ipix + ilin * npix) == 0) {
117  psf_val = *(psf_bd + ppsf + npix_s * lpsf);
118  sum_psf += psf_val;
119  sum_rad += psf_val * *(bnd_store + pdat +
120  (lpsf + lin_margin) * npix);
121  }
122  }
123  }
124  }
125  /*
126  * make the artifact-modified radiance value
127  */
128  *(in_rec->bnd_lt[ibnd] + ipix + ilin * npix) = sum_rad / sum_psf;
129  }
130  }
131  }
132  }
133  }
134  /*
135  * and end
136  */
137  return 0;
138 }
139 
140 int viirs_straylt_rd(char *file, vir_straylt_struc *stlt)
141 /*-----------------------------------------------------------------------------
142  Routine: viirs_straylt_rd
143 
144  Description: read the stray light artifact coefficients
145 
146  Returns type: int - 0 if good, else I/O error
147 
148  Arguments:
149  Type Name I/O Description
150  ---- ---- --- -----------
151  char * file I stray light table psf file name
152  vir_straylt_struc * gain O structure with stray light
153  coefficients and description of it
154  more descrip in vir_straylt_struc
155  descrip in viirs_sim_sdr.h
156 
157  Modification history:
158 
159  W. Robinson, SAIC 3 June 2011 Original development
160 
161 ----------------------------------------------------------------------------*/ {
162  int nrec_det = 16, ndet = 16, nbands, nsamp;
163 
164  h5io_str fid;
165 
166  /*
167  * open the stray light table file and get the size information
168  */
169  if (strcmp(file, "Unspecified") != 0) {
170  if (h5io_openr(file, 0, &fid) != 0) {
171  printf("%s, %d, Unable to open stray light table file: \n%s\n",
172  __FILE__, __LINE__, file);
173  return 1;
174  }
175  if (h5io_rd_attr(&fid, "nbands", (void *) &(stlt->nbands)) != 0) {
176  printf(
177  "%s, %d, Unable to read nbands attr from stray light file: \n%s\n",
178  __FILE__, __LINE__, file);
179  return 1;
180  }
181  if (h5io_rd_attr(&fid, "nrec_det", (void *) &(stlt->nrec_det)) != 0) {
182  printf(
183  "%s, %d, Unable to read nrec_det attr from stray light file: \n%s\n",
184  __FILE__, __LINE__, file);
185  return 1;
186  }
187  if (h5io_rd_attr(&fid, "nsamp", (void *) &(stlt->nsamp)) != 0) {
188  printf(
189  "%s, %d, Unable to read nsamp attr from stray light file: \n%s\n",
190  __FILE__, __LINE__, file);
191  return 1;
192  }
193  if (h5io_rd_attr(&fid, "ndet", (void *) &(stlt->ndet)) != 0) {
194  printf(
195  "%s, %d, Unable to read ndet attr from stray light file: \n%s\n",
196  __FILE__, __LINE__, file);
197  return 1;
198  }
199  if (h5io_rd_attr(&fid, "csamp", (void *) &(stlt->csamp)) != 0) {
200  printf(
201  "%s, %d, Unable to read csamp attr from stray light file: \n%s\n",
202  __FILE__, __LINE__, file);
203  return 1;
204  }
205 
206  nbands = stlt->nbands;
207  nsamp = stlt->nsamp;
208 
209  if ((stlt->nrec_det != nrec_det) || (stlt->ndet != ndet)) {
210  printf(
211  "%s, %d, Unexpected stray light array dimensions found in file: \n%s\n",
212  __FILE__, __LINE__, file);
213  printf(" nrec_det: %d, expected: %d\n", stlt->nrec_det, nrec_det);
214  printf(" ndet: %d, expected: %d\n", stlt->ndet, ndet);
215  return 1;
216  }
217  } else {
218  printf("%s, %d: Unexpectedly found Unspecified file stray light table\n",
219  __FILE__, __LINE__);
220  return 1;
221  }
222  /*
223  * allocate space for the psf array and read it in
224  */
225  if ((stlt->psf = (float *)
226  malloc(nbands * nrec_det * nsamp * ndet * sizeof ( float))) == NULL) {
227  printf("%s, %d: Error allocating the stlt->psf\n", __FILE__, __LINE__);
228  return 1;
229  }
230 
231  if (h5io_grab_ds(&fid, "psf", (void *) stlt->psf) != 0) {
232  printf(
233  "%s, %d, Unable to read psf dataset from stray light table file: \n%s\n",
234  __FILE__, __LINE__, file);
235  return 1;
236  }
237 
238  if (h5io_close(&fid) != 0) {
239  printf("%s, %d, Error closing the stray light table:\n%s\n", __FILE__,
240  __LINE__, file);
241  return 1;
242  }
243  /*
244  * return the psf and its description
245  */
246  return 0;
247 }
int h5io_openr(char *file, int opt, h5io_str *id)
Definition: h5io.c:4
#define NULL
Definition: decode_rs.h:63
int h5io_close(h5io_str *id)
Definition: h5io.c:115
int viirs_straylt_rd(char *file, vir_straylt_struc *stlt)
int nlin
Definition: get_cmp.c:28
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed file
Definition: HISTORY.txt:413
int h5io_rd_attr(h5io_str *id, char *attr_name, void *data)
Definition: h5io.c:412
int h5io_grab_ds(h5io_str *id, char *path_name, void *data)
Definition: h5io.c:1347
int32_t nbands
int viirs_straylt(ctl_struc *ctl, in_rec_struc *in_rec, int stray_stop)
Definition: viirs_straylt.c:7
int npix
Definition: get_cmp.c:27