OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
viirs_ext.c
Go to the documentation of this file.
1 #include "viirs_sim_sdr.h"
2 #include <string.h>
3 #include <stdlib.h>
4 
5 int viirs_ext(ctl_struc *ctl, in_rec_struc *in_rec)
6 /*-----------------------------------------------------------------------------
7  Program: viirs_ext.c
8 
9  Description: Add the VIIRS electrical crosstalk artifact to the simulated
10  dn field (derived from the radiance field)
11 
12  Arguments:
13  Type Name I/O Description
14  ---- ---- --- -----------
15  ctl_struc * ctl I input controls
16  in_rec_struc * in_rec I/O controls for input record reading
17 
18  Modification history:
19 
20  W. Robinson, SAIC 18 Jan 2011 Original development
21 
22 ----------------------------------------------------------------------------*/ {
23  static int ext_init = 0;
24  static float *dn_tmp[MAX_BND];
25  static double *coeff;
26  float dn_mod;
27  int nsgain, nsbnd, nsdet, nrgain, nrbnd, nrdet, nrpix, nrlin, nspix;
28  int irbnd, irlin, irpix, ispix, isdet, isbnd, isgain, irgain, irdet;
29  int reg_off, nval;
30  int16_t nsfgain, nsfbnd, nsfdet, nrfgain, nrfbnd, nrfdet;
31  h5io_str ext_fid;
32  char tstr[500];
33  /* registration offset for the re-ordered sender bands:
34  (where is band M? relative to M1?)
35  M1 M2 M3 M4 M5 M6 M7 I1 I2 */
36  static int snd_off[] = {0, -3, -9, -6, -18, -21, -15, -12, -14};
37  /*
38  * The storage of the info for the gain state in the ext corff array is
39  * in the order ( low, high ) while in the in_rec->gain_bit it is
40  * ( high, low ), so this will translate that assignment
41  */
42  static int gain_lut[] = {0, 1};
43 
44  nrlin = in_rec->ndet_scan;
45  nrpix = in_rec->npix;
46  nspix = nrpix;
47  nrbnd = N_VNIR_BND;
48  nrdet = NDET;
49  nrgain = 2;
50  nsgain = 2;
51  nsbnd = 7;
52  nsdet = NDET;
53  /*
54  * we could put in code to de-allocate the dn_tmp and coeff
55  * if in_rec->lat = NULL as scan_cvt.c does. You'd call this, when
56  * appropriate in fin_sdr.c
57  */
58  if (in_rec->lat == NULL) {
59  free(coeff);
60  for (irbnd = 0; irbnd < nrbnd; irbnd++)
61  free(dn_tmp[irbnd]);
62  return 0;
63  }
64  /*
65  * do initialization
66  */
67  if (ext_init == 0) {
68  ext_init = 1;
69 
70  printf("%s, %d: Entering routine viirs_ext\n", __FILE__, __LINE__);
71  printf("ext_coef = %s\n", ctl->ext_coeff_file);
72  /*
73  * allocate temporary, un-modified dn storage for the dn scan
74  */
75  for (irbnd = 0; irbnd < nrbnd; irbnd++)
76  if ((dn_tmp[irbnd] = (float *) malloc(nrlin * nrpix * sizeof ( float)))
77  == NULL) {
78  printf("%s, %d: Unable to allocate dn tmp storage\n", __FILE__,
79  __LINE__);
80  return 1;
81  }
82  /*
83  * Get the coefficients for the crostalk
84  */
85  nval = nsgain * nrgain * nsbnd * nsdet * nrbnd * nrdet;
86  if ((coeff = (double *) calloc(nval, sizeof ( double))) == NULL) {
87  printf("%s, %d: Unable to allocate electronic crosstalk storage\n",
88  __FILE__, __LINE__);
89  return 1;
90  }
91  if (strcmp(ctl->ext_coeff_file, "Unspecified") == 0) {
92  /* default coeffs of 0 = no crosstalk, are used if ext coeff
93  * file is unspecified
94  * perhaps some code will be needed here, so have space here
95  */
96  } else {
97  /* open file */
98  if (h5io_openr(ctl->ext_coeff_file, 0, &ext_fid) != 0) {
99  printf("%s, %d - could not open HDF 5 Electrical crosstalk file: %s\n",
100  __FILE__, __LINE__, ctl->ext_coeff_file);
101  return 1;
102  }
103  /* read info on storage size, note that most sizes are a fixed value */
104  strcpy(tstr, "number of sender gains");
105  if (h5io_rd_attr(&ext_fid, tstr, &nsfgain) != 0) {
106  printf("%s, %d - failed to read Electrical Xtalk attrib %s\n",
107  __FILE__, __LINE__, tstr);
108  return 1;
109  }
110  strcpy(tstr, "number of receiver gains");
111  if (h5io_rd_attr(&ext_fid, tstr, &nrfgain) != 0) {
112  printf("%s, %d - failed to read Electrical Xtalk attrib %s\n",
113  __FILE__, __LINE__, tstr);
114  return 1;
115  }
116  strcpy(tstr, "number of sender bands");
117  if (h5io_rd_attr(&ext_fid, tstr, &nsfbnd) != 0) {
118  printf("%s, %d - failed to read Electrical Xtalk attrib %s\n",
119  __FILE__, __LINE__, tstr);
120  return 1;
121  }
122  strcpy(tstr, "number of sender detectors");
123  if (h5io_rd_attr(&ext_fid, tstr, &nsfdet) != 0) {
124  printf("%s, %d - failed to read Electrical Xtalk attrib %s\n",
125  __FILE__, __LINE__, tstr);
126  return 1;
127  }
128  strcpy(tstr, "number of receiver bands");
129  if (h5io_rd_attr(&ext_fid, tstr, &nrfbnd) != 0) {
130  printf("%s, %d - failed to read Electrical Xtalk attrib %s\n",
131  __FILE__, __LINE__, tstr);
132  return 1;
133  }
134  strcpy(tstr, "number of receiver detectors");
135  if (h5io_rd_attr(&ext_fid, tstr, &nrfdet) != 0) {
136  printf("%s, %d - failed to read Electrical Xtalk attrib %s\n",
137  __FILE__, __LINE__, tstr);
138  return 1;
139  }
140  /*
141  * check dimensions out to match
142  */
143  if (nsfgain != nsgain) {
144  printf(
145  "%s, %d - Electrical crosstalk # sender gains: %d != expected #: %d\n",
146  __FILE__, __LINE__, nsfgain, nsgain);
147  return 1;
148  }
149  if (nrfgain != nrgain) {
150  printf(
151  "%s, %d - Electrical crosstalk # receiver gains: %d != expected #: %d\n",
152  __FILE__, __LINE__, nrfgain, nrgain);
153  return 1;
154  }
155  if (nsfbnd != nsbnd) {
156  printf(
157  "%s, %d - Electrical crosstalk # sender bands: %d != expected #: %d\n",
158  __FILE__, __LINE__, nsfbnd, nsbnd);
159  return 1;
160  }
161  if (nrfbnd != nrbnd) {
162  printf(
163  "%s, %d - Electrical crosstalk # receiver bands: %d != expected #: %d\n",
164  __FILE__, __LINE__, nrfbnd, nrbnd);
165  return 1;
166  }
167  if (nsfdet != nsdet) {
168  printf(
169  "%s, %d - Electrical crosstalk # sender detectors: %d != expected #: %d\n",
170  __FILE__, __LINE__, nsfdet, nsdet);
171  return 1;
172  }
173  if (nrfdet != nrdet) {
174  printf(
175  "%s, %d - Electrical crosstalk # receiver detectors: %d != expected #: %d\n",
176  __FILE__, __LINE__, nrfdet, nrdet);
177  return 1;
178  }
179  /*
180  * read data to the coefficient storage
181  */
182  if (h5io_grab_ds(&ext_fid, "electronic crosstalk coefficients",
183  (void *) coeff) != 0) {
184  printf("%s, %d: Unable to read electronic crosstalk coefficients\n",
185  __FILE__, __LINE__);
186  return 1;
187  }
188 
189  /* close the coeff file */
190  if (h5io_close(&ext_fid) != 0) {
191  printf(
192  "%s, %d: Unable to close electronic crosstalk coefficient file\n",
193  __FILE__, __LINE__);
194  return 1;
195  }
196  }
197  }
198  /*
199  * fill temporary storage with the scan dn
200  * Note that all the dn data is being used - no track margin cutoff
201  */
202  nval = nrpix * nrlin * sizeof ( float);
203  for (irbnd = 0; irbnd < nrbnd; irbnd++)
204  memcpy(dn_tmp[irbnd], in_rec->dn[irbnd], nval);
205  /*
206  * loop through the lines, pixels, and bands in the scan
207  */
208  for (irlin = 0; irlin < nrlin; irlin++) {
209  irdet = irlin - in_rec->margin[0];
210  if (irdet < 0) irdet = 0;
211  if (irdet >= NDET) irdet = NDET - 1;
212  for (irpix = 0; irpix < in_rec->npix; irpix++) {
213  for (irbnd = 0; irbnd < nrbnd; irbnd++) {
214  /*
215  * zero out the dn modification
216  * and accumulate all the dn modifications from the senders
217  */
218  dn_mod = 0.;
219  for (isbnd = 0; isbnd < nsbnd; isbnd++) {
220  reg_off = snd_off[isbnd] - snd_off[irbnd];
221  ispix = irpix + reg_off;
222  if ((ispix >= 0) && (ispix < in_rec->npix)) {
223  for (isdet = 0; isdet < nsdet; isdet++) {
224  isgain = gain_lut[ *(in_rec-> gain_bit[isbnd] +
225  ispix + nspix * isdet) ];
226  irgain = gain_lut[ *(in_rec-> gain_bit[irbnd] +
227  irpix + nspix * irlin) ];
228 
229  dn_mod += *(dn_tmp[isbnd] + ispix + isdet * nspix) *
230  *(coeff + isgain + nsgain * (irgain + nrgain *
231  (isbnd + nsbnd * (isdet + nsdet *
232  (irbnd + nrbnd * irdet)))));
233  }
234  }
235  }
236  /*
237  * add the modified dn to the dn for the receiver pix, lin, band
238  */
239  *(in_rec->dn[irbnd] + irpix + nrpix * irlin) =
240  *(dn_tmp[irbnd] + irpix + nrpix * irlin) + dn_mod;
241  }
242  }
243  }
244  /*
245  * scan is modified, return
246  */
247  return 0;
248 }
int h5io_openr(char *file, int opt, h5io_str *id)
Definition: h5io.c:4
#define NULL
Definition: decode_rs.h:63
#define MAX_BND
Definition: viirs_sim_sdr.h:34
int h5io_close(h5io_str *id)
Definition: h5io.c:115
int h5io_rd_attr(h5io_str *id, char *attr_name, void *data)
Definition: h5io.c:412
int viirs_ext(ctl_struc *ctl, in_rec_struc *in_rec)
Definition: viirs_ext.c:5
int h5io_grab_ds(h5io_str *id, char *path_name, void *data)
Definition: h5io.c:1347
#define NDET
Definition: polcor.c:13
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
#define N_VNIR_BND
Definition: viirs_sim_sdr.h:31