17 #define MAX_ROWS_32BIT 41068
37 if (Hishdf(binFileName) ==
TRUE) {
39 }
else if (H5Fis_hdf5(binFileName) ==
TRUE) {
43 H5Eset_auto(H5E_DEFAULT,
NULL,
NULL);
45 if (nc_open(binFileName, NC_NOWRITE, &ncid) == NC_NOERR) {
46 status = nc_get_att(ncid, NC_GLOBAL,
"Mission", nam_buf);
49 if (strcmp(nam_buf,
"SAC-D Aquarius") == 0) {
66 int numProducts = binFile->
nprod();
68 printf(
"numProducts = %d\n", numProducts);
70 for (
int i = 0;
i < numProducts;
i++) {
89 product_array[0] =
NULL;
100 hdf_bin::~hdf_bin() {
105 hdf4_bin::hdf4_bin() {
118 hdf4_bin::~hdf4_bin() {
121 hdf5_bin::hdf5_bin() {
127 for (
size_t i = 0;
i <
MAXNPROD;
i++) h5table_id[0][
i] = -1;
128 for (
size_t i = 0;
i <
MAXNPROD;
i++) h5table_id[1][
i] = -1;
129 for (
size_t i = 0;
i <
MAXNPROD;
i++) h5table_id[2][
i] = -1;
132 hdf5_bin::~hdf5_bin() {
135 cdf4_bin::cdf4_bin() {
141 access_mode = NC_NOWRITE;
155 cdf4_bin::~cdf4_bin() {
160 int hdf4_bin::create(
const char* l3b_filename, int32_t nrows) {
165 const char* fldname1[] = {
"registration",
"straddle",
"bins",
"radius",
166 "max_north",
"max_south",
"seam_lon"};
168 file_id = Hopen(l3b_filename, DFACC_CREATE, 0);
169 sd_id = SDstart(l3b_filename, DFACC_RDWR);
170 access_mode = DFACC_CREATE;
172 if (this->hasNoext ==
true)
177 vg_id = Vattach(file_id, -1,
"w");
179 Vsetname(vg_id,
"Level-3 Binned Data");
180 Vsetclass(vg_id,
"PlanetaryGrid");
184 a = (uint8_t *) malloc(44);
189 double radius = 6378.137000;
191 double south = -90.0;
192 double seam_lon = -180.0;
194 memcpy(&
a[0], &five, 4);
195 memcpy(&
a[4], &zero, 4);
197 memcpy(&
a[8], &i32, 4);
199 memcpy(&
a[20], &north, 8);
200 memcpy(&
a[28], &south, 8);
201 memcpy(&
a[36], &seam_lon, 8);
206 type[3] = DFNT_FLOAT64;
207 type[4] = DFNT_FLOAT64;
208 type[5] = DFNT_FLOAT64;
209 type[6] = DFNT_FLOAT64;
212 create_vdata(file_id, vg_id, &vdata_id[seagrid_idx],
"SEAGrid",
213 "Geometry", 7, fldname1,
type, noext,
NULL);
222 totbins = binShape->getNumBins();
233 int hdf4_bin::open(
const char* l3b_filename) {
240 hasQual = this->has_qual();
241 n_data_prod = this->nprod();
242 for (int32_t
i = 0;
i < n_data_prod;
i++)
243 this->get_prodname(
i, proddata_name[
i]);
247 if ((file_id = Hopen(l3b_filename, DFACC_RDONLY, 0)) < 0) {
248 fprintf(
stderr,
"Error: Cannot open input HDF file on Hopen - %s\n",
255 if ((sd_id = SDstart(l3b_filename, DFACC_RDONLY)) < 0) {
256 fprintf(
stderr,
"Error: Cannot open input HDF file on SDstart- %s\n",
261 access_mode = DFACC_RDONLY;
265 fprintf(
stderr,
"Error: Cannot read metadata from file - %s\n",
273 vg_ref = Vfind(file_id,
"Level-3 Binned Data");
275 vg_id = Vattach(file_id, vg_ref,
"r");
281 for (int32_t
i = 0;
i < Vntagrefs(vg_id);
i++) {
282 Vgettagref(vg_id,
i, &tag, &
ref);
283 vdata_id[
i] = VSattach(file_id,
ref,
"r");
285 if (vdata_id[
i] == -1) {
286 printf(
"Error opening Vdata (reference # %d)\n",
ref);
290 VSgetname(vdata_id[
i], nam_buf);
291 VSgetclass(vdata_id[
i], cls_buf);
293 if (strcmp(cls_buf,
"Geometry") == 0) {
297 if (strcmp(cls_buf,
"Index") == 0) {
298 nrows = VSelts(vdata_id[
i]);
302 if (strcmp(cls_buf,
"DataMain") == 0) {
303 n_data_records = VSelts(vdata_id[
i]);
307 if (strcmp(cls_buf,
"DataSubordinate") == 0) {
308 strcpy(proddata_name[n_data_prod++], nam_buf);
317 if (strcmp(cls_buf,
"DataQuality") == 0) {
324 totbins = binShape->getNumBins();
333 int hdf4_bin::readBinIndex(
int row_num_to_read) {
334 const char *binIndexFields =
335 "row_num,vsize,hsize,start_num,begin,extent,max";
338 VSseek(vdata_id[binindex_idx], row_num_to_read);
340 VSsetfields(vdata_id[binindex_idx], binIndexFields);
342 int records_read = VSread(vdata_id[binindex_idx], (uint8_t *) & binIndex,
346 if (
sizeof (binIndex) == 40) {
347 memmove(((
char *) &binIndex) + 8,
348 ((
char *) &binIndex) + 4,
350 memset(((
char *) &binIndex) + 4, 0, 4);
358 int hdf_bin::read(
char* product_list) {
359 int n = strlen(product_list);
360 char *buf = (
char *) malloc(n + 1);
361 strcpy(buf, product_list);
363 for (int32_t
i = 0;
i < n_data_prod;
i++)
364 active_data_prod[
i] =
false;
366 char *pch = strtok(buf,
",");
367 while (pch !=
NULL) {
368 if (strcmp(pch,
"qual_l3") == 0) {
369 pch = strtok(
NULL,
",");
374 for (int32_t
i = 0;
i < n_data_prod;
i++) {
375 if (strcmp(pch, proddata_name[
i]) == 0) {
377 active_data_prod[
i] =
true;
382 cout <<
"Product: " << pch <<
" not found.";
386 pch = strtok(
NULL,
",");
390 for (int32_t
i = 0;
i < n_data_prod;
i++)
391 if (active_data_prod[
i])
395 return (n_data_records * n_active_prod);
402 int32_t records_read;
404 int32_t records_remaining = VSelts(vdata_id[binlist_idx]) - bin_ptr;
405 if (records_remaining < nbins_to_read) {
406 cout << nbins_to_read <<
" requested, ";
407 cout << records_remaining <<
" to be read at bin_ptr: ";
408 cout << bin_ptr << endl;
409 nbins_to_read = records_remaining;
412 VSsetfields(vdata_id[binlist_idx],
413 "bin_num,nobs,nscenes,time_rec,weights,sel_cat,flags_set");
415 read_binList(nbins_to_read, vdata_id[binlist_idx], binList, binShape);
420 int bin_ptr0 = bin_ptr;
421 for (int32_t
i = 0;
i < n_data_prod;
i++) {
422 if (active_data_prod[
i] ==
true) {
423 strcpy(buf2, proddata_name[
i]);
424 strcat(buf2,
"_sum");
425 VSsetfields(vdata_id[bindata_idx +
i], buf2);
427 VSseek(vdata_id[bindata_idx +
i], bin_ptr0);
429 records_read = VSread(vdata_id[bindata_idx +
i],
430 (uint8_t *) &
data[
k * nbins_to_read],
431 nbins_to_read, FULL_INTERLACE);
433 if (records_read == -1) {
434 cout <<
"Read failure at bin_ptr: " << bin_ptr << endl;
439 bin_ptr += records_read;
443 for (int32_t
j = 0;
j < nbins_to_read;
j++) {
444 memcpy((
void *) &binListRec, &binList[
j],
447 data[
k * nbins_to_read +
j] /= binListRec.weights;
458 int hdf4_bin::read(
float*
data,
float* var,
461 int32_t records_read;
463 int32_t records_remaining = VSelts(vdata_id[binlist_idx]) - bin_ptr;
464 if (records_remaining < nbins_to_read) {
465 cout << nbins_to_read <<
" requested, ";
466 cout << records_remaining <<
" to be read at bin_ptr: ";
467 cout << bin_ptr << endl;
468 nbins_to_read = records_remaining;
471 VSsetfields(vdata_id[binlist_idx],
472 "bin_num,nobs,nscenes,time_rec,weights,sel_cat,flags_set");
474 read_binList(nbins_to_read, vdata_id[binlist_idx], binList, binShape);
479 int bin_ptr0 = bin_ptr;
480 for (int32_t
i = 0;
i < n_data_prod;
i++) {
481 if (active_data_prod[
i] ==
true) {
484 strcpy(buf2, proddata_name[
i]);
485 strcat(buf2,
"_sum");
486 VSsetfields(vdata_id[bindata_idx +
i], buf2);
488 VSseek(vdata_id[bindata_idx +
i], bin_ptr0);
489 records_read = VSread(vdata_id[bindata_idx +
i],
490 (uint8_t *) &
data[
k * nbins_to_read],
491 nbins_to_read, FULL_INTERLACE);
493 if (records_read == -1) {
494 cout <<
"Read failure at bin_ptr: " << bin_ptr << endl;
499 strcpy(buf2, proddata_name[
i]);
500 strcat(buf2,
"_sum_sq");
501 VSsetfields(vdata_id[bindata_idx +
i], buf2);
503 VSseek(vdata_id[bindata_idx +
i], bin_ptr0);
504 records_read = VSread(vdata_id[bindata_idx +
i],
505 (uint8_t *) & var[
k * nbins_to_read],
506 nbins_to_read, FULL_INTERLACE);
508 if (records_read == -1) {
509 cout <<
"Read failure at bin_ptr: " << bin_ptr << endl;
514 bin_ptr += records_read;
518 for (int32_t
j = 0;
j < nbins_to_read;
j++) {
519 memcpy((
void *) &binListRec, &binList[
j],
521 float sum =
data[
k * nbins_to_read +
j];
522 float wgt = binListRec.weights;
523 data[
k * nbins_to_read +
j] /= wgt;
525 if (binListRec.nscenes > 1) {
526 float tmp = var[
k * nbins_to_read +
j] * wgt;
528 tmp /= (wgt * wgt - binListRec.nobs);
529 var[
k * nbins_to_read +
j] =
tmp;
531 var[
k * nbins_to_read +
j] = 1.0;
542 int hdf4_bin::readBinList(
int nbins_to_read) {
543 int32_t records_read;
546 if (lastBinListPtr == binListPtr && lastNumBins == nbins_to_read)
547 return nbins_to_read;
548 lastBinListPtr = binListPtr;
549 lastNumBins = nbins_to_read;
551 if (nbins_to_read == 0)
554 const char *binListFields =
555 "bin_num,nobs,nscenes,time_rec,weights,sel_cat,flags_set";
557 VSsetfields(vdata_id[binlist_idx], binListFields);
559 records_read =
read_binList(nbins_to_read, vdata_id[binlist_idx], binList, binShape);
561 binListPtr += records_read;
568 int hdf4_bin::readBinList() {
569 int32_t records_read;
571 records_read = this->readBinList(n_data_records);
576 int hdf4_bin::readBinList(
int nbins_to_read, int32_t list_reset_ptr) {
577 int32_t records_read;
579 VSseek(vdata_id[binlist_idx], list_reset_ptr);
581 records_read = this->readBinList(nbins_to_read);
583 binListPtr = list_reset_ptr + records_read;
591 int32_t records_read;
593 records_read = this->read(
data, binList, n_data_records);
601 int32_t records_read;
603 records_read = this->read(
data, var, binList, n_data_records);
608 int hdf4_bin::write(
char *product_list, int32_t nwrite,
float *outData,
611 const char* fldname2[] = {
"bin_num",
"nobs",
"nscenes",
"time_rec",
612 "weights",
"sel_cat",
"flags_set"};
617 if (vdata_id[binlist_idx] == -1) {
623 type[5] = DFNT_UCHAR8;
625 create_vdata(file_id, vg_id, &vdata_id[binlist_idx],
"BinList",
626 "DataMain", 7, fldname2,
type, noext,
NULL);
633 int n = strlen(product_list);
634 char *buf = (
char *) malloc(n + 1);
635 strcpy(buf, product_list);
636 char *pch = strtok(buf,
",");
642 while (pch !=
NULL) {
645 if (vdata_id[bindata_idx +
i] == -1) {
646 strcpy(proddata_name[n_data_prod++], pch);
648 fldname3[0] = (
char *) calloc(strlen(pch) + 5,
sizeof (char));
649 fldname3[1] = (
char *) calloc(strlen(pch) + 8,
sizeof (char));
651 strcat(fldname3[0],
"_sum");
653 strcat(fldname3[1],
"_sum_sq");
655 create_vdata(file_id, vg_id, &vdata_id[bindata_idx +
i], pch,
656 "DataSubordinate", 2, fldname3,
type, noext, &aid[
i]);
662 &outData[(bindata_idx +
i - 2) * nwrite], binList);
664 pch = strtok(
NULL,
",");
668 n_data_records += nwrite;
676 const char* fldname2[] = {
"bin_num",
"nobs",
"nscenes",
"time_rec",
677 "weights",
"sel_cat",
"flags_set"};
682 if (vdata_id[binlist_idx] == -1) {
688 type[5] = DFNT_UCHAR8;
690 create_vdata(file_id, vg_id, &vdata_id[binlist_idx],
"BinList",
691 "DataMain", 7, fldname2,
type, noext,
NULL);
693 for (int32_t
i = 0;
i < nwrite;
i++) {
694 write_binList(1, vdata_id[binlist_idx], &inBinList[binsToCopy[
i]]);
699 int n = strlen(product_list);
700 char *buf = (
char *) malloc(n + 1);
701 strcpy(buf, product_list);
702 char *pch = strtok(buf,
",");
708 while (pch !=
NULL) {
709 strcpy(proddata_name[n_data_prod++], pch);
711 fldname3[0] = (
char *) calloc(strlen(pch) + 5,
sizeof (char));
712 fldname3[1] = (
char *) calloc(strlen(pch) + 8,
sizeof (char));
714 strcat(fldname3[0],
"_sum");
716 strcat(fldname3[1],
"_sum_sq");
718 create_vdata(file_id, vg_id, &vdata_id[bindata_idx +
i], pch,
719 "DataSubordinate", 2, fldname3,
type, noext, &aid[
i]);
721 int32_t
ref = VSfind(input_binfile->file_id, pch);
722 int32_t in_vdata_id = VSattach(input_binfile->file_id,
ref,
"r");
723 if (in_vdata_id == -1) {
724 printf(
"Error opening Vdata (reference # %d)\n",
ref);
728 in_vdata_id, vdata_id[bindata_idx +
i]);
729 VSdetach(in_vdata_id);
735 pch = strtok(
NULL,
",");
739 n_data_records += nwrite;
746 int hdf4_bin::readQual(uint8_t*
qual, int32_t nbins_to_read) {
747 int records_read = -1;
748 if (binqual_idx != -1) {
749 if (nbins_to_read > 0) {
750 records_read = VSread(vdata_id[binqual_idx], (uint8_t *)
qual,
751 nbins_to_read, FULL_INTERLACE);
759 int hdf4_bin::readQual(uint8_t*
qual, int32_t nbins_to_read,
760 int32_t row_num_to_read) {
761 int records_read = -1;
762 if (binqual_idx != -1) {
763 if (nbins_to_read > 0) {
765 VSseek(vdata_id[binqual_idx], row_num_to_read);
767 records_read = VSread(vdata_id[binqual_idx], (uint8_t *)
qual,
768 nbins_to_read, FULL_INTERLACE);
778 int hdf4_bin::readSums(
float* sums, int32_t nbins_to_read,
int iprod) {
780 int32_t records_read;
783 for (int32_t
i = 0;
i < n_data_prod;
i++) {
784 if (active_data_prod[
i] ==
true && (
i == iprod || iprod == -1)) {
787 strcpy(buf2, proddata_name[
i]);
788 strcat(buf2,
"_sum,");
789 strcat(buf2, proddata_name[
i]);
790 strcat(buf2,
"_sum_sq");
791 VSsetfields(vdata_id[bindata_idx +
i], buf2);
793 records_read = VSread(vdata_id[bindata_idx +
i],
794 (uint8_t *) & sums[2 *
k * nbins_to_read],
795 nbins_to_read, FULL_INTERLACE);
797 if (records_read == -1) {
811 int hdf4_bin::readSums(
float* sums, int32_t* listOfBins,
812 int32_t nbins_to_read,
int iprod) {
814 int32_t records_read;
817 for (int32_t
i = 0;
i < n_data_prod;
i++) {
818 if (active_data_prod[
i] ==
true && (
i == iprod || iprod == -1)) {
821 strcpy(buf2, proddata_name[
i]);
822 strcat(buf2,
"_sum,");
823 strcat(buf2, proddata_name[
i]);
824 strcat(buf2,
"_sum_sq");
825 VSsetfields(vdata_id[bindata_idx +
i], buf2);
827 for (int32_t
j = 0;
j < nbins_to_read;
j++) {
829 VSseek(vdata_id[bindata_idx +
i], listOfBins[
j]);
831 records_read = VSread(vdata_id[bindata_idx +
i],
832 (uint8_t *) & sums[2 *
k], 1, FULL_INTERLACE);
834 if (records_read == -1) {
847 int hdf4_bin::setDataPtrAbsolute(int32_t recordNum) {
848 for (int32_t
i = 0;
i < n_data_prod;
i++) {
849 if (VSseek(vdata_id[bindata_idx +
i], recordNum) < 0)
855 int hdf4_bin::writeBinList(int32_t nbins_to_write) {
857 const char* fldname2[] = {
"bin_num",
"nobs",
"nscenes",
"time_rec",
858 "weights",
"sel_cat",
"flags_set"};
863 if (vdata_id[binlist_idx] == -1) {
869 type[5] = DFNT_UCHAR8;
871 create_vdata(file_id, vg_id, &vdata_id[binlist_idx],
"BinList",
872 "DataMain", 7, fldname2,
type, noext,
NULL);
874 write_binList(nbins_to_write, vdata_id[binlist_idx], &binList[0]);
876 n_data_records += nbins_to_write;
883 int hdf4_bin::writeQual(uint8_t*
qual, int32_t nbins_to_write) {
884 const char* fldname5[] = {
"qual_l3"};
886 type[0] = DFNT_UCHAR8;
888 if (binqual_idx == -1) {
889 binqual_idx = bindata_idx + n_data_prod + 1;
890 create_vdata(file_id, vg_id, &vdata_id[binqual_idx],
"qual_l3",
891 "DataQuality", 1, fldname5,
type, noext,
NULL);
894 int records_written = -1;
895 if (binqual_idx != -1) {
897 if (nbins_to_write > 0) {
898 records_written = VSwrite(vdata_id[binqual_idx], (uint8_t *)
qual,
899 nbins_to_write, FULL_INTERLACE);
902 return records_written;
905 int hdf4_bin::writeSums(
float* sums, int32_t nbins_to_write,
906 const char *prodname) {
911 for (iprod = 0; iprod < n_data_prod; iprod++) {
912 if (strcmp(proddata_name[iprod], prodname) == 0) {
922 if (found ==
false) {
923 strcpy(proddata_name[n_data_prod], prodname);
928 if (vdata_id[bindata_idx + iprod] == -1) {
929 fldname3[0] = (
char *) calloc(strlen(prodname) + 5,
sizeof (char));
930 fldname3[1] = (
char *) calloc(strlen(prodname) + 8,
sizeof (char));
932 strcpy(fldname3[0], prodname);
933 strcat(fldname3[0],
"_sum");
934 strcpy(fldname3[1], prodname);
935 strcat(fldname3[1],
"_sum_sq");
937 create_vdata(file_id, vg_id, &vdata_id[bindata_idx + iprod],
938 prodname,
"DataSubordinate", 2, fldname3,
type,
944 int records_written = VSwrite(vdata_id[bindata_idx + iprod],
945 (
const uint8_t*) sums, nbins_to_write,
948 return records_written;
951 int hdf4_bin::close() {
953 int32_t n_total_bins = 0;
954 float slat = +90, nlat = -90, elon = -180, wlon = +180;
956 if (access_mode == DFACC_CREATE) {
957 int32_t *binnum_data = (int32_t *) calloc(n_data_records,
sizeof (int32_t));
959 status = VSsetfields(vdata_id[binlist_idx],
"bin_num");
961 printf(
"Unable to set \"bin_num\" in VSsetfields for id: %d.\n",
962 vdata_id[binlist_idx]);
965 VSseek(vdata_id[binlist_idx], 0);
966 int32_t nread = VSread(vdata_id[binlist_idx], (uint8_t *) binnum_data,
967 n_data_records, FULL_INTERLACE);
969 printf(
"Unable to read bin number input in hdf4_bin::close()\n");
980 int32_t *beg = (int32_t *) calloc(
nrows,
sizeof (int32_t));
981 int32_t *
ext = (int32_t *) calloc(
nrows,
sizeof (int32_t));
983 int32_t *numbin_out = (int32_t *) calloc(
nrows,
sizeof (int32_t));
984 int32_t *basebin_out = (int32_t *) calloc(
nrows + 1,
sizeof (int32_t));
986 for (int32_t
i = 0;
i <
nrows;
i++) {
987 float latbin = (
i + 0.5) * (180.0 / (
nrows)) - 90.0;
988 numbin_out[
i] = (int32_t) (cos(latbin *
PI / 180.0) * (2.0 *
nrows) + 0.5);
989 n_total_bins += numbin_out[
i];
993 for (int32_t
i = 1;
i <=
nrows;
i++) {
994 basebin_out[
i] = basebin_out[
i - 1] + numbin_out[
i - 1];
1003 uint8_t *
a = (uint8_t *) calloc(36 *
nrows, 1);
1005 for (int32_t
i = 0;
i <
nrows;
i++) {
1006 double vsize = 180.0 /
nrows;
1007 double hsize = 360.0 / numbin_out[
i];
1008 memcpy(&
a[
i * 36], &
i, 4);
1009 memcpy(&
a[
i * 36 + 4], &vsize, 8);
1010 memcpy(&
a[
i * 36 + 12], &hsize, 8);
1011 memcpy(&
a[
i * 36 + 20], &basebin_out[
i], 4);
1012 memcpy(&
a[
i * 36 + 24], &beg[
i], 4);
1013 memcpy(&
a[
i * 36 + 28], &
ext[
i], 4);
1014 memcpy(&
a[
i * 36 + 32], &numbin_out[
i], 4);
1020 lat = ((
i + 0.5) /
nrows) * 180.0 - 90.0;
1026 lon = 360.0 * (beg[
i] - basebin_out[
i] + 0.5)
1027 / numbin_out[
i] - 180.0;
1031 lon = 360.0 * (binnum_data[i32 +
ext[
i] - 1] - basebin_out[
i] + 0.5)
1032 / numbin_out[
i] - 180.0;
1039 const char* fldname4[] = {
"row_num",
"vsize",
"hsize",
"start_num",
1040 "begin",
"extent",
"max"};
1044 type[1] = DFNT_FLOAT64;
1045 type[2] = DFNT_FLOAT64;
1051 binindex_idx = 2 + n_data_prod;
1052 create_vdata(file_id, vg_id, &vdata_id[binindex_idx],
"BinIndex",
1053 "Index", 7, fldname4,
type, noext,
NULL);
1067 meta_l3b.north = nlat;
1068 meta_l3b.south = slat;
1069 meta_l3b.east = elon;
1070 meta_l3b.west = wlon;
1071 meta_l3b.data_bins = n_data_records;
1072 meta_l3b.pct_databins = 100 * ((
float) n_data_records) / n_total_bins;
1076 }
else if (
nrows == 360) {
1078 }
else if (
nrows == 540) {
1080 }
else if (
nrows == 720) {
1082 }
else if (
nrows == 1080) {
1084 }
else if (
nrows == 2160) {
1086 }
else if (
nrows == 4320) {
1088 }
else if (
nrows == 8640) {
1090 }
else if (
nrows == 17280) {
1092 }
else if (
nrows == 34560) {
1094 }
else if (
nrows == 69120) {
1096 }
else if (
nrows == 172800) {
1098 }
else if (
nrows == 345600) {
1101 meta_l3b.resolution =
BAD_FLT;
1111 status = VSdetach(vdata_id[seagrid_idx]);
1112 status = VSdetach(vdata_id[binlist_idx]);
1113 status = VSdetach(vdata_id[binindex_idx]);
1115 if (binqual_idx != -1)
1116 VSdetach(vdata_id[binqual_idx]);
1118 for (int32_t
i = 0;
i < n_data_prod;
i++) {
1119 status = VSdetach(vdata_id[bindata_idx +
i]);
1128 status = Hclose(file_id);
1135 bool hdf4_bin::has_qual() {
1137 if (binqual_idx != -1) {
1147 int hdf5_bin::create(
const char *l3b_filename, int32_t nrows) {
1148 h5fid = H5Fcreate(l3b_filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
1150 cout <<
"Cannot create: " << l3b_filename << endl;
1154 this->nrows =
nrows;
1156 grp0 = H5Gopen1(h5fid,
"/");
1157 grp1 = H5Gcreate1(h5fid,
"Level-3 Binned Data", 0);
1162 totbins = binShape->getNumBins();
1172 int hdf5_bin::open(
const char* l3b_filename) {
1173 access_mode = H5F_ACC_RDONLY;
1174 h5fid = H5Fopen(l3b_filename, access_mode, H5P_DEFAULT);
1176 cout << l3b_filename <<
" cannot be opened." << endl;
1180 grp0 = H5Gopen1(h5fid,
"/");
1181 grp1 = H5Gopen1(h5fid,
"Level-3 Binned Data");
1183 cout <<
"Group \"Level-3 Binned Data\" cannot be opened in \""
1184 << l3b_filename <<
"\"" << endl;
1192 strcpy(meta_l3b.prod_type,
"");
1193 char *fstunderscore = (
char *) strchr(l3b_filename,
'_');
1194 if (fstunderscore !=
NULL) {
1195 char *sndunderscore = strchr(fstunderscore + 1,
'_');
1196 if (sndunderscore !=
NULL) {
1197 char prodtypebuf[8];
1198 memset(prodtypebuf, 0, 8);
1199 memcpy(prodtypebuf, fstunderscore + 1,
1200 sndunderscore - fstunderscore - 1);
1201 strcpy(meta_l3b.prod_type, prodtypebuf);
1206 H5Gget_num_objs(grp1, &numTables);
1212 for (
size_t i = 0;
i < numTables;
i++) {
1213 H5Gget_objname_by_idx(grp1,
i, nam_buf, 80);
1214 H5G_obj_t objType = H5Gget_objtype_by_idx(grp1,
i);
1215 if (objType == H5G_DATASET) {
1218 if (strcmp(nam_buf,
"BinIndex") == 0) {
1219 h5table_id[0][
i] = H5Dopen1(grp1,
"BinIndex");
1220 if (h5table_id[0][
i] < 0) {
1221 cout <<
"BinIndex cannot be opened" << endl;
1226 h5table_id[1][
i] = H5Dget_space(h5table_id[0][
i]);
1227 if (h5table_id[1][
i] < 0) {
1228 cout <<
"Filespace error for BinIndex" << endl;
1232 h5table_id[2][
i] = H5Dget_type(h5table_id[0][
i]);
1233 if (h5table_id[2][
i] < 0) {
1234 cout <<
"Datatype error for BinIndex" << endl;
1238 H5Sget_simple_extent_dims(h5table_id[1][
i],
1246 if (strcmp(nam_buf,
"BinList") == 0) {
1247 h5table_id[0][
i] = H5Dopen1(grp1,
"BinList");
1248 if (h5table_id[0][
i] < 0) {
1249 cout <<
"BinList cannot be opened" << endl;
1254 h5table_id[1][
i] = H5Dget_space(h5table_id[0][
i]);
1255 if (h5table_id[1][
i] < 0) {
1256 cout <<
"Filespace error for BinList" << endl;
1260 h5table_id[2][
i] = H5Dget_type(h5table_id[0][
i]);
1261 if (h5table_id[2][
i] < 0) {
1262 cout <<
"Datatype error for BinList" << endl;
1266 H5Sget_simple_extent_dims(h5table_id[1][
i],
1267 (hsize_t*) & n_data_records,
NULL);
1273 h5table_id[0][
i] = H5Dopen1(grp1, nam_buf);
1274 if (h5table_id[0][
i] < 0) {
1275 cout << nam_buf <<
" cannot be opened" << endl;
1279 h5table_id[1][
i] = H5Dget_space(h5table_id[0][
i]);
1280 if (h5table_id[1][
i] < 0) {
1281 cout <<
"Filespace error for " << nam_buf << endl;
1285 h5table_id[2][
i] = H5Dget_type(h5table_id[0][
i]);
1286 if (h5table_id[2][
i] < 0) {
1287 cout <<
"Datatype error for " << nam_buf << endl;
1291 strcpy(proddata_name[n_data_prod++], nam_buf);
1302 totbins = binShape->getNumBins();
1312 int hdf5_bin::readBinIndex(
int row_num_to_read) {
1317 hid_t
dataset = h5table_id[0][binindex_idx];
1319 cout <<
"Improper BinIndex id" << endl;
1323 hid_t filespace = h5table_id[1][binindex_idx];
1324 if (filespace < 0) {
1325 cout <<
"Improper Filespace id for BinIndex" << endl;
1329 hid_t datatype = h5table_id[2][binindex_idx];
1331 cout <<
"Improper Datatype id for BinIndex" << endl;
1335 hsize_t strt = row_num_to_read;
1336 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET,
1339 cout <<
"H5Sselect_hyperslab error (readBinIndex)" << endl;
1343 hid_t dataspace = H5Screate_simple(1, &one,
NULL);
1344 if (dataspace < 0) {
1345 cout <<
"Dataspace cannot be created for BinIndex." << endl;
1350 H5P_DEFAULT, &binIndex);
1352 cout <<
"H5Dread error (readBinIndex)" << endl;
1356 H5Sclose(dataspace);
1359 if (
sizeof (binIndex) == 40) {
1360 memmove(((
char *) &binIndex) + 8,
1361 ((
char *) &binIndex) + 4,
1363 memset(((
char *) &binIndex) + 4, 0, 4);
1371 int hdf5_bin::readBinList(
int nbins_to_read) {
1373 hsize_t n2read = nbins_to_read;
1376 if (lastBinListPtr == binListPtr && lastNumBins == nbins_to_read)
1378 lastBinListPtr = binListPtr;
1379 lastNumBins = nbins_to_read;
1381 hid_t
dataset = h5table_id[0][binlist_idx];
1383 cout <<
"Improper BinList id" << endl;
1387 hid_t filespace = h5table_id[1][binlist_idx];
1388 if (filespace < 0) {
1389 cout <<
"Improper Filespace id for BinList" << endl;
1393 hid_t datatype = h5table_id[2][binlist_idx];
1395 cout <<
"Improper Datatype id for BinList" << endl;
1399 hsize_t binListPtr_hsize;
1400 binListPtr_hsize = binListPtr;
1401 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET,
1402 &binListPtr_hsize,
NULL,
1405 cout <<
"H5Sselect_hyperslab error (readBinList)" << endl;
1409 hid_t dataspace = H5Screate_simple(1, &n2read,
NULL);
1410 if (dataspace < 0) {
1411 cout <<
"Dataspace cannot be created for BinList." << endl;
1416 H5P_DEFAULT, binList);
1418 cout <<
"H5Dread error (readBinList)" << endl;
1422 binListPtr += nbins_to_read;
1424 H5Sclose(dataspace);
1429 int hdf5_bin::readBinList(
int nbins_to_read, int32_t list_reset_ptr) {
1430 int32_t records_read;
1432 binListPtr = list_reset_ptr;
1434 records_read = this->readBinList(nbins_to_read);
1436 binListPtr = list_reset_ptr + records_read;
1438 return records_read;
1443 int hdf5_bin::readBinList() {
1450 int hdf5_bin::readQual(
unsigned char*
qual, int32_t nbins_to_read) {
1455 int hdf5_bin::readQual(
unsigned char*
qual, int32_t nbins_to_read,
1456 int32_t row_num_to_read) {
1463 int hdf5_bin::readSums(
float* sums, int32_t nbins_to_read,
int iprod) {
1466 hsize_t n2read = nbins_to_read;
1469 for (int32_t
i = 0;
i < n_data_prod;
i++) {
1470 if (active_data_prod[
i] ==
true && (
i == iprod || iprod == -1)) {
1472 hid_t
dataset = h5table_id[0][bindata_idx +
i];
1474 cout <<
"Improper BinData id for product: "
1479 hid_t filespace = h5table_id[1][bindata_idx +
i];
1480 if (filespace < 0) {
1481 cout <<
"Improper Filespace id for product: "
1486 hid_t datatype = h5table_id[2][bindata_idx +
i];
1488 cout <<
"Improper Datatype id for product: "
1493 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET,
1496 cout <<
"H5Sselect_hyperslab error for product: "
1501 hid_t dataspace = H5Screate_simple(1, &n2read,
NULL);
1502 if (dataspace < 0) {
1503 cout <<
"Dataspace cannot be created for product: "
1509 H5P_DEFAULT, &sums[2 *
k * nbins_to_read]);
1511 cout <<
"H5Dread error for product: "
1516 H5Sclose(dataspace);
1527 int hdf5_bin::readSums(
float* sums, int32_t* listOfBins,
1528 int32_t nbins_to_read,
int iprod) {
1535 for (int32_t
i = 0;
i < n_data_prod;
i++) {
1536 if (active_data_prod[
i] ==
true && (
i == iprod || iprod == -1)) {
1538 hid_t
dataset = h5table_id[0][bindata_idx +
i];
1540 cout <<
"Improper BinData id for product: "
1545 hid_t filespace = h5table_id[1][bindata_idx +
i];
1546 if (filespace < 0) {
1547 cout <<
"Improper Filespace id for product: "
1552 hid_t datatype = h5table_id[2][bindata_idx +
i];
1554 cout <<
"Improper Datatype id for product: "
1559 hid_t dataspace = H5Screate_simple(1, &one,
NULL);
1560 if (dataspace < 0) {
1561 cout <<
"Dataspace cannot be created for product: "
1566 for (int32_t
j = 0;
j < nbins_to_read;
j++) {
1567 binPtr = listOfBins[
j];
1568 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET,
1571 cout <<
"H5Sselect_hyperslab error for product: "
1577 H5P_DEFAULT, &sums[2 *
k]);
1579 cout <<
"H5Dread error for product: "
1587 H5Sclose(dataspace);
1595 int hdf5_bin::write(
const char *product_list, hsize_t nwrite,
float *outData,
1600 const char* fldname2[] = {
"bin_num",
"nobs",
"nscenes",
1601 "weights",
"flags_set"};
1603 hsize_t dim = nwrite;
1604 hsize_t unlimited = H5S_UNLIMITED;
1606 hid_t dataspace = H5Screate_simple(1, &dim, &unlimited);
1607 if (dataspace < 0) {
1608 cout <<
"Dataspace cannot be created (write)." << endl;
1612 if (binList !=
NULL) {
1613 if (h5table_id[0][binlist_idx] == -1) {
1630 &h5table_id[0][binlist_idx],
1631 &h5table_id[2][binlist_idx],
1633 &h5table_id[1][binlist_idx], dataspace);
1635 hsize_t newsize = n_data_records + nwrite;
1636 status = H5Dextend(h5table_id[0][binlist_idx], &newsize);
1637 H5Sset_extent_simple(h5table_id[1][binlist_idx], 1, &newsize,
NULL);
1639 hid_t
dataset = h5table_id[0][binlist_idx];
1641 cout <<
"Improper Dataset id for BinList (write)" << endl;
1645 hid_t filespace = h5table_id[1][binlist_idx];
1646 if (filespace < 0) {
1647 cout <<
"Improper Filespace id for BinList (write)" << endl;
1651 hid_t binlist_tid = h5table_id[2][binlist_idx];
1652 if (binlist_tid < 0) {
1653 cout <<
"Improper Datatype id for BinList (write)" << endl;
1657 const hsize_t
start = n_data_records;
1658 const hsize_t
count = nwrite;
1659 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET,
1662 cout <<
"H5Sselect_hyperslab error for BinList (write)" << endl;
1667 filespace, H5P_DEFAULT, binList);
1669 cout <<
"Error writing BinList (write)." << endl;
1670 cout <<
"Dataset id: " <<
dataset << endl;
1671 cout <<
"Datatype id: " << binlist_tid << endl;
1672 cout <<
"Filespace id: " << filespace << endl;
1673 cout <<
"Start: " << n_data_records << endl;
1674 cout <<
"Count: " << nwrite << endl;
1680 if (product_list ==
NULL) {
1681 H5Sclose(dataspace);
1688 int n = strlen(product_list);
1689 char *buf = (
char *) malloc(n + 1);
1690 strcpy(buf, product_list);
1691 char *pch = strtok(buf,
",");
1693 while (pch !=
NULL) {
1697 while (h5table_id[0][bindata_idx +
i] != -1) {
1698 H5Iget_name(h5table_id[0][bindata_idx +
i], ds_name, 200);
1699 if (strcmp(pch, &ds_name[21]) == 0)
1704 if (h5table_id[0][bindata_idx +
i] == -1) {
1706 strcpy(proddata_name[n_datasets++], pch);
1708 fldname3[0] = (
char *) calloc(strlen(pch) + 5,
sizeof (char));
1709 fldname3[1] = (
char *) calloc(strlen(pch) + 8,
sizeof (char));
1710 strcpy(fldname3[0], pch);
1711 strcat(fldname3[0],
"_sum");
1712 strcpy(fldname3[1], pch);
1713 strcat(fldname3[1],
"_sum_sq");
1715 hid_t
type[2] = {H5T_NATIVE_FLOAT, H5T_NATIVE_FLOAT};
1716 size_t offset[2] = {0, 4};
1718 &h5table_id[0][bindata_idx +
i],
1719 &h5table_id[2][bindata_idx +
i],
1721 &h5table_id[1][bindata_idx +
i], dataspace);
1726 hsize_t newsize = n_data_records + nwrite;
1727 status = H5Dextend(h5table_id[0][bindata_idx +
i], &newsize);
1728 H5Sset_extent_simple(h5table_id[1][bindata_idx +
i], 1,
1732 hid_t
dataset = h5table_id[0][bindata_idx +
i];
1734 cout <<
"Improper Dataset id for: " << pch << endl;
1738 hid_t filespace = h5table_id[1][bindata_idx +
i];
1739 if (filespace < 0) {
1740 cout <<
"Improper Filespace id for: " << pch << endl;
1744 hid_t datatype = h5table_id[2][bindata_idx +
i];
1746 cout <<
"Improper Datatype id for: " << pch << endl;
1750 const hsize_t
start = n_data_records;
1751 const hsize_t
count = nwrite;
1752 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET,
1755 cout <<
"H5Sselect_hyperslab error for: " << pch << endl;
1760 dataspace, filespace, H5P_DEFAULT, outData);
1762 cout <<
"Error writing: " << pch << endl;
1763 cout <<
"Dataset id: " <<
dataset << endl;
1764 cout <<
"Datatype id: " << datatype << endl;
1765 cout <<
"Filespace id: " << filespace << endl;
1766 cout <<
"Start: " << n_data_records << endl;
1767 cout <<
"Count: " << nwrite << endl;
1771 pch = strtok(
NULL,
",");
1775 H5Sclose(dataspace);
1780 int hdf5_bin::writeBinList(int32_t nbins_to_write) {
1784 const char* fldname2[] = {
"bin_num",
"nobs",
"nscenes",
1785 "weights",
"flags_set"};
1787 hsize_t dim = nbins_to_write;
1789 hsize_t unlimited = H5S_UNLIMITED;
1791 hid_t dataspace = H5Screate_simple(1, &dim, &unlimited);
1793 if (h5table_id[0][binlist_idx] == -1) {
1810 &h5table_id[0][binlist_idx],
1811 &h5table_id[2][binlist_idx],
1813 &h5table_id[1][binlist_idx], dataspace);
1817 hsize_t newsize = n_data_records + nbins_to_write;
1818 status = H5Dextend(h5table_id[0][binlist_idx], &newsize);
1819 H5Sset_extent_simple(h5table_id[1][binlist_idx], 1, &newsize,
NULL);
1821 hid_t
dataset = h5table_id[0][binlist_idx];
1823 cout <<
"Improper Dataset id for BinList" << endl;
1827 hid_t filespace = h5table_id[1][binlist_idx];
1828 if (filespace < 0) {
1829 cout <<
"Improper Filespace id for BinList (writeBinList)." << endl;
1838 hid_t binlist_tid = h5table_id[2][binlist_idx];
1839 if (binlist_tid < 0) {
1840 cout <<
"Improper Datatype id for BinList (writeBinList)." << endl;
1844 const hsize_t
start = n_data_records;
1845 const hsize_t
count = nbins_to_write;
1846 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, &
start,
1850 filespace, H5P_DEFAULT, binList);
1852 cout <<
"Error writing BinList (writeBinList)." << endl;
1853 cout <<
"Dataset id: " <<
dataset << endl;
1854 cout <<
"Datatype id: " << binlist_tid << endl;
1855 cout <<
"Filespace id: " << filespace << endl;
1856 cout <<
"Start: " << n_data_records << endl;
1857 cout <<
"Count: " << nbins_to_write << endl;
1861 H5Sclose(dataspace);
1866 int hdf5_bin::writeSums(
float* sums, int32_t nbins_to_write,
1867 const char *prodname) {
1870 for (iprod = 0; iprod < n_data_prod; iprod++) {
1871 if (strcmp(proddata_name[iprod], prodname) == 0) {
1877 hsize_t dim = nbins_to_write;
1879 hsize_t unlimited = H5S_UNLIMITED;
1880 hid_t dataspace = H5Screate_simple(1, &dim, &unlimited);
1887 if (found ==
false) {
1888 strcpy(proddata_name[n_data_prod], prodname);
1890 fldname3[0] = (
char *) calloc(strlen(prodname) + 5,
sizeof (char));
1891 fldname3[1] = (
char *) calloc(strlen(prodname) + 8,
sizeof (char));
1893 strcpy(fldname3[0], prodname);
1894 strcat(fldname3[0],
"_sum");
1895 strcpy(fldname3[1], prodname);
1896 strcat(fldname3[1],
"_sum_sq");
1897 hid_t
type[2] = {H5T_NATIVE_FLOAT, H5T_NATIVE_FLOAT};
1898 size_t offset[2] = {0, 4};
1900 &h5table_id[0][bindata_idx + n_data_prod],
1901 &h5table_id[2][bindata_idx + n_data_prod],
1903 &h5table_id[1][bindata_idx + n_data_prod], dataspace);
1911 hsize_t newsize = n_data_records + nbins_to_write;
1912 status = H5Dextend(h5table_id[0][bindata_idx + iprod], &newsize);
1913 H5Sset_extent_simple(h5table_id[1][bindata_idx + iprod], 1,
1916 hid_t
dataset = h5table_id[0][bindata_idx + iprod];
1917 hid_t filespace = h5table_id[1][bindata_idx + iprod];
1918 hid_t bindata_tid = h5table_id[2][bindata_idx + iprod];
1921 const hsize_t
start = n_data_records;
1922 const hsize_t
count = nbins_to_write;
1923 status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, &
start,
1926 status = H5Dwrite(h5table_id[0][bindata_idx + iprod], bindata_tid,
1927 dataspace, filespace, H5P_DEFAULT, sums);
1929 cout <<
"Error writing product: " << iprod << endl;
1930 cout <<
"Dataset id: " <<
dataset << endl;
1931 cout <<
"Datatype id: " << bindata_tid << endl;
1932 cout <<
"Filespace id: " << filespace << endl;
1933 cout <<
"Start: " << n_data_records << endl;
1934 cout <<
"Count: " << nbins_to_write << endl;
1938 H5Sclose(dataspace);
1943 int hdf5_bin::close() {
1945 int32_t n_total_bins = 0;
1946 float slat = +90, nlat = -90, elon = -180, wlon = +180;
1949 H5E_auto_t old_func;
1950 void *old_client_data;
1951 H5Eget_auto(H5E_DEFAULT, &old_func, &old_client_data);
1953 H5Eset_auto(H5E_DEFAULT,
NULL,
NULL);
1954 hid_t
dataset = H5Dopen1(grp1,
"BinIndex");
1957 H5Eset_auto(H5E_DEFAULT, old_func, old_client_data);
1960 int32_t *binnum_data = (int32_t *) calloc(n_data_records,
sizeof (int32_t));
1962 hid_t tid = H5Tcreate(H5T_COMPOUND,
sizeof (int32_t));
1964 status = H5Tinsert(tid,
"bin_num", 0, H5T_STD_U32LE);
1966 status = H5Dread(h5table_id[0][binlist_idx], tid,
1967 H5S_ALL, H5S_ALL, H5P_DEFAULT, binnum_data);
1972 for (int32_t
i = 0;
i < n_data_records - 1;
i++) {
1973 if (binnum_data[
i] >= binnum_data[
i + 1]) {
1974 printf(
"Improper bin number: %ld at element: %d\n",
1975 (
long int) binnum_data[
i], (
int)
i);
1979 int32_t *beg = (int32_t *) calloc(
nrows,
sizeof (int32_t));
1980 int32_t *
ext = (int32_t *) calloc(
nrows,
sizeof (int32_t));
1982 int32_t *numbin_out = (int32_t *) calloc(
nrows,
sizeof (int32_t));
1983 int32_t *basebin_out = (int32_t *) calloc(
nrows + 1,
sizeof (int32_t));
1985 for (int32_t
i = 0;
i <
nrows;
i++) {
1986 float latbin = (
i + 0.5) * (180.0 / (
nrows)) - 90.0;
1987 numbin_out[
i] = (int32_t) (cos(latbin *
PI / 180.0) * (2.0 *
nrows) + 0.5);
1988 n_total_bins += numbin_out[
i];
1992 for (int32_t
i = 1;
i <=
nrows;
i++) {
1993 basebin_out[
i] = basebin_out[
i - 1] + numbin_out[
i - 1];
2002 uint8_t *
a = (uint8_t *) calloc(36 *
nrows, 1);
2004 for (int32_t
i = 0;
i <
nrows;
i++) {
2005 double vsize = 180.0 /
nrows;
2006 double hsize = 360.0 / numbin_out[
i];
2007 memcpy(&
a[
i * 36], &
i, 4);
2008 memcpy(&
a[
i * 36 + 4], &vsize, 8);
2009 memcpy(&
a[
i * 36 + 12], &hsize, 8);
2010 memcpy(&
a[
i * 36 + 20], &basebin_out[
i], 4);
2011 memcpy(&
a[
i * 36 + 24], &beg[
i], 4);
2012 memcpy(&
a[
i * 36 + 28], &
ext[
i], 4);
2013 memcpy(&
a[
i * 36 + 32], &numbin_out[
i], 4);
2019 lat = ((
i + 0.5) /
nrows) * 180.0 - 90.0;
2025 lon = 360.0 * (beg[
i] - basebin_out[
i] + 0.5)
2026 / numbin_out[
i] - 180.0;
2030 lon = 360.0 * (binnum_data[i32 +
ext[
i] - 1] - basebin_out[
i] + 0.5)
2031 / numbin_out[
i] - 180.0;
2038 const char* fldname4[] = {
"row_num",
"vsize",
"hsize",
"start_num",
2039 "begin",
"extent",
"max"};
2042 type[0] = H5T_STD_I32LE;
2043 type[1] = H5T_NATIVE_DOUBLE;
2044 type[2] = H5T_NATIVE_DOUBLE;
2045 type[3] = H5T_STD_I32LE;
2046 type[4] = H5T_STD_I32LE;
2047 type[5] = H5T_STD_I32LE;
2048 type[6] = H5T_STD_I32LE;
2050 binindex_idx = n_datasets;
2051 size_t offset[7] = {0, 4, 12, 20, 24, 28, 32};
2053 hsize_t
start[1] = {0};
2054 hsize_t dim =
nrows;
2055 hid_t dataspace = H5Screate_simple(1, &dim,
NULL);
2057 &h5table_id[0][binindex_idx],
2058 &h5table_id[2][binindex_idx],
2060 &h5table_id[1][binindex_idx], dataspace);
2062 status = H5Sselect_hyperslab(h5table_id[1][binindex_idx],
2065 status = H5Dwrite(h5table_id[0][binindex_idx],
2066 h5table_id[2][binindex_idx], dataspace,
2067 h5table_id[1][binindex_idx], H5P_DEFAULT,
a);
2069 cout <<
"Error writing BinIndex." << endl;
2073 H5Sclose(dataspace);
2090 strcpy(buf, this->meta_l3b.product_name);
2091 if (strcmp(buf,
"___") == 0)
2096 strcpy(buf, this->meta_l3b.sensor_name);
2097 strcat(buf,
" Level-3 Binned Data");
2100 strcpy(buf, this->meta_l3b.sensor);
2103 strcpy(buf, this->meta_l3b.sensor_char);
2106 strcpy(buf, this->meta_l3b.mission);
2109 strcpy(buf, this->meta_l3b.mission_char);
2112 strcpy(buf, this->meta_l3b.soft_name);
2115 strcpy(buf, this->meta_l3b.soft_ver);
2118 strcpy(buf, this->meta_l3b.ptime);
2121 strcpy(buf, this->meta_l3b.proc_con);
2124 strcpy(buf, this->meta_l3b.flag_names);
2127 strcpy(buf, this->meta_l3b.infiles);
2131 (VOIDP) & this->meta_l3b.start_orb));
2133 (VOIDP) & this->meta_l3b.end_orb));
2157 strcpy(buf,
"degrees North");
2159 strcpy(buf,
"degrees East");
2162 PTB(
SetScalarH5A(grp0,
"Northernmost Latitude", H5T_NATIVE_FLOAT, (VOIDP) & nlat));
2163 PTB(
SetScalarH5A(grp0,
"Southernmost Latitude", H5T_NATIVE_FLOAT, (VOIDP) & slat));
2164 PTB(
SetScalarH5A(grp0,
"Easternmost Longitude", H5T_NATIVE_FLOAT, (VOIDP) & elon));
2165 PTB(
SetScalarH5A(grp0,
"Westernmost Longitude", H5T_NATIVE_FLOAT, (VOIDP) & wlon));
2167 PTB(
SetScalarH5A(grp0,
"Data Bins", H5T_STD_U32LE, (VOIDP) & n_data_records));
2168 float f32 = 100 * ((
float) n_data_records) / n_total_bins;
2174 if (h5table_id[0][
i] != -1)
2175 H5Dclose(h5table_id[0][
i]);
2176 if (h5table_id[1][
i] != -1)
2177 H5Sclose(h5table_id[1][
i]);
2178 if (h5table_id[2][
i] != -1)
2179 H5Tclose(h5table_id[2][
i]);
2192 int cdf4_bin::create(
const char *l3b_filename, int32_t nrows) {
2200 if (n_data_prod < 1) {
2201 cout <<
"Error - " << __FILE__ <<
":" << __LINE__ <<
2202 " - setProductList needs to be set before calling create." << endl;
2206 status = nc_create(l3b_filename, NC_NETCDF4, &ncid);
2208 status = nc_def_grp(ncid,
"level-3_binned_data", &grp1);
2211 this->nrows =
nrows;
2216 totbins = binShape->getNumBins();
2247 int cdf4_bin::open(
const char* l3b_filename) {
2248 access_mode = NC_NOWRITE;
2252 status = nc_open(l3b_filename, access_mode, &ncid);
2253 if (
status != NC_NOERR) {
2254 cout << l3b_filename <<
" cannot be opened." << endl;
2257 status = nc_inq_ncid(ncid,
"level-3_binned_data", &grp1);
2258 status = nc_inq_dimid(grp1,
"binIndexDim", &dimid);
2259 status = nc_inq_dimlen(grp1, dimid, (
size_t *) &
nrows);
2261 status = nc_inq_dimid(grp1,
"binListDim", &dimid);
2262 status = nc_inq_dimlen(grp1, dimid, (
size_t *) & n_data_records);
2264 status = nc_inq_varid(grp1,
"BinIndex", &binindex_idx);
2265 status = nc_inq_varid(grp1,
"BinList", &binlist_idx);
2268 status = nc_inq_varid(grp1,
"qual_l3", &binqual_idx);
2269 if (
status == NC_NOERR) {
2285 status = nc_inq_vartype(grp1, binlist_idx, &xtype);
2288 status = nc_inq_compound_fieldindex(grp1, xtype,
"bin_num", &fieldId);
2291 status = nc_inq_compound_fieldtype(grp1, xtype, fieldId, &fieldType);
2293 if(fieldType == NC_UINT64)
2299 status = nc_inq_varids(grp1, &numTables,
NULL);
2300 int *varids = (
int *) calloc(numTables,
sizeof (
int));
2301 status = nc_inq_varids(grp1, &numTables, varids);
2306 for (
int i = 0;
i < numTables;
i++) {
2307 status = nc_inq_varname(grp1, varids[
i], nam_buf);
2309 if (strcmp(nam_buf,
"BinIndex") == 0)
2311 if (strcmp(nam_buf,
"BinList") == 0)
2314 strcpy(proddata_name[n_data_prod++], nam_buf);
2323 status = nc_get_att(ncid, NC_GLOBAL,
"binning_scheme", nam_buf);
2324 if (
status == NC_NOERR) {
2325 strcpy(this->meta_l3b.binning_scheme, nam_buf);
2327 cout <<
"Binning Scheme metadata not found" << endl;
2332 totbins = binShape->getNumBins();
2344 int cdf4_bin::readBinIndex(
int row_num_to_read) {
2346 size_t start = row_num_to_read;
2349 status = nc_get_var1(grp1, binindex_idx, &
start, (
void *) &binIndex64);
2351 status = nc_get_var1(grp1, binindex_idx, &
start, (
void *) &binIndex);
2352 if (
status != NC_NOERR) {
2353 printf(
"-E- %s %d: %s for %s\n",
2354 __FILE__, __LINE__, nc_strerror(
status),
"binIndex");
2363 int cdf4_bin::readBinList() {
2370 int cdf4_bin::readBinList(
int nbins_to_read) {
2372 size_t n2read = nbins_to_read;
2374 if (lastBinListPtr == binListPtr && lastNumBins == nbins_to_read) {
2375 binListPtr += nbins_to_read;
2376 return nbins_to_read;
2378 lastBinListPtr = binListPtr;
2379 lastNumBins = nbins_to_read;
2381 if(nbins_to_read > 0) {
2383 status = nc_get_vara(grp1, binlist_idx, &binListPtr, &n2read, (
void *) binList64);
2385 status = nc_get_vara(grp1, binlist_idx, &binListPtr, &n2read, (
void *) binList);
2387 if (
status != NC_NOERR) {
2388 printf(
"-E- %s %d: %s for %s\n",
2389 __FILE__, __LINE__, nc_strerror(
status),
"binList");
2393 binListPtr += nbins_to_read;
2396 return nbins_to_read;
2399 int cdf4_bin::readBinList(
int nbins_to_read, int32_t list_reset_ptr) {
2400 binListPtr = list_reset_ptr;
2401 this->readBinList(nbins_to_read);
2403 return nbins_to_read;
2408 int cdf4_bin::readQual(
unsigned char*
qual, int32_t nbins_to_read) {
2410 size_t n2read = nbins_to_read;
2412 if(nbins_to_read > 0) {
2413 status = nc_get_vara(grp1, binqual_idx, &binQualityPtr, &n2read, (
void *)
qual);
2414 if (
status != NC_NOERR) {
2415 printf(
"-E- %s %d: %s in readQual\n",
2416 __FILE__, __LINE__, nc_strerror(
status));
2419 binQualityPtr += nbins_to_read;
2425 int cdf4_bin::readQual(
unsigned char*
qual, int32_t nbins_to_read,
2426 int32_t row_num_to_read) {
2429 size_t n2read = nbins_to_read;
2430 size_t start = row_num_to_read;
2432 if(nbins_to_read > 0) {
2433 status = nc_get_vara(grp1, binqual_idx, &
start, &n2read, (
void *)
qual);
2434 if (
status != NC_NOERR) {
2435 printf(
"-E- %s %d: %s in readQual\n",
2436 __FILE__, __LINE__, nc_strerror(
status));
2441 binQualityPtr = row_num_to_read + nbins_to_read;
2448 int cdf4_bin::readSums(
float* sums, int32_t nbins_to_read,
int iprod) {
2450 size_t n2read = nbins_to_read;
2453 for (int32_t
i = 0;
i < n_data_prod;
i++) {
2454 if (active_data_prod[
i] ==
true && (
i == iprod || iprod == -1)) {
2456 status = nc_get_vara(grp1, bindata_idx +
i,
2457 (
size_t *) & binDataPtr, &n2read,
2458 (
void *) &sums[2 *
k * nbins_to_read]);
2459 if (
status != NC_NOERR) {
2460 printf(
"-E- %s %d: %s for prod# %d\n",
2461 __FILE__, __LINE__, nc_strerror(
status), iprod);
2474 int cdf4_bin::readSums(
float* sums, int32_t* listOfBins,
2475 int32_t nbins_to_read,
int iprod) {
2481 for (int32_t
i = 0;
i < n_data_prod;
i++) {
2482 if (active_data_prod[
i] ==
true && (
i == iprod || iprod == -1)) {
2484 for (int32_t
j = 0;
j < nbins_to_read;
j++) {
2485 binPtr = listOfBins[
j];
2486 status = nc_get_vara(grp1, bindata_idx +
i,
2487 (
size_t *) & binPtr, &one, (
void *) &sums[2 *
k]);
2488 if (
status != NC_NOERR) {
2489 printf(
"-E- %s %d: %s for prod# %d\n",
2490 __FILE__, __LINE__, nc_strerror(
status), iprod);
2501 int cdf4_bin::write(
const char *product_list, hsize_t nwrite,
float *outData,
2506 int cdf4_bin::writeBinList(int32_t nbins_to_write) {
2516 int cdf4_bin::writeQual(uint8_t*
qual, int32_t nbins_to_write) {
2523 int cdf4_bin::writeSums(
float* sums, int32_t nbins_to_write,
2524 const char *prodname) {
2528 strcpy(proddata_name[n_data_prod], prodname);
2529 for (iprod = 0; iprod < n_data_prod; iprod++) {
2530 if (strcmp(proddata_name[iprod], prodname) == 0) {
2543 int cdf4_bin::close() {
2546 int64_t n_total_bins = 0;
2547 float slat = +90, nlat = -90, elon = -180, wlon = +180;
2551 if (binindex_idx == -1) {
2552 if(n_data_records > 0) {
2553 int64_t *binnum_data = (int64_t *) calloc(n_data_records,
sizeof (int64_t));
2554 status = nc_inq_varid(grp1,
"BinList", &binlist_idx);
2555 if (
status != NC_NOERR) {
2556 cout <<
"Error looking for BinList in output file" << endl;
2564 count = n_data_records;
2566 if (
status != NC_NOERR) {
2567 cout <<
"Error reading output file binlist records" << endl;
2570 for (
size_t i = 0; (
int)
i < n_data_records;
i++) {
2578 count = n_data_records;
2580 if (
status != NC_NOERR) {
2581 cout <<
"Error reading output file binlist records" << endl;
2584 for (
size_t i = 0; (
int)
i < n_data_records;
i++) {
2590 for (int32_t
i = 0;
i < n_data_records - 1;
i++) {
2591 if (binnum_data[
i] >= binnum_data[
i + 1]) {
2592 printf(
"Improper bin number: %ld at element: %d\n",
2593 (
long int) binnum_data[
i], (
int)
i);
2597 int64_t *beg = (int64_t *) calloc(
nrows,
sizeof (int64_t));
2598 int32_t *
ext = (int32_t *) calloc(
nrows,
sizeof (int32_t));
2600 int64_t *numbin_out = (int64_t *) calloc(
nrows,
sizeof (int64_t));
2601 int64_t *basebin_out = (int64_t *) calloc(
nrows + 1,
sizeof (int64_t));
2603 if ((
nrows % 2) == 0) {
2604 for (int32_t
i = 0;
i <
nrows;
i++) {
2605 double latbin = (
i + 0.5) * (180.0 / (
nrows)) - 90.0;
2606 numbin_out[
i] = (int64_t) (cos(latbin *
PI / 180.0)
2607 * (2.0 *
nrows) + 0.5);
2608 n_total_bins += numbin_out[
i];
2612 int nside = (
nrows + 1) / 4;
2613 totbins = 12 * nside * nside;
2614 for (int32_t
i = 0;
i <=
nrows / 2;
i++) {
2615 numbin_out[
i] = 4 * (
i + 1);
2616 if (numbin_out[
i] > 4 * nside)
2617 numbin_out[
i] = 4 * nside;
2618 numbin_out[
nrows - 1 -
i] = numbin_out[
i];
2623 for (int32_t
i = 1;
i <=
nrows;
i++) {
2624 basebin_out[
i] = basebin_out[
i - 1] + numbin_out[
i - 1];
2635 for (int32_t
i = 0;
i <
nrows;
i++) {
2639 binIndex0[
i].
max = numbin_out[
i];
2642 delete [] binIndex0;
2645 for (int32_t
i = 0;
i <
nrows;
i++) {
2649 binIndex0[
i].
max = numbin_out[
i];
2652 delete [] binIndex0;
2658 for (int32_t
i = 0;
i <
nrows;
i++) {
2661 lat = ((
i + 0.5) /
nrows) * 180.0 - 90.0;
2667 lon = 360.0 * (beg[
i] - basebin_out[
i] + 0.5)
2668 / numbin_out[
i] - 180.0;
2672 lon = 360.0 * (binnum_data[i32 +
ext[
i] - 1] - basebin_out[
i] + 0.5)
2673 / numbin_out[
i] - 180.0;
2693 meta_l3b.north = nlat;
2694 meta_l3b.south = slat;
2695 meta_l3b.east = elon;
2696 meta_l3b.west = wlon;
2697 meta_l3b.data_bins = n_data_records;
2698 meta_l3b.pct_databins = 100 * ((
float) n_data_records) / n_total_bins;
2702 }
else if (
nrows == 360) {
2704 }
else if (
nrows == 540) {
2706 }
else if (
nrows == 720) {
2708 }
else if (
nrows == 1080) {
2710 }
else if (
nrows == 2160) {
2712 }
else if (
nrows == 4320) {
2714 }
else if (
nrows == 8640) {
2716 }
else if (
nrows == 17280) {
2718 }
else if (
nrows == 34560) {
2720 }
else if (
nrows == 69120) {
2722 }
else if (
nrows == 172800) {
2724 }
else if (
nrows == 345600) {
2727 meta_l3b.resolution =
BAD_FLT;
2749 bool cdf4_bin::has_qual() {
2751 if (binqual_idx != -1) {
2764 int hdf_bin::query() {
2766 for (
size_t i = 0; (
int)
i < n_data_prod;
i++) {
2767 nchar += strlen(proddata_name[
i]) + 1;
2777 int hdf_bin::query(
char* product_list) {
2778 product_list[0] = 0;
2779 for (int32_t
i = 0;
i < n_data_prod;
i++) {
2780 strcat(product_list, proddata_name[
i]);
2781 if (
i != n_data_prod - 1)
2782 strcat(product_list,
",");
2789 int hdf_bin::query(
char ***prod_array) {
2791 if (product_array[0] ==
NULL) {
2792 for (int32_t
i = 0;
i < n_data_prod;
i++) {
2793 product_array[
i] = proddata_name[
i];
2796 if (prod_array !=
NULL)
2797 *prod_array = &product_array[0];
2802 int hdf_bin::get_prodname(
int iprod,
char *prodname) {
2803 strcpy(prodname, proddata_name[iprod]);
2807 void hdf_bin::setProductList(
int numProducts,
char* prodNames[]) {
2809 printf(
"Error - %s:%d - setProductList - Maximum number of products exceeded",
2810 __FILE__, __LINE__);
2814 n_data_prod = numProducts;
2815 for (
int i = 0;
i < numProducts;
i++) {
2816 if (strlen(prodNames[
i]) >= 80) {
2817 printf(
"Error - %s:%d - setProductList - Product name too long \"%s\"",
2818 __FILE__, __LINE__, prodNames[
i]);
2821 strcpy(proddata_name[
i], prodNames[
i]);
2822 product_array[
i] = proddata_name[
i];
2831 const char* hdf_bin::getProdName(
int prodIndex)
const {
2832 if (prodIndex < 0 || prodIndex >=
MAXNPROD)
2834 return proddata_name[prodIndex];
2842 int hdf_bin::getProdIndex(
const char *prodname)
const {
2844 for (
int i = 0;
i < n_data_prod;
i++) {
2845 if (strcmp(proddata_name[
i], prodname) == 0) {
2858 const char* hdf_bin::getActiveProdName(
int prodIndex)
const {
2859 if (prodIndex < 0 || prodIndex >= n_active_prod)
2863 for (
i = 0;
i < n_data_prod;
i++) {
2864 if (active_data_prod[
i]) {
2865 if (activeProd == prodIndex) {
2866 return proddata_name[
i];
2879 strcpy(this->meta_l3b.mission_char,
2891 this->meta_l3b.start_orb = sorbit;
2899 this->meta_l3b.end_orb = eorbit;
2909 if (input_binfile[
ifile]->meta_l3b.startTime < stime) {
2919 if (input_binfile[
ifile]->meta_l3b.endTime > etime) {
2926 this->meta_l3b.bin_syear = input_binfile[sfile]->
meta_l3b.bin_syear;
2927 this->meta_l3b.bin_sday = input_binfile[sfile]->
meta_l3b.bin_sday;
2928 this->meta_l3b.bin_eyear = input_binfile[efile]->
meta_l3b.bin_eyear;
2929 this->meta_l3b.bin_eday = input_binfile[efile]->
meta_l3b.bin_eday;
2931 this->meta_l3b.syear = input_binfile[sfile]->
meta_l3b.syear;
2932 this->meta_l3b.sday = input_binfile[sfile]->
meta_l3b.sday;
2933 this->meta_l3b.smsec = input_binfile[sfile]->
meta_l3b.smsec;
2935 this->meta_l3b.eyear = input_binfile[efile]->
meta_l3b.eyear;
2936 this->meta_l3b.eday = input_binfile[efile]->
meta_l3b.eday;
2937 this->meta_l3b.emsec = input_binfile[efile]->
meta_l3b.emsec;
2945 strcpy(this->meta_l3b.binning_scheme,