bes  Updated for version 3.20.6
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  // Only variables that should be sent are in _maps. jhrg 11/3/16
110  vector<FONcMap *>::iterator i = _maps.begin();
111  vector<FONcMap *>::iterator e = _maps.end();
112  for (; i != e; i++) {
113  (*i)->define(ncid);
114  }
115 
116  // Only define if this should be sent. jhrg 11/3/16
117  if (_arr)
118  _arr->define(ncid);
119 
120  _defined = true;
121 
122  BESDEBUG("fonc", "FOncGrid::define - done defining grid " << _varname << endl);
123  }
124 }
125 
126 
142 void FONcGrid::convert(vector<string> embed)
143 {
144  FONcGrid::InGrid = true;
145  FONcBaseType::convert(embed);
146  _varname = FONcUtils::gen_name(embed, _varname, _orig_varname);
147  BESDEBUG("fonc", "FONcGrid::convert - converting grid " << _varname << endl);
148 
149  // A grid has maps, which are single dimnension arrays, and an array
150  // with that many maps for dimensions.
151  Grid::Map_iter mi = _grid->map_begin();
152  Grid::Map_iter me = _grid->map_end();
153  for (; mi != me; mi++) {
154 
155  // Only add FONcBaseType instances to _maps if the Frid Map is
156  // supposed to be sent. See Hyrax-282. jhrg 11/3/16
157  if ((*mi)->send_p()) {
158 
159  Array *map = dynamic_cast<Array *>((*mi));
160  if (!map) {
161  string err = (string) "file out netcdf, grid " + _varname + " map is not an array";
162  throw BESInternalError(err, __FILE__, __LINE__);
163  }
164 
165  vector<string> map_embed;
166 
167  FONcMap *map_found = FONcGrid::InMaps(map);
168 
169  // if we didn't find a match then found is still false. Add the
170  // map to the vector of maps. If they are the same then create a
171  // new FONcMap, add the grid name to the shared list and add the
172  // FONcMap to the FONcGrid.
173  if (!map_found) {
174  FONcArray *fa = new FONcArray(map);
175  fa->convert(map_embed);
176  map_found = new FONcMap(fa, true);
177  FONcGrid::Maps.push_back(map_found);
178  }
179  else {
180  // it's the same ... we are sharing. Add the grid name fo
181  // the list of grids sharing this map and set the embedded
182  // name to empty, just using the name of the map.
183  map_found->incref();
184  map_found->add_grid(_varname);
185  map_found->clear_embedded();
186  }
187  _maps.push_back(map_found);
188 
189  }
190  }
191 
192  // Only set _arr if the Grid Array should be sent. See Hyrax-282.
193  // jhrg 11/3/16
194  if (_grid->get_array()->send_p()) {
195  _arr = new FONcArray(_grid->get_array());
196  _arr->convert(_embed);
197  }
198 
199  BESDEBUG("fonc", "FONcGrid::convert - done converting grid " << _varname << endl);
200  FONcGrid::InGrid = false;
201 }
202 
212 void FONcGrid::write(int ncid)
213 {
214  BESDEBUG("fonc", "FOncGrid::define - writing grid " << _varname << endl);
215 
216  // FONcBaseType instances are added only if the corresponding DAP variable
217  // should be sent. See Hyrax-282. jhrg 11/3/16
218  vector<FONcMap *>::iterator i = _maps.begin();
219  vector<FONcMap *>::iterator e = _maps.end();
220  for (; i != e; i++) {
221  (*i)->write(ncid);
222  }
223 
224  // only write this if is have been convert()ed and define()ed.
225  // See above and Hyrax-282. jhrg 11/3/16
226  if (_arr)
227  _arr->write(ncid);
228 
229  _defined = true;
230 
231  BESDEBUG("fonc", "FOncGrid::define - done writing grid " << _varname << endl);
232 }
233 
239 {
240  return _grid->name();
241 }
242 
251 void FONcGrid::dump(ostream &strm) const
252 {
253  strm << BESIndent::LMarg << "FONcGrid::dump - (" << (void *) this << ")" << endl;
254  BESIndent::Indent();
255  strm << BESIndent::LMarg << "name = " << _grid->name() << " { " << endl;
256  BESIndent::Indent();
257  strm << BESIndent::LMarg << "maps:";
258  if (_maps.size()) {
259  strm << endl;
260  BESIndent::Indent();
261  vector<FONcMap *>::const_iterator i = _maps.begin();
262  vector<FONcMap *>::const_iterator e = _maps.end();
263  for (; i != e; i++) {
264  FONcMap *m = (*i);
265  m->dump(strm);
266  }
267  BESIndent::UnIndent();
268  }
269  else {
270  strm << " empty" << endl;
271  }
272  BESIndent::UnIndent();
273  strm << BESIndent::LMarg << "}" << endl;
274  strm << BESIndent::LMarg << "array:";
275  if (_arr) {
276  strm << endl;
277  BESIndent::Indent();
278  _arr->dump(strm);
279  BESIndent::UnIndent();
280  }
281  else {
282  strm << " not set" << endl;
283  }
284  BESIndent::UnIndent();
285 }
286 
287 FONcMap *
288 FONcGrid::InMaps(Array *array)
289 {
290  bool found = false;
291  vector<FONcMap *>::iterator vi = FONcGrid::Maps.begin();
292  vector<FONcMap *>::iterator ve = FONcGrid::Maps.end();
293  FONcMap *map_found = 0;
294  for (; vi != ve && !found; vi++) {
295  map_found = (*vi);
296  if (!map_found) {
297  throw BESInternalError("map_found is null.", __FILE__, __LINE__);
298  }
299  found = map_found->compare(array);
300  }
301  if (!found) {
302  map_found = 0;
303  }
304  return map_found;
305 }
306 
FONcArray
A DAP Array with file out netcdf information included.
Definition: FONcArray.h:54
FONcGrid::FONcGrid
FONcGrid(BaseType *b)
Constructor for FONcGrid that takes a DAP Grid.
Definition: FONcGrid.cc:60
FONcUtils::gen_name
static string gen_name(const vector< string > &embed, const string &name, string &original)
generate a new name for the embedded variable
Definition: FONcUtils.cc:148
FONcMap
A map of a DAP Grid with file out netcdf information included.
Definition: FONcMap.h:52
FONcArray::write
virtual void write(int ncid)
Write the array out to the netcdf file.
Definition: FONcArray.cc:366
FONcGrid::convert
virtual void convert(vector< string > embed)
convert the DAP Grid to a set of embedded variables
Definition: FONcGrid.cc:142
FONcGrid::name
virtual string name()
returns the name of the DAP Grid
Definition: FONcGrid.cc:238
FONcGrid::Maps
static vector< FONcMap * > Maps
global list of maps that could be shared amongst the different grids
Definition: FONcGrid.h:74
FONcMap::add_grid
virtual void add_grid(const std::string &name)
Add the name of the grid as a grid that uses this map.
Definition: FONcMap.cc:280
FONcMap::dump
virtual void dump(std::ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcMap.cc:328
FONcMap::compare
virtual bool compare(libdap::Array *arr)
a method to compare two grid maps, or possible grid maps.
Definition: FONcMap.cc:105
FONcGrid::~FONcGrid
virtual ~FONcGrid()
Destructor that cleans up the grid.
Definition: FONcGrid.cc:78
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
FONcBaseType
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:58
FONcGrid::write
virtual void write(int ncid)
Write the maps and array for the grid.
Definition: FONcGrid.cc:212
FONcGrid::dump
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcGrid.cc:251
FONcArray::convert
virtual void convert(std::vector< std::string > embed)
Converts the DAP Array to a FONcArray.
Definition: FONcArray.cc:107
FONcArray::define
virtual void define(int ncid)
define the DAP Array in the netcdf file
Definition: FONcArray.cc:272
FONcMap::clear_embedded
virtual void clear_embedded()
clear the embedded names for the FONcArray kept by this instance
Definition: FONcMap.cc:289
FONcGrid::InGrid
static bool InGrid
tells whether we are converting or defining a grid.
Definition: FONcGrid.h:76
FONcGrid::define
virtual void define(int ncid)
define the DAP Grid in the netcdf file
Definition: FONcGrid.cc:104
FONcArray::dump
virtual void dump(std::ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcArray.cc:549