Main Page | User's Guide | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members

Format.hxx

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

Generated on Mon Dec 18 15:24:25 2006 for SeldonData by  doxygen 1.4.2