OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
calibrate_viirs.c
Go to the documentation of this file.
1 
2 #include "nc4utils.h"
3 
12 int load_fcal_lut(char* calfile, int64_t UTC58usec, double ****ftable) {
13  int fileid, varid;
14  var_str_nc *vars;
15  size_t ntimes, nbands, ndets, nms;
16  size_t itime, iband, idet, ims;
17  int64_t *times = NULL;
18 
19  /* VIIRS-SDR-F-LUT:Dimensions = "[time, band, detector, gain, mirrorside]" */
20  size_t start[] = {0, 0, 0, 0, 0};
21  size_t count[] = {2, 1, 1, 1, 1}; // reading high-gain only
22  double flut_vals[] = {0, 0}; // before & after values
23  double factor; // for linear interpolation
24 
25  /* Open file and get info for root-level variables */
26  TRY_NC(__FILE__, __LINE__,
27  nc_open(calfile, NC_NOWRITE, &fileid));
28  vars = load_vars_nc(fileid);
29 
30  /* Populate Beginning_Time_IET array */
31  /* IET = UTC*1e6 (leap-second corrected microseconds since 1958) */
32  TRY_NC(__FILE__, __LINE__,
33  nc_inq_varid(fileid, "Beginning_Time_IET", &varid));
34  ntimes = vars[varid].dim[0].len;
35  readall_var(&vars[varid]);
36  times = vars[varid].data;
37 
38  /* Determine time indices bracketing specified time */
39  if (UTC58usec < times[0]) {
40  itime = 0;
41  printf("\n- W - %s, %d: WARNING: ", __FILE__, __LINE__);
42  printf("granule time of %ld ", (long int) UTC58usec);
43  printf("is below F-table start time of %ld\n", (long int) times[0]);
44  } else if (UTC58usec > times[ntimes - 1]) {
45  itime = ntimes - 2;
46  printf("\n- W - %s, %d: WARNING: ", __FILE__, __LINE__);
47  printf("granule time of %ld ", (long int) UTC58usec);
48  printf("is above F-table end time of %ld\n", (long int) times[ntimes - 1]);
49  } else {
50  for (itime = 0; itime < ntimes - 1; itime++)
51  if (times[itime + 1] > UTC58usec)
52  break;
53  }
54 
55  /* Calculate time interpolation factor */
56  factor =
57  (double) (UTC58usec - times[itime]) /
58  (double) (times[itime + 1] - times[itime]);
59 
60  /* Get VIIRS-SDR-F-LUT variable info */
61  TRY_NC(__FILE__, __LINE__,
62  nc_inq_varid(fileid, "VIIRS-SDR-F-LUT", &varid));
63 
64  /* Assume dimensions for M-bands, high-gain only: */
65  nbands = 16;
66  ndets = 16;
67  nms = 2;
68  /* if reading all bands & gains, use:
69  nbands = vars[varid].dim[1].len; // =22; covers all bands
70  ndets = vars[varid].dim[2].len; // =32 to cover I-bands
71  ngain = vars[varid].dim[3].len; // =3 high, low, mixed
72  nms = vars[varid].dim[4].len; // =2 HAMside
73  */
74 
75  /* Allocate space for F-cal array: [band, detector, mirrorside] */
76  /*
77  Modified to allocate a three-dimensional array passed back
78  through a command-line parameter (ftable)
79 
80  JMG 03/08/16
81  */
82 
83  *ftable = (double ***) malloc(nbands * sizeof (double**));
84  for (iband = 0; iband < nbands; iband++) {
85  ftable[0][iband] = (double **) malloc(ndets * sizeof (double*));
86  for (idet = 0; idet < ndets; idet++)
87  ftable[0][iband][idet] = (double *) malloc(nms * sizeof (double));
88  }
89 
90  /* Read F-LUT values and interpolate to observation time */
91  start[0] = itime;
92  for (iband = 0; iband < nbands; iband++) {
93  start[1] = iband + 5; // M-bands start at index 5
94  for (idet = 0; idet < ndets; idet++) {
95  start[2] = idet;
96  for (ims = 0; ims < nms; ims++) {
97  start[4] = ims;
98 
99  TRY_NC(__FILE__, __LINE__,
100  nc_get_vars_double(fileid, varid,
101  start, count, NULL, flut_vals));
102 
103  ftable[0][iband][idet][ims] =
104  (flut_vals[0] * (1.0 - factor)) +
105  (flut_vals[1] * factor);
106 
107  } // ims
108  } // idet
109  } // iband
110 
111  /* Close file and free memory allocated for related variables */
112  //free_vars_nc(vars,2);
113  TRY_NC(__FILE__, __LINE__, nc_close(fileid));
114 
115  return 0;
116 }
size_t len
Definition: nc4utils.h:44
#define TRY_NC(file, line, ncstat)
Definition: nc4utils.h:31
#define NULL
Definition: decode_rs.h:63
var_str_nc * load_vars_nc(int ncid)
Definition: nc4utils.c:254
void * data
Definition: nc4utils.h:64
integer, parameter double
dim_str_nc * dim
Definition: nc4utils.h:60
int load_fcal_lut(char *calfile, int64_t UTC58usec, double ****ftable)
int32_t nbands
int readall_var(var_str_nc *var)
Definition: nc4utils.c:334
int count
Definition: decode_rs.h:79