00001 /* Copyright (C) 2002-2008 00002 Claudia Emde <claudia.emde@dlr.de> 00003 Oliver Lemke <olemke@core-dump.info> 00004 00005 This program is free software; you can redistribute it and/or modify it 00006 under the terms of the GNU General Public License as published by the 00007 Free Software Foundation; either version 2, or (at your option) any 00008 later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00018 USA. 00019 */ 00020 00036 /*=========================================================================== 00037 === External declarations 00038 ===========================================================================*/ 00039 00040 00041 #include <stdexcept> 00042 #include <iostream> 00043 #include "exceptions.h" 00044 #include "mystring.h" 00045 #include "gridded_fields.h" 00046 00047 00048 00049 /*=========================================================================== 00050 === The functions (in alphabetical order) 00051 ===========================================================================*/ 00052 00054 00059 void GField::copy_grids (const GField& gf) 00060 { 00061 assert(gf.get_dim() == dim); 00062 00063 for (Index i = 0; i < dim; i++) 00064 { 00065 switch (gf.get_grid_type(i)) 00066 { 00067 case GRIDTYPE_NUMERIC: 00068 mgridtypes[i] = GRIDTYPE_NUMERIC; 00069 mnumericgrids[i] = gf.get_numeric_grid(i); 00070 mstringgrids[i].resize(0); 00071 break; 00072 case GRIDTYPE_STRING: 00073 mgridtypes[i] = GRIDTYPE_STRING; 00074 mstringgrids[i] = gf.get_string_grid(i); 00075 mnumericgrids[i].resize(0); 00076 break; 00077 } 00078 } 00079 } 00080 00081 00083 00089 Index GField::get_grid_size (Index i) const 00090 { 00091 Index ret = 0; 00092 assert (i < dim); 00093 switch (mgridtypes[i]) 00094 { 00095 case GRIDTYPE_NUMERIC: ret = mnumericgrids[i].nelem(); break; 00096 case GRIDTYPE_STRING: ret = mstringgrids[i].nelem(); break; 00097 } 00098 00099 return ret; 00100 } 00101 00102 00104 00112 ConstVectorView GField::get_numeric_grid (Index i) const 00113 { 00114 assert (i < dim); 00115 if (mgridtypes[i] != GRIDTYPE_NUMERIC) 00116 { 00117 ostringstream os; 00118 00119 if (mname.length()) 00120 os << mname << " "; 00121 00122 os << "Grid "; 00123 if (mgridnames[i].length()) 00124 os << mgridnames[i]; 00125 else 00126 os << i; 00127 os << " is not a numeric grid."; 00128 00129 throw runtime_error(os.str()); 00130 } 00131 00132 return (mnumericgrids[i]); 00133 } 00134 00135 00137 00145 VectorView GField::get_numeric_grid (Index i) 00146 { 00147 assert (i < dim); 00148 if (mgridtypes[i] != GRIDTYPE_NUMERIC) 00149 { 00150 ostringstream os; 00151 00152 if (mname.length()) 00153 os << mname << " "; 00154 00155 os << "Grid "; 00156 if (mgridnames[i].length()) 00157 os << mgridnames[i]; 00158 else 00159 os << i; 00160 os << " is not a numeric grid."; 00161 00162 throw runtime_error(os.str()); 00163 } 00164 00165 return (mnumericgrids[i]); 00166 } 00167 00168 00170 00178 const ArrayOfString& GField::get_string_grid (Index i) const 00179 { 00180 assert (i < dim); 00181 if (mgridtypes[i] != GRIDTYPE_STRING) 00182 { 00183 ostringstream os; 00184 00185 if (mname.length()) 00186 os << mname << " "; 00187 00188 os << "Grid "; 00189 if (mgridnames[i].length()) 00190 os << mgridnames[i]; 00191 else 00192 os << i; 00193 os << " is not a string grid."; 00194 00195 throw runtime_error(os.str()); 00196 } 00197 00198 00199 return (mstringgrids[i]); 00200 } 00201 00202 00204 00212 ArrayOfString& GField::get_string_grid (Index i) 00213 { 00214 assert (i < dim); 00215 if (mgridtypes[i] != GRIDTYPE_STRING) 00216 { 00217 ostringstream os; 00218 00219 if (mname.length()) 00220 os << mname << " "; 00221 00222 os << "Grid "; 00223 if (mgridnames[i].length()) 00224 os << mgridnames[i]; 00225 else 00226 os << i; 00227 os << " is not a string grid."; 00228 00229 throw runtime_error(os.str()); 00230 } 00231 00232 00233 return (mstringgrids[i]); 00234 } 00235 00236 00238 00244 void GField::set_grid (Index i, const Vector& g) 00245 { 00246 assert (i < dim); 00247 mgridtypes[i] = GRIDTYPE_NUMERIC; 00248 mstringgrids[i].resize(0); 00249 mnumericgrids[i] = g; 00250 } 00251 00252 00254 00260 void GField::set_grid (Index i, const ArrayOfString& g) 00261 { 00262 assert (i < dim); 00263 mgridtypes[i] = GRIDTYPE_STRING; 00264 mnumericgrids[i].resize(0); 00265 mstringgrids[i] = g; 00266 } 00267 00268 00270 00276 ostream& operator<<(ostream& os, const GField& gf) 00277 { 00278 if (gf.mname.size()) os << gf.mname << ":" << endl; 00279 00280 for( Index i = 0; i < gf.dim; i++) 00281 { 00282 if (gf.mgridnames[i].size()) os << gf.mgridnames[i]; 00283 else os << "Grid " << i; 00284 os << ": "; 00285 switch (gf.mgridtypes[i]) 00286 { 00287 case GRIDTYPE_STRING: 00288 os << gf.mstringgrids[i]; 00289 break; 00290 case GRIDTYPE_NUMERIC: 00291 os << gf.mnumericgrids[i]; 00292 break; 00293 } 00294 os << endl; 00295 } 00296 00297 return os; 00298 } 00299 00300 00302 00308 ostream& operator<<(ostream& os, const GField1& gf) 00309 { 00310 os << (GField&)gf; 00311 return os << "Data:" << endl << (Vector&)gf << endl; 00312 } 00313 00314 00316 00322 ostream& operator<<(ostream& os, const GField2& gf) 00323 { 00324 os << (GField&)gf; 00325 return os << "Data:" << endl << (Matrix&)gf; 00326 } 00327 00328 00330 00336 ostream& operator<<(ostream& os, const GField3& gf) 00337 { 00338 os << (GField&)gf; 00339 return os << "Data:" << endl << (Tensor3&)gf; 00340 } 00341 00342 00344 00350 ostream& operator<<(ostream& os, const GField4& gf) 00351 { 00352 os << (GField&)gf; 00353 return os << "Data:" << endl << (Tensor4&)gf; 00354 } 00355 00356 00357 00358 00359 00360