OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
get_niwa_iop.c
Go to the documentation of this file.
1 /*
2  * get_niwa_iop.c -- wrapper functions to interface NIWA/UoP/Moore IOP algorithm
3  * to prodgen and convl12
4  */
5 
6 #include "l12_proto.h"
7 #include "l2prod.h"
8 
9 #include "niwa_iop.h"
10 
11 
12 /* keep track of last scan line processed */
13 static int last_recnum = -1;
14 
15 
16 /* temporary arrays for iop values for this scan line */
17 static float *niwa_a; /* absorption coefficient per band and pixel */
18 static float *niwa_bb; /* backscatter coefficient per band and pixel */
19 static int16 *niwa_iopf; /* iop flags per pixel */
20 
21 /*
22  * check if we have already calculated the values for this scan line
23  */
24 static int niwa_ran(int recnum) {
25  return recnum == last_recnum;
26 }
27 
28 /*
29  * allocate space for one scan line of IOP variables
30  */
31 static void alloc_niwa(int32_t npix, int32_t nbands) {
32  if ((niwa_a = calloc(npix * nbands, sizeof (float))) == NULL) {
33  fprintf(stderr, "-E- %s line %d : error allocating memory for NIWA IOP.\n",
34  __FILE__, __LINE__);
35  exit(1);
36  }
37  if ((niwa_bb = calloc(npix * nbands, sizeof (float))) == NULL) {
38  fprintf(stderr, "-E- %s line %d : error allocating memory for NIWA IOP.\n",
39  __FILE__, __LINE__);
40  exit(1);
41  }
42  if ((niwa_iopf = calloc(npix, sizeof (int16))) == NULL) {
43  fprintf(stderr, "-E- %s line %d : error allocating memory for NIWA IOP.\n",
44  __FILE__, __LINE__);
45  exit(1);
46  }
47 }
48 
49 static void run_niwa(l2str *l2rec) {
50  static int first_time = 1;
51 
52  if (first_time) {
53  /* allocate a, bb & flag arrays */
54  alloc_niwa(l2rec->l1rec->npix, l2rec->l1rec->l1file->nbands);
55  first_time = 0;
56  }
57 
58  /* call the NIWA iop code to calculate the a and bb values */
59  niwa_iop(l2rec, niwa_a, niwa_bb, niwa_iopf);
60 
61  last_recnum = l2rec->l1rec->iscan;
62 }
63 
64 /*
65  * interface to prodgen() for NIWA iops
66  */
67 void get_niwa(l2str *l2rec, l2prodstr *p, float prod[]) {
68  int prod_id = p->cat_ix;
69  int ib = p->prod_ix;
70  int ip, ipb;
71 
72  if (!niwa_ran(l2rec->l1rec->iscan))
73  run_niwa(l2rec);
74 
75  for (ip = 0; ip < l2rec->l1rec->npix; ip++) {
76 
77  ipb = ip * l2rec->l1rec->l1file->nbands + ib;
78 
79  switch (prod_id) {
80  case CAT_a_niwa:
81  prod[ip] = niwa_a[ipb];
82  break;
83 
84  case CAT_bb_niwa:
85  prod[ip] = niwa_bb[ipb];
86  break;
87 
88  default:
89  printf("-E- %s line %d : erroneous product ID %d passed to NIWA IOP\n",
90  __FILE__, __LINE__, prod_id);
91  exit(1);
92  }
93  }
94 }
95 
96 /*
97  * get the NIWA iop status flags
98  */
99 int16 * get_flags_niwa(l2str *l2rec) {
100  if (!niwa_ran(l2rec->l1rec->iscan))
101  run_niwa(l2rec);
102 
103  return niwa_iopf;
104 }
105 
106 /*
107  * interface to convl12() for NIWA iops
108  */
109 void iops_niwa(l2str *l2rec) {
110  int ib, ip, ipb;
111 
112  l1str *l1rec = l2rec->l1rec;
113  int32_t nbands = l1rec->l1file->nbands;
114 
115  if (!niwa_ran(l1rec->iscan))
116  run_niwa(l2rec);
117 
118  for (ip = 0; ip < l1rec->npix; ip++) {
119  for (ib = 0; ib < nbands; ib++) {
120  ipb = ip * nbands + ib;
121  l2rec->a[ipb] = niwa_a[ipb];
122  l2rec->bb[ipb] = niwa_bb[ipb];
123  }
124  }
125 }
126 
integer, parameter int16
Definition: cubeio.f90:3
#define NULL
Definition: decode_rs.h:63
read l1rec
void niwa_iop(l2str *l2rec, float niwa_a[], float niwa_bb[], int16 niwa_iopf[])
Definition: niwa_iop.c:861
void iops_niwa(l2str *l2rec)
Definition: get_niwa_iop.c:109
read recnum
#define CAT_a_niwa
Definition: l2prod.h:217
int32_t nbands
void get_niwa(l2str *l2rec, l2prodstr *p, float prod[])
Definition: get_niwa_iop.c:67
int16 * get_flags_niwa(l2str *l2rec)
Definition: get_niwa_iop.c:99
#define CAT_bb_niwa
Definition: l2prod.h:218
int npix
Definition: get_cmp.c:27
float p[MODELMAX]
Definition: atrem_corl1.h:131