NASA Logo
Ocean Color Science Software

ocssw V2022
resolution_utils.cpp
Go to the documentation of this file.
1 #include <genutils.h>
2 
3 #include <boost/algorithm/string/trim.hpp>
4 #include <boost/algorithm/string/case_conv.hpp>
5 #include <boost/algorithm/string/predicate.hpp>
6 #include <boost/algorithm/string.hpp>
7 
8 bool is_digits(const std::string &str)
9 {
10  return std::all_of(str.begin(), str.end(), ::isdigit);
11 }
12 
13 double string2resolution(std::string resolutionStr) {
14  double resolution = BAD_FLT;
15  boost::trim(resolutionStr);
16  boost::to_lower(resolutionStr);
17  resolutionStr.erase(remove_if(resolutionStr.begin(), resolutionStr.end(), ::isspace), resolutionStr.end());
18 
19  if (resolutionStr.compare("90km") == 0)
21  else if (resolutionStr.compare("36km") == 0)
23  else if (resolutionStr.compare("18km") == 0)
25  else if (resolutionStr.compare("9km") == 0)
27  else if (resolutionStr.compare("4km") == 0)
29  else if (resolutionStr.compare("2km") == 0)
30  resolution = EARTH_CIRCUMFERENCE / 17280.0;
31  else if (resolutionStr.compare("1km") == 0)
32  resolution = EARTH_CIRCUMFERENCE / 34560.0;
33  else if (resolutionStr.compare("hkm") == 0)
34  resolution = EARTH_CIRCUMFERENCE / 69120.0;
35  else if (resolutionStr.compare("qkm") == 0)
36  resolution = EARTH_CIRCUMFERENCE / 138240.0;
37  else if (resolutionStr.compare("hqkm") == 0)
38  resolution = EARTH_CIRCUMFERENCE / 276480.0;
39  else if (resolutionStr.compare("hhkm") == 0)
40  resolution = EARTH_CIRCUMFERENCE / 552960.0;
41  else if (resolutionStr.compare("smi") == 0)
43  else if (resolutionStr.compare("smi4") == 0)
45  else if (resolutionStr.compare("land") == 0)
47  else if (resolutionStr.compare("thirddeg") == 0)
49  else if (boost::ends_with(resolutionStr, "km")) {
50  std::string val = resolutionStr.substr(0, resolutionStr.length() - 2);
51  resolution = atof(val.c_str()) * 1000.0;
52  } else if (boost::ends_with(resolutionStr, "m")) {
53  std::string val = resolutionStr.substr(0, resolutionStr.length() - 1);
54  resolution = atof(val.c_str());
55  } else if (boost::ends_with(resolutionStr, "deg")) {
56  std::string val = resolutionStr.substr(0, resolutionStr.length() - 3);
57  resolution = atof(val.c_str()) / 360.0 * EARTH_CIRCUMFERENCE;
58  } else if (is_digits(resolutionStr)) {
59  resolution = atof(resolutionStr.c_str());
60  } else {
62  }
63  return resolution;
64 }
65 
67  std::string resolutionStr = "Unknown";
68  if (resolution > 0) {
69  std::string geoUnits = " m";
70  if (resolution > 1000.0) {
71  resolution = resolution / 1000.0;
72  geoUnits = " km";
73  }
74  resolutionStr = std::to_string(resolution) + geoUnits;
75  }
76  return resolutionStr;
77 }
78 
79 extern "C" double str2resolution(char const * resolutionStr) {
80  std::string str(resolutionStr);
81  return string2resolution(str);
82 }
83 
84 extern "C" const char* resolution2str(double resolution) {
85  static std::string stringRes = resolution2string(resolution);
86  return stringRes.c_str();
87 }
88 
89 extern "C" double resolution2degrees(double resolution) {
90  return resolution / EARTH_CIRCUMFERENCE * 360.0;
91 }
92 
93 extern "C" double degrees2resolution(double degrees) {
94  return degrees / 360.0 * EARTH_CIRCUMFERENCE;
95 }
96 
97 // this is called resolve since the string is different that the resolution string above.
98 // This is the input string (resolve) to l2bin
99 void resolve2binRows(std::string resolve, int32_t &nrows, double &resolution) {
100  if (resolve == "1D") {
101  nrows = 180;
102  resolution = str2resolution("1deg");
103  } else if (resolve == "HD") {
104  nrows = 360;
105  resolution = str2resolution("0.5deg");
106  } else if (resolve == "QD") {
107  nrows = 720;
108  resolution = str2resolution("0.25deg");
109  } else if (resolve == "36") {
110  nrows = 2160 / 4;
111  resolution = str2resolution("36km");
112  } else if (resolve == "18") {
113  nrows = 2160 / 2;
114  resolution = str2resolution("18km");
115  } else if (resolve == "9") {
116  nrows = 2160;
117  resolution = str2resolution("9km");
118  } else if (resolve == "4") {
119  nrows = 2160 * 2;
120  resolution = str2resolution("4km");
121  } else if (resolve == "2") {
122  nrows = 2160 * 4;
123  resolution = str2resolution("2km");
124  } else if (resolve == "1") {
125  nrows = 2160 * 8;
126  resolution = str2resolution("1km");
127  } else if (resolve == "H") {
128  nrows = 2160 * 16;
129  resolution = str2resolution("hkm");
130  } else if (resolve == "Q") {
131  nrows = 2160 * 32;
132  resolution = str2resolution("qkm");
133  } else if (resolve == "HQ") {
134  nrows = 2160 * 80;
135  resolution = str2resolution("hqkm");
136  } else if (resolve == "HH") {
137  nrows = 2160 * 160;
138  resolution = str2resolution("hhkm");
139  } else {
140  nrows = -1;
141  resolution = -1;
142  }
143 }
144 
145 extern "C" int32_t resolve2binRows(const char *resolve) {
146  int32_t nrows;
147  double resolution;
148  resolve2binRows(resolve, nrows, resolution);
149  return nrows;
150 }
151 
152 extern "C" double resolve2resolution(const char *resolve) {
153  int32_t nrows;
154  double resolution;
155  resolve2binRows(resolve, nrows, resolution);
156  return resolution;
157 }
158 
159 
double degrees2resolution(double degrees)
Pixel resolution degrees to meters.
double resolution2degrees(double resolution)
Pixel resolution meters to degrees.
bool is_digits(const std::string &str)
this program makes no use of any feature of the SDP Toolkit that could generate such a then geolocation is calculated at that resolution
Definition: HISTORY.txt:188
int32 nrows
@ string
#define isdigit(c)
void resolve2binRows(std::string resolve, int32_t &nrows, double &resolution)
double string2resolution(std::string resolutionStr)
double resolve2resolution(const char *resolve)
convert l2bin style resolve string to pixel resolution in meters
double str2resolution(char const *resolutionStr)
Pixel resolution string to meters.
const char * resolution2str(double resolution)
Pixel resolution meters to string.
std::string resolution2string(double resolution)
#define EARTH_CIRCUMFERENCE
Definition: genutils.h:10
string & trim(string &s, const string &delimiters)
Definition: EnvsatUtil.cpp:29
#define isspace(c)
#define BAD_FLT
Definition: jplaeriallib.h:19
#define degrees(radians)
Definition: niwa_iop.c:30
Definition: aerosol.c:136
#define str(s)