30 #define NEW_CACHE_SIZE 16000000
31 #define NEW_CACHE_NELEMS 2003
32 #define NEW_CACHE_PREEMPTION .75
34 #define ROUND(x) ((((x) >= 0)?0.5:-0.5) + (x))
39 for (
i = 0;
i < l;
i++) {
50 int32_t attr_index = -1;
53 attr_index = SDfindattr(ds_id.
fid, nam);
55 attr_index = SDfindattr(ds_id.
sid, nam);
61 status = nc_inq_attid(ds_id.
fid, NC_GLOBAL, nam, (int32_t *) & attr_index);
63 status = nc_inq_attid(-ds_id.
fid, ds_id.
sid, nam, (int32_t *) & attr_index);
69 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
89 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
114 if (
dtype != DFNT_CHAR)
125 if (
dtype != DFNT_CHAR)
140 if(
dtype == NC_CHAR) {
146 }
else if(
dtype == NC_STRING) {
168 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
178 printf(
"-E- %s %d Could not allocate memory for reading attribute %s\n", __FILE__, __LINE__,
name);
197 attr_index = SDfindattr(ds_id.
fid, nam);
200 attr_index = SDfindattr(ds_id.
sid, nam);
206 status = nc_inq_att(ds_id.
fid, NC_GLOBAL, nam, (int32_t *)
dtype, &cnt);
213 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
219 int setAttr(
idDS ds_id,
const char *nam, int32_t typ, int32_t cnt,
const void*
data) {
226 printf(
"-E- %s %d: %s for %s\n",
227 __FILE__, __LINE__, nc_strerror(
status), nam);
230 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
244 printf(
"-E- %s %d: %s for %s\n",
245 __FILE__, __LINE__, nc_strerror(
status),
name);
248 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
260 NC_DOUBLE, 1, (
const double *) &
value);
262 printf(
"-E- %s %d: %s for %s\n",
263 __FILE__, __LINE__, nc_strerror(
status),
name);
266 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
278 NC_FLOAT, 1, (
const float *) &
value);
280 printf(
"-E- %s %d: %s for %s\n",
281 __FILE__, __LINE__, nc_strerror(
status),
name);
284 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
296 NC_SHORT, 1, (
const short *) &
value);
298 printf(
"-E- %s %d: %s for %s\n",
299 __FILE__, __LINE__, nc_strerror(
status),
name);
302 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
314 NC_BYTE, 1, (
const uint8 *) &
value);
316 printf(
"-E- %s %d: %s for %s\n",
317 __FILE__, __LINE__, nc_strerror(
status),
name);
320 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
332 NC_INT, 1, (
const int *) &
value);
334 printf(
"-E- %s %d: %s for %s\n",
335 __FILE__, __LINE__, nc_strerror(
status),
name);
338 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
344 int createDS(
idDS ds_id,
int sensorId,
const char *sname, int32_t dm[3],
345 const char dm_name[3][80]) {
346 static productInfo_t* p_info;
348 if (p_info ==
NULL) {
353 return createDS2(ds_id, sname, p_info, dm, dm_name);
355 printf(
"%s not found in XML product table\n", sname);
360 int createDS2(
idDS ds_id,
const char *sname, productInfo_t* p_info, int32_t dm[3],
361 const char dm_name[3][80]) {
367 if (strcmp(p_info->dataType,
"byte") == 0)
369 else if (strcmp(p_info->dataType,
"ubyte") == 0)
371 else if (strcmp(p_info->dataType,
"short") == 0)
373 else if (strcmp(p_info->dataType,
"ushort") == 0)
375 else if (strcmp(p_info->dataType,
"int") == 0)
377 else if (strcmp(p_info->dataType,
"uint") == 0)
379 else if (strcmp(p_info->dataType,
"float") == 0)
381 else if (strcmp(p_info->dataType,
"double") == 0)
384 printf(
"-E- %s %d: datatype %s is not valid\n", __FILE__, __LINE__, p_info->dataType);
389 p_info->units, p_info->validMin, p_info->validMax, p_info->scaleFactor,
390 p_info->addOffset, nt, p_info->rank, dm[0], dm[1], dm[2],
391 dm_name[0], dm_name[1], dm_name[2]);
396 for (
i = 0;
i < p_info->rank;
i++) {
397 s2u(dm_name[
i], buf);
398 status = nc_inq_dimid(ds_id.
fid, buf, &dimids[
i]);
400 printf(
"-E- %s %d: %s for %s\n",
401 __FILE__, __LINE__, nc_strerror(
status), dm_name[
i]);
406 if (strcmp(p_info->dataType,
"byte") == 0)
408 else if (strcmp(p_info->dataType,
"ubyte") == 0)
410 else if (strcmp(p_info->dataType,
"short") == 0)
412 else if (strcmp(p_info->dataType,
"ushort") == 0)
414 else if (strcmp(p_info->dataType,
"int") == 0)
416 else if (strcmp(p_info->dataType,
"uint") == 0)
418 else if (strcmp(p_info->dataType,
"float") == 0)
420 else if (strcmp(p_info->dataType,
"double") == 0)
423 printf(
"-E- %s %d datatype %s not defined\n", __FILE__, __LINE__, p_info->dataType);
427 status =
CreateNCDF(ds_id, sname, p_info->description, p_info->standardName,
428 p_info->reference, p_info->comment, p_info->units, p_info->validMin,
429 p_info->validMax, p_info->scaleFactor, p_info->addOffset,
430 p_info->fillValue, nt, p_info->rank, dimids);
432 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
441 outid = SDselect(ds_id.
fid, SDnametoindex(ds_id.
fid, l2_prod_name));
444 status = nc_inq_varid(ds_id.
fid, l2_prod_name, (int32_t *) & outid);
449 printf(
"-E- %s %d fftype %d not defined\n", __FILE__, __LINE__, ds_id.
fftype);
458 outid = SDselect(ds_id.
fid, SDnametoindex(ds_id.
fid, l2_prod_name));
464 status = nc_inq_varid(ds_id.
fid, l2_prod_name, (int32_t *) & outid);
469 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
492 size_t startp[3] = {s0, s1, s2};
493 size_t countp[3] = {e0, e1, e2};
496 printf(
"-E- %s %d: %s for %s\n",
497 __FILE__, __LINE__, nc_strerror(
status),
name);
502 printf(
"-E- %s %d: %s for %s\n",
503 __FILE__, __LINE__, nc_strerror(
status),
name);
508 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
534 printf(
"-E- %s %d: %s for %s\n",
535 __FILE__, __LINE__, nc_strerror(
status),
name);
540 printf(
"-E- %s %d: %s for %s\n",
541 __FILE__, __LINE__, nc_strerror(
status),
name);
545 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
570 hdfAccess = DFACC_RDONLY;
572 hdfAccess = DFACC_CREATE;
574 if (ds_id.
fid == -1) {
575 fprintf(
stderr,
"-E- %s line %d: SDstart failure, %s .\n",
578 }
else if (format ==
DS_NCDF) {
591 "-E- %s line %d: Could not set NCDF4 cache size for file, %s .\n",
597 "-E- %s line %d: Could not create NCDF4 file, %s .\n",
603 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
629 PTB(SDendaccess(ds_id.
sid));
636 return SDend(ds_id.
fid);
638 return nc_close(ds_id.
fid);
640 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
647 int32_t dims[H4_MAX_VAR_DIMS]) {
654 status = nc_inq_varid(ds_id.
fid, sdsname, &varid);
656 printf(
"-E- %s %d: %s for %s\n",
657 __FILE__, __LINE__, nc_strerror(
status), sdsname);
660 int dimids[H4_MAX_VAR_DIMS];
663 printf(
"-E- %s %d: %s for %s\n",
664 __FILE__, __LINE__, nc_strerror(
status), sdsname);
668 for (
i = 0;
i < ndims;
i++) {
669 status = nc_inq_dimlen(ds_id.
fid, dimids[
i], &dim);
675 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
687 status = nc_inq_varid(ds_id.
fid, sdsname, &varid);
689 printf(
"-E- %s %d: %s for %s\n",
690 __FILE__, __LINE__, nc_strerror(
status), sdsname);
694 printf(
"-E- %s %d: %s for %s\n",
695 __FILE__, __LINE__, nc_strerror(
status), sdsname);
699 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
708 status = SDfileinfo(ds_id.
fid, n_datasets, n_globalattr);
712 printf(
"-E- %s %d: %s\n", __FILE__, __LINE__, nc_strerror(
status));
715 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
721 int getProdlist(
const char *fname,
char **prodlist, int32_t *l2_flags_type) {
724 char buffer[2048 * 8];
729 printf(
"-E- File %s does not exist\n", fname);
733 if (Hishdf(fname) == 1) {
747 HDFfid = Hopen(fname, DFACC_READ, 0);
750 printf(
"\n%s not found or is corrupt.\n", fname);
754 sd_id = SDstart(fname, DFACC_RDONLY);
756 printf(
"Error opening (SDstart) %s\n", fname);
761 vg_ref = Vfind(HDFfid,
"Geophysical Data");
762 vgid = Vattach(HDFfid, vg_ref,
"r");
764 for (
i = 0;
i < Vntagrefs(vgid);
i++) {
765 Vgettagref(vgid,
i, &tag, &
ref);
766 sds_id = SDselect(sd_id, SDreftoindex(sd_id,
ref));
768 printf(
"Error accessing SDS (reference #: %d) in: %s .\n",
ref,
772 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
773 if (strcmp(buffer,
"l2_flags") != 0) {
774 listlen += strlen(buffer) + 1;
779 *
prodlist = (
char *) calloc(listlen + 1,
sizeof (
char));
782 for (
i = 0;
i < Vntagrefs(vgid);
i++) {
783 Vgettagref(vgid,
i, &tag, &
ref);
784 sds_id = SDselect(sd_id, SDreftoindex(sd_id,
ref));
785 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
786 if (strcmp(buffer,
"l2_flags") != 0) {
793 *l2_flags_type =
dtype;
797 cptr[listlen - 1] = 0;
807 int ncid, grp_ncid, nvars;
808 if (nc_open(fname, NC_NOWRITE, &ncid) == NC_NOERR) {
809 nc_inq_ncid(ncid,
"geophysical_data", &grp_ncid);
811 for (
i = 0;
i < nvars;
i++) {
812 nc_inq_varname(grp_ncid,
i, buffer);
813 if (strcmp(buffer,
"l2_flags") != 0) {
814 listlen += strlen(buffer) + 1;
818 *
prodlist = (
char *) calloc(listlen + 1,
sizeof (
char));
821 for (
i = 0;
i < nvars;
i++) {
822 nc_inq_varname(grp_ncid,
i, buffer);
823 if (strcmp(buffer,
"l2_flags") != 0) {
830 nc_inq_vartype(grp_ncid,
i, l2_flags_type);
834 printf(
"-E- Can not open NetCDF file %s\n", fname);
837 cptr[listlen - 1] = 0;
853 const char *reference,
866 int32_t nc_id = ds_id.
fid;
870 size_t chunksize[3] = {0, 0, 0};
871 char *validnames[2] = {
"valid_min",
"valid_max"};
874 status = nc_def_var(nc_id, sname, nt,
rank, dimids, &var_id);
876 printf(
"-E- %s %d: %s for %s\n",
877 __FILE__, __LINE__, nc_strerror(
status), sname);
884 status = nc_put_att_text(nc_id, var_id,
"long_name", strlen(lname), lname);
886 printf(
"-E- %s %d: %s for %s\n",
887 __FILE__, __LINE__, nc_strerror(
status),
"long_name");
892 if (nt != NC_FLOAT && nt != NC_DOUBLE) {
893 if (scale_factor != 1.0 || add_offset != 0.0) {
894 status = nc_put_att_float(nc_id, var_id,
"scale_factor", NC_FLOAT, 1, &scale_factor);
896 printf(
"-E- %s %d: %s for %s\n",
897 __FILE__, __LINE__, nc_strerror(
status),
"scale_factor");
900 status = nc_put_att_float(nc_id, var_id,
"add_offset", NC_FLOAT, 1, &add_offset);
902 printf(
"-E- %s %d: %s for %s\n",
903 __FILE__, __LINE__, nc_strerror(
status),
"add_offset");
912 && strcasecmp(
units,
"dimensionless") != 0
913 && strcasecmp(
units,
"unitless") != 0) {
916 printf(
"-E- %s %d: %s for %s\n",
917 __FILE__, __LINE__, nc_strerror(
status),
"units");
924 status = nc_put_att_text(nc_id, var_id,
"standard_name",
927 printf(
"-E- %s %d: %s for %s\n",
928 __FILE__, __LINE__, nc_strerror(
status),
"standard_name");
933 if (strstr(sname,
"flag_") == sname ||
934 strstr(sname,
"l2_flags") !=
NULL)
942 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fv_i8);
944 printf(
"-E- %s %d: %s for %s\n",
945 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
952 uint8_t fv_i8 = -128;
953 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fv_i8);
955 printf(
"-E- %s %d: %s for %s\n",
956 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
963 uint16_t fv_i16 = (uint16_t) fillValue;
964 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fv_i16);
966 printf(
"-E- %s %d: %s for %s\n",
967 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
974 int16_t fv_i16 = (int16_t) fillValue;
975 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fv_i16);
977 printf(
"-E- %s %d: %s for %s\n",
978 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
985 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fillValue);
987 printf(
"-E- %s %d: %s for %s\n",
988 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
995 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fillValue);
997 printf(
"-E- %s %d: %s for %s\n",
998 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
1005 float fv_f32 = (
float) fillValue;
1006 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fv_f32);
1007 if (
status != NC_NOERR) {
1008 printf(
"-E- %s %d: %s for %s\n",
1009 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
1015 fprintf(
stderr,
"-E- %s line %d: ", __FILE__, __LINE__);
1016 fprintf(
stderr,
"Got unsupported fill values number type (%d) ", nt);
1017 fprintf(
stderr,
"while trying to create NCDF variable, \"%s\", ", sname);
1030 vr[0] = (int8_t)
ROUND((low - add_offset) / scale_factor);
1031 vr[1] = (int8_t)
ROUND((high - add_offset) / scale_factor);
1032 for (
i = 0;
i < 2;
i++) {
1033 status = nc_put_att_schar(nc_id, var_id, validnames[
i], NC_BYTE,
1034 1, (
const signed char *) &vr[
i]);
1035 if (
status != NC_NOERR) {
1036 printf(
"-E- %s %d: %s for %s\n",
1037 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1046 vr[0] = (uint8_t)
ROUND((low - add_offset) / scale_factor);
1047 vr[1] = (uint8_t)
ROUND((high - add_offset) / scale_factor);
1048 for (
i = 0;
i < 2;
i++) {
1049 status = nc_put_att_uchar(nc_id, var_id, validnames[
i], NC_UBYTE,
1050 1, (
const unsigned char *) &vr[
i]);
1051 if (
status != NC_NOERR) {
1052 printf(
"-E- %s %d: %s for %s\n",
1053 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1062 vr[0] = (int16_t)
ROUND((low - add_offset) / scale_factor);
1063 vr[1] = (int16_t)
ROUND((high - add_offset) / scale_factor);
1064 for (
i = 0;
i < 2;
i++) {
1065 status = nc_put_att_short(nc_id, var_id, validnames[
i], NC_SHORT,
1067 if (
status != NC_NOERR) {
1068 printf(
"-E- %s %d: %s for %s\n",
1069 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1078 vr[0] = (uint16_t)
ROUND((low - add_offset) / scale_factor);
1079 vr[1] = (uint16_t)
ROUND((high - add_offset) / scale_factor);
1080 for (
i = 0;
i < 2;
i++) {
1081 status = nc_put_att_ushort(nc_id, var_id, validnames[
i], NC_USHORT,
1083 if (
status != NC_NOERR) {
1084 printf(
"-E- %s %d: %s for %s\n",
1085 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1094 vr[0] = (int32_t)
ROUND((low - add_offset) / scale_factor);
1095 vr[1] = (int32_t)
ROUND((high - add_offset) / scale_factor);
1096 for (
i = 0;
i < 2;
i++) {
1097 status = nc_put_att_int(nc_id, var_id, validnames[
i], NC_INT,
1099 if (
status != NC_NOERR) {
1100 printf(
"-E- %s %d: %s for %s\n",
1101 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1110 vr[0] = (uint32_t)
ROUND((low - add_offset) / scale_factor);
1111 vr[1] = (uint32_t)
ROUND((high - add_offset) / scale_factor);
1112 for (
i = 0;
i < 2;
i++) {
1113 status = nc_put_att_uint(nc_id, var_id, validnames[
i], NC_UINT,
1115 if (
status != NC_NOERR) {
1116 printf(
"-E- %s %d: %s for %s\n",
1117 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1126 vr[0] = (
float) low;
1127 vr[1] = (
float) high;
1128 for (
i = 0;
i < 2;
i++) {
1129 status = nc_put_att_float(nc_id, var_id, validnames[
i], NC_FLOAT,
1131 if (
status != NC_NOERR) {
1132 printf(
"-E- %s %d: %s for %s\n",
1133 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1140 fprintf(
stderr,
"-E- %s line %d: ", __FILE__, __LINE__);
1141 fprintf(
stderr,
"Got unsupported number type (%d) ", nt);
1142 fprintf(
stderr,
"while trying to create NCDF variable, \"%s\", ", sname);
1148 if (reference !=
NULL && strcmp(reference,
"") != 0) {
1149 status = nc_put_att_text(nc_id, var_id,
"reference",
1150 strlen(reference), reference);
1151 if (
status != NC_NOERR) {
1152 printf(
"-E- %s %d: %s for %s\n",
1153 __FILE__, __LINE__, nc_strerror(
status),
"reference");
1159 if (comment !=
NULL && strcmp(comment,
"") != 0) {
1160 status = nc_put_att_text(nc_id, var_id,
"comment",
1161 strlen(comment), comment);
1162 if (
status != NC_NOERR) {
1163 printf(
"-E- %s %d: %s for %s\n",
1164 __FILE__, __LINE__, nc_strerror(
status),
"comment");