OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
DDAncillary.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * NAME: DDAncillary.cpp
4  *
5  * DESCRIPTION: Reads ancillary meteorological data
6  *
7  * Created on: April 17, 2019
8  * Author: Sam Anderson
9  *
10  *******************************************************************************/
11 
12 #include <cmath>
13 #include <DDAncillary.h>
14 #include <DDOptions.h>
15 
16 using namespace std;
17 using namespace netCDF;
18 using namespace netCDF::exceptions;
19 
20 /**************************************************************************
21  * NAME: Ancillary()
22  *
23  * DESCRIPTION: Class Constructor
24  *
25  *************************************************************************/
26 
28  lut_ = nullptr;
29 }
30 
31 /**************************************************************************
32  * NAME: ~DDAncillary()
33  *
34  * DESCRIPTION: Class Destructor
35  *
36  *************************************************************************/
37 
39  if (lut_ != nullptr) {
40  delete lut_;
41  lut_ = nullptr;
42  }
43 }
44 
45 /**************************************************************************
46  * NAME: initialize()
47  *
48  * DESCRIPTION: Read and time-interpolate meteorology lut for granule
49  *
50  *************************************************************************/
51 
52 int DDAncillary::initialize(int hr, int min)
53 {
54  int status = DTDB_SUCCESS;
55 
56  string filepath = get_option(INPUT_GDAS);
57  if ( !filepath.empty()) {
58  } else {
59  string filepath_1 = get_option(INPUT_GDAS1);
60  string filepath_2 = get_option(INPUT_GDAS2);
61 
62  if (filepath_1.empty() || filepath_2.empty()) {
63  cerr << "DDAncillary:: Ancillary files missing: "
64  + filepath_1 << endl;
65  return DTDB_FAIL;
66  }
67 
68  met_lut* lut_1 = new met_lut;
69  memset(lut_1, 0, sizeof(met_lut));
70  status = read_merra2_file( filepath_1, lut_1);
71  if (status != DTDB_SUCCESS) {
72  cerr << "DDAncillary:: failure reading file 1: "
73  + filepath_1 << endl;
74  return status;
75  }
76  met_lut* lut_2 = new met_lut;
77  memset(lut_2, 0, sizeof(met_lut));
78  status = read_merra2_file( filepath_2, lut_2);
79  if (status != DTDB_SUCCESS) {
80  cerr << "DDAncillary:: failure reading file 2: "
81  + filepath_2 << endl;
82  return status;
83  }
84 
85  lut_ = new met_lut;
86  memset(lut_, 0, sizeof(met_lut));
87 
88  for (int iy=0; iy<NMLATS; iy++) {
89  for (int ix=0; ix<NMLONS; ix++) {
90  lut_->ps[iy][ix] = lut_1->ps[iy][ix] +
91  (lut_2->ps[iy][ix]-lut_1->ps[iy][ix])*min/60.0;
92  lut_->oz[iy][ix] = lut_1->oz[iy][ix] +
93  (lut_2->oz[iy][ix]-lut_1->oz[iy][ix])*min/60.0;
94  lut_->pw[iy][ix] = lut_1->pw[iy][ix] +
95  (lut_2->pw[iy][ix]-lut_1->pw[iy][ix])*min/60.0;
96  lut_->u_ws[iy][ix] = lut_1->u_ws[iy][ix] +
97  (lut_2->u_ws[iy][ix]-lut_1->u_ws[iy][ix])*min/60.0;
98  lut_->v_ws[iy][ix] = lut_1->v_ws[iy][ix] +
99  (lut_2->v_ws[iy][ix]-lut_1->v_ws[iy][ix])*min/60.0;
100  }
101  }
102 
103  delete lut_1;
104  delete lut_2;
105  }
106 
107  return status;
108 }
109 
110 /**************************************************************************
111  * NAME: read_merra2_file()
112  *
113  * DESCRIPTION: Open and read a MERRA2 file
114  *
115  *************************************************************************/
116 
118 {
119  int status = DTDB_SUCCESS;
120 
121  try {
122  NcFile* fio = new NcFile(filepath, NcFile::read);
123  NcVar var = fio->getVar("PS");
124  var.getVar(&in->ps[0][0]);
125  var = fio->getVar("TO3");
126  var.getVar(&in->oz[0][0]);
127  var = fio->getVar("TQV");
128  var.getVar(&in->pw[0][0]);
129  var = fio->getVar("U10M");
130  var.getVar(&in->u_ws[0][0]);
131  var = fio->getVar("V10M");
132  var.getVar(&in->v_ws[0][0]);
133  delete fio;
134  } catch (NcException& e) {
135  e.what();
136  cerr << "DDAncillary:: Failure opening ancillary file: "
137  + filepath << endl;
138  return DTDB_FAIL;
139  }
140 
141  return status;
142 }
143 
144 /**************************************************************************
145  * NAME: get_meteorology()
146  *
147  * DESCRIPTION: Get ancillary data from look-up table.
148  * Initialize granule-level output data attributes
149  *
150  *************************************************************************/
151 
152 map<string,ddata*> DDAncillary::read( map<string,ddata*> imap )
153 {
154  map<string, ddata*> omap;
155  int status = DTDB_SUCCESS;
156  ddval<int>* pstat = new ddval<int>(dtype::INT, status);
157  omap.insert({"status", pstat});
158  metio io;
159 
160  ddma<float,2>* plon = static_cast<ddma<float,2>*>(imap["longitude"]);
161  ddma<float,2>* plat = static_cast<ddma<float,2>*>(imap["latitude"]);
162  ddma<float,2>* pws = new ddma<float,2>(dtype::FLOAT, plon->start, plon->count);
163  omap.insert({"wind_speed", pws});
164  ddma<float,2>* pwd = new ddma<float,2>(dtype::FLOAT, plon->start, plon->count);
165  omap.insert({"wind_direction", pwd});
166  ddma<float,2>* ppw = new ddma<float,2>(dtype::FLOAT, plon->start, plon->count);
167  omap.insert({"water_vapor", ppw});
168  ddma<float,2>* poz = new ddma<float,2>(dtype::FLOAT, plon->start, plon->count);
169  omap.insert({"ozone", poz});
170  ddma<float,2>* pps = new ddma<float,2>(dtype::FLOAT, plon->start, plon->count);
171  omap.insert({"surface_pressure", pps});
173  omap.insert({"cloud_mask", puc});
174 
175  string filepath = get_option(INPUT_GDAS);
176  if ( !filepath.empty()) {
177  NcFile* fio = new NcFile(filepath, NcFile::read);
178  NcVar var = fio->getVar("PS");
179  var.getVar(plon->start, plon->count, (float*)pps->ptr);
180  var = fio->getVar("TO3");
181  var.getVar(plon->start, plon->count, (float*)poz->ptr);
182  var = fio->getVar("TQV");
183  var.getVar(plon->start, plon->count, (float*)ppw->ptr);
184 
185  ddma<float,2>* pu_ws = new ddma<float,2>(dtype::FLOAT, plon->start, plon->count);
186  ddma<float,2>* pv_ws = new ddma<float,2>(dtype::FLOAT, plon->start, plon->count);
187  var = fio->getVar("U10M");
188  var.getVar(plon->start, plon->count, (float*)pu_ws->ptr);
189  var = fio->getVar("V10M");
190  var.getVar(plon->start, plon->count, (float*)pv_ws->ptr);
191 
192  for ( size_t iy=0; iy<plon->count[0]; iy++) {
193  for ( size_t ix=0; ix<plon->count[1]; ix++) {
194  // wind speed
195  pws->pts[iy][ix] = sqrt(pu_ws->pts[iy][ix]*pu_ws->pts[iy][ix] +
196  pv_ws->pts[iy][ix]*pv_ws->pts[iy][ix]);
197  // wind direction
198  float wd = atan2(-1.0*pu_ws->pts[iy][ix],-1.0*pv_ws->pts[iy][ix]) *
199  180.0/M_PI;
200  pwd->pts[iy][ix] = (wd < 0) ? wd + 360.0 : wd;
201  }
202  }
203  delete fio;
204  delete pu_ws;
205  delete pv_ws;
206 
207  } else {
208  for ( size_t iy=0; iy<plon->count[0]; iy++) {
209  for ( size_t ix=0; ix<plon->count[1]; ix++) {
210  get_met(plat->pts[iy][ix], plon->pts[iy][ix], &io);
211  pws->pts[iy][ix] = io.ws;
212  pwd->pts[iy][ix] = io.wd;
213  ppw->pts[iy][ix] = io.pw;
214  poz->pts[iy][ix] = io.oz;
215  pps->pts[iy][ix] = io.ps;
216  }
217  }
218  }
220  if ( !filepath.empty()) {
221  NcFile* fio = new NcFile(filepath, NcFile::read);
222  NcVar var = fio->getVar("ADJ_MASK");
223  var.getVar(plon->start, plon->count, (unsigned char*)puc->ptr);
224  delete fio;
225  } else {
226  for ( size_t iy=0; iy<plon->count[0]; iy++) {
227  for ( size_t ix=0; ix<plon->count[1]; ix++) {
228  puc->pts[iy][ix] = DFILL_UBYTE;
229  }
230  }
231  }
232  return omap;
233 }
234 
235 int DDAncillary::get_met( const float lat, const float lon, metio* io )
236 {
237  int status = DTDB_SUCCESS;
238 
239  if(lat > 90.0 || lat < -90.0) {
240  cerr << "DDAncillary:: lat out of range, must be 90 to -90:" << lat << endl;
241  return DTDB_FAIL;
242  }
243  if (lon < -180.0 || lon > 180.0) {
244  cerr << "DDAncillary:: lon out of range, must be [-180,180):" << lon << endl;
245  return DTDB_FAIL;
246  }
247 
248  int i1 = 0;
249  int i2 = 0;
250  int j1 = 0;
251  int j2 = 0;
252 
253  status = get_interp_indexes(lat, lon, i1, i2, j1, j2);
254  if (status != DTDB_SUCCESS) {
255  cerr << "DDAncillary:: Failed to get indexes for interpolation: " << status << endl;
256  return status;
257  }
258 
259  float f[4] = {0,0,0,0};
260  float x1 = index2lon(i1);
261  float x2 = index2lon(i2);
262  float y1 = index2lat(j1);
263  float y2 = index2lat(j2);
264 
265 // perform 2D bilinear interpolation (http://en.wikipedia.org/wiki/Bilinear_interpolation)
266 // wind speed u-direction
267 
268  f[0] = lut_->u_ws[j1][i1];
269  f[1] = lut_->u_ws[j1][i2];
270  f[2] = lut_->u_ws[j2][i2];
271  f[3] = lut_->u_ws[j2][i1];
272 
273  float u = f[0]*(x2-lon)*(y2-lat) + f[1]*(lon-x1)*(y2-lat) +
274  f[2]*(lon-x1)*(lat-y1) + f[3]*(x2-lon)*(lat-y1);
275  u /= ((x2-x1)*(y2-y1));
276 
277 // wind speed v-direction
278 
279  f[0] = lut_->v_ws[j1][i1];
280  f[1] = lut_->v_ws[j1][i2];
281  f[2] = lut_->v_ws[j2][i2];
282  f[3] = lut_->v_ws[j2][i1];
283 
284  float v = f[0]*(x2-lon)*(y2-lat) + f[1]*(lon-x1)*(y2-lat) +
285  f[2]*(lon-x1)*(lat-y1) + f[3]*(x2-lon)*(lat-y1);
286  v /= ((x2-x1)*(y2-y1));
287 
288 // wind speed
289  io->ws = sqrt(u*u + v*v);
290 
291 // wind direction
292  float wd = atan2(-1.0*u,-1.0*v) * 180.0/M_PI;
293  io->wd = (wd < 0) ? wd + 360.0 : wd;
294 
295 // precipitable water vapor
296  f[0] = lut_->pw[j1][i1];
297  f[1] = lut_->pw[j1][i2];
298  f[2] = lut_->pw[j2][i2];
299  f[3] = lut_->pw[j2][i1];
300 
301  float pwat = f[0]*(x2-lon)*(y2-lat) + f[1]*(lon-x1)*(y2-lat) +
302  f[2]*(lon-x1)*(lat-y1) + f[3]*(x2-lon)*(lat-y1);
303 
304  io->pw = pwat / ((x2-x1)*(y2-y1));
305  if (io->pw <= 0.0) {
306  io->pw = 1.0e-5;
307  }
308 
309 // ozone
310  f[0] = lut_->oz[j1][i1];
311  f[1] = lut_->oz[j1][i2];
312  f[2] = lut_->oz[j2][i2];
313  f[3] = lut_->oz[j2][i1];
314 
315  float oz = f[0]*(x2-lon)*(y2-lat) + f[1]*(lon-x1)*(y2-lat) +
316  f[2]*(lon-x1)*(lat-y1) + f[3]*(x2-lon)*(lat-y1);
317 
318  io->oz = oz / ((x2-x1)*(y2-y1));
319 
320 // surface pressure
321  f[0] = lut_->ps[j1][i1];
322  f[1] = lut_->ps[j1][i2];
323  f[2] = lut_->ps[j2][i2];
324  f[3] = lut_->ps[j2][i1];
325 
326  float ps = f[0]*(x2-lon)*(y2-lat) + f[1]*(lon-x1)*(y2-lat) +
327  f[2]*(lon-x1)*(lat-y1) + f[3]*(x2-lon)*(lat-y1);
328 
329  io->ps = ps / ((x2-x1)*(y2-y1));
330 
331 
332  return status;
333 }
334 
335 /**************************************************************************
336  * NAME: lat2index()
337  *
338  * DESCRIPTION: Converts latitudes into indexes into the 2D
339  * lat/lon data arrays. Assumes a 1x1 degree resolution. Index returned is
340  * for the lower-left corner of the grid box containing the location specified.
341  *
342  *************************************************************************/
343 
344 int DDAncillary::lat2index(const float lat) {
345 
346  if (lat < -90.0 || lat > 90.0) {
347  cerr << "DDAncillary:: Invalid latitude: " << lat << endl;
348  return -1;
349  }
350  int index = floor((lat + 90.0) / 0.5);
351 
352  return index;
353 }
354 
355 /**************************************************************************
356  * NAME: index2lat()
357  *
358  * DESCRIPTION: Converts indexes into latitude.Assumes a 1x1 degree resolution.
359  *
360  *************************************************************************/
361 
362 float DDAncillary::index2lat(const int index) {
363 
364  if (index < 0 || index > NMLATS) {
365  cerr << "DDAncillary:: Index is out of bounds: " << index << endl;
366  return -1;
367  }
368  float lat = -90.0 + 0.5*(index);
369 
370  return lat;
371 }
372 
373 /**************************************************************************
374  * NAME: lon2index()
375  *
376  * DESCRIPTION: Converts longitudes into indexes into the 2D
377  * lat/lon data arrays. Assumes a 1x1 degree resolution. Index returned is
378  * for the lower-left corner of the grid box containing the location specified.
379  *
380  *************************************************************************/
381 
382 int DDAncillary::lon2index(const float lon) {
383 
384  if (lon < -180.0 || lon >= 180.0) {
385  cerr << "DDAncillary:: Invalid longitude: " << lon << endl;
386  return -1;
387  }
388  int index = floor((lon + 180.0)/0.625);
389 
390  return index;
391 }
392 
393 /**************************************************************************
394  * NAME: index2lon()
395  *
396  * DESCRIPTION: Converts indexes into longitude. Assumes a 1x1 degree resolution.
397  *
398  *************************************************************************/
399 
400 float DDAncillary::index2lon(const int index) {
401 
402  if (index < 0 || index > NMLONS) {
403  cerr << "DDAncillary:: Index is out of bounds of windspeed array: " << index << endl;
404  return -1;
405  }
406  float lon = -180.0 + 0.625*index;
407 
408  return lon;
409 }
410 
411 /**************************************************************************
412  * NAME: get_interp_indexes()
413  *
414  * DESCRIPTION: Based on values of lat and lon, returns indexes of surrounding
415  * points for 2D linear interpolation.
416  * i1 and i2 are the indexes to the west and east of the given longitude.
417  * j1 and j2 are the indexes to the south and north of the given latitude.
418  * Corner cases involving dateline and north pole (exactly 90.0N)
419  * should be handled correctly.
420  *
421  *************************************************************************/
422 
423 int DDAncillary::get_interp_indexes(const float lat, const float lon,
424  int& i1, int& i2, int& j1, int& j2)
425 {
426 
427 // specifically exclude locations where lat==90.0 exactly. This would break
428 // the interpolation below.
429  if(lat >= 90.0 || lat < -90.0) {
430  cerr << "DDAncillary:: lat out of range, must be [-90,90]:" << lat << endl;
431  return DTDB_FAIL;
432  }
433  if (lon < -180.0 || lon >= 180.0) {
434  cerr << "DDAncillary:: lon out of range, must be [-180,180]:" << lon << endl;
435  return DTDB_FAIL;
436  }
437 
438 // get indexes into the windspeed table for lower left point.
439  i1 = lon2index(lon);
440  if (i1 == -1) {
441  cerr << "DDAncillary:: Failed to convert longitude to index: " << i1 << endl;
442  return DTDB_FAIL;
443  }
444 
445  j1 = lat2index(lat);
446  if (j1 == -1) {
447  cerr << "DDAncillary:: Failed to convert latitude to index: " << j1 << endl;
448  return DTDB_FAIL;
449  }
450 
451 // simply calculate the next indexes. Watch for instances where we're
452 // crossing the dateline (i2 == NMLONS) and circle back to index 0.
453  i2 = i1 + 1;
454  if (i2 == NMLONS) i2 = 0;
455  j2 = j1 + 1;
456 
457  return DTDB_SUCCESS;
458 }
459 
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
map< string, ddata * > read(map< string, ddata * > imap)
float wd
Definition: DDAncillary.h:28
int status
Definition: l1_czcs_hdf.c:32
const int NMLATS
Definition: DDAncillary.h:21
constexpr unsigned char DFILL_UBYTE
Definition: DDataset.hpp:28
int lon2index(const float lon)
@ UBYTE
unsigned 1 byte int
These are used to scale the SD before writing it to the HDF4 file The default is and which means the product is not scaled at all Since the product is usually stored as a float inside of this is a way to write the float out as a integer l2prod min
boost::multi_array< T, ndim > pts
Definition: DDataset.hpp:324
const string INPUT_GDAS1
Definition: DDOptions.cpp:42
float index2lat(const int index)
@ FLOAT
single precision floating point number
string get_option(const string &name)
Definition: DDOptions.cpp:211
float pw[NMLATS][NMLONS]
Definition: DDAncillary.h:34
float * lat
float oz[NMLATS][NMLONS]
Definition: DDAncillary.h:33
float v_ws[NMLATS][NMLONS]
Definition: DDAncillary.h:36
int read_merra2_file(const string filename, met_lut *lut)
integer function lat2index(lat, status)
float pw
Definition: DDAncillary.h:26
const string INPUT_GDAS2
Definition: DDOptions.cpp:43
double precision function f(R1)
Definition: tmd.lp.f:1454
const int NMLONS
Definition: DDAncillary.h:20
int lat2index(const float lat)
float oz
Definition: DDAncillary.h:25
#define M_PI
Definition: pml_iop.h:15
int initialize(int hr, int min)
Definition: DDAncillary.cpp:52
string filepath
Definition: color_dtdb.py:207
int get_interp_indexes(const float lat, const float lon, int &i1, int &i2, int &j1, int &j2)
const string INPUT_CLDMASK
Definition: DDOptions.cpp:46
data_t u
Definition: decode_rs.h:74
float ps
Definition: DDAncillary.h:24
@ INT
signed 4 byte integer
vector< size_t > count
Definition: DDataset.hpp:62
float * lon
const void * ptr
Definition: DDataset.hpp:64
Definition: RsViirs.h:71
float ws
Definition: DDAncillary.h:27
vector< size_t > start
Definition: DDataset.hpp:61
float u_ws[NMLATS][NMLONS]
Definition: DDAncillary.h:35
const string INPUT_GDAS
Definition: DDOptions.cpp:44
float ps[NMLATS][NMLONS]
Definition: DDAncillary.h:32
int get_met(const float lat, const float lon, metio *io)
float index2lon(const int index)