OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
L3ShapeIsine.cpp
Go to the documentation of this file.
1 #include <L3ShapeIsine.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <math.h>
5 
6 // this should not be necessary, but M_PI seems to not be defined in C99
7 #define PI 3.141592653589793
8 
9 namespace l3 {
10 
11 
16 L3ShapeIsine::L3ShapeIsine(int32_t numRows) : L3Shape(numRows) {
17  int32_t i;
18  double radfac = PI / 180.0;
19 
20  oldRow = 0;
21 
22  numBin = (int32_t *) calloc(totalRows, sizeof (int32_t));
23  baseBin = (int64_t *) calloc(totalRows + 1, sizeof (int64_t));
24  latBin = (double *) calloc(totalRows, sizeof (double));
25 
26  for (i = 0; i < totalRows; i++) {
27  latBin[i] = (i + 0.5) * (180.0 / totalRows) - 90.0;
28  numBin[i] = (int32_t) (cos(latBin[i] * radfac) * (2.0 * totalRows) + 0.5);
29  }
30 
31  baseBin[0] = 1;
32 
33  for (i = 1; i <= totalRows; i++)
34  baseBin[i] = baseBin[i - 1] + numBin[i - 1];
36 }
37 
42  free(numBin);
43  free(baseBin);
44  free(latBin);
45 }
46 
52 void L3ShapeIsine::constrainRowCol(int32_t &row, int32_t &col) const {
53  if (row < 0) {
54  row = 0 - row;
55  // if pass a pole even times, need to change col to other side
56  if((row / totalRows) % 2 == 0) {
57  row %= totalRows;
58  col += numBin[row] / 2;
59  } else {
60  row %= totalRows;
61  }
62  } else if (row >= totalRows) {
63  // if pass a pole odd times, need to change col to other side
64  if((row / totalRows) % 2 == 1) {
65  row %= totalRows;
66  col += numBin[row] / 2;
67  } else {
68  row %= totalRows;
69  }
70  }
71 
72  if (col < 0 || col >= numBin[row]) {
73  col %= numBin[row];
74  }
75 }
76 
82 int64_t L3ShapeIsine::getBaseBin(int32_t row) const {
83  constrainRow(row);
84  return baseBin[row];
85 }
86 
92 int32_t L3ShapeIsine::getNumCols(int32_t row) const {
93  constrainRow(row);
94  return numBin[row];
95 }
96 
102 int32_t L3ShapeIsine::bin2row(int64_t bin) {
103  int32_t rlow, rhi, rmid;
104 
105  if (baseBin[oldRow] <= bin && baseBin[oldRow + 1] > bin) {
106  return oldRow;
107  } else {
108  if (bin < 1)
109  return 0; /* south pole */
110  if (bin >= totalBins)
111  return totalRows - 1; /* north pole */
112 
113  /* binary search for row */
114  rlow = 0;
115  rhi = totalRows - 1;
116  while (1) {
117  rmid = (rlow + rhi + 1) / 2;
118  if (baseBin[rmid] > bin)
119  rhi = rmid - 1;
120  else
121  rlow = rmid;
122 
123  if (rlow == rhi) {
124  oldRow = rlow;
125  return rlow;
126  }
127  }
128  }
129  return 0;
130 }
131 
138 void L3ShapeIsine::bin2rowcol(int64_t bin, int32_t &row, int32_t &col) {
139  if (bin < 1) {
140  row = col = 0; /* south pole */
141  return;
142  }
143  if (bin >= totalBins) {
144  row = totalRows - 1;
145  col = numBin[row] - 1; /* north pole */
146  return;
147  }
148  row = bin2row(bin);
149  col = bin - baseBin[row];
150 }
151 
158 int64_t L3ShapeIsine::rowcol2bin(int32_t row, int32_t col) const {
159  constrainRowCol(row, col);
160  return baseBin[row] + col;
161 }
162 
170 void L3ShapeIsine::rowcol2latlon(int32_t row, int32_t col,
171  double &lat, double &lon) const {
172  constrainRowCol(row, col);
173  lat = latBin[row];
174  lon = 360.0 * (col + 0.5) / numBin[row] + seamLon;
175 }
176 
182 int32_t L3ShapeIsine::lat2row(double lat) const {
183  lat = constrainLat(lat);
184  int32_t row = (int32_t) ((90.0 + lat) * totalRows / 180.0);
185  if (row >= totalRows)
186  row = totalRows - 1;
187  return row;
188 }
189 
195 double L3ShapeIsine::row2lat(int32_t row) const {
196  if(row < 0)
197  return latBin[0];
198  if(row >= totalRows)
199  return latBin[totalRows-1];
200  return latBin[row];
201 }
202 
210 void L3ShapeIsine::latlon2rowcol(double lat, double lon,
211  int32_t &row, int32_t &col) const {
212  row = lat2row(lat);
213  lon = constrainLon(lon);
214  col = (int32_t) ((double) numBin[row] * (lon - seamLon) / 360.0);
215  if (col >= numBin[row])
216  col = numBin[row] - 1;
217 }
218 
225 int64_t L3ShapeIsine::latlon2bin(double lat, double lon) const {
226  int32_t row, col;
227 
228  latlon2rowcol(lat, lon, row, col);
229  return baseBin[row] + col;
230 }
231 
241 void L3ShapeIsine::rowcol2bounds(int32_t row, int32_t col,
242  double &north, double &south,
243  double &east, double &west) const {
244  double lat, lon;
245  rowcol2latlon(row, col, lat, lon);
246  north = lat + 90.0 / totalRows;
247  south = lat - 90.0 / totalRows;
248  east = lon + 180.0 / numBin[row];
249  west = lon - 180.0 / numBin[row];
250 }
251 
252 }
virtual void bin2rowcol(int64_t bin, int32_t &row, int32_t &col)
double constrainLat(double lat)
Definition: L3Shape.cpp:13
virtual void constrainRowCol(int32_t &row, int32_t &col) const
virtual ~L3ShapeIsine()
virtual int64_t latlon2bin(double lat, double lon) const
virtual int64_t rowcol2bin(int32_t row, int32_t col) const
float * lat
virtual double row2lat(int32_t row) const
double constrainLon(double lon)
Definition: L3Shape.cpp:26
int32_t * numBin
Definition: L3ShapeIsine.h:18
virtual void constrainRow(int32_t &row) const
Definition: L3Shape.cpp:71
virtual void rowcol2bounds(int32_t row, int32_t col, double &north, double &south, double &east, double &west) const
double seamLon
Definition: L3Shape.h:22
L3ShapeIsine(int32_t numRows)
virtual void latlon2rowcol(double lat, double lon, int32_t &row, int32_t &col) const
virtual int32_t bin2row(int64_t bin)
virtual int64_t getBaseBin(int32_t row) const
#define PI
Definition: L3ShapeIsine.cpp:7
virtual void rowcol2latlon(int32_t row, int32_t col, double &lat, double &lon) const
virtual int32_t getNumCols(int32_t row) const
float * lon
int64_t * baseBin
Definition: L3ShapeIsine.h:19
Definition: L3File.cpp:10
int64_t totalBins
Definition: L3Shape.h:20
int i
Definition: decode_rs.h:71
int32_t totalRows
Definition: L3Shape.h:21
virtual int32_t lat2row(double lat) const