bes  Updated for version 3.20.10
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 
42 vector<FONcMap *> FONcGrid::Maps;
43 
50 bool FONcGrid::InGrid = false;
51 
60 FONcGrid::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 
104 void FONcGrid::define(int ncid)
105 {
106  if (!_defined) {
107  BESDEBUG("fonc", "FOncGrid::define - defining grid " << _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  _defined = true;
117 
118  BESDEBUG("fonc", "FOncGrid::define - done defining grid " << _varname << endl);
119  }
120 }
121 
122 
138 void 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: '" << _ncVersion << "'" << endl);
143  _varname = FONcUtils::gen_name(embed, _varname, _orig_varname);
144  BESDEBUG("fonc", "FONcGrid::convert - converting grid " << _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 " + _varname + " map is not an array";
159  throw BESInternalError(err, __FILE__, __LINE__);
160  }
161 
162  vector<string> map_embed;
163 
164  map->intern_data(*get_eval(), *get_dds());
165 
166  FONcMap *map_found = FONcGrid::InMaps(map);
167 
168  // if we didn't find a match then found is still false. Add the
169  // map to the vector of maps. If they are the same then create a
170  // new FONcMap, add the grid name to the shared list and add the
171  // FONcMap to the FONcGrid.
172  if (!map_found) {
173  FONcArray *fa = new FONcArray(map);
174  fa->setVersion(_ncVersion);
175  fa->setNC4DataModel(_nc4_datamodel);
176  fa->convert(map_embed, _dap4,is_dap4_group);
177  map_found = new FONcMap(fa, true);
178  FONcGrid::Maps.push_back(map_found);
179  }
180  else {
181  // it's the same ... we are sharing. Add the grid name fo
182  // the list of grids sharing this map and set the embedded
183  // name to empty, just using the name of the map.
184  map_found->incref();
185  map_found->add_grid(_varname);
186  map_found->clear_embedded();
187  }
188  _maps.push_back(map_found);
189 
190  }
191  }
192 
193  // Only set _arr if the Grid Array should be sent. See Hyrax-282.
194  // jhrg 11/3/16
195  if (_grid->get_array()->send_p()) {
196  _arr = new FONcArray(_grid->get_array());
197  _arr->setVersion(_ncVersion);
198  _arr->setNC4DataModel(_nc4_datamodel);
199 
200  _arr->convert(_embed, _dap4,is_dap4_group);
201  }
202 
203  BESDEBUG("fonc", "FONcGrid::convert - done converting grid " << _varname << endl);
204  FONcGrid::InGrid = false;
205 }
206 
216 void FONcGrid::write(int ncid)
217 {
218  BESDEBUG("fonc", "FOncGrid::define - writing grid " << _varname << endl);
219 
220  // FONcBaseType instances are added only if the corresponding DAP variable
221  // should be sent. See Hyrax-282. jhrg 11/3/16
222  vector<FONcMap *>::iterator i = _maps.begin();
223  vector<FONcMap *>::iterator e = _maps.end();
224  for (; i != e; i++) {
225  (*i)->write(ncid);
226  }
227 
228  // only write this if is have been convert()ed and define()ed.
229  // See above and Hyrax-282. jhrg 11/3/16
230  if (_arr)
231  _arr->write(ncid);
232 
233  _defined = true;
234 
235  BESDEBUG("fonc", "FOncGrid::define - done writing grid " << _varname << endl);
236 }
237 
243 {
244  return _grid->name();
245 }
246 
255 void FONcGrid::dump(ostream &strm) const
256 {
257  strm << BESIndent::LMarg << "FONcGrid::dump - (" << (void *) this << ")" << endl;
258  BESIndent::Indent();
259  strm << BESIndent::LMarg << "name = " << _grid->name() << " { " << endl;
260  BESIndent::Indent();
261  strm << BESIndent::LMarg << "maps:";
262  if (_maps.size()) {
263  strm << endl;
264  BESIndent::Indent();
265  vector<FONcMap *>::const_iterator i = _maps.begin();
266  vector<FONcMap *>::const_iterator e = _maps.end();
267  for (; i != e; i++) {
268  FONcMap *m = (*i);
269  m->dump(strm);
270  }
271  BESIndent::UnIndent();
272  }
273  else {
274  strm << " empty" << endl;
275  }
276  BESIndent::UnIndent();
277  strm << BESIndent::LMarg << "}" << endl;
278  strm << BESIndent::LMarg << "array:";
279  if (_arr) {
280  strm << endl;
281  BESIndent::Indent();
282  _arr->dump(strm);
283  BESIndent::UnIndent();
284  }
285  else {
286  strm << " not set" << endl;
287  }
288  BESIndent::UnIndent();
289 }
290 
291 FONcMap *
292 FONcGrid::InMaps(Array *array)
293 {
294  bool found = false;
295  vector<FONcMap *>::iterator vi = FONcGrid::Maps.begin();
296  vector<FONcMap *>::iterator ve = FONcGrid::Maps.end();
297  FONcMap *map_found = 0;
298  for (; vi != ve && !found; vi++) {
299  map_found = (*vi);
300  if (!map_found) {
301  throw BESInternalError("map_found is null.", __FILE__, __LINE__);
302  }
303  found = map_found->compare(array);
304  }
305  if (!found) {
306  map_found = 0;
307  }
308  return map_found;
309 }
310 
exception thrown if internal error encountered
A DAP Array with file out netcdf information included.
Definition: FONcArray.h:57
virtual void dump(std::ostream &strm) const override
dumps information about this object for debugging purposes
Definition: FONcArray.cc:754
virtual void define(int ncid) override
define the DAP Array in the netcdf file
Definition: FONcArray.cc:382
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:140
virtual void write(int ncid) override
Write the array out to the netcdf file.
Definition: FONcArray.cc:574
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:64
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 string name()
returns the name of the DAP Grid
Definition: FONcGrid.cc:242
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
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:216
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:255
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:179