OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimHdf5Tool.cpp
Go to the documentation of this file.
1 //**************************************************************************************************
2 //
3 // OSSIM Open Source Geospatial Data Processing Library
4 // See top level LICENSE.txt file for license information
5 //
6 //**************************************************************************************************
7 
9 #include <ossim/init/ossimInit.h>
12 #include <ossim/base/ossimNotify.h>
14 #include <ossim/base/ossimString.h>
20 #include <ossim/hdf5/ossimHdf5.h>
24 #include <iostream>
25 
26 using namespace std;
27 using namespace H5;
28 
29 const char* ossimHdf5Tool::DESCRIPTION = "Utility for parsing and extracting HDF5 image data.";
30 
31 static const string IMAGE_DATASET_KW = "image_dataset";
32 static const string GEOM_DATASET_KW = "geom_dataset";
33 static const string LIST_DATASETS_KW = "list_datasets";
34 static const string LIST_NDIMS_KW = "list_ndims";
35 static const string DUMP_INFO_KW = "dump_info";
36 static const string DUMP_KWL_KW = "dump_kwl";
37 
39 : m_dumpInfo (false),
40  m_dumpKwl (false),
41  m_listDatasets (false),
42  m_listNdimDatasets (false)
43 {
44 }
45 
47 {
48 }
49 
51 {
52  // Add options.
54  ossimString usageString = ap.getApplicationName();
55  usageString += " hdf5 [options] [--i <input-hdf5-file>] [-o <output-image>]";
56  au->setCommandLineUsage(usageString);
57 
58  // Set the command line options:
60  au->addCommandLineOption("--geom-dataset","<path_to_dataset>"
61  "Full HDF5-internal path to geometry dataset to use for image. ");
62  au->addCommandLineOption("--image-dataset","<path_to_dataset>"
63  "Full HDF5-internal path to pixel dataset to extract. ");
64  au->addCommandLineOption("--list-datasets",
65  "Lists all datasets with extents. ");
66  au->addCommandLineOption("--list-ndims",
67  "Lists all datasets with extents that are of rank 2 or higher.");
68  au->addCommandLineOption("--geom", "[<geomfilename>]\n"
69  "Dumps the geometry keyword-list to the specified file or the console if no filename specified.");
70  au->addCommandLineOption("--info",
71  "Outputs human-readable dump of all objects in the file");
72  au->addCommandLineOption("--kwl",
73  "Outputs keywordlist dump of all objects in the file");
74 
75  // Base class has its own:
77 }
78 
80 {
82  return false;
83  if (m_helpRequested)
84  return true;
85 
86  std::string tempString1;
87  ossimArgumentParser::ossimParameter stringParam1(tempString1);
88  const string TRUE_STR ("true");
89 
90  if ( ap.read("--geom-dataset", stringParam1) || ap.read("--gdata", stringParam1))
91  m_kwl.addPair(GEOM_DATASET_KW, tempString1);
92 
93  if ( ap.read("--geom", stringParam1))
95 
96  if ( ap.read("--image-dataset", stringParam1) || ap.read("--idata", stringParam1))
97  m_kwl.addPair(IMAGE_DATASET_KW, tempString1);
98 
99  if ( ap.read("--list-datasets"))
100  m_kwl.addPair(LIST_DATASETS_KW, TRUE_STR);
101 
102  if ( ap.read("--list-ndims") )
103  m_kwl.addPair(LIST_NDIMS_KW, TRUE_STR);
104 
105  if ( ap.read("--info") )
106  m_kwl.addPair(DUMP_INFO_KW, TRUE_STR);
107 
108  if ( ap.read("--kwl") )
109  m_kwl.addPair(DUMP_KWL_KW, TRUE_STR);
110 
112 
113  if ( ap.argc() > 1 )
114  {
115  // If input file already specified, then remaining arg is output file:
118  else
119  {
120  // No input specified, so remaining args contains the input filename:
121  if (ap.argc() == 2)
123  else
124  {
127  }
128  }
129  }
130 
131  initialize(m_kwl);
132  return true;
133 }
134 
136 {
137  ostringstream xmsg;
138 
139  // Don't copy KWL if member KWL passed in:
140  if (&kwl != &m_kwl)
141  {
142  // Start with clean options keyword list.
143  m_kwl.clear();
144  m_kwl.addList( kwl, true );
145  }
146 
147  m_imageDataPath = m_kwl.findKey(IMAGE_DATASET_KW);
148  m_geomDataPath = m_kwl.findKey(GEOM_DATASET_KW);
150 
151  m_kwl.getBoolKeywordValue(m_listDatasets, LIST_DATASETS_KW.c_str());
152  m_kwl.getBoolKeywordValue(m_listNdimDatasets, LIST_NDIMS_KW.c_str());
153  m_kwl.getBoolKeywordValue(m_dumpInfo, DUMP_INFO_KW.c_str());
154  m_kwl.getBoolKeywordValue(m_dumpKwl, DUMP_KWL_KW.c_str());
155 
157  if (hdfFile.empty() )
158  {
159  xmsg<<"ERROR: ossimHdf5Tool:"<<__LINE__<<" -- No input filename was specified.";
160  throw ossimException(xmsg.str());
161  }
162 
163  m_hdf5 = new ossimHdf5;
164  if (!m_hdf5->open(hdfFile))
165  {
166  xmsg<<"ERROR: ossimHdf5Tool:"<<__LINE__<<" -- Could not open <"<<hdfFile
167  <<"> as an HDF5 file.";
168  throw ossimException(xmsg.str());
169  }
170 
172  return;
173 
174  // Only if an output file is specified, do we need the services of ossimChipProcTool:
176 
178  if (m_imgLayers.empty())
179  {
180  xmsg<<"ERROR: ossimHdf5Tool:"<<__LINE__<<" -- No input chain available. Make sure an HDF5"
181  " input file name is specified";
182  throw ossimException(xmsg.str());
183  }
184 
185  // If dataset specified, then expect product filename:
187  {
188  m_productFilename = hdfFile;
190  }
191 }
192 
194 {
195  ostringstream errMsg;
196 
197  // Special handling needed for loading HDF5 file:
199  if (value.empty())
200  {
201  errMsg<<"ERROR: ossimHdf5Tool:"<<__LINE__<<" -- No input HDF5 file name specified.";
202  throw ossimException(errMsg.str());
203  }
204  ossimFilename imageFilename (value);
205 
206  // If explicit image data paths are specified, open the HDF5 handler directly, otherwise assume
207  // there is a plugin factory that will figure it out:
209  if (m_imageDataPath.empty())
210  {
211  // Use the factory system to instantiate handler, then verify it is HDF5:
213  handler = dynamic_cast<ossimHdf5ImageHandler*>(ih.get());
214  if (!handler.valid())
215  {
216  errMsg<<"ERROR: ossimHdf5Tool:"<<__LINE__<<" -- No HDF5 dataset paths were specified"
217  " and no default HDF5-derived handlers were found. Don't know what dataset to "
218  "extract. Use \"--image-dataset\" option to specify.";
219  throw ossimException(errMsg.str());
220  }
221  }
222  else
223  {
224  // Use explicitely-provided dataset names:
225  handler = new ossimHdf5ImageHandler;
226  handler->addRenderable(m_imageDataPath);
227  handler->setFilename(ossimFilename(value));
228  if (!handler->open())
229  {
230  errMsg<<"ERROR: ossimHdf5Tool:"<<__LINE__<<" -- Could not open <"<<value<<"> as an HDF5 file.";
231  throw ossimException(errMsg.str());
232  }
233  }
234 
235 
236  // Init chain with handler:
238  chain->addLast(handler.get());
239 
240  // Set up the remapper:
243  {
246  chain->add(remapper.get());
247  }
248 
249  // Add geo polygon cutter if specifried:
250  ossimString param = m_kwl.findKey(string("clip_poly_lat_lon"));
251  if (!param.empty())
252  {
253  std::vector<ossimGpt> points;
254  ossim::toVector(points, param);
255  if(points.size() >= 3)
256  {
257  ossimGeoPolygon polygon(points);
258  chain->addGeoPolyCutterPolygon(polygon);
259  }
260  }
261 
262  chain->initialize();
263  m_imgLayers.push_back(chain);
264 }
265 
267 {
269  m_procChain->add(input_mosaic.get());
270 
272  sf->setNullPixelValue( 0.0 );
273  sf->setMinPixelValue( 1.0 );
274  sf->setMaxPixelValue( 65535.0 );
275  m_procChain->add( sf.get() );
276 
277  // Cast it to uint16:
279  m_procChain->add( cf.get() );
280 
281 }
282 
284 {
285  // Need to fix output to tiff
286 
287  ostringstream errMsg;
288  if (!m_hdf5.valid())
289  return false;
290 
292  {
293  ossimHdf5Info info (m_hdf5.get());
294  if (m_dumpKwl)
295  {
296  ossimKeywordlist kwl;
297  info.getKeywordlist(kwl);
298  kwl.print(cout);
299  }
300  if (m_dumpInfo)
301  {
302  info.print(cout);
303  }
304 
305  Group root;
306  m_hdf5->getRoot(root);
307  vector<DataSet> datasets;
308  if (m_listDatasets)
309  m_hdf5->getDatasets(root, datasets, true);
310  else if (m_listNdimDatasets)
311  m_hdf5->getNdimDatasets(root, datasets, true);
312  for (ossim_uint32 i=0; i<datasets.size(); ++i)
313  {
314  info.print(cout, datasets[i]);
315  }
316  }
317 
318  if (m_imgLayers.empty())
319  return true;
320 
322  return false;
323 
324  ossimRefPtr<ossimImageGeometry> geom = m_imgLayers[0]->getImageGeometry();
325  if (geom.valid())
326  {
328  if ( proj.valid() )
329  {
331  geomFile.string() += ".geom";
332 
333  // Assume it is coarse grid (case for VIIRS):
334  ossimRefPtr<ossimHdf5GridModel> cg = dynamic_cast<ossimHdf5GridModel*>( proj.get() );
335  if ( cg.valid() )
336  {
337  // this saves geom file as well
338  cg->saveCoarseGrid( geomFile );
339  cout << "Wrote file: " << geomFile << endl;
340  }
341  else
342  {
343  // Save the state to keyword list.
344  ossimKeywordlist geomKwl;
345  geom->saveState(geomKwl);
346 
347  // Write to file:
348  geomKwl.write( geomFile.c_str() );
349  cout << "Wrote file: " << geomFile << endl;
350  }
351  }
352  }
353 
354  return true;
355 }
virtual bool initialize(ossimArgumentParser &ap)
Initial method to be ran prior to execute.
std::string getApplicationName() const
return the application name, as specified by argv[0]
void addCommandLineOption(const ossimString &option, const ossimString &explanation)
virtual void loadImageFiles()
Creates chains for image entries associated with specified keyword.
void addRenderable(const ossimString &datasetName)
Adds the dataset name, either the full HDF5 path or the simple object name, to the list of renderable...
ossimFilename noExtension() const
virtual std::ostream & print(std::ostream &os) const
Outputs theErrorStatus as an ossimErrorCode and an ossimString.
ossim_uint32 numberOf(const char *str) const
bool addLast(ossimConnectableObject *obj)
Adds it to the end.
std::basic_ostringstream< char > ostringstream
Class for char output memory streams.
Definition: ossimIosFwd.h:35
ossimString m_imageDataPath
Definition: ossimHdf5Tool.h:64
static const char * OUTPUT_FILE_KW
bool getRoot(H5::Group &root) const
Assigns the root group.
Definition: ossimHdf5.cpp:93
16 bit unsigned integer
virtual ossimImageHandler * open(const ossimFilename &fileName, bool trySuffixFirst=true, bool openOverview=true) const
open that takes a filename.
Represents serializable keyword/value map.
const std::string & findKey(const std::string &key) const
Find methods that take std::string(s).
bool m_helpRequested
Definition: ossimTool.h:150
virtual bool execute()
Performs the actual product write.
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
bool read(const std::string &str)
search for an occurance of a string in the argument list, on sucess remove that occurance from the li...
virtual bool open()
The derived class needs to initialize the raster dataset names m_renderableNames in their constructor...
This is the base class for all imagery using HDF5 as the file format.
virtual void initialize()
void setNullPixelValue(double null)
Set the null output pixel.
void addList(const ossimKeywordlist &src, bool overwrite=true)
ossimKeywordlist m_kwl
Definition: ossimTool.h:148
ossimRefPtr< ossimImageSource > combineLayers(std::vector< ossimRefPtr< ossimSingleImageChain > > &layers) const
When multiple input sources are present, this method instantiates a combiner and adds inputs...
void addPair(const std::string &key, const std::string &value, bool overwrite=true)
void setCommandLineUsage(const ossimString &explanation)
static const char * DESCRIPTION
Used by ossimUtilityFactory.
Definition: ossimHdf5Tool.h:57
ossimApplicationUsage * getApplicationUsage()
virtual bool write(const char *file, const char *comment=0) const
Methods to dump the ossimKeywordlist to a file on disk.
bool open(const ossimFilename &hdf5File)
Opens specified HDF5 file.
Definition: ossimHdf5.cpp:29
Class to shift/stretch input values to given min/max.
static bool getDatasets(H5::Group group, std::vector< H5::DataSet > &datasetList, bool recursive=false)
Assigns list of datasets under specified group.
Definition: ossimHdf5.cpp:150
ossimRefPtr< ossimHdf5 > m_hdf5
Definition: ossimHdf5Tool.h:71
Single image chain class.
ossimRefPtr< ossimImageChain > m_procChain
virtual ossimScalarType getOutputScalarType() const
This call is passed to the head of the list.
void reportRemainingOptionsAsUnrecognized(ossimErrorSeverity severity=OSSIM_BENIGN)
for each remaining option report it as an unrecongnized.
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual void setUsage(ossimArgumentParser &ap)
Initializes the aurgument parser with expected parameters and options.
virtual void setOutputScalarType(ossimScalarType scalarType)
Sets the output scalar type.
OSSIM_DLL void toVector(std::vector< ossimDpt > &result, const ossimString &stringOfPoints)
Will take a string list separated by spaces and convert to a vector of ossimDpts. ...
ossimFilename m_productFilename
ossimFilename m_geomFilename
Definition: ossimHdf5Tool.h:70
virtual void setFilename(const ossimFilename &filename)
Sets the filename.
void setMaxPixelValue(double max)
Set the max output pixel.
unsigned int ossim_uint32
virtual void setUsage(ossimArgumentParser &ap)
Initializes the aurgument parser with expected parameters and options.
std::vector< ossimRefPtr< ossimSingleImageChain > > m_imgLayers
static const char * GEOM_FILE_KW
virtual bool add(ossimConnectableObject *source)
Will return true or false if an image source was added to the chain.
bool getBoolKeywordValue(bool &rtn_val, const char *keyword, const char *prefix=0) const
[OLK, Aug/2008] Sets the boolean <rtn_val> depending on value associated with keyword for values = (y...
void setMinPixelValue(double min)
Set the min output pixel.
virtual void initProcessingChain()
Derived classes initialize their custom chains here.
Low-level OSSIM interface to HDF5 libraries.
Definition: ossimHdf5.h:27
const ossimProjection * getProjection() const
Access methods for projection (may be NULL pointer).
TIFF info class.
Definition: ossimHdf5Info.h:33
ossimScalarType m_productScalarType
void setDescription(const ossimString &desc)
bool m_listNdimDatasets
Definition: ossimHdf5Tool.h:69
bool saveCoarseGrid(const ossimFilename &cgFileName) const
Saves the coarse grid to the specified file.
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
bool empty() const
Definition: ossimString.h:411
virtual bool initialize(ossimArgumentParser &ap)
Initializes from command line arguments.
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Saves the transform (if any) and projection (if any) states to the KWL.
static ossimImageHandlerRegistry * instance()
ossimString m_geomDataPath
Definition: ossimHdf5Tool.h:65
static bool getNdimDatasets(H5::Group group, std::vector< H5::DataSet > &datasetList, bool recursive=false)
Assigns list of all multi-dimensional datasets under current active group.
Definition: ossimHdf5.cpp:188
static const char * IMAGE_FILE_KW
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
int & argc()
return the argument count.
virtual bool execute()
Performs the actual product write.
void addGeoPolyCutterPolygon(const vector< ossimGpt > &polygon)
const std::string & string() const
Definition: ossimString.h:414