OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l1aextract_viirs.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5 #include <unistd.h>
6 
7 #include <sstream>
8 #include <iomanip>
9 #include "l1agen_viirs.h"
10 #include "netcdf.h"
11 #include "nc4utils.h"
12 
13 #define VERSION "0.998"
14 #define BOUNDS_ERROR 110
15 
16 // Modification history:
17 // Programmer Organization Date Ver Description of change
18 // ---------- ------------ ---- --- ---------------------
19 // Joel Gales FutureTech 01/27/15 0.01 Original development
20 // Joel Gales FutureTech 09/02/15 0.02 Remove DNB bands,
21 // Extract navigation fields
22 // Joel Gales FutureTech 09/15/15 0.03 Add support for pixel extract
23 // Joel Gales FutureTech 12/11/15 0.04 Fix pixel->sample conversion
24 // in createl1() and copyl1()
25 // Joel Gales FutureTech 01/28/16 0.990 Write spixl, epixl in history
26 // Joel Gales FutureTech 02/02/16 0.991 Adjust spixl/spixl to line
27 // up with GEO interpolation
28 // boundaries
29 // Sean Bailey NASA/GSFC 04/21/16 0.992 accept M-band lines as input
30 // and convert to scans
31 // Joel Gales FutureTech 04/29/16 0.993 Shorten time period used to
32 // determine att/orb start/count
33 // values in copy_l1()
34 // Joel Gales FutureTech 06/16/16 0.994 Tweak if number of extracted
35 // scans one greater than
36 // # of allocated scans
37 // Joel Gales FutureTech 04/03/17 0.995 Compress extracted EV fields
38 // Joel Gales FutureTech 04/24/17 0.996 Increase GEO alignment size
39 // from 8 to 16
40 // Joel Gales FutureTech 09/27/17 0.997 Adjust so at least 8 scans
41 // are extracted
42 // Joel Gales FutureTech 10/12/17 0.998 Check for number of filled
43 // scans when less than 8 scans
44 
45 int main(int argc, char* argv[])
46 {
47 
48  cout << "l1aextract_viirs " << VERSION << " ("
49  << __DATE__ << " " << __TIME__ << ")" << endl;
50 
51  if (argc == 1) {
52  cout << endl <<
53  "l1aextract_viirs infile spixl epixl sline eline outfile\n" << endl;
54  cout << " where:" << endl;
55  cout << " infile - input VIIRS L1A file\n" << endl;
56  cout << " spixl - start pixel (1-based)" << endl;
57  cout << " epixl - end pixel (1-based)" << endl;
58  cout << " sline - start line (M-band resolution, 1-based)" << endl;
59  cout << " eline - end line (M-band resolution, 1-based)" << endl;
60  cout << " outfile - output VIIRS L1A extract file" << endl;
61 
62  cout << endl;
63  cout << "To extract entire scan line set both spixl and epixl to 0"
64  << endl;
65 
66  return EXIT_FAILURE;
67  }
68 
69  static l1aFile infile, outfile;
70  string str;
71  int32_t spixl, epixl, npixl, sline, eline, sscan, escan, nscan;
72  int npixels = 3200;
73 
74  str.assign(argv[2]);
75  istringstream(str) >> spixl;
76  if (spixl != 0) spixl--;
77 
78  str.assign(argv[3]);
79  istringstream(str) >> epixl;
80  if (epixl == 0) epixl = npixels;
81  epixl--;
82 
83  str.assign(argv[4]);
84  istringstream(str) >> sline;
85  sline--;
86  sscan = floor(sline / 16);
87 
88  str.assign(argv[5]);
89  istringstream(str) >> eline;
90  eline--;
91  escan = ceil(eline / 16);
92 
93  if ((spixl > epixl) || (sscan > escan)) {
94  cout << "\nInvalid range requested:" << endl;
95  npixl = epixl - spixl + 1;
96  nscan = escan - sscan + 1;
97  printf("spixl: %4d epixl: %4d npixl: %4d\n", spixl+1, epixl+1, npixl);
98  printf("sscan: %4d escan: %4d nscan: %4d\n", sscan+1, escan+1, nscan);
99  exit(BOUNDS_ERROR);
100  }
101 
102  // Get dimension info
103  int nfilled;
104  infile.openl1(argv[1]);
105  int ncid = infile.getNcid();
106  nc_get_att(ncid, NC_GLOBAL, "number_of_filled_scans", &nfilled);
107  infile.close();
108 
109  // Adjust start/end scans if necessary
110  nscan = escan - sscan + 1;
111  if (nscan < 8) {
112  cout << "Adjusting start/end scans so that extract is at least 8 scans"
113  << endl;
114 
115  int diff = 8 - nscan;
116  int half_diff;
117  if ((diff % 2) == 0)
118  half_diff = diff / 2;
119  else
120  half_diff = (diff / 2) + 1;
121 
122  if ((sscan - half_diff) < 0) {
123  escan = escan + 2 * half_diff - sscan;
124  sscan = 0;
125  } else if ((escan + half_diff) > (nfilled - 1)) {
126  sscan = sscan - 2 * half_diff + ((nfilled - 1) - escan);
127  escan = nfilled - 1;
128  } else {
129  sscan = sscan - half_diff;
130  escan = escan + half_diff;
131  }
132 
133  nscan = escan - sscan + 1;
134  }
135 
136  // Adjust to align with GEO processing
137  spixl = (spixl / 16) * 16;
138  epixl = (epixl / 16) * 16 + 15;
139  npixl = epixl - spixl + 1;
140 
141  // Check requested number of pixels & scan lines
142  if (spixl >= npixels) {
143  cout << "spixl " << spixl << " >= pixel count ("
144  << npixels << ")" << endl;
145  exit(BOUNDS_ERROR);
146  }
147  if (epixl >= npixels) {
148  cout << "epixl " << epixl << " >= pixel count ("
149  << npixels << ") ; adjusting." << endl;
150  epixl = npixels - 1;
151  }
152  if (sscan >= nfilled) {
153  cout << "sscan " << sscan << " >= scan count ("
154  << nfilled << ")" << endl;
155  exit(BOUNDS_ERROR);
156  }
157  if (escan >= nfilled) {
158  cout << "escan " << escan << " >= scan count ("
159  << nfilled << ") ; adjusting." << endl;
160  escan = nfilled - 1;
161  }
162 
163  // Print (1-based) sscan, escan, etc
164  cout << "Adjusted extract range:" << endl;
165  printf("spixl: %4d epixl: %4d npixl: %4d\n", spixl+1, epixl+1, npixl);
166  printf("sscan: %4d escan: %4d nscan: %4d\n", sscan+1, escan+1, nscan);
167  cout << endl;
168 
170  infile.openl1(argv[1]);
171  outfile.platform.assign(infile.platform);
172  outfile.createl1(argv[6], sscan, escan, spixl, epixl, (int32_t) 1);
173 
174  infile.copyl1(argv[1], argv[6], &outfile, sscan, escan, spixl, epixl);
175 
176  outfile.close();
177 
179 
180  return EXIT_SUCCESS;
181 }
int main(int argc, char *argv[])
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
int createl1(char *l1_filename, uint32_t nSC, uint32_t imgWidth, uint32_t imgHeight, uint32_t fndWidth, uint32_t fndHeight)
int32 nscan
Definition: l1_czcs_hdf.c:19
subroutine diff(x, conec, n, dconecno, dn, dconecmk, units, u, inno, i, outno, o, input, deriv)
Definition: ffnet.f:205
const char * str
Definition: l1c_msi.cpp:35
#define VERSION
#define BOUNDS_ERROR
std::string platform
Definition: l1agen_oci.h:52