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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 950 - (hide annotations)
Tue Feb 6 07:01:11 2007 UTC (12 years, 2 months ago) by gross
File size: 7773 byte(s)
escript data objects can now be saved to netCDF files, see http://www.unidata.ucar.edu/software/netcdf/.
Currently only constant data are implemented with expanded and tagged data to follow.
There are two new functions to dump a data object

   s=Data(...)
   s.dump(<filename>)

and to recover it

   s=load(<filename>, domain)

Notice that the function space of s is recovered but domain is still need. 

dump and load will replace archive and extract.

The installation needs now the netCDF installed. 


1 jgs 82 // $Id$
2     /*
3 elspeth 615 ************************************************************
4     * Copyright 2006 by ACcESS MNRF *
5     * *
6     * http://www.access.edu.au *
7     * Primary Business: Queensland, Australia *
8     * Licensed under the Open Software License version 3.0 *
9     * http://www.opensource.org/licenses/osl-3.0.php *
10     * *
11     ************************************************************
12 jgs 82 */
13    
14 jgs 474 #include "DataFactory.h"
15 jgs 82
16     #include <boost/python/extract.hpp>
17 gross 950 #include <iostream>
18     #include <netcdfcpp.h>
19 jgs 82
20     using namespace boost::python;
21    
22     namespace escript {
23    
24     Data
25     Scalar(double value,
26     const FunctionSpace& what,
27     bool expanded)
28     {
29     //
30     // an empty shape is a scalar
31     DataArrayView::ShapeType shape;
32     return Data(value,shape,what,expanded);
33     }
34    
35     Data
36     Vector(double value,
37     const FunctionSpace& what,
38     bool expanded)
39     {
40     DataArrayView::ShapeType shape(1,what.getDomain().getDim());
41     return Data(value,shape,what,expanded);
42     }
43    
44     Data
45     Tensor(double value,
46     const FunctionSpace& what,
47     bool expanded)
48     {
49     DataArrayView::ShapeType shape(2,what.getDomain().getDim());
50     return Data(value,shape,what,expanded);
51     }
52    
53     Data
54     Tensor3(double value,
55     const FunctionSpace& what,
56     bool expanded)
57     {
58     DataArrayView::ShapeType shape(3,what.getDomain().getDim());
59     return Data(value,shape,what,expanded);
60     }
61    
62     Data
63     Tensor4(double value,
64     const FunctionSpace& what,
65     bool expanded)
66     {
67     DataArrayView::ShapeType shape(4,what.getDomain().getDim());
68     return Data(value,shape,what,expanded);
69     }
70    
71 gross 950 Data
72     load(const std::string fileName,
73     const AbstractDomain& domain)
74     {
75     #ifdef PASO_MPI
76     throw DataException("Error - DataConstant:: dump is not implemented for MPI yet.")
77     #endif
78     NcAtt *type_att, *rank_att, *function_space_type_att;
79     // netCDF error handler
80     NcError err(NcError::verbose_nonfatal);
81     // Create the file.
82     NcFile dataFile(fileName.c_str(), NcFile::ReadOnly);
83     if (!dataFile.is_valid())
84     throw DataException("Error - load:: opening of netCDF file for input failed.");
85     /* recover function space */
86     if (! (function_space_type_att=dataFile.get_att("function_space_type")) )
87     throw DataException("Error - load:: cannot recover function_space_type attribute from escript netCDF file.");
88     int function_space_type = function_space_type_att->as_int(0);
89     delete function_space_type_att;
90     /* test if function space id is valid and create function space instance */
91     if (! domain.isValidFunctionSpaceType(function_space_type) )
92     throw DataException("Error - load:: function space type code in netCDF file is invalid for given domain.");
93     FunctionSpace function_space=FunctionSpace(domain, function_space_type);
94     /* recover rank */
95     if (! (rank_att=dataFile.get_att("rank")) )
96     throw DataException("Error - load:: cannot recover rank attribute from escript netCDF file.");
97     int rank = rank_att->as_int(0);
98     delete rank_att;
99     if (rank<0 || rank>DataArrayView::maxRank)
100     throw DataException("Error - load:: rank in escript netCDF file is greater than maximum rank.");
101    
102     /* recover type attribute */
103     if (! (type_att=dataFile.get_att("type")) )
104     throw DataException("Error - load:: cannot recover type attribute from escript netCDF file.");
105     char* type_str = type_att->as_string(0);
106     int type=-1;
107     if (strncmp(type_str, "constant", strlen("constant")) == 0 ) {
108     type =0;
109     } else if (strncmp(type_str, "tagged", strlen("tagged")) == 0 ) {
110     type =1;
111     } else if (strncmp(type_str, "expanded", strlen("expanded")) == 0 ) {
112     type =2;
113     }
114     delete type_att;
115     delete type_str;
116     /* recover dimension */
117     int ndims=dataFile.num_dims();
118     int ntags =0 , nsamples =0 , ndata_points_per_sample =0, d=0;
119     NcDim *d_dim, *tags_dim, *samples_dim, *data_points_per_sample_dim;
120     /* recover shape */
121     DataArrayView::ShapeType shape;
122     long dims[DataArrayView::maxRank+2];
123     if (rank>0) {
124     if (! (d_dim=dataFile.get_dim("d0")) )
125     throw DataException("Error - load:: unable to recover d0 from netCDF file.");
126     d=d_dim->size();
127     shape.push_back(d);
128     dims[0]=d;
129     }
130     if (rank>1) {
131     if (! (d_dim=dataFile.get_dim("d1")) )
132     throw DataException("Error - load:: unable to recover d1 from netCDF file.");
133     d=d_dim->size();
134     shape.push_back(d);
135     dims[1]=d;
136     }
137     if (rank>2) {
138     if (! (d_dim=dataFile.get_dim("d2")) )
139     throw DataException("Error - load:: unable to recover d2 from netCDF file.");
140     d=d_dim->size();
141     shape.push_back(d);
142     dims[2]=d;
143     }
144     if (rank>3) {
145     if (! (d_dim=dataFile.get_dim("d3")) )
146     throw DataException("Error - load:: unable to recover d3 from netCDF file.");
147     d=d_dim->size();
148     shape.push_back(d);
149     dims[3]=d;
150     }
151     /* recover stuff */
152     Data out;
153     NcVar *var;
154     if (type == 0) {
155     /* constant data */
156     if ( ! ( (ndims == rank && rank >0) || ( ndims ==1 && rank == 0 ) ) )
157     throw DataException("Error - load:: illegal number of dimensions for constant data in netCDF file.");
158     if (rank == 0) {
159     if (! (d_dim=dataFile.get_dim("l")) )
160     throw DataException("Error - load:: unable to recover d0 for scalar constant data in netCDF file.");
161     int d0 = d_dim->size();
162     if (! d0 == 1)
163     throw DataException("Error - load:: d0 is expected to be one for scalar constant data in netCDF file.");
164     dims[0]=1;
165     }
166     out=Data(0,shape,function_space);
167     if (!(var = dataFile.get_var("data")))
168     throw DataException("Error - load:: unable to find data in netCDF file.");
169     if (! var->get(&(out.getDataPoint(0,0).getData()[0]), dims) )
170     throw DataException("Error - load:: unable to recover data from netCDF file.");
171     } else if (type == 1) {
172     /* tagged data */
173     if ( ! (ndims == rank + 1) )
174     throw DataException("Error - load:: illegal number of dimensions for tagged data in netCDF file.");
175     if (! (tags_dim=dataFile.get_dim("tags")) )
176     throw DataException("Error - load:: unable to recover number of tags from netCDF file.");
177     ntags=tags_dim->size();
178     out=Data(0,shape,function_space);
179     } else if (type == 2) {
180     /* expanded data */
181     if ( ! (ndims == rank + 2) )
182     throw DataException("Error - load:: illegal number of dimensions for exanded data in netCDF file.");
183     if ( ! (samples_dim = dataFile.get_dim("samples") ) )
184     throw DataException("Error - load:: unable to recover number of samples from netCDF file.");
185     nsamples = samples_dim->size();
186     if ( ! (data_points_per_sample_dim = dataFile.get_dim("data_points_per_sample") ) )
187     throw DataException("Error - load:: unable to recover number of data points per sample from netCDF file.");
188     ndata_points_per_sample=data_points_per_sample_dim->size();
189     out=Data(0,shape,function_space);
190     } else {
191     throw DataException("Error - load:: unknown escript data type in netCDF file.");
192     }
193     return out;
194    
195     }
196    
197 jgs 82 Data
198     convertToData(const boost::python::object& value,
199     const FunctionSpace& what)
200     {
201     // first we try to extract a Data object from value
202     extract<Data> value_data(value);
203     if (value_data.check()) {
204     Data extracted_data=value_data();
205     if (extracted_data.isEmpty()) {
206     return extracted_data;
207     } else {
208     return Data(extracted_data,what);
209     }
210     } else {
211     return Data(value,what);
212     }
213     }
214    
215     } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26