bes  Updated for version 3.20.5
hcstream.h
1 #ifndef _HCSTREAM_H
2 #define _HCSTREAM_H
3 
5 // This file is part of the hdf4 data handler for the OPeNDAP data server.
6 
7 // Copyright (c) 2005 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This is free software; you can redistribute it and/or modify it under the
11 // terms of the GNU Lesser General Public License as published by the Free
12 // Software Foundation; either version 2.1 of the License, or (at your
13 // option) any later version.
14 //
15 // This software is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 // License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with this software; if not, write to the Free Software Foundation,
22 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 // Copyright 1996, by the California Institute of Technology.
27 // ALL RIGHTS RESERVED. United States Government Sponsorship
28 // acknowledged. Any commercial use must be negotiated with the
29 // Office of Technology Transfer at the California Institute of
30 // Technology. This software may be subject to U.S. export control
31 // laws and regulations. By accepting this software, the user
32 // agrees to comply with all applicable U.S. export laws and
33 // regulations. User has the responsibility to obtain export
34 // licenses, or other export authority as may be required before
35 // exporting such information to foreign countries or providing
36 // access to foreign persons.
37 
38 // U.S. Government Sponsorship under NASA Contract
39 // NAS7-1260 is acknowledged.
40 //
41 // Author: Todd.K.Karakashian@jpl.nasa.gov
42 //
44 
45 #include <string>
46 #include <vector>
47 
48 #include <hcerr.h>
49 #include <hdfclass.h>
50 
51 class hdfistream_obj { // base class for streams reading HDF objects
52  public:
53  explicit hdfistream_obj(const string filename = "") {
54  _init(filename);
55  }
57  THROW(hcerr_copystream);
58  }
59  virtual ~hdfistream_obj(void) {
60  }
61  void operator=(const hdfistream_obj &) {
62  THROW(hcerr_copystream);
63  }
64  virtual void open(const char *filename = 0) = 0; // open stream
65  virtual void close(void) = 0; // close stream
66  virtual void seek(int index = 0) = 0; // seek to index'th object
67  virtual void seek_next(void) = 0; // seek to next object in stream
68  virtual void rewind(void) = 0; // rewind to beginning of stream
69  virtual bool bos(void) const = 0; // are we at beginning of stream?
70  virtual bool eos(void) const = 0; // are we at end of stream?
71  virtual int index(void) const {
72  return _index;
73  } // return current position protected:
74  void _init(const string filename = "") {
75  if (filename.length())
76  _filename = filename;
77  _file_id = _index = 0;
78  }
79  string _filename;
80  int32 _file_id;
81  int _index;
82 };
83 
85 
86  public:
87  explicit hdfistream_sds(const string filename = "");
89  THROW(hcerr_copystream);
90  }
91  virtual ~ hdfistream_sds(void) {
92  _del();
93  }
94  void operator=(const hdfistream_sds &) {
95  THROW(hcerr_copystream);
96  }
97  virtual void open(const char *filename = 0); // open stream, seek to BOS
98  virtual void close(void); // close stream
99  virtual void seek(int index = 0); // seek the index'th SDS array
100  virtual void seek(const char *name); // seek the SDS array by name
101  virtual void seek_next(void); // seek the next SDS array
102  virtual void seek_ref(int ref); // seek the SDS array by ref
103  virtual void rewind(void); // position in front of first SDS
104  virtual bool bos(void) const; // positioned in front of the first SDS?
105  virtual bool eos(void) const; // positioned past the last SDS?
106  virtual bool eo_attr(void) const; // positioned past the last attribute?
107  virtual bool eo_dim(void) const; // positioned past the last dimension?
108  void setmeta(bool val) {
109  _meta = val;
110  } // set metadata loading
111  void setslab(vector < int >start, vector < int >edge,
112  vector < int >stride, bool reduce_rank = false);
113  void setslab(int *start, int *edge, int *stride, bool reduce_rank =
114  false);
115  void unsetslab(void) {
116  _slab.set = _slab.reduce_rank = false;
117  }
118  void set_map_ce(const vector < array_ce > &a_ce) {
119  _map_ce_set = true;
120  _map_ce_vec = a_ce;
121  }
122  vector < array_ce > get_map_ce() {
123  return _map_ce_vec;
124  }
125  bool is_map_ce_set() {
126  return _map_ce_set;
127  }
128  hdfistream_sds & operator>>(hdf_attr & ha); // read an attribute
129  hdfistream_sds & operator>>(vector < hdf_attr > &hav); // read all atributes
130  hdfistream_sds & operator>>(hdf_sds & hs); // read an SDS
131  hdfistream_sds & operator>>(vector < hdf_sds > &hsv); // read all SDS's
132  hdfistream_sds & operator>>(hdf_dim & hd); // read a dimension
133  hdfistream_sds & operator>>(vector < hdf_dim > &hdv); // read all dims
134  protected:
135  void _init(void);
136  void _del(void) {
137  close();
138  }
139  void _get_fileinfo(void); // get SDS info for the current file
140  void _get_sdsinfo(void); // get info about the current SDS
141  void _close_sds(void); // close the open SDS
142  void _seek_next_arr(void); // find the next SDS array in the stream
143  void _seek_arr(int index); // find the index'th SDS array in the stream
144  void _seek_arr(const string & name); // find the SDS array w/ specified name
145  void _seek_arr_ref(int ref); // find the SDS array in the stream by ref
146  void _rewind(void) {
147  _index = -1;
148  _attr_index = _dim_index = 0;
149  }
150  // position to the beginning of the stream
151  static const string long_name; // label
152  static const string units;
153  static const string format;
154  int32 _sds_id; // handle of open object in annotation interface
155  int32 _attr_index; // index of current attribute
156  int32 _dim_index; // index of current dimension
157  int32 _rank; // number of dimensions in SDS
158  int32 _nattrs; // number of attributes for this SDS
159  int32 _nsds; // number of SDS's in stream
160  int32 _nfattrs; // number of file attributes in this SDS's file
161  bool _meta;
162  struct slab {
163  bool set;
164  bool reduce_rank;
165  int32 start[hdfclass::MAXDIMS];
166  int32 edge[hdfclass::MAXDIMS];
167  int32 stride[hdfclass::MAXDIMS];
168  } _slab;
169  // Since a SDS can hold a Grid, there may be several different
170  // constraints (because a client might constrain each of the fields
171  // differently and want a Structure object back). I've added a vector of
172  // array_ce objects to hold all this CE information so that the
173  // operator>> method will be able to access it at the correct time. This
174  // new object holds only the information about constraints on the Grid's
175  // map vectors. The _slab member will hold the constraint on the Grid's
176  // array. Note that in many cases the constraints on the maps can be
177  // derived from the array's constraints, but sometimes a client will only
178  // ask for the maps and thus the array's constraint will be the entire
179  // array (the `default constraint').
180  vector < array_ce > _map_ce_vec; // Added 2/5/2002 jhrg
181  bool _map_ce_set;
182 };
183 
184 // class for a stream reading annotations
186  public:
187  hdfistream_annot(const string filename = "");
188  hdfistream_annot(const string filename, int32 tag, int32 ref);
190  THROW(hcerr_copystream);
191  }
192  virtual ~ hdfistream_annot(void) {
193  _del();
194  }
195  void operator=(const hdfistream_annot &) {
196  THROW(hcerr_copystream);
197  }
198  virtual void open(const char *filename); // open file annotations
199  virtual void open(const char *filename, int32 tag, int32 ref);
200  // open tag/ref in file, seek to BOS
201  virtual void close(void); // close open file
202  virtual void seek(int index) // seek to index'th annot in stream
203  {
204  _index = index;
205  }
206  virtual void seek_next(void) {
207  _index++;
208  } // position to next annot
209  virtual bool eos(void) const {
210  return (_index >= (int) _an_ids.size());
211  }
212  virtual bool bos(void) const {
213  return (_index <= 0);
214  }
215  virtual void rewind(void) {
216  _index = 0;
217  }
218  virtual void set_annot_type(bool label, bool desc) // specify types of
219  {
220  _lab = label;
221  _desc = desc;
222  } // annots to read
223  hdfistream_annot & operator>>(string & an); // read current annotation
224  hdfistream_annot & operator>>(vector < string > &anv); // read all annots
225  protected:
226  void _init(const string filename = "");
227  void _init(const string filename, int32 tag, int32 ref);
228  void _del(void) {
229  close();
230  }
231  void _rewind(void) {
232  _index = 0;
233  }
234  void _get_anninfo(void);
235  void _get_file_anninfo(void);
236  void _get_obj_anninfo(void);
237  void _open(const char *filename);
238  int32 _an_id; // handle of open annotation interface
239  int32 _tag, _ref; // tag, ref currently pointed to
240  bool _lab; // if true, read labels
241  bool _desc; // if true, read descriptions
242  vector < int32 > _an_ids; // list of id's of anns in stream
243 };
244 
246  public:
247  hdfistream_vdata(const string filename = "");
249  THROW(hcerr_copystream);
250  }
251  virtual ~hdfistream_vdata(void) {
252  _del();
253  }
254  void operator=(const hdfistream_vdata &) {
255  THROW(hcerr_copystream);
256  }
257  virtual void open(const char *filename); // open stream, seek to BOS
258  virtual void open(const string & filename); // open stream, seek to BOS
259  virtual void close(void); // close stream
260  virtual void seek(int index = 0); // seek the index'th Vdata
261  virtual void seek(const char *name); // seek the Vdata by name
262  virtual void seek(const string & name); // seek the Vdata by name
263  virtual void seek_next(void) {
264  _seek_next();
265  } // seek the next Vdata in file
266  virtual void seek_ref(int ref); // seek the Vdata by ref
267  virtual void rewind(void) {
268  _rewind();
269  } // position in front of first Vdata
270  virtual bool bos(void) const // positioned in front of the first Vdata?
271  {
272  return (_index <= 0);
273  }
274  virtual bool eos(void) const // positioned past the last Vdata?
275  {
276  return (_index >= (int) _vdata_refs.size());
277  }
278  virtual bool eo_attr(void) const; // positioned past the last attribute?
279  void setmeta(bool val) {
280  _meta = val;
281  } // set metadata loading
282  bool setrecs(int32 begin, int32 end);
283  hdfistream_vdata & operator>>(hdf_vdata & hs); // read a Vdata
284  hdfistream_vdata & operator>>(vector < hdf_vdata > &hsv); // read all Vdata's
285  hdfistream_vdata & operator>>(hdf_attr & ha); // read an attribute
286  hdfistream_vdata & operator>>(vector < hdf_attr > &hav); // read all attributes
287  virtual bool isInternalVdata(int ref) const; // check reference against internal type
288  protected:
289  void _init(void);
290  void _del(void) {
291  close();
292  }
293  void _get_fileinfo(void); // get Vdata info for the current file
294  void _seek_next(void); // find the next Vdata in the stream
295  void _seek(const char *name); // find the Vdata w/ specified name
296  void _seek(int32 ref); // find the index'th Vdata in the stream
297  void _rewind(void) // position to beginning of the stream
298  {
299  _index = _attr_index = 0;
300  if (_vdata_refs.size() > 0)
301  _seek(_vdata_refs[0]);
302  }
303  int32 _vdata_id; // handle of open object in annotation interface
304  int32 _attr_index; // index of current attribute
305  int32 _nattrs; // number of attributes for this Vdata
306  hdfistream_vdata & operator>>(hdf_field & hf); // read a field
307  hdfistream_vdata & operator>>(vector < hdf_field > &hfv); // read all fields
308  bool _meta;
309  vector < int32 > _vdata_refs; // list of refs for Vdata's in the file
310  struct {
311  bool set;
312  int32 begin;
313  int32 end;
314  } _recs;
315 };
316 
318  public:
319  explicit hdfistream_vgroup(const string filename = "");
321  THROW(hcerr_copystream);
322  }
323  virtual ~hdfistream_vgroup(void) {
324  _del();
325  }
326  void operator=(const hdfistream_vgroup &) {
327  THROW(hcerr_copystream);
328  }
329  virtual void open(const char *filename); // open stream, seek to BOS
330  virtual void open(const string & filename); // open stream, seek to BOS
331  virtual void close(void); // close stream
332  virtual void seek(int index = 0); // seek the index'th Vgroup
333  virtual void seek(const char *name); // seek the Vgroup by name
334  virtual void seek(const string & name); // seek the Vgroup by name
335  virtual void seek_next(void) {
336  _seek_next();
337  } // seek the next Vgroup in file
338  virtual void seek_ref(int ref); // seek the Vgroup by ref
339  virtual void rewind(void) {
340  _rewind();
341  } // position in front of first Vgroup
342  string memberName(int32 ref); // find the name of ref'd Vgroup in the stream
343  virtual bool bos(void) const // positioned in front of the first Vgroup?
344  {
345  return (_index <= 0);
346  }
347  virtual bool eos(void) const // positioned past the last Vgroup?
348  {
349  return (_index >= (int) _vgroup_refs.size());
350  }
351  virtual bool eo_attr(void) const; // positioned past the last attribute?
352  void setmeta(bool val) {
353  _meta = val;
354  } // set metadata loading
355  hdfistream_vgroup & operator>>(hdf_vgroup & hs); // read a Vgroup
356  hdfistream_vgroup & operator>>(vector < hdf_vgroup > &hsv); // read all Vgroup's
357  hdfistream_vgroup & operator>>(hdf_attr & ha); // read an attribute
358  hdfistream_vgroup & operator>>(vector < hdf_attr > &hav); // read all attributes
359  protected:
360  void _init(void);
361  void _del(void) {
362  close();
363  }
364  void _get_fileinfo(void); // get Vgroup info for the current file
365  void _seek_next(void); // find the next Vgroup in the stream
366  void _seek(const char *name); // find the Vgroup w/ specified name
367  void _seek(int32 ref); // find the index'th Vgroup in the stream
368  void _rewind(void) // position to beginning of the stream
369  {
370  _index = _attr_index = 0;
371  if (_vgroup_refs.size() > 0)
372  _seek(_vgroup_refs[0]);
373  }
374  string _memberName(int32 ref); // find the name of ref'd Vgroup in the stream
375  int32 _vgroup_id; // handle of open object in annotation interface
376 #if 0
377  int32 _member_id; // handle of child object in this Vgroup
378 #endif
379  int32 _attr_index; // index of current attribute
380  int32 _nattrs; // number of attributes for this Vgroup
381  bool _meta;
382  vector < int32 > _vgroup_refs; // list of refs for Vgroup's in the file
383  struct {
384  bool set;
385  int32 begin;
386  int32 end;
387  } _recs;
388 };
389 
390 // Raster input stream class
392  public:
393  hdfistream_gri(const string filename = "");
395  THROW(hcerr_copystream);
396  }
397  virtual ~ hdfistream_gri(void) {
398  _del();
399  }
400  void operator=(const hdfistream_gri &) {
401  THROW(hcerr_copystream);
402  }
403  virtual void open(const char *filename = 0); // open stream
404  virtual void close(void); // close stream
405  virtual void seek(int index = 0); // seek the index'th image
406  virtual void seek(const char *name); // seek image by name
407  virtual void seek_next(void) {
408  seek(_index + 1);
409  } // seek the next RI
410  virtual void seek_ref(int ref); // seek the RI by ref
411  virtual void rewind(void); // position in front of first RI
412  virtual bool bos(void) const; // position in front of first RI?
413  virtual bool eos(void) const; // position past last RI?
414  virtual bool eo_attr(void) const; // positioned past last attribute?
415  virtual bool eo_pal(void) const; // positioned past last palette?
416  void setmeta(bool val) {
417  _meta = val;
418  } // set metadata loading
419  void setslab(vector < int >start, vector < int >edge,
420  vector < int >stride, bool reduce_rank = false);
421  void unsetslab(void) {
422  _slab.set = _slab.reduce_rank = false;
423  }
424  void setinterlace(int32 interlace_mode); // set interlace type for next read
425  hdfistream_gri & operator>>(hdf_gri & hr); // read a single RI
426  hdfistream_gri & operator>>(vector < hdf_gri > &hrv); // read all RI's
427  hdfistream_gri & operator>>(hdf_attr & ha); // read an attribute
428  hdfistream_gri & operator>>(vector < hdf_attr > &hav); // read all attributes
429  hdfistream_gri & operator>>(hdf_palette & hp); // read a palette
430  hdfistream_gri & operator>>(vector < hdf_palette > &hpv); // read all palettes
431  protected:
432  void _init(void);
433  void _del(void) {
434  close();
435  }
436  void _get_fileinfo(void); // get image info for the current file
437  void _get_iminfo(void); // get info about the current RI
438  void _close_ri(void); // close the current RI
439  void _rewind(void) {
440  _index = -1;
441  _attr_index = _pal_index = 0;
442  }
443  int32 _gr_id; // GR interface id -> can't get rid of this, needed for GRend()
444  int32 _ri_id; // handle for open raster object
445  int32 _attr_index; // index to current attribute
446  int32 _pal_index; // index to current palette
447  int32 _nri; // number of rasters in the stream
448  int32 _nattrs; // number of attributes for this RI
449  int32 _nfattrs; // number of file attributes in this RI's file
450  int32 _npals; // number of palettes, set to one for now
451  int32 _interlace_mode; // interlace mode for reading images
452  bool _meta; // metadata only
453  struct {
454  bool set;
455  bool reduce_rank;
456  int32 start[2];
457  int32 edge[2];
458  int32 stride[2];
459  } _slab;
460 }; /* Note: multiple palettes is not supported in the current HDF 4.0 GR API */
461 
462 #endif // ifndef _HCSTREAM_H
463 
hdfistream_gri
Definition: hcstream.h:391
hdf_field
Definition: hdfclass.h:193
hdf_vgroup
Definition: hdfclass.h:218
hdfistream_sds::slab
Definition: hcstream.h:162
hdfistream_obj
Definition: hcstream.h:51
hdf_sds
Definition: hdfclass.h:179
hdf_dim
Definition: hdfclass.h:167
hdf_gri
Definition: hdfclass.h:243
hdf_vdata
Definition: hdfclass.h:204
hdf_palette
Definition: hdfclass.h:234
hdfistream_sds
Definition: hcstream.h:84
hdfistream_vdata
Definition: hcstream.h:245
hdfistream_annot
Definition: hcstream.h:185
hdf_attr
Definition: hdfclass.h:149
hcerr_copystream
Definition: hcerr.h:118
hdfistream_vgroup
Definition: hcstream.h:317