OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
l3rebin_meris.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <netcdf.h>
4 #include <timeutils.h>
5 
6 #include "hdf_bin.h"
7 #include <L3ShapeIsine.h>
8 
9 #define VERSION "2.0"
10 
11 using namespace std;
12 
13 /*
14  Revision 2.0 08/31/16
15  Converted to 64 bit binner
16  R. Healy
17 
18  Revision 1.03 04/05/12
19  Use (int32_t *) cast for meta_l3b.smsec and meta_l3b.emsec
20  J. Gales
21 
22  Revision 1.02 03/22/12
23  Populate Start & End Time metadata
24  J. Gales
25 
26  Revision 1.01 03/20/12
27  Add support for products other that CHL (!)
28  Clean up metadata
29  J. Gales
30 
31  Revision 1.00 03/19/12
32  Initial Version
33  J. Gales
34  */
35 
36 void usage(const char *progname) {
37  printf("%s %s (%s %s)\n", progname, VERSION, __DATE__, __TIME__);
38 
39  printf("\nUsage: %s input_meris_binfile output_hdf4_binfile meris_prodname hdf4_prodname\n",
40  progname);
41 
42  exit(0);
43 }
44 
45 int main(int argc, char **argv) {
46  int ncid;
47  int row_varid, col_varid, binid;
48  int rowid, lon_varid, lat_varid, lon_step_varid;
49  size_t nbins, nrows;
50 
51  short int *row, *col;
52  float *lon, *lat, *lon_step;
53 
54  char ncbin_filename[256];
55 
56  /* Loop index. */
57  int i;
58 
59  if (argc == 1) usage("l3rebin_meris");
60 
61  strcpy(ncbin_filename, argv[1]);
62  printf("Reading product: %s in %s\n", argv[3], ncbin_filename);
63 
64  /* Open the file. */
65  nc_open(ncbin_filename, NC_NOWRITE, &ncid);
66 
67  nc_inq_dimid(ncid, "bin", &binid);
68  nc_inq_dimlen(ncid, binid, &nbins);
69  nc_inq_dimid(ncid, "row", &rowid);
70  nc_inq_dimlen(ncid, rowid, &nrows);
71 
72  row = (short int *) calloc(nbins, sizeof (short int));
73  col = (short int *) calloc(nbins, sizeof (short int));
74 
75  lon = (float *) calloc(nrows, sizeof (float));
76  lat = (float *) calloc(nrows, sizeof (float));
77  lon_step = (float *) calloc(nrows, sizeof (float));
78 
79  l3::L3ShapeIsine binShape(nrows);
80 
81  /* Read the row/col data. */
82  nc_inq_varid(ncid, "row", &row_varid);
83  nc_inq_varid(ncid, "col", &col_varid);
84  nc_get_var_short(ncid, row_varid, row);
85  nc_get_var_short(ncid, col_varid, col);
86 
87  nc_inq_varid(ncid, "center_lon", &lon_varid);
88  nc_inq_varid(ncid, "center_lat", &lat_varid);
89  nc_inq_varid(ncid, "lon_step", &lon_step_varid);
90  nc_get_var_float(ncid, lon_varid, lon);
91  nc_get_var_float(ncid, lat_varid, lat);
92  nc_get_var_float(ncid, lon_step_varid, lon_step);
93 
94  int prod_varid, nobs_varid;
95  float *prod = (float *) calloc(nbins, sizeof (float));
96  short int *nobs = (short int *) calloc(nbins, sizeof (short int));
97 
98  int nvars;
99  nc_inq_nvars(ncid, &nvars);
100 
101  string varname;
102  string cntname;
103  varname = argv[3];
104  varname.append("_mean");
105  cntname = argv[3];
106  cntname.append("_count");
107 
108  nc_inq_varid(ncid, varname.c_str(), &prod_varid);
109  nc_inq_varid(ncid, cntname.c_str(), &nobs_varid);
110 
111  nc_get_var_float(ncid, prod_varid, prod);
112  nc_get_var_short(ncid, nobs_varid, nobs);
113 
114  Hdf::hdf_bin *output_binfile;
115  output_binfile = new Hdf::hdf4_bin;
116  output_binfile->hasNoext = true;
117  printf("Creating product: %s in %s\n\n", argv[4], argv[2]);
118  output_binfile->create(argv[2], 4320);
119  output_binfile->clear_binlist();
120 
121  int64_t bin_num;
122  int32_t irow, icol;
123  float out_sum_buf[2 * 2 * 4320];
124  int32_t last_row;
125  int32_t tot_write = 0;
126  int32_t row_off = row[0];
127  for (i = 0; i < (int) nbins; i++) {
128  if ((i % 1000000) == 0) cout << i << " out of " << nbins << endl;
129 
130  //p lat[row[0]]
131  //p lon[row[0]] + lon_step[row[0]]*col[0]
132  int32_t r = row[i] - row_off;
133  binShape.latlon2rowcol(lat[r], lon[r] + lon_step[r] * col[i], irow, icol);
134  bin_num = binShape.rowcol2bin(irow, icol);
135  irow--;
136  icol--;
137  if (i == 0) last_row = irow;
138 
139  if (irow != last_row) {
140  int32_t n_write = 0;
141  int32_t kbin_max = output_binfile->get_numbin(last_row);
142  for (int32_t kbin = 0; kbin <= kbin_max; kbin++) {
143 
144  if (output_binfile->get_bin_num(kbin) != 0) {
145 
146  /* Remove "blank" bin records */
147  /* -------------------------- */
148  if (n_write != kbin)
149  memcpy(&out_sum_buf[2 * n_write], &out_sum_buf[2 * kbin], 8);
150 
151  /* Remove "blank" bin records */
152  /* -------------------------- */
153  if (n_write != kbin)
154  output_binfile->copy_binlist(kbin, n_write);
155 
156  n_write++;
157  tot_write++;
158  } // bin_num != 0
159  } /* kbin loop */
160 
161  /* Write BinList & Data Products */
162  /* ----------------------------- */
163  output_binfile->writeBinList(n_write);
164  output_binfile->writeSums(&out_sum_buf[0], n_write, argv[4]);
165  last_row = irow;
166 
167  output_binfile->clear_binlist();
168  }
169 
170  int64_t basebin = output_binfile->get_basebin(irow);
171  int32_t offset = bin_num - basebin;
172  if (offset < 0) {
173  cout << "Negative offset: " << offset <<
174  " for bin_num: " << bin_num << endl;
175  exit(1);
176  }
177  // cout << i << " " << bin_num << " " << offset << " " << irow <<
178  //" " << lat[row[i]] << " " << lon[row[i]] + lon_step[row[i]]*col[i] <<
179  //" " << row[i] << " " << col[i] << endl;
180 
181  output_binfile->set_bin_num(offset, bin_num);
182  output_binfile->inc_nobs(offset, nobs[i]);
183  output_binfile->inc_weights(offset, (float) nobs[i]);
184  out_sum_buf[2 * icol] = prod[i] * nobs[i];
185  out_sum_buf[2 * icol + 1] = 0.0;
186  }
187 
188 
189  // Fill metadata
190  strcpy(output_binfile->meta_l3b.product_name, argv[2]);
191  strcpy(output_binfile->meta_l3b.sensor_name, "MERIS");
192  strcpy(output_binfile->meta_l3b.mission, "");
193  strcpy(output_binfile->meta_l3b.mission_char, "");
194  strcpy(output_binfile->meta_l3b.sensor, "");
195  strcpy(output_binfile->meta_l3b.sensor_char, "");
196  strcpy(output_binfile->meta_l3b.prod_type, "");
197  strcpy(output_binfile->meta_l3b.pversion, "");
198  strcpy(output_binfile->meta_l3b.soft_name, "l3rebin_meris");
199  strcpy(output_binfile->meta_l3b.soft_ver, VERSION);
200  output_binfile->meta_l3b.start_orb = 0;
201  output_binfile->meta_l3b.end_orb = 0;
202  strcpy(output_binfile->meta_l3b.ptime, "");
203  strcpy(output_binfile->meta_l3b.proc_con, "");
204  strcpy(output_binfile->meta_l3b.input_parms, "");
205  strcpy(output_binfile->meta_l3b.infiles, "");
206  strcpy(output_binfile->meta_l3b.flag_names, "");
207  // strcpy( output_binfile->meta_l3b.stime, "");
208  // strcpy( output_binfile->meta_l3b.etime, "");
209  // output_binfile->meta_l3b.bin_syear = 0;
210  // output_binfile->meta_l3b.bin_sday = 0;
211  // output_binfile->meta_l3b.bin_eyear = 0;
212  // output_binfile->meta_l3b.bin_eday = 0;
213 
214  char buf[256];
215  string str;
216  istringstream istr;
217  int32_t yy, mm, dd, hh, mn, sc;
218  int32_t year, day;
219  int32_t msec;
220  nc_get_att_text(ncid, NC_GLOBAL, "start_time", buf);
221  str = buf;
222  istr.clear();
223  istr.str(str.substr(0, 4));
224  istr >> yy;
225  istr.clear();
226  istr.str(str.substr(4, 2));
227  istr >> mm;
228  istr.clear();
229  istr.str(str.substr(6, 2));
230  istr >> dd;
231  istr.clear();
232  istr.str(str.substr(9, 2));
233  istr >> hh;
234  istr.clear();
235  istr.str(str.substr(11, 2));
236  istr >> mn;
237  istr.clear();
238  istr.str(str.substr(13, 2));
239  istr >> sc;
240 
241  ymdhms2ydmsec(yy, mm, dd, hh, mn, sc, &year, &day, &msec);
242 
243  output_binfile->meta_l3b.startTime = yds2unix((int16_t) year, (int16_t) day, (double) msec);
244 
245  nc_get_att_text(ncid, NC_GLOBAL, "end_time", buf);
246  str = buf;
247  istr.clear();
248  istr.str(str.substr(0, 4));
249  istr >> yy;
250  istr.clear();
251  istr.str(str.substr(4, 2));
252  istr >> mm;
253  istr.clear();
254  istr.str(str.substr(6, 2));
255  istr >> dd;
256  istr.clear();
257  istr.str(str.substr(9, 2));
258  istr >> hh;
259  istr.clear();
260  istr.str(str.substr(11, 2));
261  istr >> mn;
262  istr.clear();
263  istr.str(str.substr(13, 2));
264  istr >> sc;
265 
266  ymdhms2ydmsec(yy, mm, dd, hh, mn, sc, &year, &day, &msec);
267  output_binfile->meta_l3b.startTime = yds2unix((int16_t) year, (int16_t) day, (double) msec);
268 
269 
270  strcpy(output_binfile->meta_l3b.units, "");
271 
272  /* Close the file. */
273  nc_close(ncid);
274  output_binfile->close();
275 
276  return 0;
277 }
278 
279 
280 
281 
virtual int64_t get_bin_num(int kbin)=0
char product_name[SM_ATTRSZ]
Definition: meta_l3b.h:16
int r
Definition: decode_rs.h:73
char units[MD_ATTRSZ]
Definition: meta_l3b.h:28
int32_t day
double yds2unix(int16_t year, int16_t day, double secs)
Definition: yds2unix.c:7
char infiles[LG_ATTRSZ]
Definition: meta_l3b.h:38
virtual int inc_weights(int offset, float weights)=0
char mission[SM_ATTRSZ]
Definition: meta_l3b.h:21
char sensor_char[SM_ATTRSZ]
Definition: meta_l3b.h:24
virtual int create(const char *l3b_filename, int32_t nrows)=0
#define VERSION
virtual int64_t rowcol2bin(int32_t row, int32_t col) const
int32_t start_orb
Definition: meta_l3b.h:42
char input_parms[LG_ATTRSZ]
Definition: meta_l3b.h:36
float * lat
int32 * msec
Definition: l1_czcs_hdf.c:31
virtual int close()=0
virtual int64_t get_numbin(int irow)
Definition: hdf_bin.h:72
char flag_names[SM_ATTRSZ]
Definition: meta_l3b.h:37
virtual int copy_binlist(int src, int dest)=0
int32_t nobs
Definition: atrem_cor.h:93
bool hasNoext
Definition: hdf_bin.h:127
char sensor[SM_ATTRSZ]
Definition: meta_l3b.h:23
virtual int64_t get_basebin(int irow)
Definition: hdf_bin.h:76
char prod_type[SM_ATTRSZ]
Definition: meta_l3b.h:29
meta_l3bType meta_l3b
Definition: hdf_bin.h:131
int32 nrows
virtual void latlon2rowcol(double lat, double lon, int32_t &row, int32_t &col) const
virtual int writeSums(float *sums, int32_t nbins_to_write, const char *prodname)=0
int main(int argc, char **argv)
const char * str
Definition: l1c_msi.cpp:35
void ymdhms2ydmsec(int yy, int mm, int dd, int hh, int mn, int sc, int32_t *year, int32_t *day, int32_t *msec)
Definition: ydhms2ydmsec.c:3
char proc_con[MD_ATTRSZ]
Definition: meta_l3b.h:35
virtual int inc_nobs(int offset, int nobs)=0
double startTime
Definition: meta_l3b.h:39
char soft_ver[SM_ATTRSZ]
Definition: meta_l3b.h:33
char mission_char[SM_ATTRSZ]
Definition: meta_l3b.h:22
float * lon
virtual int set_bin_num(int offset, int64_t bin_num)=0
char ptime[SM_ATTRSZ]
Definition: meta_l3b.h:34
virtual int clear_binlist()=0
char pversion[SM_ATTRSZ]
Definition: meta_l3b.h:30
l2prod offset
char sensor_name[SM_ATTRSZ]
Definition: meta_l3b.h:19
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
void usage(const char *progname)
int32_t end_orb
Definition: meta_l3b.h:43
char soft_name[SM_ATTRSZ]
Definition: meta_l3b.h:32
virtual int writeBinList(int32_t nbins_to_write)=0