OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
read9km_mask.c
Go to the documentation of this file.
1 /****************************************************************/
2 /* */
3 /* Reads a binary mask file used in excluding 9km bins */
4 /* from L3 binned data */
5 /* */
6 /* result = read9km_mask(char *file, char *mask) */
7 /* */
8 /* result = 0 - success */
9 /* 1 - failure */
10 /* */
11 /* Programmer Organization Date Description of change*/
12 /* -------------- ------------ -------- ---------------------*/
13 /* Ewa Kwiatkowska SAIC 11 March 2004 Original development*/
14 /* */
15 /****************************************************************/
16 #include <math.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #define PI 3.141592653589793
20 
21 
22 static int32_t *numbin_xkm, *basebin_xkm, *numbin_9km, *basebin_9km;
23 static float *latbin_xkm;
24 
25 int read9km_mask(char *file, char *mask) {
26  char data[742553];
27  FILE *fp;
28  int32_t len, i, j;
29  unsigned char x[8];
30 
31  if ((fp = fopen(file, "r")) == NULL) {
32  printf(" Cannot open the mask file %s", file);
33  return (1);
34  }
35  if ((len = (int32_t) fread((void *) data, sizeof (char), 1536, fp)) != 1536) {
36  printf(" Cannot read the mask file %s", file);
37  fclose(fp);
38  return (1);
39  }
40  if ((len = (int32_t) fread((void *) data, sizeof (char), 742553, fp)) != 742553) {
41  printf(" Cannot read the mask file %s", file);
42  fclose(fp);
43  return (1);
44  }
45  fclose(fp);
46 
47  for (j = 0; j < 8; j++) x[j] = (unsigned char) pow(2.0, (double) j);
48 
49  for (i = 0; i < 742553; i++) {
50  for (j = 0; j < 8; j++) if (data[i] & x[j]) mask[i * 8 + j] = 1;
51  else mask[i * 8 + j] = 0;
52  }
53 
54 
55  numbin_xkm = NULL;
56  basebin_xkm = NULL;
57  latbin_xkm = NULL;
58  numbin_9km = NULL;
59  basebin_9km = NULL;
60 
61  return ( 0);
62 
63 }
64 
65 int is_masked(int32_t bin, char *mask, int32_t nrows) {
66 
67  static int first_check = 1;
68 
69  int32_t i, rlow, rhi, rmid, row, rows_9km = 2160, xbin, bin_row, bin_col;
70  float radfac, lats, lat, lon;
71 
72 
73  if (nrows == 2160) {
74 
75  if (mask[bin] > 0) return ( 1);
76  else return ( 0);
77 
78  } else
79 
80  if (nrows > 0) {
81 
82  radfac = PI / 180.0;
83 
84  if (first_check) {
85  /* ----------------- */
86  /* Set up bin arrays */
87  /* ----------------- */
88  if ((numbin_xkm = (int32_t *) malloc(nrows * sizeof (int32_t))) == NULL) {
89  printf("-E- %s line %d: Cannot allocate memory to numbin", __FILE__, __LINE__);
90  return (-1);
91  }
92  if ((basebin_xkm = (int32_t *) malloc((nrows + 1) * sizeof (int32_t))) == NULL) {
93  printf("-E- %s line %d: Cannot allocate memory to basebin", __FILE__, __LINE__);
94  return (-1);
95  }
96  if ((latbin_xkm = (float *) malloc(nrows * sizeof (float))) == NULL) {
97  printf("-E- %s line %d: Cannot allocate memory to basebin", __FILE__, __LINE__);
98  return (-1);
99  }
100 
101  for (i = 0; i < nrows; i++) {
102  *(latbin_xkm + i) = (i + 0.5) * (180.0 / nrows) - 90.0;
103  *(numbin_xkm + i) = (int32_t) (cos(*(latbin_xkm + i) * radfac) * (2.0 * nrows) + 0.5);
104  }
105 
106  *basebin_xkm = 1;
107  for (i = 1; i < nrows; i++) *(basebin_xkm + i) = *(basebin_xkm + i - 1) + *(numbin_xkm + i - 1);
108  basebin_xkm[nrows] = *(basebin_xkm + nrows - 1) + *(numbin_xkm + nrows - 1) - 1;
109 
110 
111  if ((numbin_9km = (int32_t *) malloc(rows_9km * sizeof (int32_t))) == NULL) {
112  printf("-E- %s line %d: Cannot allocate memory to numbin", __FILE__, __LINE__);
113  return (-1);
114  }
115  if ((basebin_9km = (int32_t *) malloc((rows_9km + 1) * sizeof (int32_t))) == NULL) {
116  printf("-E- %s line %d: Cannot allocate memory to basebin", __FILE__, __LINE__);
117  return (-1);
118  }
119  for (i = 0; i < rows_9km; i++) {
120  lats = (i + 0.5) * (180.0 / rows_9km) - 90.0;
121  numbin_9km[i] = (int32_t) (cos(lats * radfac) * (2.0 * rows_9km) + 0.5);
122  }
123 
124  *basebin_9km = 1;
125  for (i = 1; i < rows_9km; i++) *(basebin_9km + i) = *(basebin_9km + i - 1) + *(numbin_9km + i - 1);
126  basebin_9km[rows_9km] = *(basebin_9km + rows_9km - 1) + *(numbin_9km + rows_9km - 1) - 1;
127  }
128 
129 
130  if (bin < 1)
131  bin = 1; /* south pole */
132  if (bin > basebin_xkm[nrows])
133  bin = basebin_xkm[nrows]; /* north pole */
134 
135  /* binary search for row in range [1..nrows] */
136  rlow = 1; /* 1-relative */
137  rhi = nrows; /* 1-relative */
138  while (1) {
139 
140  rmid = (rlow + rhi - 1) / 2; /* 0-relative */
141  if (*(basebin_xkm + rmid) > bin) rhi = rmid;
142  else rlow = rmid + 1;
143 
144  if (rlow == rhi) {
145  row = rlow;
146  break;
147  }
148  }
149 
150  lat = *(latbin_xkm + row - 1);
151  lon = 360.0 * (bin - *(basebin_xkm + row - 1) + 0.5) / *(numbin_xkm + row - 1);
152  lon = lon - 180.0; /* note, lon returned here may be in 0 to 360 */
153 
154  bin_row = (int32_t) ((90.0 + lat) * ((double) rows_9km / (double) 180.0));
155  bin_col = (int32_t) ((double) numbin_9km[bin_row] * (lon + 180.0) / (double) 360.0);
156  xbin = basebin_9km[bin_row] + bin_col;
157 
158  first_check = 0;
159 
160  if (mask[xbin] > 0) return ( 1);
161  else return ( 0);
162 
163  } else {
164 
165  if (numbin_xkm != NULL) free(numbin_xkm);
166  if (basebin_xkm != NULL) free(basebin_xkm);
167  if (latbin_xkm != NULL) free(latbin_xkm);
168  if (numbin_9km != NULL) free(numbin_9km);
169  if (basebin_9km != NULL) free(basebin_9km);
170 
171  return ( 0);
172  }
173 
174 }
175 
176 
177 
178 
int j
Definition: decode_rs.h:73
#define PI
Definition: read9km_mask.c:19
#define NULL
Definition: decode_rs.h:63
float * lat
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed file
Definition: HISTORY.txt:413
int read9km_mask(char *file, char *mask)
Definition: read9km_mask.c:25
int32 nrows
a context in which it is NOT documented to do so subscript which cannot be easily calculated when extracting TONS attitude data from the Terra L0 files Corrected several defects in extraction of entrained ephemeris and and as HDF file for both the L1A and Geolocation enabling retrieval of South Polar DEM data Resolved Bug by changing to opent the geolocation file only after a successful read of the L1A and also by checking for fatal errors from not restoring C5 and to report how many of those high resolution values were water in the new WaterPresent SDS Added valid_range attribute to Land SeaMask Changed to bilinearly interpolate the geoid_height to remove artifacts at one degree lines Made corrections to const qualification of pointers allowed by new version of M API library Removed casts that are no longer for same not the geoid Corrected off by one error in calculation of high resolution offsets Corrected parsing of maneuver list configuration parameter Corrected to set Height SDS to fill values when geolocation when for elevation and land water mask
Definition: HISTORY.txt:114
integer, parameter double
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
int is_masked(int32_t bin, char *mask, int32_t nrows)
Definition: read9km_mask.c:65
float * lon
int i
Definition: decode_rs.h:71