-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc_ndvi.c
More file actions
57 lines (46 loc) · 1.13 KB
/
calc_ndvi.c
File metadata and controls
57 lines (46 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
main(int argc, char *argv[]){
FILE *inred, *innir, *incloud, *outfp;
unsigned char red;
unsigned char nir;
unsigned char cloud;
float ndvi;
int i,j,k,l;
if(argc == 1){
printf("\ncalc_ndvi.exe <red> <nir> <cloud> <output>\n\n");
exit(1);
}
if((inred=fopen(argv[1],"rb"))==NULL){
printf("\ninput RED file open error\n\n");
exit(0);
}
if((innir=fopen(argv[2],"rb"))==NULL){
printf("\ninput NIR file open error\n\n");
exit(0);
}
if((incloud=fopen(argv[3],"rb"))==NULL){
printf("\ninput CLOUD file open error\n\n");
exit(0);
}
if((outfp=fopen(argv[4],"wb"))==NULL){
printf("\noutput file open error\n\n");
exit(0);
}
while(fread(&red,sizeof(unsigned char),1,inred)){
fread(&nir,sizeof(unsigned char),1,innir);
fread(&cloud,sizeof(unsigned char),1,incloud);
if((red > 0) && (cloud < 2)){
ndvi = ((float)nir - (float)red)/((float)nir + (float)red);
}else{
ndvi = 0.0;
}
fwrite(&ndvi,sizeof(float),1,outfp);
}
fclose(inred);
fclose(innir);
fclose(incloud);
fclose(outfp);
} /* end main */