bes  Updated for version 3.20.6
HDF5CFModule.cc
Go to the documentation of this file.
1 // This file is part of the hdf5_handler implementing for the CF-compliant
2 // Copyright (c) 2011-2016 The HDF Group, Inc. and OPeNDAP, Inc.
3 //
4 // This is free software; you can redistribute it and/or modify it under the
5 // terms of the GNU Lesser General Public License as published by the Free
6 // Software Foundation; either version 2.1 of the License, or (at your
7 // option) any later version.
8 //
9 // This software is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 // License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 //
18 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
19 // You can contact The HDF Group, Inc. at 1800 South Oak Street,
20 // Suite 203, Champaign, IL 61820
21 
32 
33 #include <InternalErr.h>
34 #include "HDF5CFModule.h"
35 
36 using namespace libdap;
37 
38 H5CFModule check_module(hid_t fileid) {
39 
40  if (true == check_eos5(fileid))
41  return HDF_EOS5;
42  else if(true == check_jpss(fileid))
43  return HDF5_JPSS;
44  else
45  return HDF5_GENERAL;
46 
47 }
48 
49 bool check_eos5(hid_t file_id) {
50 
51  // check HDF-EOS5 group
52  string eos5_check_group = "/HDFEOS INFORMATION";
53  string eos5_check_attr = "HDFEOSVersion";
54  string eos5_dataset = "StructMetadata.0";
55  htri_t has_eos_group = -1;
56  bool eos5_module_fields = true;
57 
58 
59  has_eos_group = H5Lexists(file_id,eos5_check_group.c_str(),H5P_DEFAULT);
60 
61  if (has_eos_group > 0){
62 
63  hid_t eos_group_id = -1;
64  htri_t has_eos_attr = -1;
65 
66  // Open the group
67  if((eos_group_id = H5Gopen(file_id, eos5_check_group.c_str(),H5P_DEFAULT))<0) {
68 
69  string msg = "cannot open the HDF5 group ";
70  msg += eos5_check_group;
71  throw InternalErr(__FILE__, __LINE__, msg);
72  }
73 
74  // check the existence of the EOS5 attribute
75  has_eos_attr = H5Aexists(eos_group_id, eos5_check_attr.c_str());
76 
77  if (has_eos_attr >0) {
78 
79  // All HDF-EOS5 conditions are fulfilled, return true;
80  // Otherwise, return false or throw an error.
81  htri_t has_eos_dset = -1;
82 
83  // check the existence of the EOS5 dataset
84  has_eos_dset = H5Lexists(eos_group_id,eos5_dataset.c_str(),H5P_DEFAULT);
85  if (has_eos_dset >0) {
86  // We still need to check if there are non-EOS5 fields that the
87  // current HDF-EOS5 module cannot handle.
88  // If yes, the file cannot be handled by the HDF-EOS5 module since
89  // the current module is very tight to the HDF-EOS5 model.
90  // We will treat this file as a general HDF5 file.
91  eos5_module_fields = check_eos5_module_fields(file_id);
92  return eos5_module_fields;
93  }
94  else if(0 == has_eos_dset)
95  return false;
96  else {
97  string msg = "Fail to determine if the HDF5 dataset ";
98  msg += eos5_dataset;
99  msg +=" exists ";
100  H5Gclose(eos_group_id);
101  throw InternalErr(__FILE__, __LINE__, msg);
102  }
103  }
104  else if(0 == has_eos_attr)
105  return false;
106  else {
107  string msg = "Fail to determine if the HDF5 attribute ";
108  msg += eos5_check_attr;
109  msg +=" exists ";
110  H5Gclose(eos_group_id);
111  throw InternalErr(__FILE__, __LINE__, msg);
112  }
113  }
114  else if( 0 == has_eos_group) {
115  return false;
116  }
117  else {
118  string msg = "Fail to determine if the HDF5 group ";
119  msg += eos5_check_group;
120  msg +=" exists ";
121 #if 0
122 // H5Fclose(file_id);
123 #endif
124  throw InternalErr(__FILE__, __LINE__, msg);
125  }
126 }
127 
128 bool check_jpss(hid_t fileid) {
129  // Currently not supported.
130  return false;
131 }
132 
133 bool check_eos5_module_fields(hid_t fileid){
134 
135  bool ret_value = true;
136  string eos5_swath_group = "/HDFEOS/SWATHS";
137  string eos5_grid_group = "/HDFEOS/GRIDS";
138  string eos5_zas_group = "/HDFEOS/ZAS";
139  bool swath_unsupported_dset = false;
140  bool grid_unsupported_dset = false;
141  bool zas_unsupported_dset = false;
142  if(H5Lexists(fileid,eos5_swath_group.c_str(),H5P_DEFAULT)>0)
143  swath_unsupported_dset = grp_has_dset(fileid,eos5_swath_group);
144  if(swath_unsupported_dset == true)
145  return false;
146  else {
147  if(H5Lexists(fileid,eos5_grid_group.c_str(),H5P_DEFAULT)>0)
148  grid_unsupported_dset = grp_has_dset(fileid,eos5_grid_group);
149  if(grid_unsupported_dset == true)
150  return false;
151  else {
152  if(H5Lexists(fileid,eos5_zas_group.c_str(),H5P_DEFAULT)>0)
153  zas_unsupported_dset = grp_has_dset(fileid,eos5_zas_group);
154  if(zas_unsupported_dset == true)
155  return false;
156  }
157  }
158 
159  return ret_value;
160 }
161 
162 bool grp_has_dset(hid_t fileid, const string & grp_path ) {
163 
164  bool ret_value = false;
165  hid_t pid = -1;
166  if((pid = H5Gopen(fileid,grp_path.c_str(),H5P_DEFAULT))<0){
167  string msg = "Unable to open the HDF5 group ";
168  msg += grp_path;
169  throw InternalErr(__FILE__, __LINE__, msg);
170  }
171 
172  H5G_info_t g_info;
173 
174  if (H5Gget_info(pid, &g_info) < 0) {
175  H5Gclose(pid);
176  string msg = "Unable to obtain the HDF5 group info. for ";
177  msg += grp_path;
178  throw InternalErr(__FILE__, __LINE__, msg);
179  }
180 
181  hsize_t nelems = g_info.nlinks;
182 
183  for (hsize_t i = 0; i < nelems; i++) {
184 
185  // Obtain the object type
186  H5O_info_t oinfo;
187  if (H5Oget_info_by_idx(pid, ".", H5_INDEX_NAME, H5_ITER_NATIVE, i, &oinfo, H5P_DEFAULT) < 0) {
188  string msg = "Cannot obtain the object info for the group";
189  msg += grp_path;
190  throw InternalErr(__FILE__, __LINE__, msg);
191  }
192 
193  if(oinfo.type == H5O_TYPE_DATASET) {
194  ret_value = true;
195  break;
196  }
197  }
198  H5Gclose(pid);
199  return ret_value;
200 }
libdap
Definition: BESDapFunctionResponseCache.h:35
HDF5CFModule.h
This class describes the different categories of HDF5 products for the CF option.