17 int main(
int argc,
char *argv[]) {
28 static char buffer[2048];
29 static char attr_buf[2048];
37 unsigned char *n_water_pix;
54 int32
start[2] = {0, 0};
55 int32
edges[2] = {0, 0};
56 int32 mask_row_off = 0;
57 int32 mask_col_off = 0;
58 int32 sub_scan[2] = {0, 0};
59 int32 sub_pixl[2] = {0, 0};
60 int32 water_min = 1000;
62 int16 fillvalue = -32767;
63 int16 water_val = -16384;
64 int16 eval_flds_flag=0;
71 float32 NIRreldiff, NIRness;
72 float64 val1, val2, sum, weigh;
89 int32 eval_flds_index[NEVALFLD];
100 printf(
"landtimebin V2.31 (02/12/02)\n");
103 printf(
"landtimebin <File containing list of input landbin files> <Output landbin file> <NDVI/BLUE/HYBRID>");
110 fp = fopen(argv[1],
"r");
112 printf(
"Input listing file: \"%s\" not found.\n", argv[1]);
115 while (fgets(buffer, 256, fp) !=
NULL) nfiles++;
117 printf(
"%d input files\n", nfiles);
122 HDFfid_r = (int32 *) calloc(nfiles,
sizeof (int32));
123 sd_id_r = (int32 *) calloc(nfiles,
sizeof (int32));
125 fp = fopen(argv[1],
"r");
126 for (
i = 0;
i < nfiles;
i++) {
127 fgets(buffer, 256, fp);
128 buffer[strlen(buffer) - 1] = 0;
129 HDFfid_r[
i] = Hopen(buffer, DFACC_READ, 0);
130 if (HDFfid_r[
i] == -1) {
131 printf(
"Input file: \"%s\" not found.\n", buffer);
135 sd_id_r[
i] = SDstart(buffer, DFACC_RDONLY);
137 j = SDfindattr(sd_id_r[
i],
"Subset Scan Range");
138 if (
j != -1) SDreadattr(sd_id_r[
i],
j, (VOIDP) & sub_scan);
140 j = SDfindattr(sd_id_r[
i],
"Subset Pixel Range");
141 if (
j != -1) SDreadattr(sd_id_r[
i],
j, (VOIDP) & sub_pixl);
148 HDFfid_w = Hopen(argv[2], DFACC_CREATE, 0);
150 sd_id_w = SDstart(argv[2], DFACC_RDWR);
155 if (argc == 3 || (strcmp(argv[3],
"NDVI") == 0))
157 else if (strcmp(argv[3],
"BLUE") == 0)
159 else if (strcmp(argv[3],
"HYBRID") == 0)
162 printf(
"eval_flds_flag: %d\n", eval_flds_flag);
167 sds_id = SDselect(sd_id_r[0], 0);
168 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
172 data = (
char *) calloc(nfiles * ncol * 2,
sizeof (
char));
173 data_w = (
char *) calloc(ncol * 2,
sizeof (
char));
174 eval_flds = (
int16 *) calloc(nfiles * ncol * FILE_BSIZ,
sizeof (
int16));
176 n_water_pix = (
unsigned char *) calloc(8640 * FIELD_BSIZ,
sizeof (
unsigned char));
178 printf(
"%d\n", argc);
180 water_min = atol(argv[4]);
182 printf(
"water_min: %4d\n", water_min);
184 fileindex[0] = (
int16 *) calloc(ncol*FIELD_BSIZ,
sizeof (
int16));
185 fileindex[1] = (
int16 *) calloc(ncol*FIELD_BSIZ,
sizeof (
int16));
186 fileindex[2] = (
int16 *) calloc(ncol*FIELD_BSIZ,
sizeof (
int16));
191 SDfileinfo(sd_id_r[0], &ndatasets, &nglobal_attr);
196 for (
j = 0;
j < ndatasets;
j++) {
197 sds_id = SDselect(sd_id_r[0],
j);
198 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
199 sds_id_w = SDcreate(sd_id_w, buffer,
dtype,
rank, dims);
204 if (strcmp(buffer,
"NDVI") == 0) eval_flds_index[0] =
j;
205 if (strcmp(buffer,
"refl_443") == 0) eval_flds_index[1] =
j;
206 if (strcmp(buffer,
"refl_670") == 0) eval_flds_index[2] =
j;
207 if (strcmp(buffer,
"refl_865") == 0) eval_flds_index[3] =
j;
213 for (
i = 0;
i < nattrs;
i++) {
214 SDreadattr(sds_id,
i, (VOIDP) attr_buf);
216 SDsetattr(sds_id_w, buffer,
dtype,
count, (VOIDP) attr_buf);
223 dim_id_r = SDgetdimid(sds_id,
i);
224 dim_id_w = SDgetdimid(sds_id_w,
i);
225 SDdiminfo(dim_id_r, buffer, &
count, &
dtype, &nattrs);
226 SDsetdimname(dim_id_w, buffer);
229 SDendaccess(sds_id_w);
236 i = SDfindattr(sd_id_r[0],
"Projection");
238 SDreadattr(sd_id_r[0],
i, (VOIDP) attr_buf);
240 tmp_str = getenv(
"OCDATAROOT");
241 if (tmp_str == 0x0) {
242 printf(
"Environment variable OCDATAROOT not defined.\n");
247 if (strcmp(attr_buf,
"Plate Carree") == 0) {
248 strcat(buffer,
"/common/water_pix_4km_platte_carre.dat");
249 mask_4km = fopen(buffer,
"rb");
250 if (mask_4km == 0x0) {
251 printf(
"Water mask file: %s not found.\n", buffer);
256 if (strcmp(attr_buf,
"Sinusoidal") == 0) {
257 strcat(buffer,
"/common/water_pix_4km_sinusoidal.dat");
258 mask_4km = fopen(buffer,
"rb");
259 if (mask_4km == 0x0) {
260 printf(
"Water mask file: %s not found.\n", buffer);
266 i = SDfindattr(sd_id_r[0],
"Command line");
268 SDreadattr(sd_id_r[0],
i, (VOIDP) attr_buf);
269 str_ptr = strstr(attr_buf,
"-box");
270 if (str_ptr != 0x0) {
272 comma = strstr(str_ptr,
",");
274 mask_col_off = atoi(str_ptr);
276 comma = strstr(str_ptr,
",");
278 mask_row_off = atoi(str_ptr);
289 if ((
scan % 200) == 0) {
291 tmnow = localtime(&tnow);
292 printf(
"Scan:%6d %s",
scan, asctime(tmnow));
307 for (
i = 0;
i < nfiles;
i++) {
308 for (
j = 0;
j < NEVALFLD;
j++) {
309 sds_id = SDselect(sd_id_r[
i], eval_flds_index[
j]);
310 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
313 (VOIDP) & eval_flds[(FILE_BSIZ *
i + FIELD_BSIZ *
j +
offset) * ncol]);
321 fseek(mask_4km, (mask_row_off +
start[0]) * 8640, SEEK_SET);
322 fread(&n_water_pix[8640 *
offset],
edges[0] * 8640, 1, mask_4km);
328 for (
j = 0;
j < ncol;
j++) {
330 fileindex[minBLUE][ncol *
k +
j] = 0;
338 for (
i = 1;
i < nfiles;
i++) {
339 for (
j = 0;
j < ncol;
j++) {
340 f_index = fileindex[
maxNDVI][ncol *
k +
j];
341 if (eval_flds[ncol * (FILE_BSIZ *
i +
NDVI +
k) +
j] >
342 eval_flds[ncol * (FILE_BSIZ * f_index +
NDVI +
k) +
j])
351 for (
i = 1;
i < nfiles;
i++) {
352 for (
j = 0;
j < ncol;
j++) {
353 f_index = fileindex[minBLUE][ncol *
k +
j];
355 if (eval_flds[ncol * (FILE_BSIZ * f_index +
BLUE +
k) +
j] < 0 &&
356 eval_flds[ncol * (FILE_BSIZ *
i +
BLUE +
k) +
j] >= 0)
357 fileindex[minBLUE][ncol *
k +
j] =
i;
359 if (eval_flds[ncol * (FILE_BSIZ * f_index +
BLUE +
k) +
j] >= 0 &&
360 eval_flds[ncol * (FILE_BSIZ *
i +
BLUE +
k) +
j] >= 0 &&
361 eval_flds[ncol * (FILE_BSIZ *
i +
RED +
k) +
j] >= 0 &&
362 (eval_flds[ncol * (FILE_BSIZ *
i +
BLUE +
k) +
j] <
363 eval_flds[ncol * (FILE_BSIZ * f_index +
BLUE +
k) +
j]))
364 fileindex[minBLUE][ncol *
k +
j] =
i;
366 if (eval_flds[ncol * (FILE_BSIZ * f_index +
RED +
k) +
j] < 0 &&
367 eval_flds[ncol * (FILE_BSIZ *
i +
RED +
k) +
j] < 0 &&
368 (eval_flds[ncol * (FILE_BSIZ *
i +
RED +
k) +
j] >
369 eval_flds[ncol * (FILE_BSIZ * f_index +
RED +
k) +
j]))
370 fileindex[minBLUE][ncol *
k +
j] =
i;
380 for (
j = 0;
j < ncol;
j++) {
382 f_index = fileindex[
maxNDVI][ncol *
k +
j];
383 if (eval_flds[ncol * (FILE_BSIZ * f_index +
NDVI +
k) +
j] == -32767)
384 fileindex[
maxNDVI][ncol *
k +
j] = fillvalue;
386 f_index = fileindex[minBLUE][ncol *
k +
j];
387 if (eval_flds[ncol * (FILE_BSIZ * f_index +
BLUE +
k) +
j] < 0)
388 fileindex[minBLUE][ncol *
k +
j] = fillvalue;
394 if (eval_flds_flag != 2)
goto DATA_WRITE;
399 for (
j = 0;
j < ncol;
j++) {
402 f_blue = fileindex[minBLUE][ncol *
RADIUS +
j];
404 fileindex[2][ncol *
RADIUS +
j] = f_blue;
406 if (f_blue != fillvalue &&
407 f_ndvi != fillvalue &&
408 n_water_pix[(8640 *
offset)+(mask_col_off +
j)] == 0 &&
409 eval_flds[ncol * (FILE_BSIZ * f_ndvi +
NIR +
RADIUS) +
j] != fillvalue &&
410 eval_flds[ncol * (FILE_BSIZ * f_blue +
NIR +
RADIUS) +
j] != fillvalue &&
411 eval_flds[ncol * (FILE_BSIZ * f_ndvi +
BLUE +
RADIUS) +
j] != fillvalue) {
413 if (eval_flds[ncol * (FILE_BSIZ * f_blue +
NIR +
RADIUS) +
j] != 0) {
414 NIRreldiff = ((eval_flds[ncol * (FILE_BSIZ * f_ndvi +
NIR +
RADIUS) +
j] -
415 eval_flds[ncol * (FILE_BSIZ * f_blue +
NIR +
RADIUS) +
j]) /
416 abs(eval_flds[ncol * (FILE_BSIZ * f_blue +
NIR +
RADIUS) +
j]));
421 if (eval_flds[ncol * (FILE_BSIZ * f_blue +
BLUE +
RADIUS) +
j] != 0 &&
422 eval_flds[ncol * (FILE_BSIZ * f_ndvi +
BLUE +
RADIUS) +
j] != 0) {
424 if (NIRreldiff > 0.10) {
429 for (irow = 0; irow < 2 *
RADIUS + 1; irow++) {
432 if (icol < 0 || icol > ncol)
break;
437 f_ndvi = fileindex[
maxNDVI][ncol * irow + icol];
438 f_blue = fileindex[minBLUE][ncol * irow + icol];
440 if (f_ndvi != fillvalue)
441 v1 = weigh * eval_flds[ncol * (FILE_BSIZ * f_ndvi +
NIR + irow) + icol];
445 if (f_blue != fillvalue)
446 v2 = weigh * eval_flds[ncol * (FILE_BSIZ * f_blue +
NIR + irow) + icol];
450 if (v1 < 0.0) v1 = 0.0;
451 if (
v2 < 0.0)
v2 = 0.0;
466 f_blue = fileindex[minBLUE][ncol *
RADIUS +
j];
468 NIRness = (eval_flds[ncol * (FILE_BSIZ * f_ndvi +
NIR +
RADIUS) +
j] -
469 eval_flds[ncol * (FILE_BSIZ * f_ndvi +
BLUE +
RADIUS) +
j]) /
472 if (
fabs(NIRness) > 0.2 &&
473 eval_flds[ncol * (FILE_BSIZ * f_ndvi +
BLUE +
RADIUS) +
j] < 2500 &&
474 fabs(val1 - eval_flds[ncol * (FILE_BSIZ * f_ndvi +
NIR +
RADIUS) +
j]) <=
475 fabs(val2 - eval_flds[ncol * (FILE_BSIZ * f_blue +
NIR +
RADIUS) +
j]) &&
477 (eval_flds[ncol * (FILE_BSIZ * f_ndvi +
NDVI +
RADIUS) +
j] < 3000 ||
478 eval_flds[ncol * (FILE_BSIZ * f_ndvi +
BLUE +
RADIUS) +
j] < 500 ||
479 eval_flds[ncol * (FILE_BSIZ * f_blue +
NIR +
RADIUS) +
j] == 0)) {
480 fileindex[2][ncol *
RADIUS +
j] = f_ndvi;
498 for (
j = 0;
j < ndatasets;
j++) {
499 sds_id = SDselect(sd_id_r[0],
j);
500 SDgetinfo(sds_id, buffer, &
rank, dims, &
dtype, &nattrs);
503 if (strcmp(buffer,
"input_file") == 0) {
504 memcpy(&data_w[0], &fileindex[eval_flds_flag][ncol *
RADIUS], 2 * ncol);
508 for (
i = 0;
i < nfiles;
i++) {
509 sds_id = SDselect(sd_id_r[
i],
j);
512 (VOIDP) &
data[2 *
i * ncol]);
518 for (
i = 0;
i < ncol;
i++) {
519 if (fileindex[eval_flds_flag][ncol *
RADIUS +
i] == fillvalue) {
520 memcpy(&data_w[2 *
i], &fillvalue, 2);
524 else if (n_water_pix[(8640 *
offset)+(mask_col_off +
i)] >= water_min) {
525 memcpy(&data_w[2 *
i], &water_val, 2);
527 memcpy(&data_w[2 *
i], &
data[2 * (fileindex[eval_flds_flag][ncol *
RADIUS +
i] * ncol +
i)], 2);
533 sds_id_w = SDselect(sd_id_w,
j);
535 SDendaccess(sds_id_w);
541 for (
k = 1;
k < FIELD_BSIZ;
k++) {
542 for (
i = 0;
i < nfiles;
i++) {
543 memcpy(&eval_flds[ncol * (FILE_BSIZ *
i +
NDVI + (
k - 1))],
544 &eval_flds[ncol * (FILE_BSIZ *
i +
NDVI +
k)],
545 ncol *
sizeof (
int16));
547 memcpy(&eval_flds[ncol * (FILE_BSIZ *
i +
BLUE + (
k - 1))],
548 &eval_flds[ncol * (FILE_BSIZ *
i +
BLUE +
k)],
549 ncol *
sizeof (
int16));
551 memcpy(&eval_flds[ncol * (FILE_BSIZ *
i +
NIR + (
k - 1))],
552 &eval_flds[ncol * (FILE_BSIZ *
i +
NIR +
k)],
553 ncol *
sizeof (
int16));
556 memcpy(&fileindex[
maxNDVI][ncol * (
k - 1)],
558 ncol *
sizeof (
int16));
560 memcpy(&fileindex[minBLUE][ncol * (
k - 1)],
561 &fileindex[minBLUE][ncol *
k],
562 ncol *
sizeof (
int16));
564 memcpy(&n_water_pix[8640 * (
k - 1)],
565 &n_water_pix[8640 *
k],
566 8640 *
sizeof (
int16));
577 i = SDfindattr(sd_id_r[0],
"Projection");
579 SDreadattr(sd_id_r[0],
i, (VOIDP) attr_buf);
580 SDsetattr(sd_id_w, buffer,
dtype,
count, (VOIDP) attr_buf);
582 i = SDfindattr(sd_id_r[0],
"Pixel size (meters)");
584 SDreadattr(sd_id_r[0],
i, (VOIDP) attr_buf);
585 SDsetattr(sd_id_w, buffer,
dtype,
count, (VOIDP) attr_buf);
587 i = SDfindattr(sd_id_r[0],
"Compositing criterion");
589 SDreadattr(sd_id_r[0],
i, (VOIDP) attr_buf);
590 SDsetattr(sd_id_w, buffer,
dtype,
count, (VOIDP) attr_buf);
592 if ((sub_scan[0] != 0) || (sub_scan[1] != 0) ||
593 (sub_pixl[0] != 0) || (sub_pixl[1] != 0)) {
594 SDsetattr(sd_id_w,
"Subset Scan Range",
DFNT_INT32, 2, sub_scan);
595 SDsetattr(sd_id_w,
"Subset Pixel Range",
DFNT_INT32, 2, &sub_pixl);
598 fp = fopen(argv[1],
"r");
600 while (fgets(buffer, 256, fp) !=
NULL) {
601 buffer[strlen(buffer) - 1] = 0;
602 strcat(buffer,
"\n,");
603 strcat(attr_buf, buffer);
606 attr_buf[strlen(attr_buf) - 1] = 0;
607 SDsetattr(sd_id_w,
"File list", DFNT_CHAR8, (
int) strlen(attr_buf) + 1, attr_buf);
613 for (
i = 0;
i < nfiles;
i++) {
644 return 2 / 3. +
x *
x *
x / 2. -
x *
x;
646 return (2 -
x) * (2 -
x) * (2 -
x) / 6;