OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l1cgen.cpp
Go to the documentation of this file.
1 
2 //**********************************************************
3 //l1cgen firstly defined as mainL1C.cpp
4 // Originally created by Martin Montes on 3/27/2021 (first version)
5 // last version 3/7/2022
6 //**********************************************************
7 #include <allocate2d.h>
8 #include <allocate3d.h>
9 #include "allocate4d.h"
10 #include <iostream>
11 #include "l1c_input.h"
12 #include "l1c_filehandle.h"
13 #include "l1c_str.h"
14 #include "l1c.h"
15 #include "hawkeye_methods.h"
16 #include <chrono>
17 #include <sys/stat.h>
18 #include "l2_str.h"
19 
20 
21 using namespace std;
22 using namespace l1c; //l1c classes
23 using namespace std::chrono;
24 
25 
26 //#define VERSION "1.0.0"
27 
28 #define PROGRAM_NAME "l1cgen"
29 #define VERSION "5.3 3/24/2022"
30 
31 /* Global variables */
32 
33 int main(int argc, char** argv)
34 {
35  int status;
36 
38  const char* ptstr;
39  double etime;
40  string ifile_str;
41  char* ifile_char;
42 
43 
44  //------------------------------------------------------------------
46  int swtd = 1;
47  size_t num_scans, num_pixels;
48  double* time_mgv = nullptr;
49  float * lati = nullptr, * loni = nullptr, * lati2 = nullptr, * loni2 = nullptr,** lat_gd = nullptr, ** lon_gd = nullptr, * az_gd = nullptr;
50  double** lat_cgd, ** lon_cgd;
51  int32_t num_gridlines;
52  int nfiles;
53  double* tcross = nullptr, * ect_d = nullptr;
54  float* mgvswt = nullptr;
55  int16_t* tiod = nullptr, * odir = nullptr, * fileid = nullptr, * swtdid = nullptr, * nfiles_swt = nullptr;
56  int16_t c = 0;
57  float** pix_size_u, ** pix_size_v;
58  float** Ltfracsum = nullptr, ** areabinsum = nullptr, ** nobs_perbin = nullptr;
59  float** lat_asort = nullptr;
60  short** index_xy = nullptr;
61  vector<pair<float, int>> vp;
62  int NY1 = -1, NY2 = -1, NY = -1, NX = -1, c1 = 1;
63  char* diroutname="out/";
64  int check;
65  size_t sline;
66 
67 
68  L1C_input l1cinput;
69  l1c_filehandle l1cfile;
70  l1c_str l1cstr;
71  l2_str l2str;
72  L1C* ptl1c = new L1C();
73 //-------------------------------------------------------------------
74 
75  if (argc == 1)
76  {
77  l1cinput.l1c_usage(PROGRAM_NAME, VERSION);
78  return 1;
79  }
80 
81  // see if help on command line
82  for (int i = 0; i < argc; i++)
83  {
84  if ((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "-help") == 0))
85  {
86  l1cinput.l1c_usage(PROGRAM_NAME, VERSION);
87  }
88  }
89 
90  //grab CLI info
91  l1cinput.l1c_inputmain(argc, argv, &l1cinput, &l1cfile, PROGRAM_NAME, VERSION);
92 
93  ifile_str = l1cinput.files[0];
94  ifile_char = &ifile_str[0];
95  file_format format = getFormat(ifile_char);
96  l1cfile.format=format.type;
97 
98  //create output file dir
99  check=mkdir(diroutname,0777);//add error here in case the dir exists
100  if (check == -1)
101  // cerr << "Error : " << strerror(errno) << endl;
102  cout<<"the out directory was already created-------------------------------"<<endl;
103  else
104  cout << "Output Directory created..."<<diroutname<<endl;
105 
106  if (format.type != FT_INVALID)
107  {
108  if (ptl1c->load_l1c_filehandle4(&l1cfile, &l1cinput) != 0)
109  {
110  printf("-E- %s: Error loading %sl1c filehandle.\n", argv[0], l1cfile.l1b_name.c_str());
111  exit(1);
112  }
113 
114  //LEGACY OPTION 3 (binning area-weighting)
115  //******************************************************************************************************************************
116  if (format.type != FT_SPEXONE && format.type != FT_HARP2 & format.type != FT_HARP && l1cfile.l1c_pflag == 3)
117  {
118  int sfiles=0,fi=0;
119  cout<<"------------------------------area-weighting method--------------------"<<endl;
120  cout<<"*********************************legacy sensor ************************************"<<endl;
121  cout << "number of files to be processed for creating the L1C grid..." << l1cfile.ifiles.size() << endl;
122 
123  nfiles = l1cfile.ifiles.size();
124  try{
125  int result;
126  result=l1cfile.selgran[0];
127  if(result>0){
128  while(l1cfile.selgran[sfiles]>0){
129  cout<<"selected granule #..........................."<<l1cfile.selgran[sfiles]<<endl;
130  sfiles++;
131  }}
132  else{
133  throw(result);
134  }}
135  catch(int e){
136  if(e<0)//process all granules
137  {
138  cout<<"all L1B granules will be processed...."<<nfiles<<endl;
139  sfiles=nfiles;
140  cout<<"the number of files must be <=10!!..."<<endl;
141 
142  for(int j=0;j<sfiles;j++) l1cfile.selgran[j]=j+1; //the number of files must be <=10
143 
144  cout<<"ALL GRANULES are processed TOO IF selgran input argument was not setup in CLI ..please use selgran=-1 for binning ALL granules or selgran>0 for speccific granules"<<endl;
145  cout<<"otherwise all granules will be processed by default!!"<<endl;
146  }
147  if(e==0)//process all granules
148  {
149  cout<<"wrong granule #..."<<e<<"must be >0..."<<endl;
150  exit(1);
151  }
152  }
153 
154 
155 
156  tcross = (double*)calloc(nfiles,sizeof(double));
157 
158  //setting specific sensor params
159  l1cfile.nbinx = 514;
160 
161  for (int i = 0; i < nfiles; i++)
162  {
163 
164  str = l1cfile.ifiles[i];
165  ptstr = str.c_str();
166  cout << ptstr << endl;
167 
168  //computation of equatorial crossing time
169  if (status = ptl1c->ect_sf(ptstr, &l1cfile,&l1cinput) > 1)
170  {
171  printf("-E-%s: status=2 error computing equatorial crossing time%p for file%s.................\n", argv[0], tcross, ptstr);
172  exit(1);
173  }
174  tcross[i] = l1cfile.eqt;
175  if (l1cfile.eqt > 0)
176  {
177  c++;
178  }
179  }
180  cout << "number of eq crossings per day.(day and night).." << c << endl;
181  l1cfile.nswath = c;
182 
183  fileid = (int16_t*)calloc(nfiles,sizeof(int16_t));
184  swtdid = (int16_t*)calloc(nfiles,sizeof(int16_t));
185  ect_d = (double*)calloc(nfiles,sizeof(double));
186  nfiles_swt = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
187  tiod = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
188  odir = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
189  mgvswt = (float*)calloc(l1cfile.nswath,sizeof(float));
190 
191  //computation of mean orbital velocity and ground velocity for each swath
192  ptl1c->mov_sd2(&l1cfile, &l1cinput, tcross, fileid, swtdid, nfiles_swt, ect_d, tiod, odir, mgvswt);
193 
194  delete [](tcross);
195  delete [](tiod);
196 
197  num_scans = l1cfile.nscan;
198  num_pixels = l1cfile.npix;
199 
200  time_mgv = (double*)calloc(4000,sizeof(double)); //estimate of number of grids, assuming 4000 gridlines MAX...
201 
202  //compute time at each l1c gridpoint
203  ptl1c->time_swt2(swtd, &l1cfile, &l1cinput, ect_d, swtdid, fileid, nfiles_swt, mgvswt, time_mgv);
204  num_gridlines = l1cfile.num_gridlines;
205 
206  cout << "num of gridlines.before interpolation." << num_gridlines << endl;
207 
208  lat_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
209  lon_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
210  lati = (float*)calloc(num_gridlines,sizeof(float));
211  loni = (float*)calloc(num_gridlines,sizeof(float));
212  lati2 = (float*)calloc(num_gridlines,sizeof(float));
213  loni2 = (float*)calloc(num_gridlines,sizeof(float));
214  az_gd = (float*)calloc(num_gridlines,sizeof(float));
215  orb_array* velig = new orb_array[num_gridlines]();
216 
217  //first orbit interpolation---(ONLUY NADIR PIXEL ----
218  ptl1c->interp_swt1(swtd, &l1cfile,&l1cinput, swtdid, fileid, nfiles_swt, time_mgv, velig, lati, loni);
219  //second orbit interpolation---
220  ptl1c->interp_swt_dist3(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, time_mgv, lati, loni, lati2, loni2);
221  ptl1c->across_gridlines_l1c2(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, lati2, loni2, lat_gd, lon_gd, az_gd);
222 
223  for (int i = 0; i < num_gridlines-2; i++)
224  {
225  for (int j = 0; j < l1cfile.nbinx; j++)
226  {
227  if (lon_gd[i][j] < -180.){cout<<"lon_gd[i][j] < -180."<<endl;
228  lon_gd[i][j] += 360.;}
229  if (lon_gd[i][j] > 180.){cout<<"lon_gd[i][j] >180."<<endl;
230  lon_gd[i][j] -= 360.;}
231 
232 
233  }
234  }
235 
236 
237  NY = num_gridlines-2;
238  NX = l1cfile.nbinx;
239  for (int i = 0; i < NY; i++)
240  {
241  for (int j = 0; j < NX; j++)
242  {
243  if (lat_gd[i][j] >= -80.)
244  c1++;
245  if (c1 == l1cfile.nbinx)
246  {
247  NY1 = i;
248  }
249  }
250  if (NY1 >= 0)
251  break;
252  c1 = 1;
253  }
254 
255  for (int i = 0; i < NY; i++)
256  {
257  for (int j = 0; j < NX; j++)
258  {
259  if (lat_gd[i][j] >= 80.)
260  {
261  NY2 = i - 1;
262  }
263  }
264 
265  if (NY2 >= 0)
266  break;
267  }
268 
269  l1cfile.NY1 = NY1;
270  l1cfile.NY2 = NY2;
271 
272  cout << "original # gridlines.." << NY << "NY1.first gdline." << NY1 << "NY2..last gdline" << NY2 << endl;
273 
274  num_gridlines = NY2 - NY1 + 1; //this
275  l1cfile.num_gridlines=num_gridlines;
276 
277  cout << "*********** SORTING LAT/LON GRIDPOINTS AND TRACKING INDEXES ********************" << endl;
278  NY1 = l1cfile.NY1;
279  NY2 = l1cfile.NY2;
280 
281  index_xy = allocate2d_short(l1cfile.num_gridlines, l1cfile.nbinx);
282  lat_asort = allocate2d_float(l1cfile.num_gridlines, l1cfile.nbinx);
283 
284 
285  for (int col = 0; col < l1cfile.nbinx; col++)
286  {
287  for (int row = NY1; row < NY2 + 1; row++)
288  {
289  vp.push_back(make_pair(lat_gd[row][col] + 90., row));
290  }
291  //sort
292  stable_sort(vp.begin(), vp.end());
293 
294 
295  for (unsigned int i = 0; i < vp.size(); i++)
296  {
297  lat_asort[i][col] = vp[i].first;
298  index_xy[i][col] = vp[i].second;
299  }
300  vp.clear();
301  }
302 
303  cout<<"Sorting geo done!!!!!!!!!!!"<<endl;
304 
305  for(int g=0;g<sfiles;g++){
306  lat_cgd = allocate2d_double(l1cfile.num_gridlines+1,l1cfile.nbinx);
307  lon_cgd = allocate2d_double(l1cfile.num_gridlines+1,l1cfile.nbinx);
308  pix_size_u = allocate2d_float(num_scans-1,l1cfile.nbinx-1);
309  pix_size_v = allocate2d_float(num_scans-1,l1cfile.nbinx-1);
310  Ltfracsum = allocate2d_float(l1cfile.num_gridlines,l1cfile.nbinx);
311  areabinsum = allocate2d_float(l1cfile.num_gridlines,l1cfile.nbinx);
312  nobs_perbin = allocate2d_float(l1cfile.num_gridlines,l1cfile.nbinx);
313 
314  fi=l1cfile.selgran[g];
315  str=l1cfile.ifiles[fi-1];
316  ptstr = str.c_str();
317  cout<<"binning filename (NOT LINE-BY-LINE CALLING) =......."<<ptstr<<"selected granules #....."<<fi<<endl;
318  ptl1c->xy_pixsize_sf3(ptstr,&l1cfile,&l1cinput,pix_size_u,pix_size_v,lat_gd,lon_gd,lat_cgd,lon_cgd,Ltfracsum,areabinsum,nobs_perbin,lat_asort,index_xy);
319  cout<<"calling savebinL1C_v2.....producing nc files with area-weighted Lts................filename.=.."<<ptstr<<endl;
320  ptl1c->savebinL1C_v2(swtd,&l1cinput,&l1cfile,lat_gd,lon_gd,Ltfracsum,areabinsum,nobs_perbin);
321 
322  if (lat_cgd != nullptr)
323  delete [](lat_cgd);
324  if (lon_cgd != nullptr)
325  delete [](lon_cgd);
326  if (pix_size_u != nullptr)
327  delete [](pix_size_u);
328  if (pix_size_v != nullptr)
329  delete [](pix_size_v);
330  if (Ltfracsum != nullptr)
331  delete [](Ltfracsum);
332  if (areabinsum != nullptr)
333  delete [](areabinsum);
334  if (nobs_perbin != nullptr)
335  delete [](nobs_perbin);
336  }
337 
338  auto stop1 = high_resolution_clock::now();
339  auto duration = duration_cast<microseconds>(stop1 - start);
340  etime = duration.count();
341  etime /= (1000000 * 60);
342  cout << "Total run duration in minutes for aw weighting method = " << etime << endl;
343 
344  if (swtdid != nullptr)
345  delete [](swtdid);
346  if (odir != nullptr)
347  delete [](odir);
348  if (nfiles_swt != nullptr)
349  delete [](nfiles_swt);
350  if (ect_d != nullptr)
351  delete [](ect_d);
352  if (mgvswt != nullptr)
353  delete [](mgvswt);
354  if (time_mgv != nullptr)
355  delete [](time_mgv);
356  if (lati != nullptr)
357  delete [](lati);
358  if (loni != nullptr)
359  delete [](loni);
360  if (lati2 != nullptr)
361  delete [](lati2);
362  if (loni2 != nullptr)
363  delete [](loni2);
364  if (velig != nullptr)
365  delete [](velig);
366  if (lat_asort != nullptr)
367  delete [](lat_asort);
368  if (index_xy != nullptr)
369  delete [](index_xy);
370  if (fileid != nullptr)
371  delete [](fileid);
372  if (lat_gd != nullptr)
373  delete [](lat_gd);
374  if (lon_gd != nullptr)
375  delete [](lon_gd);
376  if (az_gd != nullptr)
377  delete [](az_gd);
378  if (ptl1c != nullptr)
379  delete ptl1c;
380 
381  auto stop2 = high_resolution_clock::now();
382  auto duration2 = duration_cast<microseconds>(stop2 - start);
383  etime = duration2.count();
384  etime /= (1000000 * 60);
385 
386  swtd++;
387  cout<<"done processing options #3..Binning granule based on area-weighting in "<<etime<<"minutes..."<<endl;
388  } //end if l1c_oflag<3
389 
390  //LEGACY OPTION 1 (L1C grid generation)
391  //**********************************************************************************************************************
392  else if (format.type != FT_SPEXONE && format.type != FT_HARP2 && format.type != FT_HARP && l1cfile.l1c_pflag == 1)
393  {
394  size_t sfiles=0;
395  cout << "producing L1C grid......................................................" << endl;
396 
397  nfiles = l1cfile.ifiles.size();
398 
399  try{
400  int result;
401  result=l1cfile.selgran[0];
402  if(result<0){
403  cout << "number of files to be processed..." << l1cfile.ifiles.size() << endl;
404  while(l1cfile.selgran[sfiles]<0 && sfiles<l1cfile.ifiles.size()){
405  l1cfile.selgran[sfiles]=sfiles+1;
406  cout<<"selected granule #..........................."<<l1cfile.selgran[sfiles]<<endl;
407  sfiles++;
408  }}
409  else{
410  throw(result);
411  }}
412 
413  catch(int e){
414  if(e>=0)//process all granules
415  {
416  cout<<"ERROR selgran>=0 ..all granules for the swath are used to produce the L1C grid, not specific ones.."<<"l1cfile.selgran[0]<=0...."<<e<<endl;
417  cout<<"DO NOT USE selgran option with l1c_pflag==1"<<endl;
418  exit(1);
419  }
420  }
421 
422  int16_t c = 0;
423  tcross = (double*)calloc(nfiles,sizeof(double));
424  l1cfile.nbinx = 514;
425 
426  for (int i = 0; i < nfiles; i++)
427  {
428  str = l1cfile.ifiles[i];
429  ptstr = str.c_str();
430 
431  if (status = ptl1c->ect_sf(ptstr, &l1cfile,&l1cinput) > 1)
432  {
433  printf("-E- %s: status=2 error computing equatorial crossing time%f for file%s.................\n", argv[0], tcross[i], ptstr);
434  exit(1);
435  }
436  tcross[i] = l1cfile.eqt;
437  if (l1cfile.eqt > 0)
438  {
439  c++;
440  }
441  }
442  cout << "number of eq crossings per day..." << c << endl;
443  l1cfile.nswath = c;
444 
445  fileid = (int16_t*)calloc(nfiles, sizeof(int16_t));
446  swtdid = (int16_t*)calloc(nfiles,sizeof(int16_t));
447  ect_d = (double*)calloc(nfiles,sizeof(double));
448  nfiles_swt = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
449  tiod = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
450  odir = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
451  mgvswt = (float*)calloc(l1cfile.nswath,sizeof(float)); //co
452 
453  ptl1c->mov_sd2(&l1cfile, &l1cinput, tcross, fileid, swtdid, nfiles_swt, ect_d, tiod, odir, mgvswt);
454  if (tcross != nullptr)
455  delete [](tcross);
456  if (tiod != nullptr)
457  delete [](tiod);
458  if (odir != nullptr)
459  delete [](odir);
460 
461  time_mgv = (double*)calloc(4000,sizeof(double)); //estimate of number of grids, assuming 4000 gridlines MAX...
462 
463  ptl1c->time_swt2(swtd, &l1cfile, &l1cinput, ect_d, swtdid, fileid, nfiles_swt, mgvswt, time_mgv);
464 
465  if (mgvswt != nullptr)
466  delete [](mgvswt);
467  if (ect_d != nullptr)
468  delete [](ect_d);
469 
470  num_gridlines = l1cfile.num_gridlines;
471  num_scans = l1cfile.nscan;
472  num_pixels = l1cfile.npix;
473  cout << "num of gridlines..before interpolation or (+ 2 lines)...." << num_gridlines << "num_pixels.." << num_pixels << endl;
474 
475  lat_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
476  lon_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
477  lati = (float*)calloc(num_gridlines,sizeof(float));
478  loni = (float*)calloc(num_gridlines,sizeof(float));
479  lati2 = (float*)calloc(num_gridlines,sizeof(float));
480  loni2 = (float*)calloc(num_gridlines,sizeof(float));
481  az_gd = (float*)calloc(num_gridlines,sizeof(float));
482  orb_array* velig = new orb_array[num_gridlines]();
483 
484  //first orbit interpolation---
485  ptl1c->interp_swt1(swtd, &l1cfile,&l1cinput, swtdid, fileid, nfiles_swt, time_mgv, velig, lati, loni);
486 
487  if (velig != nullptr)
488  delete [](velig);
489  //second interpolation
490  ptl1c->interp_swt_dist3(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, time_mgv, lati, loni, lati2, loni2);
491 
492  if (time_mgv != nullptr)
493  delete [](time_mgv);
494  if (lati != nullptr)
495  delete [](lati);
496  if (loni != nullptr)
497  delete [](loni);
498 
499  ptl1c->across_gridlines_l1c2(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, lati2, loni2, lat_gd, lon_gd, az_gd);
500 
501  if (lati2 != nullptr)
502  delete [](lati2);
503  if (loni2 != nullptr)
504  delete [](loni2);
505  if (lat_gd != nullptr)
506  delete [](lat_gd);
507  if (lon_gd != nullptr)
508  delete [](lon_gd);
509  if (az_gd != nullptr)
510  delete [](az_gd);
511  if (fileid != nullptr)
512  delete [](fileid);
513  if (swtdid != nullptr)
514  delete [](swtdid);
515  if (nfiles_swt != nullptr)
516  delete [](nfiles_swt);
517  if (ptl1c != nullptr)
518  delete ptl1c;
519 
520  auto stop1 = high_resolution_clock::now();
521  auto duration = duration_cast<microseconds>(stop1 - start);
522  etime = duration.count();
523  etime /= (1000000 * 60);
524  cout<<"done processing options #1..(creating L1C grid) in "<<etime<<"minutes..."<<endl;
525  }
526  //LEGACY OPTION 2 (binning SBS)
527  //**********************************************************************************************************************************************
528  else if (format.type != FT_SPEXONE && format.type != FT_HARP2 && format.type != FT_HARP && l1cfile.l1c_pflag == 2)
529  {
530  short** index_xy=nullptr;
531  float** lat_asort=nullptr,** lat_gd=nullptr, ** lon_gd=nullptr, * az_east=nullptr,****binLt=nullptr;
532  int ****bincount=nullptr;
533  int sfiles=0,fi=0;
534 
535  cout<<"------------------------------SBS method--------------------"<<endl;
536  cout<<"processing legacy sensor..."<<"l1cgen option #...."<<l1cfile.l1c_pflag<<endl;
537  cout << "loading L1C grid and processing L1C granule............................................................................." << endl;
538 
539  nfiles=l1cfile.ifiles.size();
540 
541  try{
542  int result;
543  result=l1cfile.selgran[0];
544  if(result>0){
545  while(l1cfile.selgran[sfiles]>0){
546  cout<<"selected granule #..........................."<<l1cfile.selgran[sfiles]<<endl;
547  sfiles++;
548  }}
549  else{
550  throw(result);
551  }}
552  catch(int e){
553  if(e<0)//process all granules
554  {
555  cout<<"all L1B granules will be processed...."<<nfiles<<endl;
556  cout<<"the number of files must be <=10!!..."<<endl;
557  sfiles=nfiles;
558  for(int j=0;j<sfiles;j++) l1cfile.selgran[j]=j+1; //the number of files must be <=10
559  }
560  if(e==0)//process all granules
561  {
562  cout<<"wrong granule #..."<<e<<"must be >0..."<<endl;
563  exit(1);
564  }
565  }
566 
567  cout<<"number of granules to be processed...."<<sfiles<<endl;
568 
569 
570 
571  l1cfile.nswath = 1;//#of equat crossings
572 
573  swtdid = (int16_t*)calloc(nfiles , sizeof(int16_t));
574  odir = (int16_t*)calloc(l1cfile.nswath , sizeof(int16_t));
575  fileid = (int16_t*)calloc(nfiles, sizeof(int16_t));
576  nfiles_swt = (int16_t*)calloc(l1cfile.nswath , sizeof(int16_t));
577 
578  nfiles_swt[0]=nfiles;
579  odir[0]=1;
580  int swtd=1;//swath id
581  swtdid[0]=1;
582 
583  l1cfile.nbinx=514;
584  az_east = (float*)calloc(4000 , sizeof(float));
585  lat_gd = allocate2d_float(4000, l1cfile.nbinx);
586  lon_gd = allocate2d_float(4000, l1cfile.nbinx);
587  index_xy = allocate2d_short(4000, l1cfile.nbinx);
588  lat_asort = allocate2d_float(4000, l1cfile.nbinx);
589 
590 
591  ptl1c->openL1Cgrid(swtd,&l1cstr,&l1cfile,&l1cinput,swtdid,fileid,nfiles_swt,lat_gd,lon_gd,az_east,lat_asort,index_xy);
592 
593  cout<<"OK after sorting latitudes..."<<endl;
594 //granule loop
595  for(int j=0;j<sfiles;j++){
596  fi=l1cfile.selgran[j];
597  l1cfile.l1b_name=l1cfile.ifiles[fi-1];
598  cout<<"binning granule....................................................................................."<<fi<<"filename......................................................#"<<l1cfile.l1b_name<<endl;
599 
600  if(status = l1cstr.openl1b_ocis_l1c(&l1cstr, &l1cfile, fileid)>1){
601  printf("-E- %d: status=2 error opening..L1B file for sensor...............\n", format.type);
602  exit(1);
603  }
604 
605  num_scans = l1cfile.nscan;
606 
607  binLt = allocate4d_float(l1cfile.n_views,l1cfile.nbands,l1cfile.num_gridlines,l1cfile.nbinx);
608  bincount = allocate4d_int(l1cfile.n_views,l1cfile.nbands,l1cfile.num_gridlines,l1cfile.nbinx);
609 
610  for(size_t v=0;v<l1cfile.n_views;v++){
611  for(size_t b=0;b<l1cfile.nbands;b++){
612  for(int gd=0;gd<l1cfile.num_gridlines;gd++){
613  for(int xbin=0;xbin<l1cfile.nbinx;xbin++){
614  binLt[v][b][gd][xbin]=0;
615  bincount[v][b][gd][xbin]=0;
616  }}}}
617 
618  for (sline = 0; sline < num_scans; sline++)
619  {
620  if( status = l1cstr.readl1b_ocis_l1c(&l1cstr, &l1cfile, fileid, sline)>1)
621  {
622  printf("-E- %lu: status=2 error reading line#%d..from L1B file for sensor...............\n", sline,format.type);
623  exit(1);
624  }
625  ptl1c->binL1C_sbs_line(swtd,ptl1c,&l1cstr,&l1cfile,&l1cinput,swtdid,fileid,nfiles_swt,lat_gd,lon_gd,az_east,lat_asort,index_xy,binLt,bincount,sline,fi);
626  if (sline % 100 == 0) cout <<"line #..."<<sline+1<<endl;
627  }//for line
628 
629  if(status=l1cstr.closel1b_ocis_l1c(&l1cstr,&l1cfile)>1)
630  {
631  printf("-E- %d: status=2 error closing L1B file for sensor...............\n",format.type);
632  exit(1);
633  }
634  if(binLt!=nullptr)
635  delete [] (binLt);
636  if(bincount!=nullptr)
637  delete [] (bincount);
638  }
639 
640  if (lat_gd != nullptr)
641  delete[](lat_gd);
642  if (lon_gd != nullptr)
643  delete[](lon_gd);
644  if (az_east != nullptr)
645  delete[](az_east);
646  if (lat_asort != nullptr)
647  delete[](lat_asort);
648  if (index_xy != nullptr)
649  delete[](index_xy);
650  if (ptl1c != nullptr)
651  delete ptl1c;
652  if (fileid != nullptr)
653  delete [](fileid);
654  if (swtdid != nullptr)
655  delete [](swtdid);
656  if (odir != nullptr)
657  delete [](odir);
658  if (nfiles_swt != nullptr)
659  delete [](nfiles_swt);
660 
661 
662  auto stop1 = high_resolution_clock::now();
663  auto duration = duration_cast<microseconds>(stop1 - start);
664  etime = duration.count();
665  etime /= (1000000 * 60);
666  cout<<"done processing options #2..(Binning granule using saddleback search processing (LINE-BY-LINE)"<<etime<<"minutes..."<<endl;
667  }
668  //NON-LEGACY SPEXONE OPTION 1 (L1C grid generation)
669  else if (format.type == FT_SPEXONE && l1cfile.l1c_pflag == 1)
670  { //non-legacy sensor
671  printf("create L1C grid for non-legacy sensor SPEXone\n");
672  cout << "number of files to be processed..." << l1cfile.ifiles.size() << endl;
673  nfiles = l1cfile.ifiles.size();
674  tcross = (double*)calloc(nfiles,sizeof(double));
675 
676  l1cfile.nbinx = 40;
677 
678  for (int i = 0; i < nfiles; i++)
679  {
680  str = l1cfile.ifiles[i];
681  ptstr = str.c_str();
682  cout << ptstr << endl;
683 
684  if (status = ptl1c->ect_sf2(ptstr, &l1cinput,&l1cfile) > 1)
685  {
686  printf("-E- %s: status=2 error computing equatorial crossing time%f for file%s.................\n", argv[0], tcross[i], ptstr);
687  exit(1);
688  }
689  tcross[i] = l1cfile.eqt;
690  if (l1cfile.eqt > 0)
691  {
692  c++;
693  cout<<"equat crossing time..."<<l1cfile.eqt<<" for file#.."<<i+1<<endl;
694  }
695  }
696  cout << "number of eq crossings per day..." << c << endl;
697  l1cfile.nswath = c;
698 
699  fileid = (int16_t*)calloc(nfiles, sizeof(int16_t));
700  swtdid = (int16_t*)calloc(nfiles,sizeof(int16_t));
701  ect_d = (double*)calloc(nfiles,sizeof(double));
702  nfiles_swt = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
703  tiod = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
704  odir = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
705  mgvswt = (float*)calloc(l1cfile.nswath,sizeof(float));
706 
707  ptl1c->mov_sd3(&l1cfile, &l1cinput, tcross, fileid, swtdid, nfiles_swt, ect_d, tiod, odir, mgvswt);
708  if (tcross != nullptr)
709  delete [](tcross);
710  if (tiod != nullptr)
711  delete [](tiod);
712  if (odir != nullptr)
713  delete [](odir);
714 
715  time_mgv = (double*)calloc(4000,sizeof(double));
716 
717  ptl1c->time_swt2(swtd, &l1cfile, &l1cinput, ect_d, swtdid, fileid, nfiles_swt, mgvswt, time_mgv);
718 
719  if (mgvswt != nullptr)
720  delete [](mgvswt);
721  if (ect_d != nullptr)
722  delete [](ect_d);
723 
724  num_gridlines = l1cfile.num_gridlines;
725  num_scans = l1cfile.nscan;
726  num_pixels = l1cfile.npix;
727  cout << "num of gridlines.." << num_gridlines << "num_pixels.." << num_pixels << endl;
728 
729  lat_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
730  lon_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
731  lati = (float*)calloc(num_gridlines,sizeof(float));
732  loni = (float*)calloc(num_gridlines,sizeof(float));
733  lati2 = (float*)calloc(num_gridlines,sizeof(float));
734  loni2 = (float*)calloc(num_gridlines,sizeof(float));
735  az_gd = (float*)calloc(num_gridlines,sizeof(float));
736  orb_array* velig = new orb_array[num_gridlines]();
737 
738  //first orbit interpolation---
739  ptl1c->interp_swt1(swtd, &l1cfile,&l1cinput, swtdid, fileid, nfiles_swt, time_mgv, velig, lati, loni);
740  if (velig != nullptr)
741  delete [](velig);
742  //second orbit interpolation---
743  ptl1c->interp_swt_dist3(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, time_mgv, lati, loni, lati2, loni2);
744 
745  if (time_mgv != nullptr)
746  delete [](time_mgv);
747  if (lati != nullptr)
748  delete [](lati);
749  if (loni != nullptr)
750  delete [](loni);
751 
752  ptl1c->across_gridlines_l1c2(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, lati2, loni2, lat_gd, lon_gd, az_gd);
753 
754  if (lati2 != nullptr)
755  delete [](lati2);
756  if (loni2 != nullptr)
757  delete [](loni2);
758  if (lat_gd != nullptr)
759  delete [](lat_gd);
760  if (lon_gd != nullptr)
761  delete [](lon_gd);
762  if (az_gd != nullptr)
763  delete [](az_gd);
764  if (fileid != nullptr)
765  delete [](fileid);
766  if (swtdid != nullptr)
767  delete [](swtdid);
768  if (nfiles_swt != nullptr)
769  delete [](nfiles_swt);
770  if (ptl1c != nullptr)
771  delete ptl1c;
772  }
773  //NON-LEGACY SPEXONE OPTION 2 (SBS binning)
774  else if (format.type == FT_SPEXONE && l1cfile.l1c_pflag == 2)
775  {
776  printf("binning Lt using L1C grid for SPEXone\n");
777  cout << "number of files to be processed..." << l1cfile.ifiles.size() << endl;
778 
779  nfiles = l1cfile.ifiles.size();
780  tcross = (double*)calloc(nfiles,sizeof(double));
781  l1cfile.nbinx = 40;
782 
783  for (int i = 0; i < nfiles; i++)
784  {
785  str = l1cfile.ifiles[i];
786  ptstr = str.c_str();
787  cout << ptstr << endl;
788 
789  if (status = ptl1c->ect_sf2(ptstr, &l1cinput,&l1cfile) > 1)
790  {
791  printf("-E- %s: status=2 error computing equatorial crossing time%f for file%s.................\n", argv[0], tcross[i], ptstr);
792  exit(1);
793  }
794  tcross[i] = l1cfile.eqt;
795  if (l1cfile.eqt > 0)
796  {
797  c++;
798  cout<<"equat crossing time..."<<l1cfile.eqt<<" for file#.."<<i+1<<endl;
799  }
800  }
801  cout << "number of eq crossings per day..." << c << endl;
802  l1cfile.nswath = c;
803 
804  fileid = (int16_t*)calloc(nfiles, sizeof(int16_t));
805  swtdid = (int16_t*)calloc(nfiles,sizeof(int16_t));
806  ect_d = (double*)calloc(nfiles,sizeof(double));
807  nfiles_swt = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
808  tiod = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
809  odir = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
810  mgvswt = (float*)calloc(l1cfile.nswath,sizeof(float));
811 
812  ptl1c->mov_sd4(&l1cfile, &l1cinput, tcross, fileid, swtdid, nfiles_swt, ect_d, tiod, odir, mgvswt);
813 
814  if (tcross != nullptr)
815  delete [](tcross);
816  if (tiod != nullptr)
817  delete [](tiod);
818  if (odir != nullptr)
819  delete [](odir);
820 
821  time_mgv = (double*)calloc(4000,sizeof(double));
822 
823  if (mgvswt != nullptr)
824  delete [](mgvswt);
825  if (ect_d != nullptr)
826  delete [](ect_d);
827 
828  num_gridlines=4000;//first guest
829  num_scans = l1cfile.nscan;
830  num_pixels = l1cfile.npix;
831  cout << "num of gridlines.." << num_gridlines << "num_pixels.." << num_pixels << endl;
832 
833  lat_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
834  lon_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
835  lati = (float*)calloc(num_gridlines,sizeof(float));
836  loni = (float*)calloc(num_gridlines,sizeof(float));
837  lati2 = (float*)calloc(num_gridlines,sizeof(float));
838  loni2 = (float*)calloc(num_gridlines,sizeof(float));
839  az_gd = (float*)calloc(num_gridlines,sizeof(float));
840 
841  if (time_mgv != nullptr)
842  delete [](time_mgv);
843  if (lati != nullptr)
844  delete [](lati);
845  if (loni != nullptr)
846  delete [](loni);
847 
848  ptl1c->binL1C_wgranule6(swtd,&l1cfile,&l1cinput,swtdid,odir,fileid,nfiles_swt);
849 
850  auto stop1 = high_resolution_clock::now();
851  auto duration = duration_cast<microseconds>(stop1 - start);
852  etime = duration.count();
853  etime /= (1000000 * 60);
854  cout << "Run duration in minutes after saddleback search binning "<<etime<< endl;
855 
856  if (lati2 != nullptr)
857  delete [](lati2);
858  if (loni2 != nullptr)
859  delete [](loni2);
860  if (lat_gd != nullptr)
861  delete [](lat_gd);
862  if (lon_gd != nullptr)
863  delete [](lon_gd);
864  if (az_gd != nullptr)
865  delete [](az_gd);
866  if (fileid != nullptr)
867  delete [](fileid);
868  if (swtdid != nullptr)
869  delete [](swtdid);
870  if (nfiles_swt != nullptr)
871  delete [](nfiles_swt);
872 
873  if (ptl1c != nullptr)
874  delete ptl1c;
875  }
876 
877  //NON-LEGACY HARP OPTION 1 (L1C grid generation)
878  else if (format.type == FT_HARP && l1cfile.l1c_pflag == 1){
879  printf("create L1C grid for non-legacy sensor HARP");
880  cout << "number of files to be processed..." << l1cfile.ifiles.size() << endl;
881 
882  nfiles = l1cfile.ifiles.size();
883  tcross = (double*)calloc(nfiles,sizeof(double));
884 
885  l1cfile.nbinx = 600;
886 
887  for (int i = 0; i < nfiles; i++)
888  {
889  str = l1cfile.ifiles[i];
890  ptstr = str.c_str();
891  cout << ptstr << endl;
892  if (status = ptl1c->ect_sf2(ptstr, &l1cinput,&l1cfile) > 1)
893  {
894  printf("-E- %s: status=2 error computing equatorial crossing time%f for file%s.................\n", argv[0], tcross[i], ptstr);
895  exit(1);
896  }
897  tcross[i] = l1cfile.eqt;
898  if (l1cfile.eqt > 0)
899  {
900  c++;
901  }
902 
903  }
904 
905  cout << "number of eq crossings per day..." << c << endl;
906  l1cfile.nswath = c;
907 
908  fileid = (int16_t*)calloc(nfiles,sizeof(int16_t));
909  swtdid = (int16_t*)calloc(nfiles,sizeof(int16_t));
910  nfiles_swt = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
911  ect_d = (double*)calloc(nfiles,sizeof(double));
912  tiod = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
913  odir = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
914  mgvswt = (float*)calloc(l1cfile.nswath,sizeof(float));
915 
916  ptl1c->mov_sd3(&l1cfile, &l1cinput, tcross, fileid, swtdid, nfiles_swt, ect_d, tiod, odir, mgvswt);
917 
918  time_mgv = (double*)calloc(4000,sizeof(double));
919  ptl1c->time_swt3(swtd, &l1cfile, &l1cinput, ect_d, swtdid, fileid, nfiles_swt, mgvswt, time_mgv);
920 
921  if (mgvswt != nullptr)
922  delete [](mgvswt);
923  if (ect_d != nullptr)
924  delete [](ect_d);
925 
926  num_gridlines = l1cfile.num_gridlines;
927  num_scans = l1cfile.nscan;
928  num_pixels = l1cfile.npix;
929  cout << "num of gridlines.." << num_gridlines << "num_pixels.." << num_pixels << endl;
930 
931  lat_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
932  lon_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
933  lati = (float*)calloc(num_gridlines,sizeof(float));
934  loni = (float*)calloc(num_gridlines,sizeof(float));
935  lati2 = (float*)calloc(num_gridlines,sizeof(float));
936  loni2 = (float*)calloc(num_gridlines,sizeof(float));
937  az_gd = (float*)calloc(num_gridlines,sizeof(float));
938  orb_array* velig = new orb_array[num_gridlines]();
939 
940  ptl1c->interp_swt1(swtd, &l1cfile,&l1cinput, swtdid, fileid, nfiles_swt, time_mgv, velig, lati, loni);
941  if (velig != nullptr)
942  delete [](velig);
943 
944  ptl1c->interp_swt_dist3(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, time_mgv, lati, loni, lati2, loni2);
945 
946  if (time_mgv != nullptr)
947  delete [](time_mgv);
948  if (lati != nullptr)
949  delete [](lati);
950  if (loni != nullptr)
951  delete [](loni);
952 
953  ptl1c->across_gridlines_l1c2(swtd, &l1cfile, &l1cinput, swtdid, fileid, nfiles_swt, lati2, loni2, lat_gd, lon_gd, az_gd);
954 
955  if (lati2 != nullptr)
956  delete [](lati2);
957  if (loni2 != nullptr)
958  delete [](loni2);
959  if (lat_gd != nullptr)
960  delete [](lat_gd);
961  if (lon_gd != nullptr)
962  delete [](lon_gd);
963  if (az_gd != nullptr)
964  delete [](az_gd);
965  if (ptl1c != nullptr)
966  delete ptl1c;
967  if (fileid != nullptr)
968  delete [] (fileid);
969  if (swtdid != nullptr)
970  delete [] (swtdid);
971  if (nfiles_swt != nullptr)
972  delete [] (nfiles_swt);
973  if (ect_d != nullptr)
974  delete [] (ect_d);
975  if (tiod != nullptr)
976  delete [] (tiod);
977  if (odir != nullptr)
978  delete [] (odir);
979  if (mgvswt != nullptr)
980  delete [] (mgvswt);
981  }
982  //NON-LEGACY HARP OPTION 2 (SBS binning)
983  else if (format.type == FT_HARP && l1cfile.l1c_pflag == 2){
984  printf("binning Lt using L1C grid for HARP\n");
985  cout << "number of files to be processed..." << l1cfile.ifiles.size() << endl;
986 
987  nfiles = l1cfile.ifiles.size();
988  tcross = (double*)calloc(nfiles,sizeof(double));
989 
990  // l1cfile.nbinx = 600;
991  l1cfile.nbinx = 514;
992 
993 
994  for (int i = 0; i < nfiles; i++)
995  {
996  str = l1cfile.ifiles[i];
997  ptstr = str.c_str();
998  cout << ptstr << endl;
999  }
1000  c=1;
1001  cout << "number of eq crossings per day..." << c << endl;
1002  l1cfile.nswath = c;
1003 
1004  fileid = (int16_t*)calloc(nfiles, sizeof(int16_t));
1005  swtdid = (int16_t*)calloc(nfiles,sizeof(int16_t));
1006  ect_d = (double*)calloc(nfiles,sizeof(double));
1007  nfiles_swt = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
1008  tiod = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
1009  odir = (int16_t*)calloc(l1cfile.nswath,sizeof(int16_t));
1010  mgvswt = (float*)calloc(l1cfile.nswath,sizeof(float));
1011 
1012  if (tcross != nullptr)
1013  delete [](tcross);
1014  if (tiod != nullptr)
1015  delete [](tiod);
1016  if (odir != nullptr)
1017  delete [](odir);
1018  if (mgvswt != nullptr)
1019  delete [](mgvswt);
1020  if (ect_d != nullptr)
1021  delete [](ect_d);
1022 
1023  num_gridlines=4000;//max number of gridlines , not computed or restricted
1024 
1025  lat_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
1026  lon_gd = allocate2d_float(num_gridlines, l1cfile.nbinx);
1027  lati = (float*)calloc(num_gridlines,sizeof(float));
1028  loni = (float*)calloc(num_gridlines,sizeof(float));
1029  lati2 = (float*)calloc(num_gridlines,sizeof(float));
1030  loni2 = (float*)calloc(num_gridlines,sizeof(float));
1031  az_gd = (float*)calloc(num_gridlines,sizeof(float));
1032 
1033  if (time_mgv != nullptr)
1034  delete [](time_mgv);
1035  if (lati != nullptr)
1036  delete [](lati);
1037  if (loni != nullptr)
1038  delete [](loni);
1039 
1040  swtdid[0]=1;
1041  ptl1c->binL1C_wgranule7(swtd,&l1cfile,&l1cinput,swtdid,odir,fileid,nfiles_swt);//sbs2
1042 
1043  auto stop1 = high_resolution_clock::now();
1044  auto duration = duration_cast<microseconds>(stop1 - start);
1045  etime = duration.count();
1046  etime /= (1000000 * 60);
1047  cout << "Run duration in minutes after saddleback search binning "<<etime<< endl;
1048 
1049  if (lati2 != nullptr)
1050  delete [](lati2);
1051  if (loni2 != nullptr)
1052  delete [](loni2);
1053  if (lat_gd != nullptr)
1054  delete [](lat_gd);
1055  if (lon_gd != nullptr)
1056  delete [](lon_gd);
1057  if (az_gd != nullptr)
1058  delete [](az_gd);
1059  if (fileid != nullptr)
1060  delete [](fileid);
1061  if (swtdid != nullptr)
1062  delete [](swtdid);
1063  if (nfiles_swt != nullptr)
1064  delete [](nfiles_swt);
1065 
1066  if (ptl1c != nullptr)
1067  delete ptl1c;
1068  }
1069  //----- READING L2 FILES ---READING WITH SDS --------------------------------------------------------------
1070  //**********************************************************************************************************-
1071  else if (l1cfile.l1c_pflag == 4){
1072  short** index_xy=nullptr;
1073  float** lat_asort=nullptr,** lat_gd=nullptr, ** lon_gd=nullptr, * az_east=nullptr,****binprod=nullptr;
1074  int ****bincount=nullptr;
1075  int sfiles=0,fi=0;
1076 
1077  cout<<"processing L2 file to L1C ----------"<<endl;
1078  cout<<"SDS binning products........"<<l1cinput.l2prod<<endl;
1079 
1080  cout<<"------------------------------SBS method--------------------"<<endl;
1081  cout<<"processing legacy sensor..."<<"l1cgen option #...."<<l1cfile.l1c_pflag<<endl;
1082  cout << "loading L1C grid and processing L2 file to L1C granule............................................................................." << endl;
1083 
1084  nfiles=l1cfile.ifiles.size();
1085 
1086  try{
1087  int result;
1088  result=l1cfile.selgran[0];
1089  if(result>0){
1090  while(l1cfile.selgran[sfiles]>0){
1091  cout<<"selected granule #..........................."<<l1cfile.selgran[sfiles]<<endl;
1092  sfiles++;
1093  }}
1094  else{
1095  throw(result);
1096  }}
1097  catch(int e){
1098  if(e<0)//process all granules
1099  {
1100  cout<<"all L2 granules will be processed...."<<nfiles<<endl;
1101  sfiles=nfiles;
1102  }
1103  if(e==0)//process all granules
1104  {
1105  cout<<"wrong granule #..."<<e<<"must be >0..."<<endl;
1106  exit(1);
1107  }
1108  }
1109 
1110  cout<<"number of granules to be processed...."<<sfiles<<endl;
1111 
1112 
1113 
1114  l1cfile.nswath = 1;//#of equat crossings
1115 
1116  swtdid = (int16_t*)calloc(nfiles , sizeof(int16_t));
1117  odir = (int16_t*)calloc(l1cfile.nswath , sizeof(int16_t));
1118  fileid = (int16_t*)calloc(nfiles, sizeof(int16_t));
1119  nfiles_swt = (int16_t*)calloc(l1cfile.nswath , sizeof(int16_t));
1120 
1121  nfiles_swt[0]=nfiles;
1122  int swtd=1;//swath id
1123  swtdid[0]=1;
1124  odir[0]=1;
1125 
1126  l1cfile.nbinx=514;
1127 
1128  az_east = (float*)calloc(4000 , sizeof(float));
1129  lat_gd = allocate2d_float(4000, l1cfile.nbinx);
1130  lon_gd = allocate2d_float(4000, l1cfile.nbinx);
1131  index_xy = allocate2d_short(4000, l1cfile.nbinx);
1132  lat_asort = allocate2d_float(4000, l1cfile.nbinx);
1133 
1134 
1135  ptl1c->openL1Cgrid(swtd,&l1cstr,&l1cfile,&l1cinput,swtdid,fileid,nfiles_swt,lat_gd,lon_gd,az_east,lat_asort,index_xy);
1136 
1137 //granule loop
1138 
1139  for(int j=0;j<sfiles;j++){
1140  l1cfile.l1b_name=l1cfile.ifiles[0];
1141  cout<<"binning filename......................................................................................#"<<l1cfile.l1b_name<<endl;
1142 
1143  if(status = l2str.openl2_ocis_l1c(&l1cinput,&l2str,&l1cfile,fileid)>1){
1144  printf("-E- %d: status=2 error opening..L2B file for sensor...............\n", format.type);
1145  exit(1);
1146  }
1147 
1148  num_scans = l1cfile.nscan;
1149 
1150  binprod = allocate4d_float(l1cfile.n_views,l2str.nl2prod,l1cfile.num_gridlines,l1cfile.nbinx);
1151  bincount = allocate4d_int(l1cfile.n_views,l2str.nl2prod,l1cfile.num_gridlines,l1cfile.nbinx);
1152 
1153 
1154  for(size_t v=0;v<l1cfile.n_views;v++){
1155  for(size_t p=0;p<l2str.nl2prod;p++){
1156  for(int gd=0;gd<l1cfile.num_gridlines;gd++){
1157  for(int xbin=0;xbin<l1cfile.nbinx;xbin++){
1158  binprod[v][p][gd][xbin]=0.0;
1159  bincount[v][p][gd][xbin]=0.0;
1160  }}}}
1161 
1162 
1163  cout<<"number of views.."<<l1cfile.n_views<<"number of products.."<<l2str.nl2prod<<"#gridlines.."<<l1cfile.num_gridlines<<"number of xbins.."<<l1cfile.nbinx<<endl;
1164 
1165 
1166  for (sline = 0; sline < num_scans; sline++)
1167  {
1168  if( status = l2str.readl2_ocis_l1c(&l2str, &l1cfile, fileid, sline)>1)
1169  {
1170  printf("-E- %lu: status=2 error reading line#%d..from L2 file for sensor...............\n", sline,format.type);
1171  exit(1);
1172  }
1173  ptl1c->binL1C_sbs_line_l2(swtd,ptl1c,&l2str,&l1cfile,&l1cinput,swtdid,fileid,nfiles_swt,lat_gd,lon_gd,az_east,lat_asort,index_xy,binprod,bincount,sline,fi);
1174  if (sline % 100 == 0) cout <<"line #..."<<sline+1<<endl;
1175  }
1176 
1177  if(status=l2str.closel2_ocis_l1c(&l2str,&l1cfile)>1)
1178  {
1179  printf("-E- %d: status=2 error closing L2 file for sensor...............\n",format.type);
1180  exit(1);
1181  }
1182  if(binprod!=nullptr)
1183  delete [] (binprod);
1184  if(bincount!=nullptr)
1185  delete [] (bincount);
1186  }
1187 
1188  if (lat_gd != nullptr)
1189  delete[](lat_gd);
1190  if (lon_gd != nullptr)
1191  delete[](lon_gd);
1192  if (az_east != nullptr)
1193  delete[](az_east);
1194  if (lat_asort != nullptr)
1195  delete[](lat_asort);
1196  if (index_xy != nullptr)
1197  delete[](index_xy);
1198  if (ptl1c != nullptr)
1199  delete ptl1c;
1200  if (fileid != nullptr)
1201  delete [](fileid);
1202  if (swtdid != nullptr)
1203  delete [](swtdid);
1204  if (odir != nullptr)
1205  delete [](odir);
1206  if (nfiles_swt != nullptr)
1207  delete [](nfiles_swt);
1208 
1209  auto stop1 = high_resolution_clock::now();
1210  auto duration = duration_cast<microseconds>(stop1 - start);
1211  etime = duration.count();
1212  etime /= (1000000 * 60);
1213  cout<<"done processing options #4..(Binning L2 file using saddleback search processing "<<etime<<"minutes..."<<endl;
1214 
1215  }
1216  //----- READING L2 FILES ---READING WITH SDS --------------------------------------------------------------
1217  //**********************************************************************************************************-
1218  else if (l1cfile.l1c_pflag == 5){
1219  cout<<"Reading telemetry from L1A files..--> SOCEA -- L1C grid.."<<endl;
1220 
1221  if(status = ptl1c->open_l1atol1c(&l1cinput,&l1cfile)>1){
1222  printf("-E- %d: status=2 error opening..L1A file for sensor...............\n", format.type);
1223  exit(1);
1224  }
1225 
1226  if (ptl1c != nullptr)
1227  delete ptl1c;
1228 
1229  auto stop1 = high_resolution_clock::now();
1230  auto duration = duration_cast<microseconds>(stop1 - start);
1231  etime = duration.count();
1232  etime /= (1000000 * 60);
1233  cout<<"done processing options #5..(creating SOCEA L1C common grid from telemetry "<<etime<<"minutes..."<<endl;
1234  }
1235 
1236  } //end ft diff of invalid
1237  else
1238  {
1239  printf("-E- %s: Error opening %s for reading unknown sensor...................\n", argv[0], l1cfile.l1b_name.c_str());
1240  return (1);
1241  }
1242 
1243  return (0);
1244 }
virtual int32_t readl1b_ocis_l1c(l1c_str *l1cstr, l1c_filehandle *l1cfile, int16_t *file_id, int32_t recnum)
Definition: l1c_str.cpp:413
int main(int argc, char **argv)
Definition: l1cgen.cpp:33
virtual int32_t time_swt2(int swtd, l1c_filehandle *l1cfile, L1C_input *l1cinput, double *ect_d, int16_t *swtdid, int16_t *fileid, int16_t *nfiles_swt, float *mgv_swt, double *time_mgv)
Definition: l1c.cpp:22621
virtual int32_t savebinL1C_v2(int swtd, L1C_input *l1cinput, l1c_filehandle *l1cfile, float **lat_gd, float **lon_gd, float **Ltfracsum, float **areafracsum, float **nobs_perbin)
Definition: l1c.cpp:2388
Utility functions for allocating and freeing three-dimensional arrays of various types.
#define NX
Definition: main_biosmap.c:51
int j
Definition: decode_rs.h:73
virtual int32_t binL1C_sbs_line_l2(int swtd, L1C *l1c, l2_str *l2str, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *file_id, int16_t *nfiles_swt, float **lat_gd, float **lon_gd, float *az_east, float **lat_asort, short **index_xy, float ****binmean_prod, int ****bincount, size_t sline, int granid)
Definition: l1c.cpp:5893
int status
Definition: l1_czcs_hdf.c:32
virtual int32_t binL1C_sbs_line(int swtd, L1C *l1c, l1c_str *l1cstr, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *file_id, int16_t *nfiles_swt, float **lat_gd, float **lon_gd, float *az_east, float **lat_asort, short **index_xy, float ****binLt, int ****bincount, size_t recnums, int granid)
Definition: l1c.cpp:6374
float **** allocate4d_float(size_t nr, size_t nz, size_t ny, size_t nx)
Allocate a four-dimensional array of type float of a given size.
Definition: allocate4d.c:56
@ FT_HARP
Definition: filetype.h:62
float orb_array[3]
virtual int32_t ect_sf2(const char *filename, L1C_input *l1cinput, l1c_filehandle *l1cfile)
Definition: l1c.cpp:23904
char l2prod[2048]
Definition: l1c_input.h:42
std::string l1b_name
size_t nl2prod
Definition: l2_str.h:56
virtual int32_t xy_pixsize_sf3(const char *filename, l1c_filehandle *l1cfile, L1C_input *l1cinput, float **pix_size_u, float **pix_size_v, float **lat_gd, float **lon_gd, double **lat_cgd, double **lon_cgd, float **Ltfracsum, float **areabinsum, float **nobs_perbin, float **lat_asort, short **index_xy)
Definition: l1c.cpp:16258
virtual int32_t open_l1atol1c(L1C_input *l1cinput, l1c_filehandle *l1cfile)
Definition: l1c.cpp:560
#define PROGRAM_NAME
Definition: l1cgen.cpp:28
virtual int32_t closel2_ocis_l1c(l2_str *l2str, l1c_filehandle *l1cfile)
Definition: l2_str.cpp:371
int **** allocate4d_int(size_t nr, size_t nz, size_t ny, size_t nx)
Allocate a four-dimensional array of type int of a given size.
Definition: allocate4d.c:12
@ string
virtual int32_t l1c_inputmain(int argc, char **argv, L1C_input *l1cinput, l1c_filehandle *l1cfile, const char *prog, const char *version)
Definition: l1c_input.cpp:701
virtual int32_t openl1b_ocis_l1c(l1c_str *l1cstr, l1c_filehandle *l1cfile, int16_t *file_id)
Definition: l1c_str.cpp:156
virtual int32_t openl2_ocis_l1c(L1C_input *l1cinput, l2_str *l2str, l1c_filehandle *l1cfile, int16_t *file_id)
Definition: l2_str.cpp:115
Definition: l1c.h:43
virtual int32_t ect_sf(const char *filename, l1c_filehandle *l1cfilei, L1C_input *l1cinput)
Definition: l1c.cpp:24279
Utility functions for allocating and freeing four-dimensional arrays of various types.
file_format getFormat(char *filename)
Definition: filetype.c:192
virtual int32_t readl2_ocis_l1c(l2_str *l2str, l1c_filehandle *l1cfile, int16_t *file_id, int32_t recnum)
Definition: l2_str.cpp:333
virtual int32_t across_gridlines_l1c2(int swtd, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *file_id, int16_t *nfiles_swt, float *lati3, float *loni3, float **lat_cgd, float **lon_cgd, float *az_east)
Definition: l1c.cpp:20351
virtual int32_t binL1C_wgranule6(int swtd, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *odir, int16_t *file_id, int16_t *nfiles_swt)
Definition: l1c.cpp:11035
virtual int32_t binL1C_wgranule7(int swtd, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *odir, int16_t *file_id, int16_t *nfiles_swt)
Definition: l1c.cpp:9682
virtual int32_t interp_swt1(int swtd, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *file_id, int16_t *nfiles_swt, double *time_mgv, orb_array *velig, float *lati, float *loni)
Definition: l1c.cpp:22262
Definition: l1c.cpp:76
virtual int32_t closel1b_ocis_l1c(l1c_str *l1cstr, l1c_filehandle *l1cfile)
Definition: l1c_str.cpp:481
#define VERSION
Definition: l1cgen.cpp:29
Utility functions for allocating and freeing two-dimensional arrays of various types.
@ FT_SPEXONE
Definition: filetype.h:60
std::vector< std::string > files
Definition: l1c_input.h:70
virtual int32_t interp_swt_dist3(int swtd, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *file_id, int16_t *nfiles_swt, double *time_mgv, float *lati, float *loni, float *lati2, float *loni2)
Definition: l1c.cpp:21975
data_t b[NROOTS+1]
Definition: decode_rs.h:77
#define NY
Definition: main_biosmap.c:52
const char * str
Definition: l1c_msi.cpp:35
@ FT_HARP2
Definition: filetype.h:61
virtual int32_t load_l1c_filehandle4(l1c_filehandle *l1cfile, L1C_input *l1cinput)
Definition: l1c.cpp:24585
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
@ FT_INVALID
Definition: filetype.h:12
double ** allocate2d_double(size_t h, size_t w)
Allocate a two-dimensional array of type double of a given size.
Definition: allocate2d.c:153
virtual int32_t mov_sd4(l1c_filehandle *l1cfile, L1C_input *l1cinput, double *tcross, int16_t *file_id, int16_t *swtd_id, int16_t *nfiles_swt, double *ect_swtd, int16_t *tod, int16_t *orbdir, float *mgv_swath)
Definition: l1c.cpp:23256
virtual int32_t openL1Cgrid(int swtd, l1c_str *l1cstr, l1c_filehandle *l1cfile, L1C_input *l1cinput, int16_t *swtd_id, int16_t *file_id, int16_t *nfiles_swt, float **lat_gd, float **lon_gd, float *az_east, float **lat_asort, short **index_xy)
Definition: l1c.cpp:5655
virtual int32_t time_swt3(int swtd, l1c_filehandle *l1cfile, L1C_input *l1cinput, double *ect_d, int16_t *swtdid, int16_t *fileid, int16_t *nfiles_swt, float *mgv_swt, double *time_mgv)
Definition: l1c.cpp:22497
short ** allocate2d_short(size_t h, size_t w)
Allocate a two-dimensional array of type short of a given size.
Definition: allocate2d.c:97
float ** allocate2d_float(size_t h, size_t w)
Allocate a two-dimensional array of type float of a given size.
Definition: allocate2d.c:125
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed as required for compatibility with version of the SDP toolkit Corrected test output file names to end in per delivery and then split off a new MYD_PR03 pcf file for Aqua Added AssociatedPlatformInstrumentSensor to the inventory metadata in MOD01 mcf and MOD03 mcf Created new versions named MYD01 mcf and MYD03 where AssociatedPlatformShortName is rather than Terra The program itself has been changed to read the Satellite Instrument validate it against the input L1A and LUT and to use it determine the correct files to retrieve the ephemeris and attitude data from Changed to produce a LocalGranuleID starting with MYD03 if run on Aqua data Added the Scan Type file attribute to the Geolocation copied from the L1A and attitude_angels to radians rather than degrees The accumulation of Cumulated gflags was moved from GEO_validate_earth_location c to GEO_locate_one_scan c
Definition: HISTORY.txt:464
virtual int32_t l1c_usage(const char *prog, const char *version)
Definition: l1c_input.cpp:98
int i
Definition: decode_rs.h:71
std::vector< std::string > ifiles
float p[MODELMAX]
Definition: atrem_corl1.h:131
virtual int32_t mov_sd2(l1c_filehandle *l1cfile, L1C_input *l1cinput, double *tcross, int16_t *file_id, int16_t *swtd_id, int16_t *nfiles_swt, double *ect_swtd, int16_t *tod, int16_t *orbdir, float *mgv_swath)
Definition: l1c.cpp:23582
virtual int32_t mov_sd3(l1c_filehandle *l1cfile, L1C_input *l1cinput, double *tcross, int16_t *file_id, int16_t *swtd_id, int16_t *nfiles_swt, double *ect_swtd, int16_t *tod, int16_t *orbdir, float *mgv_swath)
Definition: l1c.cpp:22745