OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
compute_l1b.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include "proto.h"
4 #include "mfhdf.h"
5 #include "calib_cal_l1a.h"
6 
7 void free_all(void *dark_rest, void *gain, void *tdi, void *scan_temp, void *side, void *msec);
8 
9 int compute_l1b(int32 sd_id, int iline, float *l1b_data[], char *calibtable, char newfile) {
10  static int16 syear, sday, eday;
11  static int32 smsec, Nlines, Npixels;
12  static int16 *dark_rest = NULL, *gain = NULL, *tdi = NULL, *scan_temp = NULL, *side = NULL;
13  static int32 *msec = NULL;
14  static uint8 *sflags = NULL;
15  static float32 dark_mean[NBANDS];
16  static int32 sds_id, start[6], edge[6];
17  static char dtype[4];
18  int32 hdf_ind, rank, dimsize[6], numtype, nattrs;
19  int16 *l1a_data_bip;
20  float32 *l1b_data_bip;
21  char name[128];
22  int kline, ipix, ib;
23  /*
24  cal_mod_struc cal_mod;
25  cal_mod.flag = 0;
26  */
27 
28  /* Get attributes */
29  if (newfile) {
30  if (read_attr(sd_id, "Number of Scan Lines", &Nlines) == -1 ||
31  read_attr(sd_id, "Pixels per Scan Line", &Npixels) == -1 ||
32  read_attr(sd_id, "Data Type", &dtype) == -1)
33  return -1;
34 
35  /* Read Level 1A data */
36  if ((hdf_ind = SDnametoindex(sd_id, "l1a_data")) < 0) {
37  fprintf(stderr, "can't locate SDS l1a_data\n");
38  return -1;
39  }
40  if ((sds_id = SDselect(sd_id, hdf_ind)) < 0) {
41  fprintf(stderr, "can't select SDS l1a_data\n");
42  return -1;
43  }
44  if (SDgetinfo(sds_id, name, &rank, dimsize, &numtype, &nattrs) < 0) {
45  fprintf(stderr, "can't get info for SDS l1a_data\n");
46  return -1;
47  }
48 
49  dark_rest = (int16 *) realloc(dark_rest, Nlines * NBANDS * sizeof (int16));
50  gain = (int16 *) realloc(gain, Nlines * NBANDS * sizeof (int16));
51  tdi = (int16 *) realloc(tdi, Nlines * NBANDS * sizeof (int16));
52  scan_temp = (int16 *) realloc(scan_temp, Nlines * NBANDS * sizeof (int16));
53  side = (int16 *) realloc(side, Nlines * sizeof (int16));
54  msec = (int32 *) realloc(msec, Nlines * sizeof (int32));
55  sflags = (uint8 *) realloc(sflags, Nlines * 4 * sizeof (uint8));
56 
57  if (get_calib_sds(sd_id, dark_rest, gain, tdi, scan_temp, side, msec) == -1) {
58  fprintf(stderr, "can't compute L1B data\n");
59  return -1;
60  }
61  if (read_sds(sd_id, "s_flags", sflags) == -1) return -1;
62 
63  /* Compute dark_mean */
64  for (ib = 0; ib < NBANDS; ib++) {
65  dark_mean[ib] = 0.;
66  for (kline = 0; kline < Nlines; kline++) dark_mean[ib] += (float) dark_rest[kline * NBANDS + ib];
67  dark_mean[ib] = dark_mean[ib] / Nlines;
68  }
69 
70  edge[0] = 1;
71  edge[1] = dimsize[1];
72  edge[2] = dimsize[2];
73  start[1] = 0;
74  start[2] = 0;
75 
76  }
77 
78  if (sflags[4 * iline] > 127) {
79  printf("Scan %d is corrupted. Skipped.\n", iline);
80  return +1;
81  }
82 
83  l1a_data_bip = (int16 *) malloc(Npixels * NBANDS * sizeof (int16));
84  l1b_data_bip = (float32 *) malloc(Npixels * NBANDS * sizeof (float32));
85  start[0] = iline;
86  if (SDreaddata(sds_id, start, NULL, edge, l1a_data_bip) < 0) {
87  fprintf(stderr, "can't read Level 1A data\n");
88  return -1;
89  }
90 
91  calibrate_l1a(calibtable, syear, sday, smsec, eday, msec[iline], dtype, 1, Npixels, &dark_rest[iline * NBANDS], dark_mean, &gain[iline * NBANDS], &tdi[iline * NBANDS], &scan_temp[iline * NBANDS], side[iline], l1a_data_bip, l1b_data_bip, NULL);
92 
93  for (ib = 0; ib < NBANDS; ib++)
94  if (l1b_data[ib])
95  for (ipix = 0; ipix < Npixels; ipix++)
96  l1b_data[ib][ipix] = l1b_data_bip[ipix * NBANDS + ib];
97 
98  free(l1a_data_bip);
99  free(l1b_data_bip);
100 
101  return 0;
102 }
integer, parameter int16
Definition: cubeio.f90:3
int16 eday
Definition: l1_czcs_hdf.c:17
float dark_mean[8]
Definition: l1a_seawifs.c:34
int16 * gain
Definition: l1_czcs_hdf.c:33
#define NULL
Definition: decode_rs.h:63
int32 calibrate_l1a(char *cal_path, int16 syear, int16 sday, int32 smsec, int16 eday, int32 msec, char *dtype, int32 st_samp, int32 nsamp, int16 *dark_rest, float32 *dark_mean, int16 *gain, int16 *tdi, int16 *scan_temp, int16 side, int16 *l1a_data, float32 *l1b_data, cal_mod_struc *cal_mod)
int16_t * dark_rest
Definition: l1a_seawifs.c:89
int get_calib_sds(int32 sd_id, int16 *dark_rest, int16 *gain, int16 *tdi, int16 *scan_temp, int16 *side, int32 *msec)
Definition: get_calib_sds.c:4
int32 * msec
Definition: l1_czcs_hdf.c:31
int syear
Definition: l1_czcs_hdf.c:15
int32 smsec
Definition: l1_czcs_hdf.c:16
int sday
Definition: l1_czcs_hdf.c:15
int read_attr(int32 sd_id, char *attrname, void *attr)
Definition: read_write.c:61
int16_t tdi[BANDS_DIMS_1A]
Definition: l1a_seawifs.c:37
void free_all(void *dark_rest, void *gain, void *tdi, void *scan_temp, void *side, void *msec)
dtype
Definition: DDataset.hpp:31
int16_t * side
Definition: l1a_seawifs.c:88
Extra metadata that will be written to the HDF4 file l2prod rank
int compute_l1b(int32 sd_id, int iline, float *l1b_data[], char *calibtable, char newfile)
Definition: compute_l1b.c:9
@ NBANDS
Definition: make_L3_v1.1.c:53
int32 read_sds(l1info_struct l1info, char *arr_name, int32 *exp_ntyp, void *array)
Definition: read_sds.c:5