OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
FONcGrid.cc
Go to the documentation of this file.
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 )
61  : FONcBaseType(), _grid( 0 ), _arr( 0 )
62 {
63  _grid = dynamic_cast<Grid *>(b) ;
64  if( !_grid )
65  {
66  string s = (string)"File out netcdf, FONcGrid was passed a "
67  + "variable that is not a DAP Grid" ;
68  throw BESInternalError( s, __FILE__, __LINE__ ) ;
69  }
70 }
71 
82 {
83 #if 0
84  // Since this is a dtor and _maps is going to go away, no need
85  // to erase. jhrg 8/28/13
86  bool done = false;
87  while( !done )
88  {
89  vector<FONcMap *>::iterator i = _maps.begin();
90  vector<FONcMap *>::iterator e = _maps.end();
91  if( i == e )
92  {
93  done = true;
94  }
95  else
96  {
97  // These are the FONc types, not the actual ones
98  FONcMap *m = (*i);
99  m->decref();
100  _maps.erase( i );
101  }
102  }
103 #endif
104 
105  vector<FONcMap *>::iterator i = _maps.begin() ;
106  while( i != _maps.end() )
107  {
108  // These are the FONc types, not the actual ones
109  (*i)->decref() ;
110  ++i;
111  }
112 
113  // Added jhrg 8/28/13
114  delete _arr;
115 }
116 
132 void
133 FONcGrid::convert( vector<string> embed )
134 {
135  FONcGrid::InGrid = true ;
136  FONcBaseType::convert( embed ) ;
138  BESDEBUG( "fonc", "FONcGrid::convert - converting grid "
139  << _varname << endl ) ;
140 
141  // A grid has maps, which are single dimnension arrays, and an array
142  // with that many maps for dimensions.
143  Grid::Map_iter mi = _grid->map_begin() ;
144  Grid::Map_iter me = _grid->map_end() ;
145  for( ; mi != me; mi++ )
146  {
147  Array *map = dynamic_cast<Array *>( (*mi) ) ;
148  if( !map )
149  {
150  string err = (string)"file out netcdf, grid "
151  + _varname + " map is not an array" ;
152  throw BESInternalError( err, __FILE__, __LINE__ ) ;
153  }
154 
155  vector<string> map_embed ;
156 
157  FONcMap *map_found = FONcGrid::InMaps( map ) ;
158 
159  // if we didn't find a match then found is still false. Add the
160  // map to the vector of maps. If they are the same then create a
161  // new FONcMap, add the grid name to the shared list and add the
162  // FONcMap to the FONcGrid.
163  if( !map_found )
164  {
165  FONcArray *fa = new FONcArray( map ) ;
166  fa->convert( map_embed ) ;
167  map_found = new FONcMap( fa, true );
168  FONcGrid::Maps.push_back( map_found ) ;
169  }
170  else
171  {
172  // it's the same ... we are sharing. Add the grid name fo
173  // the list of grids sharing this map and set the embedded
174  // name to empty, just using the name of the map.
175  map_found->incref() ;
176  map_found->add_grid( _varname ) ;
177  map_found->clear_embedded() ;
178  }
179  _maps.push_back( map_found ) ;
180  }
181  _arr = new FONcArray( _grid->get_array() ) ;
182  _arr->convert( _embed ) ;
183 
184  BESDEBUG( "fonc", "FONcGrid::convert - done converting grid "
185  << _varname << endl ) ;
186  FONcGrid::InGrid = false ;
187 }
188 
202 void
203 FONcGrid::define( int ncid )
204 {
205  if( !_defined )
206  {
207  BESDEBUG( "fonc", "FOncGrid::define - defining grid "
208  << _varname << endl ) ;
209 
210  vector<FONcMap *>::iterator i = _maps.begin() ;
211  vector<FONcMap *>::iterator e = _maps.end() ;
212  for( ; i != e; i++ )
213  {
214  (*i)->define( ncid ) ;
215  }
216  _arr->define( ncid ) ;
217 
218  _defined = true ;
219 
220  BESDEBUG( "fonc", "FOncGrid::define - done defining grid "
221  << _varname << endl ) ;
222  }
223 }
224 
234 void
235 FONcGrid::write( int ncid )
236 {
237  BESDEBUG( "fonc", "FOncGrid::define - writing grid "
238  << _varname << endl ) ;
239 
240  vector<FONcMap *>::iterator i = _maps.begin() ;
241  vector<FONcMap *>::iterator e = _maps.end() ;
242  for( ; i != e; i++ )
243  {
244  (*i)->write( ncid ) ;
245  }
246  _arr->write( ncid ) ;
247 
248  _defined = true ;
249 
250  BESDEBUG( "fonc", "FOncGrid::define - done writing grid "
251  << _varname << endl ) ;
252 }
253 
258 string
260 {
261  return _grid->name() ;
262 }
263 
272 void
273 FONcGrid::dump( ostream &strm ) const
274 {
275  strm << BESIndent::LMarg << "FONcGrid::dump - ("
276  << (void *)this << ")" << endl ;
278  strm << BESIndent::LMarg << "name = " << _grid->name() << " { " << endl ;
280  strm << BESIndent::LMarg << "maps:" ;
281  if( _maps.size() )
282  {
283  strm << endl ;
285  vector<FONcMap *>::const_iterator i = _maps.begin() ;
286  vector<FONcMap *>::const_iterator e = _maps.end() ;
287  for( ; i != e; i++ )
288  {
289  FONcMap *m = (*i) ;
290  m->dump( strm ) ;
291  }
293  }
294  else
295  {
296  strm << " empty" << endl ;
297  }
299  strm << BESIndent::LMarg << "}" << endl ;
300  strm << BESIndent::LMarg << "array:" ;
301  if( _arr )
302  {
303  strm << endl ;
305  _arr->dump( strm ) ;
307  }
308  else
309  {
310  strm << " not set" << endl ;
311  }
313 }
314 
315 FONcMap *
316 FONcGrid::InMaps( Array *array )
317 {
318  bool found = false ;
319  vector<FONcMap *>::iterator vi = FONcGrid::Maps.begin() ;
320  vector<FONcMap *>::iterator ve = FONcGrid::Maps.end() ;
321  FONcMap *map_found = 0 ;
322  for( ; vi != ve && !found; vi++ )
323  {
324  map_found = (*vi) ;
325  if( !map_found )
326  {
327  throw BESInternalError( "map_found is null.", __FILE__, __LINE__ ) ;
328  }
329  found = map_found->compare( array ) ;
330  }
331  if( !found )
332  {
333  map_found = 0 ;
334  }
335  return map_found ;
336 }
337 
virtual void add_grid(const string &name)
Add the name of the grid as a grid that uses this map.
Definition: FONcMap.cc:306
A map of a DAP Grid with file out netcdf information included.
Definition: FONcMap.h:46
exception thrown if inernal error encountered
A DAP Array with file out netcdf information included.
Definition: FONcArray.h:50
FONcGrid(BaseType *b)
Constructor for FONcGrid that takes a DAP Grid.
Definition: FONcGrid.cc:60
virtual bool compare(Array *arr)
a method to compare two grid maps, or possible grid maps.
Definition: FONcMap.cc:99
virtual void write(int ncid)
Write the array out to the netcdf file.
Definition: FONcArray.cc:357
virtual ~FONcGrid()
Destructor that cleans up the grid.
Definition: FONcGrid.cc:81
static bool InGrid
tells whether we are converting or defining a grid.
Definition: FONcGrid.h:77
virtual void incref()
Definition: FONcMap.h:60
virtual void define(int ncid)
define the DAP Grid in the netcdf file
Definition: FONcGrid.cc:203
virtual void decref()
decrements the reference count for this map
Definition: FONcMap.cc:79
static void Indent()
Definition: BESIndent.cc:38
vector< string > _embed
Definition: FONcBaseType.h:54
string _orig_varname
Definition: FONcBaseType.h:53
virtual void convert(vector< string > embed)
convert the DAP Grid to a set of embedded variables
Definition: FONcGrid.cc:133
string _varname
Definition: FONcBaseType.h:52
static FONcMap * InMaps(Array *array)
Definition: FONcGrid.cc:316
virtual void write(int ncid)
Write the maps and array for the grid.
Definition: FONcGrid.cc:235
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
virtual void define(int ncid)
define the DAP Array in the netcdf file
Definition: FONcArray.cc:289
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcMap.cc:354
virtual void convert(vector< string > embed)
Converts the DAP Array to a FONcArray.
Definition: FONcArray.cc:130
A DAP BaseType with file out netcdf information included.
Definition: FONcBaseType.h:48
virtual void clear_embedded()
clear the embedded names for the FONcArray kept by this instance
Definition: FONcMap.cc:315
static string gen_name(const vector< string > &embed, const string &name, string &original)
generate a new name for the embedded variable
Definition: FONcUtils.cc:150
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
static void UnIndent()
Definition: BESIndent.cc:44
virtual string name()
returns the name of the DAP Grid
Definition: FONcGrid.cc:259
virtual void convert(vector< string > embed)
Definition: FONcBaseType.cc:41
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcGrid.cc:273
virtual void dump(ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcArray.cc:542
static vector< FONcMap * > Maps
global list of maps that could be shared amongst the different grids
Definition: FONcGrid.h:75