bes  Updated for version 3.20.10
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 <libdap/InternalErr.h>
34 #include "HDF5CFModule.h"
35 #include "h5apicompatible.h"
36 
37 using namespace libdap;
38 
39 H5CFModule check_module(hid_t fileid) {
40 
41  if (true == check_eos5(fileid))
42  return HDF_EOS5;
43  else if(true == check_jpss(fileid))
44  return HDF5_JPSS;
45  else
46  return HDF5_GENERAL;
47 
48 }
49 
50 bool check_eos5(hid_t file_id) {
51 
52  // check HDF-EOS5 group
53  string eos5_check_group = "/HDFEOS INFORMATION";
54  string eos5_check_attr = "HDFEOSVersion";
55  string eos5_dataset = "StructMetadata.0";
56  htri_t has_eos_group = -1;
57  bool eos5_module_fields = true;
58 
59 
60  has_eos_group = H5Lexists(file_id,eos5_check_group.c_str(),H5P_DEFAULT);
61 
62  if (has_eos_group > 0){
63 
64  hid_t eos_group_id = -1;
65  htri_t has_eos_attr = -1;
66 
67  // Open the group
68  if((eos_group_id = H5Gopen(file_id, eos5_check_group.c_str(),H5P_DEFAULT))<0) {
69 
70  string msg = "cannot open the HDF5 group ";
71  msg += eos5_check_group;
72  throw InternalErr(__FILE__, __LINE__, msg);
73  }
74 
75  // check the existence of the EOS5 attribute
76  has_eos_attr = H5Aexists(eos_group_id, eos5_check_attr.c_str());
77 
78  if (has_eos_attr >0) {
79 
80  // All HDF-EOS5 conditions are fulfilled, return true;
81  // Otherwise, return false or throw an error.
82  htri_t has_eos_dset = -1;
83 
84  // check the existence of the EOS5 dataset
85  has_eos_dset = H5Lexists(eos_group_id,eos5_dataset.c_str(),H5P_DEFAULT);
86  if (has_eos_dset >0) {
87  // We still need to check if there are non-EOS5 fields that the
88  // current HDF-EOS5 module cannot handle.
89  // If yes, the file cannot be handled by the HDF-EOS5 module since
90  // the current module is very tight to the HDF-EOS5 model.
91  // We will treat this file as a general HDF5 file.
92  eos5_module_fields = check_eos5_module_fields(file_id);
93  return eos5_module_fields;
94  }
95  else if(0 == has_eos_dset)
96  return false;
97  else {
98  string msg = "Fail to determine if the HDF5 dataset ";
99  msg += eos5_dataset;
100  msg +=" exists ";
101  H5Gclose(eos_group_id);
102  throw InternalErr(__FILE__, __LINE__, msg);
103  }
104  }
105  else if(0 == has_eos_attr)
106  return false;
107  else {
108  string msg = "Fail to determine if the HDF5 attribute ";
109  msg += eos5_check_attr;
110  msg +=" exists ";
111  H5Gclose(eos_group_id);
112  throw InternalErr(__FILE__, __LINE__, msg);
113  }
114  }
115  else if( 0 == has_eos_group) {
116  return false;
117  }
118  else {
119  string msg = "Fail to determine if the HDF5 group ";
120  msg += eos5_check_group;
121  msg +=" exists ";
122 #if 0
123 // H5Fclose(file_id);
124 #endif
125  throw InternalErr(__FILE__, __LINE__, msg);
126  }
127 }
128 
129 bool check_jpss(hid_t fileid) {
130  // Currently not supported.
131  return false;
132 }
133 
134 bool check_eos5_module_fields(hid_t fileid){
135 
136  bool ret_value = true;
137  string eos5_swath_group = "/HDFEOS/SWATHS";
138  string eos5_grid_group = "/HDFEOS/GRIDS";
139  string eos5_zas_group = "/HDFEOS/ZAS";
140  bool swath_unsupported_dset = false;
141  bool grid_unsupported_dset = false;
142  bool zas_unsupported_dset = false;
143  if(H5Lexists(fileid,eos5_swath_group.c_str(),H5P_DEFAULT)>0)
144  swath_unsupported_dset = grp_has_dset(fileid,eos5_swath_group);
145  if(swath_unsupported_dset == true)
146  return false;
147  else {
148  if(H5Lexists(fileid,eos5_grid_group.c_str(),H5P_DEFAULT)>0)
149  grid_unsupported_dset = grp_has_dset(fileid,eos5_grid_group);
150  if(grid_unsupported_dset == true)
151  return false;
152  else {
153  if(H5Lexists(fileid,eos5_zas_group.c_str(),H5P_DEFAULT)>0)
154  zas_unsupported_dset = grp_has_dset(fileid,eos5_zas_group);
155  if(zas_unsupported_dset == true)
156  return false;
157  }
158  }
159 
160  return ret_value;
161 }
162 
163 bool grp_has_dset(hid_t fileid, const string & grp_path ) {
164 
165  bool ret_value = false;
166  hid_t pid = -1;
167  if((pid = H5Gopen(fileid,grp_path.c_str(),H5P_DEFAULT))<0){
168  string msg = "Unable to open the HDF5 group ";
169  msg += grp_path;
170  throw InternalErr(__FILE__, __LINE__, msg);
171  }
172 
173  H5G_info_t g_info;
174 
175  if (H5Gget_info(pid, &g_info) < 0) {
176  H5Gclose(pid);
177  string msg = "Unable to obtain the HDF5 group info. for ";
178  msg += grp_path;
179  throw InternalErr(__FILE__, __LINE__, msg);
180  }
181 
182  hsize_t nelems = g_info.nlinks;
183 
184  for (hsize_t i = 0; i < nelems; i++) {
185 
186  // Obtain the object type
187  H5O_info_t oinfo;
188  if (H5OGET_INFO_BY_IDX(pid, ".", H5_INDEX_NAME, H5_ITER_NATIVE, i, &oinfo, H5P_DEFAULT) < 0) {
189  string msg = "Cannot obtain the object info for the group";
190  msg += grp_path;
191  throw InternalErr(__FILE__, __LINE__, msg);
192  }
193 
194  if(oinfo.type == H5O_TYPE_DATASET) {
195  ret_value = true;
196  break;
197  }
198  }
199  H5Gclose(pid);
200  return ret_value;
201 }
This class describes the different categories of HDF5 products for the CF option.