OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
L3ShapeSMI.cpp
Go to the documentation of this file.
1 #include <L3ShapeSMI.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <math.h>
5 
6 namespace l3 {
7 
12 L3ShapeSMI::L3ShapeSMI(int32_t numRows, int32_t numCols,
13  double north, double south,
14  double east, double west)
15 : L3Shape(numRows) {
16  totalCols = numCols;
18 
19  north_ = north;
20  south_ = south;
21  east_ = east;
22  west_ = west;
23 }
24 
29 }
30 
36 void L3ShapeSMI::constrainRowCol(int32_t &row, int32_t &col) const {
37  if (row < 0)
38  row = 0;
39  else if (row >= totalRows)
40  row = totalRows - 1;
41 
42  if (col < 0) {
43  col = totalCols + col;
44  if(col < 0)
45  col = 0;
46  } else if (col >= totalCols) {
47  col = col - totalCols;
48  if(col >= totalCols)
49  col = totalCols - 1;
50  }
51 }
52 
58 int64_t L3ShapeSMI::getBaseBin(int32_t row) const {
59  constrainRow(row);
60  return (int64_t) row * (int64_t) totalCols + 1;
61 }
62 
68 int32_t L3ShapeSMI::getNumCols(int32_t row) const {
69  return totalCols;
70 }
71 
77 int32_t L3ShapeSMI::bin2row(int64_t bin) {
78  if (bin < 1)
79  return 0; /* south pole */
80  if (bin >= totalBins)
81  return totalRows - 1; /* north pole */
82  return (bin - 1) / totalCols;
83 }
84 
91 void L3ShapeSMI::bin2rowcol(int64_t bin, int32_t &row, int32_t &col) {
92  if (bin < 1) {
93  row = col = 0; /* south pole */
94  return;
95  }
96  if (bin >= totalBins) {
97  row = totalRows - 1;
98  col = totalCols - 1; /* north pole */
99  return;
100  }
101  bin--;
102  row = bin / totalCols;
103  col = bin - row * totalCols;
104 }
105 
112 int64_t L3ShapeSMI::rowcol2bin(int32_t row, int32_t col) const {
113  constrainRowCol(row, col);
114  return row * totalCols + col + 1;
115 }
116 
124 void L3ShapeSMI::rowcol2latlon(int32_t row, int32_t col,
125  double &lat, double &lon) const {
126  constrainRowCol(row, col);
127  lat = (north_ - south_) * (row + 0.5) / totalRows + south_;
128  lon = (east_ - west_) * (col + 0.5) / totalCols + west_;
129 }
130 
136 int32_t L3ShapeSMI::lat2row(double lat) const {
137  lat = constrainLat(lat);
138  if (lat > north_ || lat < south_)
139  return -1;
140 
141  int32_t row =
142  (int32_t) ((lat - south_) * totalRows / (north_ - south_));
143  if (row >= totalRows)
144  row = totalRows - 1;
145  if (row < 0)
146  row = 0;
147  return row;
148 }
149 
155 double L3ShapeSMI::row2lat(int32_t row) const {
156  return (north_ - south_) * (row + 0.5) / totalRows + south_;
157 }
158 
159 
167 void L3ShapeSMI::latlon2rowcol(double lat, double lon,
168  int32_t &row, int32_t &col) const {
169  row = lat2row(lat);
170  if (row == -1) {
171  col = -1;
172  return;
173  }
174  lon = constrainLon(lon);
175  if (lon < west_ || lon > east_) {
176  row = col = -1;
177  return;
178  }
179  col = (int32_t) (totalCols * (lon - west_) / (east_ - west_));
180  if (col >= totalCols || col < 0)
181  row = col = -1;
182 }
183 
190 int64_t L3ShapeSMI::latlon2bin(double lat, double lon) const {
191  int32_t row, col;
192 
193  latlon2rowcol(lat, lon, row, col);
194  return (int64_t) row * (int64_t) totalCols + (int64_t) col;
195 }
196 
206 void L3ShapeSMI::rowcol2bounds(int32_t row, int32_t col,
207  double &north, double &south,
208  double &east, double &west) const {
209  double lat, lon;
210  rowcol2latlon(row, col, lat, lon);
211  double tmp = (north_ - south_) / totalRows / 2.0;
212  north = lat + tmp;
213  south = lat - tmp;
214 
215  tmp = (east_ - west_) / totalCols / 2.0;
216  east = lon + tmp;
217  west = lon - tmp;
218 }
219 
220 }
double north_
Definition: L3ShapeSMI.h:18
double constrainLat(double lat)
Definition: L3Shape.cpp:13
double south_
Definition: L3ShapeSMI.h:18
virtual int64_t rowcol2bin(int32_t row, int32_t col) const
Definition: L3ShapeSMI.cpp:112
float * lat
virtual void rowcol2latlon(int32_t row, int32_t col, double &lat, double &lon) const
Definition: L3ShapeSMI.cpp:124
double constrainLon(double lon)
Definition: L3Shape.cpp:26
int32_t totalCols
Definition: L3ShapeSMI.h:17
virtual int64_t latlon2bin(double lat, double lon) const
Definition: L3ShapeSMI.cpp:190
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
Definition: L3ShapeSMI.cpp:206
virtual int32_t lat2row(double lat) const
Definition: L3ShapeSMI.cpp:136
data_t tmp
Definition: decode_rs.h:74
L3ShapeSMI(int32_t numRows, int32_t numCols, double north, double south, double east, double west)
Definition: L3ShapeSMI.cpp:12
virtual int32_t bin2row(int64_t bin)
Definition: L3ShapeSMI.cpp:77
virtual void bin2rowcol(int64_t bin, int32_t &row, int32_t &col)
Definition: L3ShapeSMI.cpp:91
double east_
Definition: L3ShapeSMI.h:18
virtual int32_t getNumCols(int32_t row) const
Definition: L3ShapeSMI.cpp:68
float * lon
virtual void latlon2rowcol(double lat, double lon, int32_t &row, int32_t &col) const
Definition: L3ShapeSMI.cpp:167
Definition: L3File.cpp:10
void constrainRowCol(int32_t &row, int32_t &col) const
Definition: L3ShapeSMI.cpp:36
int64_t totalBins
Definition: L3Shape.h:20
virtual int64_t getBaseBin(int32_t row) const
Definition: L3ShapeSMI.cpp:58
virtual double row2lat(int32_t row) const
Definition: L3ShapeSMI.cpp:155
double west_
Definition: L3ShapeSMI.h:18
int32_t totalRows
Definition: L3Shape.h:21
virtual ~L3ShapeSMI()
Definition: L3ShapeSMI.cpp:28