/[escript]/trunk/escript/src/DataTagged.cpp
ViewVC logotype

Annotation of /trunk/escript/src/DataTagged.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1023 - (hide annotations)
Tue Mar 13 05:43:36 2007 UTC (12 years, 8 months ago) by gross
File size: 23143 byte(s)
netCDF can now be switched off at compilation. Consequently load and dump of data objects is not available then.
1 jgs 82 // $Id$
2 jgs 121
3 jgs 82 /*
4 elspeth 615 ************************************************************
5     * Copyright 2006 by ACcESS MNRF *
6     * *
7     * http://www.access.edu.au *
8     * Primary Business: Queensland, Australia *
9     * Licensed under the Open Software License version 3.0 *
10     * http://www.opensource.org/licenses/osl-3.0.php *
11     * *
12     ************************************************************
13 jgs 82 */
14    
15 jgs 474 #include "DataTagged.h"
16 jgs 496
17 jgs 474 #include "DataConstant.h"
18     #include "DataException.h"
19 gross 1023 #ifdef USE_NETCDF
20 gross 983 #include <netcdfcpp.h>
21 gross 1023 #endif
22 jgs 82
23     using namespace std;
24    
25     namespace escript {
26    
27 jgs 121 DataTagged::DataTagged()
28     : DataAbstract(FunctionSpace())
29 jgs 82 {
30 jgs 509 // default constructor
31    
32 jgs 82 // create a scalar default value
33 jgs 151 m_data.resize(1,0.,1);
34 jgs 82 DataArrayView temp(m_data,DataArrayView::ShapeType());
35     setPointDataView(temp);
36     }
37    
38     DataTagged::DataTagged(const TagListType& tagKeys,
39     const ValueListType& values,
40     const DataArrayView& defaultValue,
41 jgs 102 const FunctionSpace& what)
42     : DataAbstract(what)
43 jgs 82 {
44 jgs 509 // constructor
45    
46 jgs 121 // initialise the array of data values
47     // the default value is always the first item in the values list
48 jgs 151 int len = defaultValue.noValues();
49     m_data.resize(len,0.,len);
50 jgs 121 for (int i=0; i<defaultValue.noValues(); i++) {
51     m_data[i]=defaultValue.getData(i);
52     }
53    
54 jgs 119 // create the data view
55 jgs 82 DataArrayView temp(m_data,defaultValue.getShape());
56     setPointDataView(temp);
57 jgs 121
58 jgs 82 // add remaining tags and values
59     addTaggedValues(tagKeys,values);
60     }
61    
62 jgs 119 DataTagged::DataTagged(const FunctionSpace& what,
63     const DataArrayView::ShapeType &shape,
64     const int tags[],
65 jgs 500 const ValueType& data)
66 jgs 119 : DataAbstract(what)
67     {
68 jgs 509 // alternative constructor
69     // not unit_tested tested yet
70    
71 jgs 121 // copy the data
72 jgs 119 m_data=data;
73 jgs 121
74 jgs 119 // create the view of the data
75     DataArrayView tempView(m_data,shape);
76     setPointDataView(tempView);
77 jgs 121
78 jgs 119 // create the tag lookup map
79     for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
80     m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
81     }
82     }
83    
84 woo409 757 DataTagged::DataTagged(const FunctionSpace& what,
85     const DataArrayView::ShapeType &shape,
86     const TagListType& tags,
87     const ValueType& data)
88     : DataAbstract(what)
89     {
90     // alternative constructor
91     // not unit_tested tested yet
92    
93     // copy the data
94     m_data=data;
95    
96     // create the view of the data
97     DataArrayView tempView(m_data,shape);
98     setPointDataView(tempView);
99    
100     // create the tag lookup map
101     for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
102     m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
103     }
104     }
105    
106    
107 jgs 102 DataTagged::DataTagged(const DataTagged& other)
108     : DataAbstract(other.getFunctionSpace()),
109 jgs 82 m_data(other.m_data),
110     m_offsetLookup(other.m_offsetLookup)
111     {
112 jgs 509 // copy constructor
113    
114 jgs 119 // create the data view
115 jgs 82 DataArrayView temp(m_data,other.getPointDataView().getShape());
116     setPointDataView(temp);
117     }
118    
119 jgs 102 DataTagged::DataTagged(const DataConstant& other)
120     : DataAbstract(other.getFunctionSpace())
121 jgs 82 {
122 jgs 509 // copy constructor
123    
124 jgs 121 // fill the default value with the constant value item from "other"
125 jgs 82 const DataArrayView& value=other.getPointDataView();
126 jgs 151 int len = value.noValues();
127     m_data.resize(len,0.,len);
128 jgs 121 for (int i=0; i<value.noValues(); i++) {
129     m_data[i]=value.getData(i);
130     }
131    
132 jgs 119 // create the data view
133 jgs 82 DataArrayView temp(m_data,value.getShape());
134     setPointDataView(temp);
135     }
136    
137     DataAbstract*
138     DataTagged::getSlice(const DataArrayView::RegionType& region) const
139     {
140 jgs 513 return new DataTagged(*this, region);
141 jgs 82 }
142    
143 jgs 513 DataTagged::DataTagged(const DataTagged& other,
144     const DataArrayView::RegionType& region)
145     : DataAbstract(other.getFunctionSpace())
146     {
147     // slice constructor
148    
149     // get the shape of the slice to copy from other
150     DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
151     DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
152    
153     // allocate enough space in this for all values
154     // (need to add one to allow for the default value)
155     int len = DataArrayView::noValues(regionShape)*(other.m_offsetLookup.size()+1);
156     m_data.resize(len,0.0,len);
157    
158     // create the data view
159     DataArrayView temp(m_data,regionShape);
160     setPointDataView(temp);
161    
162     // copy the default value from other to this
163     getDefaultValue().copySlice(other.getDefaultValue(), regionLoopRange);
164    
165     // loop through the tag values copying these
166     DataMapType::const_iterator pos;
167     DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
168     for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
169     getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,regionLoopRange);
170     m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
171     tagOffset+=getPointDataView().noValues();
172     }
173     }
174    
175 jgs 82 void
176 jgs 513 DataTagged::setSlice(const DataAbstract* other,
177     const DataArrayView::RegionType& region)
178 jgs 82 {
179 jgs 513
180     // other must be another DataTagged object
181     // Data:setSlice implementation should ensure this
182     const DataTagged* otherTemp=dynamic_cast<const DataTagged*>(other);
183     if (otherTemp==0) {
184 jgs 82 throw DataException("Programming error - casting to DataTagged.");
185     }
186 jgs 121
187 jgs 513 // determine shape of the specified region
188     DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
189    
190     // modify region specification as needed to match rank of this object
191     DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
192    
193     // ensure rank/shape of this object is compatible with specified region
194 jgs 82 if (getPointDataView().getRank()!=region.size()) {
195     throw DataException("Error - Invalid slice region.");
196     }
197 woo409 757 if (otherTemp->getPointDataView().getRank()>0 && !other->getPointDataView().checkShape(regionShape)) {
198 jgs 513 throw DataException (other->getPointDataView().createShapeErrorMessage(
199     "Error - Couldn't copy slice due to shape mismatch.",regionShape));
200 jgs 82 }
201 jgs 121
202 jgs 513 // copy slice from other default value to this default value
203     getDefaultValue().copySliceFrom(otherTemp->getDefaultValue(), regionLoopRange);
204 jgs 121
205 jgs 541 // loop through tag values in other, adding any which aren't in this, using default value
206     DataMapType::const_iterator pos;
207     for (pos=otherTemp->m_offsetLookup.begin();pos!=otherTemp->m_offsetLookup.end();pos++) {
208     if (!isCurrentTag(pos->first)) {
209     addTaggedValue(pos->first,getDefaultValue());
210     }
211     }
212    
213 jgs 513 // loop through the tag values copying slices from other to this
214 jgs 121 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
215 jgs 513 getDataPointByTag(pos->first).copySliceFrom(otherTemp->getDataPointByTag(pos->first), regionLoopRange);
216 jgs 121 }
217 jgs 513
218 jgs 82 }
219    
220 jgs 149 int
221     DataTagged::getTagNumber(int dpno)
222     {
223     //
224     // Get the number of samples and data-points per sample
225     int numSamples = getNumSamples();
226     int numDataPointsPerSample = getNumDPPSample();
227     int numDataPoints = numSamples * numDataPointsPerSample;
228    
229     if (numDataPointsPerSample==0) {
230     throw DataException("DataTagged::getTagNumber error: no data-points associated with this object.");
231     }
232    
233 jgs 496 if (dpno<0 || dpno>numDataPoints-1) {
234 jgs 149 throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied.");
235     }
236    
237     //
238     // Determine the sample number which corresponds to this data-point number
239     int sampleNo = dpno / numDataPointsPerSample;
240    
241     //
242     // Determine the tag number which corresponds to this sample number
243     int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo);
244    
245     //
246     // return the tag number
247     return(tagNo);
248     }
249    
250 jgs 82 void
251 jgs 509 DataTagged::setTaggedValues(const TagListType& tagKeys,
252     const ValueListType& values)
253     {
254     addTaggedValues(tagKeys,values);
255     }
256    
257     void
258 jgs 82 DataTagged::setTaggedValue(int tagKey,
259     const DataArrayView& value)
260     {
261 jgs 121 if (!getPointDataView().checkShape(value.getShape())) {
262     throw DataException(getPointDataView().createShapeErrorMessage(
263     "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
264     }
265 jgs 82 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
266     if (pos==m_offsetLookup.end()) {
267 jgs 121 // tag couldn't be found so use addTaggedValue
268 jgs 82 addTaggedValue(tagKey,value);
269     } else {
270 jgs 121 // copy the values into the data array at the offset determined by m_offsetLookup
271     int offset=pos->second;
272     for (int i=0; i<getPointDataView().noValues(); i++) {
273     m_data[offset+i]=value.getData(i);
274 jgs 82 }
275     }
276     }
277    
278     void
279 jgs 509 DataTagged::addTaggedValues(const TagListType& tagKeys,
280     const ValueListType& values)
281     {
282     if (values.size()==0) {
283     // copy the current default value for each of the tags
284     TagListType::const_iterator iT;
285     for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
286     // the point data view for DataTagged points at the default value
287     addTaggedValue(*iT,getPointDataView());
288     }
289     } else if (values.size()==1 && tagKeys.size()>1) {
290     // assume the one given value will be used for all tag values
291     TagListType::const_iterator iT;
292     for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
293     addTaggedValue(*iT,values[0]);
294     }
295     } else {
296     if (tagKeys.size()!=values.size()) {
297     stringstream temp;
298     temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
299     << " doesn't match number of values: " << values.size();
300     throw DataException(temp.str());
301     } else {
302     for (int i=0;i<tagKeys.size();i++) {
303     addTaggedValue(tagKeys[i],values[i]);
304     }
305     }
306     }
307     }
308    
309     void
310 jgs 82 DataTagged::addTaggedValue(int tagKey,
311     const DataArrayView& value)
312     {
313     if (!getPointDataView().checkShape(value.getShape())) {
314     throw DataException(getPointDataView().createShapeErrorMessage(
315 jgs 121 "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
316 jgs 82 }
317 jgs 121 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
318     if (pos!=m_offsetLookup.end()) {
319     // tag already exists so use setTaggedValue
320     setTaggedValue(tagKey,value);
321     } else {
322     // save the key and the location of its data in the lookup tab
323     m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size()));
324     // add the data given in "value" at the end of m_data
325     // need to make a temp copy of m_data, resize m_data, then copy
326     // all the old values plus the value to be added back into m_data
327     ValueType m_data_temp(m_data);
328     int oldSize=m_data.size();
329     int newSize=m_data.size()+value.noValues();
330 jgs 151 m_data.resize(newSize,0.,newSize);
331 jgs 121 for (int i=0;i<oldSize;i++) {
332     m_data[i]=m_data_temp[i];
333     }
334     for (int i=0;i<value.noValues();i++) {
335     m_data[oldSize+i]=value.getData(i);
336     }
337     }
338 jgs 82 }
339    
340     double*
341     DataTagged::getSampleDataByTag(int tag)
342     {
343     DataMapType::iterator pos(m_offsetLookup.find(tag));
344     if (pos==m_offsetLookup.end()) {
345     // tag couldn't be found so return the default value
346     return &(m_data[0]);
347     } else {
348     // return the data-point corresponding to the given tag
349     return &(m_data[pos->second]);
350     }
351     }
352    
353     string
354     DataTagged::toString() const
355     {
356     stringstream temp;
357     DataMapType::const_iterator i;
358     temp << "Tag(Default)" << endl;
359     temp << getDefaultValue().toString() << endl;
360     // create a temporary view as the offset will be changed
361     DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
362     for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
363     temp << "Tag(" << i->first << ")" << endl;
364     tempView.setOffset(i->second);
365     temp << tempView.toString() << endl;
366     }
367     return temp.str();
368     }
369    
370 jgs 496 DataArrayView::ValueType::size_type
371     DataTagged::getPointOffset(int sampleNo,
372     int dataPointNo) const
373 jgs 82 {
374 jgs 496 int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
375     DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
376 jgs 82 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
377     if (pos!=m_offsetLookup.end()) {
378     offset=pos->second;
379     }
380 jgs 496 return offset;
381 jgs 82 }
382    
383 jgs 496 DataArrayView
384     DataTagged::getDataPointByTag(int tag) const
385 jgs 82 {
386 jgs 496 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
387 jgs 82 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
388     if (pos!=m_offsetLookup.end()) {
389     offset=pos->second;
390     }
391 jgs 496 DataArrayView temp(getPointDataView());
392     temp.setOffset(offset);
393     return temp;
394 jgs 82 }
395    
396     DataArrayView
397     DataTagged::getDataPoint(int sampleNo,
398     int dataPointNo)
399     {
400     EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo);
401     int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
402     return getDataPointByTag(tagKey);
403     }
404    
405 jgs 123 int
406     DataTagged::archiveData(ofstream& archiveFile,
407     const DataArrayView::ValueType::size_type noValues) const
408     {
409     return(m_data.archiveData(archiveFile, noValues));
410     }
411    
412     int
413     DataTagged::extractData(ifstream& archiveFile,
414     const DataArrayView::ValueType::size_type noValues)
415     {
416     return(m_data.extractData(archiveFile, noValues));
417     }
418 gross 580 void
419 ksteube 775 DataTagged::symmetric(DataAbstract* ev)
420     {
421     DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
422     if (temp_ev==0) {
423     throw DataException("Error - DataTagged::symmetric casting to DataTagged failed (probably a programming error).");
424     }
425     const DataTagged::DataMapType& thisLookup=getTagLookup();
426     DataTagged::DataMapType::const_iterator i;
427     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
428     for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
429     temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
430     DataArrayView thisView=getDataPointByTag(i->first);
431     DataArrayView evView=temp_ev->getDataPointByTag(i->first);
432     DataArrayView::symmetric(thisView,0,evView,0);
433     }
434     DataArrayView::symmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
435     }
436     void
437     DataTagged::nonsymmetric(DataAbstract* ev)
438     {
439     DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
440     if (temp_ev==0) {
441     throw DataException("Error - DataTagged::nonsymmetric casting to DataTagged failed (probably a programming error).");
442     }
443     const DataTagged::DataMapType& thisLookup=getTagLookup();
444     DataTagged::DataMapType::const_iterator i;
445     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
446     for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
447     temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
448     DataArrayView thisView=getDataPointByTag(i->first);
449     DataArrayView evView=temp_ev->getDataPointByTag(i->first);
450     DataArrayView::nonsymmetric(thisView,0,evView,0);
451     }
452     DataArrayView::nonsymmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
453     }
454     void
455 gross 800 DataTagged::trace(DataAbstract* ev, int axis_offset)
456 ksteube 775 {
457     DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
458     if (temp_ev==0) {
459 gross 800 throw DataException("Error - DataTagged::trace casting to DataTagged failed (probably a programming error).");
460 ksteube 775 }
461     const DataTagged::DataMapType& thisLookup=getTagLookup();
462     DataTagged::DataMapType::const_iterator i;
463     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
464     for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
465     temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
466     DataArrayView thisView=getDataPointByTag(i->first);
467     DataArrayView evView=temp_ev->getDataPointByTag(i->first);
468 gross 800 DataArrayView::trace(thisView,0,evView,0, axis_offset);
469 ksteube 775 }
470 gross 800 DataArrayView::trace(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
471 ksteube 775 }
472 gross 800
473 ksteube 775 void
474     DataTagged::transpose(DataAbstract* ev, int axis_offset)
475     {
476     DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
477     if (temp_ev==0) {
478     throw DataException("Error - DataTagged::transpose casting to DataTagged failed (probably a programming error).");
479     }
480     const DataTagged::DataMapType& thisLookup=getTagLookup();
481     DataTagged::DataMapType::const_iterator i;
482     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
483     for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
484     temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
485     DataArrayView thisView=getDataPointByTag(i->first);
486     DataArrayView evView=temp_ev->getDataPointByTag(i->first);
487     DataArrayView::transpose(thisView,0,evView,0, axis_offset);
488     }
489     DataArrayView::transpose(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
490     }
491 gross 800
492 ksteube 775 void
493 gross 804 DataTagged::swapaxes(DataAbstract* ev, int axis0, int axis1)
494 gross 800 {
495     DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
496     if (temp_ev==0) {
497 gross 804 throw DataException("Error - DataTagged::swapaxes casting to DataTagged failed (probably a programming error).");
498 gross 800 }
499     const DataTagged::DataMapType& thisLookup=getTagLookup();
500     DataTagged::DataMapType::const_iterator i;
501     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
502     for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
503     temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
504     DataArrayView thisView=getDataPointByTag(i->first);
505     DataArrayView evView=temp_ev->getDataPointByTag(i->first);
506 gross 804 DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
507 gross 800 }
508 gross 804 DataArrayView::swapaxes(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis0,axis1);
509 gross 800 }
510    
511     void
512 gross 580 DataTagged::eigenvalues(DataAbstract* ev)
513     {
514     DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
515     if (temp_ev==0) {
516     throw DataException("Error - DataTagged::eigenvalues casting to DataTagged failed (propably a programming error).");
517     }
518     const DataTagged::DataMapType& thisLookup=getTagLookup();
519     DataTagged::DataMapType::const_iterator i;
520     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
521     for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
522     temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
523     DataArrayView thisView=getDataPointByTag(i->first);
524     DataArrayView evView=temp_ev->getDataPointByTag(i->first);
525     DataArrayView::eigenvalues(thisView,0,evView,0);
526     }
527     DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
528     }
529     void
530     DataTagged::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
531     {
532     DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
533     if (temp_ev==0) {
534     throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
535     }
536     DataTagged* temp_V=dynamic_cast<DataTagged*>(V);
537     if (temp_V==0) {
538     throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
539     }
540     const DataTagged::DataMapType& thisLookup=getTagLookup();
541     DataTagged::DataMapType::const_iterator i;
542     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
543     for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
544     temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
545     temp_V->addTaggedValue(i->first,temp_V->getDefaultValue());
546     DataArrayView thisView=getDataPointByTag(i->first);
547     DataArrayView evView=temp_ev->getDataPointByTag(i->first);
548     DataArrayView VView=temp_V->getDataPointByTag(i->first);
549     DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
550     }
551     DataArrayView::eigenvalues_and_eigenvectors(getDefaultValue(),0,
552     temp_ev->getDefaultValue(),0,
553     temp_V->getDefaultValue(),0,
554     tol);
555 jgs 123
556 gross 580
557     }
558    
559 gross 950 void
560     DataTagged::dump(const std::string fileName) const
561     {
562 gross 1023 #ifdef PASO_MPI
563     throw DataException("Error - DataTagged:: dump is not implemented for MPI yet.")
564     #endif
565     #ifdef USE_NETCDF
566 gross 983 const int ldims=DataArrayView::maxRank+1;
567     const NcDim* ncdims[ldims];
568     NcVar *var, *tags_var;
569     int rank = getPointDataView().getRank();
570     int type= getFunctionSpace().getTypeCode();
571     int ndims =0;
572     long dims[ldims];
573     DataArrayView::ShapeType shape = getPointDataView().getShape();
574    
575     // netCDF error handler
576     NcError err(NcError::verbose_nonfatal);
577     // Create the file.
578     NcFile dataFile(fileName.c_str(), NcFile::Replace);
579     // check if writing was successful
580     if (!dataFile.is_valid())
581     throw DataException("Error - DataTagged:: opening of netCDF file for output failed.");
582     if (!dataFile.add_att("type","tagged") )
583     throw DataException("Error - DataTagged:: appending data type to netCDF file failed.");
584     if (!dataFile.add_att("rank",rank) )
585     throw DataException("Error - DataTagged:: appending rank attribute to netCDF file failed.");
586     if (!dataFile.add_att("function_space_type",type))
587     throw DataException("Error - DataTagged:: appending function space attribute to netCDF file failed.");
588     ndims=rank+1;
589     if ( rank >0 ) {
590     dims[0]=shape[0];
591     if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
592     throw DataException("Error - DataTagged:: appending ncdimsion 0 to netCDF file failed.");
593     }
594     if ( rank >1 ) {
595     dims[1]=shape[1];
596     if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
597     throw DataException("Error - DataTagged:: appending ncdimsion 1 to netCDF file failed.");
598     }
599     if ( rank >2 ) {
600     dims[2]=shape[2];
601     if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
602     throw DataException("Error - DataTagged:: appending ncdimsion 2 to netCDF file failed.");
603     }
604     if ( rank >3 ) {
605     dims[3]=shape[3];
606     if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
607     throw DataException("Error - DataTagged:: appending ncdimsion 3 to netCDF file failed.");
608     }
609     const DataTagged::DataMapType& thisLookup=getTagLookup();
610     DataTagged::DataMapType::const_iterator i;
611     DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
612     int ntags=1;
613     for (i=thisLookup.begin();i!=thisLookupEnd;i++) ntags++;
614 phornby 1020 int* tags =(int*) malloc(ntags*sizeof(int));
615     int c=1;
616 gross 983 tags[0]=-1;
617     for (i=thisLookup.begin();i!=thisLookupEnd;i++) tags[c++]=i->first;
618     dims[rank]=ntags;
619     if (! (ncdims[rank] = dataFile.add_dim("num_tags", dims[rank])) )
620 phornby 1020 {
621     free(tags);
622     throw DataException("Error - DataTagged:: appending num_tags to netCDF file failed.");
623     }
624 gross 983 if (! ( tags_var = dataFile.add_var("tags", ncInt, ncdims[rank])) )
625 phornby 1020 {
626     free(tags);
627 gross 983 throw DataException("Error - DataTagged:: appending tags to netCDF file failed.");
628 phornby 1020 }
629 gross 983 if (! (tags_var->put(tags,dims[rank])) )
630 phornby 1020 {
631     free(tags);
632 gross 983 throw DataException("Error - DataTagged:: copy tags to netCDF buffer failed.");
633 phornby 1020 }
634 gross 983 if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
635 phornby 1020 {
636     free(tags);
637 gross 983 throw DataException("Error - DataTagged:: appending variable to netCDF file failed.");
638 phornby 1020 }
639 gross 983 if (! (var->put(&m_data[0],dims)) )
640 phornby 1020 {
641     free(tags);
642 gross 983 throw DataException("Error - DataTagged:: copy data to netCDF buffer failed.");
643 phornby 1020 }
644 gross 1023 #else
645     throw DataException("Error - DataTagged:: dump is not configured with netCDF. Please contact your installation manager.");
646     #endif
647 gross 950 }
648 jgs 82 } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26