bes  Updated for version 3.20.6
NCMLBaseArray.cc
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 
30 #include <BaseType.h>
31 //#include "MyBaseTypeFactory.h"
32 #include "NCMLBaseArray.h"
33 #include "NCMLDebug.h"
34 #include "Shape.h"
35 
36 namespace ncml_module {
37 
38 #if 0
39 // I blocked this off because it's not being actively used and it's premise (massive data copying) is pretty much a
40 // problem for the handler if it is used. ndp 8/7/15
41 
55 auto_ptr< NCMLBaseArray >
56 NCMLBaseArray::createFromArray(const libdap::Array& protoC)
57 {
58  // The const in the signature means semantic const. We promise not to change protoC,
59  // but need a non-const reference to make calls to it, unfortunately.
60  libdap::Array& proto = const_cast<libdap::Array&>(protoC);
61 
62  BESDEBUG("ncml", "NCMLBaseArray::createFromArray(): Converting prototype Array name=" + proto.name() + " into an NCMLArray..." << endl);
63 
64  BaseType* pTemplate = proto.var();
65  NCML_ASSERT_MSG(pTemplate, "NCMLArray::createFromArray(): got NULL template BaseType var() for proto name=" + proto.name());
66 
67  // Factory up and test result
68  string ncmlArrayType = "Array<" + pTemplate->type_name() + ">";
69  auto_ptr<libdap::BaseType> pNewBT = MyBaseTypeFactory::makeVariable(ncmlArrayType, proto.name());
70  VALID_PTR(pNewBT.get());
71  auto_ptr< NCMLBaseArray > pNewArray = auto_ptr< NCMLBaseArray > (dynamic_cast< NCMLBaseArray*>(pNewBT.release()));
72  VALID_PTR(pNewArray.get());
73 
74  // Finally, we should be able to copy the data now.
75  pNewArray->copyDataFrom(proto);
76 
77  return pNewArray;// relinquish
78 }
79 #endif
80 
81 NCMLBaseArray::NCMLBaseArray() :
82  Array("", 0), _noConstraints(0), _currentConstraints(0)
83 {
84 
85 }
86 
87 NCMLBaseArray::NCMLBaseArray(const std::string& name) :
88  Array(name, 0), _noConstraints(0), _currentConstraints(0)
89 {
90 }
91 
92 NCMLBaseArray::NCMLBaseArray(const NCMLBaseArray& proto) :
93  Array(proto), _noConstraints(0), _currentConstraints(0)
94 {
95  copyLocalRepFrom(proto);
96 }
97 
98 NCMLBaseArray::~NCMLBaseArray()
99 {
100  destroy(); // local data
101 }
102 
103 NCMLBaseArray&
104 NCMLBaseArray::operator=(const NCMLBaseArray& rhs)
105 {
106  if (&rhs == this) {
107  return *this;
108  }
109 
110  // Call the super assignment
111  Array::operator=(rhs);
112 
113  // Copy local private rep
114  copyLocalRepFrom(rhs);
115 
116  return *this;
117 }
118 
120 {
121  // If we haven't computed constrained buffer yet, or they changed,
122  // we must call return false to force read() to be called again.
124 }
125 
126 void NCMLBaseArray::set_read_p(bool /* state */)
127 {
128  // Just drop it on the floor we compute it
129  // Array::set_read_p(state);
130 }
131 
133 {
134  BESDEBUG("ncml", "NCMLArray::read() called!" << endl);
135 
136  // If first call, cache the full dataset. Throw if there's an error with this.
138 
139  // If _currentConstraints is null or different than current Array dimensions,
140  // compute the constrained data buffer from the local data cache and the current Array dimensions.
142  // Enumerate and set the constrained values into Vector super..
144 
145  // Copy the constraints we used to generate these values
146  // so we know if we need to redo this in another call to read() or not.
148  }
149  return true;
150 }
151 
153 {
154  // make the Shape for our superclass Array
155  return Shape(*this);
156 }
157 
159 {
160  Shape superShape = getSuperShape();
161  return superShape.isConstrained();
162 }
163 
165 {
166  // If there's none, then they've changed by definition.
167  if (!_currentConstraints) {
168  return true;
169  }
170  else // compare the current values to those currently in our Array slice
171  {
172  return ((*_currentConstraints) != getSuperShape());
173  }
174 }
175 
177 {
178  // If got some already, blow them away...
179  if (_currentConstraints) {
180  delete _currentConstraints;
181  _currentConstraints = 0;
182  }
183  _currentConstraints = new Shape(*this);
184  //BESDEBUG("ncml", "NCMLBaseArray: Cached current constraints:" << (*_currentConstraints) << endl);
185 }
186 
187 void NCMLBaseArray::cacheUnconstrainedDimensions()
188 {
189  // We already got it...
190  if (_noConstraints) {
191  return;
192  }
193 
194  // Copy from the super Array's current dimensions and force values to define an unconstrained space.
195  _noConstraints = new Shape(*this);
196  _noConstraints->setToUnconstrained();
197 
198  //BESDEBUG("ncml", "NCMLBaseArray: cached unconstrained shape=" << (*_noConstraints) << endl);
199 }
200 
202 {
203  // We had better have a template or else the width() calls will be wrong.
204  NCML_ASSERT(var());
205 
206  // First call, make sure we grab unconstrained state.
207  if (!_noConstraints) {
208  cacheUnconstrainedDimensions();
209  }
210 
211  // Subclasses will handle this
213 }
214 
215 void NCMLBaseArray::copyLocalRepFrom(const NCMLBaseArray& proto)
216 {
217  // Avoid unnecessary finagling
218  if (&proto == this) {
219  return;
220  }
221 
222  // Blow away any old data before copying new
223  destroy();
224 
225  if (proto._noConstraints) {
226  _noConstraints = new Shape(*(proto._noConstraints));
227  }
228 
229  if (proto._currentConstraints) {
230  _currentConstraints = new Shape(*(proto._currentConstraints));
231  }
232 }
233 
235 void NCMLBaseArray::destroy() throw ()
236 {
237  delete _noConstraints;
238  _noConstraints = 0;
239  delete _currentConstraints;
240  _currentConstraints = 0;
241 }
242 
243 }
ncml_module::NCMLBaseArray::read_p
virtual bool read_p()
Definition: NCMLBaseArray.cc:119
ncml_module::NCMLBaseArray::createAndSetConstrainedValueBuffer
virtual void createAndSetConstrainedValueBuffer()=0
ncml_module::Shape::isConstrained
bool isConstrained() const
Definition: Shape.cc:96
ncml_module::NCMLBaseArray::read
virtual bool read()
Definition: NCMLBaseArray.cc:132
ncml_module::NCMLBaseArray::isConstrained
virtual bool isConstrained() const
Definition: NCMLBaseArray.cc:158
ncml_module::NCMLBaseArray::getSuperShape
virtual Shape getSuperShape() const
Definition: NCMLBaseArray.cc:152
ncml_module::Shape
A wrapper class for a vector of Array::dimension structs.
Definition: Shape.h:58
ncml_module::MyBaseTypeFactory::makeVariable
static std::auto_ptr< libdap::BaseType > makeVariable(const libdap::Type &type, const string &name)
ncml_module::NCMLBaseArray::cacheSuperclassStateIfNeeded
virtual void cacheSuperclassStateIfNeeded()
Definition: NCMLBaseArray.cc:201
ncml_module::NCMLBaseArray::haveConstraintsChangedSinceLastRead
virtual bool haveConstraintsChangedSinceLastRead() const
Definition: NCMLBaseArray.cc:164
ncml_module::NCMLBaseArray::set_read_p
virtual void set_read_p(bool state)
Definition: NCMLBaseArray.cc:126
ncml_module::Shape::setToUnconstrained
void setToUnconstrained()
Definition: Shape.cc:109
ncml_module::NCMLBaseArray::cacheValuesIfNeeded
virtual void cacheValuesIfNeeded()=0
ncml_module
NcML Parser for adding/modifying/removing metadata (attributes) to existing local datasets using NcML...
Definition: AggregationElement.cc:72
ncml_module::NCMLBaseArray
Definition: NCMLBaseArray.h:43
ncml_module::NCMLBaseArray::cacheCurrentConstraints
virtual void cacheCurrentConstraints()
Definition: NCMLBaseArray.cc:176