bes  Updated for version 3.20.6
FONcMap.cc
1 // FONcMap.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 "config.h"
33 
34 #include <cstring>
35 
36 #include <Array.h>
37 
38 #include <BESDebug.h>
39 
40 #include "FONcArray.h"
41 #include "FONcMap.h"
42 #include "FONcUtils.h"
43 
57 FONcMap::FONcMap( FONcArray *a, bool ingrid )
58  : _arr( a ), _ingrid( ingrid ), _defined( false ), _ref( 1 )
59 {
60 }
61 
70 {
71  if( _ingrid )
72  {
73  delete _arr ;
74  _arr = 0 ;
75  }
76 }
77 
85 void
87 {
88  _ref-- ;
89  if( !_ref ) delete this ;
90 }
91 
105 bool FONcMap::compare(Array *tomap) {
106  bool isequal = true;
107 
108  Array *map = _arr->array();
109 
110  BESDEBUG( "fonc", "FONcMap::compare - comparing " << tomap->name()
111  << " to " << map->name() << endl );
112 
113  // compare the name
114  if (isequal && tomap->name() != map->name()) {
115  isequal = false;
116  }
117 
118  // compare the type
119  if (isequal && tomap->var()->type() != map->var()->type()) {
120  isequal = false;
121  }
122 
123  // compare the length of the array
124  if (isequal && tomap->length() != map->length()) {
125  isequal = false;
126  }
127 
128  // compare the number of dimensions
129  if (isequal && tomap->dimensions() != map->dimensions()) {
130  isequal = false;
131  }
132 
133  // the variable name needs to be the same as the dimension name
134  if (isequal && map->dimension_name(map->dim_begin()) != map->name()) {
135  isequal = false;
136  }
137 
138  // compare the dimension name
139  if (isequal && tomap->dimension_name(tomap->dim_begin()) != map->dimension_name(map->dim_begin())) {
140  isequal = false;
141  }
142 
143  // compare the dimension size. Is this the same as the length of the array
144  if (isequal && tomap->dimension_size(tomap->dim_begin(), true) != map->dimension_size(map->dim_begin(), true)) {
145  isequal = false;
146  }
147 
148  if (isequal) {
149  // compare the values of the array
150  char *map_buf = map->get_buf();
151  char *tomap_buf = tomap->get_buf();
152  int cmpres = memcmp(map_buf, tomap_buf, map->width());
153  if (0 != cmpres) {
154  isequal = false;
155  }
156  }
157 #if 0
158  switch (tomap->var()->type()) {
159  case dods_byte_c: {
160  dods_byte my_values[map->length()];
161  map->value(my_values);
162  dods_byte to_values[map->length()];
163  tomap->value(to_values);
164  for (int i = 0; i < map->length(); i++) {
165  if (my_values[i] != to_values[i]) {
166  isequal = false;
167  break;
168  }
169  }
170  }
171  break;
172  case dods_int16_c: {
173  dods_int16 my_values[map->length()];
174  map->value(my_values);
175  dods_int16 to_values[map->length()];
176  tomap->value(to_values);
177  for (int i = 0; i < map->length(); i++) {
178  if (my_values[i] != to_values[i]) {
179  isequal = false;
180  break;
181  }
182  }
183  }
184  break;
185  case dods_uint16_c: {
186  dods_uint16 my_values[map->length()];
187  map->value(my_values);
188  dods_uint16 to_values[map->length()];
189  tomap->value(to_values);
190  for (int i = 0; i < map->length(); i++) {
191  if (my_values[i] != to_values[i]) {
192  isequal = false;
193  break;
194  }
195  }
196  }
197  break;
198  case dods_int32_c: {
199  dods_int32 my_values[map->length()];
200  map->value(my_values);
201  dods_int32 to_values[map->length()];
202  tomap->value(to_values);
203  for (int i = 0; i < map->length(); i++) {
204  if (my_values[i] != to_values[i]) {
205  isequal = false;
206  break;
207  }
208  }
209  }
210  break;
211  case dods_uint32_c: {
212  dods_uint32 my_values[map->length()];
213  map->value(my_values);
214  dods_uint32 to_values[map->length()];
215  tomap->value(to_values);
216  for (int i = 0; i < map->length(); i++) {
217  if (my_values[i] != to_values[i]) {
218  isequal = false;
219  break;
220  }
221  }
222  }
223  break;
224  case dods_float32_c: {
225  dods_float32 my_values[map->length()];
226  map->value(my_values);
227  dods_float32 to_values[map->length()];
228  tomap->value(to_values);
229  for (int i = 0; i < map->length(); i++) {
230  if (my_values[i] != to_values[i]) {
231  isequal = false;
232  break;
233  }
234  }
235  }
236  break;
237  case dods_float64_c: {
238  dods_float64 my_values[map->length()];
239  map->value(my_values);
240  dods_float64 to_values[map->length()];
241  tomap->value(to_values);
242  for (int i = 0; i < map->length(); i++) {
243  if (my_values[i] != to_values[i]) {
244  isequal = false;
245  break;
246  }
247  }
248  }
249  break;
250  case dods_str_c:
251  case dods_url_c: {
252  vector<string> my_values;
253  map->value(my_values);
254  vector<string> to_values;
255  tomap->value(to_values);
256  vector<string>::const_iterator mi = my_values.begin();
257  vector<string>::const_iterator me = my_values.end();
258  vector<string>::const_iterator ti = to_values.begin();
259  for (; mi != me; mi++, ti++) {
260  if ((*mi) != (*ti)) {
261  isequal = false;
262  break;
263  }
264  }
265  }
266  break;
267  default: // Elide unknown types; this is the current behavior
268  // but I made it explicit. jhrg 12.27.2011
269  break;
270  }
271 #endif
272  BESDEBUG("fonc",
273  "FONcMap::compare - done comparing " << tomap->name() << " to " << map->name() << ": " << isequal << endl);
274  return isequal;
275 }
276 
279 void
280 FONcMap::add_grid( const string &name )
281 {
282  _shared_by.push_back( name );
283 }
284 
288 void
290 {
291  _arr->clear_embedded() ;
292 }
293 
299 void
300 FONcMap::define( int ncid )
301 {
302  if( !_defined )
303  {
304  _arr->define( ncid ) ;
305  _defined = true ;
306  }
307 }
308 
314 void
315 FONcMap::write( int ncid )
316 {
317  _arr->write( ncid ) ;
318 }
319 
327 void
328 FONcMap::dump( ostream &strm ) const
329 {
330  strm << BESIndent::LMarg << "FONcMap::dump - ("
331  << (void *)this << ")" << endl ;
332  BESIndent::Indent() ;
333  strm << BESIndent::LMarg << "array:" ;
334  if( _arr )
335  {
336  strm << endl ;
337  BESIndent::Indent() ;
338  _arr->dump( strm ) ;
339  BESIndent::UnIndent() ;
340  }
341  else
342  {
343  strm << " not set" << endl ;
344  }
345  strm << BESIndent::LMarg << "shared by: " ;
346  vector<string>::const_iterator i = _shared_by.begin() ;
347  vector<string>::const_iterator e = _shared_by.end() ;
348  bool first = true ;
349  for( ; i != e; i++ )
350  {
351  if( !first ) strm << ", " ;
352  strm << (*i) ;
353  first = false ;
354  }
355  strm << endl ;
356  BESIndent::UnIndent() ;
357 }
358 
FONcArray
A DAP Array with file out netcdf information included.
Definition: FONcArray.h:54
FONcArray::write
virtual void write(int ncid)
Write the array out to the netcdf file.
Definition: FONcArray.cc:366
FONcMap::~FONcMap
virtual ~FONcMap()
Destructor that cleans up the map.
Definition: FONcMap.cc:69
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
FONcBaseType::clear_embedded
virtual void clear_embedded()
Clears the list of embedded variable names.
Definition: FONcBaseType.cc:81
FONcMap::decref
virtual void decref()
decrements the reference count for this map
Definition: FONcMap.cc:86
FONcMap::dump
virtual void dump(std::ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcMap.cc:328
FONcMap::define
virtual void define(int ncid)
define the map in the netcdf file by calling define on the FONcArray
Definition: FONcMap.cc:300
FONcMap::compare
virtual bool compare(libdap::Array *arr)
a method to compare two grid maps, or possible grid maps.
Definition: FONcMap.cc:105
FONcMap::write
virtual void write(int ncid)
writes out the vallues of the map to the netcdf file by calling write on the FONcArray
Definition: FONcMap.cc:315
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
FONcArray::dump
virtual void dump(std::ostream &strm) const
dumps information about this object for debugging purposes
Definition: FONcArray.cc:549