MI - Fimex
GribFileIndex.h
Go to the documentation of this file.
1 /*
2  * Fimex, GribFileIndex.h
3  *
4  * (C) Copyright 2009, met.no
5  *
6  * Project Info: https://wiki.met.no/fimex/start
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21  * USA.
22  *
23  * Created on: Aug 31, 2009
24  * Author: Heiko Klein
25  */
26 
27 #ifndef GRIBFILEINDEX_H_
28 #define GRIBFILEINDEX_H_
29 
30 #include <boost/date_time/posix_time/posix_time.hpp>
31 #include <boost/filesystem/operations.hpp>
32 #include <vector>
33 #include <map>
34 #include <cstdio>
35 #include "fimex/XMLDoc.h"
36 #include <libxml/xmlreader.h>
37 #include "fimex/GridDefinition.h"
38 #include <boost/date_time/posix_time/posix_time_types.hpp>
39 #include <boost/regex.hpp>
40 
41 // forward decl of grib_api
42 struct grib_handle;
43 
44 namespace MetNoFimex
45 {
47 {
48 public:
58  GribFileMessage(boost::shared_ptr<grib_handle> gh, const std::string& fileURL, long filePos, long msgPos, const std::vector<std::pair<std::string, boost::regex> >& members=std::vector<std::pair<std::string, boost::regex> >(), const std::vector<std::string>& extraKeys=std::vector<std::string>());
59  GribFileMessage(boost::shared_ptr<XMLDoc>, std::string nsPrefix, xmlNodePtr node);
60  GribFileMessage(xmlTextReaderPtr reader, const std::string& fileName);
62 
64  bool isValid() const {return fileURL_ != "";}
66  std::string toString() const;
68  const long getEdition() const;
69  const std::string& getFileURL() const;
70  const off_t getFilePosition() const;
72  const size_t getMessageNumber() const;
73  const std::string& getName() const;
74  const std::string& getShortName() const;
75  boost::posix_time::ptime getValidTime() const;
76  boost::posix_time::ptime getReferenceTime() const;
78  long getTimeRangeIndicator() const;
79  long getLevelNumber() const;
80  long getLevelType() const;
84  size_t getTotalNumberOfEnsembles() const { return static_cast<size_t>(totalNumberOfEnsembles_); }
88  size_t getPerturbationNumber() const { return static_cast<size_t>(perturbationNo_); }
94 
100  const std::vector<long>& getParameterIds() const;
101  const std::string& getTypeOfGrid() const;
102  const GridDefinition& getGridDefinition() const;
110  size_t readData(std::vector<double>& data, double missingValue) const;
118  size_t readLevelData(std::vector<double>& levelData, double missingValue, bool asimofHeader=false) const;
119 private:
120  std::string fileURL_;
121  off_t filePos_;
122  size_t msgPos_; // for multiMessages: multimessages
123  std::string parameterName_;
124  std::string shortName_;
125  // ed1: indicatorOfParameter, gribTablesVersionNo, identificationOfOriginatingGeneratingCentre;
126  // ed2: parameterNumber, paramterCategory, discipline
127  std::vector<long> gridParameterIds_;
128  long edition_;
129  long dataTime_;
130  long dataDate_;
131  std::string stepUnits_;
132  std::string stepType_;
133  long stepStart_;
134  long stepEnd_;
135  long timeRangeIndicator_;
136  long levelType_;
137  long levelNo_;
138  long perturbationNo_;
139  long totalNumberOfEnsembles_;
140  std::map<std::string, long> otherKeys_;
141  std::string typeOfGrid_;
142  GridDefinition gridDefinition_;
143 };
144 
146 class GribFileMessageEqualTime : public std::unary_function<bool, const GribFileMessage&> {
147 public:
148  GribFileMessageEqualTime(boost::posix_time::ptime time) : time_(time) {}
150  bool operator()(const GribFileMessage& gfm) { return gfm.getValidTime() == time_; }
151 private:
152  boost::posix_time::ptime time_;
153 };
154 
156 class GribFileMessageEqualLevelTime : public std::unary_function<bool, const GribFileMessage&> {
157 public:
158  GribFileMessageEqualLevelTime(long edition, long levelType, long levelNo, boost::posix_time::ptime time) : edition_(edition), levelType_(levelType), levelNo_(levelNo), time_(time) {}
160  bool operator()(const GribFileMessage& gfm) { return (gfm.getEdition() == edition_) && (gfm.getLevelType() == levelType_) && (gfm.getLevelNumber() == levelNo_) && (gfm.getValidTime() == time_); }
161 private:
162  long edition_;
163  long levelType_;
164  long levelNo_;
165  boost::posix_time::ptime time_;
166 };
167 
168 
170 {
171 public:
172  GribFileIndex();
191  GribFileIndex(boost::filesystem::path gribFilePath, const std::vector<std::pair<std::string, boost::regex> >& members, bool ignoreExistingXml = false, std::map<std::string, std::string> options = std::map<std::string, std::string>());
213  GribFileIndex(boost::filesystem::path gribFilePath, boost::filesystem::path grbmlFilePath, const std::vector<std::pair<std::string, boost::regex> >& members, bool ignoreExistingXml = false, std::map<std::string, std::string> options = std::map<std::string, std::string>());
231  GribFileIndex(boost::filesystem::path gribmlFilePath);
232  virtual ~GribFileIndex();
233  const std::vector<GribFileMessage>& listMessages() const {return messages_;}
234  const std::string& getUrl() const {return url_;}
235 private:
236  std::string url_;
239  void init(const boost::filesystem::path& gribFilePath, const boost::filesystem::path& grbmlFilePath, const std::vector<std::pair<std::string, boost::regex> >& members, bool ignoreExistingXml);
240  void initByGrib(const boost::filesystem::path& gribFilePath, const std::vector<std::pair<std::string, boost::regex> >& members, const std::vector<std::string>& extraKeys);
241  void initByXML(const boost::filesystem::path& xmlFilePath);
242  void initByXMLReader(const boost::filesystem::path& xmlFilePath);
243 };
244 
249 
250 
251 }
252 
253 
254 #endif /* GRIBFILEINDEX_H_ */
const std::vector< GribFileMessage > & listMessages() const
Definition: GribFileIndex.h:233
const std::string & getFileURL() const
const std::string & getUrl() const
Definition: GribFileIndex.h:234
Functor to find Messages with equal time.
Definition: GribFileIndex.h:146
boost::posix_time::ptime getValidTime() const
basic_string< char > string
const GridDefinition & getGridDefinition() const
const std::string & getName() const
size_t getTotalNumberOfEnsembles() const
Definition: GribFileIndex.h:84
const std::vector< long > & getParameterIds() const
const long getEdition() const
accessors
const off_t getFilePosition() const
const size_t getMessageNumber() const
messages number within a multi-message
Functor to find messages with equal level and time.
Definition: GribFileIndex.h:156
bool isValid() const
test if this is a proper GribFileMessage or just the default constructor
Definition: GribFileIndex.h:64
const std::string & getShortName() const
basic_ostream< char > ostream
STL class.
~GribFileMessageEqualTime()
Definition: GribFileIndex.h:149
Definition: GribFileIndex.h:169
~GribFileMessageEqualLevelTime()
Definition: GribFileIndex.h:159
std::string toString() const
give a xml-string representation
size_t readLevelData(std::vector< double > &levelData, double missingValue, bool asimofHeader=false) const
Definition: GridDefinition.h:40
xmlNode * xmlNodePtr
Definition: XMLDoc.h:39
Definition: C_CDMReader.h:35
Definition: GribFileIndex.h:46
GribFileMessageEqualLevelTime(long edition, long levelType, long levelNo, boost::posix_time::ptime time)
Definition: GribFileIndex.h:158
bool operator()(const GribFileMessage &gfm)
Definition: GribFileIndex.h:150
long getTimeRangeIndicator() const
return gribs timeRangeIndicator (0=instant, 2,4=accumulated)
boost::posix_time::ptime getReferenceTime() const
std::ostream & operator<<(std::ostream &out, CoordinateAxis ca)
const std::map< std::string, long > & getOtherKeys() const
GribFileMessageEqualTime(boost::posix_time::ptime time)
Definition: GribFileIndex.h:148
bool operator()(const GribFileMessage &gfm)
Definition: GribFileIndex.h:160
size_t readData(std::vector< double > &data, double missingValue) const
size_t getPerturbationNumber() const
Definition: GribFileIndex.h:88
const std::string & getTypeOfGrid() const