bes  Updated for version 3.20.8
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 <dods-datatypes.h>
27 
28 #include <STARE.h>
29 #include <hdf5.h>
30 
31 #include "ServerFunction.h"
32 
33 namespace libdap {
34 class BaseType;
35 class DDS;
36 class D4RValueList;
37 class DMR;
38 }
39 
40 namespace functions {
41 
42 const string s_index_name = "Stare_Index";
43 
44 const std::string STARE_STORAGE_PATH_KEY = "FUNCTIONS.stareStoragePath";
45 const std::string STARE_SIDECAR_SUFFIX_KEY = "FUNCTIONS.stareSidecarSuffix";
46 
47 // These default values can be overridden using BES keys.
48 // See DapFunctions.cc. jhrg 5/21/20
49 extern string stare_storage_path;
50 extern string stare_sidecar_suffix;
51 
52 std::string get_sidecar_file_pathname(const std::string &pathName, const string &token = "_sidecar");
53 void get_sidecar_int32_values(hid_t file, const std::string &variable, std::vector<libdap::dods_int32> &values);
54 void get_sidecar_uint64_values(hid_t file, const std::string &variable, std::vector<libdap::dods_uint64> &values);
55 
56 bool target_in_dataset(const std::vector<libdap::dods_uint64> &targetIndices,
57  const std::vector<libdap::dods_uint64> &dataStareIndices);
58 unsigned int count(const std::vector<libdap::dods_uint64> &target_indices,
59  const std:: vector<libdap::dods_uint64> &dataset_indices, bool all_target_matches = false);
60 
61 template <class T>
62 void stare_subset_array_helper(vector<T> &result_data, const vector<T> &src_data,
63  const vector<libdap::dods_uint64> &target_indices,
64  const vector<libdap::dods_uint64> &dataset_indices);
65 #if 0
67 struct point {
68  libdap::dods_int32 x;
69  libdap::dods_int32 y;
70 
71  point(int x, int y): x(x), y(y) {}
72  friend std::ostream & operator << (std::ostream &out, const point &c);
73 };
74 
76 struct stare_match {
77  point coord;
78  libdap::dods_uint64 stare_index;
79 
80  stare_match(const point &p, libdap::dods_uint64 si): coord(p), stare_index(si) {}
81  stare_match(int x, int y, libdap::dods_uint64 si): coord(x, y), stare_index(si) {}
82  friend std::ostream & operator << (std::ostream &out, const stare_match &m);
83 };
84 #endif
85 
87 struct stare_matches {
88  std::vector<libdap::dods_int32> x_indices;
89  std::vector<libdap::dods_int32> y_indices;
90 
91  std::vector<libdap::dods_uint64> stare_indices;
92  std::vector<libdap::dods_uint64> target_indices;
93 
94  // Pass by value and use move
95  stare_matches(std::vector<libdap::dods_int32> x, const std::vector<libdap::dods_int32> y,
96  const std::vector<libdap::dods_uint64> si, const std::vector<libdap::dods_uint64> ti)
97  : x_indices(std::move(x)), y_indices(std::move(y)), stare_indices(std::move(si)), target_indices(std::move(ti)) {}
98 
99  stare_matches() {}
100 
101  void add(libdap::dods_int32 x, libdap::dods_int32 y, libdap::dods_uint64 si, libdap::dods_uint64 ti) {
102  x_indices.push_back(x);
103  y_indices.push_back(y);
104  stare_indices.push_back(si);
105  target_indices.push_back(ti);
106  }
107 
108  friend std::ostream & operator << (std::ostream &out, const stare_matches &m);
109 };
110 
111 unique_ptr<stare_matches> stare_subset_helper(const std::vector<libdap::dods_uint64> &target_indices,
112  const std::vector<libdap::dods_uint64> &dataset_indices,
113  const std::vector<int> &dataset_x_coords, const std::vector<int> &dataset_y_coords);
114 
115 class StareIntersectionFunction : public libdap::ServerFunction {
116 public:
117  static libdap::BaseType *stare_intersection_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
118 
119  friend class StareFunctionsTest;
120 
121 public:
123  setName("stare_intersection");
124  setDescriptionString("The stare_intersection: Returns 1 if the coverage of the current dataset includes any of the given STARE indices, 0 otherwise.");
125  setUsageString("stare_intersection(var, STARE index [, STARE index ...]) | stare_intersection(var, $UInt64(<size hint>:STARE index [, STARE index ...]))");
126  setRole("http://services.opendap.org/dap4/server-side-function/stare_intersection");
127  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_intersection");
129  setVersion("0.3");
130  }
131 
132  virtual ~StareIntersectionFunction() {
133  }
134 };
135 
136 class StareCountFunction : public libdap::ServerFunction {
137 public:
138  static libdap::BaseType *stare_count_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
139 
140  friend class StareFunctionsTest;
141 
142 public:
144  setName("stare_count");
145  setDescriptionString("The stare_count: Returns the number of the STARE indices that are included in the coverage of this dataset.");
146  setUsageString("stare_count(var, STARE index [, STARE index ...]) | stare_count(var, $UInt64(<size hint>:STARE index [, STARE index ...]))");
147  setRole("http://services.opendap.org/dap4/server-side-function/stare_count");
148  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_count");
149  setFunction(stare_count_dap4_function);
150  setVersion("0.3");
151  }
152 
153  virtual ~StareCountFunction() {
154  }
155 };
156 
157 class StareSubsetFunction : public libdap::ServerFunction {
158 public:
159  static libdap::BaseType *stare_subset_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
160 
161  friend class StareFunctionsTest;
162 
163 public:
165  setName("stare_subset");
166  setDescriptionString("The stare_subset: Returns the set of the STARE indices that are included in the coverage of this dataset.");
167  setUsageString("stare_subset(var, $UInt64(<size hint>:STARE index [, STARE index ...]))");
168  setRole("http://services.opendap.org/dap4/server-side-function/stare_subset");
169  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_subset");
170  setFunction(stare_subset_dap4_function);
171  setVersion("0.3");
172  }
173 
174  virtual ~StareSubsetFunction() {
175  }
176 };
177 
178 class StareSubsetArrayFunction : public libdap::ServerFunction {
179 public:
180  static libdap::BaseType *stare_subset_array_dap4_function(libdap::D4RValueList *args, libdap::DMR &dmr);
181 
182  friend class StareFunctionsTest;
183 
184 public:
186  setName("stare_subset_array");
187  setDescriptionString("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.");
188  setUsageString("stare_subset_array(var, mask-val, $UInt64(<size hint>:STARE index [, STARE index ...]))");
189  setRole("http://services.opendap.org/dap4/server-side-function/stare_subset_array");
190  setDocUrl("http://docs.opendap.org/index.php/Server_Side_Processing_Functions#stare_subset_array");
191  setFunction(stare_subset_array_dap4_function);
192  setVersion("0.1");
193  }
194 
195  virtual ~StareSubsetArrayFunction() {
196  }
197 
198  template <class T>
199  static void build_masked_data(libdap::Array *dependent_var, const vector<libdap::dods_uint64> &dep_var_stare_indices,
200  const vector<libdap::dods_uint64> &target_s_indices, unique_ptr<libdap::Array> &result);
201 };
202 
203 } // 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.