All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Band.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/grib/Band.cpp
22 
23  \brief Band implemntatin for GRIB.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "../raster/BandProperty.h"
29 #include "../datatype/Enums.h"
30 #include "Band.h"
31 #include "Exception.h"
32 #include "Raster.h"
33 
34 // STL
35 #include <cstring>
36 
37 // Boost
38 #include <boost/format.hpp>
39 
40 te::grib::Band::Band(Raster* r, std::size_t idx, grib_handle* handle)
41  : te::rst::Band(0, idx),
42  m_raster(r),
43  m_handle(handle),
44  m_data(0)
45 {
46  int datatypecode = te::dt::DOUBLE_TYPE; // change this for multiple codes!
47 
48  m_property = new te::rst::BandProperty(idx, datatypecode);
49 
50  std::size_t size = 0;
51 
52  int err = grib_get_size(m_handle, "values", &size);
53 
54  m_data = new double[size]; // change this if other types are needed!
55 
56  err = grib_get_double_array(m_handle, "values", m_data, &size);
57 
58  m_property->m_noDataValue = getDouble("missingValue");
59  m_property->m_blkh = getLong("numberOfPointsAlongYAxis");
60  m_property->m_blkw = getLong("numberOfPointsAlongXAxis");
63 }
64 
66  : te::rst::Band(rhs),
67  m_raster(0),
68  m_handle(0)
69 {
70  throw Exception(TR_GRIB("Not implemented yet!"));
71 }
72 
74 {
75  // flush, close?
76  if(m_handle)
77  {
78  grib_handle_delete(m_handle);
79  m_handle = 0;
80  }
81 }
82 
84 {
85  return m_raster;
86 }
87 
89 {
90  throw Exception(TR_GRIB("Not implemented yet!"));
91 }
92 
93 void te::grib::Band::getValue(unsigned int c, unsigned int r, double& value) const
94 {
95  unsigned int pos = r * m_property->m_blkw + c;
96 
97  value = m_data[pos];
98 }
99 
100 void te::grib::Band::setValue(unsigned int c, unsigned int r, const double value)
101 {
102  unsigned int pos = r * m_property->m_blkw + c;
103 
104  m_data[pos] = value;
105 }
106 
107 void te::grib::Band::getIValue(unsigned int c, unsigned int r, double& value) const
108 {
109  throw Exception(TR_GRIB("Complex data not supported by GRIB format!"));
110 }
111 
112 void te::grib::Band::setIValue(unsigned int c, unsigned int r, const double value)
113 {
114  throw Exception(TR_GRIB("Complex data not supported by GRIB format!"));
115 }
116 
117 void te::grib::Band::read(int x, int y, void* buffer) const
118 {
119  assert(x == 0 && y == 0);
120 
121  memcpy(buffer, m_data, getBlockSize());
122 }
123 
124 void* te::grib::Band::read(int x, int y)
125 {
126  assert(x == 0 && y == 0);
127 
128  return m_data;
129 }
130 
131 void te::grib::Band::write(int x, int y, void* buffer)
132 {
133  assert(x == 0 && y == 0);
134 
135  memcpy(m_data, buffer, getBlockSize());
136 }
137 
138 grib_handle* te::grib::Band::getHandle() const
139 {
140  return m_handle;
141 }
142 
143 long te::grib::Band::getLong(const char* key) const
144 {
145  long val = 0;
146 
147  int err = grib_get_long(m_handle, key, &val);
148 
149  if(err)
150  throw Exception((boost::format(TR_GRIB("Can not get key: %1%, due to: %2%.")) % key % getErrMsg(err)).str());
151 
152  return val;
153 }
154 
155 double te::grib::Band::getDouble(const char* key) const
156 {
157  double val = 0.0;
158 
159  int err = grib_get_double(m_handle, key, &val);
160 
161  if(err)
162  throw Exception((boost::format(TR_GRIB("Can not get key: %1%, due to: %2%.")) % key % getErrMsg(err)).str());
163 
164  return val;
165 }
166 
167 std::string te::grib::Band::getString(const char* key) const
168 {
169  char val[1024];
170 
171  std::size_t length = 0;
172 
173  int err = grib_get_string(m_handle, key, val, &length);
174 
175  if(err)
176  throw Exception((boost::format(TR_GRIB("Can not get key: %1%, due to: %2%.")) % key % getErrMsg(err)).str());
177 
178  return std::string(val);
179 }
180 
181 std::string te::grib::Band::getErrMsg(int errCode)
182 {
183  const char* errmsg = grib_get_error_message(errCode);
184 
185  assert(errmsg);
186 
187  return std::string(errmsg);
188 }
189 
te::rst::Raster * getRaster() const
Returns the associated raster.
Definition: Band.cpp:83
void read(int x, int y, void *buffer) const
It reads a data block to the specified buffer.
Definition: Band.cpp:117
A raster band description.
Definition: BandProperty.h:61
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
An exception class for GRIB.
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
grib_handle * m_handle
The grib handle.
Definition: Band.h:89
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits::max().
Definition: BandProperty.h:136
A raster class for GRIB format.
void setIValue(unsigned int c, unsigned int r, const double value)
Sets the imaginary attribute value in a complex band of a cell.
Definition: Band.cpp:112
void getValue(unsigned int c, unsigned int r, double &value) const
Returns the cell attribute value.
Definition: Band.cpp:93
An abstract class for raster data strucutures.
Definition: Raster.h:71
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
std::string getString(const char *key) const
Definition: Band.cpp:167
~Band()
Virtual destructor.
Definition: Band.cpp:73
long getLong(const char *key) const
Definition: Band.cpp:143
double * m_data
The matrix data.
Definition: Band.h:90
Band(Raster *r, std::size_t idx, grib_handle *handle)
Definition: Band.cpp:40
Band implemntatin for GRIB.
Definition: Band.h:48
Band & operator=(const Band &rhs)
Definition: Band.cpp:88
A raster class for GRIB format.
Definition: Raster.h:51
static std::string getErrMsg(int errCode)
Definition: Band.cpp:181
void setValue(unsigned int c, unsigned int r, const double value)
Sets the cell attribute value.
Definition: Band.cpp:100
grib_handle * getHandle() const
Definition: Band.cpp:138
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
void write(int x, int y, void *buffer)
It writes a data block from the specified buffer.
Definition: Band.cpp:131
Band implemntatin for GRIB.
#define TR_GRIB(message)
It marks a string in order to get translated. This is a special mark used in the DataAccess module of...
Definition: Config.h:69
double getDouble(const char *key) const
Definition: Band.cpp:155
BandProperty * m_property
The band information.
Definition: Band.h:474
void getIValue(unsigned int c, unsigned int r, double &value) const
Returns the imaginary attribute value in a complex band of a cell.
Definition: Band.cpp:107