OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
hybrid.c
Go to the documentation of this file.
1 
2 /*
3  cc -64 -fullwarn -O2 hybrid.c -o hybrid -I$HDFINC -L$HDFLIB -lmfhdf -ldf -lz
4  cc -64 -fullwarn -O2 hybrid.c -o /land2/jack/TEST/hybrid -I$HDFINC
5 -L$HDFLIB -lmfhdf -ldf -lz
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include "mfhdf.h"
12 
13 #define MAXNAMELENGTH 200
14 #define RADIUS 2
15 #define SDSNAME1 { "refl_443", "refl_555", "refl_670", "refl_865", "maximum_NDVI", "EVI", "input_file" }
16 #define SDSNAME2 { "minimum_refl_443", "refl_555", "refl_670", "refl_865", "NDVI", "EVI", "input_file" }
17 #define SDSNAME3 { "refl_443", "refl_555", "refl_670", "refl_865", "NDVI", "EVI", "input_file" }
18 
19 enum {
21 };
22 
23 enum {
25 };
26 
27 typedef struct {
29  int32 file_id, id, index, num_type, rank, n_attr, Nl, Np, *plane, Nplanes;
30  int32 start[H4_MAX_VAR_DIMS], edges[MAX_VAR_DIMS], dim_sizes[MAX_VAR_DIMS];
31  void *data, *fillvalue;
32 } SDS;
33 
34 double R3(double x);
35 
36 void main(int argc, char *argv[]) {
38  SDS sds[Nfiles][Nbands];
39  char *sdsname1[Nbands] = SDSNAME1;
40  char *sdsname2[Nbands] = SDSNAME2;
41  char *sdsname3[Nbands] = SDSNAME3;
42  char openmode[Nfiles];
43  int k, j, ib, irow, jcol, idx, source;
44  float NIRreldiff, NIRness;
45  int npix, nexam, nrepl;
46  int Nrows, Ncols;
47  double weigh, sum, val1, val2;
48 
49  if (argc < 4) {
50  fprintf(stderr, "Usage: hybrid <max NDVI file> <min Blue file> <hybrid file>\n");
51  exit(-1);
52  }
53 
54  for (j = 0; j < Nfiles; j++)
55  strcpy(filename[j], argv[j + 1]);
56 
57  for (ib = 0; ib < Nbands; ib++) {
58  strcpy(sds[maxNDVI][ib].name, sdsname1[ib]);
59  strcpy(sds[minBlue][ib].name, sdsname2[ib]);
60  strcpy(sds[Hybrid][ib].name, sdsname3[ib]);
61  }
62 
63  openmode[maxNDVI] = openmode[minBlue] = DFACC_RDONLY;
64  openmode[Hybrid] = DFACC_CREATE;
65 
66  for (j = 0; j < Nfiles; j++) {
67  if ((sds[j][0].file_id = SDstart(filename[j], openmode[j])) == -1) {
68  fprintf(stderr, "Cannot open file %s\n", filename[j]);
69  exit(-1);
70  }
71  for (ib = 0; ib < Nbands; ib++)
72  sds[j][ib].file_id = sds[j][0].file_id;
73  printf("File %s:\n", filename[j]);
74  if (j == maxNDVI || j == minBlue)
75  for (ib = 0; ib < Nbands; ib++) {
76  if ((sds[j][ib].index = SDnametoindex(sds[j][ib].file_id,
77  sds[j][ib].name)) == -1) {
78  fprintf(stderr, "Cannot locate SDS %s in file %s\n", sds[j][ib].name,
79  filename[j]);
80  exit(-1);
81  }
82  if ((sds[j][ib].id = SDselect(sds[j][ib].file_id, sds[j][ib].index)) ==
83  -1) {
84  fprintf(stderr, "Cannot select SDS %s in file %s\n", sds[j][ib].name,
85  filename[j]);
86  exit(-1);
87  }
88  if (SDgetinfo(sds[j][ib].id, sds[j][ib].name, &sds[j][ib].rank,
89  sds[j][ib].dim_sizes, &sds[j][ib].num_type, &sds[j][ib].n_attr) == -1) {
90  fprintf(stderr, "Cannot get info from SDS %s in file %s\n",
91  sds[j][ib].name, filename[j]);
92  exit(-1);
93  }
94  } else
95  for (ib = 0; ib < Nbands; ib++) {
96  sds[j][ib].num_type = sds[maxNDVI][ib].num_type;
97  sds[j][ib].rank = 2;
98  sds[j][ib].dim_sizes[0] = sds[maxNDVI][ib].dim_sizes[0];
99  sds[j][ib].dim_sizes[1] = sds[maxNDVI][ib].dim_sizes[1];
100  if ((sds[j][ib].id = SDcreate(sds[j][ib].file_id, sds[j][ib].name,
101  sds[j][ib].num_type, sds[j][ib].rank, sds[j][ib].dim_sizes)) == -1) {
102  fprintf(stderr, "Cannot create SDS %s in file %s\n", sds[j][ib].name,
103  filename[j]);
104  exit(-1);
105  }
106  }
107  for (ib = 0; ib < Nbands; ib++) {
108  sds[j][ib].Nl = sds[j][ib].dim_sizes[0];
109  sds[j][ib].Np = sds[j][ib].dim_sizes[1];
110  printf(" SDS %d: \"%s\" %dx%d\n", ib + 1, sds[j][ib].name, sds[j][ib].Np,
111  sds[j][ib].Nl);
112  sds[j][ib].data = malloc((2 * RADIUS + 1) * sds[j][ib].Np *
113  DFKNTsize(sds[j][ib].num_type));
114  }
115  }
116 
117  for (j = 0; j < Nfiles; j++)
118  for (ib = 0; ib < Nbands; ib++) {
119  sds[j][ib].start[1] = 0;
120  sds[j][ib].edges[0] = 1;
121  sds[j][ib].edges[1] = sds[j][ib].Np;
122  }
123 
124  npix = nexam = nrepl = 0;
125  Nrows = sds[maxNDVI][0].Nl;
126  Ncols = sds[maxNDVI][0].Np;
127 
128  for (irow = 0; irow < Nrows; irow++) {
129 
130  if (irow % 100 == 0)
131  printf("row %d\n", irow);
132 
133  for (j = 0; j < Nfiles; j++)
134  for (ib = 0; ib < Nbands; ib++) {
135  sds[j][ib].start[0] = MAX(0, irow - RADIUS);
136  sds[j][ib].edges[0] = MIN(Nrows - sds[j][ib].start[0], irow + RADIUS -
137  sds[j][ib].start[0] + 1);
138  if (SDreaddata(sds[j][ib].id, sds[j][ib].start, NULL, sds[j][ib].edges,
139  (char *) sds[j][ib].data + (2 * RADIUS + 1 - sds[j][ib].edges[0]) * Ncols *
140  DFKNTsize(sds[j][ib].num_type)) == -1) {
141  printf(" Can't read SDS \"%s\"\n", sds[j][ib].name);
142  exit(-1);
143  }
144  }
145 
146  for (jcol = 0; jcol < Ncols; jcol++) {
147 
148  idx = RADIUS * Ncols + jcol;
149 
150  if (((int16 *) sds[minBlue][NIR].data)[idx] != 0)
151  NIRreldiff = (((int16 *) sds[maxNDVI][NIR].data)[idx] - ((int16
152  *) sds[minBlue][NIR].data)[idx]) / fabs(((int16 *) sds[minBlue][NIR].data)[idx])
153  ;
154  else
155  NIRreldiff = 1;
156 
157  source = minBlue;
158  if (((int16 *) sds[minBlue][BLUE].data)[idx] != 0 && /* Not water */
159  ((int16 *) sds[maxNDVI][BLUE].data)[idx] != 0) { /* Not fill value */
160  npix++;
161  if (NIRreldiff > 0.10) { /* Signed difference */
162  nexam++;
163  val1 = 0;
164  val2 = 0;
165  sum = 0;
166  for (k = -RADIUS; k <= RADIUS; k++)
167  if (irow + k >= 0 &&
168  irow + k <= Nrows - 1)
169  for (j = -RADIUS; j <= RADIUS; j++)
170  if (jcol + j >= 0 &&
171  jcol + j <= Ncols - 1) {
172  weigh = R3(fabs(0.5 * k)) * R3(fabs(0.5 * j));
173  sum += weigh;
174  val1 += weigh * ((int16 *) sds[maxNDVI][NIR].data)[idx + k *
175  Ncols + j];
176  val2 += weigh * ((int16 *) sds[minBlue][NIR].data)[idx + k *
177  Ncols + j];
178  }
179  if (sum > 0) {
180  val1 /= sum;
181  val2 /= sum;
182  }
183  NIRness = (((int16 *) sds[maxNDVI][NIR].data)[idx] - ((int16
184  *) sds[maxNDVI][BLUE].data)[idx]) / fabs(((int16 *) sds[maxNDVI][BLUE].data)[idx
185  ]);
186  if (fabs(NIRness) > 0.2 && /* Too much spectral variation for a
187 cloud */
188  ((int16 *) sds[maxNDVI][BLUE].data)[idx] < 2500 && /* Not bright
189 enough for most clouds, but enough for most deserts (would be a better test at
190 412nm) */
191  abs(val1 - ((int16 *) sds[maxNDVI][NIR].data)[idx]) <= abs(val2 -
192  ((int16 *) sds[minBlue][NIR].data)[idx]) &&
193  (((int16 *) sds[maxNDVI][NDVI].data)[idx] < 3000 || /* Either
194 not vegetation */
195  ((int16 *) sds[maxNDVI][BLUE].data)[idx] < 500 || /* Or blue is
196 dark enough (filters out smoke over dense vegetation) */
197  ((int16 *) sds[minBlue][NIR].data)[idx] == 0)) {
198  /*
199  printf("irow %d jcol %d NIRreldiff %g NIRness %g\n", irow, jcol,
200  NIRreldiff, NIRness);
201  */
202  source = maxNDVI;
203  nrepl++;
204  }
205  }
206  }
207 
208  for (ib = 0; ib < Nbands; ib++)
209  switch (sds[Hybrid][ib].num_type) {
210  case DFNT_UINT8: ((uint8 *) sds[Hybrid][ib].data)[idx] = ((uint8
211  *) sds[source][ib].data)[idx];
212  break;
213  case DFNT_INT16: ((int16 *) sds[Hybrid][ib].data)[idx] = ((int16
214  *) sds[source][ib].data)[idx];
215  break;
216  }
217 
218  }
219 
220  for (ib = 0; ib < Nbands; ib++)
221  if (SDwritedata(sds[Hybrid][ib].id, sds[Hybrid][ib].start, NULL,
222  sds[Hybrid][ib].edges, sds[Hybrid][ib].data) == -1) {
223  fprintf(stderr, "Cannot write row %d of SDS %s\n", irow,
224  sds[Hybrid][ib].name);
225  exit(-1);
226  }
227 
228  }
229 
230  for (j = 0; j < Nfiles; j++) {
231  for (ib = 0; ib < Nbands; ib++)
232  SDendaccess(sds[j][ib].id);
233  SDend(sds[j][ib].file_id);
234  }
235 
236  printf("%d land pixels\n", npix);
237  printf("%d pixels challenged\n", nexam);
238  printf("%d pixels replaced\n", nrepl);
239 
240 }
241 
242 double R3(double x) {
243  if (x < 0 || x >= 2)
244  return 0;
245  else if (x <= 1) /* 0 <= x <= 1 */
246  return 2 / 3. + x * x * x / 2. - x * x;
247  else /* 1 <= x <= 2 */
248  return (2 - x) * (2 - x) * (2 - x) / 6;
249 }
250 
251 
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
@ Nfiles
Definition: hybrid.c:20
#define MAX(A, B)
Definition: swl0_utils.h:26
integer, parameter int16
Definition: cubeio.f90:3
@ NDVI
Definition: hybrid.c:24
#define MIN(x, y)
Definition: rice.h:169
int j
Definition: decode_rs.h:73
int32 edges[MAX_VAR_DIMS]
Definition: hybrid.c:30
@ NIR
Definition: hybrid.c:24
int32 Np
Definition: hybrid.c:29
@ GREEN
Definition: hybrid.c:24
#define NULL
Definition: decode_rs.h:63
double R3(double x)
Definition: hybrid.c:242
#define SDSNAME3
Definition: hybrid.c:17
@ Nbands
Definition: hybrid.c:24
@ RED
Definition: hybrid.c:24
#define MAXNAMELENGTH
Definition: hybrid.c:13
int32 rank
Definition: hybrid.c:29
int32 Nl
Definition: hybrid.c:29
@ maxNDVI
Definition: hybrid.c:20
int32 start[H4_MAX_VAR_DIMS]
Definition: hybrid.c:30
void main(int argc, char *argv[])
Definition: hybrid.c:36
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
#define SDSNAME1
Definition: hybrid.c:15
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT16
void * fillvalue
Definition: hybrid.c:31
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
@ BLUE
Definition: hybrid.c:24
void * data
Definition: hybrid.c:31
@ INPUT
Definition: hybrid.c:24
#define RADIUS
Definition: hybrid.c:14
#define fabs(a)
Definition: misc.h:93
Extra metadata that will be written to the HDF4 file l2prod rank
int32 dim_sizes[MAX_VAR_DIMS]
Definition: hybrid.c:30
@ EVI
Definition: hybrid.c:24
#define abs(a)
Definition: misc.h:90
Definition: hybrid.c:27
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
@ minBlue
Definition: hybrid.c:20
int32 num_type
Definition: hybrid.c:29
@ Hybrid
Definition: hybrid.c:20
int k
Definition: decode_rs.h:73
int npix
Definition: get_cmp.c:27
#define MAX_VAR_DIMS
Definition: Granule.h:10
#define SDSNAME2
Definition: hybrid.c:16