31 #define ROUND(x) ((((x) >= 0)?0.5:-0.5) + (x))
36 for (
i = 0;
i < l;
i++) {
47 int32_t attr_index = -1;
50 attr_index = SDfindattr(ds_id.
fid, nam);
52 attr_index = SDfindattr(ds_id.
sid, nam);
58 status = nc_inq_attid(ds_id.
fid, NC_GLOBAL, nam, (int32_t *) & attr_index);
60 status = nc_inq_attid(-ds_id.
fid, ds_id.
sid, nam, (int32_t *) & attr_index);
66 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
86 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
111 if (
dtype != DFNT_CHAR)
122 if (
dtype != DFNT_CHAR)
137 if(
dtype == NC_CHAR) {
143 }
else if(
dtype == NC_STRING) {
165 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
175 printf(
"-E- %s %d Could not allocate memory for reading attribute %s\n", __FILE__, __LINE__,
name);
194 attr_index = SDfindattr(ds_id.
fid, nam);
197 attr_index = SDfindattr(ds_id.
sid, nam);
203 status = nc_inq_att(ds_id.
fid, NC_GLOBAL, nam, (int32_t *)
dtype, &cnt);
210 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
216 int setAttr(
idDS ds_id,
const char *nam, int32_t typ, int32_t cnt,
const void*
data) {
223 printf(
"-E- %s %d: %s for %s\n",
224 __FILE__, __LINE__, nc_strerror(
status), nam);
227 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
241 printf(
"-E- %s %d: %s for %s\n",
242 __FILE__, __LINE__, nc_strerror(
status),
name);
245 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
257 NC_DOUBLE, 1, (
const double *) &
value);
259 printf(
"-E- %s %d: %s for %s\n",
260 __FILE__, __LINE__, nc_strerror(
status),
name);
263 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
275 NC_FLOAT, 1, (
const float *) &
value);
277 printf(
"-E- %s %d: %s for %s\n",
278 __FILE__, __LINE__, nc_strerror(
status),
name);
281 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
293 NC_SHORT, 1, (
const short *) &
value);
295 printf(
"-E- %s %d: %s for %s\n",
296 __FILE__, __LINE__, nc_strerror(
status),
name);
299 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
311 NC_BYTE, 1, (
const uint8 *) &
value);
313 printf(
"-E- %s %d: %s for %s\n",
314 __FILE__, __LINE__, nc_strerror(
status),
name);
317 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
329 NC_INT, 1, (
const int *) &
value);
331 printf(
"-E- %s %d: %s for %s\n",
332 __FILE__, __LINE__, nc_strerror(
status),
name);
335 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
341 int createDS(
idDS ds_id,
int sensorId,
const char *sname, int32_t dm[3],
342 const char dm_name[3][80]) {
343 static productInfo_t* p_info;
345 if (p_info ==
NULL) {
350 return createDS2(ds_id, sname, p_info, dm, dm_name);
352 printf(
"%s not found in XML product table\n", sname);
357 int createDS2(
idDS ds_id,
const char *sname, productInfo_t* p_info, int32_t dm[3],
358 const char dm_name[3][80]) {
364 if (strcmp(p_info->dataType,
"byte") == 0)
366 else if (strcmp(p_info->dataType,
"ubyte") == 0)
368 else if (strcmp(p_info->dataType,
"short") == 0)
370 else if (strcmp(p_info->dataType,
"ushort") == 0)
372 else if (strcmp(p_info->dataType,
"int") == 0)
374 else if (strcmp(p_info->dataType,
"uint") == 0)
376 else if (strcmp(p_info->dataType,
"float") == 0)
378 else if (strcmp(p_info->dataType,
"double") == 0)
381 printf(
"-E- %s %d: datatype %s is not valid\n", __FILE__, __LINE__, p_info->dataType);
386 p_info->units, p_info->validMin, p_info->validMax, p_info->scaleFactor,
387 p_info->addOffset, nt, p_info->rank, dm[0], dm[1], dm[2],
388 dm_name[0], dm_name[1], dm_name[2]);
393 for (
i = 0;
i < p_info->rank;
i++) {
394 s2u(dm_name[
i], buf);
395 status = nc_inq_dimid(ds_id.
fid, buf, &dimids[
i]);
397 printf(
"-E- %s %d: %s for %s\n",
398 __FILE__, __LINE__, nc_strerror(
status), dm_name[
i]);
403 if (strcmp(p_info->dataType,
"byte") == 0)
405 else if (strcmp(p_info->dataType,
"ubyte") == 0)
407 else if (strcmp(p_info->dataType,
"short") == 0)
409 else if (strcmp(p_info->dataType,
"ushort") == 0)
411 else if (strcmp(p_info->dataType,
"int") == 0)
413 else if (strcmp(p_info->dataType,
"uint") == 0)
415 else if (strcmp(p_info->dataType,
"float") == 0)
417 else if (strcmp(p_info->dataType,
"double") == 0)
420 printf(
"-E- %s %d datatype %s not defined\n", __FILE__, __LINE__, p_info->dataType);
424 status =
CreateNCDF(ds_id, sname, p_info->description, p_info->standardName,
425 p_info->reference, p_info->comment, p_info->units, p_info->validMin,
426 p_info->validMax, p_info->scaleFactor, p_info->addOffset,
427 p_info->fillValue, nt, p_info->rank, dimids);
429 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
438 outid = SDselect(ds_id.
fid, SDnametoindex(ds_id.
fid, l2_prod_name));
441 status = nc_inq_varid(ds_id.
fid, l2_prod_name, (int32_t *) & outid);
446 printf(
"-E- %s %d fftype %d not defined\n", __FILE__, __LINE__, ds_id.
fftype);
455 outid = SDselect(ds_id.
fid, SDnametoindex(ds_id.
fid, l2_prod_name));
461 status = nc_inq_varid(ds_id.
fid, l2_prod_name, (int32_t *) & outid);
466 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
489 size_t startp[3] = {s0, s1, s2};
490 size_t countp[3] = {e0, e1, e2};
493 printf(
"-E- %s %d: %s for %s\n",
494 __FILE__, __LINE__, nc_strerror(
status),
name);
499 printf(
"-E- %s %d: %s for %s\n",
500 __FILE__, __LINE__, nc_strerror(
status),
name);
505 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
531 printf(
"-E- %s %d: %s for %s\n",
532 __FILE__, __LINE__, nc_strerror(
status),
name);
537 printf(
"-E- %s %d: %s for %s\n",
538 __FILE__, __LINE__, nc_strerror(
status),
name);
542 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
567 hdfAccess = DFACC_RDONLY;
569 hdfAccess = DFACC_CREATE;
571 if (ds_id.
fid == -1) {
572 fprintf(
stderr,
"-E- %s line %d: SDstart failure, %s .\n",
575 }
else if (format ==
DS_NCDF) {
587 "-E- %s line %d: Could not create NCDF4 file, %s .\n",
593 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
619 PTB(SDendaccess(ds_id.
sid));
626 return SDend(ds_id.
fid);
628 return nc_close(ds_id.
fid);
630 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
637 int32_t dims[H4_MAX_VAR_DIMS]) {
644 status = nc_inq_varid(ds_id.
fid, sdsname, &varid);
646 printf(
"-E- %s %d: %s for %s\n",
647 __FILE__, __LINE__, nc_strerror(
status), sdsname);
650 int dimids[H4_MAX_VAR_DIMS];
653 printf(
"-E- %s %d: %s for %s\n",
654 __FILE__, __LINE__, nc_strerror(
status), sdsname);
658 for (
i = 0;
i < ndims;
i++) {
659 status = nc_inq_dimlen(ds_id.
fid, dimids[
i], &dim);
665 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
677 status = nc_inq_varid(ds_id.
fid, sdsname, &varid);
679 printf(
"-E- %s %d: %s for %s\n",
680 __FILE__, __LINE__, nc_strerror(
status), sdsname);
684 printf(
"-E- %s %d: %s for %s\n",
685 __FILE__, __LINE__, nc_strerror(
status), sdsname);
689 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
698 status = SDfileinfo(ds_id.
fid, n_datasets, n_globalattr);
702 printf(
"-E- %s %d: %s\n", __FILE__, __LINE__, nc_strerror(
status));
705 printf(
"-E- %s %d fftype not defined\n", __FILE__, __LINE__);
711 int getProdlist(
const char *fname,
char **prodlist, int32_t *l2_flags_type) {
714 char buffer[2048 * 8];
719 printf(
"-E- File %s does not exist\n", fname);
723 if (Hishdf(fname) == 1) {
737 HDFfid = Hopen(fname, DFACC_READ, 0);
740 printf(
"\n%s not found or is corrupt.\n", fname);
744 sd_id = SDstart(fname, DFACC_RDONLY);
746 printf(
"Error opening (SDstart) %s\n", fname);
751 vg_ref = Vfind(HDFfid,
"Geophysical Data");
752 vgid = Vattach(HDFfid, vg_ref,
"r");
754 for (
i = 0;
i < Vntagrefs(vgid);
i++) {
755 Vgettagref(vgid,
i, &tag, &
ref);
756 sds_id = SDselect(sd_id, SDreftoindex(sd_id,
ref));
758 printf(
"Error accessing SDS (reference #: %d) in: %s .\n",
ref,
762 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
763 if (strcmp(buffer,
"l2_flags") != 0) {
764 listlen += strlen(buffer) + 1;
769 *
prodlist = (
char *) calloc(listlen + 1,
sizeof (
char));
772 for (
i = 0;
i < Vntagrefs(vgid);
i++) {
773 Vgettagref(vgid,
i, &tag, &
ref);
774 sds_id = SDselect(sd_id, SDreftoindex(sd_id,
ref));
775 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
776 if (strcmp(buffer,
"l2_flags") != 0) {
783 *l2_flags_type =
dtype;
787 cptr[listlen - 1] = 0;
797 int ncid, grp_ncid, nvars;
798 if (nc_open(fname, NC_NOWRITE, &ncid) == NC_NOERR) {
799 nc_inq_ncid(ncid,
"geophysical_data", &grp_ncid);
801 for (
i = 0;
i < nvars;
i++) {
802 nc_inq_varname(grp_ncid,
i, buffer);
803 if (strcmp(buffer,
"l2_flags") != 0) {
804 listlen += strlen(buffer) + 1;
808 *
prodlist = (
char *) calloc(listlen + 1,
sizeof (
char));
811 for (
i = 0;
i < nvars;
i++) {
812 nc_inq_varname(grp_ncid,
i, buffer);
813 if (strcmp(buffer,
"l2_flags") != 0) {
820 nc_inq_vartype(grp_ncid,
i, l2_flags_type);
824 printf(
"-E- Can not open NetCDF file %s\n", fname);
827 cptr[listlen - 1] = 0;
843 const char *reference,
856 int32_t nc_id = ds_id.
fid;
860 size_t chunksize[3] = {0, 0, 0};
861 char *validnames[2] = {
"valid_min",
"valid_max"};
864 status = nc_def_var(nc_id, sname, nt,
rank, dimids, &var_id);
866 printf(
"-E- %s %d: %s for %s\n",
867 __FILE__, __LINE__, nc_strerror(
status), sname);
874 status = nc_put_att_text(nc_id, var_id,
"long_name", strlen(lname), lname);
876 printf(
"-E- %s %d: %s for %s\n",
877 __FILE__, __LINE__, nc_strerror(
status),
"long_name");
882 if (nt != NC_FLOAT && nt != NC_DOUBLE) {
883 if (scale_factor != 1.0 || add_offset != 0.0) {
884 status = nc_put_att_float(nc_id, var_id,
"scale_factor", NC_FLOAT, 1, &scale_factor);
886 printf(
"-E- %s %d: %s for %s\n",
887 __FILE__, __LINE__, nc_strerror(
status),
"scale_factor");
890 status = nc_put_att_float(nc_id, var_id,
"add_offset", NC_FLOAT, 1, &add_offset);
892 printf(
"-E- %s %d: %s for %s\n",
893 __FILE__, __LINE__, nc_strerror(
status),
"add_offset");
902 && strcasecmp(
units,
"dimensionless") != 0
903 && strcasecmp(
units,
"unitless") != 0) {
906 printf(
"-E- %s %d: %s for %s\n",
907 __FILE__, __LINE__, nc_strerror(
status),
"units");
913 if (
rank > 1 && strcasecmp(sname,
"longitude") != 0 && strcasecmp(sname,
"latitude") != 0) {
914 status = nc_put_att_text(nc_id, var_id,
"coordinates", 18,
"longitude latitude");
916 printf(
"-E- %s %d: %s for %s\n",
917 __FILE__, __LINE__, nc_strerror(
status),
"coordinates");
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");
1016 double fv_f64 = (
double) fillValue;
1017 status = nc_put_att(nc_id, var_id,
"_FillValue", nt, 1, &fv_f64);
1018 if (
status != NC_NOERR) {
1019 printf(
"-E- %s %d: %s for %s\n",
1020 __FILE__, __LINE__, nc_strerror(
status),
"_FillValue");
1026 fprintf(
stderr,
"-E- %s line %d: ", __FILE__, __LINE__);
1027 fprintf(
stderr,
"Got unsupported fill values number type (%d) ", nt);
1028 fprintf(
stderr,
"while trying to create NCDF variable, \"%s\", ", sname);
1042 vr[0] = (int8_t)
ROUND((low - add_offset) / scale_factor);
1043 vr[1] = (int8_t)
ROUND((high - add_offset) / scale_factor);
1044 for (
i = 0;
i < 2;
i++) {
1045 status = nc_put_att_schar(nc_id, var_id, validnames[
i], NC_BYTE,
1046 1, (
const signed char *) &vr[
i]);
1047 if (
status != NC_NOERR) {
1048 printf(
"-E- %s %d: %s for %s\n",
1049 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1058 vr[0] = (uint8_t)
ROUND((low - add_offset) / scale_factor);
1059 vr[1] = (uint8_t)
ROUND((high - add_offset) / scale_factor);
1060 for (
i = 0;
i < 2;
i++) {
1061 status = nc_put_att_uchar(nc_id, var_id, validnames[
i], NC_UBYTE,
1062 1, (
const unsigned char *) &vr[
i]);
1063 if (
status != NC_NOERR) {
1064 printf(
"-E- %s %d: %s for %s\n",
1065 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1074 vr[0] = (int16_t)
ROUND((low - add_offset) / scale_factor);
1075 vr[1] = (int16_t)
ROUND((high - add_offset) / scale_factor);
1076 for (
i = 0;
i < 2;
i++) {
1077 status = nc_put_att_short(nc_id, var_id, validnames[
i], NC_SHORT,
1079 if (
status != NC_NOERR) {
1080 printf(
"-E- %s %d: %s for %s\n",
1081 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1090 vr[0] = (uint16_t)
ROUND((low - add_offset) / scale_factor);
1091 vr[1] = (uint16_t)
ROUND((high - add_offset) / scale_factor);
1092 for (
i = 0;
i < 2;
i++) {
1093 status = nc_put_att_ushort(nc_id, var_id, validnames[
i], NC_USHORT,
1095 if (
status != NC_NOERR) {
1096 printf(
"-E- %s %d: %s for %s\n",
1097 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1106 vr[0] = (int32_t)
ROUND((low - add_offset) / scale_factor);
1107 vr[1] = (int32_t)
ROUND((high - add_offset) / scale_factor);
1108 for (
i = 0;
i < 2;
i++) {
1109 status = nc_put_att_int(nc_id, var_id, validnames[
i], NC_INT,
1111 if (
status != NC_NOERR) {
1112 printf(
"-E- %s %d: %s for %s\n",
1113 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1122 vr[0] = (uint32_t)
ROUND((low - add_offset) / scale_factor);
1123 vr[1] = (uint32_t)
ROUND((high - add_offset) / scale_factor);
1124 for (
i = 0;
i < 2;
i++) {
1125 status = nc_put_att_uint(nc_id, var_id, validnames[
i], NC_UINT,
1127 if (
status != NC_NOERR) {
1128 printf(
"-E- %s %d: %s for %s\n",
1129 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1138 vr[0] = (
float) low;
1139 vr[1] = (
float) high;
1140 for (
i = 0;
i < 2;
i++) {
1141 status = nc_put_att_float(nc_id, var_id, validnames[
i], NC_FLOAT,
1143 if (
status != NC_NOERR) {
1144 printf(
"-E- %s %d: %s for %s\n",
1145 __FILE__, __LINE__, nc_strerror(
status), validnames[
i]);
1152 fprintf(
stderr,
"-E- %s line %d: ", __FILE__, __LINE__);
1153 fprintf(
stderr,
"Got unsupported number type (%d) ", nt);
1154 fprintf(
stderr,
"while trying to create NCDF variable, \"%s\", ", sname);
1160 if (reference !=
NULL && strcmp(reference,
"") != 0) {
1161 status = nc_put_att_text(nc_id, var_id,
"reference",
1162 strlen(reference), reference);
1163 if (
status != NC_NOERR) {
1164 printf(
"-E- %s %d: %s for %s\n",
1165 __FILE__, __LINE__, nc_strerror(
status),
"reference");
1171 if (comment !=
NULL && strcmp(comment,
"") != 0) {
1172 status = nc_put_att_text(nc_id, var_id,
"comment",
1173 strlen(comment), comment);
1174 if (
status != NC_NOERR) {
1175 printf(
"-E- %s %d: %s for %s\n",
1176 __FILE__, __LINE__, nc_strerror(
status),
"comment");