bes  Updated for version 3.20.10
StareFunctions.h
1 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
2 // Access Protocol.
3 
4 // Copyright (c) 2019 OPeNDAP, Inc.
5 // Authors: Kodi Neumiller <kneumiller@opendap.org>
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
22 
23 #include <string>
24 #include <utility>
25 
26 #include <STARE.h>
27 #include <hdf5.h>
28 
29 #include <libdap/BaseType.h>
30 #include <libdap/dods-datatypes.h>
31 
32 #include <libdap/ServerFunction.h>
33 
34 #define STARE_STORAGE_PATH_KEY "FUNCTIONS.stareStoragePath"
35 #define STARE_SIDECAR_SUFFIX_KEY "FUNCTIONS.stareSidecarSuffix"
36 
37 namespace libdap {
38 class BaseType;
39 class DDS;
40 class D4RValueList;
41 class DMR;
42 }
43 
44 namespace functions {
45 
46 // These default values can be overridden using BES keys.
47 // See DapFunctions.cc. jhrg 5/21/20
48 extern string stare_storage_path;
49 extern string stare_sidecar_suffix;
50 
51 bool target_in_dataset(const std::vector<STARE_ArrayIndexSpatialValue> &target_indices,
52  const std::vector<STARE_ArrayIndexSpatialValue> &data_stare_indices);
53 
54 unsigned int count(const std::vector<STARE_ArrayIndexSpatialValue> &target_indices,
55  const std::vector<STARE_ArrayIndexSpatialValue> &dataset_indices, bool all_target_matches = false);
56 
57 template<class T>
58 void stare_subset_array_helper(vector<T> &result_data, const vector<T> &src_data,
59  const vector<STARE_ArrayIndexSpatialValue> &target_indices,
60  const vector<STARE_ArrayIndexSpatialValue> &dataset_indices);
61 
63 struct stare_matches {
64  std::vector<libdap::dods_int32> row_indices;
65  std::vector<libdap::dods_int32> col_indices;
66 
67  std::vector<STARE_ArrayIndexSpatialValue> stare_indices;
68  std::vector<STARE_ArrayIndexSpatialValue> target_indices;
69 
70  // Pass by value and use move
71  stare_matches(std::vector<libdap::dods_int32> row, std::vector<libdap::dods_int32> col,
72  std::vector<STARE_ArrayIndexSpatialValue> si, std::vector<STARE_ArrayIndexSpatialValue> ti)
73  : row_indices(std::move(row)), col_indices(std::move(col)), stare_indices(std::move(si)),
74  target_indices(std::move(ti)) {}
75 
76  stare_matches() = default;
77 
78  void add(libdap::dods_int32 row, libdap::dods_int32 col, STARE_ArrayIndexSpatialValue si, STARE_ArrayIndexSpatialValue ti) {
79  row_indices.push_back(row);
80  col_indices.push_back(col);
81  stare_indices.push_back(si);
82  target_indices.push_back(ti);
83  }
84 
85  friend std::ostream &operator<<(std::ostream &out, const stare_matches &m);
86 };
87 
88 unique_ptr<stare_matches> stare_subset_helper(const std::vector<STARE_ArrayIndexSpatialValue> &target_indices,
89  const std::vector<STARE_ArrayIndexSpatialValue> &dataset_indices,
90  size_t row, size_t cols);
91 
92 class StareIntersectionFunction : public libdap::ServerFunction {
93 public:
94  static libdap::BaseType *stare_intersection_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
95 
96  friend class StareFunctionsTest;
97 
98 public:
100  setName("stare_intersection");
101  setDescriptionString(
102  "The stare_intersection: Returns 1 if the coverage of the current dataset includes any of the given STARE indices, 0 otherwise.");
103  setUsageString(
104  "stare_intersection(var, STARE index [, STARE index ...]) | stare_intersection(var, $UInt64(<size hint>:STARE index [, STARE index ...]))");
105  setRole("http://services.opendap.org/dap4/server-side-function/stare_intersection");
106  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_intersection");
108  setVersion("0.3");
109  }
110 
111  ~StareIntersectionFunction() override = default;
112 };
113 
114 class StareCountFunction : public libdap::ServerFunction {
115 public:
116  static libdap::BaseType *stare_count_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
117 
118  friend class StareFunctionsTest;
119 
120 public:
122  setName("stare_count");
123  setDescriptionString(
124  "The stare_count: Returns the number of the STARE indices that are included in the coverage of this dataset.");
125  setUsageString(
126  "stare_count(var, STARE index [, STARE index ...]) | stare_count(var, $UInt64(<size hint>:STARE index [, STARE index ...]))");
127  setRole("http://services.opendap.org/dap4/server-side-function/stare_count");
128  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_count");
129  setFunction(stare_count_dap4_function);
130  setVersion("0.3");
131  }
132 
133  ~StareCountFunction() override = default;
134 };
135 
136 class StareSubsetFunction : public libdap::ServerFunction {
137 public:
138  static libdap::BaseType *stare_subset_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
139 
140  friend class StareFunctionsTest;
141 
142 public:
144  setName("stare_subset");
145  setDescriptionString(
146  "The stare_subset: Returns the set of the STARE indices that are included in the coverage of this dataset.");
147  setUsageString("stare_subset(var, $UInt64(<size hint>:STARE index [, STARE index ...]))");
148  setRole("http://services.opendap.org/dap4/server-side-function/stare_subset");
149  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_subset");
150  setFunction(stare_subset_dap4_function);
151  setVersion("0.3");
152  }
153 
154  ~StareSubsetFunction() override = default;
155 };
156 
157 class StareSubsetArrayFunction : public libdap::ServerFunction {
158 public:
159  static libdap::BaseType *stare_subset_array_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
160 
161  friend class StareFunctionsTest;
162 
163 public:
165  setName("stare_subset_array");
166  setDescriptionString(
167  "The stare_subset: Returns a masked copy of 'var' for the subset given the set of STARE indices that are included in the coverage of this dataset.");
168  setUsageString("stare_subset_array(var, mask-val, $UInt64(<size hint>:STARE index [, STARE index ...]))");
169  setRole("http://services.opendap.org/dap4/server-side-function/stare_subset_array");
170  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_subset_array");
171  setFunction(stare_subset_array_dap4_function);
172  setVersion("0.1");
173  }
174 
175  ~StareSubsetArrayFunction() override = default;
176 
177  template<class T>
178  static void build_masked_data(libdap::Array *dependent_var,
179  const std::vector<STARE_ArrayIndexSpatialValue> &dep_var_stare_indices,
180  const std::vector<STARE_ArrayIndexSpatialValue> &target_s_indices, T mask_value,
181  unique_ptr<libdap::Array> &result);
182 };
183 
187 struct point {
188  double lat;
189  double lon;
190  point() = default;
191  point(double lat_, double lon_) : lat(lat_), lon(lon_) {}
192 };
193 
194 STARE_SpatialIntervals stare_box_helper(const vector<point> &points, int resolution = 6);
195 STARE_SpatialIntervals stare_box_helper(const point &top_left, const point &bottom_right, int resolution = 6);
196 
197 class StareBoxFunction : public libdap::ServerFunction {
198 public:
199  static libdap::BaseType *stare_box_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
200 
201  friend class StareFunctionsTest;
202 
203 public:
204  StareBoxFunction() {
205  setName("stare_box");
206  setDescriptionString(
207  "The stare_box() function: Returns a STARE cover for the region within the four lat/lon corner points.");
208  setUsageString("stare_box(tl_lat, tl_lon, br_lat, br_lon) or stare_box(p1_lat, p1_lon, ..., p4_lat, p4_lon)");
209  setRole("http://services.opendap.org/dap4/server-side-function/stare_box");
210  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_box");
211  setFunction(stare_box_dap4_function);
212  setVersion("0.1");
213  }
214 
215  ~StareBoxFunction() override = default;
216 };
217 
218 } // functions namespace
static libdap::BaseType * stare_count_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr)
Count the number of STARE indices in the arg that overlap the indices of this dataset.
static libdap::BaseType * stare_intersection_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr)
Return true/false indicating that the given stare indices intersect the variables.
static libdap::BaseType * stare_subset_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr)
For the given target STARE indices, return the overlapping dataset X, Y, and STARE indices.
Hold the result from the subset helper function as a collection of vectors.
friend std::ostream & operator<<(std::ostream &out, const stare_matches &m)
Write a collection of STARE Matches to an ostream.