bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
FONcGrid.cc
1// FONcGrid.cc
2
3// This file is part of BES Netcdf File Out Module
4
5// Copyright (c) 2004,2005 University Corporation for Atmospheric Research
6// Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21//
22// You can contact University Corporation for Atmospheric Research at
23// 3080 Center Green Drive, Boulder, CO 80301
24
25// (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
26// Please read the full copyright statement in the file COPYRIGHT_UCAR.
27//
28// Authors:
29// pwest Patrick West <pwest@ucar.edu>
30// jgarcia Jose Garcia <jgarcia@ucar.edu>
31
32#include <BESInternalError.h>
33#include <BESDebug.h>
34
35#include "FONcGrid.h"
36#include "FONcUtils.h"
37#include "FONcAttributes.h"
38
43
50bool FONcGrid::InGrid = false;
51
60FONcGrid::FONcGrid(BaseType *b) : FONcBaseType(), _grid(0), _arr(0)
61{
62 _grid = dynamic_cast<Grid *>(b);
63 if (!_grid) {
64 string s = (string) "File out netcdf, FONcGrid was passed a " + "variable that is not a DAP Grid";
65 throw BESInternalError(s, __FILE__, __LINE__);
66 }
67}
68
79{
80 vector<FONcMap *>::iterator i = _maps.begin();
81 while (i != _maps.end()) {
82 // These are the FONc types, not DAP types
83 (*i)->decref();
84 ++i;
85 }
86
87 // Added jhrg 8/28/13
88 delete _arr;
89}
90
104void FONcGrid::define(int ncid)
105{
106 if (!d_defined) {
107 BESDEBUG("fonc", "FOncGrid::define - defining grid " << d_varname << endl);
108
109 for (auto map: _maps)
110 map->define(ncid);
111
112 // Only define if this should be sent. jhrg 11/3/16
113 if (_arr)
114 _arr->define(ncid);
115
116 d_defined = true;
117
118 BESDEBUG("fonc", "FOncGrid::define - done defining grid " << d_varname << endl);
119 }
120}
121
122
138void FONcGrid::convert(vector<string> embed, bool _dap4, bool is_dap4_group)
139{
140 FONcGrid::InGrid = true;
141 FONcBaseType::convert(embed, _dap4, is_dap4_group);
142 BESDEBUG("fonc", "FONcGrid:: version: '" << d_ncVersion << "'" << endl);
143 d_varname = FONcUtils::gen_name(embed, d_varname, d_orig_varname);
144 BESDEBUG("fonc", "FONcGrid::convert - converting grid " << d_varname << endl);
145
146 // A grid has maps, which are single dimnension arrays, and an array
147 // with one dimension for each map.
148 Grid::Map_iter mi = _grid->map_begin();
149 Grid::Map_iter me = _grid->map_end();
150 for (; mi != me; mi++) {
151
152 // Only add FONcBaseType instances to _maps if the Frid Map is
153 // supposed to be sent. See Hyrax-282. jhrg 11/3/16
154 if ((*mi)->send_p()) {
155
156 Array *map = dynamic_cast<Array *>((*mi));
157 if (!map) {
158 string err = (string) "file out netcdf, grid " + d_varname + " map is not an array";
159 throw BESInternalError(err, __FILE__, __LINE__);
160 }
161
162 vector<string> map_embed;
163
164#if 0
165 // Patch for HYRAX-1334 jhrg 2/14/24
166 map->intern_data(*get_eval(), *get_dds());
167#endif
168 if (d_is_dap4 || get_eval() == nullptr || get_dds() == nullptr)
169 map->intern_data();
170 else
171 map->intern_data(*get_eval(), *get_dds());
172
173 FONcMap *map_found = FONcGrid::InMaps(map);
174
175 // if we didn't find a match then found is still false. Add the
176 // map to the vector of maps. If they are the same then create a
177 // new FONcMap, add the grid name to the shared list and add the
178 // FONcMap to the FONcGrid.
179 if (!map_found) {
180 FONcArray *fa = new FONcArray(map);
181 fa->setVersion(d_ncVersion);
182 fa->setNC4DataModel(d_nc4_datamodel);
183 fa->convert(map_embed, _dap4,is_dap4_group);
184 map_found = new FONcMap(fa, true);
185 FONcGrid::Maps.push_back(map_found);
186 }
187 else {
188 // it's the same ... we are sharing. Add the grid name fo
189 // the list of grids sharing this map and set the embedded
190 // name to empty, just using the name of the map.
191 map_found->incref();
192 map_found->add_grid(d_varname);
193 map_found->clear_embedded();
194 }
195 _maps.push_back(map_found);
196
197 }
198 }
199
200 // Only set _arr if the Grid Array should be sent. See Hyrax-282.
201 // jhrg 11/3/16
202 if (_grid->get_array()->send_p()) {
203 _arr = new FONcArray(_grid->get_array());
204 _arr->setVersion(d_ncVersion);
205 _arr->setNC4DataModel(d_nc4_datamodel);
206
207 _arr->convert(d_embed, _dap4, is_dap4_group);
208 }
209
210 BESDEBUG("fonc", "FONcGrid::convert - done converting grid " << d_varname << endl);
211 FONcGrid::InGrid = false;
212}
213
223void FONcGrid::write(int ncid)
224{
225 BESDEBUG("fonc", "FOncGrid::define - writing grid " << d_varname << endl);
226
227 // FONcBaseType instances are added only if the corresponding DAP variable
228 // should be sent. See Hyrax-282. jhrg 11/3/16
229 vector<FONcMap *>::iterator i = _maps.begin();
230 vector<FONcMap *>::iterator e = _maps.end();
231 for (; i != e; i++) {
232 (*i)->write(ncid);
233 }
234
235 // only write this if is have been convert()ed and define()ed.
236 // See above and Hyrax-282. jhrg 11/3/16
237 if (_arr)
238 _arr->write(ncid);
239
240 d_defined = true;
241
242 BESDEBUG("fonc", "FOncGrid::define - done writing grid " << d_varname << endl);
243}
244
250{
251 return _grid->name();
252}
253
262void FONcGrid::dump(ostream &strm) const
263{
264 strm << BESIndent::LMarg << "FONcGrid::dump - (" << (void *) this << ")" << endl;
265 BESIndent::Indent();
266 strm << BESIndent::LMarg << "name = " << _grid->name() << " { " << endl;
267 BESIndent::Indent();
268 strm << BESIndent::LMarg << "maps:";
269 if (_maps.size()) {
270 strm << endl;
271 BESIndent::Indent();
272 vector<FONcMap *>::const_iterator i = _maps.begin();
273 vector<FONcMap *>::const_iterator e = _maps.end();
274 for (; i != e; i++) {
275 FONcMap *m = (*i);
276 m->dump(strm);
277 }
278 BESIndent::UnIndent();
279 }
280 else {
281 strm << " empty" << endl;
282 }
283 BESIndent::UnIndent();
284 strm << BESIndent::LMarg << "}" << endl;
285 strm << BESIndent::LMarg << "array:";
286 if (_arr) {
287 strm << endl;
288 BESIndent::Indent();
289 _arr->dump(strm);
290 BESIndent::UnIndent();
291 }
292 else {
293 strm << " not set" << endl;
294 }
295 BESIndent::UnIndent();
296}
297
298FONcMap *
299FONcGrid::InMaps(Array *array)
300{
301 bool found = false;
304 FONcMap *map_found = 0;
305 for (; vi != ve && !found; vi++) {
306 map_found = (*vi);
307 if (!map_found) {
308 throw BESInternalError("map_found is null.", __FILE__, __LINE__);
309 }
310 found = map_found->compare(array);
311 }
312 if (!found) {
313 map_found = 0;
314 }
315 return map_found;
316}
317
exception thrown if internal error encountered
A DAP Array with file out netcdf information included.
Definition FONcArray.h:56
virtual void convert(std::vector< std::string > embed, bool _dap4=false, bool is_dap4_group=false) override
Converts the DAP Array to a FONcArray.
Definition FONcArray.cc:141
virtual void setNC4DataModel(const string &nc4_datamodel)
Identifies the netCDF4 data model (CLASSIC or ENHANCED)
virtual void setVersion(const std::string &version)
Identifies variable with use of NetCDF4 features.
FONcGrid(libdap::BaseType *b)
Constructor for FONcGrid that takes a DAP Grid.
Definition FONcGrid.cc:60
virtual ~FONcGrid()
Destructor that cleans up the grid.
Definition FONcGrid.cc:78
virtual void define(int ncid) override
define the DAP Grid in the netcdf file
Definition FONcGrid.cc:104
virtual void convert(vector< string > embed, bool _dap4, bool is_dap4_group) override
convert the DAP Grid to a set of embedded variables
Definition FONcGrid.cc:138
string name() override
returns the name of the DAP Grid
Definition FONcGrid.cc:249
static vector< FONcMap * > Maps
global list of maps that could be shared amongst the different grids
Definition FONcGrid.h:80
virtual void write(int ncid) override
Write the maps and array for the grid.
Definition FONcGrid.cc:223
static bool InGrid
tells whether we are converting or defining a grid.
Definition FONcGrid.h:82
virtual void dump(ostream &strm) const override
dumps information about this object for debugging purposes
Definition FONcGrid.cc:262
A map of a DAP Grid with file out netcdf information included.
Definition FONcMap.h:52
virtual void clear_embedded()
clear the embedded names for the FONcArray kept by this instance
Definition FONcMap.cc:179
virtual void dump(std::ostream &strm) const
dumps information about this object for debugging purposes
Definition FONcMap.cc:217
virtual void add_grid(const std::string &name)
Add the name of the grid as a grid that uses this map.
Definition FONcMap.cc:170
virtual bool compare(libdap::Array *arr)
a method to compare two grid maps, or possible grid maps.
Definition FONcMap.cc:104
static string gen_name(const vector< string > &embed, const string &name, string &original)
generate a new name for the embedded variable
Definition FONcUtils.cc:182
STL class.
STL class.
STL class.
STL iterator class.
STL iterator class.
STL class.