NASA Logo
Ocean Color Science Software

ocssw V2022
extractSPEX.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <math.h>
3 #include <libgen.h>
4 
5 #include <iostream>
6 #include <fstream>
7 #include <vector>
8 #include <netcdf>
9 
10 #include <allocate2d.h>
11 #include <allocate3d.h>
12 #include <timeutils.h>
13 #include <clo.h>
14 
15 using namespace std;
16 using namespace netCDF;
17 using namespace netCDF::exceptions;
18 
19 int copyVarAtts( NcVar *varin, NcVar *varout, string override[],
20  string overridetype[]);
21 
22 int copyVarAtts( NcVar *varin, NcVar *varout);
23 
24 int main (int argc, char* argv[]) {
25 
26  NcVar var, varin, varout;
27 
29  clo_option_t *option;
30  char *strVal;
31  char keyword [50];
32  char inancfilename[FILENAME_MAX];
33  char outancfilename[FILENAME_MAX];
34 
35  list = clo_createList();
36 
37  option = clo_addOption(list, "inancfile", CLO_TYPE_IFILE, NULL,
38  "Input ancillary file file");
39 
40  option = clo_addOption(list, "outancfile", CLO_TYPE_OFILE, NULL,
41  "Output ancillary file file");
42 
43  clo_setVersion("0.1");
44 
45  if (argc == 1) {
47  exit(1);
48  }
49 
50  clo_readArgs(list, argc, argv);
51 
52  strVal = clo_getString(list, "inancfile");
53  strcpy(inancfilename, strVal);
54 
55  strVal = clo_getString(list, "outancfile");
56  strcpy(outancfilename, strVal);
57 
58  int numOptions, optionId;
59 
60  numOptions = clo_getNumOptions(list);
61  for (optionId = 0; optionId < numOptions; optionId++) {
62  option = clo_getOption(list, optionId);
63 
64  strcpy(keyword, option->key);
65 
66  }
67 
71 
72  cout << endl << "Opening ancillary file" << endl;
73  NcFile *inANCfile = new NcFile( inancfilename, NcFile::read);
74 
75  NcDim across_dim = inANCfile->getDim("bins_across_track");
76  uint32_t nacross = across_dim.getSize();
77  NcDim along_dim = inANCfile->getDim("bins_along_track");
78  uint32_t nalong = along_dim.getSize();
79 
80  NcDim levs_dim = inANCfile->getDim("levels");
81  uint32_t nlev = levs_dim.getSize();
82  // cout << "lines=" << nalong << ", pixels=" << nacross << endl;
83 
84  uint32_t nacross_out=25;
85 
86  float **out_2d = allocate2d_float(nalong, nacross_out);
87  float ***out_3d = allocate3d_float(nlev, nalong, nacross_out);
88 
89  int bins_along_track = nalong;
90 
94 
95  // Create output ancillary file
96  cout << "Creating ancillary file" << endl;
97  NcFile* nc_output;
98  nc_output = new NcFile( outancfilename, NcFile::replace);
99 
100  // Global metadata
101  string date_created = string(unix2isodate(now(), 'G'));
102 
103  char buffer[1000];
104  multimap<string, NcGroupAtt> ancillaryGAtts = inANCfile->getAtts();
105  multimap<string, NcGroupAtt>::iterator itratt;
106  for (itratt = ancillaryGAtts.begin(); itratt != ancillaryGAtts.end();
107  ++itratt) {
108  string attrname = itratt->first;
109  NcGroupAtt attin = itratt->second;
110 
111  NcType type = attin.getType();
112  size_t attlen = attin.getAttLength();
113 
114  cout << itratt->first << " " << attlen << endl;
115 
116  if (attrname.compare("date_created") == 0) {
117  nc_output->putAtt( "date_created", date_created);
118  } else if (attrname.compare("product_name") == 0) {
119  nc_output->putAtt( "product_name", basename(outancfilename));
120  } else if (attrname.compare("title") == 0) {
121  nc_output->putAtt( "title", "SPEXone L1C ancillary file");
122  } else {
123  attin.getValues((void *) buffer);
124  nc_output->putAtt( attrname, type, attlen, buffer);
125  }
126  }
127 
128  // Create netCDF dimensions
129  NcDim rows = nc_output->addDim("bins_along_track", bins_along_track);
130  NcDim cols = nc_output->addDim("bins_across_track", nacross_out);
131  NcDim levs = nc_output->addDim("levels", nlev);
132 
133  vector<NcDim> dims;
134  vector<size_t> start, count;
135 
136  multimap<string, NcVar> ancillaryVars = inANCfile->getVars();
137 
138  multimap<string, NcVar>::iterator itr;
139  for (itr = ancillaryVars.begin(); itr != ancillaryVars.end(); ++itr) {
140  string fieldname = itr->first;
141  varin = itr->second;
142 
143  cout << itr->first << endl;
144 
145  int ndims = varin.getDimCount();
146 
147  if (ndims == 2) {
148  start.clear();
149  start.push_back(0);
150  start.push_back((nacross - nacross_out) / 2);
151 
152  count.clear();
153  count.push_back(nalong);
154  count.push_back(nacross_out);
155 
156  varin.getVar( start, count, &out_2d[0][0]);
157 
158  dims.clear();
159  dims.push_back(rows);
160  dims.push_back(cols);
161 
162  varout = nc_output->addVar(fieldname, ncFloat, dims);
163  copyVarAtts( &varin, &varout);
164  varout.putVar( &out_2d[0][0]);
165  } else if (ndims == 3) {
166  start.clear();
167  start.push_back(0);
168  start.push_back(0);
169  start.push_back((nacross - nacross_out) / 2);
170 
171  count.clear();
172  count.push_back(nlev);
173  count.push_back(nalong);
174  count.push_back(nacross_out);
175 
176  varin.getVar( start, count, &out_3d[0][0][0]);
177 
178  dims.clear();
179  dims.push_back(levs);
180  dims.push_back(rows);
181  dims.push_back(cols);
182 
183  varout = nc_output->addVar(fieldname, ncFloat, dims);
184  copyVarAtts( &varin, &varout);
185  varout.putVar( &out_3d[0][0][0]);
186  }
187  }
188 
189  free2d_float(out_2d);
190  free3d_float(out_3d);
191 
192  return 0;
193 }
clo_option_t * clo_addOption(clo_optionList_t *list, const char *key, enum clo_dataType_t dataType, const char *defaultVal, const char *desc)
Definition: clo.c:684
Utility functions for allocating and freeing three-dimensional arrays of various types.
char * clo_getString(clo_optionList_t *list, const char *key)
Definition: clo.c:1357
void clo_readArgs(clo_optionList_t *list, int argc, char *argv[])
Definition: clo.c:2103
#define NULL
Definition: decode_rs.h:63
char * key
Definition: clo.h:107
@ string
int copyVarAtts(NcVar *varin, NcVar *varout, string override[], string overridetype[])
Definition: copyvaratts.cpp:10
clo_optionList_t * clo_createList()
Definition: clo.c:532
list(APPEND LIBS ${NETCDF_LIBRARIES}) find_package(GSL REQUIRED) include_directories($
Definition: CMakeLists.txt:8
void free2d_float(float **p)
Free a two-dimensional array created by allocate2d_float.
Definition: allocate2d.c:140
void clo_printUsage(clo_optionList_t *list)
Definition: clo.c:1988
int main(int argc, char *argv[])
Definition: extractSPEX.cpp:24
float *** allocate3d_float(size_t nz, size_t ny, size_t nx)
Allocate a three-dimensional array of type float of a given size.
Definition: allocate3d.c:77
@ CLO_TYPE_IFILE
Definition: clo.h:87
@ CLO_TYPE_OFILE
Definition: clo.h:88
clo_option_t * clo_getOption(clo_optionList_t *list, int i)
Definition: clo.c:908
Utility functions for allocating and freeing two-dimensional arrays of various types.
int clo_getNumOptions(clo_optionList_t *list)
Definition: clo.c:1017
void free3d_float(float ***p)
Free a three-dimensional array created by allocate3d_float.
Definition: allocate3d.c:103
#define basename(s)
Definition: l0chunk_modis.c:29
void clo_setVersion(const char *str)
Definition: clo.c:448
u5 which has been done in the LOCALGRANULEID metadata should have an extension NRT It is requested to identify the NRT production Changes from v6 which may affect scientific the sector rotation may actually occur during one of the scans earlier than the one where it is first reported As a the b1 values are about the LOCALGRANULEID metadata should have an extension NRT It is requested to identify the NRT to fill pixels affected by dead subframes with a special value Output the metadata of noisy and dead subframe Dead Subframe EV and Detector Quality Flag2 Removed the function call of Fill_Dead_Detector_SI to stop interpolating SI values for dead but also for all downstream products for science test only Changes from v5 which will affect scientific to conform to MODIS requirements Removed the Mixed option from the ScanType in the code because the L1A Scan Type is never Mixed Changed for ANSI C compliance and comments to better document the fact that when the HDF_EOS metadata is stricly the and products are off by and in the track respectively Corrected some misspelling of RCS swir_oob_sending_detector to the Reflective LUTs to enable the SWIR OOB correction detector so that if any of the sending detectors becomes noisy or non near by good detectors from the same sending band can be specified as the substitute in the new look up table Code change for adding an additional dimension of mirror side to the Band_21_b1 LUT to separate the coefficient of the two mirror sides for just like other thermal emissive so that the L1B code can calibrate Band scan to scan with mirror side dependency which leads better calibration result Changes which do not affect scientific when the EV data are not provided in this Crosstalk Correction will not be performed to the Band calibration data Changes which do not affect scientific and BB_500m in L1A Logic was added to turn off the or to spatial aggregation processes and the EV_250m_Aggr1km_RefSB and EV_500m_Aggr1km_RefSB fields were set to fill values when SDSs EV_250m and EV_500m are absent in L1A file Logic was added to skip the processing and turn off the output of the L1B QKM and HKM EV data when EV_250m and EV_500m are absent from L1A In this the new process avoids accessing and reading the and L1A EV skips and writing to the L1B and EV omits reading and subsampling SDSs from geolocation file and writing them to the L1B and omits writing metadata to L1B and EV and skips closing the L1A and L1B EV and SDSs Logic was added to turn off the L1B OBC output when the high resolution OBC SDSs are absent from L1A This is accomplished by skipping the openning the writing of metadata and the closing of the L1B OBC hdf which is Bit in the scan by scan bit QA has been changed Until now
Definition: HISTORY.txt:361
char * unix2isodate(double dtime, char zone)
Definition: unix2isodate.c:10
float ** allocate2d_float(size_t h, size_t w)
Allocate a two-dimensional array of type float of a given size.
Definition: allocate2d.c:123
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int count
Definition: decode_rs.h:79