Format.hxx

00001 // Copyright (C) 2003-2007, Vivien Mallet
00002 //
00003 // This file is part of SeldonData library, used for data processing.
00004 //
00005 // SeldonData is free software; you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation; either version 2 of the License, or (at your option)
00008 // any later version.
00009 //
00010 // SeldonData is distributed in the hope that it will be useful, but WITHOUT
00011 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 // more details.
00014 //
00015 // For more information, visit the SeldonData home page:
00016 //      http://vivienmallet.net/lib/seldondata/
00017 
00018 #ifndef FILE_SELDONDATA_FORMAT_HXX
00019 
00020 #include <stdio.h>
00021 #include <iostream>
00022 using std::cout;
00023 using std::endl;
00024 #include <string>
00025 #include <fstream>
00026 using namespace std;
00027 
00028 #ifdef SELDONDATA_WITH_NETCDF
00029 #include "netcdfcpp.h"
00030 #endif
00031 
00032 #ifdef SELDONDATA_WITH_GRIB
00033 #include "decode_grib.cpp"
00034 #endif
00035 
00036 namespace SeldonData
00037 {
00038 
00040   class Format
00041   {
00042 
00043   protected:
00044 
00045   public:
00046     Format()  throw();
00047     ~Format()  throw();
00048 
00049   };
00050 
00051 
00053   template<class T>
00054   class FormatBinary: public Format
00055   {
00056 
00057   protected:
00058 
00059   public:
00060     FormatBinary()  throw();
00061     ~FormatBinary()  throw();
00062 
00063     // Grid.
00064 
00065     template<class TG>
00066     void Read(string FileName, RegularGrid<TG>& G) const;
00067     template<class TG>
00068     void Read(ExtStream& FileStream, RegularGrid<TG>& G) const;
00069     template<class TG>
00070     void Read(ifstream& FileStream, RegularGrid<TG>& G) const;
00071     template<class TG, int N>
00072     void Read(string FileName, GeneralGrid<TG, N>& G) const;
00073     template<class TG, int N>
00074     void Read(ExtStream& FileStream, GeneralGrid<TG, N>& G) const;
00075     template<class TG, int N>
00076     void Read(ifstream& FileStream, GeneralGrid<TG, N>& G) const;
00077 
00078     template<class TG>
00079     void Write(RegularGrid<TG>& G, string FileName) const;
00080     template<class TG>
00081     void Write(RegularGrid<TG>& G, ofstream& FileStream) const;
00082     template<class TG, int N>
00083     void Write(GeneralGrid<TG, N>& G, string FileName) const;
00084     template<class TG, int N>
00085     void Write(GeneralGrid<TG, N>& G, ofstream& FileStream) const;
00086 
00087     template<class TG>
00088     void Append(RegularGrid<TG>& G, string FileName) const;
00089     template<class TG, int N>
00090     void Append(GeneralGrid<TG, N>& G, string FileName) const;
00091 
00092     // Data.
00093 
00094     template<class TD, int N, class TG>
00095     void Read(string FileName, Data<TD, N, TG>& D) const;
00096     template<class TD, int N, class TG>
00097     void Read(ExtStream& FileStream, Data<TD, N, TG>& D) const;
00098     template<class TD, int N, class TG>
00099     void Read(ifstream& FileStream, Data<TD, N, TG>& D) const;
00100 
00101     template<class TD, int N, class TG>
00102     void ReadSteps(string FileName, int steps, Data<TD, N, TG>& D) const;
00103     template<class TD, int N, class TG>
00104     void ReadRecord(string FileName, int steps, Data<TD, N, TG>& D) const;
00105 
00106     template<class TD, int N, class TG>
00107     void Write(Data<TD, N, TG>& D, string FileName) const;
00108     template<class TD, int N, class TG>
00109     void Write(Data<TD, N, TG>& D, ofstream& FileStream) const;
00110 
00111     template<class TD, int N, class TG>
00112     void Append(Data<TD, N, TG>& D, string FileName) const;
00113 
00114     // Array.
00115 
00116     template<class TA, int N>
00117     void Read(string FileName, Array<TA, N>& A) const;
00118     template<int N>
00119     void Read(ExtStream& FileStream, Array<T, N>& A) const;
00120     template<int N>
00121     void Read(ifstream& FileStream, Array<T, N>& A) const;
00122     template<class TA, int N>
00123     void Read(ExtStream& FileStream, Array<TA, N>& A) const;
00124     template<class TA, int N>
00125     void Read(ifstream& FileStream, Array<TA, N>& A) const;
00126 
00127     template<class TA, int N>
00128     void ReadSteps(string FileName, int steps, Array<TA, N>& A) const;
00129     template<class TA, int N>
00130     void ReadRecord(string FileName, int steps, Array<TA, N>& A) const;
00131 
00132     template<class TA, int N>
00133     void Write(Array<TA, N>& A, string FileName) const;
00134     template<int N>
00135     void Write(Array<T, N>& A, ofstream& FileStream) const;
00136     template<class TA, int N>
00137     void Write(Array<TA, N>& A, ofstream& FileStream) const;
00138 
00139     template<class TA, int N>
00140     void Append(Array<TA, N>& A, string FileName) const;
00141 
00142   };
00143 
00144 
00146   class FormatText: public Format
00147   {
00148 
00149   protected:
00150     string separator_;
00151     fstream::fmtflags flags_;
00152     streamsize precision_;
00153     streamsize width_;
00154 
00155   public:
00156     FormatText()  throw();
00157     FormatText(string separator)  throw();
00158     FormatText(fstream::fmtflags flags, string separator = "\t\t")  throw();
00159     FormatText(fstream::fmtflags flags, streamsize precision,
00160                streamsize width = -1, string separator = "\t\t")  throw();
00161     ~FormatText()  throw();
00162 
00163     void SetSeparator(string separator);
00164     void SetFlags(ofstream::fmtflags flags);
00165     void SetPrecision(streamsize precision);
00166     void SetWidth(streamsize width);
00167 
00168     // Grid.
00169 
00170     template<class TG>
00171     void Read(string FileName, RegularGrid<TG>& G) const;
00172     template<class TG>
00173     void Read(ifstream& FileStream, RegularGrid<TG>& G) const;
00174     template<class TG, int N>
00175     void Read(string FileName, GeneralGrid<TG, N>& G) const;
00176     template<class TG, int N>
00177     void Read(ifstream& FileStream, GeneralGrid<TG, N>& G) const;
00178 
00179     template<class TG>
00180     void Write(RegularGrid<TG>& G, string FileName) const;
00181     template<class TG>
00182     void Write(RegularGrid<TG>& G, ofstream& FileStream) const;
00183     template<class TG, int N>
00184     void Write(GeneralGrid<TG, N>& G, string FileName) const;
00185     template<class TG, int N>
00186     void Write(GeneralGrid<TG, N>& G, ofstream& FileStream) const;
00187 
00188     // Data.
00189 
00190     template<class TD, int N, class TG>
00191     void Read(string FileName, Data<TD, N, TG>& D) const;
00192     template<class TD, int N, class TG>
00193     void Read(ifstream& FileStream, Data<TD, N, TG>& D) const;
00194 
00195     template<class TD, int N, class TG>
00196     void Write(Data<TD, N, TG>& D, string FileName) const;
00197     template<class TD, int N, class TG>
00198     void Write(Data<TD, N, TG>& D, ofstream& FileStream) const;
00199 
00200     // Array.
00201 
00202     template<class TA, int N>
00203     void Read(string FileName, Array<TA, N>& A) const;
00204     template<class TA, int N>
00205     void Read(ifstream& FileStream, Array<TA, N>& A) const;
00206     template<class TA>
00207     void Read(ifstream& FileStream, Array<TA, 1>& A) const;
00208 
00209     template<class TA, int N>
00210     void Write(Array<TA, N>& A, string FileName) const;
00211     template<class TA, int N>
00212     void Write(Array<TA, N>& A, ofstream& FileStream) const;
00213 
00214   };
00215 
00216 
00218   class FormatFormattedText: public Format
00219   {
00220 
00221   protected:
00223     string format_;
00225     string comments_;
00227     string delimiters_;
00229     vector<string> info_str;
00231     vector<int> info_nb0;
00233     vector<int> info_nb1;
00234 
00235   private:
00236     void SetVectors();
00237     void SkipMarkup(ExtStream&, streampos pos, int) const;
00238     template <class T>
00239     int ReadMarkup(ExtStream&, streampos pos, int, T*, int) const;
00240 
00241   public:
00242     FormatFormattedText(string format,
00243                         string comments = "#%",
00244                         string delimiters = " \t:;,|\n");
00245     ~FormatFormattedText();
00246 
00247     string GetFormat() const;
00248     string GetDelimiters() const;
00249     string GetComments() const;
00250 
00251     void SetFormat(string format);
00252     void SetDelimiters(string delimiters);
00253     void SetComments(string comments);
00254 
00255     // Grid.
00256 
00257     template<class TG>
00258     void Read(string FileName, string extract, RegularGrid<TG>& G) const;
00259     template<class TG>
00260     void Read(ExtStream& FileStream, string extract, RegularGrid<TG>& G)
00261       const;
00262     template<class TG, int N>
00263     void Read(string FileName, string extract, GeneralGrid<TG, N>& G) const;
00264     template<class TG, int N>
00265     void Read(ExtStream& FileStream, string extract, GeneralGrid<TG, N>& G)
00266       const;
00267 
00268     // Data.
00269 
00270     template<class TD, int N, class TG>
00271     void Read(string FileName, string extract, Data<TD, N, TG>& D) const;
00272     template<class TD, int N, class TG>
00273     void Read(ExtStream& FileStream, string extract, Data<TD, N, TG>& D)
00274       const;
00275 
00276     // Array.
00277 
00278     template<class TA, int N>
00279     void Read(string FileName, string extract, Array<TA, N>& A) const;
00280     template<class TA, int N>
00281     void Read(ExtStream& FileStream, string extract, Array<TA, N>& A) const;
00282 
00283   };
00284 
00285 
00286 #ifdef SELDONDATA_WITH_NETCDF
00288   template<class T>
00289   class FormatNetCDF: public Format
00290   {
00291 
00292   protected:
00293 
00294   public:
00295     FormatNetCDF()  throw();
00296     ~FormatNetCDF()  throw();
00297     // For variables.
00298     // Grid.
00299 
00300     template<class TG>
00301     void Read(string FileName, string variable, RegularGrid<TG>& G) const;
00302     template<class TG, int N>
00303     void Read(string FileName, string variable, GeneralGrid<TG, N>& G) const;
00304 
00305     // Data.
00306 
00307     template<class TD, int N, class TG>
00308     void Read(string FileName, string variable, Data<TD, N, TG>& D) const;
00309 
00310     // Array.
00311 
00312     template<class TA, int N>
00313     void Read(string FileName, string variable, Array<TA, N>& A) const;
00314 
00315     // For dimensions.
00316     void ReadDimension(string FileName, string variable, int dim_num,
00317                                            int& dim_value) const;
00318 
00319     // For attibutes.
00320     void ReadAttribute(string FileName, string attribute, float& value) const;
00321     void ReadAttribute(string FileName, string attribute, int& value) const;
00322         
00323   };
00324 #endif
00325 
00326 
00327 #ifdef SELDONDATA_WITH_GRIB
00329   class FormatGrib: public Format
00330   {
00331 
00332   protected:
00333 
00334   public:
00335     FormatGrib()  throw();
00336     ~FormatGrib()  throw();
00337 
00338     // Grid.
00339 
00340     template<class TG>
00341     void Read(string FileName, int variable, RegularGrid<TG>& G) const;
00342     template<class TG, int N>
00343     void Read(string FileName, int variable, GeneralGrid<TG, N>& G) const;
00344 
00345     // Data.
00346 
00347     template<class TD, int N, class TG>
00348     void Read(string FileName, int variable, Data<TD, N, TG>& D) const;
00349 
00350     // Array.
00351 
00352     template<class TA, int N>
00353     void Read(string FileName, int variable, Array<TA, N>& A) const;
00354 
00355   };
00356 #endif
00357 
00358 
00360   class FormatChimere: public Format
00361   {
00362 
00363   protected:
00364     int date_;
00365 
00366   public:
00367     FormatChimere()  throw();
00368     FormatChimere(int date)  throw();
00369     ~FormatChimere()  throw();
00370 
00371     void SetDate(int date);
00372     int GetDate() const;
00373 
00374     // Data.
00375 
00376     template<class TD, int N, class TG>
00377     void Read(string FileName, Data<TD, N, TG>& D,
00378               int nb_lines = -1) const;
00379     template<class TD, int N, class TG>
00380     void Read(ifstream& FileStream, Data<TD, N, TG>& D,
00381               int nb_lines = -1) const;
00382 
00383     // Array.
00384 
00385     template<class TA, int N>
00386     void Read(string FileName, Array<TA, N>& A,
00387               int nb_lines = -1) const;
00388     template<class TA, int N>
00389     void Read(ifstream& FileStream, Array<TA, N>& A,
00390               int nb_lines = -1) const;
00391 
00392   };
00393 
00394 }  // namespace SeldonData.
00395 
00396 #define FILE_SELDONDATA_FORMAT_HXX
00397 #endif

Generated on Tue Nov 17 11:20:52 2009 for SeldonData by  doxygen 1.5.1