OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
world_avg.c
Go to the documentation of this file.
1 #include <mfhdf.h>
2 #include "ancil.h"
3 
4 void world_avg(int16 *inarr, int32 nlin, int32 npix, int avg_pix,
5  int avg_lin, int16 *outarr)
6 /*******************************************************************
7 
8  world_avg
9 
10  purpose: average all non 0 array values inside a box, cross the
11  longitude seam to do this
12 
13  Returns type: void no value
14 
15  Parameters: (in calling order)
16  Type Name I/O Description
17  ---- ---- --- -----------
18  int16 * inarr I input array
19  int32 nlin I # lines in the input array
20  int32 npix I # pixels in the input array
21  int avg_pix I pixel size of box to average
22  with, will be forced to 1
23  if <1 and to npix - 1
24  if > npix - 1
25  int avg_lin I line size of box to average
26  with, will be forced to 1
27  if <1 and to nlin - 1
28  if > nlin - 1
29  int16 * outarr O output array
30 
31  Modification history:
32  Programmer Date Description of change
33  ---------- ---- ---------------------
34  W. Robinson 3-Jul-1997 Original development
35 
36  *******************************************************************/
37  {
38  int pix_minus, pix_plus, lin_minus, lin_plus;
39  int num, avg, ipx, iln, jpx, jln, jpxm;
40  /*
41  * check / adjust the box size
42  */
43  if (npix < 2 || nlin < 2) {
44  printf("world_avg: either #pix (%d) or # lin (%d) < 2\n", npix, nlin);
45  printf(" no averaging done (probably an error)\n");
46  return;
47  }
48  if (avg_pix < 1) {
49  printf("world_avg: avg_pix (%d) < 1, resetting to 1\n", avg_pix);
50  avg_pix = 1;
51  }
52  if (avg_lin < 1) {
53  printf("world_avg: avg_lin (%d) < 1, resetting to 1\n", avg_lin);
54  avg_lin = 1;
55  }
56  if (avg_pix > npix - 1) {
57  printf(
58  "world_avg: avg_pix (%d) > # pixels - 1 (# pixels = %d), resetting to %d\n",
59  avg_pix, npix, (npix - 1));
60  avg_pix = npix - 1;
61  }
62  if (avg_lin > nlin - 1) {
63  printf(
64  "world_avg: avg_lin (%d) > # lines - 1 (# lines = %d), resetting to %d\n",
65  avg_lin, nlin, (nlin - 1));
66  avg_pix = npix - 1;
67  }
68  /*
69  * get the box extent in +, - direction from box size
70  */
71  pix_minus = (avg_pix - 1) / 2;
72  pix_plus = avg_pix / 2;
73 
74  lin_minus = (avg_lin - 1) / 2;
75  lin_plus = avg_lin / 2;
76  /*
77  * loop over the lines and pixels and only smooth the non-zero (data) points
78  */
79  for (iln = 0; iln < nlin; iln++) {
80  for (ipx = 0; ipx < npix; ipx++) {
81  if (*(inarr + ipx + iln * npix) > 0) {
82  /*
83  * for this data pixel, average the surrounding box of values
84  */
85  num = 0;
86  avg = 0;
87  for (jln = iln - lin_minus; jln <= iln + lin_plus; jln++) {
88  if (jln >= 0 && jln < nlin) {
89  for (jpx = ipx - pix_minus; jpx <= ipx + pix_plus; jpx++) {
90  /*
91  * adjust the pixel to wrap around the seam
92  */
93  if (jpx < 0)
94  jpxm = npix + jpx;
95  else if (jpx >= npix)
96  jpxm = jpx - npix;
97  else
98  jpxm = jpx;
99 
100  /*
101  * sum the pixel if it is > 0
102  */
103  if (*(inarr + jpxm + jln * npix) > 0) {
104  num++;
105  avg += *(inarr + jpxm + jln * npix);
106  }
107  }
108  }
109  }
110  *(outarr + ipx + iln * npix) = avg / num;
111  } else {
112  *(outarr + ipx + iln * npix) = 0;
113  }
114  }
115  }
116  /*
117  * return the output
118  */
119  return;
120 }
integer, parameter int16
Definition: cubeio.f90:3
int nlin
Definition: get_cmp.c:28
int npix
Definition: get_cmp.c:27
void world_avg(int16 *inarr, int32 nlin, int32 npix, int avg_pix, int avg_lin, int16 *outarr)
Definition: world_avg.c:4