OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
main_l1bgen.c
Go to the documentation of this file.
1 /* ========================================================================
2  * MSl1bgen - multi-sensor L1B generator
3  *
4  * Synopsis:
5  *
6  * MSl1bgen par=parfile
7  *
8  * Description:
9  *
10  * Modification history:
11  *
12  * Programmer Organization Date Description of change
13  * -------------- ------------ -------- ---------------------
14  * Bryan A. Franz GSC 21 July 1998 Original development
15  * Joel M. Gales Futuretech 20 Sept 1999 Generate standard L1B
16  * SeaWIFS output
17  * Bryan A. Franz GSC 28 Jan 2000 switch to parfile and
18  * added stray-light control
19  *
20  * ======================================================================== */
21 
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <libgen.h>
31 #include <math.h>
32 
33 #include "l12_proto.h"
34 
35 #define INT32 int32_t
36 #define FLOAT32 float
37 #define BYTE unsigned char
38 
39 int msl1bgen_usage(char *prog);
40 
41 /* -------------------------------------------------------------------- *
42  * main *
43  * -------------------------------------------------------------------- */
44 int main(int argc, char *argv[]) {
45  int32_t iscan = 0; /* input scan number */
46  int32_t oscan = 0; /* input scan number */
47  int32_t spix = 0; /* start pixel for subscene process */
48  int32_t epix = -1; /* end pixel for subscene process */
49  int32_t dpix = 1; /* pixel subsampling increment */
50  int32_t sscan = 0; /* start scan for subscene process */
51  int32_t escan = -1; /* end scan for subscene process */
52  int32_t dscan = 1; /* scan subsampling increment */
53  int32_t npix = 0; /* Number of output pixels per scan */
54 
55  l1str l1rec; /* generic level-1b scan structure */
56  filehandle ifile; /* input file handle */
57  filehandle ofile; /* output file handle */
58 
59  int32_t ip, ib, ipb, i;
60 
61  if (argc == 1) {
62  l2gen_usage("l1bgen_generic");
63  return 1;
64  }
65 
66  // see if help on command line
67  for (i = 0; i < argc; i++) {
68  if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "-help") == 0)) {
69  l2gen_usage("l1bgen_generic");
70  }
71  }
72 
73  cdata_();
77 
78  /* Parse input parameters */
79  if (msl12_input(argc, argv, "l1bgen_generic", &ifile) != 0) {
80  printf("-E- %s: Error parsing input parameters.\n", argv[0]);
81  exit(FATAL_ERROR);
82  }
83 
84  if (access(input->ifile[0], F_OK) || access(input->ifile[0], R_OK)) {
85  printf("-E- %s: Input file '%s' does not exist or cannot open.\n",
86  argv[0], input->ifile[0]);
87  exit(FATAL_ERROR);
88  }
89 
90  /* */
91  /* Open input file and get sensor and scan information from handle */
92  /* */
93  if (openl1(&ifile) != 0) {
94  printf("-E- %s: Error opening %s for reading.\n", argv[0], ifile.name);
95  exit(1);
96  }
97 
98  /* */
99  /* Allocate memory for L1 scan data */
100  /* */
101  if (alloc_l1(&ifile, &l1rec) == 0) {
102  printf("-E- %s: Unable to allocate L1 record.\n", argv[0]);
103  exit(FATAL_ERROR);
104  }
105 
106 
107  /* Set the end pixel if it was not set by command argument */
108  if (l1_input->epixl == -1)
109  l1_input->epixl = ifile.npix;
110  if (l1_input->eline == -1)
111  l1_input->eline = ifile.nscan;
112 
113  spix = MAX(l1_input->spixl - 1, 0);
114  epix = MIN(l1_input->epixl - 1, ifile.npix - 1);
115  dpix = MAX(l1_input->dpixl, 1);
116  sscan = MAX(l1_input->sline - 1, 0);
117  escan = MIN(l1_input->eline - 1, ifile.nscan - 1);
118  dscan = MAX(l1_input->dline, 1);
119 
120  if (sscan > escan || spix > epix) {
121  printf("-E- %s: scan and pixel limits make no sense.\n", argv[0]);
122  printf(" start scan = %d\n", sscan + 1);
123  printf(" end scan = %d\n", escan + 1);
124  printf(" start pixel = %d\n", spix + 1);
125  printf(" end pixel = %d\n", epix + 1);
126  exit(FATAL_ERROR);
127  }
128 
129  /* Note: for the L1 file, npix is still the native scan pixel count */
130  ifile.spix = spix; /* start pixel rel to L1 scan */
131  ifile.epix = epix; /* end pixel rel to L1 scan */
132 
133  npix = (epix - spix) / dpix + 1;
134 
135  /* */
136  /* Transfer sensor and scan info to output filehandle and open */
137  /* */
138  strcpy(ofile.name, input->ofile[0]);
139  if (strcmp(input->oformat, "netCDF4") == 0)
140  ofile.format = FT_L1BNCDF;
141  else
142  ofile.format = FT_L1HDF;
143  ofile.mode = WRITE;
144  ofile.sensorID = ifile.sensorID;
145  ofile.nbands = ifile.nbands;
146  ofile.bindx = ifile.bindx;
147  ofile.spix = spix;
148  ofile.epix = epix;
149  ofile.npix = (epix - spix) / dpix + 1;
150  ofile.nscan = (escan - sscan) / dscan + 1;
151  ofile.ctl_pt_incr = input->ctl_pt_incr;
152 
153  strcpy(ofile.spatialResolution, ifile.spatialResolution);
154 
155 
156  printf("Opening L1B output file: %s\n", ofile.name);
157 
158  if (openl1(&ofile) != 0) {
159  printf("-E- %s: Error opening %s for writing.\n", argv[0], ofile.name);
160  exit(1);
161  }
162 
163  /* */
164  /* Read file scan by scan, scale radiances, and write. */
165  /* */
166  for (iscan = sscan; iscan <= escan; iscan += dscan, oscan++) {
167 
168  if ((iscan % 100) == 0) printf("Processing scan %d\n", iscan);
169 
170  readl1(&ifile, iscan, &l1rec);
171 
172  /* apply vicarious calibration */
173  for (ip = 0; ip < npix; ip++) {
174  for (ib = 0; ib < ifile.nbands; ib++) {
175  i = ifile.bindx[ib];
176  ipb = ip * ifile.nbands + i;
177  l1rec.Lt[ipb] = l1rec.Lt[ipb] * l1_input->gain[ib] + l1_input->offset[ib];
178  }
179  }
180 
181  /* write this record */
182  if (writel1(&ofile, oscan, &l1rec) != 0) {
183  printf("-E- %s: error writing to %s.\n",
184  argv[0], ofile.name);
185  exit(FATAL_ERROR);
186  }
187 
188  }
189 
190 
191  // if (ifile.sensorID == SEAWIFS) {
192  // DPTB(l1b_seawifs(&ifile, &ofile,
193  // sscan, escan, dscan,
194  // spix, epix, dpix));
195  // }
196 
197  closel1(&ifile);
198  closel1(&ofile);
199 
200  printf("\nProcessing Completed\n");
201 
202  exit(0);
203 }
204 
#define MAX(A, B)
Definition: swl0_utils.h:26
#define MIN(x, y)
Definition: rice.h:169
@ FT_L1BNCDF
Definition: filetype.h:19
int readl1(filehandle *l1file, int32_t recnum, l1str *l1rec)
Definition: l1_io.c:400
int msl12_input(int argc, char *argv[], const char *progName, filehandle *l1file)
Definition: msl12_input.c:4201
void filehandle_init(filehandle *file)
read l1rec
int writel1(filehandle *l1file, int32_t recnum, l1str *l1rec)
void msl12_input_init()
Definition: msl12_input.c:485
int l2gen_usage(const char *prog)
Definition: msl12_input.c:4254
instr * input
void cdata_()
l1_input_t * l1_input
Definition: l1_options.c:9
#define FATAL_ERROR
Definition: swl0_parms.h:5
void closel1(filehandle *l1file)
Definition: l1_io.c:68
#define WRITE
Definition: create_Vdata.c:7
int msl1bgen_usage(char *prog)
int32 dpix
Definition: l1_czcs_hdf.c:22
int32 spix
Definition: l1_czcs_hdf.c:21
int32_t iscan
int main(int argc, char *argv[])
Definition: main_l1bgen.c:44
@ FT_L1HDF
Definition: filetype.h:20
int32 epix
Definition: l1_czcs_hdf.c:23
int32_t alloc_l1(filehandle *l1file, l1str *l1rec)
Definition: alloc_l1.c:15
int i
Definition: decode_rs.h:71
int openl1(filehandle *l1file)
Definition: l1_io.c:207
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int npix
Definition: get_cmp.c:27